blob: 59e10a1286d53ff73aa3e0a6c91a5aed9033de9e [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;
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000118static unsigned long *pirq_eoi_map;
119static bool (*pirq_needs_eoi)(unsigned irq);
Jeremy Fitzhardinge3b32f572009-08-13 12:50:37 -0700120
Ian Campbellcb60d112011-03-10 16:08:08 +0000121static DEFINE_PER_CPU(unsigned long [NR_EVENT_CHANNELS/BITS_PER_LONG],
122 cpu_evtchn_mask);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700123
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700124/* Xen will never allocate port zero for any purpose. */
125#define VALID_EVTCHN(chn) ((chn) != 0)
126
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700127static struct irq_chip xen_dynamic_chip;
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700128static struct irq_chip xen_percpu_chip;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400129static struct irq_chip xen_pirq_chip;
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100130static void enable_dynirq(struct irq_data *data);
131static void disable_dynirq(struct irq_data *data);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700132
Ian Campbell9158c352011-03-10 16:08:09 +0000133/* Get info for IRQ */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800134static struct irq_info *info_for_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700135{
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100136 return irq_get_handler_data(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700137}
138
Ian Campbell9158c352011-03-10 16:08:09 +0000139/* Constructors for packed IRQ information. */
140static void xen_irq_info_common_init(struct irq_info *info,
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000141 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000142 enum xen_irq_type type,
143 unsigned short evtchn,
144 unsigned short cpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700145{
Ian Campbell9158c352011-03-10 16:08:09 +0000146
147 BUG_ON(info->type != IRQT_UNBOUND && info->type != type);
148
149 info->type = type;
Ian Campbell6cb65372011-03-10 16:08:11 +0000150 info->irq = irq;
Ian Campbell9158c352011-03-10 16:08:09 +0000151 info->evtchn = evtchn;
152 info->cpu = cpu;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000153
154 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700155}
156
Ian Campbell9158c352011-03-10 16:08:09 +0000157static void xen_irq_info_evtchn_init(unsigned irq,
158 unsigned short evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700159{
Ian Campbell9158c352011-03-10 16:08:09 +0000160 struct irq_info *info = info_for_irq(irq);
161
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000162 xen_irq_info_common_init(info, irq, IRQT_EVTCHN, evtchn, 0);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700163}
164
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000165static void xen_irq_info_ipi_init(unsigned cpu,
166 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000167 unsigned short evtchn,
168 enum ipi_vector ipi)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700169{
Ian Campbell9158c352011-03-10 16:08:09 +0000170 struct irq_info *info = info_for_irq(irq);
171
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000172 xen_irq_info_common_init(info, irq, IRQT_IPI, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000173
174 info->u.ipi = ipi;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000175
176 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700177}
178
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000179static void xen_irq_info_virq_init(unsigned cpu,
180 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000181 unsigned short evtchn,
182 unsigned short virq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700183{
Ian Campbell9158c352011-03-10 16:08:09 +0000184 struct irq_info *info = info_for_irq(irq);
185
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000186 xen_irq_info_common_init(info, irq, IRQT_VIRQ, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000187
188 info->u.virq = virq;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000189
190 per_cpu(virq_to_irq, cpu)[virq] = irq;
Ian Campbell9158c352011-03-10 16:08:09 +0000191}
192
193static void xen_irq_info_pirq_init(unsigned irq,
194 unsigned short evtchn,
195 unsigned short pirq,
196 unsigned short gsi,
197 unsigned short vector,
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400198 uint16_t domid,
Ian Campbell9158c352011-03-10 16:08:09 +0000199 unsigned char flags)
200{
201 struct irq_info *info = info_for_irq(irq);
202
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000203 xen_irq_info_common_init(info, irq, IRQT_PIRQ, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000204
205 info->u.pirq.pirq = pirq;
206 info->u.pirq.gsi = gsi;
207 info->u.pirq.vector = vector;
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400208 info->u.pirq.domid = domid;
Ian Campbell9158c352011-03-10 16:08:09 +0000209 info->u.pirq.flags = flags;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700210}
211
212/*
213 * Accessors for packed IRQ information.
214 */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800215static unsigned int evtchn_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700216{
Joe Jin110e7c72011-01-07 14:50:12 +0800217 if (unlikely(WARN(irq < 0 || irq >= nr_irqs, "Invalid irq %d!\n", irq)))
218 return 0;
219
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800220 return info_for_irq(irq)->evtchn;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700221}
222
Ian Campbelld4c04532009-02-06 19:20:31 -0800223unsigned irq_from_evtchn(unsigned int evtchn)
224{
225 return evtchn_to_irq[evtchn];
226}
227EXPORT_SYMBOL_GPL(irq_from_evtchn);
228
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800229static enum ipi_vector ipi_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700230{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800231 struct irq_info *info = info_for_irq(irq);
232
233 BUG_ON(info == NULL);
234 BUG_ON(info->type != IRQT_IPI);
235
236 return info->u.ipi;
237}
238
239static unsigned virq_from_irq(unsigned irq)
240{
241 struct irq_info *info = info_for_irq(irq);
242
243 BUG_ON(info == NULL);
244 BUG_ON(info->type != IRQT_VIRQ);
245
246 return info->u.virq;
247}
248
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100249static unsigned pirq_from_irq(unsigned irq)
250{
251 struct irq_info *info = info_for_irq(irq);
252
253 BUG_ON(info == NULL);
254 BUG_ON(info->type != IRQT_PIRQ);
255
256 return info->u.pirq.pirq;
257}
258
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800259static enum xen_irq_type type_from_irq(unsigned irq)
260{
261 return info_for_irq(irq)->type;
262}
263
264static unsigned cpu_from_irq(unsigned irq)
265{
266 return info_for_irq(irq)->cpu;
267}
268
269static unsigned int cpu_from_evtchn(unsigned int evtchn)
270{
271 int irq = evtchn_to_irq[evtchn];
272 unsigned ret = 0;
273
274 if (irq != -1)
275 ret = cpu_from_irq(irq);
276
277 return ret;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700278}
279
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000280static bool pirq_check_eoi_map(unsigned irq)
281{
Stefano Stabellini521394e2012-04-25 16:11:38 +0100282 return test_bit(pirq_from_irq(irq), pirq_eoi_map);
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000283}
284
285static bool pirq_needs_eoi_flag(unsigned irq)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400286{
287 struct irq_info *info = info_for_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400288 BUG_ON(info->type != IRQT_PIRQ);
289
290 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
291}
292
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700293static inline unsigned long active_evtchns(unsigned int cpu,
294 struct shared_info *sh,
295 unsigned int idx)
296{
Ruslan Pisarev088c05a2011-07-26 14:16:13 +0300297 return sh->evtchn_pending[idx] &
Ian Campbellcb60d112011-03-10 16:08:08 +0000298 per_cpu(cpu_evtchn_mask, cpu)[idx] &
Ruslan Pisarev088c05a2011-07-26 14:16:13 +0300299 ~sh->evtchn_mask[idx];
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700300}
301
302static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
303{
304 int irq = evtchn_to_irq[chn];
305
306 BUG_ON(irq == -1);
307#ifdef CONFIG_SMP
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000308 cpumask_copy(irq_to_desc(irq)->irq_data.affinity, cpumask_of(cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700309#endif
310
Ian Campbellcb60d112011-03-10 16:08:08 +0000311 clear_bit(chn, per_cpu(cpu_evtchn_mask, cpu_from_irq(irq)));
312 set_bit(chn, per_cpu(cpu_evtchn_mask, cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700313
Ian Campbellca62ce82011-03-10 16:08:12 +0000314 info_for_irq(irq)->cpu = cpu;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700315}
316
317static void init_evtchn_cpu_bindings(void)
318{
Jan Beulich1c6969e2010-11-16 14:55:33 -0800319 int i;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700320#ifdef CONFIG_SMP
Ian Campbell6cb65372011-03-10 16:08:11 +0000321 struct irq_info *info;
Thomas Gleixner10e58082008-10-16 14:19:04 +0200322
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700323 /* By default all event channels notify CPU#0. */
Ian Campbell6cb65372011-03-10 16:08:11 +0000324 list_for_each_entry(info, &xen_irq_list_head, list) {
325 struct irq_desc *desc = irq_to_desc(info->irq);
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000326 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800327 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700328#endif
329
Jan Beulich1c6969e2010-11-16 14:55:33 -0800330 for_each_possible_cpu(i)
Ian Campbellcb60d112011-03-10 16:08:08 +0000331 memset(per_cpu(cpu_evtchn_mask, i),
332 (i == 0) ? ~0 : 0, sizeof(*per_cpu(cpu_evtchn_mask, i)));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700333}
334
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700335static inline void clear_evtchn(int port)
336{
337 struct shared_info *s = HYPERVISOR_shared_info;
338 sync_clear_bit(port, &s->evtchn_pending[0]);
339}
340
341static inline void set_evtchn(int port)
342{
343 struct shared_info *s = HYPERVISOR_shared_info;
344 sync_set_bit(port, &s->evtchn_pending[0]);
345}
346
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700347static inline int test_evtchn(int port)
348{
349 struct shared_info *s = HYPERVISOR_shared_info;
350 return sync_test_bit(port, &s->evtchn_pending[0]);
351}
352
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700353
354/**
355 * notify_remote_via_irq - send event to remote end of event channel via irq
356 * @irq: irq of event channel to send event to
357 *
358 * Unlike notify_remote_via_evtchn(), this is safe to use across
359 * save/restore. Notifications on a broken connection are silently
360 * dropped.
361 */
362void notify_remote_via_irq(int irq)
363{
364 int evtchn = evtchn_from_irq(irq);
365
366 if (VALID_EVTCHN(evtchn))
367 notify_remote_via_evtchn(evtchn);
368}
369EXPORT_SYMBOL_GPL(notify_remote_via_irq);
370
371static void mask_evtchn(int port)
372{
373 struct shared_info *s = HYPERVISOR_shared_info;
374 sync_set_bit(port, &s->evtchn_mask[0]);
375}
376
377static void unmask_evtchn(int port)
378{
379 struct shared_info *s = HYPERVISOR_shared_info;
380 unsigned int cpu = get_cpu();
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100381 int do_hypercall = 0, evtchn_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700382
383 BUG_ON(!irqs_disabled());
384
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100385 if (unlikely((cpu != cpu_from_evtchn(port))))
386 do_hypercall = 1;
387 else
388 evtchn_pending = sync_test_bit(port, &s->evtchn_pending[0]);
389
390 if (unlikely(evtchn_pending && xen_hvm_domain()))
391 do_hypercall = 1;
392
393 /* Slow path (hypercall) if this is a non-local port or if this is
394 * an hvm domain and an event is pending (hvm domains don't have
395 * their own implementation of irq_enable). */
396 if (do_hypercall) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700397 struct evtchn_unmask unmask = { .port = port };
398 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
399 } else {
Christoph Lameter780f36d2010-12-06 11:16:29 -0600400 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700401
402 sync_clear_bit(port, &s->evtchn_mask[0]);
403
404 /*
405 * The following is basically the equivalent of
406 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
407 * the interrupt edge' if the channel is masked.
408 */
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100409 if (evtchn_pending &&
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700410 !sync_test_and_set_bit(port / BITS_PER_LONG,
411 &vcpu_info->evtchn_pending_sel))
412 vcpu_info->evtchn_upcall_pending = 1;
413 }
414
415 put_cpu();
416}
417
Ian Campbell6cb65372011-03-10 16:08:11 +0000418static void xen_irq_init(unsigned irq)
419{
420 struct irq_info *info;
Konrad Rzeszutek Wilkb5328cd2011-06-15 14:24:29 -0400421#ifdef CONFIG_SMP
Ian Campbell6cb65372011-03-10 16:08:11 +0000422 struct irq_desc *desc = irq_to_desc(irq);
423
424 /* By default all event channels notify CPU#0. */
425 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
Konrad Rzeszutek Wilk44626e42011-03-15 16:40:33 -0400426#endif
Ian Campbell6cb65372011-03-10 16:08:11 +0000427
Ian Campbellca62ce82011-03-10 16:08:12 +0000428 info = kzalloc(sizeof(*info), GFP_KERNEL);
429 if (info == NULL)
430 panic("Unable to allocate metadata for IRQ%d\n", irq);
Ian Campbell6cb65372011-03-10 16:08:11 +0000431
432 info->type = IRQT_UNBOUND;
Daniel De Graaf420eb552011-10-27 17:58:47 -0400433 info->refcnt = -1;
Ian Campbell6cb65372011-03-10 16:08:11 +0000434
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100435 irq_set_handler_data(irq, info);
Ian Campbellca62ce82011-03-10 16:08:12 +0000436
Ian Campbell6cb65372011-03-10 16:08:11 +0000437 list_add_tail(&info->list, &xen_irq_list_head);
438}
439
Ian Campbell7bee9762011-03-10 16:08:15 +0000440static int __must_check xen_allocate_irq_dynamic(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700441{
Ian Campbell89911502011-03-03 11:57:44 -0500442 int first = 0;
443 int irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700444
Ian Campbell89911502011-03-03 11:57:44 -0500445#ifdef CONFIG_X86_IO_APIC
446 /*
447 * For an HVM guest or domain 0 which see "real" (emulated or
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300448 * actual respectively) GSIs we allocate dynamic IRQs
Ian Campbell89911502011-03-03 11:57:44 -0500449 * e.g. those corresponding to event channels or MSIs
450 * etc. from the range above those "real" GSIs to avoid
451 * collisions.
Konrad Rzeszutek Wilkd1b758e2010-12-09 14:53:29 -0500452 */
Ian Campbell89911502011-03-03 11:57:44 -0500453 if (xen_initial_domain() || xen_hvm_domain())
454 first = get_nr_irqs_gsi();
455#endif
456
Ian Campbell89911502011-03-03 11:57:44 -0500457 irq = irq_alloc_desc_from(first, -1);
458
Konrad Rzeszutek Wilke6599222011-09-29 13:26:45 -0400459 if (irq >= 0)
460 xen_irq_init(irq);
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800461
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700462 return irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400463}
464
Ian Campbell7bee9762011-03-10 16:08:15 +0000465static int __must_check xen_allocate_irq_gsi(unsigned gsi)
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000466{
467 int irq;
468
Ian Campbell89911502011-03-03 11:57:44 -0500469 /*
470 * A PV guest has no concept of a GSI (since it has no ACPI
471 * nor access to/knowledge of the physical APICs). Therefore
472 * all IRQs are dynamically allocated from the entire IRQ
473 * space.
474 */
475 if (xen_pv_domain() && !xen_initial_domain())
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000476 return xen_allocate_irq_dynamic();
477
478 /* Legacy IRQ descriptors are already allocated by the arch. */
479 if (gsi < NR_IRQS_LEGACY)
Ian Campbell6cb65372011-03-10 16:08:11 +0000480 irq = gsi;
481 else
482 irq = irq_alloc_desc_at(gsi, -1);
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000483
Ian Campbell6cb65372011-03-10 16:08:11 +0000484 xen_irq_init(irq);
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000485
486 return irq;
487}
488
489static void xen_free_irq(unsigned irq)
490{
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100491 struct irq_info *info = irq_get_handler_data(irq);
Ian Campbell6cb65372011-03-10 16:08:11 +0000492
493 list_del(&info->list);
Ian Campbell9158c352011-03-10 16:08:09 +0000494
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100495 irq_set_handler_data(irq, NULL);
Ian Campbellca62ce82011-03-10 16:08:12 +0000496
Daniel De Graaf420eb552011-10-27 17:58:47 -0400497 WARN_ON(info->refcnt > 0);
498
Ian Campbellca62ce82011-03-10 16:08:12 +0000499 kfree(info);
500
Ian Campbell72146102011-02-03 09:49:35 +0000501 /* Legacy IRQ descriptors are managed by the arch. */
502 if (irq < NR_IRQS_LEGACY)
503 return;
504
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000505 irq_free_desc(irq);
506}
507
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400508static void pirq_query_unmask(int irq)
509{
510 struct physdev_irq_status_query irq_status;
511 struct irq_info *info = info_for_irq(irq);
512
513 BUG_ON(info->type != IRQT_PIRQ);
514
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100515 irq_status.irq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400516 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
517 irq_status.flags = 0;
518
519 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
520 if (irq_status.flags & XENIRQSTAT_needs_eoi)
521 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
522}
523
524static bool probing_irq(int irq)
525{
526 struct irq_desc *desc = irq_to_desc(irq);
527
528 return desc && desc->action == NULL;
529}
530
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100531static void eoi_pirq(struct irq_data *data)
532{
533 int evtchn = evtchn_from_irq(data->irq);
534 struct physdev_eoi eoi = { .irq = pirq_from_irq(data->irq) };
535 int rc = 0;
536
537 irq_move_irq(data);
538
539 if (VALID_EVTCHN(evtchn))
540 clear_evtchn(evtchn);
541
542 if (pirq_needs_eoi(data->irq)) {
543 rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
544 WARN_ON(rc);
545 }
546}
547
548static void mask_ack_pirq(struct irq_data *data)
549{
550 disable_dynirq(data);
551 eoi_pirq(data);
552}
553
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000554static unsigned int __startup_pirq(unsigned int irq)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400555{
556 struct evtchn_bind_pirq bind_pirq;
557 struct irq_info *info = info_for_irq(irq);
558 int evtchn = evtchn_from_irq(irq);
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400559 int rc;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400560
561 BUG_ON(info->type != IRQT_PIRQ);
562
563 if (VALID_EVTCHN(evtchn))
564 goto out;
565
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100566 bind_pirq.pirq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400567 /* NB. We are happy to share unless we are probing. */
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400568 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
569 BIND_PIRQ__WILL_SHARE : 0;
570 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
571 if (rc != 0) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400572 if (!probing_irq(irq))
573 printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
574 irq);
575 return 0;
576 }
577 evtchn = bind_pirq.port;
578
579 pirq_query_unmask(irq);
580
581 evtchn_to_irq[evtchn] = irq;
582 bind_evtchn_to_cpu(evtchn, 0);
583 info->evtchn = evtchn;
584
585out:
586 unmask_evtchn(evtchn);
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100587 eoi_pirq(irq_get_irq_data(irq));
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400588
589 return 0;
590}
591
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000592static unsigned int startup_pirq(struct irq_data *data)
593{
594 return __startup_pirq(data->irq);
595}
596
597static void shutdown_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400598{
599 struct evtchn_close close;
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000600 unsigned int irq = data->irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400601 struct irq_info *info = info_for_irq(irq);
602 int evtchn = evtchn_from_irq(irq);
603
604 BUG_ON(info->type != IRQT_PIRQ);
605
606 if (!VALID_EVTCHN(evtchn))
607 return;
608
609 mask_evtchn(evtchn);
610
611 close.port = evtchn;
612 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
613 BUG();
614
615 bind_evtchn_to_cpu(evtchn, 0);
616 evtchn_to_irq[evtchn] = -1;
617 info->evtchn = 0;
618}
619
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000620static void enable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400621{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000622 startup_pirq(data);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400623}
624
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000625static void disable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400626{
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100627 disable_dynirq(data);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400628}
629
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100630int xen_irq_from_gsi(unsigned gsi)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400631{
Ian Campbell6cb65372011-03-10 16:08:11 +0000632 struct irq_info *info;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400633
Ian Campbell6cb65372011-03-10 16:08:11 +0000634 list_for_each_entry(info, &xen_irq_list_head, list) {
635 if (info->type != IRQT_PIRQ)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400636 continue;
637
Ian Campbell6cb65372011-03-10 16:08:11 +0000638 if (info->u.pirq.gsi == gsi)
639 return info->irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400640 }
641
642 return -1;
643}
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100644EXPORT_SYMBOL_GPL(xen_irq_from_gsi);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400645
Ian Campbell653378a2011-03-10 16:08:04 +0000646/*
647 * Do not make any assumptions regarding the relationship between the
648 * IRQ number returned here and the Xen pirq argument.
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100649 *
650 * Note: We don't assign an event channel until the irq actually started
651 * up. Return an existing irq if we've already got one for the gsi.
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100652 *
653 * Shareable implies level triggered, not shareable implies edge
654 * triggered here.
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400655 */
Ian Campbellf4d06352011-03-10 16:08:07 +0000656int xen_bind_pirq_gsi_to_irq(unsigned gsi,
657 unsigned pirq, int shareable, char *name)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400658{
Ian Campbella0e18112011-03-10 16:08:03 +0000659 int irq = -1;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400660 struct physdev_irq irq_op;
661
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400662 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400663
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100664 irq = xen_irq_from_gsi(gsi);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400665 if (irq != -1) {
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100666 printk(KERN_INFO "xen_map_pirq_gsi: returning irq %d for gsi %u\n",
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400667 irq, gsi);
Daniel De Graaf420eb552011-10-27 17:58:47 -0400668 goto out;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400669 }
670
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000671 irq = xen_allocate_irq_gsi(gsi);
Ian Campbell7bee9762011-03-10 16:08:15 +0000672 if (irq < 0)
673 goto out;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400674
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400675 irq_op.irq = irq;
Alex Nixonb5401a92010-03-18 16:31:34 -0400676 irq_op.vector = 0;
677
678 /* Only the privileged domain can do this. For non-priv, the pcifront
679 * driver provides a PCI bus that does the call to do exactly
680 * this in the priv domain. */
681 if (xen_initial_domain() &&
682 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000683 xen_free_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400684 irq = -ENOSPC;
685 goto out;
686 }
687
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400688 xen_irq_info_pirq_init(irq, 0, pirq, gsi, irq_op.vector, DOMID_SELF,
Ian Campbell9158c352011-03-10 16:08:09 +0000689 shareable ? PIRQ_SHAREABLE : 0);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400690
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100691 pirq_query_unmask(irq);
692 /* We try to use the handler with the appropriate semantic for the
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100693 * type of interrupt: if the interrupt is an edge triggered
694 * interrupt we use handle_edge_irq.
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100695 *
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100696 * On the other hand if the interrupt is level triggered we use
697 * handle_fasteoi_irq like the native code does for this kind of
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100698 * interrupts.
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100699 *
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100700 * Depending on the Xen version, pirq_needs_eoi might return true
701 * not only for level triggered interrupts but for edge triggered
702 * interrupts too. In any case Xen always honors the eoi mechanism,
703 * not injecting any more pirqs of the same kind if the first one
704 * hasn't received an eoi yet. Therefore using the fasteoi handler
705 * is the right choice either way.
706 */
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100707 if (shareable)
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100708 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
709 handle_fasteoi_irq, name);
710 else
711 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
712 handle_edge_irq, name);
713
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400714out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400715 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400716
717 return irq;
718}
719
Qing Hef731e3ef2010-10-11 15:30:09 +0100720#ifdef CONFIG_PCI_MSI
Ian Campbellbf480d92011-02-18 16:43:32 +0000721int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000722{
Ian Campbell5cad61a2011-02-18 16:43:31 +0000723 int rc;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000724 struct physdev_get_free_pirq op_get_free_pirq;
Ian Campbell5cad61a2011-02-18 16:43:31 +0000725
Ian Campbellbf480d92011-02-18 16:43:32 +0000726 op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000727 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000728
Ian Campbell5cad61a2011-02-18 16:43:31 +0000729 WARN_ONCE(rc == -ENOSYS,
730 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
731
732 return rc ? -1 : op_get_free_pirq.pirq;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000733}
734
Ian Campbellbf480d92011-02-18 16:43:32 +0000735int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400736 int pirq, int vector, const char *name,
737 domid_t domid)
Stefano Stabellini809f9262010-07-01 17:10:39 +0100738{
Ian Campbellbf480d92011-02-18 16:43:32 +0000739 int irq, ret;
Ian Campbell4b41df72011-02-18 16:43:29 +0000740
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400741 mutex_lock(&irq_mapping_update_lock);
Stefano Stabellini809f9262010-07-01 17:10:39 +0100742
Ian Campbell4b41df72011-02-18 16:43:29 +0000743 irq = xen_allocate_irq_dynamic();
Konrad Rzeszutek Wilke6599222011-09-29 13:26:45 -0400744 if (irq < 0)
Ian Campbellbb5d0792011-02-18 16:43:28 +0000745 goto out;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100746
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100747 irq_set_chip_and_handler_name(irq, &xen_pirq_chip, handle_edge_irq,
748 name);
Stefano Stabellini809f9262010-07-01 17:10:39 +0100749
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400750 xen_irq_info_pirq_init(irq, 0, pirq, 0, vector, domid, 0);
Linus Torvalds5f6fb452011-03-15 19:23:40 -0700751 ret = irq_set_msi_desc(irq, msidesc);
Ian Campbellbf480d92011-02-18 16:43:32 +0000752 if (ret < 0)
753 goto error_irq;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100754out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400755 mutex_unlock(&irq_mapping_update_lock);
Ian Campbell4b41df72011-02-18 16:43:29 +0000756 return irq;
Ian Campbellbf480d92011-02-18 16:43:32 +0000757error_irq:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400758 mutex_unlock(&irq_mapping_update_lock);
Ian Campbellbf480d92011-02-18 16:43:32 +0000759 xen_free_irq(irq);
Konrad Rzeszutek Wilke6599222011-09-29 13:26:45 -0400760 return ret;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100761}
Qing Hef731e3ef2010-10-11 15:30:09 +0100762#endif
763
Alex Nixonb5401a92010-03-18 16:31:34 -0400764int xen_destroy_irq(int irq)
765{
766 struct irq_desc *desc;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100767 struct physdev_unmap_pirq unmap_irq;
768 struct irq_info *info = info_for_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400769 int rc = -ENOENT;
770
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400771 mutex_lock(&irq_mapping_update_lock);
Alex Nixonb5401a92010-03-18 16:31:34 -0400772
773 desc = irq_to_desc(irq);
774 if (!desc)
775 goto out;
776
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100777 if (xen_initial_domain()) {
Konrad Rzeszutek Wilk12334712010-11-19 11:27:09 -0500778 unmap_irq.pirq = info->u.pirq.pirq;
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400779 unmap_irq.domid = info->u.pirq.domid;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100780 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
Konrad Rzeszutek Wilk1eff1ad2011-02-16 16:26:44 -0500781 /* If another domain quits without making the pci_disable_msix
782 * call, the Xen hypervisor takes care of freeing the PIRQs
783 * (free_domain_pirqs).
784 */
785 if ((rc == -ESRCH && info->u.pirq.domid != DOMID_SELF))
786 printk(KERN_INFO "domain %d does not have %d anymore\n",
787 info->u.pirq.domid, info->u.pirq.pirq);
788 else if (rc) {
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100789 printk(KERN_WARNING "unmap irq failed %d\n", rc);
790 goto out;
791 }
792 }
Alex Nixonb5401a92010-03-18 16:31:34 -0400793
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000794 xen_free_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400795
796out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400797 mutex_unlock(&irq_mapping_update_lock);
Alex Nixonb5401a92010-03-18 16:31:34 -0400798 return rc;
799}
800
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000801int xen_irq_from_pirq(unsigned pirq)
802{
Ian Campbell69c358c2011-03-10 16:08:13 +0000803 int irq;
804
805 struct irq_info *info;
806
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400807 mutex_lock(&irq_mapping_update_lock);
Ian Campbell69c358c2011-03-10 16:08:13 +0000808
809 list_for_each_entry(info, &xen_irq_list_head, list) {
Konrad Rzeszutek Wilk9bb9efe2011-09-29 13:13:30 -0400810 if (info->type != IRQT_PIRQ)
Ian Campbell69c358c2011-03-10 16:08:13 +0000811 continue;
812 irq = info->irq;
813 if (info->u.pirq.pirq == pirq)
814 goto out;
815 }
816 irq = -1;
817out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400818 mutex_unlock(&irq_mapping_update_lock);
Ian Campbell69c358c2011-03-10 16:08:13 +0000819
820 return irq;
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000821}
822
Konrad Rzeszutek Wilke6197ac2011-02-24 14:20:12 -0500823
824int xen_pirq_from_irq(unsigned irq)
825{
826 return pirq_from_irq(irq);
827}
828EXPORT_SYMBOL_GPL(xen_pirq_from_irq);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700829int bind_evtchn_to_irq(unsigned int evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700830{
831 int irq;
832
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400833 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700834
835 irq = evtchn_to_irq[evtchn];
836
837 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000838 irq = xen_allocate_irq_dynamic();
Ian Campbell7bee9762011-03-10 16:08:15 +0000839 if (irq == -1)
840 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700841
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100842 irq_set_chip_and_handler_name(irq, &xen_dynamic_chip,
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100843 handle_edge_irq, "event");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700844
Ian Campbell9158c352011-03-10 16:08:09 +0000845 xen_irq_info_evtchn_init(irq, evtchn);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400846 } else {
847 struct irq_info *info = info_for_irq(irq);
848 WARN_ON(info == NULL || info->type != IRQT_EVTCHN);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700849 }
Stefano Stabellinia8636c02012-08-22 17:20:15 +0100850 irq_clear_status_flags(irq, IRQ_NOREQUEST|IRQ_NOAUTOEN);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700851
Ian Campbell7bee9762011-03-10 16:08:15 +0000852out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400853 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700854
855 return irq;
856}
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700857EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700858
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700859static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
860{
861 struct evtchn_bind_ipi bind_ipi;
862 int evtchn, irq;
863
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400864 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700865
866 irq = per_cpu(ipi_to_irq, cpu)[ipi];
Ian Campbell90af9512009-02-06 16:55:58 -0800867
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700868 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000869 irq = xen_allocate_irq_dynamic();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700870 if (irq < 0)
871 goto out;
872
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100873 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700874 handle_percpu_irq, "ipi");
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700875
876 bind_ipi.vcpu = cpu;
877 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
878 &bind_ipi) != 0)
879 BUG();
880 evtchn = bind_ipi.port;
881
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000882 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700883
884 bind_evtchn_to_cpu(evtchn, cpu);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400885 } else {
886 struct irq_info *info = info_for_irq(irq);
887 WARN_ON(info == NULL || info->type != IRQT_IPI);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700888 }
889
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700890 out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400891 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700892 return irq;
893}
894
Ian Campbell2e820f52009-02-09 12:05:50 -0800895static int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
896 unsigned int remote_port)
897{
898 struct evtchn_bind_interdomain bind_interdomain;
899 int err;
900
901 bind_interdomain.remote_dom = remote_domain;
902 bind_interdomain.remote_port = remote_port;
903
904 err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
905 &bind_interdomain);
906
907 return err ? : bind_evtchn_to_irq(bind_interdomain.local_port);
908}
909
Olaf Hering62cc5fc2011-08-25 18:30:48 +0200910static int find_virq(unsigned int virq, unsigned int cpu)
911{
912 struct evtchn_status status;
913 int port, rc = -ENOENT;
914
915 memset(&status, 0, sizeof(status));
916 for (port = 0; port <= NR_EVENT_CHANNELS; port++) {
917 status.dom = DOMID_SELF;
918 status.port = port;
919 rc = HYPERVISOR_event_channel_op(EVTCHNOP_status, &status);
920 if (rc < 0)
921 continue;
922 if (status.status != EVTCHNSTAT_virq)
923 continue;
924 if (status.u.virq == virq && status.vcpu == cpu) {
925 rc = port;
926 break;
927 }
928 }
929 return rc;
930}
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700931
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100932int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700933{
934 struct evtchn_bind_virq bind_virq;
Olaf Hering62cc5fc2011-08-25 18:30:48 +0200935 int evtchn, irq, ret;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700936
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400937 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700938
939 irq = per_cpu(virq_to_irq, cpu)[virq];
940
941 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000942 irq = xen_allocate_irq_dynamic();
Ian Campbell7bee9762011-03-10 16:08:15 +0000943 if (irq == -1)
944 goto out;
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700945
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100946 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700947 handle_percpu_irq, "virq");
948
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700949 bind_virq.virq = virq;
950 bind_virq.vcpu = cpu;
Olaf Hering62cc5fc2011-08-25 18:30:48 +0200951 ret = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
952 &bind_virq);
953 if (ret == 0)
954 evtchn = bind_virq.port;
955 else {
956 if (ret == -EEXIST)
957 ret = find_virq(virq, cpu);
958 BUG_ON(ret < 0);
959 evtchn = ret;
960 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700961
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000962 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700963
964 bind_evtchn_to_cpu(evtchn, cpu);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400965 } else {
966 struct irq_info *info = info_for_irq(irq);
967 WARN_ON(info == NULL || info->type != IRQT_VIRQ);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700968 }
969
Ian Campbell7bee9762011-03-10 16:08:15 +0000970out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400971 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700972
973 return irq;
974}
975
976static void unbind_from_irq(unsigned int irq)
977{
978 struct evtchn_close close;
979 int evtchn = evtchn_from_irq(irq);
Daniel De Graaf420eb552011-10-27 17:58:47 -0400980 struct irq_info *info = irq_get_handler_data(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700981
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400982 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700983
Daniel De Graaf420eb552011-10-27 17:58:47 -0400984 if (info->refcnt > 0) {
985 info->refcnt--;
986 if (info->refcnt != 0)
987 goto done;
988 }
989
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800990 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700991 close.port = evtchn;
992 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
993 BUG();
994
995 switch (type_from_irq(irq)) {
996 case IRQT_VIRQ:
997 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800998 [virq_from_irq(irq)] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700999 break;
Alex Nixond68d82a2008-08-22 11:52:15 +01001000 case IRQT_IPI:
1001 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001002 [ipi_from_irq(irq)] = -1;
Alex Nixond68d82a2008-08-22 11:52:15 +01001003 break;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001004 default:
1005 break;
1006 }
1007
1008 /* Closed ports are implicitly re-bound to VCPU0. */
1009 bind_evtchn_to_cpu(evtchn, 0);
1010
1011 evtchn_to_irq[evtchn] = -1;
Ian Campbellfed5ea82009-12-01 16:15:30 +00001012 }
1013
Ian Campbellca62ce82011-03-10 16:08:12 +00001014 BUG_ON(info_for_irq(irq)->type == IRQT_UNBOUND);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001015
Ian Campbell9158c352011-03-10 16:08:09 +00001016 xen_free_irq(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001017
Daniel De Graaf420eb552011-10-27 17:58:47 -04001018 done:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001019 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001020}
1021
1022int bind_evtchn_to_irqhandler(unsigned int evtchn,
Jeff Garzik7c239972007-10-19 03:12:20 -04001023 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001024 unsigned long irqflags,
1025 const char *devname, void *dev_id)
1026{
Nicolas Kaiser361ae8c2011-03-30 21:14:26 +02001027 int irq, retval;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001028
1029 irq = bind_evtchn_to_irq(evtchn);
Ian Campbell7bee9762011-03-10 16:08:15 +00001030 if (irq < 0)
1031 return irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001032 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1033 if (retval != 0) {
1034 unbind_from_irq(irq);
1035 return retval;
1036 }
1037
1038 return irq;
1039}
1040EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
1041
Ian Campbell2e820f52009-02-09 12:05:50 -08001042int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
1043 unsigned int remote_port,
1044 irq_handler_t handler,
1045 unsigned long irqflags,
1046 const char *devname,
1047 void *dev_id)
1048{
1049 int irq, retval;
1050
1051 irq = bind_interdomain_evtchn_to_irq(remote_domain, remote_port);
1052 if (irq < 0)
1053 return irq;
1054
1055 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1056 if (retval != 0) {
1057 unbind_from_irq(irq);
1058 return retval;
1059 }
1060
1061 return irq;
1062}
1063EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler);
1064
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001065int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
Jeff Garzik7c239972007-10-19 03:12:20 -04001066 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001067 unsigned long irqflags, const char *devname, void *dev_id)
1068{
Nicolas Kaiser361ae8c2011-03-30 21:14:26 +02001069 int irq, retval;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001070
1071 irq = bind_virq_to_irq(virq, cpu);
Ian Campbell7bee9762011-03-10 16:08:15 +00001072 if (irq < 0)
1073 return irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001074 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1075 if (retval != 0) {
1076 unbind_from_irq(irq);
1077 return retval;
1078 }
1079
1080 return irq;
1081}
1082EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
1083
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001084int bind_ipi_to_irqhandler(enum ipi_vector ipi,
1085 unsigned int cpu,
1086 irq_handler_t handler,
1087 unsigned long irqflags,
1088 const char *devname,
1089 void *dev_id)
1090{
1091 int irq, retval;
1092
1093 irq = bind_ipi_to_irq(ipi, cpu);
1094 if (irq < 0)
1095 return irq;
1096
Ian Campbell9bab0b72011-10-03 15:37:00 +01001097 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME | IRQF_EARLY_RESUME;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -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}
1106
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001107void unbind_from_irqhandler(unsigned int irq, void *dev_id)
1108{
1109 free_irq(irq, dev_id);
1110 unbind_from_irq(irq);
1111}
1112EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
1113
Daniel De Graaf420eb552011-10-27 17:58:47 -04001114int evtchn_make_refcounted(unsigned int evtchn)
1115{
1116 int irq = evtchn_to_irq[evtchn];
1117 struct irq_info *info;
1118
1119 if (irq == -1)
1120 return -ENOENT;
1121
1122 info = irq_get_handler_data(irq);
1123
1124 if (!info)
1125 return -ENOENT;
1126
1127 WARN_ON(info->refcnt != -1);
1128
1129 info->refcnt = 1;
1130
1131 return 0;
1132}
1133EXPORT_SYMBOL_GPL(evtchn_make_refcounted);
1134
1135int evtchn_get(unsigned int evtchn)
1136{
1137 int irq;
1138 struct irq_info *info;
1139 int err = -ENOENT;
1140
Daniel De Graafc3b3f162011-11-28 11:49:09 -05001141 if (evtchn >= NR_EVENT_CHANNELS)
1142 return -EINVAL;
1143
Daniel De Graaf420eb552011-10-27 17:58:47 -04001144 mutex_lock(&irq_mapping_update_lock);
1145
1146 irq = evtchn_to_irq[evtchn];
1147 if (irq == -1)
1148 goto done;
1149
1150 info = irq_get_handler_data(irq);
1151
1152 if (!info)
1153 goto done;
1154
1155 err = -EINVAL;
1156 if (info->refcnt <= 0)
1157 goto done;
1158
1159 info->refcnt++;
1160 err = 0;
1161 done:
1162 mutex_unlock(&irq_mapping_update_lock);
1163
1164 return err;
1165}
1166EXPORT_SYMBOL_GPL(evtchn_get);
1167
1168void evtchn_put(unsigned int evtchn)
1169{
1170 int irq = evtchn_to_irq[evtchn];
1171 if (WARN_ON(irq == -1))
1172 return;
1173 unbind_from_irq(irq);
1174}
1175EXPORT_SYMBOL_GPL(evtchn_put);
1176
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001177void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
1178{
1179 int irq = per_cpu(ipi_to_irq, cpu)[vector];
1180 BUG_ON(irq < 0);
1181 notify_remote_via_irq(irq);
1182}
1183
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001184irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
1185{
1186 struct shared_info *sh = HYPERVISOR_shared_info;
1187 int cpu = smp_processor_id();
Ian Campbellcb60d112011-03-10 16:08:08 +00001188 unsigned long *cpu_evtchn = per_cpu(cpu_evtchn_mask, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001189 int i;
1190 unsigned long flags;
1191 static DEFINE_SPINLOCK(debug_lock);
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001192 struct vcpu_info *v;
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001193
1194 spin_lock_irqsave(&debug_lock, flags);
1195
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001196 printk("\nvcpu %d\n ", cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001197
1198 for_each_online_cpu(i) {
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001199 int pending;
1200 v = per_cpu(xen_vcpu, i);
1201 pending = (get_irq_regs() && i == cpu)
1202 ? xen_irqs_disabled(get_irq_regs())
1203 : v->evtchn_upcall_mask;
1204 printk("%d: masked=%d pending=%d event_sel %0*lx\n ", i,
1205 pending, v->evtchn_upcall_pending,
1206 (int)(sizeof(v->evtchn_pending_sel)*2),
1207 v->evtchn_pending_sel);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001208 }
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001209 v = per_cpu(xen_vcpu, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001210
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001211 printk("\npending:\n ");
1212 for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
1213 printk("%0*lx%s", (int)sizeof(sh->evtchn_pending[0])*2,
1214 sh->evtchn_pending[i],
1215 i % 8 == 0 ? "\n " : " ");
1216 printk("\nglobal mask:\n ");
1217 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1218 printk("%0*lx%s",
1219 (int)(sizeof(sh->evtchn_mask[0])*2),
1220 sh->evtchn_mask[i],
1221 i % 8 == 0 ? "\n " : " ");
1222
1223 printk("\nglobally unmasked:\n ");
1224 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1225 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
1226 sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
1227 i % 8 == 0 ? "\n " : " ");
1228
1229 printk("\nlocal cpu%d mask:\n ", cpu);
1230 for (i = (NR_EVENT_CHANNELS/BITS_PER_LONG)-1; i >= 0; i--)
1231 printk("%0*lx%s", (int)(sizeof(cpu_evtchn[0])*2),
1232 cpu_evtchn[i],
1233 i % 8 == 0 ? "\n " : " ");
1234
1235 printk("\nlocally unmasked:\n ");
1236 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) {
1237 unsigned long pending = sh->evtchn_pending[i]
1238 & ~sh->evtchn_mask[i]
1239 & cpu_evtchn[i];
1240 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
1241 pending, i % 8 == 0 ? "\n " : " ");
1242 }
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001243
1244 printk("\npending list:\n");
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001245 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001246 if (sync_test_bit(i, sh->evtchn_pending)) {
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001247 int word_idx = i / BITS_PER_LONG;
1248 printk(" %d: event %d -> irq %d%s%s%s\n",
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001249 cpu_from_evtchn(i), i,
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001250 evtchn_to_irq[i],
1251 sync_test_bit(word_idx, &v->evtchn_pending_sel)
1252 ? "" : " l2-clear",
1253 !sync_test_bit(i, sh->evtchn_mask)
1254 ? "" : " globally-masked",
1255 sync_test_bit(i, cpu_evtchn)
1256 ? "" : " locally-masked");
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001257 }
1258 }
1259
1260 spin_unlock_irqrestore(&debug_lock, flags);
1261
1262 return IRQ_HANDLED;
1263}
1264
Tejun Heo245b2e72009-06-24 15:13:48 +09001265static DEFINE_PER_CPU(unsigned, xed_nesting_count);
Keir Fraserada68142011-03-03 10:01:11 +00001266static DEFINE_PER_CPU(unsigned int, current_word_idx);
1267static DEFINE_PER_CPU(unsigned int, current_bit_idx);
Tejun Heo245b2e72009-06-24 15:13:48 +09001268
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001269/*
Scott Rixnerab7f8632011-03-03 09:30:08 +00001270 * Mask out the i least significant bits of w
1271 */
1272#define MASK_LSBS(w, i) (w & ((~0UL) << i))
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001273
1274/*
1275 * Search the CPUs pending events bitmasks. For each one found, map
1276 * the event number to an irq, and feed it into do_IRQ() for
1277 * handling.
1278 *
1279 * Xen uses a two-level bitmap to speed searching. The first level is
1280 * a bitset of words which contain pending event bits. The second
1281 * level is a bitset of pending events themselves.
1282 */
Sheng Yang38e20b02010-05-14 12:40:51 +01001283static void __xen_evtchn_do_upcall(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001284{
Keir Fraser24b51c22011-03-03 11:06:28 +00001285 int start_word_idx, start_bit_idx;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001286 int word_idx, bit_idx;
Keir Fraser24b51c22011-03-03 11:06:28 +00001287 int i;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001288 int cpu = get_cpu();
1289 struct shared_info *s = HYPERVISOR_shared_info;
Christoph Lameter780f36d2010-12-06 11:16:29 -06001290 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
Ruslan Pisarev088c05a2011-07-26 14:16:13 +03001291 unsigned count;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001292
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001293 do {
1294 unsigned long pending_words;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001295
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001296 vcpu_info->evtchn_upcall_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001297
Christoph Lameterb2e4ae62010-12-06 11:40:07 -06001298 if (__this_cpu_inc_return(xed_nesting_count) - 1)
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001299 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001300
Isaku Yamahatae849c3e2008-04-02 10:53:56 -07001301#ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
1302 /* Clear master flag /before/ clearing selector flag. */
Isaku Yamahata6673cf62008-06-16 14:58:13 -07001303 wmb();
Isaku Yamahatae849c3e2008-04-02 10:53:56 -07001304#endif
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001305 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001306
Keir Fraser24b51c22011-03-03 11:06:28 +00001307 start_word_idx = __this_cpu_read(current_word_idx);
1308 start_bit_idx = __this_cpu_read(current_bit_idx);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001309
Keir Fraser24b51c22011-03-03 11:06:28 +00001310 word_idx = start_word_idx;
1311
1312 for (i = 0; pending_words != 0; i++) {
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001313 unsigned long pending_bits;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001314 unsigned long words;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001315
Scott Rixnerab7f8632011-03-03 09:30:08 +00001316 words = MASK_LSBS(pending_words, word_idx);
1317
1318 /*
Keir Fraserada68142011-03-03 10:01:11 +00001319 * If we masked out all events, wrap to beginning.
Scott Rixnerab7f8632011-03-03 09:30:08 +00001320 */
1321 if (words == 0) {
Keir Fraserada68142011-03-03 10:01:11 +00001322 word_idx = 0;
1323 bit_idx = 0;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001324 continue;
1325 }
1326 word_idx = __ffs(words);
1327
Keir Fraser24b51c22011-03-03 11:06:28 +00001328 pending_bits = active_evtchns(cpu, s, word_idx);
1329 bit_idx = 0; /* usually scan entire word from start */
1330 if (word_idx == start_word_idx) {
1331 /* We scan the starting word in two parts */
1332 if (i == 0)
1333 /* 1st time: start in the middle */
1334 bit_idx = start_bit_idx;
1335 else
1336 /* 2nd time: mask bits done already */
1337 bit_idx &= (1UL << start_bit_idx) - 1;
1338 }
1339
Scott Rixnerab7f8632011-03-03 09:30:08 +00001340 do {
1341 unsigned long bits;
1342 int port, irq;
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -08001343 struct irq_desc *desc;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001344
Scott Rixnerab7f8632011-03-03 09:30:08 +00001345 bits = MASK_LSBS(pending_bits, bit_idx);
1346
1347 /* If we masked out all events, move on. */
Keir Fraserada68142011-03-03 10:01:11 +00001348 if (bits == 0)
Scott Rixnerab7f8632011-03-03 09:30:08 +00001349 break;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001350
1351 bit_idx = __ffs(bits);
1352
1353 /* Process port. */
1354 port = (word_idx * BITS_PER_LONG) + bit_idx;
1355 irq = evtchn_to_irq[port];
1356
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -08001357 if (irq != -1) {
1358 desc = irq_to_desc(irq);
1359 if (desc)
1360 generic_handle_irq_desc(irq, desc);
1361 }
Scott Rixnerab7f8632011-03-03 09:30:08 +00001362
Keir Fraserada68142011-03-03 10:01:11 +00001363 bit_idx = (bit_idx + 1) % BITS_PER_LONG;
1364
1365 /* Next caller starts at last processed + 1 */
1366 __this_cpu_write(current_word_idx,
1367 bit_idx ? word_idx :
1368 (word_idx+1) % BITS_PER_LONG);
1369 __this_cpu_write(current_bit_idx, bit_idx);
1370 } while (bit_idx != 0);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001371
Keir Fraser24b51c22011-03-03 11:06:28 +00001372 /* Scan start_l1i twice; all others once. */
1373 if ((word_idx != start_word_idx) || (i != 0))
Scott Rixnerab7f8632011-03-03 09:30:08 +00001374 pending_words &= ~(1UL << word_idx);
Keir Fraserada68142011-03-03 10:01:11 +00001375
1376 word_idx = (word_idx + 1) % BITS_PER_LONG;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001377 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001378
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001379 BUG_ON(!irqs_disabled());
1380
Christoph Lameter780f36d2010-12-06 11:16:29 -06001381 count = __this_cpu_read(xed_nesting_count);
1382 __this_cpu_write(xed_nesting_count, 0);
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001383 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001384
1385out:
Jeremy Fitzhardinge3445a8f2009-02-06 14:09:46 -08001386
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001387 put_cpu();
1388}
1389
Sheng Yang38e20b02010-05-14 12:40:51 +01001390void xen_evtchn_do_upcall(struct pt_regs *regs)
1391{
1392 struct pt_regs *old_regs = set_irq_regs(regs);
1393
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001394#ifdef CONFIG_X86
Sheng Yang38e20b02010-05-14 12:40:51 +01001395 exit_idle();
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001396#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001397 irq_enter();
1398
1399 __xen_evtchn_do_upcall();
1400
1401 irq_exit();
1402 set_irq_regs(old_regs);
1403}
1404
1405void xen_hvm_evtchn_do_upcall(void)
1406{
1407 __xen_evtchn_do_upcall();
1408}
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001409EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
Sheng Yang38e20b02010-05-14 12:40:51 +01001410
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001411/* Rebind a new event channel to an existing irq. */
1412void rebind_evtchn_irq(int evtchn, int irq)
1413{
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001414 struct irq_info *info = info_for_irq(irq);
1415
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001416 /* Make sure the irq is masked, since the new event channel
1417 will also be masked. */
1418 disable_irq(irq);
1419
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001420 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001421
1422 /* After resume the irq<->evtchn mappings are all cleared out */
1423 BUG_ON(evtchn_to_irq[evtchn] != -1);
1424 /* Expect irq to have been bound before,
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001425 so there should be a proper type */
1426 BUG_ON(info->type == IRQT_UNBOUND);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001427
Ian Campbell9158c352011-03-10 16:08:09 +00001428 xen_irq_info_evtchn_init(irq, evtchn);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001429
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001430 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001431
1432 /* new event channels are always bound to cpu 0 */
Rusty Russell0de26522008-12-13 21:20:26 +10301433 irq_set_affinity(irq, cpumask_of(0));
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001434
1435 /* Unmask the event channel. */
1436 enable_irq(irq);
1437}
1438
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001439/* Rebind an evtchn so that it gets delivered to a specific cpu */
Yinghai Lud5dedd42009-04-27 17:59:21 -07001440static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001441{
1442 struct evtchn_bind_vcpu bind_vcpu;
1443 int evtchn = evtchn_from_irq(irq);
1444
Ian Campbellbe494722011-03-10 16:08:02 +00001445 if (!VALID_EVTCHN(evtchn))
1446 return -1;
1447
1448 /*
1449 * Events delivered via platform PCI interrupts are always
1450 * routed to vcpu 0 and hence cannot be rebound.
1451 */
1452 if (xen_hvm_domain() && !xen_have_vector_callback)
Yinghai Lud5dedd42009-04-27 17:59:21 -07001453 return -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001454
1455 /* Send future instances of this interrupt to other vcpu. */
1456 bind_vcpu.port = evtchn;
1457 bind_vcpu.vcpu = tcpu;
1458
1459 /*
1460 * If this fails, it usually just indicates that we're dealing with a
1461 * virq or IPI channel, which don't actually need to be rebound. Ignore
1462 * it, but don't do the xenlinux-level rebind in that case.
1463 */
1464 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1465 bind_evtchn_to_cpu(evtchn, tcpu);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001466
1467 return 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001468}
1469
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001470static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1471 bool force)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001472{
Rusty Russell0de26522008-12-13 21:20:26 +10301473 unsigned tcpu = cpumask_first(dest);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001474
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001475 return rebind_irq_to_cpu(data->irq, tcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001476}
1477
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001478int resend_irq_on_evtchn(unsigned int irq)
1479{
1480 int masked, evtchn = evtchn_from_irq(irq);
1481 struct shared_info *s = HYPERVISOR_shared_info;
1482
1483 if (!VALID_EVTCHN(evtchn))
1484 return 1;
1485
1486 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
1487 sync_set_bit(evtchn, s->evtchn_pending);
1488 if (!masked)
1489 unmask_evtchn(evtchn);
1490
1491 return 1;
1492}
1493
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001494static void enable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001495{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001496 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001497
1498 if (VALID_EVTCHN(evtchn))
1499 unmask_evtchn(evtchn);
1500}
1501
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001502static void disable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001503{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001504 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001505
1506 if (VALID_EVTCHN(evtchn))
1507 mask_evtchn(evtchn);
1508}
1509
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001510static void ack_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001511{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001512 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001513
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001514 irq_move_irq(data);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001515
1516 if (VALID_EVTCHN(evtchn))
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001517 clear_evtchn(evtchn);
1518}
1519
1520static void mask_ack_dynirq(struct irq_data *data)
1521{
1522 disable_dynirq(data);
1523 ack_dynirq(data);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001524}
1525
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001526static int retrigger_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001527{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001528 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001529 struct shared_info *sh = HYPERVISOR_shared_info;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001530 int ret = 0;
1531
1532 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001533 int masked;
1534
1535 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
1536 sync_set_bit(evtchn, sh->evtchn_pending);
1537 if (!masked)
1538 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001539 ret = 1;
1540 }
1541
1542 return ret;
1543}
1544
Ian Campbell0a852262011-03-10 16:08:06 +00001545static void restore_pirqs(void)
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001546{
1547 int pirq, rc, irq, gsi;
1548 struct physdev_map_pirq map_irq;
Ian Campbell69c358c2011-03-10 16:08:13 +00001549 struct irq_info *info;
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001550
Ian Campbell69c358c2011-03-10 16:08:13 +00001551 list_for_each_entry(info, &xen_irq_list_head, list) {
1552 if (info->type != IRQT_PIRQ)
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001553 continue;
1554
Ian Campbell69c358c2011-03-10 16:08:13 +00001555 pirq = info->u.pirq.pirq;
1556 gsi = info->u.pirq.gsi;
1557 irq = info->irq;
1558
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001559 /* save/restore of PT devices doesn't work, so at this point the
1560 * only devices present are GSI based emulated devices */
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001561 if (!gsi)
1562 continue;
1563
1564 map_irq.domid = DOMID_SELF;
1565 map_irq.type = MAP_PIRQ_TYPE_GSI;
1566 map_irq.index = gsi;
1567 map_irq.pirq = pirq;
1568
1569 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1570 if (rc) {
1571 printk(KERN_WARNING "xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1572 gsi, irq, pirq, rc);
Ian Campbell9158c352011-03-10 16:08:09 +00001573 xen_free_irq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001574 continue;
1575 }
1576
1577 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1578
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001579 __startup_pirq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001580 }
1581}
1582
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001583static void restore_cpu_virqs(unsigned int cpu)
1584{
1585 struct evtchn_bind_virq bind_virq;
1586 int virq, irq, evtchn;
1587
1588 for (virq = 0; virq < NR_VIRQS; virq++) {
1589 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1590 continue;
1591
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001592 BUG_ON(virq_from_irq(irq) != virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001593
1594 /* Get a new binding from Xen. */
1595 bind_virq.virq = virq;
1596 bind_virq.vcpu = cpu;
1597 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1598 &bind_virq) != 0)
1599 BUG();
1600 evtchn = bind_virq.port;
1601
1602 /* Record the new mapping. */
Ian Campbell3d4cfa32011-03-10 16:08:10 +00001603 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001604 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001605 }
1606}
1607
1608static void restore_cpu_ipis(unsigned int cpu)
1609{
1610 struct evtchn_bind_ipi bind_ipi;
1611 int ipi, irq, evtchn;
1612
1613 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1614 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1615 continue;
1616
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001617 BUG_ON(ipi_from_irq(irq) != ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001618
1619 /* Get a new binding from Xen. */
1620 bind_ipi.vcpu = cpu;
1621 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1622 &bind_ipi) != 0)
1623 BUG();
1624 evtchn = bind_ipi.port;
1625
1626 /* Record the new mapping. */
Ian Campbell3d4cfa32011-03-10 16:08:10 +00001627 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001628 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001629 }
1630}
1631
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001632/* Clear an irq's pending state, in preparation for polling on it */
1633void xen_clear_irq_pending(int irq)
1634{
1635 int evtchn = evtchn_from_irq(irq);
1636
1637 if (VALID_EVTCHN(evtchn))
1638 clear_evtchn(evtchn);
1639}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001640EXPORT_SYMBOL(xen_clear_irq_pending);
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -07001641void xen_set_irq_pending(int irq)
1642{
1643 int evtchn = evtchn_from_irq(irq);
1644
1645 if (VALID_EVTCHN(evtchn))
1646 set_evtchn(evtchn);
1647}
1648
1649bool xen_test_irq_pending(int irq)
1650{
1651 int evtchn = evtchn_from_irq(irq);
1652 bool ret = false;
1653
1654 if (VALID_EVTCHN(evtchn))
1655 ret = test_evtchn(evtchn);
1656
1657 return ret;
1658}
1659
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001660/* Poll waiting for an irq to become pending with timeout. In the usual case,
1661 * the irq will be disabled so it won't deliver an interrupt. */
1662void xen_poll_irq_timeout(int irq, u64 timeout)
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001663{
1664 evtchn_port_t evtchn = evtchn_from_irq(irq);
1665
1666 if (VALID_EVTCHN(evtchn)) {
1667 struct sched_poll poll;
1668
1669 poll.nr_ports = 1;
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001670 poll.timeout = timeout;
Isaku Yamahataff3c5362008-10-14 17:50:44 -07001671 set_xen_guest_handle(poll.ports, &evtchn);
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001672
1673 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1674 BUG();
1675 }
1676}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001677EXPORT_SYMBOL(xen_poll_irq_timeout);
1678/* Poll waiting for an irq to become pending. In the usual case, the
1679 * irq will be disabled so it won't deliver an interrupt. */
1680void xen_poll_irq(int irq)
1681{
1682 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1683}
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001684
Konrad Rzeszutek Wilkc7c2c3a2010-11-08 14:26:36 -05001685/* Check whether the IRQ line is shared with other guests. */
1686int xen_test_irq_shared(int irq)
1687{
1688 struct irq_info *info = info_for_irq(irq);
1689 struct physdev_irq_status_query irq_status = { .irq = info->u.pirq.pirq };
1690
1691 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
1692 return 0;
1693 return !(irq_status.flags & XENIRQSTAT_shared);
1694}
1695EXPORT_SYMBOL_GPL(xen_test_irq_shared);
1696
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001697void xen_irq_resume(void)
1698{
Ian Campbell6cb65372011-03-10 16:08:11 +00001699 unsigned int cpu, evtchn;
1700 struct irq_info *info;
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001701
1702 init_evtchn_cpu_bindings();
1703
1704 /* New event-channel space is not 'live' yet. */
1705 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1706 mask_evtchn(evtchn);
1707
1708 /* No IRQ <-> event-channel mappings. */
Ian Campbell6cb65372011-03-10 16:08:11 +00001709 list_for_each_entry(info, &xen_irq_list_head, list)
1710 info->evtchn = 0; /* zap event-channel binding */
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001711
1712 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1713 evtchn_to_irq[evtchn] = -1;
1714
1715 for_each_possible_cpu(cpu) {
1716 restore_cpu_virqs(cpu);
1717 restore_cpu_ipis(cpu);
1718 }
Ian Campbell69035912010-11-01 16:30:09 +00001719
Ian Campbell0a852262011-03-10 16:08:06 +00001720 restore_pirqs();
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001721}
1722
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001723static struct irq_chip xen_dynamic_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001724 .name = "xen-dyn",
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001725
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001726 .irq_disable = disable_dynirq,
1727 .irq_mask = disable_dynirq,
1728 .irq_unmask = enable_dynirq,
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001729
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001730 .irq_ack = ack_dynirq,
1731 .irq_mask_ack = mask_ack_dynirq,
1732
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001733 .irq_set_affinity = set_affinity_irq,
1734 .irq_retrigger = retrigger_dynirq,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001735};
1736
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001737static struct irq_chip xen_pirq_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001738 .name = "xen-pirq",
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001739
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001740 .irq_startup = startup_pirq,
1741 .irq_shutdown = shutdown_pirq,
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001742 .irq_enable = enable_pirq,
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001743 .irq_disable = disable_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001744
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001745 .irq_mask = disable_dynirq,
1746 .irq_unmask = enable_dynirq,
1747
1748 .irq_ack = eoi_pirq,
1749 .irq_eoi = eoi_pirq,
1750 .irq_mask_ack = mask_ack_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001751
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001752 .irq_set_affinity = set_affinity_irq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001753
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001754 .irq_retrigger = retrigger_dynirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001755};
1756
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001757static struct irq_chip xen_percpu_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001758 .name = "xen-percpu",
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001759
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001760 .irq_disable = disable_dynirq,
1761 .irq_mask = disable_dynirq,
1762 .irq_unmask = enable_dynirq,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001763
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001764 .irq_ack = ack_dynirq,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001765};
1766
Sheng Yang38e20b02010-05-14 12:40:51 +01001767int xen_set_callback_via(uint64_t via)
1768{
1769 struct xen_hvm_param a;
1770 a.domid = DOMID_SELF;
1771 a.index = HVM_PARAM_CALLBACK_IRQ;
1772 a.value = via;
1773 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1774}
1775EXPORT_SYMBOL_GPL(xen_set_callback_via);
1776
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001777#ifdef CONFIG_XEN_PVHVM
Sheng Yang38e20b02010-05-14 12:40:51 +01001778/* Vector callbacks are better than PCI interrupts to receive event
1779 * channel notifications because we can receive vector callbacks on any
1780 * vcpu and we don't need PCI support or APIC interactions. */
1781void xen_callback_vector(void)
1782{
1783 int rc;
1784 uint64_t callback_via;
1785 if (xen_have_vector_callback) {
1786 callback_via = HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK);
1787 rc = xen_set_callback_via(callback_via);
1788 if (rc) {
1789 printk(KERN_ERR "Request for Xen HVM callback vector"
1790 " failed.\n");
1791 xen_have_vector_callback = 0;
1792 return;
1793 }
1794 printk(KERN_INFO "Xen HVM callback vector for event delivery is "
1795 "enabled\n");
1796 /* in the restore case the vector has already been allocated */
1797 if (!test_bit(XEN_HVM_EVTCHN_CALLBACK, used_vectors))
1798 alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK, xen_hvm_callback_vector);
1799 }
1800}
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001801#else
1802void xen_callback_vector(void) {}
1803#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001804
Stefano Stabellini2e3d8862012-10-02 15:57:57 +01001805void __init xen_init_IRQ(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001806{
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001807 int i;
Mike Travisc7a35892009-01-10 21:58:11 -08001808
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001809 evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
1810 GFP_KERNEL);
Konrad Rzeszutek Wilk9d093e22011-09-29 13:31:21 -04001811 BUG_ON(!evtchn_to_irq);
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001812 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1813 evtchn_to_irq[i] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001814
1815 init_evtchn_cpu_bindings();
1816
1817 /* No event channels are 'live' right now. */
1818 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1819 mask_evtchn(i);
1820
Stefano Stabellini9846ff12012-01-30 16:21:48 +00001821 pirq_needs_eoi = pirq_needs_eoi_flag;
1822
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001823#ifdef CONFIG_X86
Sheng Yang38e20b02010-05-14 12:40:51 +01001824 if (xen_hvm_domain()) {
1825 xen_callback_vector();
1826 native_init_IRQ();
Stefano Stabellini3942b7402010-06-24 17:50:18 +01001827 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1828 * __acpi_register_gsi can point at the right function */
1829 pci_xen_hvm_init();
Sheng Yang38e20b02010-05-14 12:40:51 +01001830 } else {
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001831 int rc;
Stefano Stabellini9846ff12012-01-30 16:21:48 +00001832 struct physdev_pirq_eoi_gmfn eoi_gmfn;
1833
Sheng Yang38e20b02010-05-14 12:40:51 +01001834 irq_ctx_init(smp_processor_id());
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +01001835 if (xen_initial_domain())
Konrad Rzeszutek Wilka0ee0562011-06-09 09:49:13 -04001836 pci_xen_initial_domain();
Stefano Stabellini9846ff12012-01-30 16:21:48 +00001837
1838 pirq_eoi_map = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
1839 eoi_gmfn.gmfn = virt_to_mfn(pirq_eoi_map);
1840 rc = HYPERVISOR_physdev_op(PHYSDEVOP_pirq_eoi_gmfn_v2, &eoi_gmfn);
1841 if (rc != 0) {
1842 free_page((unsigned long) pirq_eoi_map);
1843 pirq_eoi_map = NULL;
1844 } else
1845 pirq_needs_eoi = pirq_check_eoi_map;
Sheng Yang38e20b02010-05-14 12:40:51 +01001846 }
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001847#endif
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001848}