blob: b0cf80bf4169d71af6c732948ea44f4baafcc813 [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.
19 * 4. Hardware interrupts. Not supported at present.
20 *
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 Fitzhardingee46cdb62007-07-17 18:37:05 -070031
Sheng Yang38e20b02010-05-14 12:40:51 +010032#include <asm/desc.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070033#include <asm/ptrace.h>
34#include <asm/irq.h>
Jeremy Fitzhardinge792dc4f2009-02-06 14:09:43 -080035#include <asm/idle.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070036#include <asm/sync_bitops.h>
37#include <asm/xen/hypercall.h>
Adrian Bunk8d1b8752007-07-20 00:31:44 -070038#include <asm/xen/hypervisor.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070039
Sheng Yang38e20b02010-05-14 12:40:51 +010040#include <xen/xen.h>
41#include <xen/hvm.h>
Isaku Yamahatae04d0d02008-04-02 10:53:55 -070042#include <xen/xen-ops.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070043#include <xen/events.h>
44#include <xen/interface/xen.h>
45#include <xen/interface/event_channel.h>
Sheng Yang38e20b02010-05-14 12:40:51 +010046#include <xen/interface/hvm/hvm_op.h>
47#include <xen/interface/hvm/params.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070048
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070049/*
50 * This lock protects updates to the following mapping and reference-count
51 * arrays. The lock does not need to be acquired to read the mapping tables.
52 */
53static DEFINE_SPINLOCK(irq_mapping_update_lock);
54
55/* IRQ <-> VIRQ mapping. */
Tejun Heo204fba42009-06-24 15:13:45 +090056static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070057
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070058/* IRQ <-> IPI mapping */
Tejun Heo204fba42009-06-24 15:13:45 +090059static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070060
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080061/* Interrupt types. */
62enum xen_irq_type {
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -080063 IRQT_UNBOUND = 0,
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070064 IRQT_PIRQ,
65 IRQT_VIRQ,
66 IRQT_IPI,
67 IRQT_EVTCHN
68};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070069
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080070/*
71 * Packed IRQ information:
72 * type - enum xen_irq_type
73 * event channel - irq->event channel mapping
74 * cpu - cpu this event channel is bound to
75 * index - type-specific information:
76 * PIRQ - vector, with MSB being "needs EIO"
77 * VIRQ - virq number
78 * IPI - IPI vector
79 * EVTCHN -
80 */
81struct irq_info
82{
83 enum xen_irq_type type; /* type */
84 unsigned short evtchn; /* event channel */
85 unsigned short cpu; /* cpu bound */
86
87 union {
88 unsigned short virq;
89 enum ipi_vector ipi;
90 struct {
91 unsigned short gsi;
92 unsigned short vector;
93 } pirq;
94 } u;
95};
96
97static struct irq_info irq_info[NR_IRQS];
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070098
99static int evtchn_to_irq[NR_EVENT_CHANNELS] = {
100 [0 ... NR_EVENT_CHANNELS-1] = -1
101};
Mike Travisc7a35892009-01-10 21:58:11 -0800102struct cpu_evtchn_s {
103 unsigned long bits[NR_EVENT_CHANNELS/BITS_PER_LONG];
104};
105static struct cpu_evtchn_s *cpu_evtchn_mask_p;
106static inline unsigned long *cpu_evtchn_mask(int cpu)
107{
108 return cpu_evtchn_mask_p[cpu].bits;
109}
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700110
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700111/* Xen will never allocate port zero for any purpose. */
112#define VALID_EVTCHN(chn) ((chn) != 0)
113
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700114static struct irq_chip xen_dynamic_chip;
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700115static struct irq_chip xen_percpu_chip;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700116
117/* Constructor for packed IRQ information. */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800118static struct irq_info mk_unbound_info(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700119{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800120 return (struct irq_info) { .type = IRQT_UNBOUND };
121}
122
123static struct irq_info mk_evtchn_info(unsigned short evtchn)
124{
Ian Campbell90af9512009-02-06 16:55:58 -0800125 return (struct irq_info) { .type = IRQT_EVTCHN, .evtchn = evtchn,
126 .cpu = 0 };
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800127}
128
129static struct irq_info mk_ipi_info(unsigned short evtchn, enum ipi_vector ipi)
130{
131 return (struct irq_info) { .type = IRQT_IPI, .evtchn = evtchn,
Ian Campbell90af9512009-02-06 16:55:58 -0800132 .cpu = 0, .u.ipi = ipi };
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800133}
134
135static struct irq_info mk_virq_info(unsigned short evtchn, unsigned short virq)
136{
137 return (struct irq_info) { .type = IRQT_VIRQ, .evtchn = evtchn,
Ian Campbell90af9512009-02-06 16:55:58 -0800138 .cpu = 0, .u.virq = virq };
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800139}
140
141static struct irq_info mk_pirq_info(unsigned short evtchn,
142 unsigned short gsi, unsigned short vector)
143{
144 return (struct irq_info) { .type = IRQT_PIRQ, .evtchn = evtchn,
Ian Campbell90af9512009-02-06 16:55:58 -0800145 .cpu = 0, .u.pirq = { .gsi = gsi, .vector = vector } };
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700146}
147
148/*
149 * Accessors for packed IRQ information.
150 */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800151static struct irq_info *info_for_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700152{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800153 return &irq_info[irq];
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700154}
155
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800156static unsigned int evtchn_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700157{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800158 return info_for_irq(irq)->evtchn;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700159}
160
Ian Campbelld4c04532009-02-06 19:20:31 -0800161unsigned irq_from_evtchn(unsigned int evtchn)
162{
163 return evtchn_to_irq[evtchn];
164}
165EXPORT_SYMBOL_GPL(irq_from_evtchn);
166
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800167static enum ipi_vector ipi_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700168{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800169 struct irq_info *info = info_for_irq(irq);
170
171 BUG_ON(info == NULL);
172 BUG_ON(info->type != IRQT_IPI);
173
174 return info->u.ipi;
175}
176
177static unsigned virq_from_irq(unsigned irq)
178{
179 struct irq_info *info = info_for_irq(irq);
180
181 BUG_ON(info == NULL);
182 BUG_ON(info->type != IRQT_VIRQ);
183
184 return info->u.virq;
185}
186
187static unsigned gsi_from_irq(unsigned irq)
188{
189 struct irq_info *info = info_for_irq(irq);
190
191 BUG_ON(info == NULL);
192 BUG_ON(info->type != IRQT_PIRQ);
193
194 return info->u.pirq.gsi;
195}
196
197static unsigned vector_from_irq(unsigned irq)
198{
199 struct irq_info *info = info_for_irq(irq);
200
201 BUG_ON(info == NULL);
202 BUG_ON(info->type != IRQT_PIRQ);
203
204 return info->u.pirq.vector;
205}
206
207static enum xen_irq_type type_from_irq(unsigned irq)
208{
209 return info_for_irq(irq)->type;
210}
211
212static unsigned cpu_from_irq(unsigned irq)
213{
214 return info_for_irq(irq)->cpu;
215}
216
217static unsigned int cpu_from_evtchn(unsigned int evtchn)
218{
219 int irq = evtchn_to_irq[evtchn];
220 unsigned ret = 0;
221
222 if (irq != -1)
223 ret = cpu_from_irq(irq);
224
225 return ret;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700226}
227
228static inline unsigned long active_evtchns(unsigned int cpu,
229 struct shared_info *sh,
230 unsigned int idx)
231{
232 return (sh->evtchn_pending[idx] &
Mike Travisc7a35892009-01-10 21:58:11 -0800233 cpu_evtchn_mask(cpu)[idx] &
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700234 ~sh->evtchn_mask[idx]);
235}
236
237static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
238{
239 int irq = evtchn_to_irq[chn];
240
241 BUG_ON(irq == -1);
242#ifdef CONFIG_SMP
Mike Travis7f7ace02009-01-10 21:58:08 -0800243 cpumask_copy(irq_to_desc(irq)->affinity, cpumask_of(cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700244#endif
245
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800246 __clear_bit(chn, cpu_evtchn_mask(cpu_from_irq(irq)));
Mike Travisc7a35892009-01-10 21:58:11 -0800247 __set_bit(chn, cpu_evtchn_mask(cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700248
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800249 irq_info[irq].cpu = cpu;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700250}
251
252static void init_evtchn_cpu_bindings(void)
253{
254#ifdef CONFIG_SMP
Thomas Gleixner10e58082008-10-16 14:19:04 +0200255 struct irq_desc *desc;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700256 int i;
Thomas Gleixner10e58082008-10-16 14:19:04 +0200257
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700258 /* By default all event channels notify CPU#0. */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800259 for_each_irq_desc(i, desc) {
Mike Travis7f7ace02009-01-10 21:58:08 -0800260 cpumask_copy(desc->affinity, cpumask_of(0));
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800261 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700262#endif
263
Ian Campbellb0097ad2010-10-08 16:59:12 +0100264 memset(cpu_evtchn_mask(0), ~0, sizeof(struct cpu_evtchn_s));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700265}
266
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700267static inline void clear_evtchn(int port)
268{
269 struct shared_info *s = HYPERVISOR_shared_info;
270 sync_clear_bit(port, &s->evtchn_pending[0]);
271}
272
273static inline void set_evtchn(int port)
274{
275 struct shared_info *s = HYPERVISOR_shared_info;
276 sync_set_bit(port, &s->evtchn_pending[0]);
277}
278
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700279static inline int test_evtchn(int port)
280{
281 struct shared_info *s = HYPERVISOR_shared_info;
282 return sync_test_bit(port, &s->evtchn_pending[0]);
283}
284
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700285
286/**
287 * notify_remote_via_irq - send event to remote end of event channel via irq
288 * @irq: irq of event channel to send event to
289 *
290 * Unlike notify_remote_via_evtchn(), this is safe to use across
291 * save/restore. Notifications on a broken connection are silently
292 * dropped.
293 */
294void notify_remote_via_irq(int irq)
295{
296 int evtchn = evtchn_from_irq(irq);
297
298 if (VALID_EVTCHN(evtchn))
299 notify_remote_via_evtchn(evtchn);
300}
301EXPORT_SYMBOL_GPL(notify_remote_via_irq);
302
303static void mask_evtchn(int port)
304{
305 struct shared_info *s = HYPERVISOR_shared_info;
306 sync_set_bit(port, &s->evtchn_mask[0]);
307}
308
309static void unmask_evtchn(int port)
310{
311 struct shared_info *s = HYPERVISOR_shared_info;
312 unsigned int cpu = get_cpu();
313
314 BUG_ON(!irqs_disabled());
315
316 /* Slow path (hypercall) if this is a non-local port. */
317 if (unlikely(cpu != cpu_from_evtchn(port))) {
318 struct evtchn_unmask unmask = { .port = port };
319 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
320 } else {
321 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
322
323 sync_clear_bit(port, &s->evtchn_mask[0]);
324
325 /*
326 * The following is basically the equivalent of
327 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
328 * the interrupt edge' if the channel is masked.
329 */
330 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
331 !sync_test_and_set_bit(port / BITS_PER_LONG,
332 &vcpu_info->evtchn_pending_sel))
333 vcpu_info->evtchn_upcall_pending = 1;
334 }
335
336 put_cpu();
337}
338
339static int find_unbound_irq(void)
340{
341 int irq;
Jeremy Fitzhardinge6f8a0ed2008-12-17 13:42:29 -0800342 struct irq_desc *desc;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700343
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100344 for (irq = 0; irq < nr_irqs; irq++) {
345 desc = irq_to_desc(irq);
346 /* only 0->15 have init'd desc; handle irq > 16 */
347 if (desc == NULL)
348 break;
349 if (desc->chip == &no_irq_chip)
350 break;
351 if (desc->chip != &xen_dynamic_chip)
352 continue;
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800353 if (irq_info[irq].type == IRQT_UNBOUND)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700354 break;
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100355 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700356
Yinghai Lu5a15d7e2008-08-19 20:49:57 -0700357 if (irq == nr_irqs)
358 panic("No available IRQ to bind to: increase nr_irqs!\n");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700359
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700360 desc = irq_to_desc_alloc_node(irq, 0);
Jeremy Fitzhardinge6f8a0ed2008-12-17 13:42:29 -0800361 if (WARN_ON(desc == NULL))
362 return -1;
363
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100364 dynamic_irq_init_keep_chip_data(irq);
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800365
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700366 return irq;
367}
368
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700369int bind_evtchn_to_irq(unsigned int evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700370{
371 int irq;
372
373 spin_lock(&irq_mapping_update_lock);
374
375 irq = evtchn_to_irq[evtchn];
376
377 if (irq == -1) {
378 irq = find_unbound_irq();
379
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700380 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -0700381 handle_fasteoi_irq, "event");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700382
383 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800384 irq_info[irq] = mk_evtchn_info(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700385 }
386
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700387 spin_unlock(&irq_mapping_update_lock);
388
389 return irq;
390}
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700391EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700392
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700393static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
394{
395 struct evtchn_bind_ipi bind_ipi;
396 int evtchn, irq;
397
398 spin_lock(&irq_mapping_update_lock);
399
400 irq = per_cpu(ipi_to_irq, cpu)[ipi];
Ian Campbell90af9512009-02-06 16:55:58 -0800401
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700402 if (irq == -1) {
403 irq = find_unbound_irq();
404 if (irq < 0)
405 goto out;
406
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700407 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
408 handle_percpu_irq, "ipi");
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700409
410 bind_ipi.vcpu = cpu;
411 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
412 &bind_ipi) != 0)
413 BUG();
414 evtchn = bind_ipi.port;
415
416 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800417 irq_info[irq] = mk_ipi_info(evtchn, ipi);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700418 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
419
420 bind_evtchn_to_cpu(evtchn, cpu);
421 }
422
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700423 out:
424 spin_unlock(&irq_mapping_update_lock);
425 return irq;
426}
427
428
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700429static int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
430{
431 struct evtchn_bind_virq bind_virq;
432 int evtchn, irq;
433
434 spin_lock(&irq_mapping_update_lock);
435
436 irq = per_cpu(virq_to_irq, cpu)[virq];
437
438 if (irq == -1) {
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700439 irq = find_unbound_irq();
440
441 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
442 handle_percpu_irq, "virq");
443
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700444 bind_virq.virq = virq;
445 bind_virq.vcpu = cpu;
446 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
447 &bind_virq) != 0)
448 BUG();
449 evtchn = bind_virq.port;
450
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700451 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800452 irq_info[irq] = mk_virq_info(evtchn, virq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700453
454 per_cpu(virq_to_irq, cpu)[virq] = irq;
455
456 bind_evtchn_to_cpu(evtchn, cpu);
457 }
458
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700459 spin_unlock(&irq_mapping_update_lock);
460
461 return irq;
462}
463
464static void unbind_from_irq(unsigned int irq)
465{
466 struct evtchn_close close;
467 int evtchn = evtchn_from_irq(irq);
468
469 spin_lock(&irq_mapping_update_lock);
470
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800471 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700472 close.port = evtchn;
473 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
474 BUG();
475
476 switch (type_from_irq(irq)) {
477 case IRQT_VIRQ:
478 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800479 [virq_from_irq(irq)] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700480 break;
Alex Nixond68d82a2008-08-22 11:52:15 +0100481 case IRQT_IPI:
482 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800483 [ipi_from_irq(irq)] = -1;
Alex Nixond68d82a2008-08-22 11:52:15 +0100484 break;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700485 default:
486 break;
487 }
488
489 /* Closed ports are implicitly re-bound to VCPU0. */
490 bind_evtchn_to_cpu(evtchn, 0);
491
492 evtchn_to_irq[evtchn] = -1;
Ian Campbellfed5ea82009-12-01 16:15:30 +0000493 }
494
495 if (irq_info[irq].type != IRQT_UNBOUND) {
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800496 irq_info[irq] = mk_unbound_info();
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700497
Jeremy Fitzhardinge0f2287a2008-05-26 23:31:24 +0100498 dynamic_irq_cleanup(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700499 }
500
501 spin_unlock(&irq_mapping_update_lock);
502}
503
504int bind_evtchn_to_irqhandler(unsigned int evtchn,
Jeff Garzik7c239972007-10-19 03:12:20 -0400505 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700506 unsigned long irqflags,
507 const char *devname, void *dev_id)
508{
509 unsigned int irq;
510 int retval;
511
512 irq = bind_evtchn_to_irq(evtchn);
513 retval = request_irq(irq, handler, irqflags, devname, dev_id);
514 if (retval != 0) {
515 unbind_from_irq(irq);
516 return retval;
517 }
518
519 return irq;
520}
521EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
522
523int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
Jeff Garzik7c239972007-10-19 03:12:20 -0400524 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700525 unsigned long irqflags, const char *devname, void *dev_id)
526{
527 unsigned int irq;
528 int retval;
529
530 irq = bind_virq_to_irq(virq, cpu);
531 retval = request_irq(irq, handler, irqflags, devname, dev_id);
532 if (retval != 0) {
533 unbind_from_irq(irq);
534 return retval;
535 }
536
537 return irq;
538}
539EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
540
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700541int bind_ipi_to_irqhandler(enum ipi_vector ipi,
542 unsigned int cpu,
543 irq_handler_t handler,
544 unsigned long irqflags,
545 const char *devname,
546 void *dev_id)
547{
548 int irq, retval;
549
550 irq = bind_ipi_to_irq(ipi, cpu);
551 if (irq < 0)
552 return irq;
553
Ian Campbell4877c732010-07-29 11:16:35 +0100554 irqflags |= IRQF_NO_SUSPEND;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700555 retval = request_irq(irq, handler, irqflags, devname, dev_id);
556 if (retval != 0) {
557 unbind_from_irq(irq);
558 return retval;
559 }
560
561 return irq;
562}
563
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700564void unbind_from_irqhandler(unsigned int irq, void *dev_id)
565{
566 free_irq(irq, dev_id);
567 unbind_from_irq(irq);
568}
569EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
570
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700571void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
572{
573 int irq = per_cpu(ipi_to_irq, cpu)[vector];
574 BUG_ON(irq < 0);
575 notify_remote_via_irq(irq);
576}
577
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700578irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
579{
580 struct shared_info *sh = HYPERVISOR_shared_info;
581 int cpu = smp_processor_id();
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100582 unsigned long *cpu_evtchn = cpu_evtchn_mask(cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700583 int i;
584 unsigned long flags;
585 static DEFINE_SPINLOCK(debug_lock);
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100586 struct vcpu_info *v;
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700587
588 spin_lock_irqsave(&debug_lock, flags);
589
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100590 printk("\nvcpu %d\n ", cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700591
592 for_each_online_cpu(i) {
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100593 int pending;
594 v = per_cpu(xen_vcpu, i);
595 pending = (get_irq_regs() && i == cpu)
596 ? xen_irqs_disabled(get_irq_regs())
597 : v->evtchn_upcall_mask;
598 printk("%d: masked=%d pending=%d event_sel %0*lx\n ", i,
599 pending, v->evtchn_upcall_pending,
600 (int)(sizeof(v->evtchn_pending_sel)*2),
601 v->evtchn_pending_sel);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700602 }
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100603 v = per_cpu(xen_vcpu, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700604
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100605 printk("\npending:\n ");
606 for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
607 printk("%0*lx%s", (int)sizeof(sh->evtchn_pending[0])*2,
608 sh->evtchn_pending[i],
609 i % 8 == 0 ? "\n " : " ");
610 printk("\nglobal mask:\n ");
611 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
612 printk("%0*lx%s",
613 (int)(sizeof(sh->evtchn_mask[0])*2),
614 sh->evtchn_mask[i],
615 i % 8 == 0 ? "\n " : " ");
616
617 printk("\nglobally unmasked:\n ");
618 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
619 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
620 sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
621 i % 8 == 0 ? "\n " : " ");
622
623 printk("\nlocal cpu%d mask:\n ", cpu);
624 for (i = (NR_EVENT_CHANNELS/BITS_PER_LONG)-1; i >= 0; i--)
625 printk("%0*lx%s", (int)(sizeof(cpu_evtchn[0])*2),
626 cpu_evtchn[i],
627 i % 8 == 0 ? "\n " : " ");
628
629 printk("\nlocally unmasked:\n ");
630 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) {
631 unsigned long pending = sh->evtchn_pending[i]
632 & ~sh->evtchn_mask[i]
633 & cpu_evtchn[i];
634 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
635 pending, i % 8 == 0 ? "\n " : " ");
636 }
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700637
638 printk("\npending list:\n");
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100639 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700640 if (sync_test_bit(i, sh->evtchn_pending)) {
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100641 int word_idx = i / BITS_PER_LONG;
642 printk(" %d: event %d -> irq %d%s%s%s\n",
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800643 cpu_from_evtchn(i), i,
Ian Campbellcb52e6d2010-10-15 11:52:46 +0100644 evtchn_to_irq[i],
645 sync_test_bit(word_idx, &v->evtchn_pending_sel)
646 ? "" : " l2-clear",
647 !sync_test_bit(i, sh->evtchn_mask)
648 ? "" : " globally-masked",
649 sync_test_bit(i, cpu_evtchn)
650 ? "" : " locally-masked");
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700651 }
652 }
653
654 spin_unlock_irqrestore(&debug_lock, flags);
655
656 return IRQ_HANDLED;
657}
658
Tejun Heo245b2e72009-06-24 15:13:48 +0900659static DEFINE_PER_CPU(unsigned, xed_nesting_count);
660
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700661/*
662 * Search the CPUs pending events bitmasks. For each one found, map
663 * the event number to an irq, and feed it into do_IRQ() for
664 * handling.
665 *
666 * Xen uses a two-level bitmap to speed searching. The first level is
667 * a bitset of words which contain pending event bits. The second
668 * level is a bitset of pending events themselves.
669 */
Sheng Yang38e20b02010-05-14 12:40:51 +0100670static void __xen_evtchn_do_upcall(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700671{
672 int cpu = get_cpu();
673 struct shared_info *s = HYPERVISOR_shared_info;
674 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700675 unsigned count;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700676
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700677 do {
678 unsigned long pending_words;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700679
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700680 vcpu_info->evtchn_upcall_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700681
Tejun Heo245b2e72009-06-24 15:13:48 +0900682 if (__get_cpu_var(xed_nesting_count)++)
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700683 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700684
Isaku Yamahatae849c3e2008-04-02 10:53:56 -0700685#ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
686 /* Clear master flag /before/ clearing selector flag. */
Isaku Yamahata6673cf62008-06-16 14:58:13 -0700687 wmb();
Isaku Yamahatae849c3e2008-04-02 10:53:56 -0700688#endif
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700689 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
690 while (pending_words != 0) {
691 unsigned long pending_bits;
692 int word_idx = __ffs(pending_words);
693 pending_words &= ~(1UL << word_idx);
694
695 while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
696 int bit_idx = __ffs(pending_bits);
697 int port = (word_idx * BITS_PER_LONG) + bit_idx;
698 int irq = evtchn_to_irq[port];
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -0800699 struct irq_desc *desc;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700700
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -0700701 mask_evtchn(port);
702 clear_evtchn(port);
703
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -0800704 if (irq != -1) {
705 desc = irq_to_desc(irq);
706 if (desc)
707 generic_handle_irq_desc(irq, desc);
708 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700709 }
710 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700711
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700712 BUG_ON(!irqs_disabled());
713
Tejun Heo245b2e72009-06-24 15:13:48 +0900714 count = __get_cpu_var(xed_nesting_count);
715 __get_cpu_var(xed_nesting_count) = 0;
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100716 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700717
718out:
Jeremy Fitzhardinge3445a8f2009-02-06 14:09:46 -0800719
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700720 put_cpu();
721}
722
Sheng Yang38e20b02010-05-14 12:40:51 +0100723void xen_evtchn_do_upcall(struct pt_regs *regs)
724{
725 struct pt_regs *old_regs = set_irq_regs(regs);
726
727 exit_idle();
728 irq_enter();
729
730 __xen_evtchn_do_upcall();
731
732 irq_exit();
733 set_irq_regs(old_regs);
734}
735
736void xen_hvm_evtchn_do_upcall(void)
737{
738 __xen_evtchn_do_upcall();
739}
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100740EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
Sheng Yang38e20b02010-05-14 12:40:51 +0100741
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +0100742/* Rebind a new event channel to an existing irq. */
743void rebind_evtchn_irq(int evtchn, int irq)
744{
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800745 struct irq_info *info = info_for_irq(irq);
746
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +0100747 /* Make sure the irq is masked, since the new event channel
748 will also be masked. */
749 disable_irq(irq);
750
751 spin_lock(&irq_mapping_update_lock);
752
753 /* After resume the irq<->evtchn mappings are all cleared out */
754 BUG_ON(evtchn_to_irq[evtchn] != -1);
755 /* Expect irq to have been bound before,
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800756 so there should be a proper type */
757 BUG_ON(info->type == IRQT_UNBOUND);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +0100758
759 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800760 irq_info[irq] = mk_evtchn_info(evtchn);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +0100761
762 spin_unlock(&irq_mapping_update_lock);
763
764 /* new event channels are always bound to cpu 0 */
Rusty Russell0de26522008-12-13 21:20:26 +1030765 irq_set_affinity(irq, cpumask_of(0));
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +0100766
767 /* Unmask the event channel. */
768 enable_irq(irq);
769}
770
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700771/* Rebind an evtchn so that it gets delivered to a specific cpu */
Yinghai Lud5dedd42009-04-27 17:59:21 -0700772static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700773{
774 struct evtchn_bind_vcpu bind_vcpu;
775 int evtchn = evtchn_from_irq(irq);
776
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100777 /* events delivered via platform PCI interrupts are always
778 * routed to vcpu 0 */
779 if (!VALID_EVTCHN(evtchn) ||
780 (xen_hvm_domain() && !xen_have_vector_callback))
Yinghai Lud5dedd42009-04-27 17:59:21 -0700781 return -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700782
783 /* Send future instances of this interrupt to other vcpu. */
784 bind_vcpu.port = evtchn;
785 bind_vcpu.vcpu = tcpu;
786
787 /*
788 * If this fails, it usually just indicates that we're dealing with a
789 * virq or IPI channel, which don't actually need to be rebound. Ignore
790 * it, but don't do the xenlinux-level rebind in that case.
791 */
792 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
793 bind_evtchn_to_cpu(evtchn, tcpu);
Yinghai Lud5dedd42009-04-27 17:59:21 -0700794
795 return 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700796}
797
Yinghai Lud5dedd42009-04-27 17:59:21 -0700798static int set_affinity_irq(unsigned irq, const struct cpumask *dest)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700799{
Rusty Russell0de26522008-12-13 21:20:26 +1030800 unsigned tcpu = cpumask_first(dest);
Yinghai Lud5dedd42009-04-27 17:59:21 -0700801
802 return rebind_irq_to_cpu(irq, tcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700803}
804
Isaku Yamahata642e0c82008-04-02 10:53:57 -0700805int resend_irq_on_evtchn(unsigned int irq)
806{
807 int masked, evtchn = evtchn_from_irq(irq);
808 struct shared_info *s = HYPERVISOR_shared_info;
809
810 if (!VALID_EVTCHN(evtchn))
811 return 1;
812
813 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
814 sync_set_bit(evtchn, s->evtchn_pending);
815 if (!masked)
816 unmask_evtchn(evtchn);
817
818 return 1;
819}
820
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700821static void enable_dynirq(unsigned int irq)
822{
823 int evtchn = evtchn_from_irq(irq);
824
825 if (VALID_EVTCHN(evtchn))
826 unmask_evtchn(evtchn);
827}
828
829static void disable_dynirq(unsigned int irq)
830{
831 int evtchn = evtchn_from_irq(irq);
832
833 if (VALID_EVTCHN(evtchn))
834 mask_evtchn(evtchn);
835}
836
837static void ack_dynirq(unsigned int irq)
838{
839 int evtchn = evtchn_from_irq(irq);
840
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -0700841 move_masked_irq(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700842
843 if (VALID_EVTCHN(evtchn))
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -0700844 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700845}
846
847static int retrigger_dynirq(unsigned int irq)
848{
849 int evtchn = evtchn_from_irq(irq);
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -0700850 struct shared_info *sh = HYPERVISOR_shared_info;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700851 int ret = 0;
852
853 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -0700854 int masked;
855
856 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
857 sync_set_bit(evtchn, sh->evtchn_pending);
858 if (!masked)
859 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700860 ret = 1;
861 }
862
863 return ret;
864}
865
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100866static void restore_cpu_virqs(unsigned int cpu)
867{
868 struct evtchn_bind_virq bind_virq;
869 int virq, irq, evtchn;
870
871 for (virq = 0; virq < NR_VIRQS; virq++) {
872 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
873 continue;
874
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800875 BUG_ON(virq_from_irq(irq) != virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100876
877 /* Get a new binding from Xen. */
878 bind_virq.virq = virq;
879 bind_virq.vcpu = cpu;
880 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
881 &bind_virq) != 0)
882 BUG();
883 evtchn = bind_virq.port;
884
885 /* Record the new mapping. */
886 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800887 irq_info[irq] = mk_virq_info(evtchn, virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100888 bind_evtchn_to_cpu(evtchn, cpu);
889
890 /* Ready for use. */
891 unmask_evtchn(evtchn);
892 }
893}
894
895static void restore_cpu_ipis(unsigned int cpu)
896{
897 struct evtchn_bind_ipi bind_ipi;
898 int ipi, irq, evtchn;
899
900 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
901 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
902 continue;
903
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800904 BUG_ON(ipi_from_irq(irq) != ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100905
906 /* Get a new binding from Xen. */
907 bind_ipi.vcpu = cpu;
908 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
909 &bind_ipi) != 0)
910 BUG();
911 evtchn = bind_ipi.port;
912
913 /* Record the new mapping. */
914 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800915 irq_info[irq] = mk_ipi_info(evtchn, ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100916 bind_evtchn_to_cpu(evtchn, cpu);
917
918 /* Ready for use. */
919 unmask_evtchn(evtchn);
920
921 }
922}
923
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -0700924/* Clear an irq's pending state, in preparation for polling on it */
925void xen_clear_irq_pending(int irq)
926{
927 int evtchn = evtchn_from_irq(irq);
928
929 if (VALID_EVTCHN(evtchn))
930 clear_evtchn(evtchn);
931}
932
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700933void xen_set_irq_pending(int irq)
934{
935 int evtchn = evtchn_from_irq(irq);
936
937 if (VALID_EVTCHN(evtchn))
938 set_evtchn(evtchn);
939}
940
941bool xen_test_irq_pending(int irq)
942{
943 int evtchn = evtchn_from_irq(irq);
944 bool ret = false;
945
946 if (VALID_EVTCHN(evtchn))
947 ret = test_evtchn(evtchn);
948
949 return ret;
950}
951
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -0700952/* Poll waiting for an irq to become pending. In the usual case, the
953 irq will be disabled so it won't deliver an interrupt. */
954void xen_poll_irq(int irq)
955{
956 evtchn_port_t evtchn = evtchn_from_irq(irq);
957
958 if (VALID_EVTCHN(evtchn)) {
959 struct sched_poll poll;
960
961 poll.nr_ports = 1;
962 poll.timeout = 0;
Isaku Yamahataff3c5362008-10-14 17:50:44 -0700963 set_xen_guest_handle(poll.ports, &evtchn);
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -0700964
965 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
966 BUG();
967 }
968}
969
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100970void xen_irq_resume(void)
971{
972 unsigned int cpu, irq, evtchn;
973
974 init_evtchn_cpu_bindings();
975
976 /* New event-channel space is not 'live' yet. */
977 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
978 mask_evtchn(evtchn);
979
980 /* No IRQ <-> event-channel mappings. */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800981 for (irq = 0; irq < nr_irqs; irq++)
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100982 irq_info[irq].evtchn = 0; /* zap event-channel binding */
983
984 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
985 evtchn_to_irq[evtchn] = -1;
986
987 for_each_possible_cpu(cpu) {
988 restore_cpu_virqs(cpu);
989 restore_cpu_ipis(cpu);
990 }
991}
992
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700993static struct irq_chip xen_dynamic_chip __read_mostly = {
994 .name = "xen-dyn",
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -0800995
996 .disable = disable_dynirq,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700997 .mask = disable_dynirq,
998 .unmask = enable_dynirq,
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -0800999
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -07001000 .eoi = ack_dynirq,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001001 .set_affinity = set_affinity_irq,
1002 .retrigger = retrigger_dynirq,
1003};
1004
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001005static struct irq_chip xen_percpu_chip __read_mostly = {
1006 .name = "xen-percpu",
1007
1008 .disable = disable_dynirq,
1009 .mask = disable_dynirq,
1010 .unmask = enable_dynirq,
1011
1012 .ack = ack_dynirq,
1013};
1014
Sheng Yang38e20b02010-05-14 12:40:51 +01001015int xen_set_callback_via(uint64_t via)
1016{
1017 struct xen_hvm_param a;
1018 a.domid = DOMID_SELF;
1019 a.index = HVM_PARAM_CALLBACK_IRQ;
1020 a.value = via;
1021 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1022}
1023EXPORT_SYMBOL_GPL(xen_set_callback_via);
1024
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001025#ifdef CONFIG_XEN_PVHVM
Sheng Yang38e20b02010-05-14 12:40:51 +01001026/* Vector callbacks are better than PCI interrupts to receive event
1027 * channel notifications because we can receive vector callbacks on any
1028 * vcpu and we don't need PCI support or APIC interactions. */
1029void xen_callback_vector(void)
1030{
1031 int rc;
1032 uint64_t callback_via;
1033 if (xen_have_vector_callback) {
1034 callback_via = HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK);
1035 rc = xen_set_callback_via(callback_via);
1036 if (rc) {
1037 printk(KERN_ERR "Request for Xen HVM callback vector"
1038 " failed.\n");
1039 xen_have_vector_callback = 0;
1040 return;
1041 }
1042 printk(KERN_INFO "Xen HVM callback vector for event delivery is "
1043 "enabled\n");
1044 /* in the restore case the vector has already been allocated */
1045 if (!test_bit(XEN_HVM_EVTCHN_CALLBACK, used_vectors))
1046 alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK, xen_hvm_callback_vector);
1047 }
1048}
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001049#else
1050void xen_callback_vector(void) {}
1051#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001052
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001053void __init xen_init_IRQ(void)
1054{
1055 int i;
Mike Travisc7a35892009-01-10 21:58:11 -08001056
Pekka Enberga70c3522009-07-01 11:51:18 +03001057 cpu_evtchn_mask_p = kcalloc(nr_cpu_ids, sizeof(struct cpu_evtchn_s),
1058 GFP_KERNEL);
Christophe Saout28e08862009-01-11 11:46:23 -08001059 BUG_ON(cpu_evtchn_mask_p == NULL);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001060
1061 init_evtchn_cpu_bindings();
1062
1063 /* No event channels are 'live' right now. */
1064 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1065 mask_evtchn(i);
1066
Sheng Yang38e20b02010-05-14 12:40:51 +01001067 if (xen_hvm_domain()) {
1068 xen_callback_vector();
1069 native_init_IRQ();
1070 } else {
1071 irq_ctx_init(smp_processor_id());
1072 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001073}