blob: 07e56e5a5d2d57521b6e914de51d692df5d0d1fc [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 {
Stefano Stabellini7a043f12010-07-01 17:08:14 +010093 unsigned short pirq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080094 unsigned short gsi;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -040095 unsigned char vector;
96 unsigned char flags;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080097 } pirq;
98 } u;
99};
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400100#define PIRQ_NEEDS_EOI (1 << 0)
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400101#define PIRQ_SHAREABLE (1 << 1)
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800102
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -0400103static struct irq_info *irq_info;
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100104static int *pirq_to_irq;
Stefano Stabellini01557ba2010-08-20 14:46:52 +0100105static int nr_pirqs;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700106
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -0400107static int *evtchn_to_irq;
Mike Travisc7a35892009-01-10 21:58:11 -0800108struct cpu_evtchn_s {
109 unsigned long bits[NR_EVENT_CHANNELS/BITS_PER_LONG];
110};
Jeremy Fitzhardinge3b32f572009-08-13 12:50:37 -0700111
112static __initdata struct cpu_evtchn_s init_evtchn_mask = {
113 .bits[0 ... (NR_EVENT_CHANNELS/BITS_PER_LONG)-1] = ~0ul,
114};
115static struct cpu_evtchn_s *cpu_evtchn_mask_p = &init_evtchn_mask;
116
Mike Travisc7a35892009-01-10 21:58:11 -0800117static inline unsigned long *cpu_evtchn_mask(int cpu)
118{
119 return cpu_evtchn_mask_p[cpu].bits;
120}
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700121
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700122/* Xen will never allocate port zero for any purpose. */
123#define VALID_EVTCHN(chn) ((chn) != 0)
124
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700125static struct irq_chip xen_dynamic_chip;
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700126static struct irq_chip xen_percpu_chip;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400127static struct irq_chip xen_pirq_chip;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700128
129/* Constructor for packed IRQ information. */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800130static struct irq_info mk_unbound_info(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700131{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800132 return (struct irq_info) { .type = IRQT_UNBOUND };
133}
134
135static struct irq_info mk_evtchn_info(unsigned short evtchn)
136{
Ian Campbell90af9512009-02-06 16:55:58 -0800137 return (struct irq_info) { .type = IRQT_EVTCHN, .evtchn = evtchn,
138 .cpu = 0 };
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800139}
140
141static struct irq_info mk_ipi_info(unsigned short evtchn, enum ipi_vector ipi)
142{
143 return (struct irq_info) { .type = IRQT_IPI, .evtchn = evtchn,
Ian Campbell90af9512009-02-06 16:55:58 -0800144 .cpu = 0, .u.ipi = ipi };
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800145}
146
147static struct irq_info mk_virq_info(unsigned short evtchn, unsigned short virq)
148{
149 return (struct irq_info) { .type = IRQT_VIRQ, .evtchn = evtchn,
Ian Campbell90af9512009-02-06 16:55:58 -0800150 .cpu = 0, .u.virq = virq };
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800151}
152
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100153static struct irq_info mk_pirq_info(unsigned short evtchn, unsigned short pirq,
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800154 unsigned short gsi, unsigned short vector)
155{
156 return (struct irq_info) { .type = IRQT_PIRQ, .evtchn = evtchn,
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100157 .cpu = 0,
158 .u.pirq = { .pirq = pirq, .gsi = gsi, .vector = vector } };
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700159}
160
161/*
162 * Accessors for packed IRQ information.
163 */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800164static struct irq_info *info_for_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700165{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800166 return &irq_info[irq];
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700167}
168
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800169static unsigned int evtchn_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700170{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800171 return info_for_irq(irq)->evtchn;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700172}
173
Ian Campbelld4c04532009-02-06 19:20:31 -0800174unsigned irq_from_evtchn(unsigned int evtchn)
175{
176 return evtchn_to_irq[evtchn];
177}
178EXPORT_SYMBOL_GPL(irq_from_evtchn);
179
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800180static enum ipi_vector ipi_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700181{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800182 struct irq_info *info = info_for_irq(irq);
183
184 BUG_ON(info == NULL);
185 BUG_ON(info->type != IRQT_IPI);
186
187 return info->u.ipi;
188}
189
190static unsigned virq_from_irq(unsigned irq)
191{
192 struct irq_info *info = info_for_irq(irq);
193
194 BUG_ON(info == NULL);
195 BUG_ON(info->type != IRQT_VIRQ);
196
197 return info->u.virq;
198}
199
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100200static unsigned pirq_from_irq(unsigned irq)
201{
202 struct irq_info *info = info_for_irq(irq);
203
204 BUG_ON(info == NULL);
205 BUG_ON(info->type != IRQT_PIRQ);
206
207 return info->u.pirq.pirq;
208}
209
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800210static unsigned gsi_from_irq(unsigned irq)
211{
212 struct irq_info *info = info_for_irq(irq);
213
214 BUG_ON(info == NULL);
215 BUG_ON(info->type != IRQT_PIRQ);
216
217 return info->u.pirq.gsi;
218}
219
220static unsigned vector_from_irq(unsigned irq)
221{
222 struct irq_info *info = info_for_irq(irq);
223
224 BUG_ON(info == NULL);
225 BUG_ON(info->type != IRQT_PIRQ);
226
227 return info->u.pirq.vector;
228}
229
230static enum xen_irq_type type_from_irq(unsigned irq)
231{
232 return info_for_irq(irq)->type;
233}
234
235static unsigned cpu_from_irq(unsigned irq)
236{
237 return info_for_irq(irq)->cpu;
238}
239
240static unsigned int cpu_from_evtchn(unsigned int evtchn)
241{
242 int irq = evtchn_to_irq[evtchn];
243 unsigned ret = 0;
244
245 if (irq != -1)
246 ret = cpu_from_irq(irq);
247
248 return ret;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700249}
250
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400251static bool pirq_needs_eoi(unsigned irq)
252{
253 struct irq_info *info = info_for_irq(irq);
254
255 BUG_ON(info->type != IRQT_PIRQ);
256
257 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
258}
259
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700260static inline unsigned long active_evtchns(unsigned int cpu,
261 struct shared_info *sh,
262 unsigned int idx)
263{
264 return (sh->evtchn_pending[idx] &
Mike Travisc7a35892009-01-10 21:58:11 -0800265 cpu_evtchn_mask(cpu)[idx] &
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700266 ~sh->evtchn_mask[idx]);
267}
268
269static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
270{
271 int irq = evtchn_to_irq[chn];
272
273 BUG_ON(irq == -1);
274#ifdef CONFIG_SMP
Mike Travis7f7ace02009-01-10 21:58:08 -0800275 cpumask_copy(irq_to_desc(irq)->affinity, cpumask_of(cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700276#endif
277
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800278 __clear_bit(chn, cpu_evtchn_mask(cpu_from_irq(irq)));
Mike Travisc7a35892009-01-10 21:58:11 -0800279 __set_bit(chn, cpu_evtchn_mask(cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700280
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800281 irq_info[irq].cpu = cpu;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700282}
283
284static void init_evtchn_cpu_bindings(void)
285{
286#ifdef CONFIG_SMP
Thomas Gleixner10e58082008-10-16 14:19:04 +0200287 struct irq_desc *desc;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700288 int i;
Thomas Gleixner10e58082008-10-16 14:19:04 +0200289
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700290 /* By default all event channels notify CPU#0. */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800291 for_each_irq_desc(i, desc) {
Mike Travis7f7ace02009-01-10 21:58:08 -0800292 cpumask_copy(desc->affinity, cpumask_of(0));
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800293 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700294#endif
295
Mike Travisc7a35892009-01-10 21:58:11 -0800296 memset(cpu_evtchn_mask(0), ~0, sizeof(cpu_evtchn_mask(0)));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700297}
298
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700299static inline void clear_evtchn(int port)
300{
301 struct shared_info *s = HYPERVISOR_shared_info;
302 sync_clear_bit(port, &s->evtchn_pending[0]);
303}
304
305static inline void set_evtchn(int port)
306{
307 struct shared_info *s = HYPERVISOR_shared_info;
308 sync_set_bit(port, &s->evtchn_pending[0]);
309}
310
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700311static inline int test_evtchn(int port)
312{
313 struct shared_info *s = HYPERVISOR_shared_info;
314 return sync_test_bit(port, &s->evtchn_pending[0]);
315}
316
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700317
318/**
319 * notify_remote_via_irq - send event to remote end of event channel via irq
320 * @irq: irq of event channel to send event to
321 *
322 * Unlike notify_remote_via_evtchn(), this is safe to use across
323 * save/restore. Notifications on a broken connection are silently
324 * dropped.
325 */
326void notify_remote_via_irq(int irq)
327{
328 int evtchn = evtchn_from_irq(irq);
329
330 if (VALID_EVTCHN(evtchn))
331 notify_remote_via_evtchn(evtchn);
332}
333EXPORT_SYMBOL_GPL(notify_remote_via_irq);
334
335static void mask_evtchn(int port)
336{
337 struct shared_info *s = HYPERVISOR_shared_info;
338 sync_set_bit(port, &s->evtchn_mask[0]);
339}
340
341static void unmask_evtchn(int port)
342{
343 struct shared_info *s = HYPERVISOR_shared_info;
344 unsigned int cpu = get_cpu();
345
346 BUG_ON(!irqs_disabled());
347
348 /* Slow path (hypercall) if this is a non-local port. */
349 if (unlikely(cpu != cpu_from_evtchn(port))) {
350 struct evtchn_unmask unmask = { .port = port };
351 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
352 } else {
353 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
354
355 sync_clear_bit(port, &s->evtchn_mask[0]);
356
357 /*
358 * The following is basically the equivalent of
359 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
360 * the interrupt edge' if the channel is masked.
361 */
362 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
363 !sync_test_and_set_bit(port / BITS_PER_LONG,
364 &vcpu_info->evtchn_pending_sel))
365 vcpu_info->evtchn_upcall_pending = 1;
366 }
367
368 put_cpu();
369}
370
Konrad Rzeszutek Wilk0794bfc2010-10-18 10:41:08 -0400371static int get_nr_hw_irqs(void)
372{
373 int ret = 1;
374
375#ifdef CONFIG_X86_IO_APIC
376 ret = get_nr_irqs_gsi();
377#endif
378
379 return ret;
380}
381
Stefano Stabellini01557ba2010-08-20 14:46:52 +0100382/* callers of this function should make sure that PHYSDEVOP_get_nr_pirqs
383 * succeeded otherwise nr_pirqs won't hold the right value */
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100384static int find_unbound_pirq(void)
385{
386 int i;
Stefano Stabellini01557ba2010-08-20 14:46:52 +0100387 for (i = nr_pirqs-1; i >= 0; i--) {
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100388 if (pirq_to_irq[i] < 0)
389 return i;
390 }
391 return -1;
392}
393
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700394static int find_unbound_irq(void)
395{
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200396 struct irq_data *data;
397 int irq, res;
Konrad Rzeszutek Wilk3a69e912010-10-18 10:49:10 -0400398 int start = get_nr_hw_irqs();
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700399
Konrad Rzeszutek Wilk3a69e912010-10-18 10:49:10 -0400400 if (start == nr_irqs)
401 goto no_irqs;
402
403 /* nr_irqs is a magic value. Must not use it.*/
404 for (irq = nr_irqs-1; irq > start; irq--) {
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200405 data = irq_get_irq_data(irq);
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100406 /* only 0->15 have init'd desc; handle irq > 16 */
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200407 if (!data)
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100408 break;
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200409 if (data->chip == &no_irq_chip)
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100410 break;
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200411 if (data->chip != &xen_dynamic_chip)
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100412 continue;
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800413 if (irq_info[irq].type == IRQT_UNBOUND)
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200414 return irq;
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100415 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700416
Konrad Rzeszutek Wilk3a69e912010-10-18 10:49:10 -0400417 if (irq == start)
418 goto no_irqs;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700419
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200420 res = irq_alloc_desc_at(irq, 0);
Jeremy Fitzhardinge6f8a0ed2008-12-17 13:42:29 -0800421
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200422 if (WARN_ON(res != irq))
423 return -1;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800424
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700425 return irq;
Konrad Rzeszutek Wilk3a69e912010-10-18 10:49:10 -0400426
427no_irqs:
428 panic("No available IRQ to bind to: increase nr_irqs!\n");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700429}
430
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400431static bool identity_mapped_irq(unsigned irq)
432{
Konrad Rzeszutek Wilk0794bfc2010-10-18 10:41:08 -0400433 /* identity map all the hardware irqs */
434 return irq < get_nr_hw_irqs();
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400435}
436
437static void pirq_unmask_notify(int irq)
438{
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100439 struct physdev_eoi eoi = { .irq = pirq_from_irq(irq) };
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400440
441 if (unlikely(pirq_needs_eoi(irq))) {
442 int rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
443 WARN_ON(rc);
444 }
445}
446
447static void pirq_query_unmask(int irq)
448{
449 struct physdev_irq_status_query irq_status;
450 struct irq_info *info = info_for_irq(irq);
451
452 BUG_ON(info->type != IRQT_PIRQ);
453
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100454 irq_status.irq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400455 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
456 irq_status.flags = 0;
457
458 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
459 if (irq_status.flags & XENIRQSTAT_needs_eoi)
460 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
461}
462
463static bool probing_irq(int irq)
464{
465 struct irq_desc *desc = irq_to_desc(irq);
466
467 return desc && desc->action == NULL;
468}
469
470static unsigned int startup_pirq(unsigned int irq)
471{
472 struct evtchn_bind_pirq bind_pirq;
473 struct irq_info *info = info_for_irq(irq);
474 int evtchn = evtchn_from_irq(irq);
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400475 int rc;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400476
477 BUG_ON(info->type != IRQT_PIRQ);
478
479 if (VALID_EVTCHN(evtchn))
480 goto out;
481
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100482 bind_pirq.pirq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400483 /* NB. We are happy to share unless we are probing. */
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400484 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
485 BIND_PIRQ__WILL_SHARE : 0;
486 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
487 if (rc != 0) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400488 if (!probing_irq(irq))
489 printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
490 irq);
491 return 0;
492 }
493 evtchn = bind_pirq.port;
494
495 pirq_query_unmask(irq);
496
497 evtchn_to_irq[evtchn] = irq;
498 bind_evtchn_to_cpu(evtchn, 0);
499 info->evtchn = evtchn;
500
501out:
502 unmask_evtchn(evtchn);
503 pirq_unmask_notify(irq);
504
505 return 0;
506}
507
508static void shutdown_pirq(unsigned int irq)
509{
510 struct evtchn_close close;
511 struct irq_info *info = info_for_irq(irq);
512 int evtchn = evtchn_from_irq(irq);
513
514 BUG_ON(info->type != IRQT_PIRQ);
515
516 if (!VALID_EVTCHN(evtchn))
517 return;
518
519 mask_evtchn(evtchn);
520
521 close.port = evtchn;
522 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
523 BUG();
524
525 bind_evtchn_to_cpu(evtchn, 0);
526 evtchn_to_irq[evtchn] = -1;
527 info->evtchn = 0;
528}
529
530static void enable_pirq(unsigned int irq)
531{
532 startup_pirq(irq);
533}
534
535static void disable_pirq(unsigned int irq)
536{
537}
538
539static void ack_pirq(unsigned int irq)
540{
541 int evtchn = evtchn_from_irq(irq);
542
543 move_native_irq(irq);
544
545 if (VALID_EVTCHN(evtchn)) {
546 mask_evtchn(evtchn);
547 clear_evtchn(evtchn);
548 }
549}
550
551static void end_pirq(unsigned int irq)
552{
553 int evtchn = evtchn_from_irq(irq);
554 struct irq_desc *desc = irq_to_desc(irq);
555
556 if (WARN_ON(!desc))
557 return;
558
559 if ((desc->status & (IRQ_DISABLED|IRQ_PENDING)) ==
560 (IRQ_DISABLED|IRQ_PENDING)) {
561 shutdown_pirq(irq);
562 } else if (VALID_EVTCHN(evtchn)) {
563 unmask_evtchn(evtchn);
564 pirq_unmask_notify(irq);
565 }
566}
567
568static int find_irq_by_gsi(unsigned gsi)
569{
570 int irq;
571
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -0400572 for (irq = 0; irq < nr_irqs; irq++) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400573 struct irq_info *info = info_for_irq(irq);
574
575 if (info == NULL || info->type != IRQT_PIRQ)
576 continue;
577
578 if (gsi_from_irq(irq) == gsi)
579 return irq;
580 }
581
582 return -1;
583}
584
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100585int xen_allocate_pirq(unsigned gsi, int shareable, char *name)
586{
587 return xen_map_pirq_gsi(gsi, gsi, shareable, name);
588}
589
590/* xen_map_pirq_gsi might allocate irqs from the top down, as a
Konrad Rzeszutek Wilk3a69e912010-10-18 10:49:10 -0400591 * consequence don't assume that the irq number returned has a low value
592 * or can be used as a pirq number unless you know otherwise.
593 *
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100594 * One notable exception is when xen_map_pirq_gsi is called passing an
Konrad Rzeszutek Wilk3a69e912010-10-18 10:49:10 -0400595 * hardware gsi as argument, in that case the irq number returned
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100596 * matches the gsi number passed as second argument.
597 *
598 * Note: We don't assign an event channel until the irq actually started
599 * up. Return an existing irq if we've already got one for the gsi.
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400600 */
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100601int xen_map_pirq_gsi(unsigned pirq, unsigned gsi, int shareable, char *name)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400602{
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100603 int irq = 0;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400604 struct physdev_irq irq_op;
605
606 spin_lock(&irq_mapping_update_lock);
607
Stefano Stabellini01557ba2010-08-20 14:46:52 +0100608 if ((pirq > nr_pirqs) || (gsi > nr_irqs)) {
609 printk(KERN_WARNING "xen_map_pirq_gsi: %s %s is incorrect!\n",
610 pirq > nr_pirqs ? "nr_pirqs" :"",
611 gsi > nr_irqs ? "nr_irqs" : "");
612 goto out;
613 }
614
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400615 irq = find_irq_by_gsi(gsi);
616 if (irq != -1) {
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100617 printk(KERN_INFO "xen_map_pirq_gsi: returning irq %d for gsi %u\n",
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400618 irq, gsi);
619 goto out; /* XXX need refcount? */
620 }
621
Alex Nixonb5401a92010-03-18 16:31:34 -0400622 /* If we are a PV guest, we don't have GSIs (no ACPI passed). Therefore
623 * we are using the !xen_initial_domain() to drop in the function.*/
624 if (identity_mapped_irq(gsi) || !xen_initial_domain()) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400625 irq = gsi;
Konrad Rzeszutek Wilk2c52f8d2010-10-18 17:11:10 -0400626 irq_alloc_desc_at(irq, 0);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400627 } else
628 irq = find_unbound_irq();
629
630 set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
Gerd Hoffmann1a60d052010-10-04 13:42:27 -0400631 handle_level_irq, name);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400632
633 irq_op.irq = irq;
Alex Nixonb5401a92010-03-18 16:31:34 -0400634 irq_op.vector = 0;
635
636 /* Only the privileged domain can do this. For non-priv, the pcifront
637 * driver provides a PCI bus that does the call to do exactly
638 * this in the priv domain. */
639 if (xen_initial_domain() &&
640 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
Konrad Rzeszutek Wilk2c52f8d2010-10-18 17:11:10 -0400641 irq_free_desc(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400642 irq = -ENOSPC;
643 goto out;
644 }
645
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100646 irq_info[irq] = mk_pirq_info(0, pirq, gsi, irq_op.vector);
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400647 irq_info[irq].u.pirq.flags |= shareable ? PIRQ_SHAREABLE : 0;
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100648 pirq_to_irq[pirq] = irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400649
650out:
651 spin_unlock(&irq_mapping_update_lock);
652
653 return irq;
654}
655
Alex Nixonb5401a92010-03-18 16:31:34 -0400656int xen_destroy_irq(int irq)
657{
658 struct irq_desc *desc;
659 int rc = -ENOENT;
660
661 spin_lock(&irq_mapping_update_lock);
662
663 desc = irq_to_desc(irq);
664 if (!desc)
665 goto out;
666
667 irq_info[irq] = mk_unbound_info();
668
Konrad Rzeszutek Wilk2c52f8d2010-10-18 17:11:10 -0400669 irq_free_desc(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400670
671out:
672 spin_unlock(&irq_mapping_update_lock);
673 return rc;
674}
675
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400676int xen_vector_from_irq(unsigned irq)
677{
678 return vector_from_irq(irq);
679}
680
681int xen_gsi_from_irq(unsigned irq)
682{
683 return gsi_from_irq(irq);
684}
685
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700686int bind_evtchn_to_irq(unsigned int evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700687{
688 int irq;
689
690 spin_lock(&irq_mapping_update_lock);
691
692 irq = evtchn_to_irq[evtchn];
693
694 if (irq == -1) {
695 irq = find_unbound_irq();
696
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700697 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
Jeremy Fitzhardingedffe2e12010-08-20 19:10:01 -0700698 handle_edge_irq, "event");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700699
700 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800701 irq_info[irq] = mk_evtchn_info(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700702 }
703
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700704 spin_unlock(&irq_mapping_update_lock);
705
706 return irq;
707}
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700708EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700709
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700710static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
711{
712 struct evtchn_bind_ipi bind_ipi;
713 int evtchn, irq;
714
715 spin_lock(&irq_mapping_update_lock);
716
717 irq = per_cpu(ipi_to_irq, cpu)[ipi];
Ian Campbell90af9512009-02-06 16:55:58 -0800718
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700719 if (irq == -1) {
720 irq = find_unbound_irq();
721 if (irq < 0)
722 goto out;
723
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700724 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
725 handle_percpu_irq, "ipi");
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700726
727 bind_ipi.vcpu = cpu;
728 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
729 &bind_ipi) != 0)
730 BUG();
731 evtchn = bind_ipi.port;
732
733 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800734 irq_info[irq] = mk_ipi_info(evtchn, ipi);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700735 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
736
737 bind_evtchn_to_cpu(evtchn, cpu);
738 }
739
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700740 out:
741 spin_unlock(&irq_mapping_update_lock);
742 return irq;
743}
744
745
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700746static int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
747{
748 struct evtchn_bind_virq bind_virq;
749 int evtchn, irq;
750
751 spin_lock(&irq_mapping_update_lock);
752
753 irq = per_cpu(virq_to_irq, cpu)[virq];
754
755 if (irq == -1) {
756 bind_virq.virq = virq;
757 bind_virq.vcpu = cpu;
758 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
759 &bind_virq) != 0)
760 BUG();
761 evtchn = bind_virq.port;
762
763 irq = find_unbound_irq();
764
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700765 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
766 handle_percpu_irq, "virq");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700767
768 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800769 irq_info[irq] = mk_virq_info(evtchn, virq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700770
771 per_cpu(virq_to_irq, cpu)[virq] = irq;
772
773 bind_evtchn_to_cpu(evtchn, cpu);
774 }
775
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700776 spin_unlock(&irq_mapping_update_lock);
777
778 return irq;
779}
780
781static void unbind_from_irq(unsigned int irq)
782{
783 struct evtchn_close close;
784 int evtchn = evtchn_from_irq(irq);
785
786 spin_lock(&irq_mapping_update_lock);
787
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800788 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700789 close.port = evtchn;
790 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
791 BUG();
792
793 switch (type_from_irq(irq)) {
794 case IRQT_VIRQ:
795 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800796 [virq_from_irq(irq)] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700797 break;
Alex Nixond68d82a2008-08-22 11:52:15 +0100798 case IRQT_IPI:
799 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800800 [ipi_from_irq(irq)] = -1;
Alex Nixond68d82a2008-08-22 11:52:15 +0100801 break;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700802 default:
803 break;
804 }
805
806 /* Closed ports are implicitly re-bound to VCPU0. */
807 bind_evtchn_to_cpu(evtchn, 0);
808
809 evtchn_to_irq[evtchn] = -1;
Ian Campbellfed5ea82009-12-01 16:15:30 +0000810 }
811
812 if (irq_info[irq].type != IRQT_UNBOUND) {
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800813 irq_info[irq] = mk_unbound_info();
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700814
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200815 irq_free_desc(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700816 }
817
818 spin_unlock(&irq_mapping_update_lock);
819}
820
821int bind_evtchn_to_irqhandler(unsigned int evtchn,
Jeff Garzik7c239972007-10-19 03:12:20 -0400822 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700823 unsigned long irqflags,
824 const char *devname, void *dev_id)
825{
826 unsigned int irq;
827 int retval;
828
829 irq = bind_evtchn_to_irq(evtchn);
830 retval = request_irq(irq, handler, irqflags, devname, dev_id);
831 if (retval != 0) {
832 unbind_from_irq(irq);
833 return retval;
834 }
835
836 return irq;
837}
838EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
839
840int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
Jeff Garzik7c239972007-10-19 03:12:20 -0400841 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700842 unsigned long irqflags, const char *devname, void *dev_id)
843{
844 unsigned int irq;
845 int retval;
846
847 irq = bind_virq_to_irq(virq, cpu);
848 retval = request_irq(irq, handler, irqflags, devname, dev_id);
849 if (retval != 0) {
850 unbind_from_irq(irq);
851 return retval;
852 }
853
854 return irq;
855}
856EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
857
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700858int bind_ipi_to_irqhandler(enum ipi_vector ipi,
859 unsigned int cpu,
860 irq_handler_t handler,
861 unsigned long irqflags,
862 const char *devname,
863 void *dev_id)
864{
865 int irq, retval;
866
867 irq = bind_ipi_to_irq(ipi, cpu);
868 if (irq < 0)
869 return irq;
870
Ian Campbell4877c732010-07-29 11:16:35 +0100871 irqflags |= IRQF_NO_SUSPEND;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700872 retval = request_irq(irq, handler, irqflags, devname, dev_id);
873 if (retval != 0) {
874 unbind_from_irq(irq);
875 return retval;
876 }
877
878 return irq;
879}
880
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700881void unbind_from_irqhandler(unsigned int irq, void *dev_id)
882{
883 free_irq(irq, dev_id);
884 unbind_from_irq(irq);
885}
886EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
887
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700888void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
889{
890 int irq = per_cpu(ipi_to_irq, cpu)[vector];
891 BUG_ON(irq < 0);
892 notify_remote_via_irq(irq);
893}
894
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700895irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
896{
897 struct shared_info *sh = HYPERVISOR_shared_info;
898 int cpu = smp_processor_id();
899 int i;
900 unsigned long flags;
901 static DEFINE_SPINLOCK(debug_lock);
902
903 spin_lock_irqsave(&debug_lock, flags);
904
905 printk("vcpu %d\n ", cpu);
906
907 for_each_online_cpu(i) {
908 struct vcpu_info *v = per_cpu(xen_vcpu, i);
909 printk("%d: masked=%d pending=%d event_sel %08lx\n ", i,
Isaku Yamahatae849c3e2008-04-02 10:53:56 -0700910 (get_irq_regs() && i == cpu) ? xen_irqs_disabled(get_irq_regs()) : v->evtchn_upcall_mask,
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700911 v->evtchn_upcall_pending,
912 v->evtchn_pending_sel);
913 }
914 printk("pending:\n ");
915 for(i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
916 printk("%08lx%s", sh->evtchn_pending[i],
917 i % 8 == 0 ? "\n " : " ");
918 printk("\nmasks:\n ");
919 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
920 printk("%08lx%s", sh->evtchn_mask[i],
921 i % 8 == 0 ? "\n " : " ");
922
923 printk("\nunmasked:\n ");
924 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
925 printk("%08lx%s", sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
926 i % 8 == 0 ? "\n " : " ");
927
928 printk("\npending list:\n");
929 for(i = 0; i < NR_EVENT_CHANNELS; i++) {
930 if (sync_test_bit(i, sh->evtchn_pending)) {
931 printk(" %d: event %d -> irq %d\n",
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800932 cpu_from_evtchn(i), i,
933 evtchn_to_irq[i]);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700934 }
935 }
936
937 spin_unlock_irqrestore(&debug_lock, flags);
938
939 return IRQ_HANDLED;
940}
941
Tejun Heo245b2e72009-06-24 15:13:48 +0900942static DEFINE_PER_CPU(unsigned, xed_nesting_count);
943
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700944/*
945 * Search the CPUs pending events bitmasks. For each one found, map
946 * the event number to an irq, and feed it into do_IRQ() for
947 * handling.
948 *
949 * Xen uses a two-level bitmap to speed searching. The first level is
950 * a bitset of words which contain pending event bits. The second
951 * level is a bitset of pending events themselves.
952 */
Sheng Yang38e20b02010-05-14 12:40:51 +0100953static void __xen_evtchn_do_upcall(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700954{
955 int cpu = get_cpu();
956 struct shared_info *s = HYPERVISOR_shared_info;
957 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700958 unsigned count;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700959
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700960 do {
961 unsigned long pending_words;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700962
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700963 vcpu_info->evtchn_upcall_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700964
Tejun Heo245b2e72009-06-24 15:13:48 +0900965 if (__get_cpu_var(xed_nesting_count)++)
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700966 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700967
Isaku Yamahatae849c3e2008-04-02 10:53:56 -0700968#ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
969 /* Clear master flag /before/ clearing selector flag. */
Isaku Yamahata6673cf62008-06-16 14:58:13 -0700970 wmb();
Isaku Yamahatae849c3e2008-04-02 10:53:56 -0700971#endif
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700972 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
973 while (pending_words != 0) {
974 unsigned long pending_bits;
975 int word_idx = __ffs(pending_words);
976 pending_words &= ~(1UL << word_idx);
977
978 while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
979 int bit_idx = __ffs(pending_bits);
980 int port = (word_idx * BITS_PER_LONG) + bit_idx;
981 int irq = evtchn_to_irq[port];
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -0800982 struct irq_desc *desc;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700983
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -0800984 if (irq != -1) {
985 desc = irq_to_desc(irq);
986 if (desc)
987 generic_handle_irq_desc(irq, desc);
988 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700989 }
990 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700991
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700992 BUG_ON(!irqs_disabled());
993
Tejun Heo245b2e72009-06-24 15:13:48 +0900994 count = __get_cpu_var(xed_nesting_count);
995 __get_cpu_var(xed_nesting_count) = 0;
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100996 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700997
998out:
Jeremy Fitzhardinge3445a8f2009-02-06 14:09:46 -0800999
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001000 put_cpu();
1001}
1002
Sheng Yang38e20b02010-05-14 12:40:51 +01001003void xen_evtchn_do_upcall(struct pt_regs *regs)
1004{
1005 struct pt_regs *old_regs = set_irq_regs(regs);
1006
1007 exit_idle();
1008 irq_enter();
1009
1010 __xen_evtchn_do_upcall();
1011
1012 irq_exit();
1013 set_irq_regs(old_regs);
1014}
1015
1016void xen_hvm_evtchn_do_upcall(void)
1017{
1018 __xen_evtchn_do_upcall();
1019}
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001020EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
Sheng Yang38e20b02010-05-14 12:40:51 +01001021
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001022/* Rebind a new event channel to an existing irq. */
1023void rebind_evtchn_irq(int evtchn, int irq)
1024{
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001025 struct irq_info *info = info_for_irq(irq);
1026
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001027 /* Make sure the irq is masked, since the new event channel
1028 will also be masked. */
1029 disable_irq(irq);
1030
1031 spin_lock(&irq_mapping_update_lock);
1032
1033 /* After resume the irq<->evtchn mappings are all cleared out */
1034 BUG_ON(evtchn_to_irq[evtchn] != -1);
1035 /* Expect irq to have been bound before,
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001036 so there should be a proper type */
1037 BUG_ON(info->type == IRQT_UNBOUND);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001038
1039 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001040 irq_info[irq] = mk_evtchn_info(evtchn);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001041
1042 spin_unlock(&irq_mapping_update_lock);
1043
1044 /* new event channels are always bound to cpu 0 */
Rusty Russell0de26522008-12-13 21:20:26 +10301045 irq_set_affinity(irq, cpumask_of(0));
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001046
1047 /* Unmask the event channel. */
1048 enable_irq(irq);
1049}
1050
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001051/* Rebind an evtchn so that it gets delivered to a specific cpu */
Yinghai Lud5dedd42009-04-27 17:59:21 -07001052static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001053{
1054 struct evtchn_bind_vcpu bind_vcpu;
1055 int evtchn = evtchn_from_irq(irq);
1056
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001057 /* events delivered via platform PCI interrupts are always
1058 * routed to vcpu 0 */
1059 if (!VALID_EVTCHN(evtchn) ||
1060 (xen_hvm_domain() && !xen_have_vector_callback))
Yinghai Lud5dedd42009-04-27 17:59:21 -07001061 return -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001062
1063 /* Send future instances of this interrupt to other vcpu. */
1064 bind_vcpu.port = evtchn;
1065 bind_vcpu.vcpu = tcpu;
1066
1067 /*
1068 * If this fails, it usually just indicates that we're dealing with a
1069 * virq or IPI channel, which don't actually need to be rebound. Ignore
1070 * it, but don't do the xenlinux-level rebind in that case.
1071 */
1072 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1073 bind_evtchn_to_cpu(evtchn, tcpu);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001074
1075 return 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001076}
1077
Yinghai Lud5dedd42009-04-27 17:59:21 -07001078static int set_affinity_irq(unsigned irq, const struct cpumask *dest)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001079{
Rusty Russell0de26522008-12-13 21:20:26 +10301080 unsigned tcpu = cpumask_first(dest);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001081
1082 return rebind_irq_to_cpu(irq, tcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001083}
1084
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001085int resend_irq_on_evtchn(unsigned int irq)
1086{
1087 int masked, evtchn = evtchn_from_irq(irq);
1088 struct shared_info *s = HYPERVISOR_shared_info;
1089
1090 if (!VALID_EVTCHN(evtchn))
1091 return 1;
1092
1093 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
1094 sync_set_bit(evtchn, s->evtchn_pending);
1095 if (!masked)
1096 unmask_evtchn(evtchn);
1097
1098 return 1;
1099}
1100
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001101static void enable_dynirq(unsigned int irq)
1102{
1103 int evtchn = evtchn_from_irq(irq);
1104
1105 if (VALID_EVTCHN(evtchn))
1106 unmask_evtchn(evtchn);
1107}
1108
1109static void disable_dynirq(unsigned int irq)
1110{
1111 int evtchn = evtchn_from_irq(irq);
1112
1113 if (VALID_EVTCHN(evtchn))
1114 mask_evtchn(evtchn);
1115}
1116
1117static void ack_dynirq(unsigned int irq)
1118{
1119 int evtchn = evtchn_from_irq(irq);
1120
1121 move_native_irq(irq);
1122
1123 if (VALID_EVTCHN(evtchn))
1124 clear_evtchn(evtchn);
1125}
1126
1127static int retrigger_dynirq(unsigned int irq)
1128{
1129 int evtchn = evtchn_from_irq(irq);
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001130 struct shared_info *sh = HYPERVISOR_shared_info;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001131 int ret = 0;
1132
1133 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001134 int masked;
1135
1136 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
1137 sync_set_bit(evtchn, sh->evtchn_pending);
1138 if (!masked)
1139 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001140 ret = 1;
1141 }
1142
1143 return ret;
1144}
1145
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001146static void restore_cpu_virqs(unsigned int cpu)
1147{
1148 struct evtchn_bind_virq bind_virq;
1149 int virq, irq, evtchn;
1150
1151 for (virq = 0; virq < NR_VIRQS; virq++) {
1152 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1153 continue;
1154
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001155 BUG_ON(virq_from_irq(irq) != virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001156
1157 /* Get a new binding from Xen. */
1158 bind_virq.virq = virq;
1159 bind_virq.vcpu = cpu;
1160 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1161 &bind_virq) != 0)
1162 BUG();
1163 evtchn = bind_virq.port;
1164
1165 /* Record the new mapping. */
1166 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001167 irq_info[irq] = mk_virq_info(evtchn, virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001168 bind_evtchn_to_cpu(evtchn, cpu);
1169
1170 /* Ready for use. */
1171 unmask_evtchn(evtchn);
1172 }
1173}
1174
1175static void restore_cpu_ipis(unsigned int cpu)
1176{
1177 struct evtchn_bind_ipi bind_ipi;
1178 int ipi, irq, evtchn;
1179
1180 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1181 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1182 continue;
1183
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001184 BUG_ON(ipi_from_irq(irq) != ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001185
1186 /* Get a new binding from Xen. */
1187 bind_ipi.vcpu = cpu;
1188 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1189 &bind_ipi) != 0)
1190 BUG();
1191 evtchn = bind_ipi.port;
1192
1193 /* Record the new mapping. */
1194 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001195 irq_info[irq] = mk_ipi_info(evtchn, ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001196 bind_evtchn_to_cpu(evtchn, cpu);
1197
1198 /* Ready for use. */
1199 unmask_evtchn(evtchn);
1200
1201 }
1202}
1203
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001204/* Clear an irq's pending state, in preparation for polling on it */
1205void xen_clear_irq_pending(int irq)
1206{
1207 int evtchn = evtchn_from_irq(irq);
1208
1209 if (VALID_EVTCHN(evtchn))
1210 clear_evtchn(evtchn);
1211}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001212EXPORT_SYMBOL(xen_clear_irq_pending);
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -07001213void xen_set_irq_pending(int irq)
1214{
1215 int evtchn = evtchn_from_irq(irq);
1216
1217 if (VALID_EVTCHN(evtchn))
1218 set_evtchn(evtchn);
1219}
1220
1221bool xen_test_irq_pending(int irq)
1222{
1223 int evtchn = evtchn_from_irq(irq);
1224 bool ret = false;
1225
1226 if (VALID_EVTCHN(evtchn))
1227 ret = test_evtchn(evtchn);
1228
1229 return ret;
1230}
1231
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001232/* Poll waiting for an irq to become pending with timeout. In the usual case,
1233 * the irq will be disabled so it won't deliver an interrupt. */
1234void xen_poll_irq_timeout(int irq, u64 timeout)
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001235{
1236 evtchn_port_t evtchn = evtchn_from_irq(irq);
1237
1238 if (VALID_EVTCHN(evtchn)) {
1239 struct sched_poll poll;
1240
1241 poll.nr_ports = 1;
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001242 poll.timeout = timeout;
Isaku Yamahataff3c5362008-10-14 17:50:44 -07001243 set_xen_guest_handle(poll.ports, &evtchn);
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001244
1245 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1246 BUG();
1247 }
1248}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001249EXPORT_SYMBOL(xen_poll_irq_timeout);
1250/* Poll waiting for an irq to become pending. In the usual case, the
1251 * irq will be disabled so it won't deliver an interrupt. */
1252void xen_poll_irq(int irq)
1253{
1254 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1255}
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001256
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001257void xen_irq_resume(void)
1258{
1259 unsigned int cpu, irq, evtchn;
1260
1261 init_evtchn_cpu_bindings();
1262
1263 /* New event-channel space is not 'live' yet. */
1264 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1265 mask_evtchn(evtchn);
1266
1267 /* No IRQ <-> event-channel mappings. */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001268 for (irq = 0; irq < nr_irqs; irq++)
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001269 irq_info[irq].evtchn = 0; /* zap event-channel binding */
1270
1271 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1272 evtchn_to_irq[evtchn] = -1;
1273
1274 for_each_possible_cpu(cpu) {
1275 restore_cpu_virqs(cpu);
1276 restore_cpu_ipis(cpu);
1277 }
1278}
1279
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001280static struct irq_chip xen_dynamic_chip __read_mostly = {
1281 .name = "xen-dyn",
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001282
1283 .disable = disable_dynirq,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001284 .mask = disable_dynirq,
1285 .unmask = enable_dynirq,
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001286
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001287 .ack = ack_dynirq,
1288 .set_affinity = set_affinity_irq,
1289 .retrigger = retrigger_dynirq,
1290};
1291
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001292static struct irq_chip xen_pirq_chip __read_mostly = {
1293 .name = "xen-pirq",
1294
1295 .startup = startup_pirq,
1296 .shutdown = shutdown_pirq,
1297
1298 .enable = enable_pirq,
1299 .unmask = enable_pirq,
1300
1301 .disable = disable_pirq,
1302 .mask = disable_pirq,
1303
1304 .ack = ack_pirq,
1305 .end = end_pirq,
1306
1307 .set_affinity = set_affinity_irq,
1308
1309 .retrigger = retrigger_dynirq,
1310};
1311
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001312static struct irq_chip xen_percpu_chip __read_mostly = {
1313 .name = "xen-percpu",
1314
1315 .disable = disable_dynirq,
1316 .mask = disable_dynirq,
1317 .unmask = enable_dynirq,
1318
1319 .ack = ack_dynirq,
1320};
1321
Sheng Yang38e20b02010-05-14 12:40:51 +01001322int xen_set_callback_via(uint64_t via)
1323{
1324 struct xen_hvm_param a;
1325 a.domid = DOMID_SELF;
1326 a.index = HVM_PARAM_CALLBACK_IRQ;
1327 a.value = via;
1328 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1329}
1330EXPORT_SYMBOL_GPL(xen_set_callback_via);
1331
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001332#ifdef CONFIG_XEN_PVHVM
Sheng Yang38e20b02010-05-14 12:40:51 +01001333/* Vector callbacks are better than PCI interrupts to receive event
1334 * channel notifications because we can receive vector callbacks on any
1335 * vcpu and we don't need PCI support or APIC interactions. */
1336void xen_callback_vector(void)
1337{
1338 int rc;
1339 uint64_t callback_via;
1340 if (xen_have_vector_callback) {
1341 callback_via = HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK);
1342 rc = xen_set_callback_via(callback_via);
1343 if (rc) {
1344 printk(KERN_ERR "Request for Xen HVM callback vector"
1345 " failed.\n");
1346 xen_have_vector_callback = 0;
1347 return;
1348 }
1349 printk(KERN_INFO "Xen HVM callback vector for event delivery is "
1350 "enabled\n");
1351 /* in the restore case the vector has already been allocated */
1352 if (!test_bit(XEN_HVM_EVTCHN_CALLBACK, used_vectors))
1353 alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK, xen_hvm_callback_vector);
1354 }
1355}
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001356#else
1357void xen_callback_vector(void) {}
1358#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001359
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001360void __init xen_init_IRQ(void)
1361{
Stefano Stabellini01557ba2010-08-20 14:46:52 +01001362 int i, rc;
1363 struct physdev_nr_pirqs op_nr_pirqs;
Mike Travisc7a35892009-01-10 21:58:11 -08001364
Pekka Enberga70c3522009-07-01 11:51:18 +03001365 cpu_evtchn_mask_p = kcalloc(nr_cpu_ids, sizeof(struct cpu_evtchn_s),
1366 GFP_KERNEL);
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001367 irq_info = kcalloc(nr_irqs, sizeof(*irq_info), GFP_KERNEL);
1368
Stefano Stabellini01557ba2010-08-20 14:46:52 +01001369 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_nr_pirqs, &op_nr_pirqs);
1370 if (rc < 0) {
1371 nr_pirqs = nr_irqs;
1372 if (rc != -ENOSYS)
1373 printk(KERN_WARNING "PHYSDEVOP_get_nr_pirqs returned rc=%d\n", rc);
1374 } else {
1375 if (xen_pv_domain() && !xen_initial_domain())
1376 nr_pirqs = max((int)op_nr_pirqs.nr_pirqs, nr_irqs);
1377 else
1378 nr_pirqs = op_nr_pirqs.nr_pirqs;
1379 }
1380 pirq_to_irq = kcalloc(nr_pirqs, sizeof(*pirq_to_irq), GFP_KERNEL);
1381 for (i = 0; i < nr_pirqs; i++)
Stefano Stabellini7a043f12010-07-01 17:08:14 +01001382 pirq_to_irq[i] = -1;
1383
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001384 evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
1385 GFP_KERNEL);
1386 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1387 evtchn_to_irq[i] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001388
1389 init_evtchn_cpu_bindings();
1390
1391 /* No event channels are 'live' right now. */
1392 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1393 mask_evtchn(i);
1394
Sheng Yang38e20b02010-05-14 12:40:51 +01001395 if (xen_hvm_domain()) {
1396 xen_callback_vector();
1397 native_init_IRQ();
1398 } else {
1399 irq_ctx_init(smp_processor_id());
1400 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001401}