blob: 3df53de6b43ae7285f3ef39036cc4e166c5beb4a [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>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070032
Sheng Yang38e20b02010-05-14 12:40:51 +010033#include <asm/desc.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070034#include <asm/ptrace.h>
35#include <asm/irq.h>
Jeremy Fitzhardinge792dc4f2009-02-06 14:09:43 -080036#include <asm/idle.h>
Konrad Rzeszutek Wilk0794bfc2010-10-18 10:41:08 -040037#include <asm/io_apic.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070038#include <asm/sync_bitops.h>
39#include <asm/xen/hypercall.h>
Adrian Bunk8d1b8752007-07-20 00:31:44 -070040#include <asm/xen/hypervisor.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070041
Sheng Yang38e20b02010-05-14 12:40:51 +010042#include <xen/xen.h>
43#include <xen/hvm.h>
Isaku Yamahatae04d0d02008-04-02 10:53:55 -070044#include <xen/xen-ops.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070045#include <xen/events.h>
46#include <xen/interface/xen.h>
47#include <xen/interface/event_channel.h>
Sheng Yang38e20b02010-05-14 12:40:51 +010048#include <xen/interface/hvm/hvm_op.h>
49#include <xen/interface/hvm/params.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070050
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070051/*
52 * This lock protects updates to the following mapping and reference-count
53 * arrays. The lock does not need to be acquired to read the mapping tables.
54 */
55static DEFINE_SPINLOCK(irq_mapping_update_lock);
56
57/* IRQ <-> VIRQ mapping. */
Tejun Heo204fba42009-06-24 15:13:45 +090058static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070059
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070060/* IRQ <-> IPI mapping */
Tejun Heo204fba42009-06-24 15:13:45 +090061static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070062
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080063/* Interrupt types. */
64enum xen_irq_type {
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -080065 IRQT_UNBOUND = 0,
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070066 IRQT_PIRQ,
67 IRQT_VIRQ,
68 IRQT_IPI,
69 IRQT_EVTCHN
70};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070071
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080072/*
73 * Packed IRQ information:
74 * type - enum xen_irq_type
75 * event channel - irq->event channel mapping
76 * cpu - cpu this event channel is bound to
77 * index - type-specific information:
78 * PIRQ - vector, with MSB being "needs EIO"
79 * VIRQ - virq number
80 * IPI - IPI vector
81 * EVTCHN -
82 */
83struct irq_info
84{
85 enum xen_irq_type type; /* type */
86 unsigned short evtchn; /* event channel */
87 unsigned short cpu; /* cpu bound */
88
89 union {
90 unsigned short virq;
91 enum ipi_vector ipi;
92 struct {
93 unsigned short gsi;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -040094 unsigned char vector;
95 unsigned char flags;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080096 } pirq;
97 } u;
98};
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -040099#define PIRQ_NEEDS_EOI (1 << 0)
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400100#define PIRQ_SHAREABLE (1 << 1)
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800101
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -0400102static struct irq_info *irq_info;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700103
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -0400104static int *evtchn_to_irq;
Mike Travisc7a35892009-01-10 21:58:11 -0800105struct cpu_evtchn_s {
106 unsigned long bits[NR_EVENT_CHANNELS/BITS_PER_LONG];
107};
Jeremy Fitzhardinge3b32f572009-08-13 12:50:37 -0700108
109static __initdata struct cpu_evtchn_s init_evtchn_mask = {
110 .bits[0 ... (NR_EVENT_CHANNELS/BITS_PER_LONG)-1] = ~0ul,
111};
112static struct cpu_evtchn_s *cpu_evtchn_mask_p = &init_evtchn_mask;
113
Mike Travisc7a35892009-01-10 21:58:11 -0800114static inline unsigned long *cpu_evtchn_mask(int cpu)
115{
116 return cpu_evtchn_mask_p[cpu].bits;
117}
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700118
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700119/* Xen will never allocate port zero for any purpose. */
120#define VALID_EVTCHN(chn) ((chn) != 0)
121
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700122static struct irq_chip xen_dynamic_chip;
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700123static struct irq_chip xen_percpu_chip;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400124static struct irq_chip xen_pirq_chip;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700125
126/* Constructor for packed IRQ information. */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800127static struct irq_info mk_unbound_info(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700128{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800129 return (struct irq_info) { .type = IRQT_UNBOUND };
130}
131
132static struct irq_info mk_evtchn_info(unsigned short evtchn)
133{
Ian Campbell90af9512009-02-06 16:55:58 -0800134 return (struct irq_info) { .type = IRQT_EVTCHN, .evtchn = evtchn,
135 .cpu = 0 };
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800136}
137
138static struct irq_info mk_ipi_info(unsigned short evtchn, enum ipi_vector ipi)
139{
140 return (struct irq_info) { .type = IRQT_IPI, .evtchn = evtchn,
Ian Campbell90af9512009-02-06 16:55:58 -0800141 .cpu = 0, .u.ipi = ipi };
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800142}
143
144static struct irq_info mk_virq_info(unsigned short evtchn, unsigned short virq)
145{
146 return (struct irq_info) { .type = IRQT_VIRQ, .evtchn = evtchn,
Ian Campbell90af9512009-02-06 16:55:58 -0800147 .cpu = 0, .u.virq = virq };
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800148}
149
150static struct irq_info mk_pirq_info(unsigned short evtchn,
151 unsigned short gsi, unsigned short vector)
152{
153 return (struct irq_info) { .type = IRQT_PIRQ, .evtchn = evtchn,
Ian Campbell90af9512009-02-06 16:55:58 -0800154 .cpu = 0, .u.pirq = { .gsi = gsi, .vector = vector } };
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700155}
156
157/*
158 * Accessors for packed IRQ information.
159 */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800160static struct irq_info *info_for_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700161{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800162 return &irq_info[irq];
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700163}
164
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800165static unsigned int evtchn_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700166{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800167 return info_for_irq(irq)->evtchn;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700168}
169
Ian Campbelld4c04532009-02-06 19:20:31 -0800170unsigned irq_from_evtchn(unsigned int evtchn)
171{
172 return evtchn_to_irq[evtchn];
173}
174EXPORT_SYMBOL_GPL(irq_from_evtchn);
175
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800176static enum ipi_vector ipi_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700177{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800178 struct irq_info *info = info_for_irq(irq);
179
180 BUG_ON(info == NULL);
181 BUG_ON(info->type != IRQT_IPI);
182
183 return info->u.ipi;
184}
185
186static unsigned virq_from_irq(unsigned irq)
187{
188 struct irq_info *info = info_for_irq(irq);
189
190 BUG_ON(info == NULL);
191 BUG_ON(info->type != IRQT_VIRQ);
192
193 return info->u.virq;
194}
195
196static unsigned gsi_from_irq(unsigned irq)
197{
198 struct irq_info *info = info_for_irq(irq);
199
200 BUG_ON(info == NULL);
201 BUG_ON(info->type != IRQT_PIRQ);
202
203 return info->u.pirq.gsi;
204}
205
206static unsigned vector_from_irq(unsigned irq)
207{
208 struct irq_info *info = info_for_irq(irq);
209
210 BUG_ON(info == NULL);
211 BUG_ON(info->type != IRQT_PIRQ);
212
213 return info->u.pirq.vector;
214}
215
216static enum xen_irq_type type_from_irq(unsigned irq)
217{
218 return info_for_irq(irq)->type;
219}
220
221static unsigned cpu_from_irq(unsigned irq)
222{
223 return info_for_irq(irq)->cpu;
224}
225
226static unsigned int cpu_from_evtchn(unsigned int evtchn)
227{
228 int irq = evtchn_to_irq[evtchn];
229 unsigned ret = 0;
230
231 if (irq != -1)
232 ret = cpu_from_irq(irq);
233
234 return ret;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700235}
236
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400237static bool pirq_needs_eoi(unsigned irq)
238{
239 struct irq_info *info = info_for_irq(irq);
240
241 BUG_ON(info->type != IRQT_PIRQ);
242
243 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
244}
245
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700246static inline unsigned long active_evtchns(unsigned int cpu,
247 struct shared_info *sh,
248 unsigned int idx)
249{
250 return (sh->evtchn_pending[idx] &
Mike Travisc7a35892009-01-10 21:58:11 -0800251 cpu_evtchn_mask(cpu)[idx] &
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700252 ~sh->evtchn_mask[idx]);
253}
254
255static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
256{
257 int irq = evtchn_to_irq[chn];
258
259 BUG_ON(irq == -1);
260#ifdef CONFIG_SMP
Mike Travis7f7ace02009-01-10 21:58:08 -0800261 cpumask_copy(irq_to_desc(irq)->affinity, cpumask_of(cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700262#endif
263
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800264 __clear_bit(chn, cpu_evtchn_mask(cpu_from_irq(irq)));
Mike Travisc7a35892009-01-10 21:58:11 -0800265 __set_bit(chn, cpu_evtchn_mask(cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700266
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800267 irq_info[irq].cpu = cpu;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700268}
269
270static void init_evtchn_cpu_bindings(void)
271{
272#ifdef CONFIG_SMP
Thomas Gleixner10e58082008-10-16 14:19:04 +0200273 struct irq_desc *desc;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700274 int i;
Thomas Gleixner10e58082008-10-16 14:19:04 +0200275
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700276 /* By default all event channels notify CPU#0. */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800277 for_each_irq_desc(i, desc) {
Mike Travis7f7ace02009-01-10 21:58:08 -0800278 cpumask_copy(desc->affinity, cpumask_of(0));
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800279 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700280#endif
281
Mike Travisc7a35892009-01-10 21:58:11 -0800282 memset(cpu_evtchn_mask(0), ~0, sizeof(cpu_evtchn_mask(0)));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700283}
284
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700285static inline void clear_evtchn(int port)
286{
287 struct shared_info *s = HYPERVISOR_shared_info;
288 sync_clear_bit(port, &s->evtchn_pending[0]);
289}
290
291static inline void set_evtchn(int port)
292{
293 struct shared_info *s = HYPERVISOR_shared_info;
294 sync_set_bit(port, &s->evtchn_pending[0]);
295}
296
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700297static inline int test_evtchn(int port)
298{
299 struct shared_info *s = HYPERVISOR_shared_info;
300 return sync_test_bit(port, &s->evtchn_pending[0]);
301}
302
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700303
304/**
305 * notify_remote_via_irq - send event to remote end of event channel via irq
306 * @irq: irq of event channel to send event to
307 *
308 * Unlike notify_remote_via_evtchn(), this is safe to use across
309 * save/restore. Notifications on a broken connection are silently
310 * dropped.
311 */
312void notify_remote_via_irq(int irq)
313{
314 int evtchn = evtchn_from_irq(irq);
315
316 if (VALID_EVTCHN(evtchn))
317 notify_remote_via_evtchn(evtchn);
318}
319EXPORT_SYMBOL_GPL(notify_remote_via_irq);
320
321static void mask_evtchn(int port)
322{
323 struct shared_info *s = HYPERVISOR_shared_info;
324 sync_set_bit(port, &s->evtchn_mask[0]);
325}
326
327static void unmask_evtchn(int port)
328{
329 struct shared_info *s = HYPERVISOR_shared_info;
330 unsigned int cpu = get_cpu();
331
332 BUG_ON(!irqs_disabled());
333
334 /* Slow path (hypercall) if this is a non-local port. */
335 if (unlikely(cpu != cpu_from_evtchn(port))) {
336 struct evtchn_unmask unmask = { .port = port };
337 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
338 } else {
339 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
340
341 sync_clear_bit(port, &s->evtchn_mask[0]);
342
343 /*
344 * The following is basically the equivalent of
345 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
346 * the interrupt edge' if the channel is masked.
347 */
348 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
349 !sync_test_and_set_bit(port / BITS_PER_LONG,
350 &vcpu_info->evtchn_pending_sel))
351 vcpu_info->evtchn_upcall_pending = 1;
352 }
353
354 put_cpu();
355}
356
Konrad Rzeszutek Wilk0794bfc2010-10-18 10:41:08 -0400357static int get_nr_hw_irqs(void)
358{
359 int ret = 1;
360
361#ifdef CONFIG_X86_IO_APIC
362 ret = get_nr_irqs_gsi();
363#endif
364
365 return ret;
366}
367
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700368static int find_unbound_irq(void)
369{
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200370 struct irq_data *data;
371 int irq, res;
Konrad Rzeszutek Wilk3a69e912010-10-18 10:49:10 -0400372 int start = get_nr_hw_irqs();
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700373
Konrad Rzeszutek Wilk3a69e912010-10-18 10:49:10 -0400374 if (start == nr_irqs)
375 goto no_irqs;
376
377 /* nr_irqs is a magic value. Must not use it.*/
378 for (irq = nr_irqs-1; irq > start; irq--) {
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200379 data = irq_get_irq_data(irq);
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100380 /* only 0->15 have init'd desc; handle irq > 16 */
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200381 if (!data)
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100382 break;
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200383 if (data->chip == &no_irq_chip)
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100384 break;
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200385 if (data->chip != &xen_dynamic_chip)
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100386 continue;
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800387 if (irq_info[irq].type == IRQT_UNBOUND)
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200388 return irq;
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100389 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700390
Konrad Rzeszutek Wilk3a69e912010-10-18 10:49:10 -0400391 if (irq == start)
392 goto no_irqs;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700393
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200394 res = irq_alloc_desc_at(irq, 0);
Jeremy Fitzhardinge6f8a0ed2008-12-17 13:42:29 -0800395
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200396 if (WARN_ON(res != irq))
397 return -1;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800398
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700399 return irq;
Konrad Rzeszutek Wilk3a69e912010-10-18 10:49:10 -0400400
401no_irqs:
402 panic("No available IRQ to bind to: increase nr_irqs!\n");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700403}
404
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400405static bool identity_mapped_irq(unsigned irq)
406{
Konrad Rzeszutek Wilk0794bfc2010-10-18 10:41:08 -0400407 /* identity map all the hardware irqs */
408 return irq < get_nr_hw_irqs();
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400409}
410
411static void pirq_unmask_notify(int irq)
412{
413 struct physdev_eoi eoi = { .irq = irq };
414
415 if (unlikely(pirq_needs_eoi(irq))) {
416 int rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
417 WARN_ON(rc);
418 }
419}
420
421static void pirq_query_unmask(int irq)
422{
423 struct physdev_irq_status_query irq_status;
424 struct irq_info *info = info_for_irq(irq);
425
426 BUG_ON(info->type != IRQT_PIRQ);
427
428 irq_status.irq = irq;
429 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
430 irq_status.flags = 0;
431
432 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
433 if (irq_status.flags & XENIRQSTAT_needs_eoi)
434 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
435}
436
437static bool probing_irq(int irq)
438{
439 struct irq_desc *desc = irq_to_desc(irq);
440
441 return desc && desc->action == NULL;
442}
443
444static unsigned int startup_pirq(unsigned int irq)
445{
446 struct evtchn_bind_pirq bind_pirq;
447 struct irq_info *info = info_for_irq(irq);
448 int evtchn = evtchn_from_irq(irq);
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400449 int rc;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400450
451 BUG_ON(info->type != IRQT_PIRQ);
452
453 if (VALID_EVTCHN(evtchn))
454 goto out;
455
456 bind_pirq.pirq = irq;
457 /* NB. We are happy to share unless we are probing. */
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400458 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
459 BIND_PIRQ__WILL_SHARE : 0;
460 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
461 if (rc != 0) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400462 if (!probing_irq(irq))
463 printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
464 irq);
465 return 0;
466 }
467 evtchn = bind_pirq.port;
468
469 pirq_query_unmask(irq);
470
471 evtchn_to_irq[evtchn] = irq;
472 bind_evtchn_to_cpu(evtchn, 0);
473 info->evtchn = evtchn;
474
475out:
476 unmask_evtchn(evtchn);
477 pirq_unmask_notify(irq);
478
479 return 0;
480}
481
482static void shutdown_pirq(unsigned int irq)
483{
484 struct evtchn_close close;
485 struct irq_info *info = info_for_irq(irq);
486 int evtchn = evtchn_from_irq(irq);
487
488 BUG_ON(info->type != IRQT_PIRQ);
489
490 if (!VALID_EVTCHN(evtchn))
491 return;
492
493 mask_evtchn(evtchn);
494
495 close.port = evtchn;
496 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
497 BUG();
498
499 bind_evtchn_to_cpu(evtchn, 0);
500 evtchn_to_irq[evtchn] = -1;
501 info->evtchn = 0;
502}
503
504static void enable_pirq(unsigned int irq)
505{
506 startup_pirq(irq);
507}
508
509static void disable_pirq(unsigned int irq)
510{
511}
512
513static void ack_pirq(unsigned int irq)
514{
515 int evtchn = evtchn_from_irq(irq);
516
517 move_native_irq(irq);
518
519 if (VALID_EVTCHN(evtchn)) {
520 mask_evtchn(evtchn);
521 clear_evtchn(evtchn);
522 }
523}
524
525static void end_pirq(unsigned int irq)
526{
527 int evtchn = evtchn_from_irq(irq);
528 struct irq_desc *desc = irq_to_desc(irq);
529
530 if (WARN_ON(!desc))
531 return;
532
533 if ((desc->status & (IRQ_DISABLED|IRQ_PENDING)) ==
534 (IRQ_DISABLED|IRQ_PENDING)) {
535 shutdown_pirq(irq);
536 } else if (VALID_EVTCHN(evtchn)) {
537 unmask_evtchn(evtchn);
538 pirq_unmask_notify(irq);
539 }
540}
541
542static int find_irq_by_gsi(unsigned gsi)
543{
544 int irq;
545
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -0400546 for (irq = 0; irq < nr_irqs; irq++) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400547 struct irq_info *info = info_for_irq(irq);
548
549 if (info == NULL || info->type != IRQT_PIRQ)
550 continue;
551
552 if (gsi_from_irq(irq) == gsi)
553 return irq;
554 }
555
556 return -1;
557}
558
Konrad Rzeszutek Wilk3a69e912010-10-18 10:49:10 -0400559/* xen_allocate_irq might allocate irqs from the top down, as a
560 * consequence don't assume that the irq number returned has a low value
561 * or can be used as a pirq number unless you know otherwise.
562 *
563 * One notable exception is when xen_allocate_irq is called passing an
564 * hardware gsi as argument, in that case the irq number returned
565 * matches the gsi number passed as first argument.
566
567 * Note: We don't assign an
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400568 * event channel until the irq actually started up. Return an
569 * existing irq if we've already got one for the gsi.
570 */
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400571int xen_allocate_pirq(unsigned gsi, int shareable, char *name)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400572{
573 int irq;
574 struct physdev_irq irq_op;
575
576 spin_lock(&irq_mapping_update_lock);
577
578 irq = find_irq_by_gsi(gsi);
579 if (irq != -1) {
580 printk(KERN_INFO "xen_allocate_pirq: returning irq %d for gsi %u\n",
581 irq, gsi);
582 goto out; /* XXX need refcount? */
583 }
584
Alex Nixonb5401a92010-03-18 16:31:34 -0400585 /* If we are a PV guest, we don't have GSIs (no ACPI passed). Therefore
586 * we are using the !xen_initial_domain() to drop in the function.*/
587 if (identity_mapped_irq(gsi) || !xen_initial_domain()) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400588 irq = gsi;
Konrad Rzeszutek Wilk2c52f8d2010-10-18 17:11:10 -0400589 irq_alloc_desc_at(irq, 0);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400590 } else
591 irq = find_unbound_irq();
592
593 set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
Gerd Hoffmann1a60d052010-10-04 13:42:27 -0400594 handle_level_irq, name);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400595
596 irq_op.irq = irq;
Alex Nixonb5401a92010-03-18 16:31:34 -0400597 irq_op.vector = 0;
598
599 /* Only the privileged domain can do this. For non-priv, the pcifront
600 * driver provides a PCI bus that does the call to do exactly
601 * this in the priv domain. */
602 if (xen_initial_domain() &&
603 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
Konrad Rzeszutek Wilk2c52f8d2010-10-18 17:11:10 -0400604 irq_free_desc(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400605 irq = -ENOSPC;
606 goto out;
607 }
608
609 irq_info[irq] = mk_pirq_info(0, gsi, irq_op.vector);
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400610 irq_info[irq].u.pirq.flags |= shareable ? PIRQ_SHAREABLE : 0;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400611
612out:
613 spin_unlock(&irq_mapping_update_lock);
614
615 return irq;
616}
617
Alex Nixonb5401a92010-03-18 16:31:34 -0400618int xen_destroy_irq(int irq)
619{
620 struct irq_desc *desc;
621 int rc = -ENOENT;
622
623 spin_lock(&irq_mapping_update_lock);
624
625 desc = irq_to_desc(irq);
626 if (!desc)
627 goto out;
628
629 irq_info[irq] = mk_unbound_info();
630
Konrad Rzeszutek Wilk2c52f8d2010-10-18 17:11:10 -0400631 irq_free_desc(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400632
633out:
634 spin_unlock(&irq_mapping_update_lock);
635 return rc;
636}
637
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400638int xen_vector_from_irq(unsigned irq)
639{
640 return vector_from_irq(irq);
641}
642
643int xen_gsi_from_irq(unsigned irq)
644{
645 return gsi_from_irq(irq);
646}
647
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700648int bind_evtchn_to_irq(unsigned int evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700649{
650 int irq;
651
652 spin_lock(&irq_mapping_update_lock);
653
654 irq = evtchn_to_irq[evtchn];
655
656 if (irq == -1) {
657 irq = find_unbound_irq();
658
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700659 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
Jeremy Fitzhardingedffe2e12010-08-20 19:10:01 -0700660 handle_edge_irq, "event");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700661
662 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800663 irq_info[irq] = mk_evtchn_info(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700664 }
665
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700666 spin_unlock(&irq_mapping_update_lock);
667
668 return irq;
669}
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700670EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700671
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700672static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
673{
674 struct evtchn_bind_ipi bind_ipi;
675 int evtchn, irq;
676
677 spin_lock(&irq_mapping_update_lock);
678
679 irq = per_cpu(ipi_to_irq, cpu)[ipi];
Ian Campbell90af9512009-02-06 16:55:58 -0800680
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700681 if (irq == -1) {
682 irq = find_unbound_irq();
683 if (irq < 0)
684 goto out;
685
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700686 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
687 handle_percpu_irq, "ipi");
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700688
689 bind_ipi.vcpu = cpu;
690 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
691 &bind_ipi) != 0)
692 BUG();
693 evtchn = bind_ipi.port;
694
695 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800696 irq_info[irq] = mk_ipi_info(evtchn, ipi);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700697 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
698
699 bind_evtchn_to_cpu(evtchn, cpu);
700 }
701
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700702 out:
703 spin_unlock(&irq_mapping_update_lock);
704 return irq;
705}
706
707
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700708static int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
709{
710 struct evtchn_bind_virq bind_virq;
711 int evtchn, irq;
712
713 spin_lock(&irq_mapping_update_lock);
714
715 irq = per_cpu(virq_to_irq, cpu)[virq];
716
717 if (irq == -1) {
718 bind_virq.virq = virq;
719 bind_virq.vcpu = cpu;
720 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
721 &bind_virq) != 0)
722 BUG();
723 evtchn = bind_virq.port;
724
725 irq = find_unbound_irq();
726
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700727 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
728 handle_percpu_irq, "virq");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700729
730 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800731 irq_info[irq] = mk_virq_info(evtchn, virq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700732
733 per_cpu(virq_to_irq, cpu)[virq] = irq;
734
735 bind_evtchn_to_cpu(evtchn, cpu);
736 }
737
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700738 spin_unlock(&irq_mapping_update_lock);
739
740 return irq;
741}
742
743static void unbind_from_irq(unsigned int irq)
744{
745 struct evtchn_close close;
746 int evtchn = evtchn_from_irq(irq);
747
748 spin_lock(&irq_mapping_update_lock);
749
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800750 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700751 close.port = evtchn;
752 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
753 BUG();
754
755 switch (type_from_irq(irq)) {
756 case IRQT_VIRQ:
757 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800758 [virq_from_irq(irq)] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700759 break;
Alex Nixond68d82a2008-08-22 11:52:15 +0100760 case IRQT_IPI:
761 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800762 [ipi_from_irq(irq)] = -1;
Alex Nixond68d82a2008-08-22 11:52:15 +0100763 break;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700764 default:
765 break;
766 }
767
768 /* Closed ports are implicitly re-bound to VCPU0. */
769 bind_evtchn_to_cpu(evtchn, 0);
770
771 evtchn_to_irq[evtchn] = -1;
Ian Campbellfed5ea82009-12-01 16:15:30 +0000772 }
773
774 if (irq_info[irq].type != IRQT_UNBOUND) {
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800775 irq_info[irq] = mk_unbound_info();
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700776
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200777 irq_free_desc(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700778 }
779
780 spin_unlock(&irq_mapping_update_lock);
781}
782
783int bind_evtchn_to_irqhandler(unsigned int evtchn,
Jeff Garzik7c239972007-10-19 03:12:20 -0400784 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700785 unsigned long irqflags,
786 const char *devname, void *dev_id)
787{
788 unsigned int irq;
789 int retval;
790
791 irq = bind_evtchn_to_irq(evtchn);
792 retval = request_irq(irq, handler, irqflags, devname, dev_id);
793 if (retval != 0) {
794 unbind_from_irq(irq);
795 return retval;
796 }
797
798 return irq;
799}
800EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
801
802int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
Jeff Garzik7c239972007-10-19 03:12:20 -0400803 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700804 unsigned long irqflags, const char *devname, void *dev_id)
805{
806 unsigned int irq;
807 int retval;
808
809 irq = bind_virq_to_irq(virq, cpu);
810 retval = request_irq(irq, handler, irqflags, devname, dev_id);
811 if (retval != 0) {
812 unbind_from_irq(irq);
813 return retval;
814 }
815
816 return irq;
817}
818EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
819
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700820int bind_ipi_to_irqhandler(enum ipi_vector ipi,
821 unsigned int cpu,
822 irq_handler_t handler,
823 unsigned long irqflags,
824 const char *devname,
825 void *dev_id)
826{
827 int irq, retval;
828
829 irq = bind_ipi_to_irq(ipi, cpu);
830 if (irq < 0)
831 return irq;
832
Ian Campbell4877c732010-07-29 11:16:35 +0100833 irqflags |= IRQF_NO_SUSPEND;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700834 retval = request_irq(irq, handler, irqflags, devname, dev_id);
835 if (retval != 0) {
836 unbind_from_irq(irq);
837 return retval;
838 }
839
840 return irq;
841}
842
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700843void unbind_from_irqhandler(unsigned int irq, void *dev_id)
844{
845 free_irq(irq, dev_id);
846 unbind_from_irq(irq);
847}
848EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
849
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700850void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
851{
852 int irq = per_cpu(ipi_to_irq, cpu)[vector];
853 BUG_ON(irq < 0);
854 notify_remote_via_irq(irq);
855}
856
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700857irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
858{
859 struct shared_info *sh = HYPERVISOR_shared_info;
860 int cpu = smp_processor_id();
861 int i;
862 unsigned long flags;
863 static DEFINE_SPINLOCK(debug_lock);
864
865 spin_lock_irqsave(&debug_lock, flags);
866
867 printk("vcpu %d\n ", cpu);
868
869 for_each_online_cpu(i) {
870 struct vcpu_info *v = per_cpu(xen_vcpu, i);
871 printk("%d: masked=%d pending=%d event_sel %08lx\n ", i,
Isaku Yamahatae849c3e2008-04-02 10:53:56 -0700872 (get_irq_regs() && i == cpu) ? xen_irqs_disabled(get_irq_regs()) : v->evtchn_upcall_mask,
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700873 v->evtchn_upcall_pending,
874 v->evtchn_pending_sel);
875 }
876 printk("pending:\n ");
877 for(i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
878 printk("%08lx%s", sh->evtchn_pending[i],
879 i % 8 == 0 ? "\n " : " ");
880 printk("\nmasks:\n ");
881 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
882 printk("%08lx%s", sh->evtchn_mask[i],
883 i % 8 == 0 ? "\n " : " ");
884
885 printk("\nunmasked:\n ");
886 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
887 printk("%08lx%s", sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
888 i % 8 == 0 ? "\n " : " ");
889
890 printk("\npending list:\n");
891 for(i = 0; i < NR_EVENT_CHANNELS; i++) {
892 if (sync_test_bit(i, sh->evtchn_pending)) {
893 printk(" %d: event %d -> irq %d\n",
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800894 cpu_from_evtchn(i), i,
895 evtchn_to_irq[i]);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700896 }
897 }
898
899 spin_unlock_irqrestore(&debug_lock, flags);
900
901 return IRQ_HANDLED;
902}
903
Tejun Heo245b2e72009-06-24 15:13:48 +0900904static DEFINE_PER_CPU(unsigned, xed_nesting_count);
905
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700906/*
907 * Search the CPUs pending events bitmasks. For each one found, map
908 * the event number to an irq, and feed it into do_IRQ() for
909 * handling.
910 *
911 * Xen uses a two-level bitmap to speed searching. The first level is
912 * a bitset of words which contain pending event bits. The second
913 * level is a bitset of pending events themselves.
914 */
Sheng Yang38e20b02010-05-14 12:40:51 +0100915static void __xen_evtchn_do_upcall(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700916{
917 int cpu = get_cpu();
918 struct shared_info *s = HYPERVISOR_shared_info;
919 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700920 unsigned count;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700921
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700922 do {
923 unsigned long pending_words;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700924
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700925 vcpu_info->evtchn_upcall_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700926
Tejun Heo245b2e72009-06-24 15:13:48 +0900927 if (__get_cpu_var(xed_nesting_count)++)
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700928 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700929
Isaku Yamahatae849c3e2008-04-02 10:53:56 -0700930#ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
931 /* Clear master flag /before/ clearing selector flag. */
Isaku Yamahata6673cf62008-06-16 14:58:13 -0700932 wmb();
Isaku Yamahatae849c3e2008-04-02 10:53:56 -0700933#endif
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700934 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
935 while (pending_words != 0) {
936 unsigned long pending_bits;
937 int word_idx = __ffs(pending_words);
938 pending_words &= ~(1UL << word_idx);
939
940 while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
941 int bit_idx = __ffs(pending_bits);
942 int port = (word_idx * BITS_PER_LONG) + bit_idx;
943 int irq = evtchn_to_irq[port];
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -0800944 struct irq_desc *desc;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700945
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -0800946 if (irq != -1) {
947 desc = irq_to_desc(irq);
948 if (desc)
949 generic_handle_irq_desc(irq, desc);
950 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700951 }
952 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700953
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700954 BUG_ON(!irqs_disabled());
955
Tejun Heo245b2e72009-06-24 15:13:48 +0900956 count = __get_cpu_var(xed_nesting_count);
957 __get_cpu_var(xed_nesting_count) = 0;
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100958 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700959
960out:
Jeremy Fitzhardinge3445a8f2009-02-06 14:09:46 -0800961
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700962 put_cpu();
963}
964
Sheng Yang38e20b02010-05-14 12:40:51 +0100965void xen_evtchn_do_upcall(struct pt_regs *regs)
966{
967 struct pt_regs *old_regs = set_irq_regs(regs);
968
969 exit_idle();
970 irq_enter();
971
972 __xen_evtchn_do_upcall();
973
974 irq_exit();
975 set_irq_regs(old_regs);
976}
977
978void xen_hvm_evtchn_do_upcall(void)
979{
980 __xen_evtchn_do_upcall();
981}
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100982EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
Sheng Yang38e20b02010-05-14 12:40:51 +0100983
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +0100984/* Rebind a new event channel to an existing irq. */
985void rebind_evtchn_irq(int evtchn, int irq)
986{
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800987 struct irq_info *info = info_for_irq(irq);
988
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +0100989 /* Make sure the irq is masked, since the new event channel
990 will also be masked. */
991 disable_irq(irq);
992
993 spin_lock(&irq_mapping_update_lock);
994
995 /* After resume the irq<->evtchn mappings are all cleared out */
996 BUG_ON(evtchn_to_irq[evtchn] != -1);
997 /* Expect irq to have been bound before,
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800998 so there should be a proper type */
999 BUG_ON(info->type == IRQT_UNBOUND);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001000
1001 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001002 irq_info[irq] = mk_evtchn_info(evtchn);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001003
1004 spin_unlock(&irq_mapping_update_lock);
1005
1006 /* new event channels are always bound to cpu 0 */
Rusty Russell0de26522008-12-13 21:20:26 +10301007 irq_set_affinity(irq, cpumask_of(0));
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001008
1009 /* Unmask the event channel. */
1010 enable_irq(irq);
1011}
1012
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001013/* Rebind an evtchn so that it gets delivered to a specific cpu */
Yinghai Lud5dedd42009-04-27 17:59:21 -07001014static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001015{
1016 struct evtchn_bind_vcpu bind_vcpu;
1017 int evtchn = evtchn_from_irq(irq);
1018
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001019 /* events delivered via platform PCI interrupts are always
1020 * routed to vcpu 0 */
1021 if (!VALID_EVTCHN(evtchn) ||
1022 (xen_hvm_domain() && !xen_have_vector_callback))
Yinghai Lud5dedd42009-04-27 17:59:21 -07001023 return -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001024
1025 /* Send future instances of this interrupt to other vcpu. */
1026 bind_vcpu.port = evtchn;
1027 bind_vcpu.vcpu = tcpu;
1028
1029 /*
1030 * If this fails, it usually just indicates that we're dealing with a
1031 * virq or IPI channel, which don't actually need to be rebound. Ignore
1032 * it, but don't do the xenlinux-level rebind in that case.
1033 */
1034 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1035 bind_evtchn_to_cpu(evtchn, tcpu);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001036
1037 return 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001038}
1039
Yinghai Lud5dedd42009-04-27 17:59:21 -07001040static int set_affinity_irq(unsigned irq, const struct cpumask *dest)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001041{
Rusty Russell0de26522008-12-13 21:20:26 +10301042 unsigned tcpu = cpumask_first(dest);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001043
1044 return rebind_irq_to_cpu(irq, tcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001045}
1046
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001047int resend_irq_on_evtchn(unsigned int irq)
1048{
1049 int masked, evtchn = evtchn_from_irq(irq);
1050 struct shared_info *s = HYPERVISOR_shared_info;
1051
1052 if (!VALID_EVTCHN(evtchn))
1053 return 1;
1054
1055 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
1056 sync_set_bit(evtchn, s->evtchn_pending);
1057 if (!masked)
1058 unmask_evtchn(evtchn);
1059
1060 return 1;
1061}
1062
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001063static void enable_dynirq(unsigned int irq)
1064{
1065 int evtchn = evtchn_from_irq(irq);
1066
1067 if (VALID_EVTCHN(evtchn))
1068 unmask_evtchn(evtchn);
1069}
1070
1071static void disable_dynirq(unsigned int irq)
1072{
1073 int evtchn = evtchn_from_irq(irq);
1074
1075 if (VALID_EVTCHN(evtchn))
1076 mask_evtchn(evtchn);
1077}
1078
1079static void ack_dynirq(unsigned int irq)
1080{
1081 int evtchn = evtchn_from_irq(irq);
1082
1083 move_native_irq(irq);
1084
1085 if (VALID_EVTCHN(evtchn))
1086 clear_evtchn(evtchn);
1087}
1088
1089static int retrigger_dynirq(unsigned int irq)
1090{
1091 int evtchn = evtchn_from_irq(irq);
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001092 struct shared_info *sh = HYPERVISOR_shared_info;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001093 int ret = 0;
1094
1095 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001096 int masked;
1097
1098 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
1099 sync_set_bit(evtchn, sh->evtchn_pending);
1100 if (!masked)
1101 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001102 ret = 1;
1103 }
1104
1105 return ret;
1106}
1107
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001108static void restore_cpu_virqs(unsigned int cpu)
1109{
1110 struct evtchn_bind_virq bind_virq;
1111 int virq, irq, evtchn;
1112
1113 for (virq = 0; virq < NR_VIRQS; virq++) {
1114 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1115 continue;
1116
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001117 BUG_ON(virq_from_irq(irq) != virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001118
1119 /* Get a new binding from Xen. */
1120 bind_virq.virq = virq;
1121 bind_virq.vcpu = cpu;
1122 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1123 &bind_virq) != 0)
1124 BUG();
1125 evtchn = bind_virq.port;
1126
1127 /* Record the new mapping. */
1128 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001129 irq_info[irq] = mk_virq_info(evtchn, virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001130 bind_evtchn_to_cpu(evtchn, cpu);
1131
1132 /* Ready for use. */
1133 unmask_evtchn(evtchn);
1134 }
1135}
1136
1137static void restore_cpu_ipis(unsigned int cpu)
1138{
1139 struct evtchn_bind_ipi bind_ipi;
1140 int ipi, irq, evtchn;
1141
1142 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1143 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1144 continue;
1145
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001146 BUG_ON(ipi_from_irq(irq) != ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001147
1148 /* Get a new binding from Xen. */
1149 bind_ipi.vcpu = cpu;
1150 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1151 &bind_ipi) != 0)
1152 BUG();
1153 evtchn = bind_ipi.port;
1154
1155 /* Record the new mapping. */
1156 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001157 irq_info[irq] = mk_ipi_info(evtchn, ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001158 bind_evtchn_to_cpu(evtchn, cpu);
1159
1160 /* Ready for use. */
1161 unmask_evtchn(evtchn);
1162
1163 }
1164}
1165
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001166/* Clear an irq's pending state, in preparation for polling on it */
1167void xen_clear_irq_pending(int irq)
1168{
1169 int evtchn = evtchn_from_irq(irq);
1170
1171 if (VALID_EVTCHN(evtchn))
1172 clear_evtchn(evtchn);
1173}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001174EXPORT_SYMBOL(xen_clear_irq_pending);
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -07001175void xen_set_irq_pending(int irq)
1176{
1177 int evtchn = evtchn_from_irq(irq);
1178
1179 if (VALID_EVTCHN(evtchn))
1180 set_evtchn(evtchn);
1181}
1182
1183bool xen_test_irq_pending(int irq)
1184{
1185 int evtchn = evtchn_from_irq(irq);
1186 bool ret = false;
1187
1188 if (VALID_EVTCHN(evtchn))
1189 ret = test_evtchn(evtchn);
1190
1191 return ret;
1192}
1193
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001194/* Poll waiting for an irq to become pending with timeout. In the usual case,
1195 * the irq will be disabled so it won't deliver an interrupt. */
1196void xen_poll_irq_timeout(int irq, u64 timeout)
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001197{
1198 evtchn_port_t evtchn = evtchn_from_irq(irq);
1199
1200 if (VALID_EVTCHN(evtchn)) {
1201 struct sched_poll poll;
1202
1203 poll.nr_ports = 1;
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001204 poll.timeout = timeout;
Isaku Yamahataff3c5362008-10-14 17:50:44 -07001205 set_xen_guest_handle(poll.ports, &evtchn);
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001206
1207 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1208 BUG();
1209 }
1210}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001211EXPORT_SYMBOL(xen_poll_irq_timeout);
1212/* Poll waiting for an irq to become pending. In the usual case, the
1213 * irq will be disabled so it won't deliver an interrupt. */
1214void xen_poll_irq(int irq)
1215{
1216 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1217}
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001218
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001219void xen_irq_resume(void)
1220{
1221 unsigned int cpu, irq, evtchn;
1222
1223 init_evtchn_cpu_bindings();
1224
1225 /* New event-channel space is not 'live' yet. */
1226 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1227 mask_evtchn(evtchn);
1228
1229 /* No IRQ <-> event-channel mappings. */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001230 for (irq = 0; irq < nr_irqs; irq++)
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001231 irq_info[irq].evtchn = 0; /* zap event-channel binding */
1232
1233 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1234 evtchn_to_irq[evtchn] = -1;
1235
1236 for_each_possible_cpu(cpu) {
1237 restore_cpu_virqs(cpu);
1238 restore_cpu_ipis(cpu);
1239 }
1240}
1241
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001242static struct irq_chip xen_dynamic_chip __read_mostly = {
1243 .name = "xen-dyn",
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001244
1245 .disable = disable_dynirq,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001246 .mask = disable_dynirq,
1247 .unmask = enable_dynirq,
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001248
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001249 .ack = ack_dynirq,
1250 .set_affinity = set_affinity_irq,
1251 .retrigger = retrigger_dynirq,
1252};
1253
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001254static struct irq_chip xen_pirq_chip __read_mostly = {
1255 .name = "xen-pirq",
1256
1257 .startup = startup_pirq,
1258 .shutdown = shutdown_pirq,
1259
1260 .enable = enable_pirq,
1261 .unmask = enable_pirq,
1262
1263 .disable = disable_pirq,
1264 .mask = disable_pirq,
1265
1266 .ack = ack_pirq,
1267 .end = end_pirq,
1268
1269 .set_affinity = set_affinity_irq,
1270
1271 .retrigger = retrigger_dynirq,
1272};
1273
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001274static struct irq_chip xen_percpu_chip __read_mostly = {
1275 .name = "xen-percpu",
1276
1277 .disable = disable_dynirq,
1278 .mask = disable_dynirq,
1279 .unmask = enable_dynirq,
1280
1281 .ack = ack_dynirq,
1282};
1283
Sheng Yang38e20b02010-05-14 12:40:51 +01001284int xen_set_callback_via(uint64_t via)
1285{
1286 struct xen_hvm_param a;
1287 a.domid = DOMID_SELF;
1288 a.index = HVM_PARAM_CALLBACK_IRQ;
1289 a.value = via;
1290 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1291}
1292EXPORT_SYMBOL_GPL(xen_set_callback_via);
1293
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001294#ifdef CONFIG_XEN_PVHVM
Sheng Yang38e20b02010-05-14 12:40:51 +01001295/* Vector callbacks are better than PCI interrupts to receive event
1296 * channel notifications because we can receive vector callbacks on any
1297 * vcpu and we don't need PCI support or APIC interactions. */
1298void xen_callback_vector(void)
1299{
1300 int rc;
1301 uint64_t callback_via;
1302 if (xen_have_vector_callback) {
1303 callback_via = HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK);
1304 rc = xen_set_callback_via(callback_via);
1305 if (rc) {
1306 printk(KERN_ERR "Request for Xen HVM callback vector"
1307 " failed.\n");
1308 xen_have_vector_callback = 0;
1309 return;
1310 }
1311 printk(KERN_INFO "Xen HVM callback vector for event delivery is "
1312 "enabled\n");
1313 /* in the restore case the vector has already been allocated */
1314 if (!test_bit(XEN_HVM_EVTCHN_CALLBACK, used_vectors))
1315 alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK, xen_hvm_callback_vector);
1316 }
1317}
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001318#else
1319void xen_callback_vector(void) {}
1320#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001321
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001322void __init xen_init_IRQ(void)
1323{
1324 int i;
Mike Travisc7a35892009-01-10 21:58:11 -08001325
Pekka Enberga70c3522009-07-01 11:51:18 +03001326 cpu_evtchn_mask_p = kcalloc(nr_cpu_ids, sizeof(struct cpu_evtchn_s),
1327 GFP_KERNEL);
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001328 irq_info = kcalloc(nr_irqs, sizeof(*irq_info), GFP_KERNEL);
1329
1330 evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
1331 GFP_KERNEL);
1332 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1333 evtchn_to_irq[i] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001334
1335 init_evtchn_cpu_bindings();
1336
1337 /* No event channels are 'live' right now. */
1338 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1339 mask_evtchn(i);
1340
Sheng Yang38e20b02010-05-14 12:40:51 +01001341 if (xen_hvm_domain()) {
1342 xen_callback_vector();
1343 native_init_IRQ();
1344 } else {
1345 irq_ctx_init(smp_processor_id());
1346 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001347}