blob: 2811bb988ea0551b85bc4c77d15b8590382abdf4 [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 Fitzhardingee0419562010-11-16 14:56:47 -0800281 clear_bit(chn, cpu_evtchn_mask(cpu_from_irq(irq)));
282 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{
Jan Beulich1c6969e2010-11-16 14:55:33 -0800289 int i;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700290#ifdef CONFIG_SMP
Thomas Gleixner10e58082008-10-16 14:19:04 +0200291 struct irq_desc *desc;
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
Jan Beulich1c6969e2010-11-16 14:55:33 -0800299 for_each_possible_cpu(i)
300 memset(cpu_evtchn_mask(i),
301 (i == 0) ? ~0 : 0, sizeof(struct cpu_evtchn_s));
302
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700303}
304
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700305static inline void clear_evtchn(int port)
306{
307 struct shared_info *s = HYPERVISOR_shared_info;
308 sync_clear_bit(port, &s->evtchn_pending[0]);
309}
310
311static inline void set_evtchn(int port)
312{
313 struct shared_info *s = HYPERVISOR_shared_info;
314 sync_set_bit(port, &s->evtchn_pending[0]);
315}
316
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700317static inline int test_evtchn(int port)
318{
319 struct shared_info *s = HYPERVISOR_shared_info;
320 return sync_test_bit(port, &s->evtchn_pending[0]);
321}
322
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700323
324/**
325 * notify_remote_via_irq - send event to remote end of event channel via irq
326 * @irq: irq of event channel to send event to
327 *
328 * Unlike notify_remote_via_evtchn(), this is safe to use across
329 * save/restore. Notifications on a broken connection are silently
330 * dropped.
331 */
332void notify_remote_via_irq(int irq)
333{
334 int evtchn = evtchn_from_irq(irq);
335
336 if (VALID_EVTCHN(evtchn))
337 notify_remote_via_evtchn(evtchn);
338}
339EXPORT_SYMBOL_GPL(notify_remote_via_irq);
340
341static void mask_evtchn(int port)
342{
343 struct shared_info *s = HYPERVISOR_shared_info;
344 sync_set_bit(port, &s->evtchn_mask[0]);
345}
346
347static void unmask_evtchn(int port)
348{
349 struct shared_info *s = HYPERVISOR_shared_info;
350 unsigned int cpu = get_cpu();
351
352 BUG_ON(!irqs_disabled());
353
354 /* Slow path (hypercall) if this is a non-local port. */
355 if (unlikely(cpu != cpu_from_evtchn(port))) {
356 struct evtchn_unmask unmask = { .port = port };
357 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
358 } else {
359 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
360
361 sync_clear_bit(port, &s->evtchn_mask[0]);
362
363 /*
364 * The following is basically the equivalent of
365 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
366 * the interrupt edge' if the channel is masked.
367 */
368 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
369 !sync_test_and_set_bit(port / BITS_PER_LONG,
370 &vcpu_info->evtchn_pending_sel))
371 vcpu_info->evtchn_upcall_pending = 1;
372 }
373
374 put_cpu();
375}
376
Konrad Rzeszutek Wilk0794bfc2010-10-18 10:41:08 -0400377static int get_nr_hw_irqs(void)
378{
379 int ret = 1;
380
381#ifdef CONFIG_X86_IO_APIC
382 ret = get_nr_irqs_gsi();
383#endif
384
385 return ret;
386}
387
Stefano Stabellini01557ba2010-08-20 14:46:52 +0100388/* callers of this function should make sure that PHYSDEVOP_get_nr_pirqs
389 * succeeded otherwise nr_pirqs won't hold the right value */
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100390static int find_unbound_pirq(void)
391{
392 int i;
Stefano Stabellini01557ba2010-08-20 14:46:52 +0100393 for (i = nr_pirqs-1; i >= 0; i--) {
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100394 if (pirq_to_irq[i] < 0)
395 return i;
396 }
397 return -1;
398}
399
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700400static int find_unbound_irq(void)
401{
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200402 struct irq_data *data;
403 int irq, res;
Konrad Rzeszutek Wilk3a69e912010-10-18 10:49:10 -0400404 int start = get_nr_hw_irqs();
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700405
Konrad Rzeszutek Wilk3a69e912010-10-18 10:49:10 -0400406 if (start == nr_irqs)
407 goto no_irqs;
408
409 /* nr_irqs is a magic value. Must not use it.*/
410 for (irq = nr_irqs-1; irq > start; irq--) {
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200411 data = irq_get_irq_data(irq);
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100412 /* only 0->15 have init'd desc; handle irq > 16 */
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200413 if (!data)
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100414 break;
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200415 if (data->chip == &no_irq_chip)
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100416 break;
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200417 if (data->chip != &xen_dynamic_chip)
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100418 continue;
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800419 if (irq_info[irq].type == IRQT_UNBOUND)
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200420 return irq;
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100421 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700422
Konrad Rzeszutek Wilk3a69e912010-10-18 10:49:10 -0400423 if (irq == start)
424 goto no_irqs;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700425
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200426 res = irq_alloc_desc_at(irq, 0);
Jeremy Fitzhardinge6f8a0ed2008-12-17 13:42:29 -0800427
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200428 if (WARN_ON(res != irq))
429 return -1;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800430
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700431 return irq;
Konrad Rzeszutek Wilk3a69e912010-10-18 10:49:10 -0400432
433no_irqs:
434 panic("No available IRQ to bind to: increase nr_irqs!\n");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700435}
436
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400437static bool identity_mapped_irq(unsigned irq)
438{
Konrad Rzeszutek Wilk0794bfc2010-10-18 10:41:08 -0400439 /* identity map all the hardware irqs */
440 return irq < get_nr_hw_irqs();
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400441}
442
443static void pirq_unmask_notify(int irq)
444{
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100445 struct physdev_eoi eoi = { .irq = pirq_from_irq(irq) };
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400446
447 if (unlikely(pirq_needs_eoi(irq))) {
448 int rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
449 WARN_ON(rc);
450 }
451}
452
453static void pirq_query_unmask(int irq)
454{
455 struct physdev_irq_status_query irq_status;
456 struct irq_info *info = info_for_irq(irq);
457
458 BUG_ON(info->type != IRQT_PIRQ);
459
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100460 irq_status.irq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400461 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
462 irq_status.flags = 0;
463
464 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
465 if (irq_status.flags & XENIRQSTAT_needs_eoi)
466 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
467}
468
469static bool probing_irq(int irq)
470{
471 struct irq_desc *desc = irq_to_desc(irq);
472
473 return desc && desc->action == NULL;
474}
475
476static unsigned int startup_pirq(unsigned int irq)
477{
478 struct evtchn_bind_pirq bind_pirq;
479 struct irq_info *info = info_for_irq(irq);
480 int evtchn = evtchn_from_irq(irq);
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400481 int rc;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400482
483 BUG_ON(info->type != IRQT_PIRQ);
484
485 if (VALID_EVTCHN(evtchn))
486 goto out;
487
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100488 bind_pirq.pirq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400489 /* NB. We are happy to share unless we are probing. */
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400490 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
491 BIND_PIRQ__WILL_SHARE : 0;
492 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
493 if (rc != 0) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400494 if (!probing_irq(irq))
495 printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
496 irq);
497 return 0;
498 }
499 evtchn = bind_pirq.port;
500
501 pirq_query_unmask(irq);
502
503 evtchn_to_irq[evtchn] = irq;
504 bind_evtchn_to_cpu(evtchn, 0);
505 info->evtchn = evtchn;
506
507out:
508 unmask_evtchn(evtchn);
509 pirq_unmask_notify(irq);
510
511 return 0;
512}
513
514static void shutdown_pirq(unsigned int irq)
515{
516 struct evtchn_close close;
517 struct irq_info *info = info_for_irq(irq);
518 int evtchn = evtchn_from_irq(irq);
519
520 BUG_ON(info->type != IRQT_PIRQ);
521
522 if (!VALID_EVTCHN(evtchn))
523 return;
524
525 mask_evtchn(evtchn);
526
527 close.port = evtchn;
528 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
529 BUG();
530
531 bind_evtchn_to_cpu(evtchn, 0);
532 evtchn_to_irq[evtchn] = -1;
533 info->evtchn = 0;
534}
535
536static void enable_pirq(unsigned int irq)
537{
538 startup_pirq(irq);
539}
540
541static void disable_pirq(unsigned int irq)
542{
543}
544
545static void ack_pirq(unsigned int irq)
546{
547 int evtchn = evtchn_from_irq(irq);
548
549 move_native_irq(irq);
550
551 if (VALID_EVTCHN(evtchn)) {
552 mask_evtchn(evtchn);
553 clear_evtchn(evtchn);
554 }
555}
556
557static void end_pirq(unsigned int irq)
558{
559 int evtchn = evtchn_from_irq(irq);
560 struct irq_desc *desc = irq_to_desc(irq);
561
562 if (WARN_ON(!desc))
563 return;
564
565 if ((desc->status & (IRQ_DISABLED|IRQ_PENDING)) ==
566 (IRQ_DISABLED|IRQ_PENDING)) {
567 shutdown_pirq(irq);
568 } else if (VALID_EVTCHN(evtchn)) {
569 unmask_evtchn(evtchn);
570 pirq_unmask_notify(irq);
571 }
572}
573
574static int find_irq_by_gsi(unsigned gsi)
575{
576 int irq;
577
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -0400578 for (irq = 0; irq < nr_irqs; irq++) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400579 struct irq_info *info = info_for_irq(irq);
580
581 if (info == NULL || info->type != IRQT_PIRQ)
582 continue;
583
584 if (gsi_from_irq(irq) == gsi)
585 return irq;
586 }
587
588 return -1;
589}
590
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100591int xen_allocate_pirq(unsigned gsi, int shareable, char *name)
592{
593 return xen_map_pirq_gsi(gsi, gsi, shareable, name);
594}
595
596/* xen_map_pirq_gsi might allocate irqs from the top down, as a
Konrad Rzeszutek Wilk3a69e912010-10-18 10:49:10 -0400597 * consequence don't assume that the irq number returned has a low value
598 * or can be used as a pirq number unless you know otherwise.
599 *
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100600 * One notable exception is when xen_map_pirq_gsi is called passing an
Konrad Rzeszutek Wilk3a69e912010-10-18 10:49:10 -0400601 * hardware gsi as argument, in that case the irq number returned
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100602 * matches the gsi number passed as second argument.
603 *
604 * Note: We don't assign an event channel until the irq actually started
605 * up. Return an existing irq if we've already got one for the gsi.
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400606 */
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100607int xen_map_pirq_gsi(unsigned pirq, unsigned gsi, int shareable, char *name)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400608{
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100609 int irq = 0;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400610 struct physdev_irq irq_op;
611
612 spin_lock(&irq_mapping_update_lock);
613
Stefano Stabellini01557ba2010-08-20 14:46:52 +0100614 if ((pirq > nr_pirqs) || (gsi > nr_irqs)) {
615 printk(KERN_WARNING "xen_map_pirq_gsi: %s %s is incorrect!\n",
616 pirq > nr_pirqs ? "nr_pirqs" :"",
617 gsi > nr_irqs ? "nr_irqs" : "");
618 goto out;
619 }
620
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400621 irq = find_irq_by_gsi(gsi);
622 if (irq != -1) {
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100623 printk(KERN_INFO "xen_map_pirq_gsi: returning irq %d for gsi %u\n",
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400624 irq, gsi);
625 goto out; /* XXX need refcount? */
626 }
627
Alex Nixonb5401a92010-03-18 16:31:34 -0400628 /* If we are a PV guest, we don't have GSIs (no ACPI passed). Therefore
629 * we are using the !xen_initial_domain() to drop in the function.*/
Stefano Stabellini3942b742010-06-24 17:50:18 +0100630 if (identity_mapped_irq(gsi) || (!xen_initial_domain() &&
631 xen_pv_domain())) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400632 irq = gsi;
Konrad Rzeszutek Wilk2c52f8d2010-10-18 17:11:10 -0400633 irq_alloc_desc_at(irq, 0);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400634 } else
635 irq = find_unbound_irq();
636
637 set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
Gerd Hoffmann1a60d052010-10-04 13:42:27 -0400638 handle_level_irq, name);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400639
640 irq_op.irq = irq;
Alex Nixonb5401a92010-03-18 16:31:34 -0400641 irq_op.vector = 0;
642
643 /* Only the privileged domain can do this. For non-priv, the pcifront
644 * driver provides a PCI bus that does the call to do exactly
645 * this in the priv domain. */
646 if (xen_initial_domain() &&
647 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
Konrad Rzeszutek Wilk2c52f8d2010-10-18 17:11:10 -0400648 irq_free_desc(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400649 irq = -ENOSPC;
650 goto out;
651 }
652
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100653 irq_info[irq] = mk_pirq_info(0, pirq, gsi, irq_op.vector);
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400654 irq_info[irq].u.pirq.flags |= shareable ? PIRQ_SHAREABLE : 0;
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100655 pirq_to_irq[pirq] = irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400656
657out:
658 spin_unlock(&irq_mapping_update_lock);
659
660 return irq;
661}
662
Qing Hef731e3ef2010-10-11 15:30:09 +0100663#ifdef CONFIG_PCI_MSI
664#include <linux/msi.h>
665#include "../pci/msi.h"
666
Stefano Stabellini809f9262010-07-01 17:10:39 +0100667void xen_allocate_pirq_msi(char *name, int *irq, int *pirq)
668{
669 spin_lock(&irq_mapping_update_lock);
670
671 *irq = find_unbound_irq();
672 if (*irq == -1)
673 goto out;
674
675 *pirq = find_unbound_pirq();
676 if (*pirq == -1)
677 goto out;
678
679 set_irq_chip_and_handler_name(*irq, &xen_pirq_chip,
680 handle_level_irq, name);
681
682 irq_info[*irq] = mk_pirq_info(0, *pirq, 0, 0);
683 pirq_to_irq[*pirq] = *irq;
684
685out:
686 spin_unlock(&irq_mapping_update_lock);
687}
688
Qing Hef731e3ef2010-10-11 15:30:09 +0100689int xen_create_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc, int type)
690{
691 int irq = -1;
692 struct physdev_map_pirq map_irq;
693 int rc;
694 int pos;
695 u32 table_offset, bir;
696
697 memset(&map_irq, 0, sizeof(map_irq));
698 map_irq.domid = DOMID_SELF;
699 map_irq.type = MAP_PIRQ_TYPE_MSI;
700 map_irq.index = -1;
701 map_irq.pirq = -1;
702 map_irq.bus = dev->bus->number;
703 map_irq.devfn = dev->devfn;
704
705 if (type == PCI_CAP_ID_MSIX) {
706 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
707
708 pci_read_config_dword(dev, msix_table_offset_reg(pos),
709 &table_offset);
710 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
711
712 map_irq.table_base = pci_resource_start(dev, bir);
713 map_irq.entry_nr = msidesc->msi_attrib.entry_nr;
714 }
715
716 spin_lock(&irq_mapping_update_lock);
717
718 irq = find_unbound_irq();
719
720 if (irq == -1)
721 goto out;
722
723 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
724 if (rc) {
725 printk(KERN_WARNING "xen map irq failed %d\n", rc);
726
727 irq_free_desc(irq);
728
729 irq = -1;
730 goto out;
731 }
732 irq_info[irq] = mk_pirq_info(0, map_irq.pirq, 0, map_irq.index);
733
734 set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
735 handle_level_irq,
736 (type == PCI_CAP_ID_MSIX) ? "msi-x":"msi");
737
738out:
739 spin_unlock(&irq_mapping_update_lock);
740 return irq;
741}
742#endif
743
Alex Nixonb5401a92010-03-18 16:31:34 -0400744int xen_destroy_irq(int irq)
745{
746 struct irq_desc *desc;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100747 struct physdev_unmap_pirq unmap_irq;
748 struct irq_info *info = info_for_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400749 int rc = -ENOENT;
750
751 spin_lock(&irq_mapping_update_lock);
752
753 desc = irq_to_desc(irq);
754 if (!desc)
755 goto out;
756
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100757 if (xen_initial_domain()) {
Konrad Rzeszutek Wilk12334712010-11-19 11:27:09 -0500758 unmap_irq.pirq = info->u.pirq.pirq;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100759 unmap_irq.domid = DOMID_SELF;
760 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
761 if (rc) {
762 printk(KERN_WARNING "unmap irq failed %d\n", rc);
763 goto out;
764 }
765 }
Alex Nixonb5401a92010-03-18 16:31:34 -0400766 irq_info[irq] = mk_unbound_info();
767
Konrad Rzeszutek Wilk2c52f8d2010-10-18 17:11:10 -0400768 irq_free_desc(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400769
770out:
771 spin_unlock(&irq_mapping_update_lock);
772 return rc;
773}
774
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400775int xen_vector_from_irq(unsigned irq)
776{
777 return vector_from_irq(irq);
778}
779
780int xen_gsi_from_irq(unsigned irq)
781{
782 return gsi_from_irq(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700783}
784
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700785int bind_evtchn_to_irq(unsigned int evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700786{
787 int irq;
788
789 spin_lock(&irq_mapping_update_lock);
790
791 irq = evtchn_to_irq[evtchn];
792
793 if (irq == -1) {
794 irq = find_unbound_irq();
795
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700796 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -0700797 handle_fasteoi_irq, "event");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700798
799 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800800 irq_info[irq] = mk_evtchn_info(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700801 }
802
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700803 spin_unlock(&irq_mapping_update_lock);
804
805 return irq;
806}
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700807EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700808
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700809static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
810{
811 struct evtchn_bind_ipi bind_ipi;
812 int evtchn, irq;
813
814 spin_lock(&irq_mapping_update_lock);
815
816 irq = per_cpu(ipi_to_irq, cpu)[ipi];
Ian Campbell90af9512009-02-06 16:55:58 -0800817
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700818 if (irq == -1) {
819 irq = find_unbound_irq();
820 if (irq < 0)
821 goto out;
822
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700823 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
824 handle_percpu_irq, "ipi");
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700825
826 bind_ipi.vcpu = cpu;
827 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
828 &bind_ipi) != 0)
829 BUG();
830 evtchn = bind_ipi.port;
831
832 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800833 irq_info[irq] = mk_ipi_info(evtchn, ipi);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700834 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
835
836 bind_evtchn_to_cpu(evtchn, cpu);
837 }
838
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700839 out:
840 spin_unlock(&irq_mapping_update_lock);
841 return irq;
842}
843
844
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100845int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700846{
847 struct evtchn_bind_virq bind_virq;
848 int evtchn, irq;
849
850 spin_lock(&irq_mapping_update_lock);
851
852 irq = per_cpu(virq_to_irq, cpu)[virq];
853
854 if (irq == -1) {
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700855 irq = find_unbound_irq();
856
857 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
858 handle_percpu_irq, "virq");
859
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700860 bind_virq.virq = virq;
861 bind_virq.vcpu = cpu;
862 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
863 &bind_virq) != 0)
864 BUG();
865 evtchn = bind_virq.port;
866
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700867 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800868 irq_info[irq] = mk_virq_info(evtchn, virq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700869
870 per_cpu(virq_to_irq, cpu)[virq] = irq;
871
872 bind_evtchn_to_cpu(evtchn, cpu);
873 }
874
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700875 spin_unlock(&irq_mapping_update_lock);
876
877 return irq;
878}
879
880static void unbind_from_irq(unsigned int irq)
881{
882 struct evtchn_close close;
883 int evtchn = evtchn_from_irq(irq);
884
885 spin_lock(&irq_mapping_update_lock);
886
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800887 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700888 close.port = evtchn;
889 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
890 BUG();
891
892 switch (type_from_irq(irq)) {
893 case IRQT_VIRQ:
894 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800895 [virq_from_irq(irq)] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700896 break;
Alex Nixond68d82a2008-08-22 11:52:15 +0100897 case IRQT_IPI:
898 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800899 [ipi_from_irq(irq)] = -1;
Alex Nixond68d82a2008-08-22 11:52:15 +0100900 break;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700901 default:
902 break;
903 }
904
905 /* Closed ports are implicitly re-bound to VCPU0. */
906 bind_evtchn_to_cpu(evtchn, 0);
907
908 evtchn_to_irq[evtchn] = -1;
Ian Campbellfed5ea82009-12-01 16:15:30 +0000909 }
910
911 if (irq_info[irq].type != IRQT_UNBOUND) {
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800912 irq_info[irq] = mk_unbound_info();
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700913
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200914 irq_free_desc(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700915 }
916
917 spin_unlock(&irq_mapping_update_lock);
918}
919
920int bind_evtchn_to_irqhandler(unsigned int evtchn,
Jeff Garzik7c239972007-10-19 03:12:20 -0400921 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700922 unsigned long irqflags,
923 const char *devname, void *dev_id)
924{
925 unsigned int irq;
926 int retval;
927
928 irq = bind_evtchn_to_irq(evtchn);
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_evtchn_to_irqhandler);
938
939int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
Jeff Garzik7c239972007-10-19 03:12:20 -0400940 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700941 unsigned long irqflags, const char *devname, void *dev_id)
942{
943 unsigned int irq;
944 int retval;
945
946 irq = bind_virq_to_irq(virq, cpu);
947 retval = request_irq(irq, handler, irqflags, devname, dev_id);
948 if (retval != 0) {
949 unbind_from_irq(irq);
950 return retval;
951 }
952
953 return irq;
954}
955EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
956
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700957int bind_ipi_to_irqhandler(enum ipi_vector ipi,
958 unsigned int cpu,
959 irq_handler_t handler,
960 unsigned long irqflags,
961 const char *devname,
962 void *dev_id)
963{
964 int irq, retval;
965
966 irq = bind_ipi_to_irq(ipi, cpu);
967 if (irq < 0)
968 return irq;
969
Ian Campbell4877c732010-07-29 11:16:35 +0100970 irqflags |= IRQF_NO_SUSPEND;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700971 retval = request_irq(irq, handler, irqflags, devname, dev_id);
972 if (retval != 0) {
973 unbind_from_irq(irq);
974 return retval;
975 }
976
977 return irq;
978}
979
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700980void unbind_from_irqhandler(unsigned int irq, void *dev_id)
981{
982 free_irq(irq, dev_id);
983 unbind_from_irq(irq);
984}
985EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
986
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700987void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
988{
989 int irq = per_cpu(ipi_to_irq, cpu)[vector];
990 BUG_ON(irq < 0);
991 notify_remote_via_irq(irq);
992}
993
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700994irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
995{
996 struct shared_info *sh = HYPERVISOR_shared_info;
997 int cpu = smp_processor_id();
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100998 unsigned long *cpu_evtchn = cpu_evtchn_mask(cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700999 int i;
1000 unsigned long flags;
1001 static DEFINE_SPINLOCK(debug_lock);
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001002 struct vcpu_info *v;
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001003
1004 spin_lock_irqsave(&debug_lock, flags);
1005
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001006 printk("\nvcpu %d\n ", cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001007
1008 for_each_online_cpu(i) {
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001009 int pending;
1010 v = per_cpu(xen_vcpu, i);
1011 pending = (get_irq_regs() && i == cpu)
1012 ? xen_irqs_disabled(get_irq_regs())
1013 : v->evtchn_upcall_mask;
1014 printk("%d: masked=%d pending=%d event_sel %0*lx\n ", i,
1015 pending, v->evtchn_upcall_pending,
1016 (int)(sizeof(v->evtchn_pending_sel)*2),
1017 v->evtchn_pending_sel);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001018 }
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001019 v = per_cpu(xen_vcpu, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001020
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001021 printk("\npending:\n ");
1022 for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
1023 printk("%0*lx%s", (int)sizeof(sh->evtchn_pending[0])*2,
1024 sh->evtchn_pending[i],
1025 i % 8 == 0 ? "\n " : " ");
1026 printk("\nglobal mask:\n ");
1027 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1028 printk("%0*lx%s",
1029 (int)(sizeof(sh->evtchn_mask[0])*2),
1030 sh->evtchn_mask[i],
1031 i % 8 == 0 ? "\n " : " ");
1032
1033 printk("\nglobally unmasked:\n ");
1034 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1035 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
1036 sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
1037 i % 8 == 0 ? "\n " : " ");
1038
1039 printk("\nlocal cpu%d mask:\n ", cpu);
1040 for (i = (NR_EVENT_CHANNELS/BITS_PER_LONG)-1; i >= 0; i--)
1041 printk("%0*lx%s", (int)(sizeof(cpu_evtchn[0])*2),
1042 cpu_evtchn[i],
1043 i % 8 == 0 ? "\n " : " ");
1044
1045 printk("\nlocally unmasked:\n ");
1046 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) {
1047 unsigned long pending = sh->evtchn_pending[i]
1048 & ~sh->evtchn_mask[i]
1049 & cpu_evtchn[i];
1050 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
1051 pending, i % 8 == 0 ? "\n " : " ");
1052 }
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001053
1054 printk("\npending list:\n");
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001055 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001056 if (sync_test_bit(i, sh->evtchn_pending)) {
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001057 int word_idx = i / BITS_PER_LONG;
1058 printk(" %d: event %d -> irq %d%s%s%s\n",
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001059 cpu_from_evtchn(i), i,
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001060 evtchn_to_irq[i],
1061 sync_test_bit(word_idx, &v->evtchn_pending_sel)
1062 ? "" : " l2-clear",
1063 !sync_test_bit(i, sh->evtchn_mask)
1064 ? "" : " globally-masked",
1065 sync_test_bit(i, cpu_evtchn)
1066 ? "" : " locally-masked");
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001067 }
1068 }
1069
1070 spin_unlock_irqrestore(&debug_lock, flags);
1071
1072 return IRQ_HANDLED;
1073}
1074
Tejun Heo245b2e72009-06-24 15:13:48 +09001075static DEFINE_PER_CPU(unsigned, xed_nesting_count);
1076
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001077/*
1078 * Search the CPUs pending events bitmasks. For each one found, map
1079 * the event number to an irq, and feed it into do_IRQ() for
1080 * handling.
1081 *
1082 * Xen uses a two-level bitmap to speed searching. The first level is
1083 * a bitset of words which contain pending event bits. The second
1084 * level is a bitset of pending events themselves.
1085 */
Sheng Yang38e20b02010-05-14 12:40:51 +01001086static void __xen_evtchn_do_upcall(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001087{
1088 int cpu = get_cpu();
1089 struct shared_info *s = HYPERVISOR_shared_info;
1090 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001091 unsigned count;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001092
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001093 do {
1094 unsigned long pending_words;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001095
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001096 vcpu_info->evtchn_upcall_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001097
Tejun Heo245b2e72009-06-24 15:13:48 +09001098 if (__get_cpu_var(xed_nesting_count)++)
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001099 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001100
Isaku Yamahatae849c3e2008-04-02 10:53:56 -07001101#ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
1102 /* Clear master flag /before/ clearing selector flag. */
Isaku Yamahata6673cf62008-06-16 14:58:13 -07001103 wmb();
Isaku Yamahatae849c3e2008-04-02 10:53:56 -07001104#endif
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001105 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
1106 while (pending_words != 0) {
1107 unsigned long pending_bits;
1108 int word_idx = __ffs(pending_words);
1109 pending_words &= ~(1UL << word_idx);
1110
1111 while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
1112 int bit_idx = __ffs(pending_bits);
1113 int port = (word_idx * BITS_PER_LONG) + bit_idx;
1114 int irq = evtchn_to_irq[port];
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -08001115 struct irq_desc *desc;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001116
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -07001117 mask_evtchn(port);
1118 clear_evtchn(port);
1119
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -08001120 if (irq != -1) {
1121 desc = irq_to_desc(irq);
1122 if (desc)
1123 generic_handle_irq_desc(irq, desc);
1124 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001125 }
1126 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001127
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001128 BUG_ON(!irqs_disabled());
1129
Tejun Heo245b2e72009-06-24 15:13:48 +09001130 count = __get_cpu_var(xed_nesting_count);
1131 __get_cpu_var(xed_nesting_count) = 0;
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001132 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001133
1134out:
Jeremy Fitzhardinge3445a8f2009-02-06 14:09:46 -08001135
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001136 put_cpu();
1137}
1138
Sheng Yang38e20b02010-05-14 12:40:51 +01001139void xen_evtchn_do_upcall(struct pt_regs *regs)
1140{
1141 struct pt_regs *old_regs = set_irq_regs(regs);
1142
1143 exit_idle();
1144 irq_enter();
1145
1146 __xen_evtchn_do_upcall();
1147
1148 irq_exit();
1149 set_irq_regs(old_regs);
1150}
1151
1152void xen_hvm_evtchn_do_upcall(void)
1153{
1154 __xen_evtchn_do_upcall();
1155}
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001156EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
Sheng Yang38e20b02010-05-14 12:40:51 +01001157
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001158/* Rebind a new event channel to an existing irq. */
1159void rebind_evtchn_irq(int evtchn, int irq)
1160{
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001161 struct irq_info *info = info_for_irq(irq);
1162
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001163 /* Make sure the irq is masked, since the new event channel
1164 will also be masked. */
1165 disable_irq(irq);
1166
1167 spin_lock(&irq_mapping_update_lock);
1168
1169 /* After resume the irq<->evtchn mappings are all cleared out */
1170 BUG_ON(evtchn_to_irq[evtchn] != -1);
1171 /* Expect irq to have been bound before,
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001172 so there should be a proper type */
1173 BUG_ON(info->type == IRQT_UNBOUND);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001174
1175 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001176 irq_info[irq] = mk_evtchn_info(evtchn);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001177
1178 spin_unlock(&irq_mapping_update_lock);
1179
1180 /* new event channels are always bound to cpu 0 */
Rusty Russell0de26522008-12-13 21:20:26 +10301181 irq_set_affinity(irq, cpumask_of(0));
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001182
1183 /* Unmask the event channel. */
1184 enable_irq(irq);
1185}
1186
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001187/* Rebind an evtchn so that it gets delivered to a specific cpu */
Yinghai Lud5dedd42009-04-27 17:59:21 -07001188static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001189{
1190 struct evtchn_bind_vcpu bind_vcpu;
1191 int evtchn = evtchn_from_irq(irq);
1192
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001193 /* events delivered via platform PCI interrupts are always
1194 * routed to vcpu 0 */
1195 if (!VALID_EVTCHN(evtchn) ||
1196 (xen_hvm_domain() && !xen_have_vector_callback))
Yinghai Lud5dedd42009-04-27 17:59:21 -07001197 return -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001198
1199 /* Send future instances of this interrupt to other vcpu. */
1200 bind_vcpu.port = evtchn;
1201 bind_vcpu.vcpu = tcpu;
1202
1203 /*
1204 * If this fails, it usually just indicates that we're dealing with a
1205 * virq or IPI channel, which don't actually need to be rebound. Ignore
1206 * it, but don't do the xenlinux-level rebind in that case.
1207 */
1208 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1209 bind_evtchn_to_cpu(evtchn, tcpu);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001210
1211 return 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001212}
1213
Yinghai Lud5dedd42009-04-27 17:59:21 -07001214static int set_affinity_irq(unsigned irq, const struct cpumask *dest)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001215{
Rusty Russell0de26522008-12-13 21:20:26 +10301216 unsigned tcpu = cpumask_first(dest);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001217
1218 return rebind_irq_to_cpu(irq, tcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001219}
1220
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001221int resend_irq_on_evtchn(unsigned int irq)
1222{
1223 int masked, evtchn = evtchn_from_irq(irq);
1224 struct shared_info *s = HYPERVISOR_shared_info;
1225
1226 if (!VALID_EVTCHN(evtchn))
1227 return 1;
1228
1229 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
1230 sync_set_bit(evtchn, s->evtchn_pending);
1231 if (!masked)
1232 unmask_evtchn(evtchn);
1233
1234 return 1;
1235}
1236
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001237static void enable_dynirq(unsigned int irq)
1238{
1239 int evtchn = evtchn_from_irq(irq);
1240
1241 if (VALID_EVTCHN(evtchn))
1242 unmask_evtchn(evtchn);
1243}
1244
1245static void disable_dynirq(unsigned int irq)
1246{
1247 int evtchn = evtchn_from_irq(irq);
1248
1249 if (VALID_EVTCHN(evtchn))
1250 mask_evtchn(evtchn);
1251}
1252
1253static void ack_dynirq(unsigned int irq)
1254{
1255 int evtchn = evtchn_from_irq(irq);
1256
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -07001257 move_masked_irq(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001258
1259 if (VALID_EVTCHN(evtchn))
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -07001260 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001261}
1262
1263static int retrigger_dynirq(unsigned int irq)
1264{
1265 int evtchn = evtchn_from_irq(irq);
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001266 struct shared_info *sh = HYPERVISOR_shared_info;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001267 int ret = 0;
1268
1269 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001270 int masked;
1271
1272 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
1273 sync_set_bit(evtchn, sh->evtchn_pending);
1274 if (!masked)
1275 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001276 ret = 1;
1277 }
1278
1279 return ret;
1280}
1281
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001282static void restore_cpu_virqs(unsigned int cpu)
1283{
1284 struct evtchn_bind_virq bind_virq;
1285 int virq, irq, evtchn;
1286
1287 for (virq = 0; virq < NR_VIRQS; virq++) {
1288 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1289 continue;
1290
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001291 BUG_ON(virq_from_irq(irq) != virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001292
1293 /* Get a new binding from Xen. */
1294 bind_virq.virq = virq;
1295 bind_virq.vcpu = cpu;
1296 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1297 &bind_virq) != 0)
1298 BUG();
1299 evtchn = bind_virq.port;
1300
1301 /* Record the new mapping. */
1302 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001303 irq_info[irq] = mk_virq_info(evtchn, virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001304 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001305 }
1306}
1307
1308static void restore_cpu_ipis(unsigned int cpu)
1309{
1310 struct evtchn_bind_ipi bind_ipi;
1311 int ipi, irq, evtchn;
1312
1313 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1314 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1315 continue;
1316
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001317 BUG_ON(ipi_from_irq(irq) != ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001318
1319 /* Get a new binding from Xen. */
1320 bind_ipi.vcpu = cpu;
1321 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1322 &bind_ipi) != 0)
1323 BUG();
1324 evtchn = bind_ipi.port;
1325
1326 /* Record the new mapping. */
1327 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001328 irq_info[irq] = mk_ipi_info(evtchn, ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001329 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001330 }
1331}
1332
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001333/* Clear an irq's pending state, in preparation for polling on it */
1334void xen_clear_irq_pending(int irq)
1335{
1336 int evtchn = evtchn_from_irq(irq);
1337
1338 if (VALID_EVTCHN(evtchn))
1339 clear_evtchn(evtchn);
1340}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001341EXPORT_SYMBOL(xen_clear_irq_pending);
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -07001342void xen_set_irq_pending(int irq)
1343{
1344 int evtchn = evtchn_from_irq(irq);
1345
1346 if (VALID_EVTCHN(evtchn))
1347 set_evtchn(evtchn);
1348}
1349
1350bool xen_test_irq_pending(int irq)
1351{
1352 int evtchn = evtchn_from_irq(irq);
1353 bool ret = false;
1354
1355 if (VALID_EVTCHN(evtchn))
1356 ret = test_evtchn(evtchn);
1357
1358 return ret;
1359}
1360
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001361/* Poll waiting for an irq to become pending with timeout. In the usual case,
1362 * the irq will be disabled so it won't deliver an interrupt. */
1363void xen_poll_irq_timeout(int irq, u64 timeout)
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001364{
1365 evtchn_port_t evtchn = evtchn_from_irq(irq);
1366
1367 if (VALID_EVTCHN(evtchn)) {
1368 struct sched_poll poll;
1369
1370 poll.nr_ports = 1;
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001371 poll.timeout = timeout;
Isaku Yamahataff3c5362008-10-14 17:50:44 -07001372 set_xen_guest_handle(poll.ports, &evtchn);
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001373
1374 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1375 BUG();
1376 }
1377}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001378EXPORT_SYMBOL(xen_poll_irq_timeout);
1379/* Poll waiting for an irq to become pending. In the usual case, the
1380 * irq will be disabled so it won't deliver an interrupt. */
1381void xen_poll_irq(int irq)
1382{
1383 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1384}
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001385
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001386void xen_irq_resume(void)
1387{
1388 unsigned int cpu, irq, evtchn;
Ian Campbell69035912010-11-01 16:30:09 +00001389 struct irq_desc *desc;
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001390
1391 init_evtchn_cpu_bindings();
1392
1393 /* New event-channel space is not 'live' yet. */
1394 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1395 mask_evtchn(evtchn);
1396
1397 /* No IRQ <-> event-channel mappings. */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001398 for (irq = 0; irq < nr_irqs; irq++)
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001399 irq_info[irq].evtchn = 0; /* zap event-channel binding */
1400
1401 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1402 evtchn_to_irq[evtchn] = -1;
1403
1404 for_each_possible_cpu(cpu) {
1405 restore_cpu_virqs(cpu);
1406 restore_cpu_ipis(cpu);
1407 }
Ian Campbell69035912010-11-01 16:30:09 +00001408
1409 /*
1410 * Unmask any IRQF_NO_SUSPEND IRQs which are enabled. These
1411 * are not handled by the IRQ core.
1412 */
1413 for_each_irq_desc(irq, desc) {
1414 if (!desc->action || !(desc->action->flags & IRQF_NO_SUSPEND))
1415 continue;
1416 if (desc->status & IRQ_DISABLED)
1417 continue;
1418
1419 evtchn = evtchn_from_irq(irq);
1420 if (evtchn == -1)
1421 continue;
1422
1423 unmask_evtchn(evtchn);
1424 }
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001425}
1426
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001427static struct irq_chip xen_dynamic_chip __read_mostly = {
1428 .name = "xen-dyn",
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001429
1430 .disable = disable_dynirq,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001431 .mask = disable_dynirq,
1432 .unmask = enable_dynirq,
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001433
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -07001434 .eoi = ack_dynirq,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001435 .set_affinity = set_affinity_irq,
1436 .retrigger = retrigger_dynirq,
1437};
1438
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001439static struct irq_chip xen_pirq_chip __read_mostly = {
1440 .name = "xen-pirq",
1441
1442 .startup = startup_pirq,
1443 .shutdown = shutdown_pirq,
1444
1445 .enable = enable_pirq,
1446 .unmask = enable_pirq,
1447
1448 .disable = disable_pirq,
1449 .mask = disable_pirq,
1450
1451 .ack = ack_pirq,
1452 .end = end_pirq,
1453
1454 .set_affinity = set_affinity_irq,
1455
1456 .retrigger = retrigger_dynirq,
1457};
1458
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001459static struct irq_chip xen_percpu_chip __read_mostly = {
1460 .name = "xen-percpu",
1461
1462 .disable = disable_dynirq,
1463 .mask = disable_dynirq,
1464 .unmask = enable_dynirq,
1465
1466 .ack = ack_dynirq,
1467};
1468
Sheng Yang38e20b02010-05-14 12:40:51 +01001469int xen_set_callback_via(uint64_t via)
1470{
1471 struct xen_hvm_param a;
1472 a.domid = DOMID_SELF;
1473 a.index = HVM_PARAM_CALLBACK_IRQ;
1474 a.value = via;
1475 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1476}
1477EXPORT_SYMBOL_GPL(xen_set_callback_via);
1478
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001479#ifdef CONFIG_XEN_PVHVM
Sheng Yang38e20b02010-05-14 12:40:51 +01001480/* Vector callbacks are better than PCI interrupts to receive event
1481 * channel notifications because we can receive vector callbacks on any
1482 * vcpu and we don't need PCI support or APIC interactions. */
1483void xen_callback_vector(void)
1484{
1485 int rc;
1486 uint64_t callback_via;
1487 if (xen_have_vector_callback) {
1488 callback_via = HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK);
1489 rc = xen_set_callback_via(callback_via);
1490 if (rc) {
1491 printk(KERN_ERR "Request for Xen HVM callback vector"
1492 " failed.\n");
1493 xen_have_vector_callback = 0;
1494 return;
1495 }
1496 printk(KERN_INFO "Xen HVM callback vector for event delivery is "
1497 "enabled\n");
1498 /* in the restore case the vector has already been allocated */
1499 if (!test_bit(XEN_HVM_EVTCHN_CALLBACK, used_vectors))
1500 alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK, xen_hvm_callback_vector);
1501 }
1502}
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001503#else
1504void xen_callback_vector(void) {}
1505#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001506
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001507void __init xen_init_IRQ(void)
1508{
Stefano Stabellini01557ba2010-08-20 14:46:52 +01001509 int i, rc;
1510 struct physdev_nr_pirqs op_nr_pirqs;
Mike Travisc7a35892009-01-10 21:58:11 -08001511
Pekka Enberga70c3522009-07-01 11:51:18 +03001512 cpu_evtchn_mask_p = kcalloc(nr_cpu_ids, sizeof(struct cpu_evtchn_s),
1513 GFP_KERNEL);
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001514 irq_info = kcalloc(nr_irqs, sizeof(*irq_info), GFP_KERNEL);
1515
Stefano Stabellini01557ba2010-08-20 14:46:52 +01001516 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_nr_pirqs, &op_nr_pirqs);
1517 if (rc < 0) {
1518 nr_pirqs = nr_irqs;
1519 if (rc != -ENOSYS)
1520 printk(KERN_WARNING "PHYSDEVOP_get_nr_pirqs returned rc=%d\n", rc);
1521 } else {
1522 if (xen_pv_domain() && !xen_initial_domain())
1523 nr_pirqs = max((int)op_nr_pirqs.nr_pirqs, nr_irqs);
1524 else
1525 nr_pirqs = op_nr_pirqs.nr_pirqs;
1526 }
1527 pirq_to_irq = kcalloc(nr_pirqs, sizeof(*pirq_to_irq), GFP_KERNEL);
1528 for (i = 0; i < nr_pirqs; i++)
Stefano Stabellini7a043f12010-07-01 17:08:14 +01001529 pirq_to_irq[i] = -1;
1530
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001531 evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
1532 GFP_KERNEL);
1533 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1534 evtchn_to_irq[i] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001535
1536 init_evtchn_cpu_bindings();
1537
1538 /* No event channels are 'live' right now. */
1539 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1540 mask_evtchn(i);
1541
Sheng Yang38e20b02010-05-14 12:40:51 +01001542 if (xen_hvm_domain()) {
1543 xen_callback_vector();
1544 native_init_IRQ();
Stefano Stabellini3942b742010-06-24 17:50:18 +01001545 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1546 * __acpi_register_gsi can point at the right function */
1547 pci_xen_hvm_init();
Sheng Yang38e20b02010-05-14 12:40:51 +01001548 } else {
1549 irq_ctx_init(smp_processor_id());
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +01001550 if (xen_initial_domain())
1551 xen_setup_pirqs();
Sheng Yang38e20b02010-05-14 12:40:51 +01001552 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001553}