blob: 1bb51e459ab27018345f745eb645c906579d409f [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 Fitzhardingee46cdb62007-07-17 18:37:05 -070031
Sheng Yang38e20b02010-05-14 12:40:51 +010032#include <asm/desc.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070033#include <asm/ptrace.h>
34#include <asm/irq.h>
Jeremy Fitzhardinge792dc4f2009-02-06 14:09:43 -080035#include <asm/idle.h>
Konrad Rzeszutek Wilk0794bfc2010-10-18 10:41:08 -040036#include <asm/io_apic.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070037#include <asm/sync_bitops.h>
38#include <asm/xen/hypercall.h>
Adrian Bunk8d1b8752007-07-20 00:31:44 -070039#include <asm/xen/hypervisor.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070040
Sheng Yang38e20b02010-05-14 12:40:51 +010041#include <xen/xen.h>
42#include <xen/hvm.h>
Isaku Yamahatae04d0d02008-04-02 10:53:55 -070043#include <xen/xen-ops.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070044#include <xen/events.h>
45#include <xen/interface/xen.h>
46#include <xen/interface/event_channel.h>
Sheng Yang38e20b02010-05-14 12:40:51 +010047#include <xen/interface/hvm/hvm_op.h>
48#include <xen/interface/hvm/params.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070049
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070050/*
51 * This lock protects updates to the following mapping and reference-count
52 * arrays. The lock does not need to be acquired to read the mapping tables.
53 */
54static DEFINE_SPINLOCK(irq_mapping_update_lock);
55
56/* IRQ <-> VIRQ mapping. */
Tejun Heo204fba42009-06-24 15:13:45 +090057static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070058
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070059/* IRQ <-> IPI mapping */
Tejun Heo204fba42009-06-24 15:13:45 +090060static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070061
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080062/* Interrupt types. */
63enum xen_irq_type {
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -080064 IRQT_UNBOUND = 0,
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070065 IRQT_PIRQ,
66 IRQT_VIRQ,
67 IRQT_IPI,
68 IRQT_EVTCHN
69};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070070
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080071/*
72 * Packed IRQ information:
73 * type - enum xen_irq_type
74 * event channel - irq->event channel mapping
75 * cpu - cpu this event channel is bound to
76 * index - type-specific information:
77 * PIRQ - vector, with MSB being "needs EIO"
78 * VIRQ - virq number
79 * IPI - IPI vector
80 * EVTCHN -
81 */
82struct irq_info
83{
84 enum xen_irq_type type; /* type */
85 unsigned short evtchn; /* event channel */
86 unsigned short cpu; /* cpu bound */
87
88 union {
89 unsigned short virq;
90 enum ipi_vector ipi;
91 struct {
92 unsigned short gsi;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -040093 unsigned char vector;
94 unsigned char flags;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080095 } pirq;
96 } u;
97};
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -040098#define PIRQ_NEEDS_EOI (1 << 0)
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080099
100static struct irq_info irq_info[NR_IRQS];
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700101
102static int evtchn_to_irq[NR_EVENT_CHANNELS] = {
103 [0 ... NR_EVENT_CHANNELS-1] = -1
104};
Mike Travisc7a35892009-01-10 21:58:11 -0800105struct cpu_evtchn_s {
106 unsigned long bits[NR_EVENT_CHANNELS/BITS_PER_LONG];
107};
108static struct cpu_evtchn_s *cpu_evtchn_mask_p;
109static inline unsigned long *cpu_evtchn_mask(int cpu)
110{
111 return cpu_evtchn_mask_p[cpu].bits;
112}
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700113
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700114/* Xen will never allocate port zero for any purpose. */
115#define VALID_EVTCHN(chn) ((chn) != 0)
116
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700117static struct irq_chip xen_dynamic_chip;
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700118static struct irq_chip xen_percpu_chip;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400119static struct irq_chip xen_pirq_chip;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700120
121/* Constructor for packed IRQ information. */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800122static struct irq_info mk_unbound_info(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700123{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800124 return (struct irq_info) { .type = IRQT_UNBOUND };
125}
126
127static struct irq_info mk_evtchn_info(unsigned short evtchn)
128{
Ian Campbell90af9512009-02-06 16:55:58 -0800129 return (struct irq_info) { .type = IRQT_EVTCHN, .evtchn = evtchn,
130 .cpu = 0 };
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800131}
132
133static struct irq_info mk_ipi_info(unsigned short evtchn, enum ipi_vector ipi)
134{
135 return (struct irq_info) { .type = IRQT_IPI, .evtchn = evtchn,
Ian Campbell90af9512009-02-06 16:55:58 -0800136 .cpu = 0, .u.ipi = ipi };
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800137}
138
139static struct irq_info mk_virq_info(unsigned short evtchn, unsigned short virq)
140{
141 return (struct irq_info) { .type = IRQT_VIRQ, .evtchn = evtchn,
Ian Campbell90af9512009-02-06 16:55:58 -0800142 .cpu = 0, .u.virq = virq };
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800143}
144
145static struct irq_info mk_pirq_info(unsigned short evtchn,
146 unsigned short gsi, unsigned short vector)
147{
148 return (struct irq_info) { .type = IRQT_PIRQ, .evtchn = evtchn,
Ian Campbell90af9512009-02-06 16:55:58 -0800149 .cpu = 0, .u.pirq = { .gsi = gsi, .vector = vector } };
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700150}
151
152/*
153 * Accessors for packed IRQ information.
154 */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800155static struct irq_info *info_for_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700156{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800157 return &irq_info[irq];
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700158}
159
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800160static unsigned int evtchn_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700161{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800162 return info_for_irq(irq)->evtchn;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700163}
164
Ian Campbelld4c04532009-02-06 19:20:31 -0800165unsigned irq_from_evtchn(unsigned int evtchn)
166{
167 return evtchn_to_irq[evtchn];
168}
169EXPORT_SYMBOL_GPL(irq_from_evtchn);
170
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800171static enum ipi_vector ipi_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700172{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800173 struct irq_info *info = info_for_irq(irq);
174
175 BUG_ON(info == NULL);
176 BUG_ON(info->type != IRQT_IPI);
177
178 return info->u.ipi;
179}
180
181static unsigned virq_from_irq(unsigned irq)
182{
183 struct irq_info *info = info_for_irq(irq);
184
185 BUG_ON(info == NULL);
186 BUG_ON(info->type != IRQT_VIRQ);
187
188 return info->u.virq;
189}
190
191static unsigned gsi_from_irq(unsigned irq)
192{
193 struct irq_info *info = info_for_irq(irq);
194
195 BUG_ON(info == NULL);
196 BUG_ON(info->type != IRQT_PIRQ);
197
198 return info->u.pirq.gsi;
199}
200
201static unsigned vector_from_irq(unsigned irq)
202{
203 struct irq_info *info = info_for_irq(irq);
204
205 BUG_ON(info == NULL);
206 BUG_ON(info->type != IRQT_PIRQ);
207
208 return info->u.pirq.vector;
209}
210
211static enum xen_irq_type type_from_irq(unsigned irq)
212{
213 return info_for_irq(irq)->type;
214}
215
216static unsigned cpu_from_irq(unsigned irq)
217{
218 return info_for_irq(irq)->cpu;
219}
220
221static unsigned int cpu_from_evtchn(unsigned int evtchn)
222{
223 int irq = evtchn_to_irq[evtchn];
224 unsigned ret = 0;
225
226 if (irq != -1)
227 ret = cpu_from_irq(irq);
228
229 return ret;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700230}
231
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400232static bool pirq_needs_eoi(unsigned irq)
233{
234 struct irq_info *info = info_for_irq(irq);
235
236 BUG_ON(info->type != IRQT_PIRQ);
237
238 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
239}
240
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700241static inline unsigned long active_evtchns(unsigned int cpu,
242 struct shared_info *sh,
243 unsigned int idx)
244{
245 return (sh->evtchn_pending[idx] &
Mike Travisc7a35892009-01-10 21:58:11 -0800246 cpu_evtchn_mask(cpu)[idx] &
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700247 ~sh->evtchn_mask[idx]);
248}
249
250static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
251{
252 int irq = evtchn_to_irq[chn];
253
254 BUG_ON(irq == -1);
255#ifdef CONFIG_SMP
Mike Travis7f7ace02009-01-10 21:58:08 -0800256 cpumask_copy(irq_to_desc(irq)->affinity, cpumask_of(cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700257#endif
258
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800259 __clear_bit(chn, cpu_evtchn_mask(cpu_from_irq(irq)));
Mike Travisc7a35892009-01-10 21:58:11 -0800260 __set_bit(chn, cpu_evtchn_mask(cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700261
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800262 irq_info[irq].cpu = cpu;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700263}
264
265static void init_evtchn_cpu_bindings(void)
266{
267#ifdef CONFIG_SMP
Thomas Gleixner10e58082008-10-16 14:19:04 +0200268 struct irq_desc *desc;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700269 int i;
Thomas Gleixner10e58082008-10-16 14:19:04 +0200270
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700271 /* By default all event channels notify CPU#0. */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800272 for_each_irq_desc(i, desc) {
Mike Travis7f7ace02009-01-10 21:58:08 -0800273 cpumask_copy(desc->affinity, cpumask_of(0));
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800274 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700275#endif
276
Mike Travisc7a35892009-01-10 21:58:11 -0800277 memset(cpu_evtchn_mask(0), ~0, sizeof(cpu_evtchn_mask(0)));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700278}
279
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700280static inline void clear_evtchn(int port)
281{
282 struct shared_info *s = HYPERVISOR_shared_info;
283 sync_clear_bit(port, &s->evtchn_pending[0]);
284}
285
286static inline void set_evtchn(int port)
287{
288 struct shared_info *s = HYPERVISOR_shared_info;
289 sync_set_bit(port, &s->evtchn_pending[0]);
290}
291
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700292static inline int test_evtchn(int port)
293{
294 struct shared_info *s = HYPERVISOR_shared_info;
295 return sync_test_bit(port, &s->evtchn_pending[0]);
296}
297
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700298
299/**
300 * notify_remote_via_irq - send event to remote end of event channel via irq
301 * @irq: irq of event channel to send event to
302 *
303 * Unlike notify_remote_via_evtchn(), this is safe to use across
304 * save/restore. Notifications on a broken connection are silently
305 * dropped.
306 */
307void notify_remote_via_irq(int irq)
308{
309 int evtchn = evtchn_from_irq(irq);
310
311 if (VALID_EVTCHN(evtchn))
312 notify_remote_via_evtchn(evtchn);
313}
314EXPORT_SYMBOL_GPL(notify_remote_via_irq);
315
316static void mask_evtchn(int port)
317{
318 struct shared_info *s = HYPERVISOR_shared_info;
319 sync_set_bit(port, &s->evtchn_mask[0]);
320}
321
322static void unmask_evtchn(int port)
323{
324 struct shared_info *s = HYPERVISOR_shared_info;
325 unsigned int cpu = get_cpu();
326
327 BUG_ON(!irqs_disabled());
328
329 /* Slow path (hypercall) if this is a non-local port. */
330 if (unlikely(cpu != cpu_from_evtchn(port))) {
331 struct evtchn_unmask unmask = { .port = port };
332 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
333 } else {
334 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
335
336 sync_clear_bit(port, &s->evtchn_mask[0]);
337
338 /*
339 * The following is basically the equivalent of
340 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
341 * the interrupt edge' if the channel is masked.
342 */
343 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
344 !sync_test_and_set_bit(port / BITS_PER_LONG,
345 &vcpu_info->evtchn_pending_sel))
346 vcpu_info->evtchn_upcall_pending = 1;
347 }
348
349 put_cpu();
350}
351
Konrad Rzeszutek Wilk0794bfc2010-10-18 10:41:08 -0400352static int get_nr_hw_irqs(void)
353{
354 int ret = 1;
355
356#ifdef CONFIG_X86_IO_APIC
357 ret = get_nr_irqs_gsi();
358#endif
359
360 return ret;
361}
362
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700363static int find_unbound_irq(void)
364{
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200365 struct irq_data *data;
366 int irq, res;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700367
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100368 for (irq = 0; irq < nr_irqs; irq++) {
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200369 data = irq_get_irq_data(irq);
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100370 /* only 0->15 have init'd desc; handle irq > 16 */
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200371 if (!data)
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100372 break;
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200373 if (data->chip == &no_irq_chip)
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100374 break;
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200375 if (data->chip != &xen_dynamic_chip)
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100376 continue;
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800377 if (irq_info[irq].type == IRQT_UNBOUND)
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200378 return irq;
Stefano Stabellini99ad1982010-05-14 12:41:20 +0100379 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700380
Yinghai Lu5a15d7e2008-08-19 20:49:57 -0700381 if (irq == nr_irqs)
382 panic("No available IRQ to bind to: increase nr_irqs!\n");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700383
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200384 res = irq_alloc_desc_at(irq, 0);
Jeremy Fitzhardinge6f8a0ed2008-12-17 13:42:29 -0800385
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200386 if (WARN_ON(res != irq))
387 return -1;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800388
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700389 return irq;
390}
391
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400392static bool identity_mapped_irq(unsigned irq)
393{
Konrad Rzeszutek Wilk0794bfc2010-10-18 10:41:08 -0400394 /* identity map all the hardware irqs */
395 return irq < get_nr_hw_irqs();
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400396}
397
398static void pirq_unmask_notify(int irq)
399{
400 struct physdev_eoi eoi = { .irq = irq };
401
402 if (unlikely(pirq_needs_eoi(irq))) {
403 int rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
404 WARN_ON(rc);
405 }
406}
407
408static void pirq_query_unmask(int irq)
409{
410 struct physdev_irq_status_query irq_status;
411 struct irq_info *info = info_for_irq(irq);
412
413 BUG_ON(info->type != IRQT_PIRQ);
414
415 irq_status.irq = irq;
416 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
417 irq_status.flags = 0;
418
419 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
420 if (irq_status.flags & XENIRQSTAT_needs_eoi)
421 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
422}
423
424static bool probing_irq(int irq)
425{
426 struct irq_desc *desc = irq_to_desc(irq);
427
428 return desc && desc->action == NULL;
429}
430
431static unsigned int startup_pirq(unsigned int irq)
432{
433 struct evtchn_bind_pirq bind_pirq;
434 struct irq_info *info = info_for_irq(irq);
435 int evtchn = evtchn_from_irq(irq);
436
437 BUG_ON(info->type != IRQT_PIRQ);
438
439 if (VALID_EVTCHN(evtchn))
440 goto out;
441
442 bind_pirq.pirq = irq;
443 /* NB. We are happy to share unless we are probing. */
444 bind_pirq.flags = probing_irq(irq) ? 0 : BIND_PIRQ__WILL_SHARE;
445 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq) != 0) {
446 if (!probing_irq(irq))
447 printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
448 irq);
449 return 0;
450 }
451 evtchn = bind_pirq.port;
452
453 pirq_query_unmask(irq);
454
455 evtchn_to_irq[evtchn] = irq;
456 bind_evtchn_to_cpu(evtchn, 0);
457 info->evtchn = evtchn;
458
459out:
460 unmask_evtchn(evtchn);
461 pirq_unmask_notify(irq);
462
463 return 0;
464}
465
466static void shutdown_pirq(unsigned int irq)
467{
468 struct evtchn_close close;
469 struct irq_info *info = info_for_irq(irq);
470 int evtchn = evtchn_from_irq(irq);
471
472 BUG_ON(info->type != IRQT_PIRQ);
473
474 if (!VALID_EVTCHN(evtchn))
475 return;
476
477 mask_evtchn(evtchn);
478
479 close.port = evtchn;
480 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
481 BUG();
482
483 bind_evtchn_to_cpu(evtchn, 0);
484 evtchn_to_irq[evtchn] = -1;
485 info->evtchn = 0;
486}
487
488static void enable_pirq(unsigned int irq)
489{
490 startup_pirq(irq);
491}
492
493static void disable_pirq(unsigned int irq)
494{
495}
496
497static void ack_pirq(unsigned int irq)
498{
499 int evtchn = evtchn_from_irq(irq);
500
501 move_native_irq(irq);
502
503 if (VALID_EVTCHN(evtchn)) {
504 mask_evtchn(evtchn);
505 clear_evtchn(evtchn);
506 }
507}
508
509static void end_pirq(unsigned int irq)
510{
511 int evtchn = evtchn_from_irq(irq);
512 struct irq_desc *desc = irq_to_desc(irq);
513
514 if (WARN_ON(!desc))
515 return;
516
517 if ((desc->status & (IRQ_DISABLED|IRQ_PENDING)) ==
518 (IRQ_DISABLED|IRQ_PENDING)) {
519 shutdown_pirq(irq);
520 } else if (VALID_EVTCHN(evtchn)) {
521 unmask_evtchn(evtchn);
522 pirq_unmask_notify(irq);
523 }
524}
525
526static int find_irq_by_gsi(unsigned gsi)
527{
528 int irq;
529
530 for (irq = 0; irq < NR_IRQS; irq++) {
531 struct irq_info *info = info_for_irq(irq);
532
533 if (info == NULL || info->type != IRQT_PIRQ)
534 continue;
535
536 if (gsi_from_irq(irq) == gsi)
537 return irq;
538 }
539
540 return -1;
541}
542
543/*
544 * Allocate a physical irq, along with a vector. We don't assign an
545 * event channel until the irq actually started up. Return an
546 * existing irq if we've already got one for the gsi.
547 */
548int xen_allocate_pirq(unsigned gsi)
549{
550 int irq;
551 struct physdev_irq irq_op;
552
553 spin_lock(&irq_mapping_update_lock);
554
555 irq = find_irq_by_gsi(gsi);
556 if (irq != -1) {
557 printk(KERN_INFO "xen_allocate_pirq: returning irq %d for gsi %u\n",
558 irq, gsi);
559 goto out; /* XXX need refcount? */
560 }
561
562 if (identity_mapped_irq(gsi)) {
563 irq = gsi;
Konrad Rzeszutek Wilk0794bfc2010-10-18 10:41:08 -0400564 irq_to_desc_alloc_node(irq, 0);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400565 dynamic_irq_init(irq);
566 } else
567 irq = find_unbound_irq();
568
569 set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
570 handle_level_irq, "pirq");
571
572 irq_op.irq = irq;
573 if (HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
574 dynamic_irq_cleanup(irq);
575 irq = -ENOSPC;
576 goto out;
577 }
578
579 irq_info[irq] = mk_pirq_info(0, gsi, irq_op.vector);
580
581out:
582 spin_unlock(&irq_mapping_update_lock);
583
584 return irq;
585}
586
587int xen_vector_from_irq(unsigned irq)
588{
589 return vector_from_irq(irq);
590}
591
592int xen_gsi_from_irq(unsigned irq)
593{
594 return gsi_from_irq(irq);
595}
596
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700597int bind_evtchn_to_irq(unsigned int evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700598{
599 int irq;
600
601 spin_lock(&irq_mapping_update_lock);
602
603 irq = evtchn_to_irq[evtchn];
604
605 if (irq == -1) {
606 irq = find_unbound_irq();
607
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700608 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
Jeremy Fitzhardingedffe2e12010-08-20 19:10:01 -0700609 handle_edge_irq, "event");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700610
611 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800612 irq_info[irq] = mk_evtchn_info(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700613 }
614
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700615 spin_unlock(&irq_mapping_update_lock);
616
617 return irq;
618}
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700619EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700620
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700621static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
622{
623 struct evtchn_bind_ipi bind_ipi;
624 int evtchn, irq;
625
626 spin_lock(&irq_mapping_update_lock);
627
628 irq = per_cpu(ipi_to_irq, cpu)[ipi];
Ian Campbell90af9512009-02-06 16:55:58 -0800629
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700630 if (irq == -1) {
631 irq = find_unbound_irq();
632 if (irq < 0)
633 goto out;
634
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700635 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
636 handle_percpu_irq, "ipi");
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700637
638 bind_ipi.vcpu = cpu;
639 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
640 &bind_ipi) != 0)
641 BUG();
642 evtchn = bind_ipi.port;
643
644 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800645 irq_info[irq] = mk_ipi_info(evtchn, ipi);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700646 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
647
648 bind_evtchn_to_cpu(evtchn, cpu);
649 }
650
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700651 out:
652 spin_unlock(&irq_mapping_update_lock);
653 return irq;
654}
655
656
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700657static int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
658{
659 struct evtchn_bind_virq bind_virq;
660 int evtchn, irq;
661
662 spin_lock(&irq_mapping_update_lock);
663
664 irq = per_cpu(virq_to_irq, cpu)[virq];
665
666 if (irq == -1) {
667 bind_virq.virq = virq;
668 bind_virq.vcpu = cpu;
669 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
670 &bind_virq) != 0)
671 BUG();
672 evtchn = bind_virq.port;
673
674 irq = find_unbound_irq();
675
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700676 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
677 handle_percpu_irq, "virq");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700678
679 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800680 irq_info[irq] = mk_virq_info(evtchn, virq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700681
682 per_cpu(virq_to_irq, cpu)[virq] = irq;
683
684 bind_evtchn_to_cpu(evtchn, cpu);
685 }
686
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700687 spin_unlock(&irq_mapping_update_lock);
688
689 return irq;
690}
691
692static void unbind_from_irq(unsigned int irq)
693{
694 struct evtchn_close close;
695 int evtchn = evtchn_from_irq(irq);
696
697 spin_lock(&irq_mapping_update_lock);
698
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800699 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700700 close.port = evtchn;
701 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
702 BUG();
703
704 switch (type_from_irq(irq)) {
705 case IRQT_VIRQ:
706 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800707 [virq_from_irq(irq)] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700708 break;
Alex Nixond68d82a2008-08-22 11:52:15 +0100709 case IRQT_IPI:
710 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800711 [ipi_from_irq(irq)] = -1;
Alex Nixond68d82a2008-08-22 11:52:15 +0100712 break;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700713 default:
714 break;
715 }
716
717 /* Closed ports are implicitly re-bound to VCPU0. */
718 bind_evtchn_to_cpu(evtchn, 0);
719
720 evtchn_to_irq[evtchn] = -1;
Ian Campbellfed5ea82009-12-01 16:15:30 +0000721 }
722
723 if (irq_info[irq].type != IRQT_UNBOUND) {
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800724 irq_info[irq] = mk_unbound_info();
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700725
Thomas Gleixner77dff1c2010-09-29 17:37:10 +0200726 irq_free_desc(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700727 }
728
729 spin_unlock(&irq_mapping_update_lock);
730}
731
732int bind_evtchn_to_irqhandler(unsigned int evtchn,
Jeff Garzik7c239972007-10-19 03:12:20 -0400733 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700734 unsigned long irqflags,
735 const char *devname, void *dev_id)
736{
737 unsigned int irq;
738 int retval;
739
740 irq = bind_evtchn_to_irq(evtchn);
741 retval = request_irq(irq, handler, irqflags, devname, dev_id);
742 if (retval != 0) {
743 unbind_from_irq(irq);
744 return retval;
745 }
746
747 return irq;
748}
749EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
750
751int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
Jeff Garzik7c239972007-10-19 03:12:20 -0400752 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700753 unsigned long irqflags, const char *devname, void *dev_id)
754{
755 unsigned int irq;
756 int retval;
757
758 irq = bind_virq_to_irq(virq, cpu);
759 retval = request_irq(irq, handler, irqflags, devname, dev_id);
760 if (retval != 0) {
761 unbind_from_irq(irq);
762 return retval;
763 }
764
765 return irq;
766}
767EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
768
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700769int bind_ipi_to_irqhandler(enum ipi_vector ipi,
770 unsigned int cpu,
771 irq_handler_t handler,
772 unsigned long irqflags,
773 const char *devname,
774 void *dev_id)
775{
776 int irq, retval;
777
778 irq = bind_ipi_to_irq(ipi, cpu);
779 if (irq < 0)
780 return irq;
781
Ian Campbell4877c732010-07-29 11:16:35 +0100782 irqflags |= IRQF_NO_SUSPEND;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700783 retval = request_irq(irq, handler, irqflags, devname, dev_id);
784 if (retval != 0) {
785 unbind_from_irq(irq);
786 return retval;
787 }
788
789 return irq;
790}
791
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700792void unbind_from_irqhandler(unsigned int irq, void *dev_id)
793{
794 free_irq(irq, dev_id);
795 unbind_from_irq(irq);
796}
797EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
798
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700799void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
800{
801 int irq = per_cpu(ipi_to_irq, cpu)[vector];
802 BUG_ON(irq < 0);
803 notify_remote_via_irq(irq);
804}
805
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700806irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
807{
808 struct shared_info *sh = HYPERVISOR_shared_info;
809 int cpu = smp_processor_id();
810 int i;
811 unsigned long flags;
812 static DEFINE_SPINLOCK(debug_lock);
813
814 spin_lock_irqsave(&debug_lock, flags);
815
816 printk("vcpu %d\n ", cpu);
817
818 for_each_online_cpu(i) {
819 struct vcpu_info *v = per_cpu(xen_vcpu, i);
820 printk("%d: masked=%d pending=%d event_sel %08lx\n ", i,
Isaku Yamahatae849c3e2008-04-02 10:53:56 -0700821 (get_irq_regs() && i == cpu) ? xen_irqs_disabled(get_irq_regs()) : v->evtchn_upcall_mask,
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700822 v->evtchn_upcall_pending,
823 v->evtchn_pending_sel);
824 }
825 printk("pending:\n ");
826 for(i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
827 printk("%08lx%s", sh->evtchn_pending[i],
828 i % 8 == 0 ? "\n " : " ");
829 printk("\nmasks:\n ");
830 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
831 printk("%08lx%s", sh->evtchn_mask[i],
832 i % 8 == 0 ? "\n " : " ");
833
834 printk("\nunmasked:\n ");
835 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
836 printk("%08lx%s", sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
837 i % 8 == 0 ? "\n " : " ");
838
839 printk("\npending list:\n");
840 for(i = 0; i < NR_EVENT_CHANNELS; i++) {
841 if (sync_test_bit(i, sh->evtchn_pending)) {
842 printk(" %d: event %d -> irq %d\n",
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800843 cpu_from_evtchn(i), i,
844 evtchn_to_irq[i]);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700845 }
846 }
847
848 spin_unlock_irqrestore(&debug_lock, flags);
849
850 return IRQ_HANDLED;
851}
852
Tejun Heo245b2e72009-06-24 15:13:48 +0900853static DEFINE_PER_CPU(unsigned, xed_nesting_count);
854
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700855/*
856 * Search the CPUs pending events bitmasks. For each one found, map
857 * the event number to an irq, and feed it into do_IRQ() for
858 * handling.
859 *
860 * Xen uses a two-level bitmap to speed searching. The first level is
861 * a bitset of words which contain pending event bits. The second
862 * level is a bitset of pending events themselves.
863 */
Sheng Yang38e20b02010-05-14 12:40:51 +0100864static void __xen_evtchn_do_upcall(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700865{
866 int cpu = get_cpu();
867 struct shared_info *s = HYPERVISOR_shared_info;
868 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700869 unsigned count;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700870
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700871 do {
872 unsigned long pending_words;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700873
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700874 vcpu_info->evtchn_upcall_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700875
Tejun Heo245b2e72009-06-24 15:13:48 +0900876 if (__get_cpu_var(xed_nesting_count)++)
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700877 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700878
Isaku Yamahatae849c3e2008-04-02 10:53:56 -0700879#ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
880 /* Clear master flag /before/ clearing selector flag. */
Isaku Yamahata6673cf62008-06-16 14:58:13 -0700881 wmb();
Isaku Yamahatae849c3e2008-04-02 10:53:56 -0700882#endif
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700883 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
884 while (pending_words != 0) {
885 unsigned long pending_bits;
886 int word_idx = __ffs(pending_words);
887 pending_words &= ~(1UL << word_idx);
888
889 while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
890 int bit_idx = __ffs(pending_bits);
891 int port = (word_idx * BITS_PER_LONG) + bit_idx;
892 int irq = evtchn_to_irq[port];
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -0800893 struct irq_desc *desc;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700894
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -0800895 if (irq != -1) {
896 desc = irq_to_desc(irq);
897 if (desc)
898 generic_handle_irq_desc(irq, desc);
899 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700900 }
901 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700902
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700903 BUG_ON(!irqs_disabled());
904
Tejun Heo245b2e72009-06-24 15:13:48 +0900905 count = __get_cpu_var(xed_nesting_count);
906 __get_cpu_var(xed_nesting_count) = 0;
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100907 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700908
909out:
Jeremy Fitzhardinge3445a8f2009-02-06 14:09:46 -0800910
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700911 put_cpu();
912}
913
Sheng Yang38e20b02010-05-14 12:40:51 +0100914void xen_evtchn_do_upcall(struct pt_regs *regs)
915{
916 struct pt_regs *old_regs = set_irq_regs(regs);
917
918 exit_idle();
919 irq_enter();
920
921 __xen_evtchn_do_upcall();
922
923 irq_exit();
924 set_irq_regs(old_regs);
925}
926
927void xen_hvm_evtchn_do_upcall(void)
928{
929 __xen_evtchn_do_upcall();
930}
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100931EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
Sheng Yang38e20b02010-05-14 12:40:51 +0100932
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +0100933/* Rebind a new event channel to an existing irq. */
934void rebind_evtchn_irq(int evtchn, int irq)
935{
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800936 struct irq_info *info = info_for_irq(irq);
937
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +0100938 /* Make sure the irq is masked, since the new event channel
939 will also be masked. */
940 disable_irq(irq);
941
942 spin_lock(&irq_mapping_update_lock);
943
944 /* After resume the irq<->evtchn mappings are all cleared out */
945 BUG_ON(evtchn_to_irq[evtchn] != -1);
946 /* Expect irq to have been bound before,
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800947 so there should be a proper type */
948 BUG_ON(info->type == IRQT_UNBOUND);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +0100949
950 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800951 irq_info[irq] = mk_evtchn_info(evtchn);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +0100952
953 spin_unlock(&irq_mapping_update_lock);
954
955 /* new event channels are always bound to cpu 0 */
Rusty Russell0de26522008-12-13 21:20:26 +1030956 irq_set_affinity(irq, cpumask_of(0));
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +0100957
958 /* Unmask the event channel. */
959 enable_irq(irq);
960}
961
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700962/* Rebind an evtchn so that it gets delivered to a specific cpu */
Yinghai Lud5dedd42009-04-27 17:59:21 -0700963static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700964{
965 struct evtchn_bind_vcpu bind_vcpu;
966 int evtchn = evtchn_from_irq(irq);
967
Stefano Stabellini183d03c2010-05-17 17:08:21 +0100968 /* events delivered via platform PCI interrupts are always
969 * routed to vcpu 0 */
970 if (!VALID_EVTCHN(evtchn) ||
971 (xen_hvm_domain() && !xen_have_vector_callback))
Yinghai Lud5dedd42009-04-27 17:59:21 -0700972 return -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700973
974 /* Send future instances of this interrupt to other vcpu. */
975 bind_vcpu.port = evtchn;
976 bind_vcpu.vcpu = tcpu;
977
978 /*
979 * If this fails, it usually just indicates that we're dealing with a
980 * virq or IPI channel, which don't actually need to be rebound. Ignore
981 * it, but don't do the xenlinux-level rebind in that case.
982 */
983 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
984 bind_evtchn_to_cpu(evtchn, tcpu);
Yinghai Lud5dedd42009-04-27 17:59:21 -0700985
986 return 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700987}
988
Yinghai Lud5dedd42009-04-27 17:59:21 -0700989static int set_affinity_irq(unsigned irq, const struct cpumask *dest)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700990{
Rusty Russell0de26522008-12-13 21:20:26 +1030991 unsigned tcpu = cpumask_first(dest);
Yinghai Lud5dedd42009-04-27 17:59:21 -0700992
993 return rebind_irq_to_cpu(irq, tcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700994}
995
Isaku Yamahata642e0c82008-04-02 10:53:57 -0700996int resend_irq_on_evtchn(unsigned int irq)
997{
998 int masked, evtchn = evtchn_from_irq(irq);
999 struct shared_info *s = HYPERVISOR_shared_info;
1000
1001 if (!VALID_EVTCHN(evtchn))
1002 return 1;
1003
1004 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
1005 sync_set_bit(evtchn, s->evtchn_pending);
1006 if (!masked)
1007 unmask_evtchn(evtchn);
1008
1009 return 1;
1010}
1011
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001012static void enable_dynirq(unsigned int irq)
1013{
1014 int evtchn = evtchn_from_irq(irq);
1015
1016 if (VALID_EVTCHN(evtchn))
1017 unmask_evtchn(evtchn);
1018}
1019
1020static void disable_dynirq(unsigned int irq)
1021{
1022 int evtchn = evtchn_from_irq(irq);
1023
1024 if (VALID_EVTCHN(evtchn))
1025 mask_evtchn(evtchn);
1026}
1027
1028static void ack_dynirq(unsigned int irq)
1029{
1030 int evtchn = evtchn_from_irq(irq);
1031
1032 move_native_irq(irq);
1033
1034 if (VALID_EVTCHN(evtchn))
1035 clear_evtchn(evtchn);
1036}
1037
1038static int retrigger_dynirq(unsigned int irq)
1039{
1040 int evtchn = evtchn_from_irq(irq);
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001041 struct shared_info *sh = HYPERVISOR_shared_info;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001042 int ret = 0;
1043
1044 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001045 int masked;
1046
1047 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
1048 sync_set_bit(evtchn, sh->evtchn_pending);
1049 if (!masked)
1050 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001051 ret = 1;
1052 }
1053
1054 return ret;
1055}
1056
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001057static void restore_cpu_virqs(unsigned int cpu)
1058{
1059 struct evtchn_bind_virq bind_virq;
1060 int virq, irq, evtchn;
1061
1062 for (virq = 0; virq < NR_VIRQS; virq++) {
1063 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1064 continue;
1065
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001066 BUG_ON(virq_from_irq(irq) != virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001067
1068 /* Get a new binding from Xen. */
1069 bind_virq.virq = virq;
1070 bind_virq.vcpu = cpu;
1071 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1072 &bind_virq) != 0)
1073 BUG();
1074 evtchn = bind_virq.port;
1075
1076 /* Record the new mapping. */
1077 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001078 irq_info[irq] = mk_virq_info(evtchn, virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001079 bind_evtchn_to_cpu(evtchn, cpu);
1080
1081 /* Ready for use. */
1082 unmask_evtchn(evtchn);
1083 }
1084}
1085
1086static void restore_cpu_ipis(unsigned int cpu)
1087{
1088 struct evtchn_bind_ipi bind_ipi;
1089 int ipi, irq, evtchn;
1090
1091 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1092 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1093 continue;
1094
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001095 BUG_ON(ipi_from_irq(irq) != ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001096
1097 /* Get a new binding from Xen. */
1098 bind_ipi.vcpu = cpu;
1099 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1100 &bind_ipi) != 0)
1101 BUG();
1102 evtchn = bind_ipi.port;
1103
1104 /* Record the new mapping. */
1105 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001106 irq_info[irq] = mk_ipi_info(evtchn, ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001107 bind_evtchn_to_cpu(evtchn, cpu);
1108
1109 /* Ready for use. */
1110 unmask_evtchn(evtchn);
1111
1112 }
1113}
1114
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001115/* Clear an irq's pending state, in preparation for polling on it */
1116void xen_clear_irq_pending(int irq)
1117{
1118 int evtchn = evtchn_from_irq(irq);
1119
1120 if (VALID_EVTCHN(evtchn))
1121 clear_evtchn(evtchn);
1122}
1123
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -07001124void xen_set_irq_pending(int irq)
1125{
1126 int evtchn = evtchn_from_irq(irq);
1127
1128 if (VALID_EVTCHN(evtchn))
1129 set_evtchn(evtchn);
1130}
1131
1132bool xen_test_irq_pending(int irq)
1133{
1134 int evtchn = evtchn_from_irq(irq);
1135 bool ret = false;
1136
1137 if (VALID_EVTCHN(evtchn))
1138 ret = test_evtchn(evtchn);
1139
1140 return ret;
1141}
1142
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001143/* Poll waiting for an irq to become pending. In the usual case, the
1144 irq will be disabled so it won't deliver an interrupt. */
1145void xen_poll_irq(int irq)
1146{
1147 evtchn_port_t evtchn = evtchn_from_irq(irq);
1148
1149 if (VALID_EVTCHN(evtchn)) {
1150 struct sched_poll poll;
1151
1152 poll.nr_ports = 1;
1153 poll.timeout = 0;
Isaku Yamahataff3c5362008-10-14 17:50:44 -07001154 set_xen_guest_handle(poll.ports, &evtchn);
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001155
1156 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1157 BUG();
1158 }
1159}
1160
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001161void xen_irq_resume(void)
1162{
1163 unsigned int cpu, irq, evtchn;
1164
1165 init_evtchn_cpu_bindings();
1166
1167 /* New event-channel space is not 'live' yet. */
1168 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1169 mask_evtchn(evtchn);
1170
1171 /* No IRQ <-> event-channel mappings. */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001172 for (irq = 0; irq < nr_irqs; irq++)
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001173 irq_info[irq].evtchn = 0; /* zap event-channel binding */
1174
1175 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1176 evtchn_to_irq[evtchn] = -1;
1177
1178 for_each_possible_cpu(cpu) {
1179 restore_cpu_virqs(cpu);
1180 restore_cpu_ipis(cpu);
1181 }
1182}
1183
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001184static struct irq_chip xen_dynamic_chip __read_mostly = {
1185 .name = "xen-dyn",
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001186
1187 .disable = disable_dynirq,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001188 .mask = disable_dynirq,
1189 .unmask = enable_dynirq,
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001190
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001191 .ack = ack_dynirq,
1192 .set_affinity = set_affinity_irq,
1193 .retrigger = retrigger_dynirq,
1194};
1195
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001196static struct irq_chip xen_pirq_chip __read_mostly = {
1197 .name = "xen-pirq",
1198
1199 .startup = startup_pirq,
1200 .shutdown = shutdown_pirq,
1201
1202 .enable = enable_pirq,
1203 .unmask = enable_pirq,
1204
1205 .disable = disable_pirq,
1206 .mask = disable_pirq,
1207
1208 .ack = ack_pirq,
1209 .end = end_pirq,
1210
1211 .set_affinity = set_affinity_irq,
1212
1213 .retrigger = retrigger_dynirq,
1214};
1215
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001216static struct irq_chip xen_percpu_chip __read_mostly = {
1217 .name = "xen-percpu",
1218
1219 .disable = disable_dynirq,
1220 .mask = disable_dynirq,
1221 .unmask = enable_dynirq,
1222
1223 .ack = ack_dynirq,
1224};
1225
Sheng Yang38e20b02010-05-14 12:40:51 +01001226int xen_set_callback_via(uint64_t via)
1227{
1228 struct xen_hvm_param a;
1229 a.domid = DOMID_SELF;
1230 a.index = HVM_PARAM_CALLBACK_IRQ;
1231 a.value = via;
1232 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1233}
1234EXPORT_SYMBOL_GPL(xen_set_callback_via);
1235
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001236#ifdef CONFIG_XEN_PVHVM
Sheng Yang38e20b02010-05-14 12:40:51 +01001237/* Vector callbacks are better than PCI interrupts to receive event
1238 * channel notifications because we can receive vector callbacks on any
1239 * vcpu and we don't need PCI support or APIC interactions. */
1240void xen_callback_vector(void)
1241{
1242 int rc;
1243 uint64_t callback_via;
1244 if (xen_have_vector_callback) {
1245 callback_via = HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK);
1246 rc = xen_set_callback_via(callback_via);
1247 if (rc) {
1248 printk(KERN_ERR "Request for Xen HVM callback vector"
1249 " failed.\n");
1250 xen_have_vector_callback = 0;
1251 return;
1252 }
1253 printk(KERN_INFO "Xen HVM callback vector for event delivery is "
1254 "enabled\n");
1255 /* in the restore case the vector has already been allocated */
1256 if (!test_bit(XEN_HVM_EVTCHN_CALLBACK, used_vectors))
1257 alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK, xen_hvm_callback_vector);
1258 }
1259}
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001260#else
1261void xen_callback_vector(void) {}
1262#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001263
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001264void __init xen_init_IRQ(void)
1265{
1266 int i;
Mike Travisc7a35892009-01-10 21:58:11 -08001267
Pekka Enberga70c3522009-07-01 11:51:18 +03001268 cpu_evtchn_mask_p = kcalloc(nr_cpu_ids, sizeof(struct cpu_evtchn_s),
1269 GFP_KERNEL);
Christophe Saout28e08862009-01-11 11:46:23 -08001270 BUG_ON(cpu_evtchn_mask_p == NULL);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001271
1272 init_evtchn_cpu_bindings();
1273
1274 /* No event channels are 'live' right now. */
1275 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1276 mask_evtchn(i);
1277
Sheng Yang38e20b02010-05-14 12:40:51 +01001278 if (xen_hvm_domain()) {
1279 xen_callback_vector();
1280 native_init_IRQ();
1281 } else {
1282 irq_ctx_init(smp_processor_id());
1283 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001284}