blob: 22f77c5f60120ea5d2737294cf08be25e2ce8bf9 [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:
Stefano Stabellini42a1de52010-06-24 16:42:04 +010088 * PIRQ - vector, with MSB being "needs EIO", or physical IRQ of the HVM
89 * guest, or GSI (real passthrough IRQ) of the device.
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080090 * VIRQ - virq number
91 * IPI - IPI vector
92 * EVTCHN -
93 */
Ruslan Pisarev088c05a2011-07-26 14:16:13 +030094struct irq_info {
Ian Campbell6cb65372011-03-10 16:08:11 +000095 struct list_head list;
Daniel De Graaf420eb552011-10-27 17:58:47 -040096 int refcnt;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080097 enum xen_irq_type type; /* type */
Ian Campbell6cb65372011-03-10 16:08:11 +000098 unsigned irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080099 unsigned short evtchn; /* event channel */
100 unsigned short cpu; /* cpu bound */
101
102 union {
103 unsigned short virq;
104 enum ipi_vector ipi;
105 struct {
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100106 unsigned short pirq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800107 unsigned short gsi;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400108 unsigned char vector;
109 unsigned char flags;
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400110 uint16_t domid;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800111 } pirq;
112 } u;
113};
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400114#define PIRQ_NEEDS_EOI (1 << 0)
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400115#define PIRQ_SHAREABLE (1 << 1)
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800116
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -0400117static int *evtchn_to_irq;
Ian Campbellbf86ad82012-10-17 09:39:12 +0100118#ifdef CONFIG_X86
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000119static unsigned long *pirq_eoi_map;
Ian Campbellbf86ad82012-10-17 09:39:12 +0100120#endif
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000121static bool (*pirq_needs_eoi)(unsigned irq);
Jeremy Fitzhardinge3b32f572009-08-13 12:50:37 -0700122
Ian Campbellcb60d112011-03-10 16:08:08 +0000123static DEFINE_PER_CPU(unsigned long [NR_EVENT_CHANNELS/BITS_PER_LONG],
124 cpu_evtchn_mask);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700125
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700126/* Xen will never allocate port zero for any purpose. */
127#define VALID_EVTCHN(chn) ((chn) != 0)
128
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700129static struct irq_chip xen_dynamic_chip;
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700130static struct irq_chip xen_percpu_chip;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400131static struct irq_chip xen_pirq_chip;
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100132static void enable_dynirq(struct irq_data *data);
133static void disable_dynirq(struct irq_data *data);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700134
Ian Campbell9158c352011-03-10 16:08:09 +0000135/* Get info for IRQ */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800136static struct irq_info *info_for_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700137{
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100138 return irq_get_handler_data(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700139}
140
Ian Campbell9158c352011-03-10 16:08:09 +0000141/* Constructors for packed IRQ information. */
142static void xen_irq_info_common_init(struct irq_info *info,
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000143 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000144 enum xen_irq_type type,
145 unsigned short evtchn,
146 unsigned short cpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700147{
Ian Campbell9158c352011-03-10 16:08:09 +0000148
149 BUG_ON(info->type != IRQT_UNBOUND && info->type != type);
150
151 info->type = type;
Ian Campbell6cb65372011-03-10 16:08:11 +0000152 info->irq = irq;
Ian Campbell9158c352011-03-10 16:08:09 +0000153 info->evtchn = evtchn;
154 info->cpu = cpu;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000155
156 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700157}
158
Ian Campbell9158c352011-03-10 16:08:09 +0000159static void xen_irq_info_evtchn_init(unsigned irq,
160 unsigned short evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700161{
Ian Campbell9158c352011-03-10 16:08:09 +0000162 struct irq_info *info = info_for_irq(irq);
163
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000164 xen_irq_info_common_init(info, irq, IRQT_EVTCHN, evtchn, 0);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700165}
166
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000167static void xen_irq_info_ipi_init(unsigned cpu,
168 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000169 unsigned short evtchn,
170 enum ipi_vector ipi)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700171{
Ian Campbell9158c352011-03-10 16:08:09 +0000172 struct irq_info *info = info_for_irq(irq);
173
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000174 xen_irq_info_common_init(info, irq, IRQT_IPI, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000175
176 info->u.ipi = ipi;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000177
178 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700179}
180
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000181static void xen_irq_info_virq_init(unsigned cpu,
182 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000183 unsigned short evtchn,
184 unsigned short virq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700185{
Ian Campbell9158c352011-03-10 16:08:09 +0000186 struct irq_info *info = info_for_irq(irq);
187
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000188 xen_irq_info_common_init(info, irq, IRQT_VIRQ, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000189
190 info->u.virq = virq;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000191
192 per_cpu(virq_to_irq, cpu)[virq] = irq;
Ian Campbell9158c352011-03-10 16:08:09 +0000193}
194
195static void xen_irq_info_pirq_init(unsigned irq,
196 unsigned short evtchn,
197 unsigned short pirq,
198 unsigned short gsi,
199 unsigned short vector,
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400200 uint16_t domid,
Ian Campbell9158c352011-03-10 16:08:09 +0000201 unsigned char flags)
202{
203 struct irq_info *info = info_for_irq(irq);
204
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000205 xen_irq_info_common_init(info, irq, IRQT_PIRQ, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000206
207 info->u.pirq.pirq = pirq;
208 info->u.pirq.gsi = gsi;
209 info->u.pirq.vector = vector;
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400210 info->u.pirq.domid = domid;
Ian Campbell9158c352011-03-10 16:08:09 +0000211 info->u.pirq.flags = flags;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700212}
213
214/*
215 * Accessors for packed IRQ information.
216 */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800217static unsigned int evtchn_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700218{
Joe Jin110e7c72011-01-07 14:50:12 +0800219 if (unlikely(WARN(irq < 0 || irq >= nr_irqs, "Invalid irq %d!\n", irq)))
220 return 0;
221
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800222 return info_for_irq(irq)->evtchn;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700223}
224
Ian Campbelld4c04532009-02-06 19:20:31 -0800225unsigned irq_from_evtchn(unsigned int evtchn)
226{
227 return evtchn_to_irq[evtchn];
228}
229EXPORT_SYMBOL_GPL(irq_from_evtchn);
230
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800231static enum ipi_vector ipi_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700232{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800233 struct irq_info *info = info_for_irq(irq);
234
235 BUG_ON(info == NULL);
236 BUG_ON(info->type != IRQT_IPI);
237
238 return info->u.ipi;
239}
240
241static unsigned virq_from_irq(unsigned irq)
242{
243 struct irq_info *info = info_for_irq(irq);
244
245 BUG_ON(info == NULL);
246 BUG_ON(info->type != IRQT_VIRQ);
247
248 return info->u.virq;
249}
250
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100251static unsigned pirq_from_irq(unsigned irq)
252{
253 struct irq_info *info = info_for_irq(irq);
254
255 BUG_ON(info == NULL);
256 BUG_ON(info->type != IRQT_PIRQ);
257
258 return info->u.pirq.pirq;
259}
260
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800261static enum xen_irq_type type_from_irq(unsigned irq)
262{
263 return info_for_irq(irq)->type;
264}
265
266static unsigned cpu_from_irq(unsigned irq)
267{
268 return info_for_irq(irq)->cpu;
269}
270
271static unsigned int cpu_from_evtchn(unsigned int evtchn)
272{
273 int irq = evtchn_to_irq[evtchn];
274 unsigned ret = 0;
275
276 if (irq != -1)
277 ret = cpu_from_irq(irq);
278
279 return ret;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700280}
281
Ian Campbellbf86ad82012-10-17 09:39:12 +0100282#ifdef CONFIG_X86
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000283static bool pirq_check_eoi_map(unsigned irq)
284{
Stefano Stabellini521394e2012-04-25 16:11:38 +0100285 return test_bit(pirq_from_irq(irq), pirq_eoi_map);
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000286}
Ian Campbellbf86ad82012-10-17 09:39:12 +0100287#endif
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000288
289static bool pirq_needs_eoi_flag(unsigned irq)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400290{
291 struct irq_info *info = info_for_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400292 BUG_ON(info->type != IRQT_PIRQ);
293
294 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
295}
296
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700297static inline unsigned long active_evtchns(unsigned int cpu,
298 struct shared_info *sh,
299 unsigned int idx)
300{
Ruslan Pisarev088c05a2011-07-26 14:16:13 +0300301 return sh->evtchn_pending[idx] &
Ian Campbellcb60d112011-03-10 16:08:08 +0000302 per_cpu(cpu_evtchn_mask, cpu)[idx] &
Ruslan Pisarev088c05a2011-07-26 14:16:13 +0300303 ~sh->evtchn_mask[idx];
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700304}
305
306static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
307{
308 int irq = evtchn_to_irq[chn];
309
310 BUG_ON(irq == -1);
311#ifdef CONFIG_SMP
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000312 cpumask_copy(irq_to_desc(irq)->irq_data.affinity, cpumask_of(cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700313#endif
314
Ian Campbellcb60d112011-03-10 16:08:08 +0000315 clear_bit(chn, per_cpu(cpu_evtchn_mask, cpu_from_irq(irq)));
316 set_bit(chn, per_cpu(cpu_evtchn_mask, cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700317
Ian Campbellca62ce82011-03-10 16:08:12 +0000318 info_for_irq(irq)->cpu = cpu;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700319}
320
321static void init_evtchn_cpu_bindings(void)
322{
Jan Beulich1c6969e2010-11-16 14:55:33 -0800323 int i;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700324#ifdef CONFIG_SMP
Ian Campbell6cb65372011-03-10 16:08:11 +0000325 struct irq_info *info;
Thomas Gleixner10e58082008-10-16 14:19:04 +0200326
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700327 /* By default all event channels notify CPU#0. */
Ian Campbell6cb65372011-03-10 16:08:11 +0000328 list_for_each_entry(info, &xen_irq_list_head, list) {
329 struct irq_desc *desc = irq_to_desc(info->irq);
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000330 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800331 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700332#endif
333
Jan Beulich1c6969e2010-11-16 14:55:33 -0800334 for_each_possible_cpu(i)
Ian Campbellcb60d112011-03-10 16:08:08 +0000335 memset(per_cpu(cpu_evtchn_mask, i),
336 (i == 0) ? ~0 : 0, sizeof(*per_cpu(cpu_evtchn_mask, i)));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700337}
338
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700339static inline void clear_evtchn(int port)
340{
341 struct shared_info *s = HYPERVISOR_shared_info;
342 sync_clear_bit(port, &s->evtchn_pending[0]);
343}
344
345static inline void set_evtchn(int port)
346{
347 struct shared_info *s = HYPERVISOR_shared_info;
348 sync_set_bit(port, &s->evtchn_pending[0]);
349}
350
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700351static inline int test_evtchn(int port)
352{
353 struct shared_info *s = HYPERVISOR_shared_info;
354 return sync_test_bit(port, &s->evtchn_pending[0]);
355}
356
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700357
358/**
359 * notify_remote_via_irq - send event to remote end of event channel via irq
360 * @irq: irq of event channel to send event to
361 *
362 * Unlike notify_remote_via_evtchn(), this is safe to use across
363 * save/restore. Notifications on a broken connection are silently
364 * dropped.
365 */
366void notify_remote_via_irq(int irq)
367{
368 int evtchn = evtchn_from_irq(irq);
369
370 if (VALID_EVTCHN(evtchn))
371 notify_remote_via_evtchn(evtchn);
372}
373EXPORT_SYMBOL_GPL(notify_remote_via_irq);
374
375static void mask_evtchn(int port)
376{
377 struct shared_info *s = HYPERVISOR_shared_info;
378 sync_set_bit(port, &s->evtchn_mask[0]);
379}
380
381static void unmask_evtchn(int port)
382{
383 struct shared_info *s = HYPERVISOR_shared_info;
384 unsigned int cpu = get_cpu();
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100385 int do_hypercall = 0, evtchn_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700386
387 BUG_ON(!irqs_disabled());
388
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100389 if (unlikely((cpu != cpu_from_evtchn(port))))
390 do_hypercall = 1;
391 else
392 evtchn_pending = sync_test_bit(port, &s->evtchn_pending[0]);
393
394 if (unlikely(evtchn_pending && xen_hvm_domain()))
395 do_hypercall = 1;
396
397 /* Slow path (hypercall) if this is a non-local port or if this is
398 * an hvm domain and an event is pending (hvm domains don't have
399 * their own implementation of irq_enable). */
400 if (do_hypercall) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700401 struct evtchn_unmask unmask = { .port = port };
402 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
403 } else {
Christoph Lameter780f36d2010-12-06 11:16:29 -0600404 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700405
406 sync_clear_bit(port, &s->evtchn_mask[0]);
407
408 /*
409 * The following is basically the equivalent of
410 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
411 * the interrupt edge' if the channel is masked.
412 */
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100413 if (evtchn_pending &&
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700414 !sync_test_and_set_bit(port / BITS_PER_LONG,
415 &vcpu_info->evtchn_pending_sel))
416 vcpu_info->evtchn_upcall_pending = 1;
417 }
418
419 put_cpu();
420}
421
Ian Campbell6cb65372011-03-10 16:08:11 +0000422static void xen_irq_init(unsigned irq)
423{
424 struct irq_info *info;
Konrad Rzeszutek Wilkb5328cd2011-06-15 14:24:29 -0400425#ifdef CONFIG_SMP
Ian Campbell6cb65372011-03-10 16:08:11 +0000426 struct irq_desc *desc = irq_to_desc(irq);
427
428 /* By default all event channels notify CPU#0. */
429 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
Konrad Rzeszutek Wilk44626e42011-03-15 16:40:33 -0400430#endif
Ian Campbell6cb65372011-03-10 16:08:11 +0000431
Ian Campbellca62ce82011-03-10 16:08:12 +0000432 info = kzalloc(sizeof(*info), GFP_KERNEL);
433 if (info == NULL)
434 panic("Unable to allocate metadata for IRQ%d\n", irq);
Ian Campbell6cb65372011-03-10 16:08:11 +0000435
436 info->type = IRQT_UNBOUND;
Daniel De Graaf420eb552011-10-27 17:58:47 -0400437 info->refcnt = -1;
Ian Campbell6cb65372011-03-10 16:08:11 +0000438
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100439 irq_set_handler_data(irq, info);
Ian Campbellca62ce82011-03-10 16:08:12 +0000440
Ian Campbell6cb65372011-03-10 16:08:11 +0000441 list_add_tail(&info->list, &xen_irq_list_head);
442}
443
Ian Campbell7bee9762011-03-10 16:08:15 +0000444static int __must_check xen_allocate_irq_dynamic(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700445{
Ian Campbell89911502011-03-03 11:57:44 -0500446 int first = 0;
447 int irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700448
Ian Campbell89911502011-03-03 11:57:44 -0500449#ifdef CONFIG_X86_IO_APIC
450 /*
451 * For an HVM guest or domain 0 which see "real" (emulated or
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300452 * actual respectively) GSIs we allocate dynamic IRQs
Ian Campbell89911502011-03-03 11:57:44 -0500453 * e.g. those corresponding to event channels or MSIs
454 * etc. from the range above those "real" GSIs to avoid
455 * collisions.
Konrad Rzeszutek Wilkd1b758e2010-12-09 14:53:29 -0500456 */
Ian Campbell89911502011-03-03 11:57:44 -0500457 if (xen_initial_domain() || xen_hvm_domain())
458 first = get_nr_irqs_gsi();
459#endif
460
Ian Campbell89911502011-03-03 11:57:44 -0500461 irq = irq_alloc_desc_from(first, -1);
462
Konrad Rzeszutek Wilke6599222011-09-29 13:26:45 -0400463 if (irq >= 0)
464 xen_irq_init(irq);
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800465
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700466 return irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400467}
468
Ian Campbell7bee9762011-03-10 16:08:15 +0000469static int __must_check xen_allocate_irq_gsi(unsigned gsi)
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000470{
471 int irq;
472
Ian Campbell89911502011-03-03 11:57:44 -0500473 /*
474 * A PV guest has no concept of a GSI (since it has no ACPI
475 * nor access to/knowledge of the physical APICs). Therefore
476 * all IRQs are dynamically allocated from the entire IRQ
477 * space.
478 */
479 if (xen_pv_domain() && !xen_initial_domain())
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000480 return xen_allocate_irq_dynamic();
481
482 /* Legacy IRQ descriptors are already allocated by the arch. */
483 if (gsi < NR_IRQS_LEGACY)
Ian Campbell6cb65372011-03-10 16:08:11 +0000484 irq = gsi;
485 else
486 irq = irq_alloc_desc_at(gsi, -1);
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000487
Ian Campbell6cb65372011-03-10 16:08:11 +0000488 xen_irq_init(irq);
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000489
490 return irq;
491}
492
493static void xen_free_irq(unsigned irq)
494{
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100495 struct irq_info *info = irq_get_handler_data(irq);
Ian Campbell6cb65372011-03-10 16:08:11 +0000496
497 list_del(&info->list);
Ian Campbell9158c352011-03-10 16:08:09 +0000498
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100499 irq_set_handler_data(irq, NULL);
Ian Campbellca62ce82011-03-10 16:08:12 +0000500
Daniel De Graaf420eb552011-10-27 17:58:47 -0400501 WARN_ON(info->refcnt > 0);
502
Ian Campbellca62ce82011-03-10 16:08:12 +0000503 kfree(info);
504
Ian Campbell72146102011-02-03 09:49:35 +0000505 /* Legacy IRQ descriptors are managed by the arch. */
506 if (irq < NR_IRQS_LEGACY)
507 return;
508
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000509 irq_free_desc(irq);
510}
511
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400512static void pirq_query_unmask(int irq)
513{
514 struct physdev_irq_status_query irq_status;
515 struct irq_info *info = info_for_irq(irq);
516
517 BUG_ON(info->type != IRQT_PIRQ);
518
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100519 irq_status.irq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400520 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
521 irq_status.flags = 0;
522
523 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
524 if (irq_status.flags & XENIRQSTAT_needs_eoi)
525 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
526}
527
528static bool probing_irq(int irq)
529{
530 struct irq_desc *desc = irq_to_desc(irq);
531
532 return desc && desc->action == NULL;
533}
534
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100535static void eoi_pirq(struct irq_data *data)
536{
537 int evtchn = evtchn_from_irq(data->irq);
538 struct physdev_eoi eoi = { .irq = pirq_from_irq(data->irq) };
539 int rc = 0;
540
541 irq_move_irq(data);
542
543 if (VALID_EVTCHN(evtchn))
544 clear_evtchn(evtchn);
545
546 if (pirq_needs_eoi(data->irq)) {
547 rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
548 WARN_ON(rc);
549 }
550}
551
552static void mask_ack_pirq(struct irq_data *data)
553{
554 disable_dynirq(data);
555 eoi_pirq(data);
556}
557
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000558static unsigned int __startup_pirq(unsigned int irq)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400559{
560 struct evtchn_bind_pirq bind_pirq;
561 struct irq_info *info = info_for_irq(irq);
562 int evtchn = evtchn_from_irq(irq);
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400563 int rc;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400564
565 BUG_ON(info->type != IRQT_PIRQ);
566
567 if (VALID_EVTCHN(evtchn))
568 goto out;
569
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100570 bind_pirq.pirq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400571 /* NB. We are happy to share unless we are probing. */
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400572 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
573 BIND_PIRQ__WILL_SHARE : 0;
574 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
575 if (rc != 0) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400576 if (!probing_irq(irq))
577 printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
578 irq);
579 return 0;
580 }
581 evtchn = bind_pirq.port;
582
583 pirq_query_unmask(irq);
584
585 evtchn_to_irq[evtchn] = irq;
586 bind_evtchn_to_cpu(evtchn, 0);
587 info->evtchn = evtchn;
588
589out:
590 unmask_evtchn(evtchn);
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100591 eoi_pirq(irq_get_irq_data(irq));
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400592
593 return 0;
594}
595
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000596static unsigned int startup_pirq(struct irq_data *data)
597{
598 return __startup_pirq(data->irq);
599}
600
601static void shutdown_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400602{
603 struct evtchn_close close;
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000604 unsigned int irq = data->irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400605 struct irq_info *info = info_for_irq(irq);
606 int evtchn = evtchn_from_irq(irq);
607
608 BUG_ON(info->type != IRQT_PIRQ);
609
610 if (!VALID_EVTCHN(evtchn))
611 return;
612
613 mask_evtchn(evtchn);
614
615 close.port = evtchn;
616 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
617 BUG();
618
619 bind_evtchn_to_cpu(evtchn, 0);
620 evtchn_to_irq[evtchn] = -1;
621 info->evtchn = 0;
622}
623
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000624static void enable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400625{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000626 startup_pirq(data);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400627}
628
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000629static void disable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400630{
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100631 disable_dynirq(data);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400632}
633
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100634int xen_irq_from_gsi(unsigned gsi)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400635{
Ian Campbell6cb65372011-03-10 16:08:11 +0000636 struct irq_info *info;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400637
Ian Campbell6cb65372011-03-10 16:08:11 +0000638 list_for_each_entry(info, &xen_irq_list_head, list) {
639 if (info->type != IRQT_PIRQ)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400640 continue;
641
Ian Campbell6cb65372011-03-10 16:08:11 +0000642 if (info->u.pirq.gsi == gsi)
643 return info->irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400644 }
645
646 return -1;
647}
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100648EXPORT_SYMBOL_GPL(xen_irq_from_gsi);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400649
Ian Campbell653378a2011-03-10 16:08:04 +0000650/*
651 * Do not make any assumptions regarding the relationship between the
652 * IRQ number returned here and the Xen pirq argument.
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100653 *
654 * Note: We don't assign an event channel until the irq actually started
655 * up. Return an existing irq if we've already got one for the gsi.
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100656 *
657 * Shareable implies level triggered, not shareable implies edge
658 * triggered here.
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400659 */
Ian Campbellf4d06352011-03-10 16:08:07 +0000660int xen_bind_pirq_gsi_to_irq(unsigned gsi,
661 unsigned pirq, int shareable, char *name)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400662{
Ian Campbella0e18112011-03-10 16:08:03 +0000663 int irq = -1;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400664 struct physdev_irq irq_op;
665
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400666 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400667
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100668 irq = xen_irq_from_gsi(gsi);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400669 if (irq != -1) {
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100670 printk(KERN_INFO "xen_map_pirq_gsi: returning irq %d for gsi %u\n",
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400671 irq, gsi);
Daniel De Graaf420eb552011-10-27 17:58:47 -0400672 goto out;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400673 }
674
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000675 irq = xen_allocate_irq_gsi(gsi);
Ian Campbell7bee9762011-03-10 16:08:15 +0000676 if (irq < 0)
677 goto out;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400678
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400679 irq_op.irq = irq;
Alex Nixonb5401a92010-03-18 16:31:34 -0400680 irq_op.vector = 0;
681
682 /* Only the privileged domain can do this. For non-priv, the pcifront
683 * driver provides a PCI bus that does the call to do exactly
684 * this in the priv domain. */
685 if (xen_initial_domain() &&
686 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000687 xen_free_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400688 irq = -ENOSPC;
689 goto out;
690 }
691
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400692 xen_irq_info_pirq_init(irq, 0, pirq, gsi, irq_op.vector, DOMID_SELF,
Ian Campbell9158c352011-03-10 16:08:09 +0000693 shareable ? PIRQ_SHAREABLE : 0);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400694
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100695 pirq_query_unmask(irq);
696 /* We try to use the handler with the appropriate semantic for the
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100697 * type of interrupt: if the interrupt is an edge triggered
698 * interrupt we use handle_edge_irq.
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100699 *
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100700 * On the other hand if the interrupt is level triggered we use
701 * handle_fasteoi_irq like the native code does for this kind of
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100702 * interrupts.
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100703 *
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100704 * Depending on the Xen version, pirq_needs_eoi might return true
705 * not only for level triggered interrupts but for edge triggered
706 * interrupts too. In any case Xen always honors the eoi mechanism,
707 * not injecting any more pirqs of the same kind if the first one
708 * hasn't received an eoi yet. Therefore using the fasteoi handler
709 * is the right choice either way.
710 */
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100711 if (shareable)
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100712 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
713 handle_fasteoi_irq, name);
714 else
715 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
716 handle_edge_irq, name);
717
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400718out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400719 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400720
721 return irq;
722}
723
Qing Hef731e3ef2010-10-11 15:30:09 +0100724#ifdef CONFIG_PCI_MSI
Ian Campbellbf480d92011-02-18 16:43:32 +0000725int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000726{
Ian Campbell5cad61a2011-02-18 16:43:31 +0000727 int rc;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000728 struct physdev_get_free_pirq op_get_free_pirq;
Ian Campbell5cad61a2011-02-18 16:43:31 +0000729
Ian Campbellbf480d92011-02-18 16:43:32 +0000730 op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000731 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000732
Ian Campbell5cad61a2011-02-18 16:43:31 +0000733 WARN_ONCE(rc == -ENOSYS,
734 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
735
736 return rc ? -1 : op_get_free_pirq.pirq;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000737}
738
Ian Campbellbf480d92011-02-18 16:43:32 +0000739int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400740 int pirq, int vector, const char *name,
741 domid_t domid)
Stefano Stabellini809f9262010-07-01 17:10:39 +0100742{
Ian Campbellbf480d92011-02-18 16:43:32 +0000743 int irq, ret;
Ian Campbell4b41df72011-02-18 16:43:29 +0000744
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400745 mutex_lock(&irq_mapping_update_lock);
Stefano Stabellini809f9262010-07-01 17:10:39 +0100746
Ian Campbell4b41df72011-02-18 16:43:29 +0000747 irq = xen_allocate_irq_dynamic();
Konrad Rzeszutek Wilke6599222011-09-29 13:26:45 -0400748 if (irq < 0)
Ian Campbellbb5d0792011-02-18 16:43:28 +0000749 goto out;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100750
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100751 irq_set_chip_and_handler_name(irq, &xen_pirq_chip, handle_edge_irq,
752 name);
Stefano Stabellini809f9262010-07-01 17:10:39 +0100753
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400754 xen_irq_info_pirq_init(irq, 0, pirq, 0, vector, domid, 0);
Linus Torvalds5f6fb452011-03-15 19:23:40 -0700755 ret = irq_set_msi_desc(irq, msidesc);
Ian Campbellbf480d92011-02-18 16:43:32 +0000756 if (ret < 0)
757 goto error_irq;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100758out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400759 mutex_unlock(&irq_mapping_update_lock);
Ian Campbell4b41df72011-02-18 16:43:29 +0000760 return irq;
Ian Campbellbf480d92011-02-18 16:43:32 +0000761error_irq:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400762 mutex_unlock(&irq_mapping_update_lock);
Ian Campbellbf480d92011-02-18 16:43:32 +0000763 xen_free_irq(irq);
Konrad Rzeszutek Wilke6599222011-09-29 13:26:45 -0400764 return ret;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100765}
Qing Hef731e3ef2010-10-11 15:30:09 +0100766#endif
767
Alex Nixonb5401a92010-03-18 16:31:34 -0400768int xen_destroy_irq(int irq)
769{
770 struct irq_desc *desc;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100771 struct physdev_unmap_pirq unmap_irq;
772 struct irq_info *info = info_for_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400773 int rc = -ENOENT;
774
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400775 mutex_lock(&irq_mapping_update_lock);
Alex Nixonb5401a92010-03-18 16:31:34 -0400776
777 desc = irq_to_desc(irq);
778 if (!desc)
779 goto out;
780
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100781 if (xen_initial_domain()) {
Konrad Rzeszutek Wilk12334712010-11-19 11:27:09 -0500782 unmap_irq.pirq = info->u.pirq.pirq;
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400783 unmap_irq.domid = info->u.pirq.domid;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100784 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
Konrad Rzeszutek Wilk1eff1ad2011-02-16 16:26:44 -0500785 /* If another domain quits without making the pci_disable_msix
786 * call, the Xen hypervisor takes care of freeing the PIRQs
787 * (free_domain_pirqs).
788 */
789 if ((rc == -ESRCH && info->u.pirq.domid != DOMID_SELF))
790 printk(KERN_INFO "domain %d does not have %d anymore\n",
791 info->u.pirq.domid, info->u.pirq.pirq);
792 else if (rc) {
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100793 printk(KERN_WARNING "unmap irq failed %d\n", rc);
794 goto out;
795 }
796 }
Alex Nixonb5401a92010-03-18 16:31:34 -0400797
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000798 xen_free_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400799
800out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400801 mutex_unlock(&irq_mapping_update_lock);
Alex Nixonb5401a92010-03-18 16:31:34 -0400802 return rc;
803}
804
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000805int xen_irq_from_pirq(unsigned pirq)
806{
Ian Campbell69c358c2011-03-10 16:08:13 +0000807 int irq;
808
809 struct irq_info *info;
810
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400811 mutex_lock(&irq_mapping_update_lock);
Ian Campbell69c358c2011-03-10 16:08:13 +0000812
813 list_for_each_entry(info, &xen_irq_list_head, list) {
Konrad Rzeszutek Wilk9bb9efe2011-09-29 13:13:30 -0400814 if (info->type != IRQT_PIRQ)
Ian Campbell69c358c2011-03-10 16:08:13 +0000815 continue;
816 irq = info->irq;
817 if (info->u.pirq.pirq == pirq)
818 goto out;
819 }
820 irq = -1;
821out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400822 mutex_unlock(&irq_mapping_update_lock);
Ian Campbell69c358c2011-03-10 16:08:13 +0000823
824 return irq;
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000825}
826
Konrad Rzeszutek Wilke6197ac2011-02-24 14:20:12 -0500827
828int xen_pirq_from_irq(unsigned irq)
829{
830 return pirq_from_irq(irq);
831}
832EXPORT_SYMBOL_GPL(xen_pirq_from_irq);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700833int bind_evtchn_to_irq(unsigned int evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700834{
835 int irq;
836
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400837 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700838
839 irq = evtchn_to_irq[evtchn];
840
841 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000842 irq = xen_allocate_irq_dynamic();
Wei Liu68ba45f2013-01-31 14:46:56 +0000843 if (irq < 0)
Ian Campbell7bee9762011-03-10 16:08:15 +0000844 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700845
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100846 irq_set_chip_and_handler_name(irq, &xen_dynamic_chip,
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100847 handle_edge_irq, "event");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700848
Ian Campbell9158c352011-03-10 16:08:09 +0000849 xen_irq_info_evtchn_init(irq, evtchn);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400850 } else {
851 struct irq_info *info = info_for_irq(irq);
852 WARN_ON(info == NULL || info->type != IRQT_EVTCHN);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700853 }
Stefano Stabellinia8636c02012-08-22 17:20:15 +0100854 irq_clear_status_flags(irq, IRQ_NOREQUEST|IRQ_NOAUTOEN);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700855
Ian Campbell7bee9762011-03-10 16:08:15 +0000856out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400857 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700858
859 return irq;
860}
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700861EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700862
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700863static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
864{
865 struct evtchn_bind_ipi bind_ipi;
866 int evtchn, irq;
867
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400868 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700869
870 irq = per_cpu(ipi_to_irq, cpu)[ipi];
Ian Campbell90af9512009-02-06 16:55:58 -0800871
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700872 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000873 irq = xen_allocate_irq_dynamic();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700874 if (irq < 0)
875 goto out;
876
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100877 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700878 handle_percpu_irq, "ipi");
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700879
880 bind_ipi.vcpu = cpu;
881 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
882 &bind_ipi) != 0)
883 BUG();
884 evtchn = bind_ipi.port;
885
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000886 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700887
888 bind_evtchn_to_cpu(evtchn, cpu);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400889 } else {
890 struct irq_info *info = info_for_irq(irq);
891 WARN_ON(info == NULL || info->type != IRQT_IPI);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700892 }
893
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700894 out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400895 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700896 return irq;
897}
898
Ian Campbell2e820f52009-02-09 12:05:50 -0800899static int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
900 unsigned int remote_port)
901{
902 struct evtchn_bind_interdomain bind_interdomain;
903 int err;
904
905 bind_interdomain.remote_dom = remote_domain;
906 bind_interdomain.remote_port = remote_port;
907
908 err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
909 &bind_interdomain);
910
911 return err ? : bind_evtchn_to_irq(bind_interdomain.local_port);
912}
913
Olaf Hering62cc5fc2011-08-25 18:30:48 +0200914static int find_virq(unsigned int virq, unsigned int cpu)
915{
916 struct evtchn_status status;
917 int port, rc = -ENOENT;
918
919 memset(&status, 0, sizeof(status));
920 for (port = 0; port <= NR_EVENT_CHANNELS; port++) {
921 status.dom = DOMID_SELF;
922 status.port = port;
923 rc = HYPERVISOR_event_channel_op(EVTCHNOP_status, &status);
924 if (rc < 0)
925 continue;
926 if (status.status != EVTCHNSTAT_virq)
927 continue;
928 if (status.u.virq == virq && status.vcpu == cpu) {
929 rc = port;
930 break;
931 }
932 }
933 return rc;
934}
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700935
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100936int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700937{
938 struct evtchn_bind_virq bind_virq;
Olaf Hering62cc5fc2011-08-25 18:30:48 +0200939 int evtchn, irq, ret;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700940
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400941 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700942
943 irq = per_cpu(virq_to_irq, cpu)[virq];
944
945 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000946 irq = xen_allocate_irq_dynamic();
Wei Liu68ba45f2013-01-31 14:46:56 +0000947 if (irq < 0)
Ian Campbell7bee9762011-03-10 16:08:15 +0000948 goto out;
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700949
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100950 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700951 handle_percpu_irq, "virq");
952
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700953 bind_virq.virq = virq;
954 bind_virq.vcpu = cpu;
Olaf Hering62cc5fc2011-08-25 18:30:48 +0200955 ret = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
956 &bind_virq);
957 if (ret == 0)
958 evtchn = bind_virq.port;
959 else {
960 if (ret == -EEXIST)
961 ret = find_virq(virq, cpu);
962 BUG_ON(ret < 0);
963 evtchn = ret;
964 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700965
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000966 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700967
968 bind_evtchn_to_cpu(evtchn, cpu);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400969 } else {
970 struct irq_info *info = info_for_irq(irq);
971 WARN_ON(info == NULL || info->type != IRQT_VIRQ);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700972 }
973
Ian Campbell7bee9762011-03-10 16:08:15 +0000974out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400975 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700976
977 return irq;
978}
979
980static void unbind_from_irq(unsigned int irq)
981{
982 struct evtchn_close close;
983 int evtchn = evtchn_from_irq(irq);
Daniel De Graaf420eb552011-10-27 17:58:47 -0400984 struct irq_info *info = irq_get_handler_data(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700985
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400986 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700987
Daniel De Graaf420eb552011-10-27 17:58:47 -0400988 if (info->refcnt > 0) {
989 info->refcnt--;
990 if (info->refcnt != 0)
991 goto done;
992 }
993
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800994 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700995 close.port = evtchn;
996 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
997 BUG();
998
999 switch (type_from_irq(irq)) {
1000 case IRQT_VIRQ:
1001 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001002 [virq_from_irq(irq)] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001003 break;
Alex Nixond68d82a2008-08-22 11:52:15 +01001004 case IRQT_IPI:
1005 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001006 [ipi_from_irq(irq)] = -1;
Alex Nixond68d82a2008-08-22 11:52:15 +01001007 break;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001008 default:
1009 break;
1010 }
1011
1012 /* Closed ports are implicitly re-bound to VCPU0. */
1013 bind_evtchn_to_cpu(evtchn, 0);
1014
1015 evtchn_to_irq[evtchn] = -1;
Ian Campbellfed5ea82009-12-01 16:15:30 +00001016 }
1017
Ian Campbellca62ce82011-03-10 16:08:12 +00001018 BUG_ON(info_for_irq(irq)->type == IRQT_UNBOUND);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001019
Ian Campbell9158c352011-03-10 16:08:09 +00001020 xen_free_irq(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001021
Daniel De Graaf420eb552011-10-27 17:58:47 -04001022 done:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001023 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001024}
1025
1026int bind_evtchn_to_irqhandler(unsigned int evtchn,
Jeff Garzik7c239972007-10-19 03:12:20 -04001027 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001028 unsigned long irqflags,
1029 const char *devname, void *dev_id)
1030{
Nicolas Kaiser361ae8c2011-03-30 21:14:26 +02001031 int irq, retval;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001032
1033 irq = bind_evtchn_to_irq(evtchn);
Ian Campbell7bee9762011-03-10 16:08:15 +00001034 if (irq < 0)
1035 return irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001036 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1037 if (retval != 0) {
1038 unbind_from_irq(irq);
1039 return retval;
1040 }
1041
1042 return irq;
1043}
1044EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
1045
Ian Campbell2e820f52009-02-09 12:05:50 -08001046int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
1047 unsigned int remote_port,
1048 irq_handler_t handler,
1049 unsigned long irqflags,
1050 const char *devname,
1051 void *dev_id)
1052{
1053 int irq, retval;
1054
1055 irq = bind_interdomain_evtchn_to_irq(remote_domain, remote_port);
1056 if (irq < 0)
1057 return irq;
1058
1059 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1060 if (retval != 0) {
1061 unbind_from_irq(irq);
1062 return retval;
1063 }
1064
1065 return irq;
1066}
1067EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler);
1068
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001069int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
Jeff Garzik7c239972007-10-19 03:12:20 -04001070 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001071 unsigned long irqflags, const char *devname, void *dev_id)
1072{
Nicolas Kaiser361ae8c2011-03-30 21:14:26 +02001073 int irq, retval;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001074
1075 irq = bind_virq_to_irq(virq, cpu);
Ian Campbell7bee9762011-03-10 16:08:15 +00001076 if (irq < 0)
1077 return irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001078 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1079 if (retval != 0) {
1080 unbind_from_irq(irq);
1081 return retval;
1082 }
1083
1084 return irq;
1085}
1086EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
1087
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001088int bind_ipi_to_irqhandler(enum ipi_vector ipi,
1089 unsigned int cpu,
1090 irq_handler_t handler,
1091 unsigned long irqflags,
1092 const char *devname,
1093 void *dev_id)
1094{
1095 int irq, retval;
1096
1097 irq = bind_ipi_to_irq(ipi, cpu);
1098 if (irq < 0)
1099 return irq;
1100
Ian Campbell9bab0b72011-10-03 15:37:00 +01001101 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME | IRQF_EARLY_RESUME;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001102 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1103 if (retval != 0) {
1104 unbind_from_irq(irq);
1105 return retval;
1106 }
1107
1108 return irq;
1109}
1110
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001111void unbind_from_irqhandler(unsigned int irq, void *dev_id)
1112{
1113 free_irq(irq, dev_id);
1114 unbind_from_irq(irq);
1115}
1116EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
1117
Daniel De Graaf420eb552011-10-27 17:58:47 -04001118int evtchn_make_refcounted(unsigned int evtchn)
1119{
1120 int irq = evtchn_to_irq[evtchn];
1121 struct irq_info *info;
1122
1123 if (irq == -1)
1124 return -ENOENT;
1125
1126 info = irq_get_handler_data(irq);
1127
1128 if (!info)
1129 return -ENOENT;
1130
1131 WARN_ON(info->refcnt != -1);
1132
1133 info->refcnt = 1;
1134
1135 return 0;
1136}
1137EXPORT_SYMBOL_GPL(evtchn_make_refcounted);
1138
1139int evtchn_get(unsigned int evtchn)
1140{
1141 int irq;
1142 struct irq_info *info;
1143 int err = -ENOENT;
1144
Daniel De Graafc3b3f162011-11-28 11:49:09 -05001145 if (evtchn >= NR_EVENT_CHANNELS)
1146 return -EINVAL;
1147
Daniel De Graaf420eb552011-10-27 17:58:47 -04001148 mutex_lock(&irq_mapping_update_lock);
1149
1150 irq = evtchn_to_irq[evtchn];
1151 if (irq == -1)
1152 goto done;
1153
1154 info = irq_get_handler_data(irq);
1155
1156 if (!info)
1157 goto done;
1158
1159 err = -EINVAL;
1160 if (info->refcnt <= 0)
1161 goto done;
1162
1163 info->refcnt++;
1164 err = 0;
1165 done:
1166 mutex_unlock(&irq_mapping_update_lock);
1167
1168 return err;
1169}
1170EXPORT_SYMBOL_GPL(evtchn_get);
1171
1172void evtchn_put(unsigned int evtchn)
1173{
1174 int irq = evtchn_to_irq[evtchn];
1175 if (WARN_ON(irq == -1))
1176 return;
1177 unbind_from_irq(irq);
1178}
1179EXPORT_SYMBOL_GPL(evtchn_put);
1180
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001181void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
1182{
1183 int irq = per_cpu(ipi_to_irq, cpu)[vector];
1184 BUG_ON(irq < 0);
1185 notify_remote_via_irq(irq);
1186}
1187
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001188irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
1189{
1190 struct shared_info *sh = HYPERVISOR_shared_info;
1191 int cpu = smp_processor_id();
Ian Campbellcb60d112011-03-10 16:08:08 +00001192 unsigned long *cpu_evtchn = per_cpu(cpu_evtchn_mask, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001193 int i;
1194 unsigned long flags;
1195 static DEFINE_SPINLOCK(debug_lock);
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001196 struct vcpu_info *v;
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001197
1198 spin_lock_irqsave(&debug_lock, flags);
1199
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001200 printk("\nvcpu %d\n ", cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001201
1202 for_each_online_cpu(i) {
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001203 int pending;
1204 v = per_cpu(xen_vcpu, i);
1205 pending = (get_irq_regs() && i == cpu)
1206 ? xen_irqs_disabled(get_irq_regs())
1207 : v->evtchn_upcall_mask;
1208 printk("%d: masked=%d pending=%d event_sel %0*lx\n ", i,
1209 pending, v->evtchn_upcall_pending,
1210 (int)(sizeof(v->evtchn_pending_sel)*2),
1211 v->evtchn_pending_sel);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001212 }
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001213 v = per_cpu(xen_vcpu, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001214
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001215 printk("\npending:\n ");
1216 for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
1217 printk("%0*lx%s", (int)sizeof(sh->evtchn_pending[0])*2,
1218 sh->evtchn_pending[i],
1219 i % 8 == 0 ? "\n " : " ");
1220 printk("\nglobal mask:\n ");
1221 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1222 printk("%0*lx%s",
1223 (int)(sizeof(sh->evtchn_mask[0])*2),
1224 sh->evtchn_mask[i],
1225 i % 8 == 0 ? "\n " : " ");
1226
1227 printk("\nglobally unmasked:\n ");
1228 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1229 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
1230 sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
1231 i % 8 == 0 ? "\n " : " ");
1232
1233 printk("\nlocal cpu%d mask:\n ", cpu);
1234 for (i = (NR_EVENT_CHANNELS/BITS_PER_LONG)-1; i >= 0; i--)
1235 printk("%0*lx%s", (int)(sizeof(cpu_evtchn[0])*2),
1236 cpu_evtchn[i],
1237 i % 8 == 0 ? "\n " : " ");
1238
1239 printk("\nlocally unmasked:\n ");
1240 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) {
1241 unsigned long pending = sh->evtchn_pending[i]
1242 & ~sh->evtchn_mask[i]
1243 & cpu_evtchn[i];
1244 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
1245 pending, i % 8 == 0 ? "\n " : " ");
1246 }
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001247
1248 printk("\npending list:\n");
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001249 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001250 if (sync_test_bit(i, sh->evtchn_pending)) {
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001251 int word_idx = i / BITS_PER_LONG;
1252 printk(" %d: event %d -> irq %d%s%s%s\n",
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001253 cpu_from_evtchn(i), i,
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001254 evtchn_to_irq[i],
1255 sync_test_bit(word_idx, &v->evtchn_pending_sel)
1256 ? "" : " l2-clear",
1257 !sync_test_bit(i, sh->evtchn_mask)
1258 ? "" : " globally-masked",
1259 sync_test_bit(i, cpu_evtchn)
1260 ? "" : " locally-masked");
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001261 }
1262 }
1263
1264 spin_unlock_irqrestore(&debug_lock, flags);
1265
1266 return IRQ_HANDLED;
1267}
1268
Tejun Heo245b2e72009-06-24 15:13:48 +09001269static DEFINE_PER_CPU(unsigned, xed_nesting_count);
Keir Fraserada68142011-03-03 10:01:11 +00001270static DEFINE_PER_CPU(unsigned int, current_word_idx);
1271static DEFINE_PER_CPU(unsigned int, current_bit_idx);
Tejun Heo245b2e72009-06-24 15:13:48 +09001272
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001273/*
Scott Rixnerab7f8632011-03-03 09:30:08 +00001274 * Mask out the i least significant bits of w
1275 */
1276#define MASK_LSBS(w, i) (w & ((~0UL) << i))
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001277
1278/*
1279 * Search the CPUs pending events bitmasks. For each one found, map
1280 * the event number to an irq, and feed it into do_IRQ() for
1281 * handling.
1282 *
1283 * Xen uses a two-level bitmap to speed searching. The first level is
1284 * a bitset of words which contain pending event bits. The second
1285 * level is a bitset of pending events themselves.
1286 */
Sheng Yang38e20b02010-05-14 12:40:51 +01001287static void __xen_evtchn_do_upcall(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001288{
Keir Fraser24b51c22011-03-03 11:06:28 +00001289 int start_word_idx, start_bit_idx;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001290 int word_idx, bit_idx;
Keir Fraser24b51c22011-03-03 11:06:28 +00001291 int i;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001292 int cpu = get_cpu();
1293 struct shared_info *s = HYPERVISOR_shared_info;
Christoph Lameter780f36d2010-12-06 11:16:29 -06001294 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
Ruslan Pisarev088c05a2011-07-26 14:16:13 +03001295 unsigned count;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001296
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001297 do {
1298 unsigned long pending_words;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001299
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001300 vcpu_info->evtchn_upcall_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001301
Christoph Lameterb2e4ae62010-12-06 11:40:07 -06001302 if (__this_cpu_inc_return(xed_nesting_count) - 1)
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001303 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001304
Isaku Yamahatae849c3e2008-04-02 10:53:56 -07001305#ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
1306 /* Clear master flag /before/ clearing selector flag. */
Isaku Yamahata6673cf62008-06-16 14:58:13 -07001307 wmb();
Isaku Yamahatae849c3e2008-04-02 10:53:56 -07001308#endif
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001309 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001310
Keir Fraser24b51c22011-03-03 11:06:28 +00001311 start_word_idx = __this_cpu_read(current_word_idx);
1312 start_bit_idx = __this_cpu_read(current_bit_idx);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001313
Keir Fraser24b51c22011-03-03 11:06:28 +00001314 word_idx = start_word_idx;
1315
1316 for (i = 0; pending_words != 0; i++) {
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001317 unsigned long pending_bits;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001318 unsigned long words;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001319
Scott Rixnerab7f8632011-03-03 09:30:08 +00001320 words = MASK_LSBS(pending_words, word_idx);
1321
1322 /*
Keir Fraserada68142011-03-03 10:01:11 +00001323 * If we masked out all events, wrap to beginning.
Scott Rixnerab7f8632011-03-03 09:30:08 +00001324 */
1325 if (words == 0) {
Keir Fraserada68142011-03-03 10:01:11 +00001326 word_idx = 0;
1327 bit_idx = 0;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001328 continue;
1329 }
1330 word_idx = __ffs(words);
1331
Keir Fraser24b51c22011-03-03 11:06:28 +00001332 pending_bits = active_evtchns(cpu, s, word_idx);
1333 bit_idx = 0; /* usually scan entire word from start */
1334 if (word_idx == start_word_idx) {
1335 /* We scan the starting word in two parts */
1336 if (i == 0)
1337 /* 1st time: start in the middle */
1338 bit_idx = start_bit_idx;
1339 else
1340 /* 2nd time: mask bits done already */
1341 bit_idx &= (1UL << start_bit_idx) - 1;
1342 }
1343
Scott Rixnerab7f8632011-03-03 09:30:08 +00001344 do {
1345 unsigned long bits;
1346 int port, irq;
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -08001347 struct irq_desc *desc;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001348
Scott Rixnerab7f8632011-03-03 09:30:08 +00001349 bits = MASK_LSBS(pending_bits, bit_idx);
1350
1351 /* If we masked out all events, move on. */
Keir Fraserada68142011-03-03 10:01:11 +00001352 if (bits == 0)
Scott Rixnerab7f8632011-03-03 09:30:08 +00001353 break;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001354
1355 bit_idx = __ffs(bits);
1356
1357 /* Process port. */
1358 port = (word_idx * BITS_PER_LONG) + bit_idx;
1359 irq = evtchn_to_irq[port];
1360
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -08001361 if (irq != -1) {
1362 desc = irq_to_desc(irq);
1363 if (desc)
1364 generic_handle_irq_desc(irq, desc);
1365 }
Scott Rixnerab7f8632011-03-03 09:30:08 +00001366
Keir Fraserada68142011-03-03 10:01:11 +00001367 bit_idx = (bit_idx + 1) % BITS_PER_LONG;
1368
1369 /* Next caller starts at last processed + 1 */
1370 __this_cpu_write(current_word_idx,
1371 bit_idx ? word_idx :
1372 (word_idx+1) % BITS_PER_LONG);
1373 __this_cpu_write(current_bit_idx, bit_idx);
1374 } while (bit_idx != 0);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001375
Keir Fraser24b51c22011-03-03 11:06:28 +00001376 /* Scan start_l1i twice; all others once. */
1377 if ((word_idx != start_word_idx) || (i != 0))
Scott Rixnerab7f8632011-03-03 09:30:08 +00001378 pending_words &= ~(1UL << word_idx);
Keir Fraserada68142011-03-03 10:01:11 +00001379
1380 word_idx = (word_idx + 1) % BITS_PER_LONG;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001381 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001382
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001383 BUG_ON(!irqs_disabled());
1384
Christoph Lameter780f36d2010-12-06 11:16:29 -06001385 count = __this_cpu_read(xed_nesting_count);
1386 __this_cpu_write(xed_nesting_count, 0);
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001387 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001388
1389out:
Jeremy Fitzhardinge3445a8f2009-02-06 14:09:46 -08001390
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001391 put_cpu();
1392}
1393
Sheng Yang38e20b02010-05-14 12:40:51 +01001394void xen_evtchn_do_upcall(struct pt_regs *regs)
1395{
1396 struct pt_regs *old_regs = set_irq_regs(regs);
1397
Mojiong Qiu772aebc2012-11-06 16:08:15 +08001398 irq_enter();
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001399#ifdef CONFIG_X86
Sheng Yang38e20b02010-05-14 12:40:51 +01001400 exit_idle();
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001401#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001402
1403 __xen_evtchn_do_upcall();
1404
1405 irq_exit();
1406 set_irq_regs(old_regs);
1407}
1408
1409void xen_hvm_evtchn_do_upcall(void)
1410{
1411 __xen_evtchn_do_upcall();
1412}
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001413EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
Sheng Yang38e20b02010-05-14 12:40:51 +01001414
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001415/* Rebind a new event channel to an existing irq. */
1416void rebind_evtchn_irq(int evtchn, int irq)
1417{
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001418 struct irq_info *info = info_for_irq(irq);
1419
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001420 /* Make sure the irq is masked, since the new event channel
1421 will also be masked. */
1422 disable_irq(irq);
1423
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001424 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001425
1426 /* After resume the irq<->evtchn mappings are all cleared out */
1427 BUG_ON(evtchn_to_irq[evtchn] != -1);
1428 /* Expect irq to have been bound before,
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001429 so there should be a proper type */
1430 BUG_ON(info->type == IRQT_UNBOUND);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001431
Ian Campbell9158c352011-03-10 16:08:09 +00001432 xen_irq_info_evtchn_init(irq, evtchn);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001433
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001434 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001435
1436 /* new event channels are always bound to cpu 0 */
Rusty Russell0de26522008-12-13 21:20:26 +10301437 irq_set_affinity(irq, cpumask_of(0));
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001438
1439 /* Unmask the event channel. */
1440 enable_irq(irq);
1441}
1442
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001443/* Rebind an evtchn so that it gets delivered to a specific cpu */
Yinghai Lud5dedd42009-04-27 17:59:21 -07001444static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001445{
1446 struct evtchn_bind_vcpu bind_vcpu;
1447 int evtchn = evtchn_from_irq(irq);
1448
Ian Campbellbe494722011-03-10 16:08:02 +00001449 if (!VALID_EVTCHN(evtchn))
1450 return -1;
1451
1452 /*
1453 * Events delivered via platform PCI interrupts are always
1454 * routed to vcpu 0 and hence cannot be rebound.
1455 */
1456 if (xen_hvm_domain() && !xen_have_vector_callback)
Yinghai Lud5dedd42009-04-27 17:59:21 -07001457 return -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001458
1459 /* Send future instances of this interrupt to other vcpu. */
1460 bind_vcpu.port = evtchn;
1461 bind_vcpu.vcpu = tcpu;
1462
1463 /*
1464 * If this fails, it usually just indicates that we're dealing with a
1465 * virq or IPI channel, which don't actually need to be rebound. Ignore
1466 * it, but don't do the xenlinux-level rebind in that case.
1467 */
1468 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1469 bind_evtchn_to_cpu(evtchn, tcpu);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001470
1471 return 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001472}
1473
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001474static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1475 bool force)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001476{
Rusty Russell0de26522008-12-13 21:20:26 +10301477 unsigned tcpu = cpumask_first(dest);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001478
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001479 return rebind_irq_to_cpu(data->irq, tcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001480}
1481
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001482int resend_irq_on_evtchn(unsigned int irq)
1483{
1484 int masked, evtchn = evtchn_from_irq(irq);
1485 struct shared_info *s = HYPERVISOR_shared_info;
1486
1487 if (!VALID_EVTCHN(evtchn))
1488 return 1;
1489
1490 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
1491 sync_set_bit(evtchn, s->evtchn_pending);
1492 if (!masked)
1493 unmask_evtchn(evtchn);
1494
1495 return 1;
1496}
1497
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001498static void enable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001499{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001500 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001501
1502 if (VALID_EVTCHN(evtchn))
1503 unmask_evtchn(evtchn);
1504}
1505
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001506static void disable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001507{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001508 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001509
1510 if (VALID_EVTCHN(evtchn))
1511 mask_evtchn(evtchn);
1512}
1513
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001514static void ack_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001515{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001516 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001517
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001518 irq_move_irq(data);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001519
1520 if (VALID_EVTCHN(evtchn))
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001521 clear_evtchn(evtchn);
1522}
1523
1524static void mask_ack_dynirq(struct irq_data *data)
1525{
1526 disable_dynirq(data);
1527 ack_dynirq(data);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001528}
1529
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001530static int retrigger_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 Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001533 struct shared_info *sh = HYPERVISOR_shared_info;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001534 int ret = 0;
1535
1536 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001537 int masked;
1538
1539 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
1540 sync_set_bit(evtchn, sh->evtchn_pending);
1541 if (!masked)
1542 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001543 ret = 1;
1544 }
1545
1546 return ret;
1547}
1548
Ian Campbell0a852262011-03-10 16:08:06 +00001549static void restore_pirqs(void)
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001550{
1551 int pirq, rc, irq, gsi;
1552 struct physdev_map_pirq map_irq;
Ian Campbell69c358c2011-03-10 16:08:13 +00001553 struct irq_info *info;
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001554
Ian Campbell69c358c2011-03-10 16:08:13 +00001555 list_for_each_entry(info, &xen_irq_list_head, list) {
1556 if (info->type != IRQT_PIRQ)
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001557 continue;
1558
Ian Campbell69c358c2011-03-10 16:08:13 +00001559 pirq = info->u.pirq.pirq;
1560 gsi = info->u.pirq.gsi;
1561 irq = info->irq;
1562
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001563 /* save/restore of PT devices doesn't work, so at this point the
1564 * only devices present are GSI based emulated devices */
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001565 if (!gsi)
1566 continue;
1567
1568 map_irq.domid = DOMID_SELF;
1569 map_irq.type = MAP_PIRQ_TYPE_GSI;
1570 map_irq.index = gsi;
1571 map_irq.pirq = pirq;
1572
1573 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1574 if (rc) {
1575 printk(KERN_WARNING "xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1576 gsi, irq, pirq, rc);
Ian Campbell9158c352011-03-10 16:08:09 +00001577 xen_free_irq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001578 continue;
1579 }
1580
1581 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1582
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001583 __startup_pirq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001584 }
1585}
1586
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001587static void restore_cpu_virqs(unsigned int cpu)
1588{
1589 struct evtchn_bind_virq bind_virq;
1590 int virq, irq, evtchn;
1591
1592 for (virq = 0; virq < NR_VIRQS; virq++) {
1593 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1594 continue;
1595
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001596 BUG_ON(virq_from_irq(irq) != virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001597
1598 /* Get a new binding from Xen. */
1599 bind_virq.virq = virq;
1600 bind_virq.vcpu = cpu;
1601 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1602 &bind_virq) != 0)
1603 BUG();
1604 evtchn = bind_virq.port;
1605
1606 /* Record the new mapping. */
Ian Campbell3d4cfa32011-03-10 16:08:10 +00001607 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001608 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001609 }
1610}
1611
1612static void restore_cpu_ipis(unsigned int cpu)
1613{
1614 struct evtchn_bind_ipi bind_ipi;
1615 int ipi, irq, evtchn;
1616
1617 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1618 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1619 continue;
1620
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001621 BUG_ON(ipi_from_irq(irq) != ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001622
1623 /* Get a new binding from Xen. */
1624 bind_ipi.vcpu = cpu;
1625 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1626 &bind_ipi) != 0)
1627 BUG();
1628 evtchn = bind_ipi.port;
1629
1630 /* Record the new mapping. */
Ian Campbell3d4cfa32011-03-10 16:08:10 +00001631 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001632 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001633 }
1634}
1635
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001636/* Clear an irq's pending state, in preparation for polling on it */
1637void xen_clear_irq_pending(int irq)
1638{
1639 int evtchn = evtchn_from_irq(irq);
1640
1641 if (VALID_EVTCHN(evtchn))
1642 clear_evtchn(evtchn);
1643}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001644EXPORT_SYMBOL(xen_clear_irq_pending);
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -07001645void xen_set_irq_pending(int irq)
1646{
1647 int evtchn = evtchn_from_irq(irq);
1648
1649 if (VALID_EVTCHN(evtchn))
1650 set_evtchn(evtchn);
1651}
1652
1653bool xen_test_irq_pending(int irq)
1654{
1655 int evtchn = evtchn_from_irq(irq);
1656 bool ret = false;
1657
1658 if (VALID_EVTCHN(evtchn))
1659 ret = test_evtchn(evtchn);
1660
1661 return ret;
1662}
1663
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001664/* Poll waiting for an irq to become pending with timeout. In the usual case,
1665 * the irq will be disabled so it won't deliver an interrupt. */
1666void xen_poll_irq_timeout(int irq, u64 timeout)
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001667{
1668 evtchn_port_t evtchn = evtchn_from_irq(irq);
1669
1670 if (VALID_EVTCHN(evtchn)) {
1671 struct sched_poll poll;
1672
1673 poll.nr_ports = 1;
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001674 poll.timeout = timeout;
Isaku Yamahataff3c5362008-10-14 17:50:44 -07001675 set_xen_guest_handle(poll.ports, &evtchn);
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001676
1677 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1678 BUG();
1679 }
1680}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001681EXPORT_SYMBOL(xen_poll_irq_timeout);
1682/* Poll waiting for an irq to become pending. In the usual case, the
1683 * irq will be disabled so it won't deliver an interrupt. */
1684void xen_poll_irq(int irq)
1685{
1686 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1687}
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001688
Konrad Rzeszutek Wilkc7c2c3a2010-11-08 14:26:36 -05001689/* Check whether the IRQ line is shared with other guests. */
1690int xen_test_irq_shared(int irq)
1691{
1692 struct irq_info *info = info_for_irq(irq);
1693 struct physdev_irq_status_query irq_status = { .irq = info->u.pirq.pirq };
1694
1695 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
1696 return 0;
1697 return !(irq_status.flags & XENIRQSTAT_shared);
1698}
1699EXPORT_SYMBOL_GPL(xen_test_irq_shared);
1700
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001701void xen_irq_resume(void)
1702{
Ian Campbell6cb65372011-03-10 16:08:11 +00001703 unsigned int cpu, evtchn;
1704 struct irq_info *info;
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001705
1706 init_evtchn_cpu_bindings();
1707
1708 /* New event-channel space is not 'live' yet. */
1709 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1710 mask_evtchn(evtchn);
1711
1712 /* No IRQ <-> event-channel mappings. */
Ian Campbell6cb65372011-03-10 16:08:11 +00001713 list_for_each_entry(info, &xen_irq_list_head, list)
1714 info->evtchn = 0; /* zap event-channel binding */
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001715
1716 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1717 evtchn_to_irq[evtchn] = -1;
1718
1719 for_each_possible_cpu(cpu) {
1720 restore_cpu_virqs(cpu);
1721 restore_cpu_ipis(cpu);
1722 }
Ian Campbell69035912010-11-01 16:30:09 +00001723
Ian Campbell0a852262011-03-10 16:08:06 +00001724 restore_pirqs();
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001725}
1726
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001727static struct irq_chip xen_dynamic_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001728 .name = "xen-dyn",
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001729
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001730 .irq_disable = disable_dynirq,
1731 .irq_mask = disable_dynirq,
1732 .irq_unmask = enable_dynirq,
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001733
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001734 .irq_ack = ack_dynirq,
1735 .irq_mask_ack = mask_ack_dynirq,
1736
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001737 .irq_set_affinity = set_affinity_irq,
1738 .irq_retrigger = retrigger_dynirq,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001739};
1740
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001741static struct irq_chip xen_pirq_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001742 .name = "xen-pirq",
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001743
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001744 .irq_startup = startup_pirq,
1745 .irq_shutdown = shutdown_pirq,
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001746 .irq_enable = enable_pirq,
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001747 .irq_disable = disable_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001748
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001749 .irq_mask = disable_dynirq,
1750 .irq_unmask = enable_dynirq,
1751
1752 .irq_ack = eoi_pirq,
1753 .irq_eoi = eoi_pirq,
1754 .irq_mask_ack = mask_ack_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001755
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001756 .irq_set_affinity = set_affinity_irq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001757
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001758 .irq_retrigger = retrigger_dynirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001759};
1760
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001761static struct irq_chip xen_percpu_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001762 .name = "xen-percpu",
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001763
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001764 .irq_disable = disable_dynirq,
1765 .irq_mask = disable_dynirq,
1766 .irq_unmask = enable_dynirq,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001767
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001768 .irq_ack = ack_dynirq,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001769};
1770
Sheng Yang38e20b02010-05-14 12:40:51 +01001771int xen_set_callback_via(uint64_t via)
1772{
1773 struct xen_hvm_param a;
1774 a.domid = DOMID_SELF;
1775 a.index = HVM_PARAM_CALLBACK_IRQ;
1776 a.value = via;
1777 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1778}
1779EXPORT_SYMBOL_GPL(xen_set_callback_via);
1780
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001781#ifdef CONFIG_XEN_PVHVM
Sheng Yang38e20b02010-05-14 12:40:51 +01001782/* Vector callbacks are better than PCI interrupts to receive event
1783 * channel notifications because we can receive vector callbacks on any
1784 * vcpu and we don't need PCI support or APIC interactions. */
1785void xen_callback_vector(void)
1786{
1787 int rc;
1788 uint64_t callback_via;
1789 if (xen_have_vector_callback) {
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -08001790 callback_via = HVM_CALLBACK_VECTOR(HYPERVISOR_CALLBACK_VECTOR);
Sheng Yang38e20b02010-05-14 12:40:51 +01001791 rc = xen_set_callback_via(callback_via);
1792 if (rc) {
1793 printk(KERN_ERR "Request for Xen HVM callback vector"
1794 " failed.\n");
1795 xen_have_vector_callback = 0;
1796 return;
1797 }
1798 printk(KERN_INFO "Xen HVM callback vector for event delivery is "
1799 "enabled\n");
1800 /* in the restore case the vector has already been allocated */
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -08001801 if (!test_bit(HYPERVISOR_CALLBACK_VECTOR, used_vectors))
1802 alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR,
1803 xen_hvm_callback_vector);
Sheng Yang38e20b02010-05-14 12:40:51 +01001804 }
1805}
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001806#else
1807void xen_callback_vector(void) {}
1808#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001809
Stefano Stabellini2e3d8862012-10-02 15:57:57 +01001810void __init xen_init_IRQ(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001811{
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001812 int i;
Mike Travisc7a35892009-01-10 21:58:11 -08001813
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001814 evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
1815 GFP_KERNEL);
Konrad Rzeszutek Wilk9d093e22011-09-29 13:31:21 -04001816 BUG_ON(!evtchn_to_irq);
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001817 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1818 evtchn_to_irq[i] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001819
1820 init_evtchn_cpu_bindings();
1821
1822 /* No event channels are 'live' right now. */
1823 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1824 mask_evtchn(i);
1825
Stefano Stabellini9846ff12012-01-30 16:21:48 +00001826 pirq_needs_eoi = pirq_needs_eoi_flag;
1827
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001828#ifdef CONFIG_X86
Sheng Yang38e20b02010-05-14 12:40:51 +01001829 if (xen_hvm_domain()) {
1830 xen_callback_vector();
1831 native_init_IRQ();
Stefano Stabellini3942b742010-06-24 17:50:18 +01001832 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1833 * __acpi_register_gsi can point at the right function */
1834 pci_xen_hvm_init();
Sheng Yang38e20b02010-05-14 12:40:51 +01001835 } else {
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001836 int rc;
Stefano Stabellini9846ff12012-01-30 16:21:48 +00001837 struct physdev_pirq_eoi_gmfn eoi_gmfn;
1838
Sheng Yang38e20b02010-05-14 12:40:51 +01001839 irq_ctx_init(smp_processor_id());
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +01001840 if (xen_initial_domain())
Konrad Rzeszutek Wilka0ee0562011-06-09 09:49:13 -04001841 pci_xen_initial_domain();
Stefano Stabellini9846ff12012-01-30 16:21:48 +00001842
1843 pirq_eoi_map = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
1844 eoi_gmfn.gmfn = virt_to_mfn(pirq_eoi_map);
1845 rc = HYPERVISOR_physdev_op(PHYSDEVOP_pirq_eoi_gmfn_v2, &eoi_gmfn);
1846 if (rc != 0) {
1847 free_page((unsigned long) pirq_eoi_map);
1848 pirq_eoi_map = NULL;
1849 } else
1850 pirq_needs_eoi = pirq_check_eoi_map;
Sheng Yang38e20b02010-05-14 12:40:51 +01001851 }
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001852#endif
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001853}