blob: 002283e01749b5a4f700b0b027619631eb534790 [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
8 * chip. When an event is recieved, it is mapped to an irq and sent
9 * 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
Sheng Yang38e20b02010-05-14 12:40:51 +010034#include <asm/desc.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070035#include <asm/ptrace.h>
36#include <asm/irq.h>
Jeremy Fitzhardinge792dc4f2009-02-06 14:09:43 -080037#include <asm/idle.h>
Konrad Rzeszutek Wilk0794bfc2010-10-18 10:41:08 -040038#include <asm/io_apic.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070039#include <asm/sync_bitops.h>
Stefano Stabellini42a1de52010-06-24 16:42:04 +010040#include <asm/xen/pci.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070041#include <asm/xen/hypercall.h>
Adrian Bunk8d1b8752007-07-20 00:31:44 -070042#include <asm/xen/hypervisor.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070043
Sheng Yang38e20b02010-05-14 12:40:51 +010044#include <xen/xen.h>
45#include <xen/hvm.h>
Isaku Yamahatae04d0d02008-04-02 10:53:55 -070046#include <xen/xen-ops.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070047#include <xen/events.h>
48#include <xen/interface/xen.h>
49#include <xen/interface/event_channel.h>
Sheng Yang38e20b02010-05-14 12:40:51 +010050#include <xen/interface/hvm/hvm_op.h>
51#include <xen/interface/hvm/params.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070052
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070053/*
54 * This lock protects updates to the following mapping and reference-count
55 * arrays. The lock does not need to be acquired to read the mapping tables.
56 */
57static DEFINE_SPINLOCK(irq_mapping_update_lock);
58
Ian Campbell6cb65372011-03-10 16:08:11 +000059static LIST_HEAD(xen_irq_list_head);
60
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070061/* IRQ <-> VIRQ mapping. */
Tejun Heo204fba42009-06-24 15:13:45 +090062static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070063
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070064/* IRQ <-> IPI mapping */
Tejun Heo204fba42009-06-24 15:13:45 +090065static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070066
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080067/* Interrupt types. */
68enum xen_irq_type {
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -080069 IRQT_UNBOUND = 0,
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070070 IRQT_PIRQ,
71 IRQT_VIRQ,
72 IRQT_IPI,
73 IRQT_EVTCHN
74};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070075
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080076/*
77 * Packed IRQ information:
78 * type - enum xen_irq_type
79 * event channel - irq->event channel mapping
80 * cpu - cpu this event channel is bound to
81 * index - type-specific information:
Stefano Stabellini42a1de52010-06-24 16:42:04 +010082 * PIRQ - vector, with MSB being "needs EIO", or physical IRQ of the HVM
83 * guest, or GSI (real passthrough IRQ) of the device.
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080084 * VIRQ - virq number
85 * IPI - IPI vector
86 * EVTCHN -
87 */
88struct irq_info
89{
Ian Campbell6cb65372011-03-10 16:08:11 +000090 struct list_head list;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080091 enum xen_irq_type type; /* type */
Ian Campbell6cb65372011-03-10 16:08:11 +000092 unsigned irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080093 unsigned short evtchn; /* event channel */
94 unsigned short cpu; /* cpu bound */
95
96 union {
97 unsigned short virq;
98 enum ipi_vector ipi;
99 struct {
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100100 unsigned short pirq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800101 unsigned short gsi;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400102 unsigned char vector;
103 unsigned char flags;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800104 } pirq;
105 } u;
106};
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400107#define PIRQ_NEEDS_EOI (1 << 0)
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400108#define PIRQ_SHAREABLE (1 << 1)
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800109
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100110static int *pirq_to_irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700111
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -0400112static int *evtchn_to_irq;
Jeremy Fitzhardinge3b32f572009-08-13 12:50:37 -0700113
Ian Campbellcb60d112011-03-10 16:08:08 +0000114static DEFINE_PER_CPU(unsigned long [NR_EVENT_CHANNELS/BITS_PER_LONG],
115 cpu_evtchn_mask);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700116
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700117/* Xen will never allocate port zero for any purpose. */
118#define VALID_EVTCHN(chn) ((chn) != 0)
119
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700120static struct irq_chip xen_dynamic_chip;
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700121static struct irq_chip xen_percpu_chip;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400122static struct irq_chip xen_pirq_chip;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700123
Ian Campbell9158c352011-03-10 16:08:09 +0000124/* Get info for IRQ */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800125static struct irq_info *info_for_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700126{
Ian Campbellca62ce82011-03-10 16:08:12 +0000127 return get_irq_data(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700128}
129
Ian Campbell9158c352011-03-10 16:08:09 +0000130/* Constructors for packed IRQ information. */
131static void xen_irq_info_common_init(struct irq_info *info,
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000132 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000133 enum xen_irq_type type,
134 unsigned short evtchn,
135 unsigned short cpu)
136{
137
138 BUG_ON(info->type != IRQT_UNBOUND && info->type != type);
139
140 info->type = type;
Ian Campbell6cb65372011-03-10 16:08:11 +0000141 info->irq = irq;
Ian Campbell9158c352011-03-10 16:08:09 +0000142 info->evtchn = evtchn;
143 info->cpu = cpu;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000144
145 evtchn_to_irq[evtchn] = irq;
Ian Campbell9158c352011-03-10 16:08:09 +0000146}
147
148static void xen_irq_info_evtchn_init(unsigned irq,
149 unsigned short evtchn)
150{
151 struct irq_info *info = info_for_irq(irq);
152
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000153 xen_irq_info_common_init(info, irq, IRQT_EVTCHN, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000154}
155
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000156static void xen_irq_info_ipi_init(unsigned cpu,
157 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000158 unsigned short evtchn,
159 enum ipi_vector ipi)
160{
161 struct irq_info *info = info_for_irq(irq);
162
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000163 xen_irq_info_common_init(info, irq, IRQT_IPI, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000164
165 info->u.ipi = ipi;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000166
167 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
Ian Campbell9158c352011-03-10 16:08:09 +0000168}
169
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000170static void xen_irq_info_virq_init(unsigned cpu,
171 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000172 unsigned short evtchn,
173 unsigned short virq)
174{
175 struct irq_info *info = info_for_irq(irq);
176
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000177 xen_irq_info_common_init(info, irq, IRQT_VIRQ, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000178
179 info->u.virq = virq;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000180
181 per_cpu(virq_to_irq, cpu)[virq] = irq;
Ian Campbell9158c352011-03-10 16:08:09 +0000182}
183
184static void xen_irq_info_pirq_init(unsigned irq,
185 unsigned short evtchn,
186 unsigned short pirq,
187 unsigned short gsi,
188 unsigned short vector,
189 unsigned char flags)
190{
191 struct irq_info *info = info_for_irq(irq);
192
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000193 xen_irq_info_common_init(info, irq, IRQT_PIRQ, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000194
195 info->u.pirq.pirq = pirq;
196 info->u.pirq.gsi = gsi;
197 info->u.pirq.vector = vector;
198 info->u.pirq.flags = flags;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000199
200 pirq_to_irq[pirq] = irq;
Ian Campbell9158c352011-03-10 16:08:09 +0000201}
202
203/*
204 * Accessors for packed IRQ information.
205 */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800206static unsigned int evtchn_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700207{
Joe Jin110e7c72011-01-07 14:50:12 +0800208 if (unlikely(WARN(irq < 0 || irq >= nr_irqs, "Invalid irq %d!\n", irq)))
209 return 0;
210
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800211 return info_for_irq(irq)->evtchn;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700212}
213
Ian Campbelld4c04532009-02-06 19:20:31 -0800214unsigned irq_from_evtchn(unsigned int evtchn)
215{
216 return evtchn_to_irq[evtchn];
217}
218EXPORT_SYMBOL_GPL(irq_from_evtchn);
219
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800220static enum ipi_vector ipi_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700221{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800222 struct irq_info *info = info_for_irq(irq);
223
224 BUG_ON(info == NULL);
225 BUG_ON(info->type != IRQT_IPI);
226
227 return info->u.ipi;
228}
229
230static unsigned virq_from_irq(unsigned irq)
231{
232 struct irq_info *info = info_for_irq(irq);
233
234 BUG_ON(info == NULL);
235 BUG_ON(info->type != IRQT_VIRQ);
236
237 return info->u.virq;
238}
239
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100240static unsigned pirq_from_irq(unsigned irq)
241{
242 struct irq_info *info = info_for_irq(irq);
243
244 BUG_ON(info == NULL);
245 BUG_ON(info->type != IRQT_PIRQ);
246
247 return info->u.pirq.pirq;
248}
249
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800250static unsigned gsi_from_irq(unsigned irq)
251{
252 struct irq_info *info = info_for_irq(irq);
253
254 BUG_ON(info == NULL);
255 BUG_ON(info->type != IRQT_PIRQ);
256
257 return info->u.pirq.gsi;
258}
259
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800260static enum xen_irq_type type_from_irq(unsigned irq)
261{
262 return info_for_irq(irq)->type;
263}
264
265static unsigned cpu_from_irq(unsigned irq)
266{
267 return info_for_irq(irq)->cpu;
268}
269
270static unsigned int cpu_from_evtchn(unsigned int evtchn)
271{
272 int irq = evtchn_to_irq[evtchn];
273 unsigned ret = 0;
274
275 if (irq != -1)
276 ret = cpu_from_irq(irq);
277
278 return ret;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700279}
280
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400281static bool pirq_needs_eoi(unsigned irq)
282{
283 struct irq_info *info = info_for_irq(irq);
284
285 BUG_ON(info->type != IRQT_PIRQ);
286
287 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
288}
289
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700290static inline unsigned long active_evtchns(unsigned int cpu,
291 struct shared_info *sh,
292 unsigned int idx)
293{
294 return (sh->evtchn_pending[idx] &
Ian Campbellcb60d112011-03-10 16:08:08 +0000295 per_cpu(cpu_evtchn_mask, cpu)[idx] &
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700296 ~sh->evtchn_mask[idx]);
297}
298
299static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
300{
301 int irq = evtchn_to_irq[chn];
302
303 BUG_ON(irq == -1);
304#ifdef CONFIG_SMP
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000305 cpumask_copy(irq_to_desc(irq)->irq_data.affinity, cpumask_of(cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700306#endif
307
Ian Campbellcb60d112011-03-10 16:08:08 +0000308 clear_bit(chn, per_cpu(cpu_evtchn_mask, cpu_from_irq(irq)));
309 set_bit(chn, per_cpu(cpu_evtchn_mask, cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700310
Ian Campbellca62ce82011-03-10 16:08:12 +0000311 info_for_irq(irq)->cpu = cpu;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700312}
313
314static void init_evtchn_cpu_bindings(void)
315{
Jan Beulich1c6969e2010-11-16 14:55:33 -0800316 int i;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700317#ifdef CONFIG_SMP
Ian Campbell6cb65372011-03-10 16:08:11 +0000318 struct irq_info *info;
Thomas Gleixner10e58082008-10-16 14:19:04 +0200319
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700320 /* By default all event channels notify CPU#0. */
Ian Campbell6cb65372011-03-10 16:08:11 +0000321 list_for_each_entry(info, &xen_irq_list_head, list) {
322 struct irq_desc *desc = irq_to_desc(info->irq);
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000323 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800324 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700325#endif
326
Jan Beulich1c6969e2010-11-16 14:55:33 -0800327 for_each_possible_cpu(i)
Ian Campbellcb60d112011-03-10 16:08:08 +0000328 memset(per_cpu(cpu_evtchn_mask, i),
329 (i == 0) ? ~0 : 0, sizeof(*per_cpu(cpu_evtchn_mask, i)));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700330}
331
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700332static inline void clear_evtchn(int port)
333{
334 struct shared_info *s = HYPERVISOR_shared_info;
335 sync_clear_bit(port, &s->evtchn_pending[0]);
336}
337
338static inline void set_evtchn(int port)
339{
340 struct shared_info *s = HYPERVISOR_shared_info;
341 sync_set_bit(port, &s->evtchn_pending[0]);
342}
343
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700344static inline int test_evtchn(int port)
345{
346 struct shared_info *s = HYPERVISOR_shared_info;
347 return sync_test_bit(port, &s->evtchn_pending[0]);
348}
349
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700350
351/**
352 * notify_remote_via_irq - send event to remote end of event channel via irq
353 * @irq: irq of event channel to send event to
354 *
355 * Unlike notify_remote_via_evtchn(), this is safe to use across
356 * save/restore. Notifications on a broken connection are silently
357 * dropped.
358 */
359void notify_remote_via_irq(int irq)
360{
361 int evtchn = evtchn_from_irq(irq);
362
363 if (VALID_EVTCHN(evtchn))
364 notify_remote_via_evtchn(evtchn);
365}
366EXPORT_SYMBOL_GPL(notify_remote_via_irq);
367
368static void mask_evtchn(int port)
369{
370 struct shared_info *s = HYPERVISOR_shared_info;
371 sync_set_bit(port, &s->evtchn_mask[0]);
372}
373
374static void unmask_evtchn(int port)
375{
376 struct shared_info *s = HYPERVISOR_shared_info;
377 unsigned int cpu = get_cpu();
378
379 BUG_ON(!irqs_disabled());
380
381 /* Slow path (hypercall) if this is a non-local port. */
382 if (unlikely(cpu != cpu_from_evtchn(port))) {
383 struct evtchn_unmask unmask = { .port = port };
384 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
385 } else {
Christoph Lameter780f36d2010-12-06 11:16:29 -0600386 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700387
388 sync_clear_bit(port, &s->evtchn_mask[0]);
389
390 /*
391 * The following is basically the equivalent of
392 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
393 * the interrupt edge' if the channel is masked.
394 */
395 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
396 !sync_test_and_set_bit(port / BITS_PER_LONG,
397 &vcpu_info->evtchn_pending_sel))
398 vcpu_info->evtchn_upcall_pending = 1;
399 }
400
401 put_cpu();
402}
403
Ian Campbell6cb65372011-03-10 16:08:11 +0000404static void xen_irq_init(unsigned irq)
405{
406 struct irq_info *info;
407 struct irq_desc *desc = irq_to_desc(irq);
408
409 /* By default all event channels notify CPU#0. */
410 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
411
Ian Campbellca62ce82011-03-10 16:08:12 +0000412 info = kzalloc(sizeof(*info), GFP_KERNEL);
413 if (info == NULL)
414 panic("Unable to allocate metadata for IRQ%d\n", irq);
Ian Campbell6cb65372011-03-10 16:08:11 +0000415
416 info->type = IRQT_UNBOUND;
417
Ian Campbellca62ce82011-03-10 16:08:12 +0000418 set_irq_data(irq, info);
419
Ian Campbell6cb65372011-03-10 16:08:11 +0000420 list_add_tail(&info->list, &xen_irq_list_head);
421}
422
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000423static int xen_allocate_irq_dynamic(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700424{
Ian Campbell89911502011-03-03 11:57:44 -0500425 int first = 0;
426 int irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700427
Ian Campbell89911502011-03-03 11:57:44 -0500428#ifdef CONFIG_X86_IO_APIC
429 /*
430 * For an HVM guest or domain 0 which see "real" (emulated or
431 * actual repectively) GSIs we allocate dynamic IRQs
432 * e.g. those corresponding to event channels or MSIs
433 * etc. from the range above those "real" GSIs to avoid
434 * collisions.
Konrad Rzeszutek Wilkd1b758e2010-12-09 14:53:29 -0500435 */
Ian Campbell89911502011-03-03 11:57:44 -0500436 if (xen_initial_domain() || xen_hvm_domain())
437 first = get_nr_irqs_gsi();
438#endif
439
440retry:
441 irq = irq_alloc_desc_from(first, -1);
442
443 if (irq == -ENOMEM && first > NR_IRQS_LEGACY) {
444 printk(KERN_ERR "Out of dynamic IRQ space and eating into GSI space. You should increase nr_irqs\n");
445 first = max(NR_IRQS_LEGACY, first - NR_IRQS_LEGACY);
446 goto retry;
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100447 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700448
Ian Campbell89911502011-03-03 11:57:44 -0500449 if (irq < 0)
450 panic("No available IRQ to bind to: increase nr_irqs!\n");
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800451
Ian Campbell6cb65372011-03-10 16:08:11 +0000452 xen_irq_init(irq);
453
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700454 return irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400455}
456
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000457static int xen_allocate_irq_gsi(unsigned gsi)
458{
459 int irq;
460
Ian Campbell89911502011-03-03 11:57:44 -0500461 /*
462 * A PV guest has no concept of a GSI (since it has no ACPI
463 * nor access to/knowledge of the physical APICs). Therefore
464 * all IRQs are dynamically allocated from the entire IRQ
465 * space.
466 */
467 if (xen_pv_domain() && !xen_initial_domain())
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000468 return xen_allocate_irq_dynamic();
469
470 /* Legacy IRQ descriptors are already allocated by the arch. */
471 if (gsi < NR_IRQS_LEGACY)
Ian Campbell6cb65372011-03-10 16:08:11 +0000472 irq = gsi;
473 else
474 irq = irq_alloc_desc_at(gsi, -1);
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000475
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000476 if (irq < 0)
477 panic("Unable to allocate to IRQ%d (%d)\n", gsi, irq);
478
Ian Campbell6cb65372011-03-10 16:08:11 +0000479 xen_irq_init(irq);
480
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000481 return irq;
482}
483
484static void xen_free_irq(unsigned irq)
485{
Ian Campbellca62ce82011-03-10 16:08:12 +0000486 struct irq_info *info = get_irq_data(irq);
Ian Campbell6cb65372011-03-10 16:08:11 +0000487
488 list_del(&info->list);
Ian Campbell9158c352011-03-10 16:08:09 +0000489
Ian Campbellca62ce82011-03-10 16:08:12 +0000490 set_irq_data(irq, NULL);
491
492 kfree(info);
493
Ian Campbell72146102011-02-03 09:49:35 +0000494 /* Legacy IRQ descriptors are managed by the arch. */
495 if (irq < NR_IRQS_LEGACY)
496 return;
497
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000498 irq_free_desc(irq);
499}
500
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400501static void pirq_unmask_notify(int irq)
502{
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100503 struct physdev_eoi eoi = { .irq = pirq_from_irq(irq) };
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400504
505 if (unlikely(pirq_needs_eoi(irq))) {
506 int rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
507 WARN_ON(rc);
508 }
509}
510
511static void pirq_query_unmask(int irq)
512{
513 struct physdev_irq_status_query irq_status;
514 struct irq_info *info = info_for_irq(irq);
515
516 BUG_ON(info->type != IRQT_PIRQ);
517
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100518 irq_status.irq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400519 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
520 irq_status.flags = 0;
521
522 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
523 if (irq_status.flags & XENIRQSTAT_needs_eoi)
524 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
525}
526
527static bool probing_irq(int irq)
528{
529 struct irq_desc *desc = irq_to_desc(irq);
530
531 return desc && desc->action == NULL;
532}
533
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000534static unsigned int __startup_pirq(unsigned int irq)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400535{
536 struct evtchn_bind_pirq bind_pirq;
537 struct irq_info *info = info_for_irq(irq);
538 int evtchn = evtchn_from_irq(irq);
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400539 int rc;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400540
541 BUG_ON(info->type != IRQT_PIRQ);
542
543 if (VALID_EVTCHN(evtchn))
544 goto out;
545
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100546 bind_pirq.pirq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400547 /* NB. We are happy to share unless we are probing. */
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400548 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
549 BIND_PIRQ__WILL_SHARE : 0;
550 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
551 if (rc != 0) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400552 if (!probing_irq(irq))
553 printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
554 irq);
555 return 0;
556 }
557 evtchn = bind_pirq.port;
558
559 pirq_query_unmask(irq);
560
561 evtchn_to_irq[evtchn] = irq;
562 bind_evtchn_to_cpu(evtchn, 0);
563 info->evtchn = evtchn;
564
565out:
566 unmask_evtchn(evtchn);
567 pirq_unmask_notify(irq);
568
569 return 0;
570}
571
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000572static unsigned int startup_pirq(struct irq_data *data)
573{
574 return __startup_pirq(data->irq);
575}
576
577static void shutdown_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400578{
579 struct evtchn_close close;
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000580 unsigned int irq = data->irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400581 struct irq_info *info = info_for_irq(irq);
582 int evtchn = evtchn_from_irq(irq);
583
584 BUG_ON(info->type != IRQT_PIRQ);
585
586 if (!VALID_EVTCHN(evtchn))
587 return;
588
589 mask_evtchn(evtchn);
590
591 close.port = evtchn;
592 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
593 BUG();
594
595 bind_evtchn_to_cpu(evtchn, 0);
596 evtchn_to_irq[evtchn] = -1;
597 info->evtchn = 0;
598}
599
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000600static void enable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400601{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000602 startup_pirq(data);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400603}
604
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000605static void disable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400606{
607}
608
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000609static void ack_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400610{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000611 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400612
Ian Campbellaa673c12011-02-07 11:08:39 +0000613 move_native_irq(data->irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400614
615 if (VALID_EVTCHN(evtchn)) {
616 mask_evtchn(evtchn);
617 clear_evtchn(evtchn);
618 }
619}
620
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400621static int find_irq_by_gsi(unsigned gsi)
622{
Ian Campbell6cb65372011-03-10 16:08:11 +0000623 struct irq_info *info;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400624
Ian Campbell6cb65372011-03-10 16:08:11 +0000625 list_for_each_entry(info, &xen_irq_list_head, list) {
626 if (info->type != IRQT_PIRQ)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400627 continue;
628
Ian Campbell6cb65372011-03-10 16:08:11 +0000629 if (info->u.pirq.gsi == gsi)
630 return info->irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400631 }
632
633 return -1;
634}
635
Ian Campbellf4d06352011-03-10 16:08:07 +0000636int xen_allocate_pirq_gsi(unsigned gsi)
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100637{
Ian Campbellf4d06352011-03-10 16:08:07 +0000638 return gsi;
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100639}
640
Ian Campbell653378a2011-03-10 16:08:04 +0000641/*
642 * Do not make any assumptions regarding the relationship between the
643 * IRQ number returned here and the Xen pirq argument.
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100644 *
645 * Note: We don't assign an event channel until the irq actually started
646 * up. Return an existing irq if we've already got one for the gsi.
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400647 */
Ian Campbellf4d06352011-03-10 16:08:07 +0000648int xen_bind_pirq_gsi_to_irq(unsigned gsi,
649 unsigned pirq, int shareable, char *name)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400650{
Ian Campbella0e18112011-03-10 16:08:03 +0000651 int irq = -1;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400652 struct physdev_irq irq_op;
653
654 spin_lock(&irq_mapping_update_lock);
655
Ian Campbellca62ce82011-03-10 16:08:12 +0000656 if (pirq > nr_irqs) {
657 printk(KERN_WARNING "xen_map_pirq_gsi: pirq %d > nr_irqs %d!\n",
658 pirq, nr_irqs);
Stefano Stabellini01557ba2010-08-20 14:46:52 +0100659 goto out;
660 }
661
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400662 irq = find_irq_by_gsi(gsi);
663 if (irq != -1) {
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100664 printk(KERN_INFO "xen_map_pirq_gsi: returning irq %d for gsi %u\n",
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400665 irq, gsi);
666 goto out; /* XXX need refcount? */
667 }
668
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000669 irq = xen_allocate_irq_gsi(gsi);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400670
671 set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
Gerd Hoffmann1a60d052010-10-04 13:42:27 -0400672 handle_level_irq, name);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400673
674 irq_op.irq = irq;
Alex Nixonb5401a92010-03-18 16:31:34 -0400675 irq_op.vector = 0;
676
677 /* Only the privileged domain can do this. For non-priv, the pcifront
678 * driver provides a PCI bus that does the call to do exactly
679 * this in the priv domain. */
680 if (xen_initial_domain() &&
681 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000682 xen_free_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400683 irq = -ENOSPC;
684 goto out;
685 }
686
Ian Campbell9158c352011-03-10 16:08:09 +0000687 xen_irq_info_pirq_init(irq, 0, pirq, gsi, irq_op.vector,
688 shareable ? PIRQ_SHAREABLE : 0);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400689
690out:
691 spin_unlock(&irq_mapping_update_lock);
692
693 return irq;
694}
695
Qing Hef731e3ef2010-10-11 15:30:09 +0100696#ifdef CONFIG_PCI_MSI
Ian Campbellbf480d92011-02-18 16:43:32 +0000697int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000698{
Ian Campbell5cad61a2011-02-18 16:43:31 +0000699 int rc;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000700 struct physdev_get_free_pirq op_get_free_pirq;
Ian Campbell5cad61a2011-02-18 16:43:31 +0000701
Ian Campbellbf480d92011-02-18 16:43:32 +0000702 op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000703 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000704
Ian Campbell5cad61a2011-02-18 16:43:31 +0000705 WARN_ONCE(rc == -ENOSYS,
706 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
707
708 return rc ? -1 : op_get_free_pirq.pirq;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000709}
710
Ian Campbellbf480d92011-02-18 16:43:32 +0000711int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
Ian Campbellca1d8fe2011-02-18 16:43:36 +0000712 int pirq, int vector, const char *name)
Stefano Stabellini809f9262010-07-01 17:10:39 +0100713{
Ian Campbellbf480d92011-02-18 16:43:32 +0000714 int irq, ret;
Ian Campbell4b41df72011-02-18 16:43:29 +0000715
Stefano Stabellini809f9262010-07-01 17:10:39 +0100716 spin_lock(&irq_mapping_update_lock);
717
Ian Campbell4b41df72011-02-18 16:43:29 +0000718 irq = xen_allocate_irq_dynamic();
719 if (irq == -1)
Ian Campbellbb5d0792011-02-18 16:43:28 +0000720 goto out;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100721
Ian Campbell4b41df72011-02-18 16:43:29 +0000722 set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
Stefano Stabellini809f9262010-07-01 17:10:39 +0100723 handle_level_irq, name);
724
Ian Campbell9158c352011-03-10 16:08:09 +0000725 xen_irq_info_pirq_init(irq, 0, pirq, 0, vector, 0);
Ian Campbellbf480d92011-02-18 16:43:32 +0000726 ret = set_irq_msi(irq, msidesc);
727 if (ret < 0)
728 goto error_irq;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100729out:
730 spin_unlock(&irq_mapping_update_lock);
Ian Campbell4b41df72011-02-18 16:43:29 +0000731 return irq;
Ian Campbellbf480d92011-02-18 16:43:32 +0000732error_irq:
733 spin_unlock(&irq_mapping_update_lock);
734 xen_free_irq(irq);
735 return -1;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100736}
Qing Hef731e3ef2010-10-11 15:30:09 +0100737#endif
738
Alex Nixonb5401a92010-03-18 16:31:34 -0400739int xen_destroy_irq(int irq)
740{
741 struct irq_desc *desc;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100742 struct physdev_unmap_pirq unmap_irq;
743 struct irq_info *info = info_for_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400744 int rc = -ENOENT;
745
746 spin_lock(&irq_mapping_update_lock);
747
748 desc = irq_to_desc(irq);
749 if (!desc)
750 goto out;
751
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100752 if (xen_initial_domain()) {
Konrad Rzeszutek Wilk12334712010-11-19 11:27:09 -0500753 unmap_irq.pirq = info->u.pirq.pirq;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100754 unmap_irq.domid = DOMID_SELF;
755 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
756 if (rc) {
757 printk(KERN_WARNING "unmap irq failed %d\n", rc);
758 goto out;
759 }
760 }
Konrad Rzeszutek Wilk1aa0b512011-02-17 11:23:58 -0500761 pirq_to_irq[info->u.pirq.pirq] = -1;
762
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000763 xen_free_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400764
765out:
766 spin_unlock(&irq_mapping_update_lock);
767 return rc;
768}
769
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000770int xen_irq_from_pirq(unsigned pirq)
771{
772 return pirq_to_irq[pirq];
773}
774
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700775int bind_evtchn_to_irq(unsigned int evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700776{
777 int irq;
778
779 spin_lock(&irq_mapping_update_lock);
780
781 irq = evtchn_to_irq[evtchn];
782
783 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000784 irq = xen_allocate_irq_dynamic();
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700785
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700786 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -0700787 handle_fasteoi_irq, "event");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700788
Ian Campbell9158c352011-03-10 16:08:09 +0000789 xen_irq_info_evtchn_init(irq, evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700790 }
791
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700792 spin_unlock(&irq_mapping_update_lock);
793
794 return irq;
795}
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700796EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700797
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700798static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
799{
800 struct evtchn_bind_ipi bind_ipi;
801 int evtchn, irq;
802
803 spin_lock(&irq_mapping_update_lock);
804
805 irq = per_cpu(ipi_to_irq, cpu)[ipi];
Ian Campbell90af9512009-02-06 16:55:58 -0800806
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700807 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000808 irq = xen_allocate_irq_dynamic();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700809 if (irq < 0)
810 goto out;
811
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700812 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
813 handle_percpu_irq, "ipi");
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700814
815 bind_ipi.vcpu = cpu;
816 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
817 &bind_ipi) != 0)
818 BUG();
819 evtchn = bind_ipi.port;
820
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000821 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700822
823 bind_evtchn_to_cpu(evtchn, cpu);
824 }
825
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700826 out:
827 spin_unlock(&irq_mapping_update_lock);
828 return irq;
829}
830
831
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100832int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700833{
834 struct evtchn_bind_virq bind_virq;
835 int evtchn, irq;
836
837 spin_lock(&irq_mapping_update_lock);
838
839 irq = per_cpu(virq_to_irq, cpu)[virq];
840
841 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000842 irq = xen_allocate_irq_dynamic();
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700843
844 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
845 handle_percpu_irq, "virq");
846
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700847 bind_virq.virq = virq;
848 bind_virq.vcpu = cpu;
849 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
850 &bind_virq) != 0)
851 BUG();
852 evtchn = bind_virq.port;
853
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000854 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700855
856 bind_evtchn_to_cpu(evtchn, cpu);
857 }
858
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700859 spin_unlock(&irq_mapping_update_lock);
860
861 return irq;
862}
863
864static void unbind_from_irq(unsigned int irq)
865{
866 struct evtchn_close close;
867 int evtchn = evtchn_from_irq(irq);
868
869 spin_lock(&irq_mapping_update_lock);
870
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800871 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700872 close.port = evtchn;
873 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
874 BUG();
875
876 switch (type_from_irq(irq)) {
877 case IRQT_VIRQ:
878 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800879 [virq_from_irq(irq)] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700880 break;
Alex Nixond68d82a2008-08-22 11:52:15 +0100881 case IRQT_IPI:
882 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800883 [ipi_from_irq(irq)] = -1;
Alex Nixond68d82a2008-08-22 11:52:15 +0100884 break;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700885 default:
886 break;
887 }
888
889 /* Closed ports are implicitly re-bound to VCPU0. */
890 bind_evtchn_to_cpu(evtchn, 0);
891
892 evtchn_to_irq[evtchn] = -1;
Ian Campbellfed5ea82009-12-01 16:15:30 +0000893 }
894
Ian Campbellca62ce82011-03-10 16:08:12 +0000895 BUG_ON(info_for_irq(irq)->type == IRQT_UNBOUND);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700896
Ian Campbell9158c352011-03-10 16:08:09 +0000897 xen_free_irq(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700898
899 spin_unlock(&irq_mapping_update_lock);
900}
901
902int bind_evtchn_to_irqhandler(unsigned int evtchn,
Jeff Garzik7c239972007-10-19 03:12:20 -0400903 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700904 unsigned long irqflags,
905 const char *devname, void *dev_id)
906{
907 unsigned int irq;
908 int retval;
909
910 irq = bind_evtchn_to_irq(evtchn);
911 retval = request_irq(irq, handler, irqflags, devname, dev_id);
912 if (retval != 0) {
913 unbind_from_irq(irq);
914 return retval;
915 }
916
917 return irq;
918}
919EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
920
921int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
Jeff Garzik7c239972007-10-19 03:12:20 -0400922 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700923 unsigned long irqflags, const char *devname, void *dev_id)
924{
925 unsigned int irq;
926 int retval;
927
928 irq = bind_virq_to_irq(virq, cpu);
929 retval = request_irq(irq, handler, irqflags, devname, dev_id);
930 if (retval != 0) {
931 unbind_from_irq(irq);
932 return retval;
933 }
934
935 return irq;
936}
937EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
938
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700939int bind_ipi_to_irqhandler(enum ipi_vector ipi,
940 unsigned int cpu,
941 irq_handler_t handler,
942 unsigned long irqflags,
943 const char *devname,
944 void *dev_id)
945{
946 int irq, retval;
947
948 irq = bind_ipi_to_irq(ipi, cpu);
949 if (irq < 0)
950 return irq;
951
Thomas Gleixner676dc3c2011-02-05 20:08:59 +0000952 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700953 retval = request_irq(irq, handler, irqflags, devname, dev_id);
954 if (retval != 0) {
955 unbind_from_irq(irq);
956 return retval;
957 }
958
959 return irq;
960}
961
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700962void unbind_from_irqhandler(unsigned int irq, void *dev_id)
963{
964 free_irq(irq, dev_id);
965 unbind_from_irq(irq);
966}
967EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
968
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700969void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
970{
971 int irq = per_cpu(ipi_to_irq, cpu)[vector];
972 BUG_ON(irq < 0);
973 notify_remote_via_irq(irq);
974}
975
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700976irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
977{
978 struct shared_info *sh = HYPERVISOR_shared_info;
979 int cpu = smp_processor_id();
Ian Campbellcb60d112011-03-10 16:08:08 +0000980 unsigned long *cpu_evtchn = per_cpu(cpu_evtchn_mask, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700981 int i;
982 unsigned long flags;
983 static DEFINE_SPINLOCK(debug_lock);
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100984 struct vcpu_info *v;
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700985
986 spin_lock_irqsave(&debug_lock, flags);
987
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100988 printk("\nvcpu %d\n ", cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700989
990 for_each_online_cpu(i) {
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100991 int pending;
992 v = per_cpu(xen_vcpu, i);
993 pending = (get_irq_regs() && i == cpu)
994 ? xen_irqs_disabled(get_irq_regs())
995 : v->evtchn_upcall_mask;
996 printk("%d: masked=%d pending=%d event_sel %0*lx\n ", i,
997 pending, v->evtchn_upcall_pending,
998 (int)(sizeof(v->evtchn_pending_sel)*2),
999 v->evtchn_pending_sel);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001000 }
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001001 v = per_cpu(xen_vcpu, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001002
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001003 printk("\npending:\n ");
1004 for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
1005 printk("%0*lx%s", (int)sizeof(sh->evtchn_pending[0])*2,
1006 sh->evtchn_pending[i],
1007 i % 8 == 0 ? "\n " : " ");
1008 printk("\nglobal mask:\n ");
1009 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1010 printk("%0*lx%s",
1011 (int)(sizeof(sh->evtchn_mask[0])*2),
1012 sh->evtchn_mask[i],
1013 i % 8 == 0 ? "\n " : " ");
1014
1015 printk("\nglobally unmasked:\n ");
1016 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1017 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
1018 sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
1019 i % 8 == 0 ? "\n " : " ");
1020
1021 printk("\nlocal cpu%d mask:\n ", cpu);
1022 for (i = (NR_EVENT_CHANNELS/BITS_PER_LONG)-1; i >= 0; i--)
1023 printk("%0*lx%s", (int)(sizeof(cpu_evtchn[0])*2),
1024 cpu_evtchn[i],
1025 i % 8 == 0 ? "\n " : " ");
1026
1027 printk("\nlocally unmasked:\n ");
1028 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) {
1029 unsigned long pending = sh->evtchn_pending[i]
1030 & ~sh->evtchn_mask[i]
1031 & cpu_evtchn[i];
1032 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
1033 pending, i % 8 == 0 ? "\n " : " ");
1034 }
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001035
1036 printk("\npending list:\n");
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001037 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001038 if (sync_test_bit(i, sh->evtchn_pending)) {
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001039 int word_idx = i / BITS_PER_LONG;
1040 printk(" %d: event %d -> irq %d%s%s%s\n",
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001041 cpu_from_evtchn(i), i,
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001042 evtchn_to_irq[i],
1043 sync_test_bit(word_idx, &v->evtchn_pending_sel)
1044 ? "" : " l2-clear",
1045 !sync_test_bit(i, sh->evtchn_mask)
1046 ? "" : " globally-masked",
1047 sync_test_bit(i, cpu_evtchn)
1048 ? "" : " locally-masked");
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001049 }
1050 }
1051
1052 spin_unlock_irqrestore(&debug_lock, flags);
1053
1054 return IRQ_HANDLED;
1055}
1056
Tejun Heo245b2e72009-06-24 15:13:48 +09001057static DEFINE_PER_CPU(unsigned, xed_nesting_count);
1058
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001059/*
1060 * Search the CPUs pending events bitmasks. For each one found, map
1061 * the event number to an irq, and feed it into do_IRQ() for
1062 * handling.
1063 *
1064 * Xen uses a two-level bitmap to speed searching. The first level is
1065 * a bitset of words which contain pending event bits. The second
1066 * level is a bitset of pending events themselves.
1067 */
Sheng Yang38e20b02010-05-14 12:40:51 +01001068static void __xen_evtchn_do_upcall(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001069{
1070 int cpu = get_cpu();
1071 struct shared_info *s = HYPERVISOR_shared_info;
Christoph Lameter780f36d2010-12-06 11:16:29 -06001072 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001073 unsigned count;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001074
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001075 do {
1076 unsigned long pending_words;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001077
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001078 vcpu_info->evtchn_upcall_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001079
Christoph Lameterb2e4ae62010-12-06 11:40:07 -06001080 if (__this_cpu_inc_return(xed_nesting_count) - 1)
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001081 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001082
Isaku Yamahatae849c3e2008-04-02 10:53:56 -07001083#ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
1084 /* Clear master flag /before/ clearing selector flag. */
Isaku Yamahata6673cf62008-06-16 14:58:13 -07001085 wmb();
Isaku Yamahatae849c3e2008-04-02 10:53:56 -07001086#endif
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001087 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
1088 while (pending_words != 0) {
1089 unsigned long pending_bits;
1090 int word_idx = __ffs(pending_words);
1091 pending_words &= ~(1UL << word_idx);
1092
1093 while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
1094 int bit_idx = __ffs(pending_bits);
1095 int port = (word_idx * BITS_PER_LONG) + bit_idx;
1096 int irq = evtchn_to_irq[port];
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -08001097 struct irq_desc *desc;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001098
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -07001099 mask_evtchn(port);
1100 clear_evtchn(port);
1101
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -08001102 if (irq != -1) {
1103 desc = irq_to_desc(irq);
1104 if (desc)
1105 generic_handle_irq_desc(irq, desc);
1106 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001107 }
1108 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001109
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001110 BUG_ON(!irqs_disabled());
1111
Christoph Lameter780f36d2010-12-06 11:16:29 -06001112 count = __this_cpu_read(xed_nesting_count);
1113 __this_cpu_write(xed_nesting_count, 0);
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001114 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001115
1116out:
Jeremy Fitzhardinge3445a8f2009-02-06 14:09:46 -08001117
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001118 put_cpu();
1119}
1120
Sheng Yang38e20b02010-05-14 12:40:51 +01001121void xen_evtchn_do_upcall(struct pt_regs *regs)
1122{
1123 struct pt_regs *old_regs = set_irq_regs(regs);
1124
1125 exit_idle();
1126 irq_enter();
1127
1128 __xen_evtchn_do_upcall();
1129
1130 irq_exit();
1131 set_irq_regs(old_regs);
1132}
1133
1134void xen_hvm_evtchn_do_upcall(void)
1135{
1136 __xen_evtchn_do_upcall();
1137}
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001138EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
Sheng Yang38e20b02010-05-14 12:40:51 +01001139
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001140/* Rebind a new event channel to an existing irq. */
1141void rebind_evtchn_irq(int evtchn, int irq)
1142{
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001143 struct irq_info *info = info_for_irq(irq);
1144
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001145 /* Make sure the irq is masked, since the new event channel
1146 will also be masked. */
1147 disable_irq(irq);
1148
1149 spin_lock(&irq_mapping_update_lock);
1150
1151 /* After resume the irq<->evtchn mappings are all cleared out */
1152 BUG_ON(evtchn_to_irq[evtchn] != -1);
1153 /* Expect irq to have been bound before,
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001154 so there should be a proper type */
1155 BUG_ON(info->type == IRQT_UNBOUND);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001156
Ian Campbell9158c352011-03-10 16:08:09 +00001157 xen_irq_info_evtchn_init(irq, evtchn);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001158
1159 spin_unlock(&irq_mapping_update_lock);
1160
1161 /* new event channels are always bound to cpu 0 */
Rusty Russell0de26522008-12-13 21:20:26 +10301162 irq_set_affinity(irq, cpumask_of(0));
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001163
1164 /* Unmask the event channel. */
1165 enable_irq(irq);
1166}
1167
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001168/* Rebind an evtchn so that it gets delivered to a specific cpu */
Yinghai Lud5dedd42009-04-27 17:59:21 -07001169static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001170{
1171 struct evtchn_bind_vcpu bind_vcpu;
1172 int evtchn = evtchn_from_irq(irq);
1173
Ian Campbellbe494722011-03-10 16:08:02 +00001174 if (!VALID_EVTCHN(evtchn))
1175 return -1;
1176
1177 /*
1178 * Events delivered via platform PCI interrupts are always
1179 * routed to vcpu 0 and hence cannot be rebound.
1180 */
1181 if (xen_hvm_domain() && !xen_have_vector_callback)
Yinghai Lud5dedd42009-04-27 17:59:21 -07001182 return -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001183
1184 /* Send future instances of this interrupt to other vcpu. */
1185 bind_vcpu.port = evtchn;
1186 bind_vcpu.vcpu = tcpu;
1187
1188 /*
1189 * If this fails, it usually just indicates that we're dealing with a
1190 * virq or IPI channel, which don't actually need to be rebound. Ignore
1191 * it, but don't do the xenlinux-level rebind in that case.
1192 */
1193 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1194 bind_evtchn_to_cpu(evtchn, tcpu);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001195
1196 return 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001197}
1198
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001199static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1200 bool force)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001201{
Rusty Russell0de26522008-12-13 21:20:26 +10301202 unsigned tcpu = cpumask_first(dest);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001203
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001204 return rebind_irq_to_cpu(data->irq, tcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001205}
1206
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001207int resend_irq_on_evtchn(unsigned int irq)
1208{
1209 int masked, evtchn = evtchn_from_irq(irq);
1210 struct shared_info *s = HYPERVISOR_shared_info;
1211
1212 if (!VALID_EVTCHN(evtchn))
1213 return 1;
1214
1215 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
1216 sync_set_bit(evtchn, s->evtchn_pending);
1217 if (!masked)
1218 unmask_evtchn(evtchn);
1219
1220 return 1;
1221}
1222
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001223static void enable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001224{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001225 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001226
1227 if (VALID_EVTCHN(evtchn))
1228 unmask_evtchn(evtchn);
1229}
1230
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001231static void disable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001232{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001233 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001234
1235 if (VALID_EVTCHN(evtchn))
1236 mask_evtchn(evtchn);
1237}
1238
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001239static void ack_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001240{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001241 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001242
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001243 move_masked_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001244
1245 if (VALID_EVTCHN(evtchn))
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -07001246 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001247}
1248
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001249static int retrigger_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001250{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001251 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001252 struct shared_info *sh = HYPERVISOR_shared_info;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001253 int ret = 0;
1254
1255 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001256 int masked;
1257
1258 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
1259 sync_set_bit(evtchn, sh->evtchn_pending);
1260 if (!masked)
1261 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001262 ret = 1;
1263 }
1264
1265 return ret;
1266}
1267
Ian Campbell0a852262011-03-10 16:08:06 +00001268static void restore_pirqs(void)
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001269{
1270 int pirq, rc, irq, gsi;
1271 struct physdev_map_pirq map_irq;
1272
1273 for (pirq = 0; pirq < nr_irqs; pirq++) {
1274 irq = pirq_to_irq[pirq];
1275 if (irq == -1)
1276 continue;
1277
1278 /* save/restore of PT devices doesn't work, so at this point the
1279 * only devices present are GSI based emulated devices */
1280 gsi = gsi_from_irq(irq);
1281 if (!gsi)
1282 continue;
1283
1284 map_irq.domid = DOMID_SELF;
1285 map_irq.type = MAP_PIRQ_TYPE_GSI;
1286 map_irq.index = gsi;
1287 map_irq.pirq = pirq;
1288
1289 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1290 if (rc) {
1291 printk(KERN_WARNING "xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1292 gsi, irq, pirq, rc);
Ian Campbell9158c352011-03-10 16:08:09 +00001293 xen_free_irq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001294 pirq_to_irq[pirq] = -1;
1295 continue;
1296 }
1297
1298 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1299
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001300 __startup_pirq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001301 }
1302}
1303
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001304static void restore_cpu_virqs(unsigned int cpu)
1305{
1306 struct evtchn_bind_virq bind_virq;
1307 int virq, irq, evtchn;
1308
1309 for (virq = 0; virq < NR_VIRQS; virq++) {
1310 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1311 continue;
1312
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001313 BUG_ON(virq_from_irq(irq) != virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001314
1315 /* Get a new binding from Xen. */
1316 bind_virq.virq = virq;
1317 bind_virq.vcpu = cpu;
1318 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1319 &bind_virq) != 0)
1320 BUG();
1321 evtchn = bind_virq.port;
1322
1323 /* Record the new mapping. */
Ian Campbell3d4cfa32011-03-10 16:08:10 +00001324 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001325 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001326 }
1327}
1328
1329static void restore_cpu_ipis(unsigned int cpu)
1330{
1331 struct evtchn_bind_ipi bind_ipi;
1332 int ipi, irq, evtchn;
1333
1334 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1335 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1336 continue;
1337
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001338 BUG_ON(ipi_from_irq(irq) != ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001339
1340 /* Get a new binding from Xen. */
1341 bind_ipi.vcpu = cpu;
1342 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1343 &bind_ipi) != 0)
1344 BUG();
1345 evtchn = bind_ipi.port;
1346
1347 /* Record the new mapping. */
Ian Campbell3d4cfa32011-03-10 16:08:10 +00001348 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001349 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001350 }
1351}
1352
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001353/* Clear an irq's pending state, in preparation for polling on it */
1354void xen_clear_irq_pending(int irq)
1355{
1356 int evtchn = evtchn_from_irq(irq);
1357
1358 if (VALID_EVTCHN(evtchn))
1359 clear_evtchn(evtchn);
1360}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001361EXPORT_SYMBOL(xen_clear_irq_pending);
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -07001362void xen_set_irq_pending(int irq)
1363{
1364 int evtchn = evtchn_from_irq(irq);
1365
1366 if (VALID_EVTCHN(evtchn))
1367 set_evtchn(evtchn);
1368}
1369
1370bool xen_test_irq_pending(int irq)
1371{
1372 int evtchn = evtchn_from_irq(irq);
1373 bool ret = false;
1374
1375 if (VALID_EVTCHN(evtchn))
1376 ret = test_evtchn(evtchn);
1377
1378 return ret;
1379}
1380
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001381/* Poll waiting for an irq to become pending with timeout. In the usual case,
1382 * the irq will be disabled so it won't deliver an interrupt. */
1383void xen_poll_irq_timeout(int irq, u64 timeout)
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001384{
1385 evtchn_port_t evtchn = evtchn_from_irq(irq);
1386
1387 if (VALID_EVTCHN(evtchn)) {
1388 struct sched_poll poll;
1389
1390 poll.nr_ports = 1;
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001391 poll.timeout = timeout;
Isaku Yamahataff3c5362008-10-14 17:50:44 -07001392 set_xen_guest_handle(poll.ports, &evtchn);
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001393
1394 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1395 BUG();
1396 }
1397}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001398EXPORT_SYMBOL(xen_poll_irq_timeout);
1399/* Poll waiting for an irq to become pending. In the usual case, the
1400 * irq will be disabled so it won't deliver an interrupt. */
1401void xen_poll_irq(int irq)
1402{
1403 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1404}
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001405
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001406void xen_irq_resume(void)
1407{
Ian Campbell6cb65372011-03-10 16:08:11 +00001408 unsigned int cpu, evtchn;
1409 struct irq_info *info;
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001410
1411 init_evtchn_cpu_bindings();
1412
1413 /* New event-channel space is not 'live' yet. */
1414 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1415 mask_evtchn(evtchn);
1416
1417 /* No IRQ <-> event-channel mappings. */
Ian Campbell6cb65372011-03-10 16:08:11 +00001418 list_for_each_entry(info, &xen_irq_list_head, list)
1419 info->evtchn = 0; /* zap event-channel binding */
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001420
1421 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1422 evtchn_to_irq[evtchn] = -1;
1423
1424 for_each_possible_cpu(cpu) {
1425 restore_cpu_virqs(cpu);
1426 restore_cpu_ipis(cpu);
1427 }
Ian Campbell69035912010-11-01 16:30:09 +00001428
Ian Campbell0a852262011-03-10 16:08:06 +00001429 restore_pirqs();
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001430}
1431
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001432static struct irq_chip xen_dynamic_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001433 .name = "xen-dyn",
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001434
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001435 .irq_disable = disable_dynirq,
1436 .irq_mask = disable_dynirq,
1437 .irq_unmask = enable_dynirq,
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001438
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001439 .irq_eoi = ack_dynirq,
1440 .irq_set_affinity = set_affinity_irq,
1441 .irq_retrigger = retrigger_dynirq,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001442};
1443
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001444static struct irq_chip xen_pirq_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001445 .name = "xen-pirq",
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001446
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001447 .irq_startup = startup_pirq,
1448 .irq_shutdown = shutdown_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001449
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001450 .irq_enable = enable_pirq,
1451 .irq_unmask = enable_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001452
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001453 .irq_disable = disable_pirq,
1454 .irq_mask = disable_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001455
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001456 .irq_ack = ack_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001457
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001458 .irq_set_affinity = set_affinity_irq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001459
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001460 .irq_retrigger = retrigger_dynirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001461};
1462
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001463static struct irq_chip xen_percpu_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001464 .name = "xen-percpu",
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001465
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001466 .irq_disable = disable_dynirq,
1467 .irq_mask = disable_dynirq,
1468 .irq_unmask = enable_dynirq,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001469
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001470 .irq_ack = ack_dynirq,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001471};
1472
Sheng Yang38e20b02010-05-14 12:40:51 +01001473int xen_set_callback_via(uint64_t via)
1474{
1475 struct xen_hvm_param a;
1476 a.domid = DOMID_SELF;
1477 a.index = HVM_PARAM_CALLBACK_IRQ;
1478 a.value = via;
1479 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1480}
1481EXPORT_SYMBOL_GPL(xen_set_callback_via);
1482
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001483#ifdef CONFIG_XEN_PVHVM
Sheng Yang38e20b02010-05-14 12:40:51 +01001484/* Vector callbacks are better than PCI interrupts to receive event
1485 * channel notifications because we can receive vector callbacks on any
1486 * vcpu and we don't need PCI support or APIC interactions. */
1487void xen_callback_vector(void)
1488{
1489 int rc;
1490 uint64_t callback_via;
1491 if (xen_have_vector_callback) {
1492 callback_via = HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK);
1493 rc = xen_set_callback_via(callback_via);
1494 if (rc) {
1495 printk(KERN_ERR "Request for Xen HVM callback vector"
1496 " failed.\n");
1497 xen_have_vector_callback = 0;
1498 return;
1499 }
1500 printk(KERN_INFO "Xen HVM callback vector for event delivery is "
1501 "enabled\n");
1502 /* in the restore case the vector has already been allocated */
1503 if (!test_bit(XEN_HVM_EVTCHN_CALLBACK, used_vectors))
1504 alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK, xen_hvm_callback_vector);
1505 }
1506}
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001507#else
1508void xen_callback_vector(void) {}
1509#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001510
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001511void __init xen_init_IRQ(void)
1512{
Stefano Stabellinie5fc7342010-12-01 14:51:44 +00001513 int i;
Mike Travisc7a35892009-01-10 21:58:11 -08001514
Stefano Stabellinie5fc7342010-12-01 14:51:44 +00001515 /* We are using nr_irqs as the maximum number of pirq available but
1516 * that number is actually chosen by Xen and we don't know exactly
1517 * what it is. Be careful choosing high pirq numbers. */
1518 pirq_to_irq = kcalloc(nr_irqs, sizeof(*pirq_to_irq), GFP_KERNEL);
1519 for (i = 0; i < nr_irqs; i++)
Stefano Stabellini7a043f12010-07-01 17:08:14 +01001520 pirq_to_irq[i] = -1;
1521
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001522 evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
1523 GFP_KERNEL);
1524 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1525 evtchn_to_irq[i] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001526
1527 init_evtchn_cpu_bindings();
1528
1529 /* No event channels are 'live' right now. */
1530 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1531 mask_evtchn(i);
1532
Sheng Yang38e20b02010-05-14 12:40:51 +01001533 if (xen_hvm_domain()) {
1534 xen_callback_vector();
1535 native_init_IRQ();
Stefano Stabellini3942b742010-06-24 17:50:18 +01001536 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1537 * __acpi_register_gsi can point at the right function */
1538 pci_xen_hvm_init();
Sheng Yang38e20b02010-05-14 12:40:51 +01001539 } else {
1540 irq_ctx_init(smp_processor_id());
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +01001541 if (xen_initial_domain())
1542 xen_setup_pirqs();
Sheng Yang38e20b02010-05-14 12:40:51 +01001543 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001544}