blob: 321a0c8346e581bc3504ac0891b7b4883d98589c [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
59/* IRQ <-> VIRQ mapping. */
Tejun Heo204fba42009-06-24 15:13:45 +090060static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070061
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070062/* IRQ <-> IPI mapping */
Tejun Heo204fba42009-06-24 15:13:45 +090063static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070064
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080065/* Interrupt types. */
66enum xen_irq_type {
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -080067 IRQT_UNBOUND = 0,
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070068 IRQT_PIRQ,
69 IRQT_VIRQ,
70 IRQT_IPI,
71 IRQT_EVTCHN
72};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070073
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080074/*
75 * Packed IRQ information:
76 * type - enum xen_irq_type
77 * event channel - irq->event channel mapping
78 * cpu - cpu this event channel is bound to
79 * index - type-specific information:
Stefano Stabellini42a1de52010-06-24 16:42:04 +010080 * PIRQ - vector, with MSB being "needs EIO", or physical IRQ of the HVM
81 * guest, or GSI (real passthrough IRQ) of the device.
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080082 * VIRQ - virq number
83 * IPI - IPI vector
84 * EVTCHN -
85 */
86struct irq_info
87{
88 enum xen_irq_type type; /* type */
89 unsigned short evtchn; /* event channel */
90 unsigned short cpu; /* cpu bound */
91
92 union {
93 unsigned short virq;
94 enum ipi_vector ipi;
95 struct {
Stefano Stabellini7a043f12010-07-01 17:08:14 +010096 unsigned short pirq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080097 unsigned short gsi;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -040098 unsigned char vector;
99 unsigned char flags;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800100 } pirq;
101 } u;
102};
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400103#define PIRQ_NEEDS_EOI (1 << 0)
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400104#define PIRQ_SHAREABLE (1 << 1)
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800105
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -0400106static struct irq_info *irq_info;
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100107static int *pirq_to_irq;
Stefano Stabellini01557ba2010-08-20 14:46:52 +0100108static int nr_pirqs;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700109
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -0400110static int *evtchn_to_irq;
Mike Travisc7a35892009-01-10 21:58:11 -0800111struct cpu_evtchn_s {
112 unsigned long bits[NR_EVENT_CHANNELS/BITS_PER_LONG];
113};
Jeremy Fitzhardinge3b32f572009-08-13 12:50:37 -0700114
115static __initdata struct cpu_evtchn_s init_evtchn_mask = {
116 .bits[0 ... (NR_EVENT_CHANNELS/BITS_PER_LONG)-1] = ~0ul,
117};
118static struct cpu_evtchn_s *cpu_evtchn_mask_p = &init_evtchn_mask;
119
Mike Travisc7a35892009-01-10 21:58:11 -0800120static inline unsigned long *cpu_evtchn_mask(int cpu)
121{
122 return cpu_evtchn_mask_p[cpu].bits;
123}
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700124
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700125/* Xen will never allocate port zero for any purpose. */
126#define VALID_EVTCHN(chn) ((chn) != 0)
127
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700128static struct irq_chip xen_dynamic_chip;
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700129static struct irq_chip xen_percpu_chip;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400130static struct irq_chip xen_pirq_chip;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700131
132/* Constructor for packed IRQ information. */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800133static struct irq_info mk_unbound_info(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700134{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800135 return (struct irq_info) { .type = IRQT_UNBOUND };
136}
137
138static struct irq_info mk_evtchn_info(unsigned short evtchn)
139{
Ian Campbell90af9512009-02-06 16:55:58 -0800140 return (struct irq_info) { .type = IRQT_EVTCHN, .evtchn = evtchn,
141 .cpu = 0 };
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800142}
143
144static struct irq_info mk_ipi_info(unsigned short evtchn, enum ipi_vector ipi)
145{
146 return (struct irq_info) { .type = IRQT_IPI, .evtchn = evtchn,
Ian Campbell90af9512009-02-06 16:55:58 -0800147 .cpu = 0, .u.ipi = ipi };
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800148}
149
150static struct irq_info mk_virq_info(unsigned short evtchn, unsigned short virq)
151{
152 return (struct irq_info) { .type = IRQT_VIRQ, .evtchn = evtchn,
Ian Campbell90af9512009-02-06 16:55:58 -0800153 .cpu = 0, .u.virq = virq };
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800154}
155
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100156static struct irq_info mk_pirq_info(unsigned short evtchn, unsigned short pirq,
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800157 unsigned short gsi, unsigned short vector)
158{
159 return (struct irq_info) { .type = IRQT_PIRQ, .evtchn = evtchn,
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100160 .cpu = 0,
161 .u.pirq = { .pirq = pirq, .gsi = gsi, .vector = vector } };
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700162}
163
164/*
165 * Accessors for packed IRQ information.
166 */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800167static struct irq_info *info_for_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700168{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800169 return &irq_info[irq];
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700170}
171
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800172static unsigned int evtchn_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700173{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800174 return info_for_irq(irq)->evtchn;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700175}
176
Ian Campbelld4c04532009-02-06 19:20:31 -0800177unsigned irq_from_evtchn(unsigned int evtchn)
178{
179 return evtchn_to_irq[evtchn];
180}
181EXPORT_SYMBOL_GPL(irq_from_evtchn);
182
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800183static enum ipi_vector ipi_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700184{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800185 struct irq_info *info = info_for_irq(irq);
186
187 BUG_ON(info == NULL);
188 BUG_ON(info->type != IRQT_IPI);
189
190 return info->u.ipi;
191}
192
193static unsigned virq_from_irq(unsigned irq)
194{
195 struct irq_info *info = info_for_irq(irq);
196
197 BUG_ON(info == NULL);
198 BUG_ON(info->type != IRQT_VIRQ);
199
200 return info->u.virq;
201}
202
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100203static unsigned pirq_from_irq(unsigned irq)
204{
205 struct irq_info *info = info_for_irq(irq);
206
207 BUG_ON(info == NULL);
208 BUG_ON(info->type != IRQT_PIRQ);
209
210 return info->u.pirq.pirq;
211}
212
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800213static unsigned gsi_from_irq(unsigned irq)
214{
215 struct irq_info *info = info_for_irq(irq);
216
217 BUG_ON(info == NULL);
218 BUG_ON(info->type != IRQT_PIRQ);
219
220 return info->u.pirq.gsi;
221}
222
223static unsigned vector_from_irq(unsigned irq)
224{
225 struct irq_info *info = info_for_irq(irq);
226
227 BUG_ON(info == NULL);
228 BUG_ON(info->type != IRQT_PIRQ);
229
230 return info->u.pirq.vector;
231}
232
233static enum xen_irq_type type_from_irq(unsigned irq)
234{
235 return info_for_irq(irq)->type;
236}
237
238static unsigned cpu_from_irq(unsigned irq)
239{
240 return info_for_irq(irq)->cpu;
241}
242
243static unsigned int cpu_from_evtchn(unsigned int evtchn)
244{
245 int irq = evtchn_to_irq[evtchn];
246 unsigned ret = 0;
247
248 if (irq != -1)
249 ret = cpu_from_irq(irq);
250
251 return ret;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700252}
253
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400254static bool pirq_needs_eoi(unsigned irq)
255{
256 struct irq_info *info = info_for_irq(irq);
257
258 BUG_ON(info->type != IRQT_PIRQ);
259
260 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
261}
262
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700263static inline unsigned long active_evtchns(unsigned int cpu,
264 struct shared_info *sh,
265 unsigned int idx)
266{
267 return (sh->evtchn_pending[idx] &
Mike Travisc7a35892009-01-10 21:58:11 -0800268 cpu_evtchn_mask(cpu)[idx] &
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700269 ~sh->evtchn_mask[idx]);
270}
271
272static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
273{
274 int irq = evtchn_to_irq[chn];
275
276 BUG_ON(irq == -1);
277#ifdef CONFIG_SMP
Mike Travis7f7ace02009-01-10 21:58:08 -0800278 cpumask_copy(irq_to_desc(irq)->affinity, cpumask_of(cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700279#endif
280
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800281 __clear_bit(chn, cpu_evtchn_mask(cpu_from_irq(irq)));
Mike Travisc7a35892009-01-10 21:58:11 -0800282 __set_bit(chn, cpu_evtchn_mask(cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700283
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800284 irq_info[irq].cpu = cpu;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700285}
286
287static void init_evtchn_cpu_bindings(void)
288{
289#ifdef CONFIG_SMP
Thomas Gleixner10e58082008-10-16 14:19:04 +0200290 struct irq_desc *desc;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700291 int i;
Thomas Gleixner10e58082008-10-16 14:19:04 +0200292
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700293 /* By default all event channels notify CPU#0. */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800294 for_each_irq_desc(i, desc) {
Mike Travis7f7ace02009-01-10 21:58:08 -0800295 cpumask_copy(desc->affinity, cpumask_of(0));
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800296 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700297#endif
298
Ian Campbellb0097ad2010-10-08 16:59:12 +0100299 memset(cpu_evtchn_mask(0), ~0, sizeof(struct cpu_evtchn_s));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700300}
301
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700302static inline void clear_evtchn(int port)
303{
304 struct shared_info *s = HYPERVISOR_shared_info;
305 sync_clear_bit(port, &s->evtchn_pending[0]);
306}
307
308static inline void set_evtchn(int port)
309{
310 struct shared_info *s = HYPERVISOR_shared_info;
311 sync_set_bit(port, &s->evtchn_pending[0]);
312}
313
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700314static inline int test_evtchn(int port)
315{
316 struct shared_info *s = HYPERVISOR_shared_info;
317 return sync_test_bit(port, &s->evtchn_pending[0]);
318}
319
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700320
321/**
322 * notify_remote_via_irq - send event to remote end of event channel via irq
323 * @irq: irq of event channel to send event to
324 *
325 * Unlike notify_remote_via_evtchn(), this is safe to use across
326 * save/restore. Notifications on a broken connection are silently
327 * dropped.
328 */
329void notify_remote_via_irq(int irq)
330{
331 int evtchn = evtchn_from_irq(irq);
332
333 if (VALID_EVTCHN(evtchn))
334 notify_remote_via_evtchn(evtchn);
335}
336EXPORT_SYMBOL_GPL(notify_remote_via_irq);
337
338static void mask_evtchn(int port)
339{
340 struct shared_info *s = HYPERVISOR_shared_info;
341 sync_set_bit(port, &s->evtchn_mask[0]);
342}
343
344static void unmask_evtchn(int port)
345{
346 struct shared_info *s = HYPERVISOR_shared_info;
347 unsigned int cpu = get_cpu();
348
349 BUG_ON(!irqs_disabled());
350
351 /* Slow path (hypercall) if this is a non-local port. */
352 if (unlikely(cpu != cpu_from_evtchn(port))) {
353 struct evtchn_unmask unmask = { .port = port };
354 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
355 } else {
356 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
357
358 sync_clear_bit(port, &s->evtchn_mask[0]);
359
360 /*
361 * The following is basically the equivalent of
362 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
363 * the interrupt edge' if the channel is masked.
364 */
365 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
366 !sync_test_and_set_bit(port / BITS_PER_LONG,
367 &vcpu_info->evtchn_pending_sel))
368 vcpu_info->evtchn_upcall_pending = 1;
369 }
370
371 put_cpu();
372}
373
Konrad Rzeszutek Wilk0794bfc2010-10-18 10:41:08 -0400374static int get_nr_hw_irqs(void)
375{
376 int ret = 1;
377
378#ifdef CONFIG_X86_IO_APIC
379 ret = get_nr_irqs_gsi();
380#endif
381
382 return ret;
383}
384
Stefano Stabellini01557ba2010-08-20 14:46:52 +0100385/* callers of this function should make sure that PHYSDEVOP_get_nr_pirqs
386 * succeeded otherwise nr_pirqs won't hold the right value */
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100387static int find_unbound_pirq(void)
388{
389 int i;
Stefano Stabellini01557ba2010-08-20 14:46:52 +0100390 for (i = nr_pirqs-1; i >= 0; i--) {
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100391 if (pirq_to_irq[i] < 0)
392 return i;
393 }
394 return -1;
395}
396
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700397static int find_unbound_irq(void)
398{
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200399 struct irq_data *data;
400 int irq, res;
Konrad Rzeszutek Wilk3a69e912010-10-18 10:49:10 -0400401 int start = get_nr_hw_irqs();
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700402
Konrad Rzeszutek Wilk3a69e912010-10-18 10:49:10 -0400403 if (start == nr_irqs)
404 goto no_irqs;
405
406 /* nr_irqs is a magic value. Must not use it.*/
407 for (irq = nr_irqs-1; irq > start; irq--) {
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200408 data = irq_get_irq_data(irq);
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100409 /* only 0->15 have init'd desc; handle irq > 16 */
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200410 if (!data)
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100411 break;
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200412 if (data->chip == &no_irq_chip)
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100413 break;
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200414 if (data->chip != &xen_dynamic_chip)
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100415 continue;
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800416 if (irq_info[irq].type == IRQT_UNBOUND)
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200417 return irq;
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100418 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700419
Konrad Rzeszutek Wilk3a69e912010-10-18 10:49:10 -0400420 if (irq == start)
421 goto no_irqs;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700422
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200423 res = irq_alloc_desc_at(irq, 0);
Jeremy Fitzhardinge6f8a0ed2008-12-17 13:42:29 -0800424
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200425 if (WARN_ON(res != irq))
426 return -1;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800427
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700428 return irq;
Konrad Rzeszutek Wilk3a69e912010-10-18 10:49:10 -0400429
430no_irqs:
431 panic("No available IRQ to bind to: increase nr_irqs!\n");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700432}
433
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400434static bool identity_mapped_irq(unsigned irq)
435{
Konrad Rzeszutek Wilk0794bfc2010-10-18 10:41:08 -0400436 /* identity map all the hardware irqs */
437 return irq < get_nr_hw_irqs();
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400438}
439
440static void pirq_unmask_notify(int irq)
441{
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100442 struct physdev_eoi eoi = { .irq = pirq_from_irq(irq) };
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400443
444 if (unlikely(pirq_needs_eoi(irq))) {
445 int rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
446 WARN_ON(rc);
447 }
448}
449
450static void pirq_query_unmask(int irq)
451{
452 struct physdev_irq_status_query irq_status;
453 struct irq_info *info = info_for_irq(irq);
454
455 BUG_ON(info->type != IRQT_PIRQ);
456
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100457 irq_status.irq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400458 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
459 irq_status.flags = 0;
460
461 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
462 if (irq_status.flags & XENIRQSTAT_needs_eoi)
463 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
464}
465
466static bool probing_irq(int irq)
467{
468 struct irq_desc *desc = irq_to_desc(irq);
469
470 return desc && desc->action == NULL;
471}
472
473static unsigned int startup_pirq(unsigned int irq)
474{
475 struct evtchn_bind_pirq bind_pirq;
476 struct irq_info *info = info_for_irq(irq);
477 int evtchn = evtchn_from_irq(irq);
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400478 int rc;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400479
480 BUG_ON(info->type != IRQT_PIRQ);
481
482 if (VALID_EVTCHN(evtchn))
483 goto out;
484
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100485 bind_pirq.pirq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400486 /* NB. We are happy to share unless we are probing. */
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400487 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
488 BIND_PIRQ__WILL_SHARE : 0;
489 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
490 if (rc != 0) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400491 if (!probing_irq(irq))
492 printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
493 irq);
494 return 0;
495 }
496 evtchn = bind_pirq.port;
497
498 pirq_query_unmask(irq);
499
500 evtchn_to_irq[evtchn] = irq;
501 bind_evtchn_to_cpu(evtchn, 0);
502 info->evtchn = evtchn;
503
504out:
505 unmask_evtchn(evtchn);
506 pirq_unmask_notify(irq);
507
508 return 0;
509}
510
511static void shutdown_pirq(unsigned int irq)
512{
513 struct evtchn_close close;
514 struct irq_info *info = info_for_irq(irq);
515 int evtchn = evtchn_from_irq(irq);
516
517 BUG_ON(info->type != IRQT_PIRQ);
518
519 if (!VALID_EVTCHN(evtchn))
520 return;
521
522 mask_evtchn(evtchn);
523
524 close.port = evtchn;
525 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
526 BUG();
527
528 bind_evtchn_to_cpu(evtchn, 0);
529 evtchn_to_irq[evtchn] = -1;
530 info->evtchn = 0;
531}
532
533static void enable_pirq(unsigned int irq)
534{
535 startup_pirq(irq);
536}
537
538static void disable_pirq(unsigned int irq)
539{
540}
541
542static void ack_pirq(unsigned int irq)
543{
544 int evtchn = evtchn_from_irq(irq);
545
546 move_native_irq(irq);
547
548 if (VALID_EVTCHN(evtchn)) {
549 mask_evtchn(evtchn);
550 clear_evtchn(evtchn);
551 }
552}
553
554static void end_pirq(unsigned int irq)
555{
556 int evtchn = evtchn_from_irq(irq);
557 struct irq_desc *desc = irq_to_desc(irq);
558
559 if (WARN_ON(!desc))
560 return;
561
562 if ((desc->status & (IRQ_DISABLED|IRQ_PENDING)) ==
563 (IRQ_DISABLED|IRQ_PENDING)) {
564 shutdown_pirq(irq);
565 } else if (VALID_EVTCHN(evtchn)) {
566 unmask_evtchn(evtchn);
567 pirq_unmask_notify(irq);
568 }
569}
570
571static int find_irq_by_gsi(unsigned gsi)
572{
573 int irq;
574
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -0400575 for (irq = 0; irq < nr_irqs; irq++) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400576 struct irq_info *info = info_for_irq(irq);
577
578 if (info == NULL || info->type != IRQT_PIRQ)
579 continue;
580
581 if (gsi_from_irq(irq) == gsi)
582 return irq;
583 }
584
585 return -1;
586}
587
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100588int xen_allocate_pirq(unsigned gsi, int shareable, char *name)
589{
590 return xen_map_pirq_gsi(gsi, gsi, shareable, name);
591}
592
593/* xen_map_pirq_gsi might allocate irqs from the top down, as a
Konrad Rzeszutek Wilk3a69e912010-10-18 10:49:10 -0400594 * consequence don't assume that the irq number returned has a low value
595 * or can be used as a pirq number unless you know otherwise.
596 *
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100597 * One notable exception is when xen_map_pirq_gsi is called passing an
Konrad Rzeszutek Wilk3a69e912010-10-18 10:49:10 -0400598 * hardware gsi as argument, in that case the irq number returned
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100599 * matches the gsi number passed as second argument.
600 *
601 * Note: We don't assign an event channel until the irq actually started
602 * up. Return an existing irq if we've already got one for the gsi.
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400603 */
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100604int xen_map_pirq_gsi(unsigned pirq, unsigned gsi, int shareable, char *name)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400605{
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100606 int irq = 0;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400607 struct physdev_irq irq_op;
608
609 spin_lock(&irq_mapping_update_lock);
610
Stefano Stabellini01557ba2010-08-20 14:46:52 +0100611 if ((pirq > nr_pirqs) || (gsi > nr_irqs)) {
612 printk(KERN_WARNING "xen_map_pirq_gsi: %s %s is incorrect!\n",
613 pirq > nr_pirqs ? "nr_pirqs" :"",
614 gsi > nr_irqs ? "nr_irqs" : "");
615 goto out;
616 }
617
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400618 irq = find_irq_by_gsi(gsi);
619 if (irq != -1) {
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100620 printk(KERN_INFO "xen_map_pirq_gsi: returning irq %d for gsi %u\n",
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400621 irq, gsi);
622 goto out; /* XXX need refcount? */
623 }
624
Alex Nixonb5401a92010-03-18 16:31:34 -0400625 /* If we are a PV guest, we don't have GSIs (no ACPI passed). Therefore
626 * we are using the !xen_initial_domain() to drop in the function.*/
Stefano Stabellini3942b742010-06-24 17:50:18 +0100627 if (identity_mapped_irq(gsi) || (!xen_initial_domain() &&
628 xen_pv_domain())) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400629 irq = gsi;
Konrad Rzeszutek Wilk2c52f8d2010-10-18 17:11:10 -0400630 irq_alloc_desc_at(irq, 0);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400631 } else
632 irq = find_unbound_irq();
633
634 set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
Gerd Hoffmann1a60d052010-10-04 13:42:27 -0400635 handle_level_irq, name);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400636
637 irq_op.irq = irq;
Alex Nixonb5401a92010-03-18 16:31:34 -0400638 irq_op.vector = 0;
639
640 /* Only the privileged domain can do this. For non-priv, the pcifront
641 * driver provides a PCI bus that does the call to do exactly
642 * this in the priv domain. */
643 if (xen_initial_domain() &&
644 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
Konrad Rzeszutek Wilk2c52f8d2010-10-18 17:11:10 -0400645 irq_free_desc(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400646 irq = -ENOSPC;
647 goto out;
648 }
649
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100650 irq_info[irq] = mk_pirq_info(0, pirq, gsi, irq_op.vector);
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400651 irq_info[irq].u.pirq.flags |= shareable ? PIRQ_SHAREABLE : 0;
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100652 pirq_to_irq[pirq] = irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400653
654out:
655 spin_unlock(&irq_mapping_update_lock);
656
657 return irq;
658}
659
Qing Hef731e3ef2010-10-11 15:30:09 +0100660#ifdef CONFIG_PCI_MSI
661#include <linux/msi.h>
662#include "../pci/msi.h"
663
Stefano Stabellini809f9262010-07-01 17:10:39 +0100664void xen_allocate_pirq_msi(char *name, int *irq, int *pirq)
665{
666 spin_lock(&irq_mapping_update_lock);
667
668 *irq = find_unbound_irq();
669 if (*irq == -1)
670 goto out;
671
672 *pirq = find_unbound_pirq();
673 if (*pirq == -1)
674 goto out;
675
676 set_irq_chip_and_handler_name(*irq, &xen_pirq_chip,
677 handle_level_irq, name);
678
679 irq_info[*irq] = mk_pirq_info(0, *pirq, 0, 0);
680 pirq_to_irq[*pirq] = *irq;
681
682out:
683 spin_unlock(&irq_mapping_update_lock);
684}
685
Qing Hef731e3ef2010-10-11 15:30:09 +0100686int xen_create_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc, int type)
687{
688 int irq = -1;
689 struct physdev_map_pirq map_irq;
690 int rc;
691 int pos;
692 u32 table_offset, bir;
693
694 memset(&map_irq, 0, sizeof(map_irq));
695 map_irq.domid = DOMID_SELF;
696 map_irq.type = MAP_PIRQ_TYPE_MSI;
697 map_irq.index = -1;
698 map_irq.pirq = -1;
699 map_irq.bus = dev->bus->number;
700 map_irq.devfn = dev->devfn;
701
702 if (type == PCI_CAP_ID_MSIX) {
703 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
704
705 pci_read_config_dword(dev, msix_table_offset_reg(pos),
706 &table_offset);
707 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
708
709 map_irq.table_base = pci_resource_start(dev, bir);
710 map_irq.entry_nr = msidesc->msi_attrib.entry_nr;
711 }
712
713 spin_lock(&irq_mapping_update_lock);
714
715 irq = find_unbound_irq();
716
717 if (irq == -1)
718 goto out;
719
720 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
721 if (rc) {
722 printk(KERN_WARNING "xen map irq failed %d\n", rc);
723
724 irq_free_desc(irq);
725
726 irq = -1;
727 goto out;
728 }
729 irq_info[irq] = mk_pirq_info(0, map_irq.pirq, 0, map_irq.index);
730
731 set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
732 handle_level_irq,
733 (type == PCI_CAP_ID_MSIX) ? "msi-x":"msi");
734
735out:
736 spin_unlock(&irq_mapping_update_lock);
737 return irq;
738}
739#endif
740
Alex Nixonb5401a92010-03-18 16:31:34 -0400741int xen_destroy_irq(int irq)
742{
743 struct irq_desc *desc;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100744 struct physdev_unmap_pirq unmap_irq;
745 struct irq_info *info = info_for_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400746 int rc = -ENOENT;
747
748 spin_lock(&irq_mapping_update_lock);
749
750 desc = irq_to_desc(irq);
751 if (!desc)
752 goto out;
753
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100754 if (xen_initial_domain()) {
755 unmap_irq.pirq = info->u.pirq.gsi;
756 unmap_irq.domid = DOMID_SELF;
757 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
758 if (rc) {
759 printk(KERN_WARNING "unmap irq failed %d\n", rc);
760 goto out;
761 }
762 }
Alex Nixonb5401a92010-03-18 16:31:34 -0400763 irq_info[irq] = mk_unbound_info();
764
Konrad Rzeszutek Wilk2c52f8d2010-10-18 17:11:10 -0400765 irq_free_desc(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400766
767out:
768 spin_unlock(&irq_mapping_update_lock);
769 return rc;
770}
771
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400772int xen_vector_from_irq(unsigned irq)
773{
774 return vector_from_irq(irq);
775}
776
777int xen_gsi_from_irq(unsigned irq)
778{
779 return gsi_from_irq(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700780}
781
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700782int bind_evtchn_to_irq(unsigned int evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700783{
784 int irq;
785
786 spin_lock(&irq_mapping_update_lock);
787
788 irq = evtchn_to_irq[evtchn];
789
790 if (irq == -1) {
791 irq = find_unbound_irq();
792
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700793 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -0700794 handle_fasteoi_irq, "event");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700795
796 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800797 irq_info[irq] = mk_evtchn_info(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700798 }
799
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700800 spin_unlock(&irq_mapping_update_lock);
801
802 return irq;
803}
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700804EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700805
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700806static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
807{
808 struct evtchn_bind_ipi bind_ipi;
809 int evtchn, irq;
810
811 spin_lock(&irq_mapping_update_lock);
812
813 irq = per_cpu(ipi_to_irq, cpu)[ipi];
Ian Campbell90af9512009-02-06 16:55:58 -0800814
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700815 if (irq == -1) {
816 irq = find_unbound_irq();
817 if (irq < 0)
818 goto out;
819
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700820 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
821 handle_percpu_irq, "ipi");
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700822
823 bind_ipi.vcpu = cpu;
824 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
825 &bind_ipi) != 0)
826 BUG();
827 evtchn = bind_ipi.port;
828
829 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800830 irq_info[irq] = mk_ipi_info(evtchn, ipi);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700831 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
832
833 bind_evtchn_to_cpu(evtchn, cpu);
834 }
835
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700836 out:
837 spin_unlock(&irq_mapping_update_lock);
838 return irq;
839}
840
841
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100842int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700843{
844 struct evtchn_bind_virq bind_virq;
845 int evtchn, irq;
846
847 spin_lock(&irq_mapping_update_lock);
848
849 irq = per_cpu(virq_to_irq, cpu)[virq];
850
851 if (irq == -1) {
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700852 irq = find_unbound_irq();
853
854 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
855 handle_percpu_irq, "virq");
856
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700857 bind_virq.virq = virq;
858 bind_virq.vcpu = cpu;
859 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
860 &bind_virq) != 0)
861 BUG();
862 evtchn = bind_virq.port;
863
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700864 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800865 irq_info[irq] = mk_virq_info(evtchn, virq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700866
867 per_cpu(virq_to_irq, cpu)[virq] = irq;
868
869 bind_evtchn_to_cpu(evtchn, cpu);
870 }
871
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700872 spin_unlock(&irq_mapping_update_lock);
873
874 return irq;
875}
876
877static void unbind_from_irq(unsigned int irq)
878{
879 struct evtchn_close close;
880 int evtchn = evtchn_from_irq(irq);
881
882 spin_lock(&irq_mapping_update_lock);
883
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800884 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700885 close.port = evtchn;
886 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
887 BUG();
888
889 switch (type_from_irq(irq)) {
890 case IRQT_VIRQ:
891 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800892 [virq_from_irq(irq)] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700893 break;
Alex Nixond68d82a2008-08-22 11:52:15 +0100894 case IRQT_IPI:
895 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800896 [ipi_from_irq(irq)] = -1;
Alex Nixond68d82a2008-08-22 11:52:15 +0100897 break;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700898 default:
899 break;
900 }
901
902 /* Closed ports are implicitly re-bound to VCPU0. */
903 bind_evtchn_to_cpu(evtchn, 0);
904
905 evtchn_to_irq[evtchn] = -1;
Ian Campbellfed5ea82009-12-01 16:15:30 +0000906 }
907
908 if (irq_info[irq].type != IRQT_UNBOUND) {
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800909 irq_info[irq] = mk_unbound_info();
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700910
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200911 irq_free_desc(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700912 }
913
914 spin_unlock(&irq_mapping_update_lock);
915}
916
917int bind_evtchn_to_irqhandler(unsigned int evtchn,
Jeff Garzik7c239972007-10-19 03:12:20 -0400918 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700919 unsigned long irqflags,
920 const char *devname, void *dev_id)
921{
922 unsigned int irq;
923 int retval;
924
925 irq = bind_evtchn_to_irq(evtchn);
926 retval = request_irq(irq, handler, irqflags, devname, dev_id);
927 if (retval != 0) {
928 unbind_from_irq(irq);
929 return retval;
930 }
931
932 return irq;
933}
934EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
935
936int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
Jeff Garzik7c239972007-10-19 03:12:20 -0400937 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700938 unsigned long irqflags, const char *devname, void *dev_id)
939{
940 unsigned int irq;
941 int retval;
942
943 irq = bind_virq_to_irq(virq, cpu);
944 retval = request_irq(irq, handler, irqflags, devname, dev_id);
945 if (retval != 0) {
946 unbind_from_irq(irq);
947 return retval;
948 }
949
950 return irq;
951}
952EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
953
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700954int bind_ipi_to_irqhandler(enum ipi_vector ipi,
955 unsigned int cpu,
956 irq_handler_t handler,
957 unsigned long irqflags,
958 const char *devname,
959 void *dev_id)
960{
961 int irq, retval;
962
963 irq = bind_ipi_to_irq(ipi, cpu);
964 if (irq < 0)
965 return irq;
966
Ian Campbell4877c732010-07-29 11:16:35 +0100967 irqflags |= IRQF_NO_SUSPEND;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700968 retval = request_irq(irq, handler, irqflags, devname, dev_id);
969 if (retval != 0) {
970 unbind_from_irq(irq);
971 return retval;
972 }
973
974 return irq;
975}
976
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700977void unbind_from_irqhandler(unsigned int irq, void *dev_id)
978{
979 free_irq(irq, dev_id);
980 unbind_from_irq(irq);
981}
982EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
983
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700984void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
985{
986 int irq = per_cpu(ipi_to_irq, cpu)[vector];
987 BUG_ON(irq < 0);
988 notify_remote_via_irq(irq);
989}
990
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700991irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
992{
993 struct shared_info *sh = HYPERVISOR_shared_info;
994 int cpu = smp_processor_id();
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100995 unsigned long *cpu_evtchn = cpu_evtchn_mask(cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700996 int i;
997 unsigned long flags;
998 static DEFINE_SPINLOCK(debug_lock);
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100999 struct vcpu_info *v;
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001000
1001 spin_lock_irqsave(&debug_lock, flags);
1002
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001003 printk("\nvcpu %d\n ", cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001004
1005 for_each_online_cpu(i) {
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001006 int pending;
1007 v = per_cpu(xen_vcpu, i);
1008 pending = (get_irq_regs() && i == cpu)
1009 ? xen_irqs_disabled(get_irq_regs())
1010 : v->evtchn_upcall_mask;
1011 printk("%d: masked=%d pending=%d event_sel %0*lx\n ", i,
1012 pending, v->evtchn_upcall_pending,
1013 (int)(sizeof(v->evtchn_pending_sel)*2),
1014 v->evtchn_pending_sel);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001015 }
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001016 v = per_cpu(xen_vcpu, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001017
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001018 printk("\npending:\n ");
1019 for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
1020 printk("%0*lx%s", (int)sizeof(sh->evtchn_pending[0])*2,
1021 sh->evtchn_pending[i],
1022 i % 8 == 0 ? "\n " : " ");
1023 printk("\nglobal mask:\n ");
1024 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1025 printk("%0*lx%s",
1026 (int)(sizeof(sh->evtchn_mask[0])*2),
1027 sh->evtchn_mask[i],
1028 i % 8 == 0 ? "\n " : " ");
1029
1030 printk("\nglobally unmasked:\n ");
1031 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1032 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
1033 sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
1034 i % 8 == 0 ? "\n " : " ");
1035
1036 printk("\nlocal cpu%d mask:\n ", cpu);
1037 for (i = (NR_EVENT_CHANNELS/BITS_PER_LONG)-1; i >= 0; i--)
1038 printk("%0*lx%s", (int)(sizeof(cpu_evtchn[0])*2),
1039 cpu_evtchn[i],
1040 i % 8 == 0 ? "\n " : " ");
1041
1042 printk("\nlocally unmasked:\n ");
1043 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) {
1044 unsigned long pending = sh->evtchn_pending[i]
1045 & ~sh->evtchn_mask[i]
1046 & cpu_evtchn[i];
1047 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
1048 pending, i % 8 == 0 ? "\n " : " ");
1049 }
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001050
1051 printk("\npending list:\n");
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001052 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001053 if (sync_test_bit(i, sh->evtchn_pending)) {
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001054 int word_idx = i / BITS_PER_LONG;
1055 printk(" %d: event %d -> irq %d%s%s%s\n",
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001056 cpu_from_evtchn(i), i,
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001057 evtchn_to_irq[i],
1058 sync_test_bit(word_idx, &v->evtchn_pending_sel)
1059 ? "" : " l2-clear",
1060 !sync_test_bit(i, sh->evtchn_mask)
1061 ? "" : " globally-masked",
1062 sync_test_bit(i, cpu_evtchn)
1063 ? "" : " locally-masked");
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001064 }
1065 }
1066
1067 spin_unlock_irqrestore(&debug_lock, flags);
1068
1069 return IRQ_HANDLED;
1070}
1071
Tejun Heo245b2e72009-06-24 15:13:48 +09001072static DEFINE_PER_CPU(unsigned, xed_nesting_count);
1073
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001074/*
1075 * Search the CPUs pending events bitmasks. For each one found, map
1076 * the event number to an irq, and feed it into do_IRQ() for
1077 * handling.
1078 *
1079 * Xen uses a two-level bitmap to speed searching. The first level is
1080 * a bitset of words which contain pending event bits. The second
1081 * level is a bitset of pending events themselves.
1082 */
Sheng Yang38e20b02010-05-14 12:40:51 +01001083static void __xen_evtchn_do_upcall(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001084{
1085 int cpu = get_cpu();
1086 struct shared_info *s = HYPERVISOR_shared_info;
1087 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001088 unsigned count;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001089
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001090 do {
1091 unsigned long pending_words;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001092
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001093 vcpu_info->evtchn_upcall_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001094
Tejun Heo245b2e72009-06-24 15:13:48 +09001095 if (__get_cpu_var(xed_nesting_count)++)
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001096 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001097
Isaku Yamahatae849c3e2008-04-02 10:53:56 -07001098#ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
1099 /* Clear master flag /before/ clearing selector flag. */
Isaku Yamahata6673cf62008-06-16 14:58:13 -07001100 wmb();
Isaku Yamahatae849c3e2008-04-02 10:53:56 -07001101#endif
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001102 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
1103 while (pending_words != 0) {
1104 unsigned long pending_bits;
1105 int word_idx = __ffs(pending_words);
1106 pending_words &= ~(1UL << word_idx);
1107
1108 while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
1109 int bit_idx = __ffs(pending_bits);
1110 int port = (word_idx * BITS_PER_LONG) + bit_idx;
1111 int irq = evtchn_to_irq[port];
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -08001112 struct irq_desc *desc;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001113
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -07001114 mask_evtchn(port);
1115 clear_evtchn(port);
1116
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -08001117 if (irq != -1) {
1118 desc = irq_to_desc(irq);
1119 if (desc)
1120 generic_handle_irq_desc(irq, desc);
1121 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001122 }
1123 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001124
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001125 BUG_ON(!irqs_disabled());
1126
Tejun Heo245b2e72009-06-24 15:13:48 +09001127 count = __get_cpu_var(xed_nesting_count);
1128 __get_cpu_var(xed_nesting_count) = 0;
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001129 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001130
1131out:
Jeremy Fitzhardinge3445a8f2009-02-06 14:09:46 -08001132
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001133 put_cpu();
1134}
1135
Sheng Yang38e20b02010-05-14 12:40:51 +01001136void xen_evtchn_do_upcall(struct pt_regs *regs)
1137{
1138 struct pt_regs *old_regs = set_irq_regs(regs);
1139
1140 exit_idle();
1141 irq_enter();
1142
1143 __xen_evtchn_do_upcall();
1144
1145 irq_exit();
1146 set_irq_regs(old_regs);
1147}
1148
1149void xen_hvm_evtchn_do_upcall(void)
1150{
1151 __xen_evtchn_do_upcall();
1152}
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001153EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
Sheng Yang38e20b02010-05-14 12:40:51 +01001154
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001155/* Rebind a new event channel to an existing irq. */
1156void rebind_evtchn_irq(int evtchn, int irq)
1157{
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001158 struct irq_info *info = info_for_irq(irq);
1159
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001160 /* Make sure the irq is masked, since the new event channel
1161 will also be masked. */
1162 disable_irq(irq);
1163
1164 spin_lock(&irq_mapping_update_lock);
1165
1166 /* After resume the irq<->evtchn mappings are all cleared out */
1167 BUG_ON(evtchn_to_irq[evtchn] != -1);
1168 /* Expect irq to have been bound before,
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001169 so there should be a proper type */
1170 BUG_ON(info->type == IRQT_UNBOUND);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001171
1172 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001173 irq_info[irq] = mk_evtchn_info(evtchn);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001174
1175 spin_unlock(&irq_mapping_update_lock);
1176
1177 /* new event channels are always bound to cpu 0 */
Rusty Russell0de26522008-12-13 21:20:26 +10301178 irq_set_affinity(irq, cpumask_of(0));
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001179
1180 /* Unmask the event channel. */
1181 enable_irq(irq);
1182}
1183
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001184/* Rebind an evtchn so that it gets delivered to a specific cpu */
Yinghai Lud5dedd42009-04-27 17:59:21 -07001185static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001186{
1187 struct evtchn_bind_vcpu bind_vcpu;
1188 int evtchn = evtchn_from_irq(irq);
1189
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001190 /* events delivered via platform PCI interrupts are always
1191 * routed to vcpu 0 */
1192 if (!VALID_EVTCHN(evtchn) ||
1193 (xen_hvm_domain() && !xen_have_vector_callback))
Yinghai Lud5dedd42009-04-27 17:59:21 -07001194 return -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001195
1196 /* Send future instances of this interrupt to other vcpu. */
1197 bind_vcpu.port = evtchn;
1198 bind_vcpu.vcpu = tcpu;
1199
1200 /*
1201 * If this fails, it usually just indicates that we're dealing with a
1202 * virq or IPI channel, which don't actually need to be rebound. Ignore
1203 * it, but don't do the xenlinux-level rebind in that case.
1204 */
1205 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1206 bind_evtchn_to_cpu(evtchn, tcpu);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001207
1208 return 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001209}
1210
Yinghai Lud5dedd42009-04-27 17:59:21 -07001211static int set_affinity_irq(unsigned irq, const struct cpumask *dest)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001212{
Rusty Russell0de26522008-12-13 21:20:26 +10301213 unsigned tcpu = cpumask_first(dest);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001214
1215 return rebind_irq_to_cpu(irq, tcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001216}
1217
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001218int resend_irq_on_evtchn(unsigned int irq)
1219{
1220 int masked, evtchn = evtchn_from_irq(irq);
1221 struct shared_info *s = HYPERVISOR_shared_info;
1222
1223 if (!VALID_EVTCHN(evtchn))
1224 return 1;
1225
1226 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
1227 sync_set_bit(evtchn, s->evtchn_pending);
1228 if (!masked)
1229 unmask_evtchn(evtchn);
1230
1231 return 1;
1232}
1233
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001234static void enable_dynirq(unsigned int irq)
1235{
1236 int evtchn = evtchn_from_irq(irq);
1237
1238 if (VALID_EVTCHN(evtchn))
1239 unmask_evtchn(evtchn);
1240}
1241
1242static void disable_dynirq(unsigned int irq)
1243{
1244 int evtchn = evtchn_from_irq(irq);
1245
1246 if (VALID_EVTCHN(evtchn))
1247 mask_evtchn(evtchn);
1248}
1249
1250static void ack_dynirq(unsigned int irq)
1251{
1252 int evtchn = evtchn_from_irq(irq);
1253
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -07001254 move_masked_irq(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001255
1256 if (VALID_EVTCHN(evtchn))
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -07001257 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001258}
1259
1260static int retrigger_dynirq(unsigned int irq)
1261{
1262 int evtchn = evtchn_from_irq(irq);
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001263 struct shared_info *sh = HYPERVISOR_shared_info;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001264 int ret = 0;
1265
1266 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001267 int masked;
1268
1269 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
1270 sync_set_bit(evtchn, sh->evtchn_pending);
1271 if (!masked)
1272 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001273 ret = 1;
1274 }
1275
1276 return ret;
1277}
1278
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001279static void restore_cpu_virqs(unsigned int cpu)
1280{
1281 struct evtchn_bind_virq bind_virq;
1282 int virq, irq, evtchn;
1283
1284 for (virq = 0; virq < NR_VIRQS; virq++) {
1285 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1286 continue;
1287
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001288 BUG_ON(virq_from_irq(irq) != virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001289
1290 /* Get a new binding from Xen. */
1291 bind_virq.virq = virq;
1292 bind_virq.vcpu = cpu;
1293 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1294 &bind_virq) != 0)
1295 BUG();
1296 evtchn = bind_virq.port;
1297
1298 /* Record the new mapping. */
1299 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001300 irq_info[irq] = mk_virq_info(evtchn, virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001301 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001302 }
1303}
1304
1305static void restore_cpu_ipis(unsigned int cpu)
1306{
1307 struct evtchn_bind_ipi bind_ipi;
1308 int ipi, irq, evtchn;
1309
1310 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1311 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1312 continue;
1313
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001314 BUG_ON(ipi_from_irq(irq) != ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001315
1316 /* Get a new binding from Xen. */
1317 bind_ipi.vcpu = cpu;
1318 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1319 &bind_ipi) != 0)
1320 BUG();
1321 evtchn = bind_ipi.port;
1322
1323 /* Record the new mapping. */
1324 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001325 irq_info[irq] = mk_ipi_info(evtchn, ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001326 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001327 }
1328}
1329
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001330/* Clear an irq's pending state, in preparation for polling on it */
1331void xen_clear_irq_pending(int irq)
1332{
1333 int evtchn = evtchn_from_irq(irq);
1334
1335 if (VALID_EVTCHN(evtchn))
1336 clear_evtchn(evtchn);
1337}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001338EXPORT_SYMBOL(xen_clear_irq_pending);
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -07001339void xen_set_irq_pending(int irq)
1340{
1341 int evtchn = evtchn_from_irq(irq);
1342
1343 if (VALID_EVTCHN(evtchn))
1344 set_evtchn(evtchn);
1345}
1346
1347bool xen_test_irq_pending(int irq)
1348{
1349 int evtchn = evtchn_from_irq(irq);
1350 bool ret = false;
1351
1352 if (VALID_EVTCHN(evtchn))
1353 ret = test_evtchn(evtchn);
1354
1355 return ret;
1356}
1357
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001358/* Poll waiting for an irq to become pending with timeout. In the usual case,
1359 * the irq will be disabled so it won't deliver an interrupt. */
1360void xen_poll_irq_timeout(int irq, u64 timeout)
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001361{
1362 evtchn_port_t evtchn = evtchn_from_irq(irq);
1363
1364 if (VALID_EVTCHN(evtchn)) {
1365 struct sched_poll poll;
1366
1367 poll.nr_ports = 1;
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001368 poll.timeout = timeout;
Isaku Yamahataff3c5362008-10-14 17:50:44 -07001369 set_xen_guest_handle(poll.ports, &evtchn);
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001370
1371 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1372 BUG();
1373 }
1374}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001375EXPORT_SYMBOL(xen_poll_irq_timeout);
1376/* Poll waiting for an irq to become pending. In the usual case, the
1377 * irq will be disabled so it won't deliver an interrupt. */
1378void xen_poll_irq(int irq)
1379{
1380 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1381}
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001382
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001383void xen_irq_resume(void)
1384{
1385 unsigned int cpu, irq, evtchn;
Ian Campbell69035912010-11-01 16:30:09 +00001386 struct irq_desc *desc;
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001387
1388 init_evtchn_cpu_bindings();
1389
1390 /* New event-channel space is not 'live' yet. */
1391 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1392 mask_evtchn(evtchn);
1393
1394 /* No IRQ <-> event-channel mappings. */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001395 for (irq = 0; irq < nr_irqs; irq++)
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001396 irq_info[irq].evtchn = 0; /* zap event-channel binding */
1397
1398 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1399 evtchn_to_irq[evtchn] = -1;
1400
1401 for_each_possible_cpu(cpu) {
1402 restore_cpu_virqs(cpu);
1403 restore_cpu_ipis(cpu);
1404 }
Ian Campbell69035912010-11-01 16:30:09 +00001405
1406 /*
1407 * Unmask any IRQF_NO_SUSPEND IRQs which are enabled. These
1408 * are not handled by the IRQ core.
1409 */
1410 for_each_irq_desc(irq, desc) {
1411 if (!desc->action || !(desc->action->flags & IRQF_NO_SUSPEND))
1412 continue;
1413 if (desc->status & IRQ_DISABLED)
1414 continue;
1415
1416 evtchn = evtchn_from_irq(irq);
1417 if (evtchn == -1)
1418 continue;
1419
1420 unmask_evtchn(evtchn);
1421 }
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001422}
1423
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001424static struct irq_chip xen_dynamic_chip __read_mostly = {
1425 .name = "xen-dyn",
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001426
1427 .disable = disable_dynirq,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001428 .mask = disable_dynirq,
1429 .unmask = enable_dynirq,
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001430
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -07001431 .eoi = ack_dynirq,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001432 .set_affinity = set_affinity_irq,
1433 .retrigger = retrigger_dynirq,
1434};
1435
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001436static struct irq_chip xen_pirq_chip __read_mostly = {
1437 .name = "xen-pirq",
1438
1439 .startup = startup_pirq,
1440 .shutdown = shutdown_pirq,
1441
1442 .enable = enable_pirq,
1443 .unmask = enable_pirq,
1444
1445 .disable = disable_pirq,
1446 .mask = disable_pirq,
1447
1448 .ack = ack_pirq,
1449 .end = end_pirq,
1450
1451 .set_affinity = set_affinity_irq,
1452
1453 .retrigger = retrigger_dynirq,
1454};
1455
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001456static struct irq_chip xen_percpu_chip __read_mostly = {
1457 .name = "xen-percpu",
1458
1459 .disable = disable_dynirq,
1460 .mask = disable_dynirq,
1461 .unmask = enable_dynirq,
1462
1463 .ack = ack_dynirq,
1464};
1465
Sheng Yang38e20b02010-05-14 12:40:51 +01001466int xen_set_callback_via(uint64_t via)
1467{
1468 struct xen_hvm_param a;
1469 a.domid = DOMID_SELF;
1470 a.index = HVM_PARAM_CALLBACK_IRQ;
1471 a.value = via;
1472 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1473}
1474EXPORT_SYMBOL_GPL(xen_set_callback_via);
1475
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001476#ifdef CONFIG_XEN_PVHVM
Sheng Yang38e20b02010-05-14 12:40:51 +01001477/* Vector callbacks are better than PCI interrupts to receive event
1478 * channel notifications because we can receive vector callbacks on any
1479 * vcpu and we don't need PCI support or APIC interactions. */
1480void xen_callback_vector(void)
1481{
1482 int rc;
1483 uint64_t callback_via;
1484 if (xen_have_vector_callback) {
1485 callback_via = HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK);
1486 rc = xen_set_callback_via(callback_via);
1487 if (rc) {
1488 printk(KERN_ERR "Request for Xen HVM callback vector"
1489 " failed.\n");
1490 xen_have_vector_callback = 0;
1491 return;
1492 }
1493 printk(KERN_INFO "Xen HVM callback vector for event delivery is "
1494 "enabled\n");
1495 /* in the restore case the vector has already been allocated */
1496 if (!test_bit(XEN_HVM_EVTCHN_CALLBACK, used_vectors))
1497 alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK, xen_hvm_callback_vector);
1498 }
1499}
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001500#else
1501void xen_callback_vector(void) {}
1502#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001503
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001504void __init xen_init_IRQ(void)
1505{
Stefano Stabellini01557ba2010-08-20 14:46:52 +01001506 int i, rc;
1507 struct physdev_nr_pirqs op_nr_pirqs;
Mike Travisc7a35892009-01-10 21:58:11 -08001508
Pekka Enberga70c3522009-07-01 11:51:18 +03001509 cpu_evtchn_mask_p = kcalloc(nr_cpu_ids, sizeof(struct cpu_evtchn_s),
1510 GFP_KERNEL);
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001511 irq_info = kcalloc(nr_irqs, sizeof(*irq_info), GFP_KERNEL);
1512
Stefano Stabellini01557ba2010-08-20 14:46:52 +01001513 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_nr_pirqs, &op_nr_pirqs);
1514 if (rc < 0) {
1515 nr_pirqs = nr_irqs;
1516 if (rc != -ENOSYS)
1517 printk(KERN_WARNING "PHYSDEVOP_get_nr_pirqs returned rc=%d\n", rc);
1518 } else {
1519 if (xen_pv_domain() && !xen_initial_domain())
1520 nr_pirqs = max((int)op_nr_pirqs.nr_pirqs, nr_irqs);
1521 else
1522 nr_pirqs = op_nr_pirqs.nr_pirqs;
1523 }
1524 pirq_to_irq = kcalloc(nr_pirqs, sizeof(*pirq_to_irq), GFP_KERNEL);
1525 for (i = 0; i < nr_pirqs; i++)
Stefano Stabellini7a043f12010-07-01 17:08:14 +01001526 pirq_to_irq[i] = -1;
1527
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001528 evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
1529 GFP_KERNEL);
1530 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1531 evtchn_to_irq[i] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001532
1533 init_evtchn_cpu_bindings();
1534
1535 /* No event channels are 'live' right now. */
1536 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1537 mask_evtchn(i);
1538
Sheng Yang38e20b02010-05-14 12:40:51 +01001539 if (xen_hvm_domain()) {
1540 xen_callback_vector();
1541 native_init_IRQ();
Stefano Stabellini3942b742010-06-24 17:50:18 +01001542 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1543 * __acpi_register_gsi can point at the right function */
1544 pci_xen_hvm_init();
Sheng Yang38e20b02010-05-14 12:40:51 +01001545 } else {
1546 irq_ctx_init(smp_processor_id());
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +01001547 if (xen_initial_domain())
1548 xen_setup_pirqs();
Sheng Yang38e20b02010-05-14 12:40:51 +01001549 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001550}