blob: 46625cd38743c2506e1fc024c8e1c4d434a4fa26 [file] [log] [blame]
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001/*
2 * Xen event channels
3 *
4 * Xen models interrupts with abstract event channels. Because each
5 * domain gets 1024 event channels, but NR_IRQ is not that large, we
6 * must dynamically map irqs<->event channels. The event channels
7 * interface with the rest of the kernel by defining a xen interrupt
8 * chip. When an event is recieved, it is mapped to an irq and sent
9 * through the normal interrupt processing path.
10 *
11 * There are four kinds of events which can be mapped to an event
12 * channel:
13 *
14 * 1. Inter-domain notifications. This includes all the virtual
15 * device events, since they're driven by front-ends in another domain
16 * (typically dom0).
17 * 2. VIRQs, typically used for timers. These are per-cpu events.
18 * 3. IPIs.
19 * 4. Hardware interrupts. Not supported at present.
20 *
21 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
22 */
23
24#include <linux/linkage.h>
25#include <linux/interrupt.h>
26#include <linux/irq.h>
27#include <linux/module.h>
28#include <linux/string.h>
29
30#include <asm/ptrace.h>
31#include <asm/irq.h>
32#include <asm/sync_bitops.h>
33#include <asm/xen/hypercall.h>
Adrian Bunk8d1b8752007-07-20 00:31:44 -070034#include <asm/xen/hypervisor.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070035
Isaku Yamahatae04d0d02008-04-02 10:53:55 -070036#include <xen/xen-ops.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070037#include <xen/events.h>
38#include <xen/interface/xen.h>
39#include <xen/interface/event_channel.h>
40
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070041/*
42 * This lock protects updates to the following mapping and reference-count
43 * arrays. The lock does not need to be acquired to read the mapping tables.
44 */
45static DEFINE_SPINLOCK(irq_mapping_update_lock);
46
47/* IRQ <-> VIRQ mapping. */
48static DEFINE_PER_CPU(int, virq_to_irq[NR_VIRQS]) = {[0 ... NR_VIRQS-1] = -1};
49
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070050/* IRQ <-> IPI mapping */
51static DEFINE_PER_CPU(int, ipi_to_irq[XEN_NR_IPIS]) = {[0 ... XEN_NR_IPIS-1] = -1};
52
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070053/* Packed IRQ information: binding type, sub-type index, and event channel. */
54struct packed_irq
55{
56 unsigned short evtchn;
57 unsigned char index;
58 unsigned char type;
59};
60
61static struct packed_irq irq_info[NR_IRQS];
62
63/* Binding types. */
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070064enum {
65 IRQT_UNBOUND,
66 IRQT_PIRQ,
67 IRQT_VIRQ,
68 IRQT_IPI,
69 IRQT_EVTCHN
70};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070071
72/* Convenient shorthand for packed representation of an unbound IRQ. */
73#define IRQ_UNBOUND mk_irq_info(IRQT_UNBOUND, 0, 0)
74
75static int evtchn_to_irq[NR_EVENT_CHANNELS] = {
76 [0 ... NR_EVENT_CHANNELS-1] = -1
77};
78static unsigned long cpu_evtchn_mask[NR_CPUS][NR_EVENT_CHANNELS/BITS_PER_LONG];
79static u8 cpu_evtchn[NR_EVENT_CHANNELS];
80
81/* Reference counts for bindings to IRQs. */
82static int irq_bindcount[NR_IRQS];
83
84/* Xen will never allocate port zero for any purpose. */
85#define VALID_EVTCHN(chn) ((chn) != 0)
86
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070087static struct irq_chip xen_dynamic_chip;
88
89/* Constructor for packed IRQ information. */
90static inline struct packed_irq mk_irq_info(u32 type, u32 index, u32 evtchn)
91{
92 return (struct packed_irq) { evtchn, index, type };
93}
94
95/*
96 * Accessors for packed IRQ information.
97 */
98static inline unsigned int evtchn_from_irq(int irq)
99{
100 return irq_info[irq].evtchn;
101}
102
103static inline unsigned int index_from_irq(int irq)
104{
105 return irq_info[irq].index;
106}
107
108static inline unsigned int type_from_irq(int irq)
109{
110 return irq_info[irq].type;
111}
112
113static inline unsigned long active_evtchns(unsigned int cpu,
114 struct shared_info *sh,
115 unsigned int idx)
116{
117 return (sh->evtchn_pending[idx] &
118 cpu_evtchn_mask[cpu][idx] &
119 ~sh->evtchn_mask[idx]);
120}
121
122static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
123{
124 int irq = evtchn_to_irq[chn];
125
126 BUG_ON(irq == -1);
127#ifdef CONFIG_SMP
Yinghai Lu08678b02008-08-19 20:50:05 -0700128 irq_to_desc(irq)->affinity = cpumask_of_cpu(cpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700129#endif
130
131 __clear_bit(chn, cpu_evtchn_mask[cpu_evtchn[chn]]);
132 __set_bit(chn, cpu_evtchn_mask[cpu]);
133
134 cpu_evtchn[chn] = cpu;
135}
136
137static void init_evtchn_cpu_bindings(void)
138{
139#ifdef CONFIG_SMP
Thomas Gleixner10e58082008-10-16 14:19:04 +0200140 struct irq_desc *desc;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700141 int i;
Thomas Gleixner10e58082008-10-16 14:19:04 +0200142
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700143 /* By default all event channels notify CPU#0. */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800144 for_each_irq_desc(i, desc) {
145 if (!desc)
146 continue;
147
Yinghai Lu08678b02008-08-19 20:50:05 -0700148 desc->affinity = cpumask_of_cpu(0);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800149 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700150#endif
151
152 memset(cpu_evtchn, 0, sizeof(cpu_evtchn));
153 memset(cpu_evtchn_mask[0], ~0, sizeof(cpu_evtchn_mask[0]));
154}
155
156static inline unsigned int cpu_from_evtchn(unsigned int evtchn)
157{
158 return cpu_evtchn[evtchn];
159}
160
161static inline void clear_evtchn(int port)
162{
163 struct shared_info *s = HYPERVISOR_shared_info;
164 sync_clear_bit(port, &s->evtchn_pending[0]);
165}
166
167static inline void set_evtchn(int port)
168{
169 struct shared_info *s = HYPERVISOR_shared_info;
170 sync_set_bit(port, &s->evtchn_pending[0]);
171}
172
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700173static inline int test_evtchn(int port)
174{
175 struct shared_info *s = HYPERVISOR_shared_info;
176 return sync_test_bit(port, &s->evtchn_pending[0]);
177}
178
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700179
180/**
181 * notify_remote_via_irq - send event to remote end of event channel via irq
182 * @irq: irq of event channel to send event to
183 *
184 * Unlike notify_remote_via_evtchn(), this is safe to use across
185 * save/restore. Notifications on a broken connection are silently
186 * dropped.
187 */
188void notify_remote_via_irq(int irq)
189{
190 int evtchn = evtchn_from_irq(irq);
191
192 if (VALID_EVTCHN(evtchn))
193 notify_remote_via_evtchn(evtchn);
194}
195EXPORT_SYMBOL_GPL(notify_remote_via_irq);
196
197static void mask_evtchn(int port)
198{
199 struct shared_info *s = HYPERVISOR_shared_info;
200 sync_set_bit(port, &s->evtchn_mask[0]);
201}
202
203static void unmask_evtchn(int port)
204{
205 struct shared_info *s = HYPERVISOR_shared_info;
206 unsigned int cpu = get_cpu();
207
208 BUG_ON(!irqs_disabled());
209
210 /* Slow path (hypercall) if this is a non-local port. */
211 if (unlikely(cpu != cpu_from_evtchn(port))) {
212 struct evtchn_unmask unmask = { .port = port };
213 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
214 } else {
215 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
216
217 sync_clear_bit(port, &s->evtchn_mask[0]);
218
219 /*
220 * The following is basically the equivalent of
221 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
222 * the interrupt edge' if the channel is masked.
223 */
224 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
225 !sync_test_and_set_bit(port / BITS_PER_LONG,
226 &vcpu_info->evtchn_pending_sel))
227 vcpu_info->evtchn_upcall_pending = 1;
228 }
229
230 put_cpu();
231}
232
233static int find_unbound_irq(void)
234{
235 int irq;
Jeremy Fitzhardinge6f8a0ed2008-12-17 13:42:29 -0800236 struct irq_desc *desc;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700237
238 /* Only allocate from dynirq range */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800239 for (irq = 0; irq < nr_irqs; irq++)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700240 if (irq_bindcount[irq] == 0)
241 break;
242
Yinghai Lu5a15d7e2008-08-19 20:49:57 -0700243 if (irq == nr_irqs)
244 panic("No available IRQ to bind to: increase nr_irqs!\n");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700245
Jeremy Fitzhardinge6f8a0ed2008-12-17 13:42:29 -0800246 desc = irq_to_desc_alloc_cpu(irq, 0);
247 if (WARN_ON(desc == NULL))
248 return -1;
249
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700250 return irq;
251}
252
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700253int bind_evtchn_to_irq(unsigned int evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700254{
255 int irq;
256
257 spin_lock(&irq_mapping_update_lock);
258
259 irq = evtchn_to_irq[evtchn];
260
261 if (irq == -1) {
262 irq = find_unbound_irq();
263
264 dynamic_irq_init(irq);
265 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
266 handle_level_irq, "event");
267
268 evtchn_to_irq[evtchn] = irq;
269 irq_info[irq] = mk_irq_info(IRQT_EVTCHN, 0, evtchn);
270 }
271
272 irq_bindcount[irq]++;
273
274 spin_unlock(&irq_mapping_update_lock);
275
276 return irq;
277}
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700278EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700279
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700280static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
281{
282 struct evtchn_bind_ipi bind_ipi;
283 int evtchn, irq;
284
285 spin_lock(&irq_mapping_update_lock);
286
287 irq = per_cpu(ipi_to_irq, cpu)[ipi];
288 if (irq == -1) {
289 irq = find_unbound_irq();
290 if (irq < 0)
291 goto out;
292
293 dynamic_irq_init(irq);
294 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
295 handle_level_irq, "ipi");
296
297 bind_ipi.vcpu = cpu;
298 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
299 &bind_ipi) != 0)
300 BUG();
301 evtchn = bind_ipi.port;
302
303 evtchn_to_irq[evtchn] = irq;
304 irq_info[irq] = mk_irq_info(IRQT_IPI, ipi, evtchn);
305
306 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
307
308 bind_evtchn_to_cpu(evtchn, cpu);
309 }
310
311 irq_bindcount[irq]++;
312
313 out:
314 spin_unlock(&irq_mapping_update_lock);
315 return irq;
316}
317
318
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700319static int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
320{
321 struct evtchn_bind_virq bind_virq;
322 int evtchn, irq;
323
324 spin_lock(&irq_mapping_update_lock);
325
326 irq = per_cpu(virq_to_irq, cpu)[virq];
327
328 if (irq == -1) {
329 bind_virq.virq = virq;
330 bind_virq.vcpu = cpu;
331 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
332 &bind_virq) != 0)
333 BUG();
334 evtchn = bind_virq.port;
335
336 irq = find_unbound_irq();
337
338 dynamic_irq_init(irq);
339 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
340 handle_level_irq, "virq");
341
342 evtchn_to_irq[evtchn] = irq;
343 irq_info[irq] = mk_irq_info(IRQT_VIRQ, virq, evtchn);
344
345 per_cpu(virq_to_irq, cpu)[virq] = irq;
346
347 bind_evtchn_to_cpu(evtchn, cpu);
348 }
349
350 irq_bindcount[irq]++;
351
352 spin_unlock(&irq_mapping_update_lock);
353
354 return irq;
355}
356
357static void unbind_from_irq(unsigned int irq)
358{
359 struct evtchn_close close;
360 int evtchn = evtchn_from_irq(irq);
361
362 spin_lock(&irq_mapping_update_lock);
363
Jeremy Fitzhardinge0f2287a2008-05-26 23:31:24 +0100364 if ((--irq_bindcount[irq] == 0) && VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700365 close.port = evtchn;
366 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
367 BUG();
368
369 switch (type_from_irq(irq)) {
370 case IRQT_VIRQ:
371 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
372 [index_from_irq(irq)] = -1;
373 break;
Alex Nixond68d82a2008-08-22 11:52:15 +0100374 case IRQT_IPI:
375 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
376 [index_from_irq(irq)] = -1;
377 break;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700378 default:
379 break;
380 }
381
382 /* Closed ports are implicitly re-bound to VCPU0. */
383 bind_evtchn_to_cpu(evtchn, 0);
384
385 evtchn_to_irq[evtchn] = -1;
386 irq_info[irq] = IRQ_UNBOUND;
387
Jeremy Fitzhardinge0f2287a2008-05-26 23:31:24 +0100388 dynamic_irq_cleanup(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700389 }
390
391 spin_unlock(&irq_mapping_update_lock);
392}
393
394int bind_evtchn_to_irqhandler(unsigned int evtchn,
Jeff Garzik7c239972007-10-19 03:12:20 -0400395 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700396 unsigned long irqflags,
397 const char *devname, void *dev_id)
398{
399 unsigned int irq;
400 int retval;
401
402 irq = bind_evtchn_to_irq(evtchn);
403 retval = request_irq(irq, handler, irqflags, devname, dev_id);
404 if (retval != 0) {
405 unbind_from_irq(irq);
406 return retval;
407 }
408
409 return irq;
410}
411EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
412
413int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
Jeff Garzik7c239972007-10-19 03:12:20 -0400414 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700415 unsigned long irqflags, const char *devname, void *dev_id)
416{
417 unsigned int irq;
418 int retval;
419
420 irq = bind_virq_to_irq(virq, cpu);
421 retval = request_irq(irq, handler, irqflags, devname, dev_id);
422 if (retval != 0) {
423 unbind_from_irq(irq);
424 return retval;
425 }
426
427 return irq;
428}
429EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
430
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700431int bind_ipi_to_irqhandler(enum ipi_vector ipi,
432 unsigned int cpu,
433 irq_handler_t handler,
434 unsigned long irqflags,
435 const char *devname,
436 void *dev_id)
437{
438 int irq, retval;
439
440 irq = bind_ipi_to_irq(ipi, cpu);
441 if (irq < 0)
442 return irq;
443
444 retval = request_irq(irq, handler, irqflags, devname, dev_id);
445 if (retval != 0) {
446 unbind_from_irq(irq);
447 return retval;
448 }
449
450 return irq;
451}
452
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700453void unbind_from_irqhandler(unsigned int irq, void *dev_id)
454{
455 free_irq(irq, dev_id);
456 unbind_from_irq(irq);
457}
458EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
459
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700460void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
461{
462 int irq = per_cpu(ipi_to_irq, cpu)[vector];
463 BUG_ON(irq < 0);
464 notify_remote_via_irq(irq);
465}
466
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700467irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
468{
469 struct shared_info *sh = HYPERVISOR_shared_info;
470 int cpu = smp_processor_id();
471 int i;
472 unsigned long flags;
473 static DEFINE_SPINLOCK(debug_lock);
474
475 spin_lock_irqsave(&debug_lock, flags);
476
477 printk("vcpu %d\n ", cpu);
478
479 for_each_online_cpu(i) {
480 struct vcpu_info *v = per_cpu(xen_vcpu, i);
481 printk("%d: masked=%d pending=%d event_sel %08lx\n ", i,
Isaku Yamahatae849c3e2008-04-02 10:53:56 -0700482 (get_irq_regs() && i == cpu) ? xen_irqs_disabled(get_irq_regs()) : v->evtchn_upcall_mask,
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700483 v->evtchn_upcall_pending,
484 v->evtchn_pending_sel);
485 }
486 printk("pending:\n ");
487 for(i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
488 printk("%08lx%s", sh->evtchn_pending[i],
489 i % 8 == 0 ? "\n " : " ");
490 printk("\nmasks:\n ");
491 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
492 printk("%08lx%s", sh->evtchn_mask[i],
493 i % 8 == 0 ? "\n " : " ");
494
495 printk("\nunmasked:\n ");
496 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
497 printk("%08lx%s", sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
498 i % 8 == 0 ? "\n " : " ");
499
500 printk("\npending list:\n");
501 for(i = 0; i < NR_EVENT_CHANNELS; i++) {
502 if (sync_test_bit(i, sh->evtchn_pending)) {
503 printk(" %d: event %d -> irq %d\n",
504 cpu_evtchn[i], i,
505 evtchn_to_irq[i]);
506 }
507 }
508
509 spin_unlock_irqrestore(&debug_lock, flags);
510
511 return IRQ_HANDLED;
512}
513
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700514
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700515/*
516 * Search the CPUs pending events bitmasks. For each one found, map
517 * the event number to an irq, and feed it into do_IRQ() for
518 * handling.
519 *
520 * Xen uses a two-level bitmap to speed searching. The first level is
521 * a bitset of words which contain pending event bits. The second
522 * level is a bitset of pending events themselves.
523 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100524void xen_evtchn_do_upcall(struct pt_regs *regs)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700525{
526 int cpu = get_cpu();
527 struct shared_info *s = HYPERVISOR_shared_info;
528 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700529 static DEFINE_PER_CPU(unsigned, nesting_count);
530 unsigned count;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700531
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700532 do {
533 unsigned long pending_words;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700534
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700535 vcpu_info->evtchn_upcall_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700536
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700537 if (__get_cpu_var(nesting_count)++)
538 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700539
Isaku Yamahatae849c3e2008-04-02 10:53:56 -0700540#ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
541 /* Clear master flag /before/ clearing selector flag. */
Isaku Yamahata6673cf62008-06-16 14:58:13 -0700542 wmb();
Isaku Yamahatae849c3e2008-04-02 10:53:56 -0700543#endif
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700544 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
545 while (pending_words != 0) {
546 unsigned long pending_bits;
547 int word_idx = __ffs(pending_words);
548 pending_words &= ~(1UL << word_idx);
549
550 while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
551 int bit_idx = __ffs(pending_bits);
552 int port = (word_idx * BITS_PER_LONG) + bit_idx;
553 int irq = evtchn_to_irq[port];
554
Isaku Yamahatae849c3e2008-04-02 10:53:56 -0700555 if (irq != -1)
556 xen_do_IRQ(irq, regs);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700557 }
558 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700559
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700560 BUG_ON(!irqs_disabled());
561
562 count = __get_cpu_var(nesting_count);
563 __get_cpu_var(nesting_count) = 0;
564 } while(count != 1);
565
566out:
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700567 put_cpu();
568}
569
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +0100570/* Rebind a new event channel to an existing irq. */
571void rebind_evtchn_irq(int evtchn, int irq)
572{
573 /* Make sure the irq is masked, since the new event channel
574 will also be masked. */
575 disable_irq(irq);
576
577 spin_lock(&irq_mapping_update_lock);
578
579 /* After resume the irq<->evtchn mappings are all cleared out */
580 BUG_ON(evtchn_to_irq[evtchn] != -1);
581 /* Expect irq to have been bound before,
582 so the bindcount should be non-0 */
583 BUG_ON(irq_bindcount[irq] == 0);
584
585 evtchn_to_irq[evtchn] = irq;
586 irq_info[irq] = mk_irq_info(IRQT_EVTCHN, 0, evtchn);
587
588 spin_unlock(&irq_mapping_update_lock);
589
590 /* new event channels are always bound to cpu 0 */
591 irq_set_affinity(irq, cpumask_of_cpu(0));
592
593 /* Unmask the event channel. */
594 enable_irq(irq);
595}
596
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700597/* Rebind an evtchn so that it gets delivered to a specific cpu */
598static void rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
599{
600 struct evtchn_bind_vcpu bind_vcpu;
601 int evtchn = evtchn_from_irq(irq);
602
603 if (!VALID_EVTCHN(evtchn))
604 return;
605
606 /* Send future instances of this interrupt to other vcpu. */
607 bind_vcpu.port = evtchn;
608 bind_vcpu.vcpu = tcpu;
609
610 /*
611 * If this fails, it usually just indicates that we're dealing with a
612 * virq or IPI channel, which don't actually need to be rebound. Ignore
613 * it, but don't do the xenlinux-level rebind in that case.
614 */
615 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
616 bind_evtchn_to_cpu(evtchn, tcpu);
617}
618
619
620static void set_affinity_irq(unsigned irq, cpumask_t dest)
621{
622 unsigned tcpu = first_cpu(dest);
623 rebind_irq_to_cpu(irq, tcpu);
624}
625
Isaku Yamahata642e0c82008-04-02 10:53:57 -0700626int resend_irq_on_evtchn(unsigned int irq)
627{
628 int masked, evtchn = evtchn_from_irq(irq);
629 struct shared_info *s = HYPERVISOR_shared_info;
630
631 if (!VALID_EVTCHN(evtchn))
632 return 1;
633
634 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
635 sync_set_bit(evtchn, s->evtchn_pending);
636 if (!masked)
637 unmask_evtchn(evtchn);
638
639 return 1;
640}
641
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700642static void enable_dynirq(unsigned int irq)
643{
644 int evtchn = evtchn_from_irq(irq);
645
646 if (VALID_EVTCHN(evtchn))
647 unmask_evtchn(evtchn);
648}
649
650static void disable_dynirq(unsigned int irq)
651{
652 int evtchn = evtchn_from_irq(irq);
653
654 if (VALID_EVTCHN(evtchn))
655 mask_evtchn(evtchn);
656}
657
658static void ack_dynirq(unsigned int irq)
659{
660 int evtchn = evtchn_from_irq(irq);
661
662 move_native_irq(irq);
663
664 if (VALID_EVTCHN(evtchn))
665 clear_evtchn(evtchn);
666}
667
668static int retrigger_dynirq(unsigned int irq)
669{
670 int evtchn = evtchn_from_irq(irq);
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -0700671 struct shared_info *sh = HYPERVISOR_shared_info;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700672 int ret = 0;
673
674 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -0700675 int masked;
676
677 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
678 sync_set_bit(evtchn, sh->evtchn_pending);
679 if (!masked)
680 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700681 ret = 1;
682 }
683
684 return ret;
685}
686
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100687static void restore_cpu_virqs(unsigned int cpu)
688{
689 struct evtchn_bind_virq bind_virq;
690 int virq, irq, evtchn;
691
692 for (virq = 0; virq < NR_VIRQS; virq++) {
693 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
694 continue;
695
696 BUG_ON(irq_info[irq].type != IRQT_VIRQ);
697 BUG_ON(irq_info[irq].index != virq);
698
699 /* Get a new binding from Xen. */
700 bind_virq.virq = virq;
701 bind_virq.vcpu = cpu;
702 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
703 &bind_virq) != 0)
704 BUG();
705 evtchn = bind_virq.port;
706
707 /* Record the new mapping. */
708 evtchn_to_irq[evtchn] = irq;
709 irq_info[irq] = mk_irq_info(IRQT_VIRQ, virq, evtchn);
710 bind_evtchn_to_cpu(evtchn, cpu);
711
712 /* Ready for use. */
713 unmask_evtchn(evtchn);
714 }
715}
716
717static void restore_cpu_ipis(unsigned int cpu)
718{
719 struct evtchn_bind_ipi bind_ipi;
720 int ipi, irq, evtchn;
721
722 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
723 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
724 continue;
725
726 BUG_ON(irq_info[irq].type != IRQT_IPI);
727 BUG_ON(irq_info[irq].index != ipi);
728
729 /* Get a new binding from Xen. */
730 bind_ipi.vcpu = cpu;
731 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
732 &bind_ipi) != 0)
733 BUG();
734 evtchn = bind_ipi.port;
735
736 /* Record the new mapping. */
737 evtchn_to_irq[evtchn] = irq;
738 irq_info[irq] = mk_irq_info(IRQT_IPI, ipi, evtchn);
739 bind_evtchn_to_cpu(evtchn, cpu);
740
741 /* Ready for use. */
742 unmask_evtchn(evtchn);
743
744 }
745}
746
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -0700747/* Clear an irq's pending state, in preparation for polling on it */
748void xen_clear_irq_pending(int irq)
749{
750 int evtchn = evtchn_from_irq(irq);
751
752 if (VALID_EVTCHN(evtchn))
753 clear_evtchn(evtchn);
754}
755
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700756void xen_set_irq_pending(int irq)
757{
758 int evtchn = evtchn_from_irq(irq);
759
760 if (VALID_EVTCHN(evtchn))
761 set_evtchn(evtchn);
762}
763
764bool xen_test_irq_pending(int irq)
765{
766 int evtchn = evtchn_from_irq(irq);
767 bool ret = false;
768
769 if (VALID_EVTCHN(evtchn))
770 ret = test_evtchn(evtchn);
771
772 return ret;
773}
774
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -0700775/* Poll waiting for an irq to become pending. In the usual case, the
776 irq will be disabled so it won't deliver an interrupt. */
777void xen_poll_irq(int irq)
778{
779 evtchn_port_t evtchn = evtchn_from_irq(irq);
780
781 if (VALID_EVTCHN(evtchn)) {
782 struct sched_poll poll;
783
784 poll.nr_ports = 1;
785 poll.timeout = 0;
Isaku Yamahataff3c5362008-10-14 17:50:44 -0700786 set_xen_guest_handle(poll.ports, &evtchn);
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -0700787
788 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
789 BUG();
790 }
791}
792
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100793void xen_irq_resume(void)
794{
795 unsigned int cpu, irq, evtchn;
796
797 init_evtchn_cpu_bindings();
798
799 /* New event-channel space is not 'live' yet. */
800 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
801 mask_evtchn(evtchn);
802
803 /* No IRQ <-> event-channel mappings. */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800804 for (irq = 0; irq < nr_irqs; irq++)
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100805 irq_info[irq].evtchn = 0; /* zap event-channel binding */
806
807 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
808 evtchn_to_irq[evtchn] = -1;
809
810 for_each_possible_cpu(cpu) {
811 restore_cpu_virqs(cpu);
812 restore_cpu_ipis(cpu);
813 }
814}
815
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700816static struct irq_chip xen_dynamic_chip __read_mostly = {
817 .name = "xen-dyn",
818 .mask = disable_dynirq,
819 .unmask = enable_dynirq,
820 .ack = ack_dynirq,
821 .set_affinity = set_affinity_irq,
822 .retrigger = retrigger_dynirq,
823};
824
825void __init xen_init_IRQ(void)
826{
827 int i;
828
829 init_evtchn_cpu_bindings();
830
831 /* No event channels are 'live' right now. */
832 for (i = 0; i < NR_EVENT_CHANNELS; i++)
833 mask_evtchn(i);
834
835 /* Dynamic IRQ space is currently unbound. Zero the refcnts. */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800836 for (i = 0; i < nr_irqs; i++)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700837 irq_bindcount[i] = 0;
838
839 irq_ctx_init(smp_processor_id());
840}