blob: d8cc8127f19c18f9b6a4ea9f937015363b912bd8 [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
24#include <linux/linkage.h>
25#include <linux/interrupt.h>
26#include <linux/irq.h>
27#include <linux/module.h>
28#include <linux/string.h>
Christophe Saout28e08862009-01-11 11:46:23 -080029#include <linux/bootmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -040031#include <linux/irqnr.h>
Qing Hef731e3ef2010-10-11 15:30:09 +010032#include <linux/pci.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070033
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +000034#ifdef CONFIG_X86
Sheng Yang38e20b02010-05-14 12:40:51 +010035#include <asm/desc.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070036#include <asm/ptrace.h>
37#include <asm/irq.h>
Jeremy Fitzhardinge792dc4f2009-02-06 14:09:43 -080038#include <asm/idle.h>
Konrad Rzeszutek Wilk0794bfc2010-10-18 10:41:08 -040039#include <asm/io_apic.h>
Stefano Stabellini9846ff12012-01-30 16:21:48 +000040#include <asm/xen/page.h>
Stefano Stabellini42a1de52010-06-24 16:42:04 +010041#include <asm/xen/pci.h>
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +000042#endif
43#include <asm/sync_bitops.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070044#include <asm/xen/hypercall.h>
Adrian Bunk8d1b8752007-07-20 00:31:44 -070045#include <asm/xen/hypervisor.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070046
Sheng Yang38e20b02010-05-14 12:40:51 +010047#include <xen/xen.h>
48#include <xen/hvm.h>
Isaku Yamahatae04d0d02008-04-02 10:53:55 -070049#include <xen/xen-ops.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070050#include <xen/events.h>
51#include <xen/interface/xen.h>
52#include <xen/interface/event_channel.h>
Sheng Yang38e20b02010-05-14 12:40:51 +010053#include <xen/interface/hvm/hvm_op.h>
54#include <xen/interface/hvm/params.h>
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +000055#include <xen/interface/physdev.h>
56#include <xen/interface/sched.h>
57#include <asm/hw_irq.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070058
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070059/*
60 * This lock protects updates to the following mapping and reference-count
61 * arrays. The lock does not need to be acquired to read the mapping tables.
62 */
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -040063static DEFINE_MUTEX(irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070064
Ian Campbell6cb65372011-03-10 16:08:11 +000065static LIST_HEAD(xen_irq_list_head);
66
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070067/* IRQ <-> VIRQ mapping. */
Tejun Heo204fba42009-06-24 15:13:45 +090068static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070069
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070070/* IRQ <-> IPI mapping */
Tejun Heo204fba42009-06-24 15:13:45 +090071static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070072
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080073/* Interrupt types. */
74enum xen_irq_type {
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -080075 IRQT_UNBOUND = 0,
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070076 IRQT_PIRQ,
77 IRQT_VIRQ,
78 IRQT_IPI,
79 IRQT_EVTCHN
80};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070081
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080082/*
83 * Packed IRQ information:
84 * type - enum xen_irq_type
85 * event channel - irq->event channel mapping
86 * cpu - cpu this event channel is bound to
87 * index - type-specific information:
Jan Beulichdec02de2013-04-03 15:52:50 +010088 * PIRQ - physical IRQ, GSI, flags, and owner domain
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080089 * VIRQ - virq number
90 * IPI - IPI vector
91 * EVTCHN -
92 */
Ruslan Pisarev088c05a2011-07-26 14:16:13 +030093struct irq_info {
Ian Campbell6cb65372011-03-10 16:08:11 +000094 struct list_head list;
Daniel De Graaf420eb552011-10-27 17:58:47 -040095 int refcnt;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080096 enum xen_irq_type type; /* type */
Ian Campbell6cb65372011-03-10 16:08:11 +000097 unsigned irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080098 unsigned short evtchn; /* event channel */
99 unsigned short cpu; /* cpu bound */
100
101 union {
102 unsigned short virq;
103 enum ipi_vector ipi;
104 struct {
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100105 unsigned short pirq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800106 unsigned short gsi;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400107 unsigned char flags;
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400108 uint16_t domid;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800109 } pirq;
110 } u;
111};
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400112#define PIRQ_NEEDS_EOI (1 << 0)
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400113#define PIRQ_SHAREABLE (1 << 1)
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800114
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -0400115static int *evtchn_to_irq;
Ian Campbellbf86ad82012-10-17 09:39:12 +0100116#ifdef CONFIG_X86
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000117static unsigned long *pirq_eoi_map;
Ian Campbellbf86ad82012-10-17 09:39:12 +0100118#endif
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000119static bool (*pirq_needs_eoi)(unsigned irq);
Jeremy Fitzhardinge3b32f572009-08-13 12:50:37 -0700120
Ian Campbellc81611c2013-02-20 11:48:06 +0000121/*
122 * Note sizeof(xen_ulong_t) can be more than sizeof(unsigned long). Be
123 * careful to only use bitops which allow for this (e.g
124 * test_bit/find_first_bit and friends but not __ffs) and to pass
125 * BITS_PER_EVTCHN_WORD as the bitmask length.
126 */
127#define BITS_PER_EVTCHN_WORD (sizeof(xen_ulong_t)*8)
128/*
129 * Make a bitmask (i.e. unsigned long *) of a xen_ulong_t
130 * array. Primarily to avoid long lines (hence the terse name).
131 */
132#define BM(x) (unsigned long *)(x)
133/* Find the first set bit in a evtchn mask */
134#define EVTCHN_FIRST_BIT(w) find_first_bit(BM(&(w)), BITS_PER_EVTCHN_WORD)
135
136static DEFINE_PER_CPU(xen_ulong_t [NR_EVENT_CHANNELS/BITS_PER_EVTCHN_WORD],
Ian Campbellcb60d112011-03-10 16:08:08 +0000137 cpu_evtchn_mask);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700138
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700139/* Xen will never allocate port zero for any purpose. */
140#define VALID_EVTCHN(chn) ((chn) != 0)
141
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700142static struct irq_chip xen_dynamic_chip;
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700143static struct irq_chip xen_percpu_chip;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400144static struct irq_chip xen_pirq_chip;
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100145static void enable_dynirq(struct irq_data *data);
146static void disable_dynirq(struct irq_data *data);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700147
Ian Campbell9158c352011-03-10 16:08:09 +0000148/* Get info for IRQ */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800149static struct irq_info *info_for_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700150{
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100151 return irq_get_handler_data(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700152}
153
Ian Campbell9158c352011-03-10 16:08:09 +0000154/* Constructors for packed IRQ information. */
155static void xen_irq_info_common_init(struct irq_info *info,
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000156 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000157 enum xen_irq_type type,
158 unsigned short evtchn,
159 unsigned short cpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700160{
Ian Campbell9158c352011-03-10 16:08:09 +0000161
162 BUG_ON(info->type != IRQT_UNBOUND && info->type != type);
163
164 info->type = type;
Ian Campbell6cb65372011-03-10 16:08:11 +0000165 info->irq = irq;
Ian Campbell9158c352011-03-10 16:08:09 +0000166 info->evtchn = evtchn;
167 info->cpu = cpu;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000168
169 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700170}
171
Ian Campbell9158c352011-03-10 16:08:09 +0000172static void xen_irq_info_evtchn_init(unsigned irq,
173 unsigned short evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700174{
Ian Campbell9158c352011-03-10 16:08:09 +0000175 struct irq_info *info = info_for_irq(irq);
176
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000177 xen_irq_info_common_init(info, irq, IRQT_EVTCHN, evtchn, 0);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700178}
179
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000180static void xen_irq_info_ipi_init(unsigned cpu,
181 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000182 unsigned short evtchn,
183 enum ipi_vector ipi)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700184{
Ian Campbell9158c352011-03-10 16:08:09 +0000185 struct irq_info *info = info_for_irq(irq);
186
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000187 xen_irq_info_common_init(info, irq, IRQT_IPI, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000188
189 info->u.ipi = ipi;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000190
191 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700192}
193
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000194static void xen_irq_info_virq_init(unsigned cpu,
195 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000196 unsigned short evtchn,
197 unsigned short virq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700198{
Ian Campbell9158c352011-03-10 16:08:09 +0000199 struct irq_info *info = info_for_irq(irq);
200
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000201 xen_irq_info_common_init(info, irq, IRQT_VIRQ, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000202
203 info->u.virq = virq;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000204
205 per_cpu(virq_to_irq, cpu)[virq] = irq;
Ian Campbell9158c352011-03-10 16:08:09 +0000206}
207
208static void xen_irq_info_pirq_init(unsigned irq,
209 unsigned short evtchn,
210 unsigned short pirq,
211 unsigned short gsi,
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400212 uint16_t domid,
Ian Campbell9158c352011-03-10 16:08:09 +0000213 unsigned char flags)
214{
215 struct irq_info *info = info_for_irq(irq);
216
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000217 xen_irq_info_common_init(info, irq, IRQT_PIRQ, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000218
219 info->u.pirq.pirq = pirq;
220 info->u.pirq.gsi = gsi;
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400221 info->u.pirq.domid = domid;
Ian Campbell9158c352011-03-10 16:08:09 +0000222 info->u.pirq.flags = flags;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700223}
224
225/*
226 * Accessors for packed IRQ information.
227 */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800228static unsigned int evtchn_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700229{
Joe Jin110e7c72011-01-07 14:50:12 +0800230 if (unlikely(WARN(irq < 0 || irq >= nr_irqs, "Invalid irq %d!\n", irq)))
231 return 0;
232
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800233 return info_for_irq(irq)->evtchn;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700234}
235
Ian Campbelld4c04532009-02-06 19:20:31 -0800236unsigned irq_from_evtchn(unsigned int evtchn)
237{
238 return evtchn_to_irq[evtchn];
239}
240EXPORT_SYMBOL_GPL(irq_from_evtchn);
241
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800242static enum ipi_vector ipi_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700243{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800244 struct irq_info *info = info_for_irq(irq);
245
246 BUG_ON(info == NULL);
247 BUG_ON(info->type != IRQT_IPI);
248
249 return info->u.ipi;
250}
251
252static unsigned virq_from_irq(unsigned irq)
253{
254 struct irq_info *info = info_for_irq(irq);
255
256 BUG_ON(info == NULL);
257 BUG_ON(info->type != IRQT_VIRQ);
258
259 return info->u.virq;
260}
261
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100262static unsigned pirq_from_irq(unsigned irq)
263{
264 struct irq_info *info = info_for_irq(irq);
265
266 BUG_ON(info == NULL);
267 BUG_ON(info->type != IRQT_PIRQ);
268
269 return info->u.pirq.pirq;
270}
271
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800272static enum xen_irq_type type_from_irq(unsigned irq)
273{
274 return info_for_irq(irq)->type;
275}
276
277static unsigned cpu_from_irq(unsigned irq)
278{
279 return info_for_irq(irq)->cpu;
280}
281
282static unsigned int cpu_from_evtchn(unsigned int evtchn)
283{
284 int irq = evtchn_to_irq[evtchn];
285 unsigned ret = 0;
286
287 if (irq != -1)
288 ret = cpu_from_irq(irq);
289
290 return ret;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700291}
292
Ian Campbellbf86ad82012-10-17 09:39:12 +0100293#ifdef CONFIG_X86
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000294static bool pirq_check_eoi_map(unsigned irq)
295{
Stefano Stabellini521394e2012-04-25 16:11:38 +0100296 return test_bit(pirq_from_irq(irq), pirq_eoi_map);
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000297}
Ian Campbellbf86ad82012-10-17 09:39:12 +0100298#endif
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000299
300static bool pirq_needs_eoi_flag(unsigned irq)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400301{
302 struct irq_info *info = info_for_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400303 BUG_ON(info->type != IRQT_PIRQ);
304
305 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
306}
307
Ian Campbellc81611c2013-02-20 11:48:06 +0000308static inline xen_ulong_t active_evtchns(unsigned int cpu,
309 struct shared_info *sh,
310 unsigned int idx)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700311{
Ruslan Pisarev088c05a2011-07-26 14:16:13 +0300312 return sh->evtchn_pending[idx] &
Ian Campbellcb60d112011-03-10 16:08:08 +0000313 per_cpu(cpu_evtchn_mask, cpu)[idx] &
Ruslan Pisarev088c05a2011-07-26 14:16:13 +0300314 ~sh->evtchn_mask[idx];
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700315}
316
317static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
318{
319 int irq = evtchn_to_irq[chn];
320
321 BUG_ON(irq == -1);
322#ifdef CONFIG_SMP
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000323 cpumask_copy(irq_to_desc(irq)->irq_data.affinity, cpumask_of(cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700324#endif
325
Ian Campbellc81611c2013-02-20 11:48:06 +0000326 clear_bit(chn, BM(per_cpu(cpu_evtchn_mask, cpu_from_irq(irq))));
327 set_bit(chn, BM(per_cpu(cpu_evtchn_mask, cpu)));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700328
Ian Campbellca62ce82011-03-10 16:08:12 +0000329 info_for_irq(irq)->cpu = cpu;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700330}
331
332static void init_evtchn_cpu_bindings(void)
333{
Jan Beulich1c6969e2010-11-16 14:55:33 -0800334 int i;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700335#ifdef CONFIG_SMP
Ian Campbell6cb65372011-03-10 16:08:11 +0000336 struct irq_info *info;
Thomas Gleixner10e58082008-10-16 14:19:04 +0200337
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700338 /* By default all event channels notify CPU#0. */
Ian Campbell6cb65372011-03-10 16:08:11 +0000339 list_for_each_entry(info, &xen_irq_list_head, list) {
340 struct irq_desc *desc = irq_to_desc(info->irq);
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000341 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800342 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700343#endif
344
Jan Beulich1c6969e2010-11-16 14:55:33 -0800345 for_each_possible_cpu(i)
Ian Campbellcb60d112011-03-10 16:08:08 +0000346 memset(per_cpu(cpu_evtchn_mask, i),
347 (i == 0) ? ~0 : 0, sizeof(*per_cpu(cpu_evtchn_mask, i)));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700348}
349
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700350static inline void clear_evtchn(int port)
351{
352 struct shared_info *s = HYPERVISOR_shared_info;
Ian Campbellc81611c2013-02-20 11:48:06 +0000353 sync_clear_bit(port, BM(&s->evtchn_pending[0]));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700354}
355
356static inline void set_evtchn(int port)
357{
358 struct shared_info *s = HYPERVISOR_shared_info;
Ian Campbellc81611c2013-02-20 11:48:06 +0000359 sync_set_bit(port, BM(&s->evtchn_pending[0]));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700360}
361
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700362static inline int test_evtchn(int port)
363{
364 struct shared_info *s = HYPERVISOR_shared_info;
Ian Campbellc81611c2013-02-20 11:48:06 +0000365 return sync_test_bit(port, BM(&s->evtchn_pending[0]));
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700366}
367
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700368
369/**
370 * notify_remote_via_irq - send event to remote end of event channel via irq
371 * @irq: irq of event channel to send event to
372 *
373 * Unlike notify_remote_via_evtchn(), this is safe to use across
374 * save/restore. Notifications on a broken connection are silently
375 * dropped.
376 */
377void notify_remote_via_irq(int irq)
378{
379 int evtchn = evtchn_from_irq(irq);
380
381 if (VALID_EVTCHN(evtchn))
382 notify_remote_via_evtchn(evtchn);
383}
384EXPORT_SYMBOL_GPL(notify_remote_via_irq);
385
386static void mask_evtchn(int port)
387{
388 struct shared_info *s = HYPERVISOR_shared_info;
Ian Campbellc81611c2013-02-20 11:48:06 +0000389 sync_set_bit(port, BM(&s->evtchn_mask[0]));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700390}
391
392static void unmask_evtchn(int port)
393{
394 struct shared_info *s = HYPERVISOR_shared_info;
395 unsigned int cpu = get_cpu();
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100396 int do_hypercall = 0, evtchn_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700397
398 BUG_ON(!irqs_disabled());
399
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100400 if (unlikely((cpu != cpu_from_evtchn(port))))
401 do_hypercall = 1;
David Vrabelc26377e2013-03-25 14:11:19 +0000402 else {
403 /*
404 * Need to clear the mask before checking pending to
405 * avoid a race with an event becoming pending.
406 *
407 * EVTCHNOP_unmask will only trigger an upcall if the
408 * mask bit was set, so if a hypercall is needed
409 * remask the event.
410 */
411 sync_clear_bit(port, BM(&s->evtchn_mask[0]));
Ian Campbellc81611c2013-02-20 11:48:06 +0000412 evtchn_pending = sync_test_bit(port, BM(&s->evtchn_pending[0]));
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100413
David Vrabelc26377e2013-03-25 14:11:19 +0000414 if (unlikely(evtchn_pending && xen_hvm_domain())) {
415 sync_set_bit(port, BM(&s->evtchn_mask[0]));
416 do_hypercall = 1;
417 }
418 }
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100419
420 /* Slow path (hypercall) if this is a non-local port or if this is
421 * an hvm domain and an event is pending (hvm domains don't have
422 * their own implementation of irq_enable). */
423 if (do_hypercall) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700424 struct evtchn_unmask unmask = { .port = port };
425 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
426 } else {
Christoph Lameter780f36d2010-12-06 11:16:29 -0600427 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700428
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700429 /*
430 * The following is basically the equivalent of
431 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
432 * the interrupt edge' if the channel is masked.
433 */
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100434 if (evtchn_pending &&
Ian Campbellc81611c2013-02-20 11:48:06 +0000435 !sync_test_and_set_bit(port / BITS_PER_EVTCHN_WORD,
436 BM(&vcpu_info->evtchn_pending_sel)))
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700437 vcpu_info->evtchn_upcall_pending = 1;
438 }
439
440 put_cpu();
441}
442
Ian Campbell6cb65372011-03-10 16:08:11 +0000443static void xen_irq_init(unsigned irq)
444{
445 struct irq_info *info;
Konrad Rzeszutek Wilkb5328cd2011-06-15 14:24:29 -0400446#ifdef CONFIG_SMP
Ian Campbell6cb65372011-03-10 16:08:11 +0000447 struct irq_desc *desc = irq_to_desc(irq);
448
449 /* By default all event channels notify CPU#0. */
450 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
Konrad Rzeszutek Wilk44626e42011-03-15 16:40:33 -0400451#endif
Ian Campbell6cb65372011-03-10 16:08:11 +0000452
Ian Campbellca62ce82011-03-10 16:08:12 +0000453 info = kzalloc(sizeof(*info), GFP_KERNEL);
454 if (info == NULL)
455 panic("Unable to allocate metadata for IRQ%d\n", irq);
Ian Campbell6cb65372011-03-10 16:08:11 +0000456
457 info->type = IRQT_UNBOUND;
Daniel De Graaf420eb552011-10-27 17:58:47 -0400458 info->refcnt = -1;
Ian Campbell6cb65372011-03-10 16:08:11 +0000459
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100460 irq_set_handler_data(irq, info);
Ian Campbellca62ce82011-03-10 16:08:12 +0000461
Ian Campbell6cb65372011-03-10 16:08:11 +0000462 list_add_tail(&info->list, &xen_irq_list_head);
463}
464
Ian Campbell7bee9762011-03-10 16:08:15 +0000465static int __must_check xen_allocate_irq_dynamic(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700466{
Ian Campbell89911502011-03-03 11:57:44 -0500467 int first = 0;
468 int irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700469
Ian Campbell89911502011-03-03 11:57:44 -0500470#ifdef CONFIG_X86_IO_APIC
471 /*
472 * For an HVM guest or domain 0 which see "real" (emulated or
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300473 * actual respectively) GSIs we allocate dynamic IRQs
Ian Campbell89911502011-03-03 11:57:44 -0500474 * e.g. those corresponding to event channels or MSIs
475 * etc. from the range above those "real" GSIs to avoid
476 * collisions.
Konrad Rzeszutek Wilkd1b758e2010-12-09 14:53:29 -0500477 */
Ian Campbell89911502011-03-03 11:57:44 -0500478 if (xen_initial_domain() || xen_hvm_domain())
479 first = get_nr_irqs_gsi();
480#endif
481
Ian Campbell89911502011-03-03 11:57:44 -0500482 irq = irq_alloc_desc_from(first, -1);
483
Konrad Rzeszutek Wilke6599222011-09-29 13:26:45 -0400484 if (irq >= 0)
485 xen_irq_init(irq);
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800486
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700487 return irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400488}
489
Ian Campbell7bee9762011-03-10 16:08:15 +0000490static int __must_check xen_allocate_irq_gsi(unsigned gsi)
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000491{
492 int irq;
493
Ian Campbell89911502011-03-03 11:57:44 -0500494 /*
495 * A PV guest has no concept of a GSI (since it has no ACPI
496 * nor access to/knowledge of the physical APICs). Therefore
497 * all IRQs are dynamically allocated from the entire IRQ
498 * space.
499 */
500 if (xen_pv_domain() && !xen_initial_domain())
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000501 return xen_allocate_irq_dynamic();
502
503 /* Legacy IRQ descriptors are already allocated by the arch. */
504 if (gsi < NR_IRQS_LEGACY)
Ian Campbell6cb65372011-03-10 16:08:11 +0000505 irq = gsi;
506 else
507 irq = irq_alloc_desc_at(gsi, -1);
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000508
Ian Campbell6cb65372011-03-10 16:08:11 +0000509 xen_irq_init(irq);
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000510
511 return irq;
512}
513
514static void xen_free_irq(unsigned irq)
515{
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100516 struct irq_info *info = irq_get_handler_data(irq);
Ian Campbell6cb65372011-03-10 16:08:11 +0000517
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -0400518 if (WARN_ON(!info))
519 return;
520
Ian Campbell6cb65372011-03-10 16:08:11 +0000521 list_del(&info->list);
Ian Campbell9158c352011-03-10 16:08:09 +0000522
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100523 irq_set_handler_data(irq, NULL);
Ian Campbellca62ce82011-03-10 16:08:12 +0000524
Daniel De Graaf420eb552011-10-27 17:58:47 -0400525 WARN_ON(info->refcnt > 0);
526
Ian Campbellca62ce82011-03-10 16:08:12 +0000527 kfree(info);
528
Ian Campbell72146102011-02-03 09:49:35 +0000529 /* Legacy IRQ descriptors are managed by the arch. */
530 if (irq < NR_IRQS_LEGACY)
531 return;
532
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000533 irq_free_desc(irq);
534}
535
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400536static void pirq_query_unmask(int irq)
537{
538 struct physdev_irq_status_query irq_status;
539 struct irq_info *info = info_for_irq(irq);
540
541 BUG_ON(info->type != IRQT_PIRQ);
542
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100543 irq_status.irq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400544 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
545 irq_status.flags = 0;
546
547 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
548 if (irq_status.flags & XENIRQSTAT_needs_eoi)
549 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
550}
551
552static bool probing_irq(int irq)
553{
554 struct irq_desc *desc = irq_to_desc(irq);
555
556 return desc && desc->action == NULL;
557}
558
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100559static void eoi_pirq(struct irq_data *data)
560{
561 int evtchn = evtchn_from_irq(data->irq);
562 struct physdev_eoi eoi = { .irq = pirq_from_irq(data->irq) };
563 int rc = 0;
564
565 irq_move_irq(data);
566
567 if (VALID_EVTCHN(evtchn))
568 clear_evtchn(evtchn);
569
570 if (pirq_needs_eoi(data->irq)) {
571 rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
572 WARN_ON(rc);
573 }
574}
575
576static void mask_ack_pirq(struct irq_data *data)
577{
578 disable_dynirq(data);
579 eoi_pirq(data);
580}
581
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000582static unsigned int __startup_pirq(unsigned int irq)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400583{
584 struct evtchn_bind_pirq bind_pirq;
585 struct irq_info *info = info_for_irq(irq);
586 int evtchn = evtchn_from_irq(irq);
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400587 int rc;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400588
589 BUG_ON(info->type != IRQT_PIRQ);
590
591 if (VALID_EVTCHN(evtchn))
592 goto out;
593
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100594 bind_pirq.pirq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400595 /* NB. We are happy to share unless we are probing. */
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400596 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
597 BIND_PIRQ__WILL_SHARE : 0;
598 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
599 if (rc != 0) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400600 if (!probing_irq(irq))
601 printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
602 irq);
603 return 0;
604 }
605 evtchn = bind_pirq.port;
606
607 pirq_query_unmask(irq);
608
609 evtchn_to_irq[evtchn] = irq;
610 bind_evtchn_to_cpu(evtchn, 0);
611 info->evtchn = evtchn;
612
613out:
614 unmask_evtchn(evtchn);
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100615 eoi_pirq(irq_get_irq_data(irq));
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400616
617 return 0;
618}
619
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000620static unsigned int startup_pirq(struct irq_data *data)
621{
622 return __startup_pirq(data->irq);
623}
624
625static void shutdown_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400626{
627 struct evtchn_close close;
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000628 unsigned int irq = data->irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400629 struct irq_info *info = info_for_irq(irq);
630 int evtchn = evtchn_from_irq(irq);
631
632 BUG_ON(info->type != IRQT_PIRQ);
633
634 if (!VALID_EVTCHN(evtchn))
635 return;
636
637 mask_evtchn(evtchn);
638
639 close.port = evtchn;
640 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
641 BUG();
642
643 bind_evtchn_to_cpu(evtchn, 0);
644 evtchn_to_irq[evtchn] = -1;
645 info->evtchn = 0;
646}
647
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000648static void enable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400649{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000650 startup_pirq(data);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400651}
652
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000653static void disable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400654{
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100655 disable_dynirq(data);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400656}
657
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100658int xen_irq_from_gsi(unsigned gsi)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400659{
Ian Campbell6cb65372011-03-10 16:08:11 +0000660 struct irq_info *info;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400661
Ian Campbell6cb65372011-03-10 16:08:11 +0000662 list_for_each_entry(info, &xen_irq_list_head, list) {
663 if (info->type != IRQT_PIRQ)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400664 continue;
665
Ian Campbell6cb65372011-03-10 16:08:11 +0000666 if (info->u.pirq.gsi == gsi)
667 return info->irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400668 }
669
670 return -1;
671}
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100672EXPORT_SYMBOL_GPL(xen_irq_from_gsi);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400673
Ian Campbell653378a2011-03-10 16:08:04 +0000674/*
675 * Do not make any assumptions regarding the relationship between the
676 * IRQ number returned here and the Xen pirq argument.
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100677 *
678 * Note: We don't assign an event channel until the irq actually started
679 * up. Return an existing irq if we've already got one for the gsi.
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100680 *
681 * Shareable implies level triggered, not shareable implies edge
682 * triggered here.
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400683 */
Ian Campbellf4d06352011-03-10 16:08:07 +0000684int xen_bind_pirq_gsi_to_irq(unsigned gsi,
685 unsigned pirq, int shareable, char *name)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400686{
Ian Campbella0e18112011-03-10 16:08:03 +0000687 int irq = -1;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400688 struct physdev_irq irq_op;
689
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400690 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400691
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100692 irq = xen_irq_from_gsi(gsi);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400693 if (irq != -1) {
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100694 printk(KERN_INFO "xen_map_pirq_gsi: returning irq %d for gsi %u\n",
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400695 irq, gsi);
Daniel De Graaf420eb552011-10-27 17:58:47 -0400696 goto out;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400697 }
698
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000699 irq = xen_allocate_irq_gsi(gsi);
Ian Campbell7bee9762011-03-10 16:08:15 +0000700 if (irq < 0)
701 goto out;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400702
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400703 irq_op.irq = irq;
Alex Nixonb5401a92010-03-18 16:31:34 -0400704 irq_op.vector = 0;
705
706 /* Only the privileged domain can do this. For non-priv, the pcifront
707 * driver provides a PCI bus that does the call to do exactly
708 * this in the priv domain. */
709 if (xen_initial_domain() &&
710 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000711 xen_free_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400712 irq = -ENOSPC;
713 goto out;
714 }
715
Jan Beulichdec02de2013-04-03 15:52:50 +0100716 xen_irq_info_pirq_init(irq, 0, pirq, gsi, DOMID_SELF,
Ian Campbell9158c352011-03-10 16:08:09 +0000717 shareable ? PIRQ_SHAREABLE : 0);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400718
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100719 pirq_query_unmask(irq);
720 /* We try to use the handler with the appropriate semantic for the
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100721 * type of interrupt: if the interrupt is an edge triggered
722 * interrupt we use handle_edge_irq.
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100723 *
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100724 * On the other hand if the interrupt is level triggered we use
725 * handle_fasteoi_irq like the native code does for this kind of
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100726 * interrupts.
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100727 *
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100728 * Depending on the Xen version, pirq_needs_eoi might return true
729 * not only for level triggered interrupts but for edge triggered
730 * interrupts too. In any case Xen always honors the eoi mechanism,
731 * not injecting any more pirqs of the same kind if the first one
732 * hasn't received an eoi yet. Therefore using the fasteoi handler
733 * is the right choice either way.
734 */
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100735 if (shareable)
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100736 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
737 handle_fasteoi_irq, name);
738 else
739 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
740 handle_edge_irq, name);
741
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400742out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400743 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400744
745 return irq;
746}
747
Qing Hef731e3ef2010-10-11 15:30:09 +0100748#ifdef CONFIG_PCI_MSI
Ian Campbellbf480d92011-02-18 16:43:32 +0000749int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000750{
Ian Campbell5cad61a2011-02-18 16:43:31 +0000751 int rc;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000752 struct physdev_get_free_pirq op_get_free_pirq;
Ian Campbell5cad61a2011-02-18 16:43:31 +0000753
Ian Campbellbf480d92011-02-18 16:43:32 +0000754 op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000755 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000756
Ian Campbell5cad61a2011-02-18 16:43:31 +0000757 WARN_ONCE(rc == -ENOSYS,
758 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
759
760 return rc ? -1 : op_get_free_pirq.pirq;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000761}
762
Ian Campbellbf480d92011-02-18 16:43:32 +0000763int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
Jan Beulichdec02de2013-04-03 15:52:50 +0100764 int pirq, const char *name, domid_t domid)
Stefano Stabellini809f9262010-07-01 17:10:39 +0100765{
Ian Campbellbf480d92011-02-18 16:43:32 +0000766 int irq, ret;
Ian Campbell4b41df72011-02-18 16:43:29 +0000767
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400768 mutex_lock(&irq_mapping_update_lock);
Stefano Stabellini809f9262010-07-01 17:10:39 +0100769
Ian Campbell4b41df72011-02-18 16:43:29 +0000770 irq = xen_allocate_irq_dynamic();
Konrad Rzeszutek Wilke6599222011-09-29 13:26:45 -0400771 if (irq < 0)
Ian Campbellbb5d0792011-02-18 16:43:28 +0000772 goto out;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100773
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100774 irq_set_chip_and_handler_name(irq, &xen_pirq_chip, handle_edge_irq,
775 name);
Stefano Stabellini809f9262010-07-01 17:10:39 +0100776
Jan Beulichdec02de2013-04-03 15:52:50 +0100777 xen_irq_info_pirq_init(irq, 0, pirq, 0, domid, 0);
Linus Torvalds5f6fb452011-03-15 19:23:40 -0700778 ret = irq_set_msi_desc(irq, msidesc);
Ian Campbellbf480d92011-02-18 16:43:32 +0000779 if (ret < 0)
780 goto error_irq;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100781out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400782 mutex_unlock(&irq_mapping_update_lock);
Ian Campbell4b41df72011-02-18 16:43:29 +0000783 return irq;
Ian Campbellbf480d92011-02-18 16:43:32 +0000784error_irq:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400785 mutex_unlock(&irq_mapping_update_lock);
Ian Campbellbf480d92011-02-18 16:43:32 +0000786 xen_free_irq(irq);
Konrad Rzeszutek Wilke6599222011-09-29 13:26:45 -0400787 return ret;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100788}
Qing Hef731e3ef2010-10-11 15:30:09 +0100789#endif
790
Alex Nixonb5401a92010-03-18 16:31:34 -0400791int xen_destroy_irq(int irq)
792{
793 struct irq_desc *desc;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100794 struct physdev_unmap_pirq unmap_irq;
795 struct irq_info *info = info_for_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400796 int rc = -ENOENT;
797
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400798 mutex_lock(&irq_mapping_update_lock);
Alex Nixonb5401a92010-03-18 16:31:34 -0400799
800 desc = irq_to_desc(irq);
801 if (!desc)
802 goto out;
803
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100804 if (xen_initial_domain()) {
Konrad Rzeszutek Wilk12334712010-11-19 11:27:09 -0500805 unmap_irq.pirq = info->u.pirq.pirq;
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400806 unmap_irq.domid = info->u.pirq.domid;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100807 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
Konrad Rzeszutek Wilk1eff1ad2011-02-16 16:26:44 -0500808 /* If another domain quits without making the pci_disable_msix
809 * call, the Xen hypervisor takes care of freeing the PIRQs
810 * (free_domain_pirqs).
811 */
812 if ((rc == -ESRCH && info->u.pirq.domid != DOMID_SELF))
813 printk(KERN_INFO "domain %d does not have %d anymore\n",
814 info->u.pirq.domid, info->u.pirq.pirq);
815 else if (rc) {
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100816 printk(KERN_WARNING "unmap irq failed %d\n", rc);
817 goto out;
818 }
819 }
Alex Nixonb5401a92010-03-18 16:31:34 -0400820
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000821 xen_free_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400822
823out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400824 mutex_unlock(&irq_mapping_update_lock);
Alex Nixonb5401a92010-03-18 16:31:34 -0400825 return rc;
826}
827
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000828int xen_irq_from_pirq(unsigned pirq)
829{
Ian Campbell69c358c2011-03-10 16:08:13 +0000830 int irq;
831
832 struct irq_info *info;
833
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400834 mutex_lock(&irq_mapping_update_lock);
Ian Campbell69c358c2011-03-10 16:08:13 +0000835
836 list_for_each_entry(info, &xen_irq_list_head, list) {
Konrad Rzeszutek Wilk9bb9efe2011-09-29 13:13:30 -0400837 if (info->type != IRQT_PIRQ)
Ian Campbell69c358c2011-03-10 16:08:13 +0000838 continue;
839 irq = info->irq;
840 if (info->u.pirq.pirq == pirq)
841 goto out;
842 }
843 irq = -1;
844out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400845 mutex_unlock(&irq_mapping_update_lock);
Ian Campbell69c358c2011-03-10 16:08:13 +0000846
847 return irq;
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000848}
849
Konrad Rzeszutek Wilke6197ac2011-02-24 14:20:12 -0500850
851int xen_pirq_from_irq(unsigned irq)
852{
853 return pirq_from_irq(irq);
854}
855EXPORT_SYMBOL_GPL(xen_pirq_from_irq);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700856int bind_evtchn_to_irq(unsigned int evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700857{
858 int irq;
859
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400860 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700861
862 irq = evtchn_to_irq[evtchn];
863
864 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000865 irq = xen_allocate_irq_dynamic();
Wei Liu68ba45f2013-01-31 14:46:56 +0000866 if (irq < 0)
Ian Campbell7bee9762011-03-10 16:08:15 +0000867 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700868
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100869 irq_set_chip_and_handler_name(irq, &xen_dynamic_chip,
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100870 handle_edge_irq, "event");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700871
Ian Campbell9158c352011-03-10 16:08:09 +0000872 xen_irq_info_evtchn_init(irq, evtchn);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400873 } else {
874 struct irq_info *info = info_for_irq(irq);
875 WARN_ON(info == NULL || info->type != IRQT_EVTCHN);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700876 }
Stefano Stabellinia8636c02012-08-22 17:20:15 +0100877 irq_clear_status_flags(irq, IRQ_NOREQUEST|IRQ_NOAUTOEN);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700878
Ian Campbell7bee9762011-03-10 16:08:15 +0000879out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400880 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700881
882 return irq;
883}
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700884EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700885
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700886static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
887{
888 struct evtchn_bind_ipi bind_ipi;
889 int evtchn, irq;
890
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400891 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700892
893 irq = per_cpu(ipi_to_irq, cpu)[ipi];
Ian Campbell90af9512009-02-06 16:55:58 -0800894
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700895 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000896 irq = xen_allocate_irq_dynamic();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700897 if (irq < 0)
898 goto out;
899
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100900 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700901 handle_percpu_irq, "ipi");
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700902
903 bind_ipi.vcpu = cpu;
904 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
905 &bind_ipi) != 0)
906 BUG();
907 evtchn = bind_ipi.port;
908
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000909 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700910
911 bind_evtchn_to_cpu(evtchn, cpu);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400912 } else {
913 struct irq_info *info = info_for_irq(irq);
914 WARN_ON(info == NULL || info->type != IRQT_IPI);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700915 }
916
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700917 out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400918 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700919 return irq;
920}
921
Ian Campbell2e820f52009-02-09 12:05:50 -0800922static int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
923 unsigned int remote_port)
924{
925 struct evtchn_bind_interdomain bind_interdomain;
926 int err;
927
928 bind_interdomain.remote_dom = remote_domain;
929 bind_interdomain.remote_port = remote_port;
930
931 err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
932 &bind_interdomain);
933
934 return err ? : bind_evtchn_to_irq(bind_interdomain.local_port);
935}
936
Olaf Hering62cc5fc2011-08-25 18:30:48 +0200937static int find_virq(unsigned int virq, unsigned int cpu)
938{
939 struct evtchn_status status;
940 int port, rc = -ENOENT;
941
942 memset(&status, 0, sizeof(status));
943 for (port = 0; port <= NR_EVENT_CHANNELS; port++) {
944 status.dom = DOMID_SELF;
945 status.port = port;
946 rc = HYPERVISOR_event_channel_op(EVTCHNOP_status, &status);
947 if (rc < 0)
948 continue;
949 if (status.status != EVTCHNSTAT_virq)
950 continue;
951 if (status.u.virq == virq && status.vcpu == cpu) {
952 rc = port;
953 break;
954 }
955 }
956 return rc;
957}
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700958
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100959int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700960{
961 struct evtchn_bind_virq bind_virq;
Olaf Hering62cc5fc2011-08-25 18:30:48 +0200962 int evtchn, irq, ret;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700963
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400964 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700965
966 irq = per_cpu(virq_to_irq, cpu)[virq];
967
968 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000969 irq = xen_allocate_irq_dynamic();
Wei Liu68ba45f2013-01-31 14:46:56 +0000970 if (irq < 0)
Ian Campbell7bee9762011-03-10 16:08:15 +0000971 goto out;
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700972
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100973 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700974 handle_percpu_irq, "virq");
975
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700976 bind_virq.virq = virq;
977 bind_virq.vcpu = cpu;
Olaf Hering62cc5fc2011-08-25 18:30:48 +0200978 ret = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
979 &bind_virq);
980 if (ret == 0)
981 evtchn = bind_virq.port;
982 else {
983 if (ret == -EEXIST)
984 ret = find_virq(virq, cpu);
985 BUG_ON(ret < 0);
986 evtchn = ret;
987 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700988
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000989 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700990
991 bind_evtchn_to_cpu(evtchn, cpu);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400992 } else {
993 struct irq_info *info = info_for_irq(irq);
994 WARN_ON(info == NULL || info->type != IRQT_VIRQ);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700995 }
996
Ian Campbell7bee9762011-03-10 16:08:15 +0000997out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400998 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700999
1000 return irq;
1001}
1002
1003static void unbind_from_irq(unsigned int irq)
1004{
1005 struct evtchn_close close;
1006 int evtchn = evtchn_from_irq(irq);
Daniel De Graaf420eb552011-10-27 17:58:47 -04001007 struct irq_info *info = irq_get_handler_data(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001008
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -04001009 if (WARN_ON(!info))
1010 return;
1011
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001012 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001013
Daniel De Graaf420eb552011-10-27 17:58:47 -04001014 if (info->refcnt > 0) {
1015 info->refcnt--;
1016 if (info->refcnt != 0)
1017 goto done;
1018 }
1019
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001020 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001021 close.port = evtchn;
1022 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
1023 BUG();
1024
1025 switch (type_from_irq(irq)) {
1026 case IRQT_VIRQ:
1027 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001028 [virq_from_irq(irq)] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001029 break;
Alex Nixond68d82a2008-08-22 11:52:15 +01001030 case IRQT_IPI:
1031 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001032 [ipi_from_irq(irq)] = -1;
Alex Nixond68d82a2008-08-22 11:52:15 +01001033 break;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001034 default:
1035 break;
1036 }
1037
1038 /* Closed ports are implicitly re-bound to VCPU0. */
1039 bind_evtchn_to_cpu(evtchn, 0);
1040
1041 evtchn_to_irq[evtchn] = -1;
Ian Campbellfed5ea82009-12-01 16:15:30 +00001042 }
1043
Ian Campbellca62ce82011-03-10 16:08:12 +00001044 BUG_ON(info_for_irq(irq)->type == IRQT_UNBOUND);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001045
Ian Campbell9158c352011-03-10 16:08:09 +00001046 xen_free_irq(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001047
Daniel De Graaf420eb552011-10-27 17:58:47 -04001048 done:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001049 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001050}
1051
1052int bind_evtchn_to_irqhandler(unsigned int evtchn,
Jeff Garzik7c239972007-10-19 03:12:20 -04001053 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001054 unsigned long irqflags,
1055 const char *devname, void *dev_id)
1056{
Nicolas Kaiser361ae8c2011-03-30 21:14:26 +02001057 int irq, retval;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001058
1059 irq = bind_evtchn_to_irq(evtchn);
Ian Campbell7bee9762011-03-10 16:08:15 +00001060 if (irq < 0)
1061 return irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001062 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1063 if (retval != 0) {
1064 unbind_from_irq(irq);
1065 return retval;
1066 }
1067
1068 return irq;
1069}
1070EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
1071
Ian Campbell2e820f52009-02-09 12:05:50 -08001072int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
1073 unsigned int remote_port,
1074 irq_handler_t handler,
1075 unsigned long irqflags,
1076 const char *devname,
1077 void *dev_id)
1078{
1079 int irq, retval;
1080
1081 irq = bind_interdomain_evtchn_to_irq(remote_domain, remote_port);
1082 if (irq < 0)
1083 return irq;
1084
1085 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1086 if (retval != 0) {
1087 unbind_from_irq(irq);
1088 return retval;
1089 }
1090
1091 return irq;
1092}
1093EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler);
1094
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001095int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
Jeff Garzik7c239972007-10-19 03:12:20 -04001096 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001097 unsigned long irqflags, const char *devname, void *dev_id)
1098{
Nicolas Kaiser361ae8c2011-03-30 21:14:26 +02001099 int irq, retval;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001100
1101 irq = bind_virq_to_irq(virq, cpu);
Ian Campbell7bee9762011-03-10 16:08:15 +00001102 if (irq < 0)
1103 return irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001104 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1105 if (retval != 0) {
1106 unbind_from_irq(irq);
1107 return retval;
1108 }
1109
1110 return irq;
1111}
1112EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
1113
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001114int bind_ipi_to_irqhandler(enum ipi_vector ipi,
1115 unsigned int cpu,
1116 irq_handler_t handler,
1117 unsigned long irqflags,
1118 const char *devname,
1119 void *dev_id)
1120{
1121 int irq, retval;
1122
1123 irq = bind_ipi_to_irq(ipi, cpu);
1124 if (irq < 0)
1125 return irq;
1126
Ian Campbell9bab0b72011-10-03 15:37:00 +01001127 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME | IRQF_EARLY_RESUME;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001128 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1129 if (retval != 0) {
1130 unbind_from_irq(irq);
1131 return retval;
1132 }
1133
1134 return irq;
1135}
1136
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001137void unbind_from_irqhandler(unsigned int irq, void *dev_id)
1138{
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -04001139 struct irq_info *info = irq_get_handler_data(irq);
1140
1141 if (WARN_ON(!info))
1142 return;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001143 free_irq(irq, dev_id);
1144 unbind_from_irq(irq);
1145}
1146EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
1147
Daniel De Graaf420eb552011-10-27 17:58:47 -04001148int evtchn_make_refcounted(unsigned int evtchn)
1149{
1150 int irq = evtchn_to_irq[evtchn];
1151 struct irq_info *info;
1152
1153 if (irq == -1)
1154 return -ENOENT;
1155
1156 info = irq_get_handler_data(irq);
1157
1158 if (!info)
1159 return -ENOENT;
1160
1161 WARN_ON(info->refcnt != -1);
1162
1163 info->refcnt = 1;
1164
1165 return 0;
1166}
1167EXPORT_SYMBOL_GPL(evtchn_make_refcounted);
1168
1169int evtchn_get(unsigned int evtchn)
1170{
1171 int irq;
1172 struct irq_info *info;
1173 int err = -ENOENT;
1174
Daniel De Graafc3b3f162011-11-28 11:49:09 -05001175 if (evtchn >= NR_EVENT_CHANNELS)
1176 return -EINVAL;
1177
Daniel De Graaf420eb552011-10-27 17:58:47 -04001178 mutex_lock(&irq_mapping_update_lock);
1179
1180 irq = evtchn_to_irq[evtchn];
1181 if (irq == -1)
1182 goto done;
1183
1184 info = irq_get_handler_data(irq);
1185
1186 if (!info)
1187 goto done;
1188
1189 err = -EINVAL;
1190 if (info->refcnt <= 0)
1191 goto done;
1192
1193 info->refcnt++;
1194 err = 0;
1195 done:
1196 mutex_unlock(&irq_mapping_update_lock);
1197
1198 return err;
1199}
1200EXPORT_SYMBOL_GPL(evtchn_get);
1201
1202void evtchn_put(unsigned int evtchn)
1203{
1204 int irq = evtchn_to_irq[evtchn];
1205 if (WARN_ON(irq == -1))
1206 return;
1207 unbind_from_irq(irq);
1208}
1209EXPORT_SYMBOL_GPL(evtchn_put);
1210
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001211void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
1212{
1213 int irq = per_cpu(ipi_to_irq, cpu)[vector];
1214 BUG_ON(irq < 0);
1215 notify_remote_via_irq(irq);
1216}
1217
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001218irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
1219{
1220 struct shared_info *sh = HYPERVISOR_shared_info;
1221 int cpu = smp_processor_id();
Ian Campbellc81611c2013-02-20 11:48:06 +00001222 xen_ulong_t *cpu_evtchn = per_cpu(cpu_evtchn_mask, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001223 int i;
1224 unsigned long flags;
1225 static DEFINE_SPINLOCK(debug_lock);
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001226 struct vcpu_info *v;
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001227
1228 spin_lock_irqsave(&debug_lock, flags);
1229
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001230 printk("\nvcpu %d\n ", cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001231
1232 for_each_online_cpu(i) {
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001233 int pending;
1234 v = per_cpu(xen_vcpu, i);
1235 pending = (get_irq_regs() && i == cpu)
1236 ? xen_irqs_disabled(get_irq_regs())
1237 : v->evtchn_upcall_mask;
Ian Campbellc81611c2013-02-20 11:48:06 +00001238 printk("%d: masked=%d pending=%d event_sel %0*"PRI_xen_ulong"\n ", i,
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001239 pending, v->evtchn_upcall_pending,
1240 (int)(sizeof(v->evtchn_pending_sel)*2),
1241 v->evtchn_pending_sel);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001242 }
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001243 v = per_cpu(xen_vcpu, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001244
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001245 printk("\npending:\n ");
1246 for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
Ian Campbellc81611c2013-02-20 11:48:06 +00001247 printk("%0*"PRI_xen_ulong"%s",
1248 (int)sizeof(sh->evtchn_pending[0])*2,
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001249 sh->evtchn_pending[i],
1250 i % 8 == 0 ? "\n " : " ");
1251 printk("\nglobal mask:\n ");
1252 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
Ian Campbellc81611c2013-02-20 11:48:06 +00001253 printk("%0*"PRI_xen_ulong"%s",
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001254 (int)(sizeof(sh->evtchn_mask[0])*2),
1255 sh->evtchn_mask[i],
1256 i % 8 == 0 ? "\n " : " ");
1257
1258 printk("\nglobally unmasked:\n ");
1259 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
Ian Campbellc81611c2013-02-20 11:48:06 +00001260 printk("%0*"PRI_xen_ulong"%s",
1261 (int)(sizeof(sh->evtchn_mask[0])*2),
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001262 sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
1263 i % 8 == 0 ? "\n " : " ");
1264
1265 printk("\nlocal cpu%d mask:\n ", cpu);
Ian Campbellc81611c2013-02-20 11:48:06 +00001266 for (i = (NR_EVENT_CHANNELS/BITS_PER_EVTCHN_WORD)-1; i >= 0; i--)
1267 printk("%0*"PRI_xen_ulong"%s", (int)(sizeof(cpu_evtchn[0])*2),
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001268 cpu_evtchn[i],
1269 i % 8 == 0 ? "\n " : " ");
1270
1271 printk("\nlocally unmasked:\n ");
1272 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) {
Ian Campbellc81611c2013-02-20 11:48:06 +00001273 xen_ulong_t pending = sh->evtchn_pending[i]
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001274 & ~sh->evtchn_mask[i]
1275 & cpu_evtchn[i];
Ian Campbellc81611c2013-02-20 11:48:06 +00001276 printk("%0*"PRI_xen_ulong"%s",
1277 (int)(sizeof(sh->evtchn_mask[0])*2),
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001278 pending, i % 8 == 0 ? "\n " : " ");
1279 }
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001280
1281 printk("\npending list:\n");
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001282 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
Ian Campbellc81611c2013-02-20 11:48:06 +00001283 if (sync_test_bit(i, BM(sh->evtchn_pending))) {
1284 int word_idx = i / BITS_PER_EVTCHN_WORD;
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001285 printk(" %d: event %d -> irq %d%s%s%s\n",
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001286 cpu_from_evtchn(i), i,
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001287 evtchn_to_irq[i],
Ian Campbellc81611c2013-02-20 11:48:06 +00001288 sync_test_bit(word_idx, BM(&v->evtchn_pending_sel))
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001289 ? "" : " l2-clear",
Ian Campbellc81611c2013-02-20 11:48:06 +00001290 !sync_test_bit(i, BM(sh->evtchn_mask))
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001291 ? "" : " globally-masked",
Ian Campbellc81611c2013-02-20 11:48:06 +00001292 sync_test_bit(i, BM(cpu_evtchn))
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001293 ? "" : " locally-masked");
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001294 }
1295 }
1296
1297 spin_unlock_irqrestore(&debug_lock, flags);
1298
1299 return IRQ_HANDLED;
1300}
1301
Tejun Heo245b2e72009-06-24 15:13:48 +09001302static DEFINE_PER_CPU(unsigned, xed_nesting_count);
Keir Fraserada68142011-03-03 10:01:11 +00001303static DEFINE_PER_CPU(unsigned int, current_word_idx);
1304static DEFINE_PER_CPU(unsigned int, current_bit_idx);
Tejun Heo245b2e72009-06-24 15:13:48 +09001305
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001306/*
Scott Rixnerab7f8632011-03-03 09:30:08 +00001307 * Mask out the i least significant bits of w
1308 */
Ian Campbellc81611c2013-02-20 11:48:06 +00001309#define MASK_LSBS(w, i) (w & ((~((xen_ulong_t)0UL)) << i))
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001310
1311/*
1312 * Search the CPUs pending events bitmasks. For each one found, map
1313 * the event number to an irq, and feed it into do_IRQ() for
1314 * handling.
1315 *
1316 * Xen uses a two-level bitmap to speed searching. The first level is
1317 * a bitset of words which contain pending event bits. The second
1318 * level is a bitset of pending events themselves.
1319 */
Sheng Yang38e20b02010-05-14 12:40:51 +01001320static void __xen_evtchn_do_upcall(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001321{
Keir Fraser24b51c22011-03-03 11:06:28 +00001322 int start_word_idx, start_bit_idx;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001323 int word_idx, bit_idx;
Keir Fraserbee980d2013-03-28 10:03:36 -04001324 int i, irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001325 int cpu = get_cpu();
1326 struct shared_info *s = HYPERVISOR_shared_info;
Christoph Lameter780f36d2010-12-06 11:16:29 -06001327 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
Ruslan Pisarev088c05a2011-07-26 14:16:13 +03001328 unsigned count;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001329
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001330 do {
Ian Campbellc81611c2013-02-20 11:48:06 +00001331 xen_ulong_t pending_words;
Keir Fraserbee980d2013-03-28 10:03:36 -04001332 xen_ulong_t pending_bits;
1333 struct irq_desc *desc;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001334
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001335 vcpu_info->evtchn_upcall_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001336
Christoph Lameterb2e4ae62010-12-06 11:40:07 -06001337 if (__this_cpu_inc_return(xed_nesting_count) - 1)
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001338 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001339
Ian Campbellc81611c2013-02-20 11:48:06 +00001340 /*
1341 * Master flag must be cleared /before/ clearing
1342 * selector flag. xchg_xen_ulong must contain an
1343 * appropriate barrier.
1344 */
Keir Fraserbee980d2013-03-28 10:03:36 -04001345 if ((irq = per_cpu(virq_to_irq, cpu)[VIRQ_TIMER]) != -1) {
1346 int evtchn = evtchn_from_irq(irq);
1347 word_idx = evtchn / BITS_PER_LONG;
1348 pending_bits = evtchn % BITS_PER_LONG;
1349 if (active_evtchns(cpu, s, word_idx) & (1ULL << pending_bits)) {
1350 desc = irq_to_desc(irq);
1351 if (desc)
1352 generic_handle_irq_desc(irq, desc);
1353 }
1354 }
1355
Ian Campbellc81611c2013-02-20 11:48:06 +00001356 pending_words = xchg_xen_ulong(&vcpu_info->evtchn_pending_sel, 0);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001357
Keir Fraser24b51c22011-03-03 11:06:28 +00001358 start_word_idx = __this_cpu_read(current_word_idx);
1359 start_bit_idx = __this_cpu_read(current_bit_idx);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001360
Keir Fraser24b51c22011-03-03 11:06:28 +00001361 word_idx = start_word_idx;
1362
1363 for (i = 0; pending_words != 0; i++) {
Ian Campbellc81611c2013-02-20 11:48:06 +00001364 xen_ulong_t words;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001365
Scott Rixnerab7f8632011-03-03 09:30:08 +00001366 words = MASK_LSBS(pending_words, word_idx);
1367
1368 /*
Keir Fraserada68142011-03-03 10:01:11 +00001369 * If we masked out all events, wrap to beginning.
Scott Rixnerab7f8632011-03-03 09:30:08 +00001370 */
1371 if (words == 0) {
Keir Fraserada68142011-03-03 10:01:11 +00001372 word_idx = 0;
1373 bit_idx = 0;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001374 continue;
1375 }
Ian Campbellc81611c2013-02-20 11:48:06 +00001376 word_idx = EVTCHN_FIRST_BIT(words);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001377
Keir Fraser24b51c22011-03-03 11:06:28 +00001378 pending_bits = active_evtchns(cpu, s, word_idx);
1379 bit_idx = 0; /* usually scan entire word from start */
1380 if (word_idx == start_word_idx) {
1381 /* We scan the starting word in two parts */
1382 if (i == 0)
1383 /* 1st time: start in the middle */
1384 bit_idx = start_bit_idx;
1385 else
1386 /* 2nd time: mask bits done already */
1387 bit_idx &= (1UL << start_bit_idx) - 1;
1388 }
1389
Scott Rixnerab7f8632011-03-03 09:30:08 +00001390 do {
Ian Campbellc81611c2013-02-20 11:48:06 +00001391 xen_ulong_t bits;
Keir Fraserbee980d2013-03-28 10:03:36 -04001392 int port;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001393
Scott Rixnerab7f8632011-03-03 09:30:08 +00001394 bits = MASK_LSBS(pending_bits, bit_idx);
1395
1396 /* If we masked out all events, move on. */
Keir Fraserada68142011-03-03 10:01:11 +00001397 if (bits == 0)
Scott Rixnerab7f8632011-03-03 09:30:08 +00001398 break;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001399
Ian Campbellc81611c2013-02-20 11:48:06 +00001400 bit_idx = EVTCHN_FIRST_BIT(bits);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001401
1402 /* Process port. */
Ian Campbellc81611c2013-02-20 11:48:06 +00001403 port = (word_idx * BITS_PER_EVTCHN_WORD) + bit_idx;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001404 irq = evtchn_to_irq[port];
1405
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -08001406 if (irq != -1) {
1407 desc = irq_to_desc(irq);
1408 if (desc)
1409 generic_handle_irq_desc(irq, desc);
1410 }
Scott Rixnerab7f8632011-03-03 09:30:08 +00001411
Ian Campbellc81611c2013-02-20 11:48:06 +00001412 bit_idx = (bit_idx + 1) % BITS_PER_EVTCHN_WORD;
Keir Fraserada68142011-03-03 10:01:11 +00001413
1414 /* Next caller starts at last processed + 1 */
1415 __this_cpu_write(current_word_idx,
1416 bit_idx ? word_idx :
Ian Campbellc81611c2013-02-20 11:48:06 +00001417 (word_idx+1) % BITS_PER_EVTCHN_WORD);
Keir Fraserada68142011-03-03 10:01:11 +00001418 __this_cpu_write(current_bit_idx, bit_idx);
1419 } while (bit_idx != 0);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001420
Keir Fraser24b51c22011-03-03 11:06:28 +00001421 /* Scan start_l1i twice; all others once. */
1422 if ((word_idx != start_word_idx) || (i != 0))
Scott Rixnerab7f8632011-03-03 09:30:08 +00001423 pending_words &= ~(1UL << word_idx);
Keir Fraserada68142011-03-03 10:01:11 +00001424
Ian Campbellc81611c2013-02-20 11:48:06 +00001425 word_idx = (word_idx + 1) % BITS_PER_EVTCHN_WORD;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001426 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001427
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001428 BUG_ON(!irqs_disabled());
1429
Christoph Lameter780f36d2010-12-06 11:16:29 -06001430 count = __this_cpu_read(xed_nesting_count);
1431 __this_cpu_write(xed_nesting_count, 0);
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001432 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001433
1434out:
Jeremy Fitzhardinge3445a8f2009-02-06 14:09:46 -08001435
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001436 put_cpu();
1437}
1438
Sheng Yang38e20b02010-05-14 12:40:51 +01001439void xen_evtchn_do_upcall(struct pt_regs *regs)
1440{
1441 struct pt_regs *old_regs = set_irq_regs(regs);
1442
Mojiong Qiu772aebc2012-11-06 16:08:15 +08001443 irq_enter();
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001444#ifdef CONFIG_X86
Sheng Yang38e20b02010-05-14 12:40:51 +01001445 exit_idle();
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001446#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001447
1448 __xen_evtchn_do_upcall();
1449
1450 irq_exit();
1451 set_irq_regs(old_regs);
1452}
1453
1454void xen_hvm_evtchn_do_upcall(void)
1455{
1456 __xen_evtchn_do_upcall();
1457}
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001458EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
Sheng Yang38e20b02010-05-14 12:40:51 +01001459
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001460/* Rebind a new event channel to an existing irq. */
1461void rebind_evtchn_irq(int evtchn, int irq)
1462{
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001463 struct irq_info *info = info_for_irq(irq);
1464
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -04001465 if (WARN_ON(!info))
1466 return;
1467
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001468 /* Make sure the irq is masked, since the new event channel
1469 will also be masked. */
1470 disable_irq(irq);
1471
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001472 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001473
1474 /* After resume the irq<->evtchn mappings are all cleared out */
1475 BUG_ON(evtchn_to_irq[evtchn] != -1);
1476 /* Expect irq to have been bound before,
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001477 so there should be a proper type */
1478 BUG_ON(info->type == IRQT_UNBOUND);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001479
Ian Campbell9158c352011-03-10 16:08:09 +00001480 xen_irq_info_evtchn_init(irq, evtchn);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001481
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001482 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001483
1484 /* new event channels are always bound to cpu 0 */
Rusty Russell0de26522008-12-13 21:20:26 +10301485 irq_set_affinity(irq, cpumask_of(0));
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001486
1487 /* Unmask the event channel. */
1488 enable_irq(irq);
1489}
1490
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001491/* Rebind an evtchn so that it gets delivered to a specific cpu */
Yinghai Lud5dedd42009-04-27 17:59:21 -07001492static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001493{
1494 struct evtchn_bind_vcpu bind_vcpu;
1495 int evtchn = evtchn_from_irq(irq);
1496
Ian Campbellbe494722011-03-10 16:08:02 +00001497 if (!VALID_EVTCHN(evtchn))
1498 return -1;
1499
1500 /*
1501 * Events delivered via platform PCI interrupts are always
1502 * routed to vcpu 0 and hence cannot be rebound.
1503 */
1504 if (xen_hvm_domain() && !xen_have_vector_callback)
Yinghai Lud5dedd42009-04-27 17:59:21 -07001505 return -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001506
1507 /* Send future instances of this interrupt to other vcpu. */
1508 bind_vcpu.port = evtchn;
1509 bind_vcpu.vcpu = tcpu;
1510
1511 /*
1512 * If this fails, it usually just indicates that we're dealing with a
1513 * virq or IPI channel, which don't actually need to be rebound. Ignore
1514 * it, but don't do the xenlinux-level rebind in that case.
1515 */
1516 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1517 bind_evtchn_to_cpu(evtchn, tcpu);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001518
1519 return 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001520}
1521
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001522static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1523 bool force)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001524{
Rusty Russell0de26522008-12-13 21:20:26 +10301525 unsigned tcpu = cpumask_first(dest);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001526
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001527 return rebind_irq_to_cpu(data->irq, tcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001528}
1529
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001530int resend_irq_on_evtchn(unsigned int irq)
1531{
1532 int masked, evtchn = evtchn_from_irq(irq);
1533 struct shared_info *s = HYPERVISOR_shared_info;
1534
1535 if (!VALID_EVTCHN(evtchn))
1536 return 1;
1537
Ian Campbellc81611c2013-02-20 11:48:06 +00001538 masked = sync_test_and_set_bit(evtchn, BM(s->evtchn_mask));
1539 sync_set_bit(evtchn, BM(s->evtchn_pending));
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001540 if (!masked)
1541 unmask_evtchn(evtchn);
1542
1543 return 1;
1544}
1545
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001546static void enable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001547{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001548 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001549
1550 if (VALID_EVTCHN(evtchn))
1551 unmask_evtchn(evtchn);
1552}
1553
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001554static void disable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001555{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001556 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001557
1558 if (VALID_EVTCHN(evtchn))
1559 mask_evtchn(evtchn);
1560}
1561
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001562static void ack_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001563{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001564 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001565
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001566 irq_move_irq(data);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001567
1568 if (VALID_EVTCHN(evtchn))
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001569 clear_evtchn(evtchn);
1570}
1571
1572static void mask_ack_dynirq(struct irq_data *data)
1573{
1574 disable_dynirq(data);
1575 ack_dynirq(data);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001576}
1577
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001578static int retrigger_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001579{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001580 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001581 struct shared_info *sh = HYPERVISOR_shared_info;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001582 int ret = 0;
1583
1584 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001585 int masked;
1586
Ian Campbellc81611c2013-02-20 11:48:06 +00001587 masked = sync_test_and_set_bit(evtchn, BM(sh->evtchn_mask));
1588 sync_set_bit(evtchn, BM(sh->evtchn_pending));
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001589 if (!masked)
1590 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001591 ret = 1;
1592 }
1593
1594 return ret;
1595}
1596
Ian Campbell0a852262011-03-10 16:08:06 +00001597static void restore_pirqs(void)
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001598{
1599 int pirq, rc, irq, gsi;
1600 struct physdev_map_pirq map_irq;
Ian Campbell69c358c2011-03-10 16:08:13 +00001601 struct irq_info *info;
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001602
Ian Campbell69c358c2011-03-10 16:08:13 +00001603 list_for_each_entry(info, &xen_irq_list_head, list) {
1604 if (info->type != IRQT_PIRQ)
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001605 continue;
1606
Ian Campbell69c358c2011-03-10 16:08:13 +00001607 pirq = info->u.pirq.pirq;
1608 gsi = info->u.pirq.gsi;
1609 irq = info->irq;
1610
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001611 /* save/restore of PT devices doesn't work, so at this point the
1612 * only devices present are GSI based emulated devices */
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001613 if (!gsi)
1614 continue;
1615
1616 map_irq.domid = DOMID_SELF;
1617 map_irq.type = MAP_PIRQ_TYPE_GSI;
1618 map_irq.index = gsi;
1619 map_irq.pirq = pirq;
1620
1621 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1622 if (rc) {
1623 printk(KERN_WARNING "xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1624 gsi, irq, pirq, rc);
Ian Campbell9158c352011-03-10 16:08:09 +00001625 xen_free_irq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001626 continue;
1627 }
1628
1629 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1630
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001631 __startup_pirq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001632 }
1633}
1634
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001635static void restore_cpu_virqs(unsigned int cpu)
1636{
1637 struct evtchn_bind_virq bind_virq;
1638 int virq, irq, evtchn;
1639
1640 for (virq = 0; virq < NR_VIRQS; virq++) {
1641 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1642 continue;
1643
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001644 BUG_ON(virq_from_irq(irq) != virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001645
1646 /* Get a new binding from Xen. */
1647 bind_virq.virq = virq;
1648 bind_virq.vcpu = cpu;
1649 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1650 &bind_virq) != 0)
1651 BUG();
1652 evtchn = bind_virq.port;
1653
1654 /* Record the new mapping. */
Ian Campbell3d4cfa32011-03-10 16:08:10 +00001655 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001656 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001657 }
1658}
1659
1660static void restore_cpu_ipis(unsigned int cpu)
1661{
1662 struct evtchn_bind_ipi bind_ipi;
1663 int ipi, irq, evtchn;
1664
1665 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1666 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1667 continue;
1668
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001669 BUG_ON(ipi_from_irq(irq) != ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001670
1671 /* Get a new binding from Xen. */
1672 bind_ipi.vcpu = cpu;
1673 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1674 &bind_ipi) != 0)
1675 BUG();
1676 evtchn = bind_ipi.port;
1677
1678 /* Record the new mapping. */
Ian Campbell3d4cfa32011-03-10 16:08:10 +00001679 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001680 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001681 }
1682}
1683
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001684/* Clear an irq's pending state, in preparation for polling on it */
1685void xen_clear_irq_pending(int irq)
1686{
1687 int evtchn = evtchn_from_irq(irq);
1688
1689 if (VALID_EVTCHN(evtchn))
1690 clear_evtchn(evtchn);
1691}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001692EXPORT_SYMBOL(xen_clear_irq_pending);
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -07001693void xen_set_irq_pending(int irq)
1694{
1695 int evtchn = evtchn_from_irq(irq);
1696
1697 if (VALID_EVTCHN(evtchn))
1698 set_evtchn(evtchn);
1699}
1700
1701bool xen_test_irq_pending(int irq)
1702{
1703 int evtchn = evtchn_from_irq(irq);
1704 bool ret = false;
1705
1706 if (VALID_EVTCHN(evtchn))
1707 ret = test_evtchn(evtchn);
1708
1709 return ret;
1710}
1711
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001712/* Poll waiting for an irq to become pending with timeout. In the usual case,
1713 * the irq will be disabled so it won't deliver an interrupt. */
1714void xen_poll_irq_timeout(int irq, u64 timeout)
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001715{
1716 evtchn_port_t evtchn = evtchn_from_irq(irq);
1717
1718 if (VALID_EVTCHN(evtchn)) {
1719 struct sched_poll poll;
1720
1721 poll.nr_ports = 1;
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001722 poll.timeout = timeout;
Isaku Yamahataff3c5362008-10-14 17:50:44 -07001723 set_xen_guest_handle(poll.ports, &evtchn);
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001724
1725 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1726 BUG();
1727 }
1728}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001729EXPORT_SYMBOL(xen_poll_irq_timeout);
1730/* Poll waiting for an irq to become pending. In the usual case, the
1731 * irq will be disabled so it won't deliver an interrupt. */
1732void xen_poll_irq(int irq)
1733{
1734 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1735}
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001736
Konrad Rzeszutek Wilkc7c2c3a2010-11-08 14:26:36 -05001737/* Check whether the IRQ line is shared with other guests. */
1738int xen_test_irq_shared(int irq)
1739{
1740 struct irq_info *info = info_for_irq(irq);
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -04001741 struct physdev_irq_status_query irq_status;
1742
1743 if (WARN_ON(!info))
1744 return -ENOENT;
1745
1746 irq_status.irq = info->u.pirq.pirq;
Konrad Rzeszutek Wilkc7c2c3a2010-11-08 14:26:36 -05001747
1748 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
1749 return 0;
1750 return !(irq_status.flags & XENIRQSTAT_shared);
1751}
1752EXPORT_SYMBOL_GPL(xen_test_irq_shared);
1753
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001754void xen_irq_resume(void)
1755{
Ian Campbell6cb65372011-03-10 16:08:11 +00001756 unsigned int cpu, evtchn;
1757 struct irq_info *info;
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001758
1759 init_evtchn_cpu_bindings();
1760
1761 /* New event-channel space is not 'live' yet. */
1762 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1763 mask_evtchn(evtchn);
1764
1765 /* No IRQ <-> event-channel mappings. */
Ian Campbell6cb65372011-03-10 16:08:11 +00001766 list_for_each_entry(info, &xen_irq_list_head, list)
1767 info->evtchn = 0; /* zap event-channel binding */
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001768
1769 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1770 evtchn_to_irq[evtchn] = -1;
1771
1772 for_each_possible_cpu(cpu) {
1773 restore_cpu_virqs(cpu);
1774 restore_cpu_ipis(cpu);
1775 }
Ian Campbell69035912010-11-01 16:30:09 +00001776
Ian Campbell0a852262011-03-10 16:08:06 +00001777 restore_pirqs();
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001778}
1779
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001780static struct irq_chip xen_dynamic_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001781 .name = "xen-dyn",
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001782
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001783 .irq_disable = disable_dynirq,
1784 .irq_mask = disable_dynirq,
1785 .irq_unmask = enable_dynirq,
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001786
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001787 .irq_ack = ack_dynirq,
1788 .irq_mask_ack = mask_ack_dynirq,
1789
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001790 .irq_set_affinity = set_affinity_irq,
1791 .irq_retrigger = retrigger_dynirq,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001792};
1793
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001794static struct irq_chip xen_pirq_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001795 .name = "xen-pirq",
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001796
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001797 .irq_startup = startup_pirq,
1798 .irq_shutdown = shutdown_pirq,
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001799 .irq_enable = enable_pirq,
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001800 .irq_disable = disable_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001801
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001802 .irq_mask = disable_dynirq,
1803 .irq_unmask = enable_dynirq,
1804
1805 .irq_ack = eoi_pirq,
1806 .irq_eoi = eoi_pirq,
1807 .irq_mask_ack = mask_ack_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001808
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001809 .irq_set_affinity = set_affinity_irq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001810
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001811 .irq_retrigger = retrigger_dynirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001812};
1813
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001814static struct irq_chip xen_percpu_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001815 .name = "xen-percpu",
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001816
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001817 .irq_disable = disable_dynirq,
1818 .irq_mask = disable_dynirq,
1819 .irq_unmask = enable_dynirq,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001820
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001821 .irq_ack = ack_dynirq,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001822};
1823
Sheng Yang38e20b02010-05-14 12:40:51 +01001824int xen_set_callback_via(uint64_t via)
1825{
1826 struct xen_hvm_param a;
1827 a.domid = DOMID_SELF;
1828 a.index = HVM_PARAM_CALLBACK_IRQ;
1829 a.value = via;
1830 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1831}
1832EXPORT_SYMBOL_GPL(xen_set_callback_via);
1833
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001834#ifdef CONFIG_XEN_PVHVM
Sheng Yang38e20b02010-05-14 12:40:51 +01001835/* Vector callbacks are better than PCI interrupts to receive event
1836 * channel notifications because we can receive vector callbacks on any
1837 * vcpu and we don't need PCI support or APIC interactions. */
1838void xen_callback_vector(void)
1839{
1840 int rc;
1841 uint64_t callback_via;
1842 if (xen_have_vector_callback) {
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -08001843 callback_via = HVM_CALLBACK_VECTOR(HYPERVISOR_CALLBACK_VECTOR);
Sheng Yang38e20b02010-05-14 12:40:51 +01001844 rc = xen_set_callback_via(callback_via);
1845 if (rc) {
1846 printk(KERN_ERR "Request for Xen HVM callback vector"
1847 " failed.\n");
1848 xen_have_vector_callback = 0;
1849 return;
1850 }
1851 printk(KERN_INFO "Xen HVM callback vector for event delivery is "
1852 "enabled\n");
1853 /* 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}