blob: b6c2b8f16beedaab321700c6c091250d42bff3bf [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
128 irq_desc[irq].affinity = cpumask_of_cpu(cpu);
129#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
140 int i;
141 /* By default all event channels notify CPU#0. */
142 for (i = 0; i < NR_IRQS; i++)
143 irq_desc[i].affinity = cpumask_of_cpu(0);
144#endif
145
146 memset(cpu_evtchn, 0, sizeof(cpu_evtchn));
147 memset(cpu_evtchn_mask[0], ~0, sizeof(cpu_evtchn_mask[0]));
148}
149
150static inline unsigned int cpu_from_evtchn(unsigned int evtchn)
151{
152 return cpu_evtchn[evtchn];
153}
154
155static inline void clear_evtchn(int port)
156{
157 struct shared_info *s = HYPERVISOR_shared_info;
158 sync_clear_bit(port, &s->evtchn_pending[0]);
159}
160
161static inline void set_evtchn(int port)
162{
163 struct shared_info *s = HYPERVISOR_shared_info;
164 sync_set_bit(port, &s->evtchn_pending[0]);
165}
166
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700167static inline int test_evtchn(int port)
168{
169 struct shared_info *s = HYPERVISOR_shared_info;
170 return sync_test_bit(port, &s->evtchn_pending[0]);
171}
172
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700173
174/**
175 * notify_remote_via_irq - send event to remote end of event channel via irq
176 * @irq: irq of event channel to send event to
177 *
178 * Unlike notify_remote_via_evtchn(), this is safe to use across
179 * save/restore. Notifications on a broken connection are silently
180 * dropped.
181 */
182void notify_remote_via_irq(int irq)
183{
184 int evtchn = evtchn_from_irq(irq);
185
186 if (VALID_EVTCHN(evtchn))
187 notify_remote_via_evtchn(evtchn);
188}
189EXPORT_SYMBOL_GPL(notify_remote_via_irq);
190
191static void mask_evtchn(int port)
192{
193 struct shared_info *s = HYPERVISOR_shared_info;
194 sync_set_bit(port, &s->evtchn_mask[0]);
195}
196
197static void unmask_evtchn(int port)
198{
199 struct shared_info *s = HYPERVISOR_shared_info;
200 unsigned int cpu = get_cpu();
201
202 BUG_ON(!irqs_disabled());
203
204 /* Slow path (hypercall) if this is a non-local port. */
205 if (unlikely(cpu != cpu_from_evtchn(port))) {
206 struct evtchn_unmask unmask = { .port = port };
207 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
208 } else {
209 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
210
211 sync_clear_bit(port, &s->evtchn_mask[0]);
212
213 /*
214 * The following is basically the equivalent of
215 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
216 * the interrupt edge' if the channel is masked.
217 */
218 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
219 !sync_test_and_set_bit(port / BITS_PER_LONG,
220 &vcpu_info->evtchn_pending_sel))
221 vcpu_info->evtchn_upcall_pending = 1;
222 }
223
224 put_cpu();
225}
226
227static int find_unbound_irq(void)
228{
229 int irq;
230
231 /* Only allocate from dynirq range */
232 for (irq = 0; irq < NR_IRQS; irq++)
233 if (irq_bindcount[irq] == 0)
234 break;
235
236 if (irq == NR_IRQS)
237 panic("No available IRQ to bind to: increase NR_IRQS!\n");
238
239 return irq;
240}
241
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700242int bind_evtchn_to_irq(unsigned int evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700243{
244 int irq;
245
246 spin_lock(&irq_mapping_update_lock);
247
248 irq = evtchn_to_irq[evtchn];
249
250 if (irq == -1) {
251 irq = find_unbound_irq();
252
253 dynamic_irq_init(irq);
254 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
255 handle_level_irq, "event");
256
257 evtchn_to_irq[evtchn] = irq;
258 irq_info[irq] = mk_irq_info(IRQT_EVTCHN, 0, evtchn);
259 }
260
261 irq_bindcount[irq]++;
262
263 spin_unlock(&irq_mapping_update_lock);
264
265 return irq;
266}
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700267EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700268
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700269static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
270{
271 struct evtchn_bind_ipi bind_ipi;
272 int evtchn, irq;
273
274 spin_lock(&irq_mapping_update_lock);
275
276 irq = per_cpu(ipi_to_irq, cpu)[ipi];
277 if (irq == -1) {
278 irq = find_unbound_irq();
279 if (irq < 0)
280 goto out;
281
282 dynamic_irq_init(irq);
283 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
284 handle_level_irq, "ipi");
285
286 bind_ipi.vcpu = cpu;
287 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
288 &bind_ipi) != 0)
289 BUG();
290 evtchn = bind_ipi.port;
291
292 evtchn_to_irq[evtchn] = irq;
293 irq_info[irq] = mk_irq_info(IRQT_IPI, ipi, evtchn);
294
295 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
296
297 bind_evtchn_to_cpu(evtchn, cpu);
298 }
299
300 irq_bindcount[irq]++;
301
302 out:
303 spin_unlock(&irq_mapping_update_lock);
304 return irq;
305}
306
307
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700308static int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
309{
310 struct evtchn_bind_virq bind_virq;
311 int evtchn, irq;
312
313 spin_lock(&irq_mapping_update_lock);
314
315 irq = per_cpu(virq_to_irq, cpu)[virq];
316
317 if (irq == -1) {
318 bind_virq.virq = virq;
319 bind_virq.vcpu = cpu;
320 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
321 &bind_virq) != 0)
322 BUG();
323 evtchn = bind_virq.port;
324
325 irq = find_unbound_irq();
326
327 dynamic_irq_init(irq);
328 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
329 handle_level_irq, "virq");
330
331 evtchn_to_irq[evtchn] = irq;
332 irq_info[irq] = mk_irq_info(IRQT_VIRQ, virq, evtchn);
333
334 per_cpu(virq_to_irq, cpu)[virq] = irq;
335
336 bind_evtchn_to_cpu(evtchn, cpu);
337 }
338
339 irq_bindcount[irq]++;
340
341 spin_unlock(&irq_mapping_update_lock);
342
343 return irq;
344}
345
346static void unbind_from_irq(unsigned int irq)
347{
348 struct evtchn_close close;
349 int evtchn = evtchn_from_irq(irq);
350
351 spin_lock(&irq_mapping_update_lock);
352
Jeremy Fitzhardinge0f2287a2008-05-26 23:31:24 +0100353 if ((--irq_bindcount[irq] == 0) && VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700354 close.port = evtchn;
355 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
356 BUG();
357
358 switch (type_from_irq(irq)) {
359 case IRQT_VIRQ:
360 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
361 [index_from_irq(irq)] = -1;
362 break;
363 default:
364 break;
365 }
366
367 /* Closed ports are implicitly re-bound to VCPU0. */
368 bind_evtchn_to_cpu(evtchn, 0);
369
370 evtchn_to_irq[evtchn] = -1;
371 irq_info[irq] = IRQ_UNBOUND;
372
Jeremy Fitzhardinge0f2287a2008-05-26 23:31:24 +0100373 dynamic_irq_cleanup(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700374 }
375
376 spin_unlock(&irq_mapping_update_lock);
377}
378
379int bind_evtchn_to_irqhandler(unsigned int evtchn,
Jeff Garzik7c239972007-10-19 03:12:20 -0400380 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700381 unsigned long irqflags,
382 const char *devname, void *dev_id)
383{
384 unsigned int irq;
385 int retval;
386
387 irq = bind_evtchn_to_irq(evtchn);
388 retval = request_irq(irq, handler, irqflags, devname, dev_id);
389 if (retval != 0) {
390 unbind_from_irq(irq);
391 return retval;
392 }
393
394 return irq;
395}
396EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
397
398int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
Jeff Garzik7c239972007-10-19 03:12:20 -0400399 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700400 unsigned long irqflags, const char *devname, void *dev_id)
401{
402 unsigned int irq;
403 int retval;
404
405 irq = bind_virq_to_irq(virq, cpu);
406 retval = request_irq(irq, handler, irqflags, devname, dev_id);
407 if (retval != 0) {
408 unbind_from_irq(irq);
409 return retval;
410 }
411
412 return irq;
413}
414EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
415
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700416int bind_ipi_to_irqhandler(enum ipi_vector ipi,
417 unsigned int cpu,
418 irq_handler_t handler,
419 unsigned long irqflags,
420 const char *devname,
421 void *dev_id)
422{
423 int irq, retval;
424
425 irq = bind_ipi_to_irq(ipi, cpu);
426 if (irq < 0)
427 return irq;
428
429 retval = request_irq(irq, handler, irqflags, devname, dev_id);
430 if (retval != 0) {
431 unbind_from_irq(irq);
432 return retval;
433 }
434
435 return irq;
436}
437
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700438void unbind_from_irqhandler(unsigned int irq, void *dev_id)
439{
440 free_irq(irq, dev_id);
441 unbind_from_irq(irq);
442}
443EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
444
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700445void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
446{
447 int irq = per_cpu(ipi_to_irq, cpu)[vector];
448 BUG_ON(irq < 0);
449 notify_remote_via_irq(irq);
450}
451
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700452irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
453{
454 struct shared_info *sh = HYPERVISOR_shared_info;
455 int cpu = smp_processor_id();
456 int i;
457 unsigned long flags;
458 static DEFINE_SPINLOCK(debug_lock);
459
460 spin_lock_irqsave(&debug_lock, flags);
461
462 printk("vcpu %d\n ", cpu);
463
464 for_each_online_cpu(i) {
465 struct vcpu_info *v = per_cpu(xen_vcpu, i);
466 printk("%d: masked=%d pending=%d event_sel %08lx\n ", i,
Isaku Yamahatae849c3e2008-04-02 10:53:56 -0700467 (get_irq_regs() && i == cpu) ? xen_irqs_disabled(get_irq_regs()) : v->evtchn_upcall_mask,
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -0700468 v->evtchn_upcall_pending,
469 v->evtchn_pending_sel);
470 }
471 printk("pending:\n ");
472 for(i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
473 printk("%08lx%s", sh->evtchn_pending[i],
474 i % 8 == 0 ? "\n " : " ");
475 printk("\nmasks:\n ");
476 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
477 printk("%08lx%s", sh->evtchn_mask[i],
478 i % 8 == 0 ? "\n " : " ");
479
480 printk("\nunmasked:\n ");
481 for(i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
482 printk("%08lx%s", sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
483 i % 8 == 0 ? "\n " : " ");
484
485 printk("\npending list:\n");
486 for(i = 0; i < NR_EVENT_CHANNELS; i++) {
487 if (sync_test_bit(i, sh->evtchn_pending)) {
488 printk(" %d: event %d -> irq %d\n",
489 cpu_evtchn[i], i,
490 evtchn_to_irq[i]);
491 }
492 }
493
494 spin_unlock_irqrestore(&debug_lock, flags);
495
496 return IRQ_HANDLED;
497}
498
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700499
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700500/*
501 * Search the CPUs pending events bitmasks. For each one found, map
502 * the event number to an irq, and feed it into do_IRQ() for
503 * handling.
504 *
505 * Xen uses a two-level bitmap to speed searching. The first level is
506 * a bitset of words which contain pending event bits. The second
507 * level is a bitset of pending events themselves.
508 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100509void xen_evtchn_do_upcall(struct pt_regs *regs)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700510{
511 int cpu = get_cpu();
512 struct shared_info *s = HYPERVISOR_shared_info;
513 struct vcpu_info *vcpu_info = __get_cpu_var(xen_vcpu);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700514 static DEFINE_PER_CPU(unsigned, nesting_count);
515 unsigned count;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700516
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700517 do {
518 unsigned long pending_words;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700519
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700520 vcpu_info->evtchn_upcall_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700521
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700522 if (__get_cpu_var(nesting_count)++)
523 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700524
Isaku Yamahatae849c3e2008-04-02 10:53:56 -0700525#ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
526 /* Clear master flag /before/ clearing selector flag. */
Isaku Yamahata6673cf62008-06-16 14:58:13 -0700527 wmb();
Isaku Yamahatae849c3e2008-04-02 10:53:56 -0700528#endif
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700529 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
530 while (pending_words != 0) {
531 unsigned long pending_bits;
532 int word_idx = __ffs(pending_words);
533 pending_words &= ~(1UL << word_idx);
534
535 while ((pending_bits = active_evtchns(cpu, s, word_idx)) != 0) {
536 int bit_idx = __ffs(pending_bits);
537 int port = (word_idx * BITS_PER_LONG) + bit_idx;
538 int irq = evtchn_to_irq[port];
539
Isaku Yamahatae849c3e2008-04-02 10:53:56 -0700540 if (irq != -1)
541 xen_do_IRQ(irq, regs);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700542 }
543 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700544
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -0700545 BUG_ON(!irqs_disabled());
546
547 count = __get_cpu_var(nesting_count);
548 __get_cpu_var(nesting_count) = 0;
549 } while(count != 1);
550
551out:
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700552 put_cpu();
553}
554
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +0100555/* Rebind a new event channel to an existing irq. */
556void rebind_evtchn_irq(int evtchn, int irq)
557{
558 /* Make sure the irq is masked, since the new event channel
559 will also be masked. */
560 disable_irq(irq);
561
562 spin_lock(&irq_mapping_update_lock);
563
564 /* After resume the irq<->evtchn mappings are all cleared out */
565 BUG_ON(evtchn_to_irq[evtchn] != -1);
566 /* Expect irq to have been bound before,
567 so the bindcount should be non-0 */
568 BUG_ON(irq_bindcount[irq] == 0);
569
570 evtchn_to_irq[evtchn] = irq;
571 irq_info[irq] = mk_irq_info(IRQT_EVTCHN, 0, evtchn);
572
573 spin_unlock(&irq_mapping_update_lock);
574
575 /* new event channels are always bound to cpu 0 */
576 irq_set_affinity(irq, cpumask_of_cpu(0));
577
578 /* Unmask the event channel. */
579 enable_irq(irq);
580}
581
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700582/* Rebind an evtchn so that it gets delivered to a specific cpu */
583static void rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
584{
585 struct evtchn_bind_vcpu bind_vcpu;
586 int evtchn = evtchn_from_irq(irq);
587
588 if (!VALID_EVTCHN(evtchn))
589 return;
590
591 /* Send future instances of this interrupt to other vcpu. */
592 bind_vcpu.port = evtchn;
593 bind_vcpu.vcpu = tcpu;
594
595 /*
596 * If this fails, it usually just indicates that we're dealing with a
597 * virq or IPI channel, which don't actually need to be rebound. Ignore
598 * it, but don't do the xenlinux-level rebind in that case.
599 */
600 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
601 bind_evtchn_to_cpu(evtchn, tcpu);
602}
603
604
605static void set_affinity_irq(unsigned irq, cpumask_t dest)
606{
607 unsigned tcpu = first_cpu(dest);
608 rebind_irq_to_cpu(irq, tcpu);
609}
610
Isaku Yamahata642e0c82008-04-02 10:53:57 -0700611int resend_irq_on_evtchn(unsigned int irq)
612{
613 int masked, evtchn = evtchn_from_irq(irq);
614 struct shared_info *s = HYPERVISOR_shared_info;
615
616 if (!VALID_EVTCHN(evtchn))
617 return 1;
618
619 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
620 sync_set_bit(evtchn, s->evtchn_pending);
621 if (!masked)
622 unmask_evtchn(evtchn);
623
624 return 1;
625}
626
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700627static void enable_dynirq(unsigned int irq)
628{
629 int evtchn = evtchn_from_irq(irq);
630
631 if (VALID_EVTCHN(evtchn))
632 unmask_evtchn(evtchn);
633}
634
635static void disable_dynirq(unsigned int irq)
636{
637 int evtchn = evtchn_from_irq(irq);
638
639 if (VALID_EVTCHN(evtchn))
640 mask_evtchn(evtchn);
641}
642
643static void ack_dynirq(unsigned int irq)
644{
645 int evtchn = evtchn_from_irq(irq);
646
647 move_native_irq(irq);
648
649 if (VALID_EVTCHN(evtchn))
650 clear_evtchn(evtchn);
651}
652
653static int retrigger_dynirq(unsigned int irq)
654{
655 int evtchn = evtchn_from_irq(irq);
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -0700656 struct shared_info *sh = HYPERVISOR_shared_info;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700657 int ret = 0;
658
659 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -0700660 int masked;
661
662 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
663 sync_set_bit(evtchn, sh->evtchn_pending);
664 if (!masked)
665 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700666 ret = 1;
667 }
668
669 return ret;
670}
671
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100672static void restore_cpu_virqs(unsigned int cpu)
673{
674 struct evtchn_bind_virq bind_virq;
675 int virq, irq, evtchn;
676
677 for (virq = 0; virq < NR_VIRQS; virq++) {
678 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
679 continue;
680
681 BUG_ON(irq_info[irq].type != IRQT_VIRQ);
682 BUG_ON(irq_info[irq].index != virq);
683
684 /* Get a new binding from Xen. */
685 bind_virq.virq = virq;
686 bind_virq.vcpu = cpu;
687 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
688 &bind_virq) != 0)
689 BUG();
690 evtchn = bind_virq.port;
691
692 /* Record the new mapping. */
693 evtchn_to_irq[evtchn] = irq;
694 irq_info[irq] = mk_irq_info(IRQT_VIRQ, virq, evtchn);
695 bind_evtchn_to_cpu(evtchn, cpu);
696
697 /* Ready for use. */
698 unmask_evtchn(evtchn);
699 }
700}
701
702static void restore_cpu_ipis(unsigned int cpu)
703{
704 struct evtchn_bind_ipi bind_ipi;
705 int ipi, irq, evtchn;
706
707 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
708 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
709 continue;
710
711 BUG_ON(irq_info[irq].type != IRQT_IPI);
712 BUG_ON(irq_info[irq].index != ipi);
713
714 /* Get a new binding from Xen. */
715 bind_ipi.vcpu = cpu;
716 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
717 &bind_ipi) != 0)
718 BUG();
719 evtchn = bind_ipi.port;
720
721 /* Record the new mapping. */
722 evtchn_to_irq[evtchn] = irq;
723 irq_info[irq] = mk_irq_info(IRQT_IPI, ipi, evtchn);
724 bind_evtchn_to_cpu(evtchn, cpu);
725
726 /* Ready for use. */
727 unmask_evtchn(evtchn);
728
729 }
730}
731
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -0700732/* Clear an irq's pending state, in preparation for polling on it */
733void xen_clear_irq_pending(int irq)
734{
735 int evtchn = evtchn_from_irq(irq);
736
737 if (VALID_EVTCHN(evtchn))
738 clear_evtchn(evtchn);
739}
740
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700741void xen_set_irq_pending(int irq)
742{
743 int evtchn = evtchn_from_irq(irq);
744
745 if (VALID_EVTCHN(evtchn))
746 set_evtchn(evtchn);
747}
748
749bool xen_test_irq_pending(int irq)
750{
751 int evtchn = evtchn_from_irq(irq);
752 bool ret = false;
753
754 if (VALID_EVTCHN(evtchn))
755 ret = test_evtchn(evtchn);
756
757 return ret;
758}
759
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -0700760/* Poll waiting for an irq to become pending. In the usual case, the
761 irq will be disabled so it won't deliver an interrupt. */
762void xen_poll_irq(int irq)
763{
764 evtchn_port_t evtchn = evtchn_from_irq(irq);
765
766 if (VALID_EVTCHN(evtchn)) {
767 struct sched_poll poll;
768
769 poll.nr_ports = 1;
770 poll.timeout = 0;
771 poll.ports = &evtchn;
772
773 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
774 BUG();
775 }
776}
777
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +0100778void xen_irq_resume(void)
779{
780 unsigned int cpu, irq, evtchn;
781
782 init_evtchn_cpu_bindings();
783
784 /* New event-channel space is not 'live' yet. */
785 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
786 mask_evtchn(evtchn);
787
788 /* No IRQ <-> event-channel mappings. */
789 for (irq = 0; irq < NR_IRQS; irq++)
790 irq_info[irq].evtchn = 0; /* zap event-channel binding */
791
792 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
793 evtchn_to_irq[evtchn] = -1;
794
795 for_each_possible_cpu(cpu) {
796 restore_cpu_virqs(cpu);
797 restore_cpu_ipis(cpu);
798 }
799}
800
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700801static struct irq_chip xen_dynamic_chip __read_mostly = {
802 .name = "xen-dyn",
803 .mask = disable_dynirq,
804 .unmask = enable_dynirq,
805 .ack = ack_dynirq,
806 .set_affinity = set_affinity_irq,
807 .retrigger = retrigger_dynirq,
808};
809
810void __init xen_init_IRQ(void)
811{
812 int i;
813
814 init_evtchn_cpu_bindings();
815
816 /* No event channels are 'live' right now. */
817 for (i = 0; i < NR_EVENT_CHANNELS; i++)
818 mask_evtchn(i);
819
820 /* Dynamic IRQ space is currently unbound. Zero the refcnts. */
821 for (i = 0; i < NR_IRQS; i++)
822 irq_bindcount[i] = 0;
823
824 irq_ctx_init(smp_processor_id());
825}