blob: bb65f75e40a540d316abc9f307768b9c06624329 [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
518 list_del(&info->list);
Ian Campbell9158c352011-03-10 16:08:09 +0000519
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100520 irq_set_handler_data(irq, NULL);
Ian Campbellca62ce82011-03-10 16:08:12 +0000521
Daniel De Graaf420eb552011-10-27 17:58:47 -0400522 WARN_ON(info->refcnt > 0);
523
Ian Campbellca62ce82011-03-10 16:08:12 +0000524 kfree(info);
525
Ian Campbell72146102011-02-03 09:49:35 +0000526 /* Legacy IRQ descriptors are managed by the arch. */
527 if (irq < NR_IRQS_LEGACY)
528 return;
529
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000530 irq_free_desc(irq);
531}
532
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400533static void pirq_query_unmask(int irq)
534{
535 struct physdev_irq_status_query irq_status;
536 struct irq_info *info = info_for_irq(irq);
537
538 BUG_ON(info->type != IRQT_PIRQ);
539
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100540 irq_status.irq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400541 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
542 irq_status.flags = 0;
543
544 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
545 if (irq_status.flags & XENIRQSTAT_needs_eoi)
546 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
547}
548
549static bool probing_irq(int irq)
550{
551 struct irq_desc *desc = irq_to_desc(irq);
552
553 return desc && desc->action == NULL;
554}
555
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100556static void eoi_pirq(struct irq_data *data)
557{
558 int evtchn = evtchn_from_irq(data->irq);
559 struct physdev_eoi eoi = { .irq = pirq_from_irq(data->irq) };
560 int rc = 0;
561
562 irq_move_irq(data);
563
564 if (VALID_EVTCHN(evtchn))
565 clear_evtchn(evtchn);
566
567 if (pirq_needs_eoi(data->irq)) {
568 rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
569 WARN_ON(rc);
570 }
571}
572
573static void mask_ack_pirq(struct irq_data *data)
574{
575 disable_dynirq(data);
576 eoi_pirq(data);
577}
578
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000579static unsigned int __startup_pirq(unsigned int irq)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400580{
581 struct evtchn_bind_pirq bind_pirq;
582 struct irq_info *info = info_for_irq(irq);
583 int evtchn = evtchn_from_irq(irq);
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400584 int rc;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400585
586 BUG_ON(info->type != IRQT_PIRQ);
587
588 if (VALID_EVTCHN(evtchn))
589 goto out;
590
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100591 bind_pirq.pirq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400592 /* NB. We are happy to share unless we are probing. */
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400593 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
594 BIND_PIRQ__WILL_SHARE : 0;
595 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
596 if (rc != 0) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400597 if (!probing_irq(irq))
598 printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
599 irq);
600 return 0;
601 }
602 evtchn = bind_pirq.port;
603
604 pirq_query_unmask(irq);
605
606 evtchn_to_irq[evtchn] = irq;
607 bind_evtchn_to_cpu(evtchn, 0);
608 info->evtchn = evtchn;
609
610out:
611 unmask_evtchn(evtchn);
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100612 eoi_pirq(irq_get_irq_data(irq));
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400613
614 return 0;
615}
616
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000617static unsigned int startup_pirq(struct irq_data *data)
618{
619 return __startup_pirq(data->irq);
620}
621
622static void shutdown_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400623{
624 struct evtchn_close close;
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000625 unsigned int irq = data->irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400626 struct irq_info *info = info_for_irq(irq);
627 int evtchn = evtchn_from_irq(irq);
628
629 BUG_ON(info->type != IRQT_PIRQ);
630
631 if (!VALID_EVTCHN(evtchn))
632 return;
633
634 mask_evtchn(evtchn);
635
636 close.port = evtchn;
637 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
638 BUG();
639
640 bind_evtchn_to_cpu(evtchn, 0);
641 evtchn_to_irq[evtchn] = -1;
642 info->evtchn = 0;
643}
644
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000645static void enable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400646{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000647 startup_pirq(data);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400648}
649
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000650static void disable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400651{
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100652 disable_dynirq(data);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400653}
654
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100655int xen_irq_from_gsi(unsigned gsi)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400656{
Ian Campbell6cb65372011-03-10 16:08:11 +0000657 struct irq_info *info;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400658
Ian Campbell6cb65372011-03-10 16:08:11 +0000659 list_for_each_entry(info, &xen_irq_list_head, list) {
660 if (info->type != IRQT_PIRQ)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400661 continue;
662
Ian Campbell6cb65372011-03-10 16:08:11 +0000663 if (info->u.pirq.gsi == gsi)
664 return info->irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400665 }
666
667 return -1;
668}
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100669EXPORT_SYMBOL_GPL(xen_irq_from_gsi);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400670
Ian Campbell653378a2011-03-10 16:08:04 +0000671/*
672 * Do not make any assumptions regarding the relationship between the
673 * IRQ number returned here and the Xen pirq argument.
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100674 *
675 * Note: We don't assign an event channel until the irq actually started
676 * up. Return an existing irq if we've already got one for the gsi.
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100677 *
678 * Shareable implies level triggered, not shareable implies edge
679 * triggered here.
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400680 */
Ian Campbellf4d06352011-03-10 16:08:07 +0000681int xen_bind_pirq_gsi_to_irq(unsigned gsi,
682 unsigned pirq, int shareable, char *name)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400683{
Ian Campbella0e18112011-03-10 16:08:03 +0000684 int irq = -1;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400685 struct physdev_irq irq_op;
686
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400687 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400688
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100689 irq = xen_irq_from_gsi(gsi);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400690 if (irq != -1) {
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100691 printk(KERN_INFO "xen_map_pirq_gsi: returning irq %d for gsi %u\n",
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400692 irq, gsi);
Daniel De Graaf420eb552011-10-27 17:58:47 -0400693 goto out;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400694 }
695
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000696 irq = xen_allocate_irq_gsi(gsi);
Ian Campbell7bee9762011-03-10 16:08:15 +0000697 if (irq < 0)
698 goto out;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400699
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400700 irq_op.irq = irq;
Alex Nixonb5401a92010-03-18 16:31:34 -0400701 irq_op.vector = 0;
702
703 /* Only the privileged domain can do this. For non-priv, the pcifront
704 * driver provides a PCI bus that does the call to do exactly
705 * this in the priv domain. */
706 if (xen_initial_domain() &&
707 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000708 xen_free_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400709 irq = -ENOSPC;
710 goto out;
711 }
712
Jan Beulichdec02de2013-04-03 15:52:50 +0100713 xen_irq_info_pirq_init(irq, 0, pirq, gsi, DOMID_SELF,
Ian Campbell9158c352011-03-10 16:08:09 +0000714 shareable ? PIRQ_SHAREABLE : 0);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400715
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100716 pirq_query_unmask(irq);
717 /* We try to use the handler with the appropriate semantic for the
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100718 * type of interrupt: if the interrupt is an edge triggered
719 * interrupt we use handle_edge_irq.
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100720 *
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100721 * On the other hand if the interrupt is level triggered we use
722 * handle_fasteoi_irq like the native code does for this kind of
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100723 * interrupts.
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100724 *
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100725 * Depending on the Xen version, pirq_needs_eoi might return true
726 * not only for level triggered interrupts but for edge triggered
727 * interrupts too. In any case Xen always honors the eoi mechanism,
728 * not injecting any more pirqs of the same kind if the first one
729 * hasn't received an eoi yet. Therefore using the fasteoi handler
730 * is the right choice either way.
731 */
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100732 if (shareable)
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100733 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
734 handle_fasteoi_irq, name);
735 else
736 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
737 handle_edge_irq, name);
738
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400739out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400740 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400741
742 return irq;
743}
744
Qing Hef731e3ef2010-10-11 15:30:09 +0100745#ifdef CONFIG_PCI_MSI
Ian Campbellbf480d92011-02-18 16:43:32 +0000746int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000747{
Ian Campbell5cad61a2011-02-18 16:43:31 +0000748 int rc;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000749 struct physdev_get_free_pirq op_get_free_pirq;
Ian Campbell5cad61a2011-02-18 16:43:31 +0000750
Ian Campbellbf480d92011-02-18 16:43:32 +0000751 op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000752 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000753
Ian Campbell5cad61a2011-02-18 16:43:31 +0000754 WARN_ONCE(rc == -ENOSYS,
755 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
756
757 return rc ? -1 : op_get_free_pirq.pirq;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000758}
759
Ian Campbellbf480d92011-02-18 16:43:32 +0000760int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
Jan Beulichdec02de2013-04-03 15:52:50 +0100761 int pirq, const char *name, domid_t domid)
Stefano Stabellini809f9262010-07-01 17:10:39 +0100762{
Ian Campbellbf480d92011-02-18 16:43:32 +0000763 int irq, ret;
Ian Campbell4b41df72011-02-18 16:43:29 +0000764
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400765 mutex_lock(&irq_mapping_update_lock);
Stefano Stabellini809f9262010-07-01 17:10:39 +0100766
Ian Campbell4b41df72011-02-18 16:43:29 +0000767 irq = xen_allocate_irq_dynamic();
Konrad Rzeszutek Wilke6599222011-09-29 13:26:45 -0400768 if (irq < 0)
Ian Campbellbb5d0792011-02-18 16:43:28 +0000769 goto out;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100770
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100771 irq_set_chip_and_handler_name(irq, &xen_pirq_chip, handle_edge_irq,
772 name);
Stefano Stabellini809f9262010-07-01 17:10:39 +0100773
Jan Beulichdec02de2013-04-03 15:52:50 +0100774 xen_irq_info_pirq_init(irq, 0, pirq, 0, domid, 0);
Linus Torvalds5f6fb452011-03-15 19:23:40 -0700775 ret = irq_set_msi_desc(irq, msidesc);
Ian Campbellbf480d92011-02-18 16:43:32 +0000776 if (ret < 0)
777 goto error_irq;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100778out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400779 mutex_unlock(&irq_mapping_update_lock);
Ian Campbell4b41df72011-02-18 16:43:29 +0000780 return irq;
Ian Campbellbf480d92011-02-18 16:43:32 +0000781error_irq:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400782 mutex_unlock(&irq_mapping_update_lock);
Ian Campbellbf480d92011-02-18 16:43:32 +0000783 xen_free_irq(irq);
Konrad Rzeszutek Wilke6599222011-09-29 13:26:45 -0400784 return ret;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100785}
Qing Hef731e3ef2010-10-11 15:30:09 +0100786#endif
787
Alex Nixonb5401a92010-03-18 16:31:34 -0400788int xen_destroy_irq(int irq)
789{
790 struct irq_desc *desc;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100791 struct physdev_unmap_pirq unmap_irq;
792 struct irq_info *info = info_for_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400793 int rc = -ENOENT;
794
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400795 mutex_lock(&irq_mapping_update_lock);
Alex Nixonb5401a92010-03-18 16:31:34 -0400796
797 desc = irq_to_desc(irq);
798 if (!desc)
799 goto out;
800
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100801 if (xen_initial_domain()) {
Konrad Rzeszutek Wilk12334712010-11-19 11:27:09 -0500802 unmap_irq.pirq = info->u.pirq.pirq;
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400803 unmap_irq.domid = info->u.pirq.domid;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100804 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
Konrad Rzeszutek Wilk1eff1ad2011-02-16 16:26:44 -0500805 /* If another domain quits without making the pci_disable_msix
806 * call, the Xen hypervisor takes care of freeing the PIRQs
807 * (free_domain_pirqs).
808 */
809 if ((rc == -ESRCH && info->u.pirq.domid != DOMID_SELF))
810 printk(KERN_INFO "domain %d does not have %d anymore\n",
811 info->u.pirq.domid, info->u.pirq.pirq);
812 else if (rc) {
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100813 printk(KERN_WARNING "unmap irq failed %d\n", rc);
814 goto out;
815 }
816 }
Alex Nixonb5401a92010-03-18 16:31:34 -0400817
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000818 xen_free_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400819
820out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400821 mutex_unlock(&irq_mapping_update_lock);
Alex Nixonb5401a92010-03-18 16:31:34 -0400822 return rc;
823}
824
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000825int xen_irq_from_pirq(unsigned pirq)
826{
Ian Campbell69c358c2011-03-10 16:08:13 +0000827 int irq;
828
829 struct irq_info *info;
830
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400831 mutex_lock(&irq_mapping_update_lock);
Ian Campbell69c358c2011-03-10 16:08:13 +0000832
833 list_for_each_entry(info, &xen_irq_list_head, list) {
Konrad Rzeszutek Wilk9bb9efe2011-09-29 13:13:30 -0400834 if (info->type != IRQT_PIRQ)
Ian Campbell69c358c2011-03-10 16:08:13 +0000835 continue;
836 irq = info->irq;
837 if (info->u.pirq.pirq == pirq)
838 goto out;
839 }
840 irq = -1;
841out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400842 mutex_unlock(&irq_mapping_update_lock);
Ian Campbell69c358c2011-03-10 16:08:13 +0000843
844 return irq;
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000845}
846
Konrad Rzeszutek Wilke6197ac2011-02-24 14:20:12 -0500847
848int xen_pirq_from_irq(unsigned irq)
849{
850 return pirq_from_irq(irq);
851}
852EXPORT_SYMBOL_GPL(xen_pirq_from_irq);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700853int bind_evtchn_to_irq(unsigned int evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700854{
855 int irq;
856
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400857 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700858
859 irq = evtchn_to_irq[evtchn];
860
861 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000862 irq = xen_allocate_irq_dynamic();
Wei Liu68ba45f2013-01-31 14:46:56 +0000863 if (irq < 0)
Ian Campbell7bee9762011-03-10 16:08:15 +0000864 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700865
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100866 irq_set_chip_and_handler_name(irq, &xen_dynamic_chip,
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100867 handle_edge_irq, "event");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700868
Ian Campbell9158c352011-03-10 16:08:09 +0000869 xen_irq_info_evtchn_init(irq, evtchn);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400870 } else {
871 struct irq_info *info = info_for_irq(irq);
872 WARN_ON(info == NULL || info->type != IRQT_EVTCHN);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700873 }
Stefano Stabellinia8636c02012-08-22 17:20:15 +0100874 irq_clear_status_flags(irq, IRQ_NOREQUEST|IRQ_NOAUTOEN);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700875
Ian Campbell7bee9762011-03-10 16:08:15 +0000876out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400877 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700878
879 return irq;
880}
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700881EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700882
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700883static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
884{
885 struct evtchn_bind_ipi bind_ipi;
886 int evtchn, irq;
887
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400888 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700889
890 irq = per_cpu(ipi_to_irq, cpu)[ipi];
Ian Campbell90af9512009-02-06 16:55:58 -0800891
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700892 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000893 irq = xen_allocate_irq_dynamic();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700894 if (irq < 0)
895 goto out;
896
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100897 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700898 handle_percpu_irq, "ipi");
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700899
900 bind_ipi.vcpu = cpu;
901 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
902 &bind_ipi) != 0)
903 BUG();
904 evtchn = bind_ipi.port;
905
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000906 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700907
908 bind_evtchn_to_cpu(evtchn, cpu);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400909 } else {
910 struct irq_info *info = info_for_irq(irq);
911 WARN_ON(info == NULL || info->type != IRQT_IPI);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700912 }
913
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700914 out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400915 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700916 return irq;
917}
918
Ian Campbell2e820f52009-02-09 12:05:50 -0800919static int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
920 unsigned int remote_port)
921{
922 struct evtchn_bind_interdomain bind_interdomain;
923 int err;
924
925 bind_interdomain.remote_dom = remote_domain;
926 bind_interdomain.remote_port = remote_port;
927
928 err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
929 &bind_interdomain);
930
931 return err ? : bind_evtchn_to_irq(bind_interdomain.local_port);
932}
933
Olaf Hering62cc5fc2011-08-25 18:30:48 +0200934static int find_virq(unsigned int virq, unsigned int cpu)
935{
936 struct evtchn_status status;
937 int port, rc = -ENOENT;
938
939 memset(&status, 0, sizeof(status));
940 for (port = 0; port <= NR_EVENT_CHANNELS; port++) {
941 status.dom = DOMID_SELF;
942 status.port = port;
943 rc = HYPERVISOR_event_channel_op(EVTCHNOP_status, &status);
944 if (rc < 0)
945 continue;
946 if (status.status != EVTCHNSTAT_virq)
947 continue;
948 if (status.u.virq == virq && status.vcpu == cpu) {
949 rc = port;
950 break;
951 }
952 }
953 return rc;
954}
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700955
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100956int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700957{
958 struct evtchn_bind_virq bind_virq;
Olaf Hering62cc5fc2011-08-25 18:30:48 +0200959 int evtchn, irq, ret;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700960
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400961 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700962
963 irq = per_cpu(virq_to_irq, cpu)[virq];
964
965 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000966 irq = xen_allocate_irq_dynamic();
Wei Liu68ba45f2013-01-31 14:46:56 +0000967 if (irq < 0)
Ian Campbell7bee9762011-03-10 16:08:15 +0000968 goto out;
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700969
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100970 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700971 handle_percpu_irq, "virq");
972
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700973 bind_virq.virq = virq;
974 bind_virq.vcpu = cpu;
Olaf Hering62cc5fc2011-08-25 18:30:48 +0200975 ret = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
976 &bind_virq);
977 if (ret == 0)
978 evtchn = bind_virq.port;
979 else {
980 if (ret == -EEXIST)
981 ret = find_virq(virq, cpu);
982 BUG_ON(ret < 0);
983 evtchn = ret;
984 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700985
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000986 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700987
988 bind_evtchn_to_cpu(evtchn, cpu);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400989 } else {
990 struct irq_info *info = info_for_irq(irq);
991 WARN_ON(info == NULL || info->type != IRQT_VIRQ);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700992 }
993
Ian Campbell7bee9762011-03-10 16:08:15 +0000994out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400995 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700996
997 return irq;
998}
999
1000static void unbind_from_irq(unsigned int irq)
1001{
1002 struct evtchn_close close;
1003 int evtchn = evtchn_from_irq(irq);
Daniel De Graaf420eb552011-10-27 17:58:47 -04001004 struct irq_info *info = irq_get_handler_data(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001005
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001006 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001007
Daniel De Graaf420eb552011-10-27 17:58:47 -04001008 if (info->refcnt > 0) {
1009 info->refcnt--;
1010 if (info->refcnt != 0)
1011 goto done;
1012 }
1013
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001014 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001015 close.port = evtchn;
1016 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
1017 BUG();
1018
1019 switch (type_from_irq(irq)) {
1020 case IRQT_VIRQ:
1021 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001022 [virq_from_irq(irq)] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001023 break;
Alex Nixond68d82a2008-08-22 11:52:15 +01001024 case IRQT_IPI:
1025 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001026 [ipi_from_irq(irq)] = -1;
Alex Nixond68d82a2008-08-22 11:52:15 +01001027 break;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001028 default:
1029 break;
1030 }
1031
1032 /* Closed ports are implicitly re-bound to VCPU0. */
1033 bind_evtchn_to_cpu(evtchn, 0);
1034
1035 evtchn_to_irq[evtchn] = -1;
Ian Campbellfed5ea82009-12-01 16:15:30 +00001036 }
1037
Ian Campbellca62ce82011-03-10 16:08:12 +00001038 BUG_ON(info_for_irq(irq)->type == IRQT_UNBOUND);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001039
Ian Campbell9158c352011-03-10 16:08:09 +00001040 xen_free_irq(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001041
Daniel De Graaf420eb552011-10-27 17:58:47 -04001042 done:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001043 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001044}
1045
1046int bind_evtchn_to_irqhandler(unsigned int evtchn,
Jeff Garzik7c239972007-10-19 03:12:20 -04001047 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001048 unsigned long irqflags,
1049 const char *devname, void *dev_id)
1050{
Nicolas Kaiser361ae8c2011-03-30 21:14:26 +02001051 int irq, retval;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001052
1053 irq = bind_evtchn_to_irq(evtchn);
Ian Campbell7bee9762011-03-10 16:08:15 +00001054 if (irq < 0)
1055 return irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001056 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1057 if (retval != 0) {
1058 unbind_from_irq(irq);
1059 return retval;
1060 }
1061
1062 return irq;
1063}
1064EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
1065
Ian Campbell2e820f52009-02-09 12:05:50 -08001066int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
1067 unsigned int remote_port,
1068 irq_handler_t handler,
1069 unsigned long irqflags,
1070 const char *devname,
1071 void *dev_id)
1072{
1073 int irq, retval;
1074
1075 irq = bind_interdomain_evtchn_to_irq(remote_domain, remote_port);
1076 if (irq < 0)
1077 return irq;
1078
1079 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1080 if (retval != 0) {
1081 unbind_from_irq(irq);
1082 return retval;
1083 }
1084
1085 return irq;
1086}
1087EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler);
1088
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001089int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
Jeff Garzik7c239972007-10-19 03:12:20 -04001090 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001091 unsigned long irqflags, const char *devname, void *dev_id)
1092{
Nicolas Kaiser361ae8c2011-03-30 21:14:26 +02001093 int irq, retval;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001094
1095 irq = bind_virq_to_irq(virq, cpu);
Ian Campbell7bee9762011-03-10 16:08:15 +00001096 if (irq < 0)
1097 return irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001098 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1099 if (retval != 0) {
1100 unbind_from_irq(irq);
1101 return retval;
1102 }
1103
1104 return irq;
1105}
1106EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
1107
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001108int bind_ipi_to_irqhandler(enum ipi_vector ipi,
1109 unsigned int cpu,
1110 irq_handler_t handler,
1111 unsigned long irqflags,
1112 const char *devname,
1113 void *dev_id)
1114{
1115 int irq, retval;
1116
1117 irq = bind_ipi_to_irq(ipi, cpu);
1118 if (irq < 0)
1119 return irq;
1120
Ian Campbell9bab0b72011-10-03 15:37:00 +01001121 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME | IRQF_EARLY_RESUME;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001122 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1123 if (retval != 0) {
1124 unbind_from_irq(irq);
1125 return retval;
1126 }
1127
1128 return irq;
1129}
1130
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001131void unbind_from_irqhandler(unsigned int irq, void *dev_id)
1132{
1133 free_irq(irq, dev_id);
1134 unbind_from_irq(irq);
1135}
1136EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
1137
Daniel De Graaf420eb552011-10-27 17:58:47 -04001138int evtchn_make_refcounted(unsigned int evtchn)
1139{
1140 int irq = evtchn_to_irq[evtchn];
1141 struct irq_info *info;
1142
1143 if (irq == -1)
1144 return -ENOENT;
1145
1146 info = irq_get_handler_data(irq);
1147
1148 if (!info)
1149 return -ENOENT;
1150
1151 WARN_ON(info->refcnt != -1);
1152
1153 info->refcnt = 1;
1154
1155 return 0;
1156}
1157EXPORT_SYMBOL_GPL(evtchn_make_refcounted);
1158
1159int evtchn_get(unsigned int evtchn)
1160{
1161 int irq;
1162 struct irq_info *info;
1163 int err = -ENOENT;
1164
Daniel De Graafc3b3f162011-11-28 11:49:09 -05001165 if (evtchn >= NR_EVENT_CHANNELS)
1166 return -EINVAL;
1167
Daniel De Graaf420eb552011-10-27 17:58:47 -04001168 mutex_lock(&irq_mapping_update_lock);
1169
1170 irq = evtchn_to_irq[evtchn];
1171 if (irq == -1)
1172 goto done;
1173
1174 info = irq_get_handler_data(irq);
1175
1176 if (!info)
1177 goto done;
1178
1179 err = -EINVAL;
1180 if (info->refcnt <= 0)
1181 goto done;
1182
1183 info->refcnt++;
1184 err = 0;
1185 done:
1186 mutex_unlock(&irq_mapping_update_lock);
1187
1188 return err;
1189}
1190EXPORT_SYMBOL_GPL(evtchn_get);
1191
1192void evtchn_put(unsigned int evtchn)
1193{
1194 int irq = evtchn_to_irq[evtchn];
1195 if (WARN_ON(irq == -1))
1196 return;
1197 unbind_from_irq(irq);
1198}
1199EXPORT_SYMBOL_GPL(evtchn_put);
1200
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001201void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
1202{
1203 int irq = per_cpu(ipi_to_irq, cpu)[vector];
1204 BUG_ON(irq < 0);
1205 notify_remote_via_irq(irq);
1206}
1207
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001208irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
1209{
1210 struct shared_info *sh = HYPERVISOR_shared_info;
1211 int cpu = smp_processor_id();
Ian Campbellc81611c2013-02-20 11:48:06 +00001212 xen_ulong_t *cpu_evtchn = per_cpu(cpu_evtchn_mask, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001213 int i;
1214 unsigned long flags;
1215 static DEFINE_SPINLOCK(debug_lock);
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001216 struct vcpu_info *v;
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001217
1218 spin_lock_irqsave(&debug_lock, flags);
1219
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001220 printk("\nvcpu %d\n ", cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001221
1222 for_each_online_cpu(i) {
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001223 int pending;
1224 v = per_cpu(xen_vcpu, i);
1225 pending = (get_irq_regs() && i == cpu)
1226 ? xen_irqs_disabled(get_irq_regs())
1227 : v->evtchn_upcall_mask;
Ian Campbellc81611c2013-02-20 11:48:06 +00001228 printk("%d: masked=%d pending=%d event_sel %0*"PRI_xen_ulong"\n ", i,
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001229 pending, v->evtchn_upcall_pending,
1230 (int)(sizeof(v->evtchn_pending_sel)*2),
1231 v->evtchn_pending_sel);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001232 }
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001233 v = per_cpu(xen_vcpu, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001234
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001235 printk("\npending:\n ");
1236 for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
Ian Campbellc81611c2013-02-20 11:48:06 +00001237 printk("%0*"PRI_xen_ulong"%s",
1238 (int)sizeof(sh->evtchn_pending[0])*2,
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001239 sh->evtchn_pending[i],
1240 i % 8 == 0 ? "\n " : " ");
1241 printk("\nglobal mask:\n ");
1242 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
Ian Campbellc81611c2013-02-20 11:48:06 +00001243 printk("%0*"PRI_xen_ulong"%s",
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001244 (int)(sizeof(sh->evtchn_mask[0])*2),
1245 sh->evtchn_mask[i],
1246 i % 8 == 0 ? "\n " : " ");
1247
1248 printk("\nglobally unmasked:\n ");
1249 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
Ian Campbellc81611c2013-02-20 11:48:06 +00001250 printk("%0*"PRI_xen_ulong"%s",
1251 (int)(sizeof(sh->evtchn_mask[0])*2),
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001252 sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
1253 i % 8 == 0 ? "\n " : " ");
1254
1255 printk("\nlocal cpu%d mask:\n ", cpu);
Ian Campbellc81611c2013-02-20 11:48:06 +00001256 for (i = (NR_EVENT_CHANNELS/BITS_PER_EVTCHN_WORD)-1; i >= 0; i--)
1257 printk("%0*"PRI_xen_ulong"%s", (int)(sizeof(cpu_evtchn[0])*2),
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001258 cpu_evtchn[i],
1259 i % 8 == 0 ? "\n " : " ");
1260
1261 printk("\nlocally unmasked:\n ");
1262 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) {
Ian Campbellc81611c2013-02-20 11:48:06 +00001263 xen_ulong_t pending = sh->evtchn_pending[i]
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001264 & ~sh->evtchn_mask[i]
1265 & cpu_evtchn[i];
Ian Campbellc81611c2013-02-20 11:48:06 +00001266 printk("%0*"PRI_xen_ulong"%s",
1267 (int)(sizeof(sh->evtchn_mask[0])*2),
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001268 pending, i % 8 == 0 ? "\n " : " ");
1269 }
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001270
1271 printk("\npending list:\n");
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001272 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
Ian Campbellc81611c2013-02-20 11:48:06 +00001273 if (sync_test_bit(i, BM(sh->evtchn_pending))) {
1274 int word_idx = i / BITS_PER_EVTCHN_WORD;
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001275 printk(" %d: event %d -> irq %d%s%s%s\n",
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001276 cpu_from_evtchn(i), i,
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001277 evtchn_to_irq[i],
Ian Campbellc81611c2013-02-20 11:48:06 +00001278 sync_test_bit(word_idx, BM(&v->evtchn_pending_sel))
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001279 ? "" : " l2-clear",
Ian Campbellc81611c2013-02-20 11:48:06 +00001280 !sync_test_bit(i, BM(sh->evtchn_mask))
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001281 ? "" : " globally-masked",
Ian Campbellc81611c2013-02-20 11:48:06 +00001282 sync_test_bit(i, BM(cpu_evtchn))
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001283 ? "" : " locally-masked");
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001284 }
1285 }
1286
1287 spin_unlock_irqrestore(&debug_lock, flags);
1288
1289 return IRQ_HANDLED;
1290}
1291
Tejun Heo245b2e72009-06-24 15:13:48 +09001292static DEFINE_PER_CPU(unsigned, xed_nesting_count);
Keir Fraserada68142011-03-03 10:01:11 +00001293static DEFINE_PER_CPU(unsigned int, current_word_idx);
1294static DEFINE_PER_CPU(unsigned int, current_bit_idx);
Tejun Heo245b2e72009-06-24 15:13:48 +09001295
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001296/*
Scott Rixnerab7f8632011-03-03 09:30:08 +00001297 * Mask out the i least significant bits of w
1298 */
Ian Campbellc81611c2013-02-20 11:48:06 +00001299#define MASK_LSBS(w, i) (w & ((~((xen_ulong_t)0UL)) << i))
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001300
1301/*
1302 * Search the CPUs pending events bitmasks. For each one found, map
1303 * the event number to an irq, and feed it into do_IRQ() for
1304 * handling.
1305 *
1306 * Xen uses a two-level bitmap to speed searching. The first level is
1307 * a bitset of words which contain pending event bits. The second
1308 * level is a bitset of pending events themselves.
1309 */
Sheng Yang38e20b02010-05-14 12:40:51 +01001310static void __xen_evtchn_do_upcall(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001311{
Keir Fraser24b51c22011-03-03 11:06:28 +00001312 int start_word_idx, start_bit_idx;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001313 int word_idx, bit_idx;
Keir Fraser24b51c22011-03-03 11:06:28 +00001314 int i;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001315 int cpu = get_cpu();
1316 struct shared_info *s = HYPERVISOR_shared_info;
Christoph Lameter780f36d2010-12-06 11:16:29 -06001317 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
Ruslan Pisarev088c05a2011-07-26 14:16:13 +03001318 unsigned count;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001319
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001320 do {
Ian Campbellc81611c2013-02-20 11:48:06 +00001321 xen_ulong_t pending_words;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001322
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001323 vcpu_info->evtchn_upcall_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001324
Christoph Lameterb2e4ae62010-12-06 11:40:07 -06001325 if (__this_cpu_inc_return(xed_nesting_count) - 1)
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001326 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001327
Ian Campbellc81611c2013-02-20 11:48:06 +00001328 /*
1329 * Master flag must be cleared /before/ clearing
1330 * selector flag. xchg_xen_ulong must contain an
1331 * appropriate barrier.
1332 */
1333 pending_words = xchg_xen_ulong(&vcpu_info->evtchn_pending_sel, 0);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001334
Keir Fraser24b51c22011-03-03 11:06:28 +00001335 start_word_idx = __this_cpu_read(current_word_idx);
1336 start_bit_idx = __this_cpu_read(current_bit_idx);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001337
Keir Fraser24b51c22011-03-03 11:06:28 +00001338 word_idx = start_word_idx;
1339
1340 for (i = 0; pending_words != 0; i++) {
Ian Campbellc81611c2013-02-20 11:48:06 +00001341 xen_ulong_t pending_bits;
1342 xen_ulong_t words;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001343
Scott Rixnerab7f8632011-03-03 09:30:08 +00001344 words = MASK_LSBS(pending_words, word_idx);
1345
1346 /*
Keir Fraserada68142011-03-03 10:01:11 +00001347 * If we masked out all events, wrap to beginning.
Scott Rixnerab7f8632011-03-03 09:30:08 +00001348 */
1349 if (words == 0) {
Keir Fraserada68142011-03-03 10:01:11 +00001350 word_idx = 0;
1351 bit_idx = 0;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001352 continue;
1353 }
Ian Campbellc81611c2013-02-20 11:48:06 +00001354 word_idx = EVTCHN_FIRST_BIT(words);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001355
Keir Fraser24b51c22011-03-03 11:06:28 +00001356 pending_bits = active_evtchns(cpu, s, word_idx);
1357 bit_idx = 0; /* usually scan entire word from start */
1358 if (word_idx == start_word_idx) {
1359 /* We scan the starting word in two parts */
1360 if (i == 0)
1361 /* 1st time: start in the middle */
1362 bit_idx = start_bit_idx;
1363 else
1364 /* 2nd time: mask bits done already */
1365 bit_idx &= (1UL << start_bit_idx) - 1;
1366 }
1367
Scott Rixnerab7f8632011-03-03 09:30:08 +00001368 do {
Ian Campbellc81611c2013-02-20 11:48:06 +00001369 xen_ulong_t bits;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001370 int port, irq;
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -08001371 struct irq_desc *desc;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001372
Scott Rixnerab7f8632011-03-03 09:30:08 +00001373 bits = MASK_LSBS(pending_bits, bit_idx);
1374
1375 /* If we masked out all events, move on. */
Keir Fraserada68142011-03-03 10:01:11 +00001376 if (bits == 0)
Scott Rixnerab7f8632011-03-03 09:30:08 +00001377 break;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001378
Ian Campbellc81611c2013-02-20 11:48:06 +00001379 bit_idx = EVTCHN_FIRST_BIT(bits);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001380
1381 /* Process port. */
Ian Campbellc81611c2013-02-20 11:48:06 +00001382 port = (word_idx * BITS_PER_EVTCHN_WORD) + bit_idx;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001383 irq = evtchn_to_irq[port];
1384
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -08001385 if (irq != -1) {
1386 desc = irq_to_desc(irq);
1387 if (desc)
1388 generic_handle_irq_desc(irq, desc);
1389 }
Scott Rixnerab7f8632011-03-03 09:30:08 +00001390
Ian Campbellc81611c2013-02-20 11:48:06 +00001391 bit_idx = (bit_idx + 1) % BITS_PER_EVTCHN_WORD;
Keir Fraserada68142011-03-03 10:01:11 +00001392
1393 /* Next caller starts at last processed + 1 */
1394 __this_cpu_write(current_word_idx,
1395 bit_idx ? word_idx :
Ian Campbellc81611c2013-02-20 11:48:06 +00001396 (word_idx+1) % BITS_PER_EVTCHN_WORD);
Keir Fraserada68142011-03-03 10:01:11 +00001397 __this_cpu_write(current_bit_idx, bit_idx);
1398 } while (bit_idx != 0);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001399
Keir Fraser24b51c22011-03-03 11:06:28 +00001400 /* Scan start_l1i twice; all others once. */
1401 if ((word_idx != start_word_idx) || (i != 0))
Scott Rixnerab7f8632011-03-03 09:30:08 +00001402 pending_words &= ~(1UL << word_idx);
Keir Fraserada68142011-03-03 10:01:11 +00001403
Ian Campbellc81611c2013-02-20 11:48:06 +00001404 word_idx = (word_idx + 1) % BITS_PER_EVTCHN_WORD;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001405 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001406
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001407 BUG_ON(!irqs_disabled());
1408
Christoph Lameter780f36d2010-12-06 11:16:29 -06001409 count = __this_cpu_read(xed_nesting_count);
1410 __this_cpu_write(xed_nesting_count, 0);
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001411 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001412
1413out:
Jeremy Fitzhardinge3445a8f2009-02-06 14:09:46 -08001414
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001415 put_cpu();
1416}
1417
Sheng Yang38e20b02010-05-14 12:40:51 +01001418void xen_evtchn_do_upcall(struct pt_regs *regs)
1419{
1420 struct pt_regs *old_regs = set_irq_regs(regs);
1421
Mojiong Qiu772aebc2012-11-06 16:08:15 +08001422 irq_enter();
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001423#ifdef CONFIG_X86
Sheng Yang38e20b02010-05-14 12:40:51 +01001424 exit_idle();
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001425#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001426
1427 __xen_evtchn_do_upcall();
1428
1429 irq_exit();
1430 set_irq_regs(old_regs);
1431}
1432
1433void xen_hvm_evtchn_do_upcall(void)
1434{
1435 __xen_evtchn_do_upcall();
1436}
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001437EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
Sheng Yang38e20b02010-05-14 12:40:51 +01001438
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001439/* Rebind a new event channel to an existing irq. */
1440void rebind_evtchn_irq(int evtchn, int irq)
1441{
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001442 struct irq_info *info = info_for_irq(irq);
1443
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001444 /* Make sure the irq is masked, since the new event channel
1445 will also be masked. */
1446 disable_irq(irq);
1447
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001448 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001449
1450 /* After resume the irq<->evtchn mappings are all cleared out */
1451 BUG_ON(evtchn_to_irq[evtchn] != -1);
1452 /* Expect irq to have been bound before,
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001453 so there should be a proper type */
1454 BUG_ON(info->type == IRQT_UNBOUND);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001455
Ian Campbell9158c352011-03-10 16:08:09 +00001456 xen_irq_info_evtchn_init(irq, evtchn);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001457
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001458 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001459
1460 /* new event channels are always bound to cpu 0 */
Rusty Russell0de26522008-12-13 21:20:26 +10301461 irq_set_affinity(irq, cpumask_of(0));
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001462
1463 /* Unmask the event channel. */
1464 enable_irq(irq);
1465}
1466
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001467/* Rebind an evtchn so that it gets delivered to a specific cpu */
Yinghai Lud5dedd42009-04-27 17:59:21 -07001468static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001469{
1470 struct evtchn_bind_vcpu bind_vcpu;
1471 int evtchn = evtchn_from_irq(irq);
1472
Ian Campbellbe494722011-03-10 16:08:02 +00001473 if (!VALID_EVTCHN(evtchn))
1474 return -1;
1475
1476 /*
1477 * Events delivered via platform PCI interrupts are always
1478 * routed to vcpu 0 and hence cannot be rebound.
1479 */
1480 if (xen_hvm_domain() && !xen_have_vector_callback)
Yinghai Lud5dedd42009-04-27 17:59:21 -07001481 return -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001482
1483 /* Send future instances of this interrupt to other vcpu. */
1484 bind_vcpu.port = evtchn;
1485 bind_vcpu.vcpu = tcpu;
1486
1487 /*
1488 * If this fails, it usually just indicates that we're dealing with a
1489 * virq or IPI channel, which don't actually need to be rebound. Ignore
1490 * it, but don't do the xenlinux-level rebind in that case.
1491 */
1492 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1493 bind_evtchn_to_cpu(evtchn, tcpu);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001494
1495 return 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001496}
1497
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001498static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1499 bool force)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001500{
Rusty Russell0de26522008-12-13 21:20:26 +10301501 unsigned tcpu = cpumask_first(dest);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001502
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001503 return rebind_irq_to_cpu(data->irq, tcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001504}
1505
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001506int resend_irq_on_evtchn(unsigned int irq)
1507{
1508 int masked, evtchn = evtchn_from_irq(irq);
1509 struct shared_info *s = HYPERVISOR_shared_info;
1510
1511 if (!VALID_EVTCHN(evtchn))
1512 return 1;
1513
Ian Campbellc81611c2013-02-20 11:48:06 +00001514 masked = sync_test_and_set_bit(evtchn, BM(s->evtchn_mask));
1515 sync_set_bit(evtchn, BM(s->evtchn_pending));
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001516 if (!masked)
1517 unmask_evtchn(evtchn);
1518
1519 return 1;
1520}
1521
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001522static void enable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001523{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001524 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001525
1526 if (VALID_EVTCHN(evtchn))
1527 unmask_evtchn(evtchn);
1528}
1529
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001530static void disable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001531{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001532 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001533
1534 if (VALID_EVTCHN(evtchn))
1535 mask_evtchn(evtchn);
1536}
1537
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001538static void ack_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001539{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001540 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001541
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001542 irq_move_irq(data);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001543
1544 if (VALID_EVTCHN(evtchn))
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001545 clear_evtchn(evtchn);
1546}
1547
1548static void mask_ack_dynirq(struct irq_data *data)
1549{
1550 disable_dynirq(data);
1551 ack_dynirq(data);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001552}
1553
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001554static int retrigger_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 Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001557 struct shared_info *sh = HYPERVISOR_shared_info;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001558 int ret = 0;
1559
1560 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001561 int masked;
1562
Ian Campbellc81611c2013-02-20 11:48:06 +00001563 masked = sync_test_and_set_bit(evtchn, BM(sh->evtchn_mask));
1564 sync_set_bit(evtchn, BM(sh->evtchn_pending));
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001565 if (!masked)
1566 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001567 ret = 1;
1568 }
1569
1570 return ret;
1571}
1572
Ian Campbell0a852262011-03-10 16:08:06 +00001573static void restore_pirqs(void)
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001574{
1575 int pirq, rc, irq, gsi;
1576 struct physdev_map_pirq map_irq;
Ian Campbell69c358c2011-03-10 16:08:13 +00001577 struct irq_info *info;
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001578
Ian Campbell69c358c2011-03-10 16:08:13 +00001579 list_for_each_entry(info, &xen_irq_list_head, list) {
1580 if (info->type != IRQT_PIRQ)
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001581 continue;
1582
Ian Campbell69c358c2011-03-10 16:08:13 +00001583 pirq = info->u.pirq.pirq;
1584 gsi = info->u.pirq.gsi;
1585 irq = info->irq;
1586
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001587 /* save/restore of PT devices doesn't work, so at this point the
1588 * only devices present are GSI based emulated devices */
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001589 if (!gsi)
1590 continue;
1591
1592 map_irq.domid = DOMID_SELF;
1593 map_irq.type = MAP_PIRQ_TYPE_GSI;
1594 map_irq.index = gsi;
1595 map_irq.pirq = pirq;
1596
1597 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1598 if (rc) {
1599 printk(KERN_WARNING "xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1600 gsi, irq, pirq, rc);
Ian Campbell9158c352011-03-10 16:08:09 +00001601 xen_free_irq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001602 continue;
1603 }
1604
1605 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1606
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001607 __startup_pirq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001608 }
1609}
1610
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001611static void restore_cpu_virqs(unsigned int cpu)
1612{
1613 struct evtchn_bind_virq bind_virq;
1614 int virq, irq, evtchn;
1615
1616 for (virq = 0; virq < NR_VIRQS; virq++) {
1617 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1618 continue;
1619
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001620 BUG_ON(virq_from_irq(irq) != virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001621
1622 /* Get a new binding from Xen. */
1623 bind_virq.virq = virq;
1624 bind_virq.vcpu = cpu;
1625 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1626 &bind_virq) != 0)
1627 BUG();
1628 evtchn = bind_virq.port;
1629
1630 /* Record the new mapping. */
Ian Campbell3d4cfa32011-03-10 16:08:10 +00001631 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001632 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001633 }
1634}
1635
1636static void restore_cpu_ipis(unsigned int cpu)
1637{
1638 struct evtchn_bind_ipi bind_ipi;
1639 int ipi, irq, evtchn;
1640
1641 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1642 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1643 continue;
1644
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001645 BUG_ON(ipi_from_irq(irq) != ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001646
1647 /* Get a new binding from Xen. */
1648 bind_ipi.vcpu = cpu;
1649 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1650 &bind_ipi) != 0)
1651 BUG();
1652 evtchn = bind_ipi.port;
1653
1654 /* Record the new mapping. */
Ian Campbell3d4cfa32011-03-10 16:08:10 +00001655 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001656 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001657 }
1658}
1659
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001660/* Clear an irq's pending state, in preparation for polling on it */
1661void xen_clear_irq_pending(int irq)
1662{
1663 int evtchn = evtchn_from_irq(irq);
1664
1665 if (VALID_EVTCHN(evtchn))
1666 clear_evtchn(evtchn);
1667}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001668EXPORT_SYMBOL(xen_clear_irq_pending);
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -07001669void xen_set_irq_pending(int irq)
1670{
1671 int evtchn = evtchn_from_irq(irq);
1672
1673 if (VALID_EVTCHN(evtchn))
1674 set_evtchn(evtchn);
1675}
1676
1677bool xen_test_irq_pending(int irq)
1678{
1679 int evtchn = evtchn_from_irq(irq);
1680 bool ret = false;
1681
1682 if (VALID_EVTCHN(evtchn))
1683 ret = test_evtchn(evtchn);
1684
1685 return ret;
1686}
1687
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001688/* Poll waiting for an irq to become pending with timeout. In the usual case,
1689 * the irq will be disabled so it won't deliver an interrupt. */
1690void xen_poll_irq_timeout(int irq, u64 timeout)
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001691{
1692 evtchn_port_t evtchn = evtchn_from_irq(irq);
1693
1694 if (VALID_EVTCHN(evtchn)) {
1695 struct sched_poll poll;
1696
1697 poll.nr_ports = 1;
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001698 poll.timeout = timeout;
Isaku Yamahataff3c5362008-10-14 17:50:44 -07001699 set_xen_guest_handle(poll.ports, &evtchn);
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001700
1701 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1702 BUG();
1703 }
1704}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001705EXPORT_SYMBOL(xen_poll_irq_timeout);
1706/* Poll waiting for an irq to become pending. In the usual case, the
1707 * irq will be disabled so it won't deliver an interrupt. */
1708void xen_poll_irq(int irq)
1709{
1710 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1711}
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001712
Konrad Rzeszutek Wilkc7c2c3a2010-11-08 14:26:36 -05001713/* Check whether the IRQ line is shared with other guests. */
1714int xen_test_irq_shared(int irq)
1715{
1716 struct irq_info *info = info_for_irq(irq);
1717 struct physdev_irq_status_query irq_status = { .irq = info->u.pirq.pirq };
1718
1719 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
1720 return 0;
1721 return !(irq_status.flags & XENIRQSTAT_shared);
1722}
1723EXPORT_SYMBOL_GPL(xen_test_irq_shared);
1724
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001725void xen_irq_resume(void)
1726{
Ian Campbell6cb65372011-03-10 16:08:11 +00001727 unsigned int cpu, evtchn;
1728 struct irq_info *info;
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001729
1730 init_evtchn_cpu_bindings();
1731
1732 /* New event-channel space is not 'live' yet. */
1733 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1734 mask_evtchn(evtchn);
1735
1736 /* No IRQ <-> event-channel mappings. */
Ian Campbell6cb65372011-03-10 16:08:11 +00001737 list_for_each_entry(info, &xen_irq_list_head, list)
1738 info->evtchn = 0; /* zap event-channel binding */
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001739
1740 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1741 evtchn_to_irq[evtchn] = -1;
1742
1743 for_each_possible_cpu(cpu) {
1744 restore_cpu_virqs(cpu);
1745 restore_cpu_ipis(cpu);
1746 }
Ian Campbell69035912010-11-01 16:30:09 +00001747
Ian Campbell0a852262011-03-10 16:08:06 +00001748 restore_pirqs();
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001749}
1750
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001751static struct irq_chip xen_dynamic_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001752 .name = "xen-dyn",
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001753
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001754 .irq_disable = disable_dynirq,
1755 .irq_mask = disable_dynirq,
1756 .irq_unmask = enable_dynirq,
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001757
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001758 .irq_ack = ack_dynirq,
1759 .irq_mask_ack = mask_ack_dynirq,
1760
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001761 .irq_set_affinity = set_affinity_irq,
1762 .irq_retrigger = retrigger_dynirq,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001763};
1764
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001765static struct irq_chip xen_pirq_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001766 .name = "xen-pirq",
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001767
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001768 .irq_startup = startup_pirq,
1769 .irq_shutdown = shutdown_pirq,
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001770 .irq_enable = enable_pirq,
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001771 .irq_disable = disable_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001772
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001773 .irq_mask = disable_dynirq,
1774 .irq_unmask = enable_dynirq,
1775
1776 .irq_ack = eoi_pirq,
1777 .irq_eoi = eoi_pirq,
1778 .irq_mask_ack = mask_ack_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001779
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001780 .irq_set_affinity = set_affinity_irq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001781
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001782 .irq_retrigger = retrigger_dynirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001783};
1784
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001785static struct irq_chip xen_percpu_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001786 .name = "xen-percpu",
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001787
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001788 .irq_disable = disable_dynirq,
1789 .irq_mask = disable_dynirq,
1790 .irq_unmask = enable_dynirq,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001791
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001792 .irq_ack = ack_dynirq,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001793};
1794
Sheng Yang38e20b02010-05-14 12:40:51 +01001795int xen_set_callback_via(uint64_t via)
1796{
1797 struct xen_hvm_param a;
1798 a.domid = DOMID_SELF;
1799 a.index = HVM_PARAM_CALLBACK_IRQ;
1800 a.value = via;
1801 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1802}
1803EXPORT_SYMBOL_GPL(xen_set_callback_via);
1804
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001805#ifdef CONFIG_XEN_PVHVM
Sheng Yang38e20b02010-05-14 12:40:51 +01001806/* Vector callbacks are better than PCI interrupts to receive event
1807 * channel notifications because we can receive vector callbacks on any
1808 * vcpu and we don't need PCI support or APIC interactions. */
1809void xen_callback_vector(void)
1810{
1811 int rc;
1812 uint64_t callback_via;
1813 if (xen_have_vector_callback) {
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -08001814 callback_via = HVM_CALLBACK_VECTOR(HYPERVISOR_CALLBACK_VECTOR);
Sheng Yang38e20b02010-05-14 12:40:51 +01001815 rc = xen_set_callback_via(callback_via);
1816 if (rc) {
1817 printk(KERN_ERR "Request for Xen HVM callback vector"
1818 " failed.\n");
1819 xen_have_vector_callback = 0;
1820 return;
1821 }
1822 printk(KERN_INFO "Xen HVM callback vector for event delivery is "
1823 "enabled\n");
1824 /* in the restore case the vector has already been allocated */
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -08001825 if (!test_bit(HYPERVISOR_CALLBACK_VECTOR, used_vectors))
1826 alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR,
1827 xen_hvm_callback_vector);
Sheng Yang38e20b02010-05-14 12:40:51 +01001828 }
1829}
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001830#else
1831void xen_callback_vector(void) {}
1832#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001833
Stefano Stabellini2e3d8862012-10-02 15:57:57 +01001834void __init xen_init_IRQ(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001835{
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001836 int i;
Mike Travisc7a35892009-01-10 21:58:11 -08001837
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001838 evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
1839 GFP_KERNEL);
Konrad Rzeszutek Wilk9d093e22011-09-29 13:31:21 -04001840 BUG_ON(!evtchn_to_irq);
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001841 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1842 evtchn_to_irq[i] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001843
1844 init_evtchn_cpu_bindings();
1845
1846 /* No event channels are 'live' right now. */
1847 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1848 mask_evtchn(i);
1849
Stefano Stabellini9846ff12012-01-30 16:21:48 +00001850 pirq_needs_eoi = pirq_needs_eoi_flag;
1851
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001852#ifdef CONFIG_X86
Sheng Yang38e20b02010-05-14 12:40:51 +01001853 if (xen_hvm_domain()) {
1854 xen_callback_vector();
1855 native_init_IRQ();
Stefano Stabellini3942b742010-06-24 17:50:18 +01001856 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1857 * __acpi_register_gsi can point at the right function */
1858 pci_xen_hvm_init();
Sheng Yang38e20b02010-05-14 12:40:51 +01001859 } else {
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001860 int rc;
Stefano Stabellini9846ff12012-01-30 16:21:48 +00001861 struct physdev_pirq_eoi_gmfn eoi_gmfn;
1862
Sheng Yang38e20b02010-05-14 12:40:51 +01001863 irq_ctx_init(smp_processor_id());
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +01001864 if (xen_initial_domain())
Konrad Rzeszutek Wilka0ee0562011-06-09 09:49:13 -04001865 pci_xen_initial_domain();
Stefano Stabellini9846ff12012-01-30 16:21:48 +00001866
1867 pirq_eoi_map = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
1868 eoi_gmfn.gmfn = virt_to_mfn(pirq_eoi_map);
1869 rc = HYPERVISOR_physdev_op(PHYSDEVOP_pirq_eoi_gmfn_v2, &eoi_gmfn);
1870 if (rc != 0) {
1871 free_page((unsigned long) pirq_eoi_map);
1872 pirq_eoi_map = NULL;
1873 } else
1874 pirq_needs_eoi = pirq_check_eoi_map;
Sheng Yang38e20b02010-05-14 12:40:51 +01001875 }
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001876#endif
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001877}