xref: /openbmc/linux/drivers/xen/events/events_base.c (revision 7ebf812b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Xen event channels
4  *
5  * Xen models interrupts with abstract event channels.  Because each
6  * domain gets 1024 event channels, but NR_IRQ is not that large, we
7  * must dynamically map irqs<->event channels.  The event channels
8  * interface with the rest of the kernel by defining a xen interrupt
9  * chip.  When an event is received, it is mapped to an irq and sent
10  * through the normal interrupt processing path.
11  *
12  * There are four kinds of events which can be mapped to an event
13  * channel:
14  *
15  * 1. Inter-domain notifications.  This includes all the virtual
16  *    device events, since they're driven by front-ends in another domain
17  *    (typically dom0).
18  * 2. VIRQs, typically used for timers.  These are per-cpu events.
19  * 3. IPIs.
20  * 4. PIRQs - Hardware interrupts.
21  *
22  * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
23  */
24 
25 #define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
26 
27 #include <linux/linkage.h>
28 #include <linux/interrupt.h>
29 #include <linux/irq.h>
30 #include <linux/moduleparam.h>
31 #include <linux/string.h>
32 #include <linux/memblock.h>
33 #include <linux/slab.h>
34 #include <linux/irqnr.h>
35 #include <linux/pci.h>
36 #include <linux/rcupdate.h>
37 #include <linux/spinlock.h>
38 #include <linux/cpuhotplug.h>
39 #include <linux/atomic.h>
40 #include <linux/ktime.h>
41 
42 #ifdef CONFIG_X86
43 #include <asm/desc.h>
44 #include <asm/ptrace.h>
45 #include <asm/idtentry.h>
46 #include <asm/irq.h>
47 #include <asm/io_apic.h>
48 #include <asm/i8259.h>
49 #include <asm/xen/cpuid.h>
50 #include <asm/xen/pci.h>
51 #endif
52 #include <asm/sync_bitops.h>
53 #include <asm/xen/hypercall.h>
54 #include <asm/xen/hypervisor.h>
55 #include <xen/page.h>
56 
57 #include <xen/xen.h>
58 #include <xen/hvm.h>
59 #include <xen/xen-ops.h>
60 #include <xen/events.h>
61 #include <xen/interface/xen.h>
62 #include <xen/interface/event_channel.h>
63 #include <xen/interface/hvm/hvm_op.h>
64 #include <xen/interface/hvm/params.h>
65 #include <xen/interface/physdev.h>
66 #include <xen/interface/sched.h>
67 #include <xen/interface/vcpu.h>
68 #include <xen/xenbus.h>
69 #include <asm/hw_irq.h>
70 
71 #include "events_internal.h"
72 
73 #undef MODULE_PARAM_PREFIX
74 #define MODULE_PARAM_PREFIX "xen."
75 
76 /* Interrupt types. */
77 enum xen_irq_type {
78 	IRQT_UNBOUND = 0,
79 	IRQT_PIRQ,
80 	IRQT_VIRQ,
81 	IRQT_IPI,
82 	IRQT_EVTCHN
83 };
84 
85 /*
86  * Packed IRQ information:
87  * type - enum xen_irq_type
88  * event channel - irq->event channel mapping
89  * cpu - cpu this event channel is bound to
90  * index - type-specific information:
91  *    PIRQ - vector, with MSB being "needs EIO", or physical IRQ of the HVM
92  *           guest, or GSI (real passthrough IRQ) of the device.
93  *    VIRQ - virq number
94  *    IPI - IPI vector
95  *    EVTCHN -
96  */
97 struct irq_info {
98 	struct list_head list;
99 	struct list_head eoi_list;
100 	struct rcu_work rwork;
101 	short refcnt;
102 	u8 spurious_cnt;
103 	u8 is_accounted;
104 	short type;		/* type: IRQT_* */
105 	u8 mask_reason;		/* Why is event channel masked */
106 #define EVT_MASK_REASON_EXPLICIT	0x01
107 #define EVT_MASK_REASON_TEMPORARY	0x02
108 #define EVT_MASK_REASON_EOI_PENDING	0x04
109 	u8 is_active;		/* Is event just being handled? */
110 	unsigned irq;
111 	evtchn_port_t evtchn;   /* event channel */
112 	unsigned short cpu;     /* cpu bound */
113 	unsigned short eoi_cpu; /* EOI must happen on this cpu-1 */
114 	unsigned int irq_epoch; /* If eoi_cpu valid: irq_epoch of event */
115 	u64 eoi_time;           /* Time in jiffies when to EOI. */
116 	raw_spinlock_t lock;
117 	bool is_static;           /* Is event channel static */
118 
119 	union {
120 		unsigned short virq;
121 		enum ipi_vector ipi;
122 		struct {
123 			unsigned short pirq;
124 			unsigned short gsi;
125 			unsigned char vector;
126 			unsigned char flags;
127 			uint16_t domid;
128 		} pirq;
129 		struct xenbus_device *interdomain;
130 	} u;
131 };
132 
133 #define PIRQ_NEEDS_EOI	(1 << 0)
134 #define PIRQ_SHAREABLE	(1 << 1)
135 #define PIRQ_MSI_GROUP	(1 << 2)
136 
137 static uint __read_mostly event_loop_timeout = 2;
138 module_param(event_loop_timeout, uint, 0644);
139 
140 static uint __read_mostly event_eoi_delay = 10;
141 module_param(event_eoi_delay, uint, 0644);
142 
143 const struct evtchn_ops *evtchn_ops;
144 
145 /*
146  * This lock protects updates to the following mapping and reference-count
147  * arrays. The lock does not need to be acquired to read the mapping tables.
148  */
149 static DEFINE_MUTEX(irq_mapping_update_lock);
150 
151 /*
152  * Lock hierarchy:
153  *
154  * irq_mapping_update_lock
155  *   IRQ-desc lock
156  *     percpu eoi_list_lock
157  *       irq_info->lock
158  */
159 
160 static LIST_HEAD(xen_irq_list_head);
161 
162 /* IRQ <-> VIRQ mapping. */
163 static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
164 
165 /* IRQ <-> IPI mapping */
166 static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
167 /* Cache for IPI event channels - needed for hot cpu unplug (avoid RCU usage). */
168 static DEFINE_PER_CPU(evtchn_port_t [XEN_NR_IPIS], ipi_to_evtchn) = {[0 ... XEN_NR_IPIS-1] = 0};
169 
170 /* Event channel distribution data */
171 static atomic_t channels_on_cpu[NR_CPUS];
172 
173 static int **evtchn_to_irq;
174 #ifdef CONFIG_X86
175 static unsigned long *pirq_eoi_map;
176 #endif
177 static bool (*pirq_needs_eoi)(unsigned irq);
178 
179 #define EVTCHN_ROW(e)  (e / (PAGE_SIZE/sizeof(**evtchn_to_irq)))
180 #define EVTCHN_COL(e)  (e % (PAGE_SIZE/sizeof(**evtchn_to_irq)))
181 #define EVTCHN_PER_ROW (PAGE_SIZE / sizeof(**evtchn_to_irq))
182 
183 /* Xen will never allocate port zero for any purpose. */
184 #define VALID_EVTCHN(chn)	((chn) != 0)
185 
186 static struct irq_info *legacy_info_ptrs[NR_IRQS_LEGACY];
187 
188 static struct irq_chip xen_dynamic_chip;
189 static struct irq_chip xen_lateeoi_chip;
190 static struct irq_chip xen_percpu_chip;
191 static struct irq_chip xen_pirq_chip;
192 static void enable_dynirq(struct irq_data *data);
193 static void disable_dynirq(struct irq_data *data);
194 
195 static DEFINE_PER_CPU(unsigned int, irq_epoch);
196 
197 static void clear_evtchn_to_irq_row(int *evtchn_row)
198 {
199 	unsigned col;
200 
201 	for (col = 0; col < EVTCHN_PER_ROW; col++)
202 		WRITE_ONCE(evtchn_row[col], -1);
203 }
204 
205 static void clear_evtchn_to_irq_all(void)
206 {
207 	unsigned row;
208 
209 	for (row = 0; row < EVTCHN_ROW(xen_evtchn_max_channels()); row++) {
210 		if (evtchn_to_irq[row] == NULL)
211 			continue;
212 		clear_evtchn_to_irq_row(evtchn_to_irq[row]);
213 	}
214 }
215 
216 static int set_evtchn_to_irq(evtchn_port_t evtchn, unsigned int irq)
217 {
218 	unsigned row;
219 	unsigned col;
220 	int *evtchn_row;
221 
222 	if (evtchn >= xen_evtchn_max_channels())
223 		return -EINVAL;
224 
225 	row = EVTCHN_ROW(evtchn);
226 	col = EVTCHN_COL(evtchn);
227 
228 	if (evtchn_to_irq[row] == NULL) {
229 		/* Unallocated irq entries return -1 anyway */
230 		if (irq == -1)
231 			return 0;
232 
233 		evtchn_row = (int *) __get_free_pages(GFP_KERNEL, 0);
234 		if (evtchn_row == NULL)
235 			return -ENOMEM;
236 
237 		clear_evtchn_to_irq_row(evtchn_row);
238 
239 		/*
240 		 * We've prepared an empty row for the mapping. If a different
241 		 * thread was faster inserting it, we can drop ours.
242 		 */
243 		if (cmpxchg(&evtchn_to_irq[row], NULL, evtchn_row) != NULL)
244 			free_page((unsigned long) evtchn_row);
245 	}
246 
247 	WRITE_ONCE(evtchn_to_irq[row][col], irq);
248 	return 0;
249 }
250 
251 int get_evtchn_to_irq(evtchn_port_t evtchn)
252 {
253 	if (evtchn >= xen_evtchn_max_channels())
254 		return -1;
255 	if (evtchn_to_irq[EVTCHN_ROW(evtchn)] == NULL)
256 		return -1;
257 	return READ_ONCE(evtchn_to_irq[EVTCHN_ROW(evtchn)][EVTCHN_COL(evtchn)]);
258 }
259 
260 /* Get info for IRQ */
261 static struct irq_info *info_for_irq(unsigned irq)
262 {
263 	if (irq < nr_legacy_irqs())
264 		return legacy_info_ptrs[irq];
265 	else
266 		return irq_get_chip_data(irq);
267 }
268 
269 static void set_info_for_irq(unsigned int irq, struct irq_info *info)
270 {
271 	if (irq < nr_legacy_irqs())
272 		legacy_info_ptrs[irq] = info;
273 	else
274 		irq_set_chip_data(irq, info);
275 }
276 
277 /* Per CPU channel accounting */
278 static void channels_on_cpu_dec(struct irq_info *info)
279 {
280 	if (!info->is_accounted)
281 		return;
282 
283 	info->is_accounted = 0;
284 
285 	if (WARN_ON_ONCE(info->cpu >= nr_cpu_ids))
286 		return;
287 
288 	WARN_ON_ONCE(!atomic_add_unless(&channels_on_cpu[info->cpu], -1 , 0));
289 }
290 
291 static void channels_on_cpu_inc(struct irq_info *info)
292 {
293 	if (WARN_ON_ONCE(info->cpu >= nr_cpu_ids))
294 		return;
295 
296 	if (WARN_ON_ONCE(!atomic_add_unless(&channels_on_cpu[info->cpu], 1,
297 					    INT_MAX)))
298 		return;
299 
300 	info->is_accounted = 1;
301 }
302 
303 static void delayed_free_irq(struct work_struct *work)
304 {
305 	struct irq_info *info = container_of(to_rcu_work(work), struct irq_info,
306 					     rwork);
307 	unsigned int irq = info->irq;
308 
309 	/* Remove the info pointer only now, with no potential users left. */
310 	set_info_for_irq(irq, NULL);
311 
312 	kfree(info);
313 
314 	/* Legacy IRQ descriptors are managed by the arch. */
315 	if (irq >= nr_legacy_irqs())
316 		irq_free_desc(irq);
317 }
318 
319 /* Constructors for packed IRQ information. */
320 static int xen_irq_info_common_setup(struct irq_info *info,
321 				     unsigned irq,
322 				     enum xen_irq_type type,
323 				     evtchn_port_t evtchn,
324 				     unsigned short cpu)
325 {
326 	int ret;
327 
328 	BUG_ON(info->type != IRQT_UNBOUND && info->type != type);
329 
330 	info->type = type;
331 	info->irq = irq;
332 	info->evtchn = evtchn;
333 	info->cpu = cpu;
334 	info->mask_reason = EVT_MASK_REASON_EXPLICIT;
335 	raw_spin_lock_init(&info->lock);
336 
337 	ret = set_evtchn_to_irq(evtchn, irq);
338 	if (ret < 0)
339 		return ret;
340 
341 	irq_clear_status_flags(irq, IRQ_NOREQUEST|IRQ_NOAUTOEN);
342 
343 	return xen_evtchn_port_setup(evtchn);
344 }
345 
346 static int xen_irq_info_evtchn_setup(unsigned irq,
347 				     evtchn_port_t evtchn,
348 				     struct xenbus_device *dev)
349 {
350 	struct irq_info *info = info_for_irq(irq);
351 	int ret;
352 
353 	ret = xen_irq_info_common_setup(info, irq, IRQT_EVTCHN, evtchn, 0);
354 	info->u.interdomain = dev;
355 	if (dev)
356 		atomic_inc(&dev->event_channels);
357 
358 	return ret;
359 }
360 
361 static int xen_irq_info_ipi_setup(unsigned cpu,
362 				  unsigned irq,
363 				  evtchn_port_t evtchn,
364 				  enum ipi_vector ipi)
365 {
366 	struct irq_info *info = info_for_irq(irq);
367 
368 	info->u.ipi = ipi;
369 
370 	per_cpu(ipi_to_irq, cpu)[ipi] = irq;
371 	per_cpu(ipi_to_evtchn, cpu)[ipi] = evtchn;
372 
373 	return xen_irq_info_common_setup(info, irq, IRQT_IPI, evtchn, 0);
374 }
375 
376 static int xen_irq_info_virq_setup(unsigned cpu,
377 				   unsigned irq,
378 				   evtchn_port_t evtchn,
379 				   unsigned virq)
380 {
381 	struct irq_info *info = info_for_irq(irq);
382 
383 	info->u.virq = virq;
384 
385 	per_cpu(virq_to_irq, cpu)[virq] = irq;
386 
387 	return xen_irq_info_common_setup(info, irq, IRQT_VIRQ, evtchn, 0);
388 }
389 
390 static int xen_irq_info_pirq_setup(unsigned irq,
391 				   evtchn_port_t evtchn,
392 				   unsigned pirq,
393 				   unsigned gsi,
394 				   uint16_t domid,
395 				   unsigned char flags)
396 {
397 	struct irq_info *info = info_for_irq(irq);
398 
399 	info->u.pirq.pirq = pirq;
400 	info->u.pirq.gsi = gsi;
401 	info->u.pirq.domid = domid;
402 	info->u.pirq.flags = flags;
403 
404 	return xen_irq_info_common_setup(info, irq, IRQT_PIRQ, evtchn, 0);
405 }
406 
407 static void xen_irq_info_cleanup(struct irq_info *info)
408 {
409 	set_evtchn_to_irq(info->evtchn, -1);
410 	xen_evtchn_port_remove(info->evtchn, info->cpu);
411 	info->evtchn = 0;
412 	channels_on_cpu_dec(info);
413 }
414 
415 /*
416  * Accessors for packed IRQ information.
417  */
418 evtchn_port_t evtchn_from_irq(unsigned irq)
419 {
420 	const struct irq_info *info = NULL;
421 
422 	if (likely(irq < nr_irqs))
423 		info = info_for_irq(irq);
424 	if (!info)
425 		return 0;
426 
427 	return info->evtchn;
428 }
429 
430 unsigned int irq_from_evtchn(evtchn_port_t evtchn)
431 {
432 	return get_evtchn_to_irq(evtchn);
433 }
434 EXPORT_SYMBOL_GPL(irq_from_evtchn);
435 
436 int irq_from_virq(unsigned int cpu, unsigned int virq)
437 {
438 	return per_cpu(virq_to_irq, cpu)[virq];
439 }
440 
441 static enum ipi_vector ipi_from_irq(unsigned irq)
442 {
443 	struct irq_info *info = info_for_irq(irq);
444 
445 	BUG_ON(info == NULL);
446 	BUG_ON(info->type != IRQT_IPI);
447 
448 	return info->u.ipi;
449 }
450 
451 static unsigned virq_from_irq(unsigned irq)
452 {
453 	struct irq_info *info = info_for_irq(irq);
454 
455 	BUG_ON(info == NULL);
456 	BUG_ON(info->type != IRQT_VIRQ);
457 
458 	return info->u.virq;
459 }
460 
461 static unsigned pirq_from_irq(unsigned irq)
462 {
463 	struct irq_info *info = info_for_irq(irq);
464 
465 	BUG_ON(info == NULL);
466 	BUG_ON(info->type != IRQT_PIRQ);
467 
468 	return info->u.pirq.pirq;
469 }
470 
471 static enum xen_irq_type type_from_irq(unsigned irq)
472 {
473 	return info_for_irq(irq)->type;
474 }
475 
476 static unsigned cpu_from_irq(unsigned irq)
477 {
478 	return info_for_irq(irq)->cpu;
479 }
480 
481 unsigned int cpu_from_evtchn(evtchn_port_t evtchn)
482 {
483 	int irq = get_evtchn_to_irq(evtchn);
484 	unsigned ret = 0;
485 
486 	if (irq != -1)
487 		ret = cpu_from_irq(irq);
488 
489 	return ret;
490 }
491 
492 static void do_mask(struct irq_info *info, u8 reason)
493 {
494 	unsigned long flags;
495 
496 	raw_spin_lock_irqsave(&info->lock, flags);
497 
498 	if (!info->mask_reason)
499 		mask_evtchn(info->evtchn);
500 
501 	info->mask_reason |= reason;
502 
503 	raw_spin_unlock_irqrestore(&info->lock, flags);
504 }
505 
506 static void do_unmask(struct irq_info *info, u8 reason)
507 {
508 	unsigned long flags;
509 
510 	raw_spin_lock_irqsave(&info->lock, flags);
511 
512 	info->mask_reason &= ~reason;
513 
514 	if (!info->mask_reason)
515 		unmask_evtchn(info->evtchn);
516 
517 	raw_spin_unlock_irqrestore(&info->lock, flags);
518 }
519 
520 #ifdef CONFIG_X86
521 static bool pirq_check_eoi_map(unsigned irq)
522 {
523 	return test_bit(pirq_from_irq(irq), pirq_eoi_map);
524 }
525 #endif
526 
527 static bool pirq_needs_eoi_flag(unsigned irq)
528 {
529 	struct irq_info *info = info_for_irq(irq);
530 	BUG_ON(info->type != IRQT_PIRQ);
531 
532 	return info->u.pirq.flags & PIRQ_NEEDS_EOI;
533 }
534 
535 static void bind_evtchn_to_cpu(evtchn_port_t evtchn, unsigned int cpu,
536 			       bool force_affinity)
537 {
538 	int irq = get_evtchn_to_irq(evtchn);
539 	struct irq_info *info = info_for_irq(irq);
540 
541 	BUG_ON(irq == -1);
542 
543 	if (IS_ENABLED(CONFIG_SMP) && force_affinity) {
544 		struct irq_data *data = irq_get_irq_data(irq);
545 
546 		irq_data_update_affinity(data, cpumask_of(cpu));
547 		irq_data_update_effective_affinity(data, cpumask_of(cpu));
548 	}
549 
550 	xen_evtchn_port_bind_to_cpu(evtchn, cpu, info->cpu);
551 
552 	channels_on_cpu_dec(info);
553 	info->cpu = cpu;
554 	channels_on_cpu_inc(info);
555 }
556 
557 /**
558  * notify_remote_via_irq - send event to remote end of event channel via irq
559  * @irq: irq of event channel to send event to
560  *
561  * Unlike notify_remote_via_evtchn(), this is safe to use across
562  * save/restore. Notifications on a broken connection are silently
563  * dropped.
564  */
565 void notify_remote_via_irq(int irq)
566 {
567 	evtchn_port_t evtchn = evtchn_from_irq(irq);
568 
569 	if (VALID_EVTCHN(evtchn))
570 		notify_remote_via_evtchn(evtchn);
571 }
572 EXPORT_SYMBOL_GPL(notify_remote_via_irq);
573 
574 struct lateeoi_work {
575 	struct delayed_work delayed;
576 	spinlock_t eoi_list_lock;
577 	struct list_head eoi_list;
578 };
579 
580 static DEFINE_PER_CPU(struct lateeoi_work, lateeoi);
581 
582 static void lateeoi_list_del(struct irq_info *info)
583 {
584 	struct lateeoi_work *eoi = &per_cpu(lateeoi, info->eoi_cpu);
585 	unsigned long flags;
586 
587 	spin_lock_irqsave(&eoi->eoi_list_lock, flags);
588 	list_del_init(&info->eoi_list);
589 	spin_unlock_irqrestore(&eoi->eoi_list_lock, flags);
590 }
591 
592 static void lateeoi_list_add(struct irq_info *info)
593 {
594 	struct lateeoi_work *eoi = &per_cpu(lateeoi, info->eoi_cpu);
595 	struct irq_info *elem;
596 	u64 now = get_jiffies_64();
597 	unsigned long delay;
598 	unsigned long flags;
599 
600 	if (now < info->eoi_time)
601 		delay = info->eoi_time - now;
602 	else
603 		delay = 1;
604 
605 	spin_lock_irqsave(&eoi->eoi_list_lock, flags);
606 
607 	elem = list_first_entry_or_null(&eoi->eoi_list, struct irq_info,
608 					eoi_list);
609 	if (!elem || info->eoi_time < elem->eoi_time) {
610 		list_add(&info->eoi_list, &eoi->eoi_list);
611 		mod_delayed_work_on(info->eoi_cpu, system_wq,
612 				    &eoi->delayed, delay);
613 	} else {
614 		list_for_each_entry_reverse(elem, &eoi->eoi_list, eoi_list) {
615 			if (elem->eoi_time <= info->eoi_time)
616 				break;
617 		}
618 		list_add(&info->eoi_list, &elem->eoi_list);
619 	}
620 
621 	spin_unlock_irqrestore(&eoi->eoi_list_lock, flags);
622 }
623 
624 static void xen_irq_lateeoi_locked(struct irq_info *info, bool spurious)
625 {
626 	evtchn_port_t evtchn;
627 	unsigned int cpu;
628 	unsigned int delay = 0;
629 
630 	evtchn = info->evtchn;
631 	if (!VALID_EVTCHN(evtchn) || !list_empty(&info->eoi_list))
632 		return;
633 
634 	if (spurious) {
635 		struct xenbus_device *dev = info->u.interdomain;
636 		unsigned int threshold = 1;
637 
638 		if (dev && dev->spurious_threshold)
639 			threshold = dev->spurious_threshold;
640 
641 		if ((1 << info->spurious_cnt) < (HZ << 2)) {
642 			if (info->spurious_cnt != 0xFF)
643 				info->spurious_cnt++;
644 		}
645 		if (info->spurious_cnt > threshold) {
646 			delay = 1 << (info->spurious_cnt - 1 - threshold);
647 			if (delay > HZ)
648 				delay = HZ;
649 			if (!info->eoi_time)
650 				info->eoi_cpu = smp_processor_id();
651 			info->eoi_time = get_jiffies_64() + delay;
652 			if (dev)
653 				atomic_add(delay, &dev->jiffies_eoi_delayed);
654 		}
655 		if (dev)
656 			atomic_inc(&dev->spurious_events);
657 	} else {
658 		info->spurious_cnt = 0;
659 	}
660 
661 	cpu = info->eoi_cpu;
662 	if (info->eoi_time &&
663 	    (info->irq_epoch == per_cpu(irq_epoch, cpu) || delay)) {
664 		lateeoi_list_add(info);
665 		return;
666 	}
667 
668 	info->eoi_time = 0;
669 
670 	/* is_active hasn't been reset yet, do it now. */
671 	smp_store_release(&info->is_active, 0);
672 	do_unmask(info, EVT_MASK_REASON_EOI_PENDING);
673 }
674 
675 static void xen_irq_lateeoi_worker(struct work_struct *work)
676 {
677 	struct lateeoi_work *eoi;
678 	struct irq_info *info;
679 	u64 now = get_jiffies_64();
680 	unsigned long flags;
681 
682 	eoi = container_of(to_delayed_work(work), struct lateeoi_work, delayed);
683 
684 	rcu_read_lock();
685 
686 	while (true) {
687 		spin_lock_irqsave(&eoi->eoi_list_lock, flags);
688 
689 		info = list_first_entry_or_null(&eoi->eoi_list, struct irq_info,
690 						eoi_list);
691 
692 		if (info == NULL)
693 			break;
694 
695 		if (now < info->eoi_time) {
696 			mod_delayed_work_on(info->eoi_cpu, system_wq,
697 					    &eoi->delayed,
698 					    info->eoi_time - now);
699 			break;
700 		}
701 
702 		list_del_init(&info->eoi_list);
703 
704 		spin_unlock_irqrestore(&eoi->eoi_list_lock, flags);
705 
706 		info->eoi_time = 0;
707 
708 		xen_irq_lateeoi_locked(info, false);
709 	}
710 
711 	spin_unlock_irqrestore(&eoi->eoi_list_lock, flags);
712 
713 	rcu_read_unlock();
714 }
715 
716 static void xen_cpu_init_eoi(unsigned int cpu)
717 {
718 	struct lateeoi_work *eoi = &per_cpu(lateeoi, cpu);
719 
720 	INIT_DELAYED_WORK(&eoi->delayed, xen_irq_lateeoi_worker);
721 	spin_lock_init(&eoi->eoi_list_lock);
722 	INIT_LIST_HEAD(&eoi->eoi_list);
723 }
724 
725 void xen_irq_lateeoi(unsigned int irq, unsigned int eoi_flags)
726 {
727 	struct irq_info *info;
728 
729 	rcu_read_lock();
730 
731 	info = info_for_irq(irq);
732 
733 	if (info)
734 		xen_irq_lateeoi_locked(info, eoi_flags & XEN_EOI_FLAG_SPURIOUS);
735 
736 	rcu_read_unlock();
737 }
738 EXPORT_SYMBOL_GPL(xen_irq_lateeoi);
739 
740 static void xen_irq_init(unsigned irq)
741 {
742 	struct irq_info *info;
743 
744 	info = kzalloc(sizeof(*info), GFP_KERNEL);
745 	if (info == NULL)
746 		panic("Unable to allocate metadata for IRQ%d\n", irq);
747 
748 	info->type = IRQT_UNBOUND;
749 	info->refcnt = -1;
750 	INIT_RCU_WORK(&info->rwork, delayed_free_irq);
751 
752 	set_info_for_irq(irq, info);
753 	/*
754 	 * Interrupt affinity setting can be immediate. No point
755 	 * in delaying it until an interrupt is handled.
756 	 */
757 	irq_set_status_flags(irq, IRQ_MOVE_PCNTXT);
758 
759 	INIT_LIST_HEAD(&info->eoi_list);
760 	list_add_tail(&info->list, &xen_irq_list_head);
761 }
762 
763 static int __must_check xen_allocate_irqs_dynamic(int nvec)
764 {
765 	int i, irq = irq_alloc_descs(-1, 0, nvec, -1);
766 
767 	if (irq >= 0) {
768 		for (i = 0; i < nvec; i++)
769 			xen_irq_init(irq + i);
770 	}
771 
772 	return irq;
773 }
774 
775 static inline int __must_check xen_allocate_irq_dynamic(void)
776 {
777 
778 	return xen_allocate_irqs_dynamic(1);
779 }
780 
781 static int __must_check xen_allocate_irq_gsi(unsigned gsi)
782 {
783 	int irq;
784 
785 	/*
786 	 * A PV guest has no concept of a GSI (since it has no ACPI
787 	 * nor access to/knowledge of the physical APICs). Therefore
788 	 * all IRQs are dynamically allocated from the entire IRQ
789 	 * space.
790 	 */
791 	if (xen_pv_domain() && !xen_initial_domain())
792 		return xen_allocate_irq_dynamic();
793 
794 	/* Legacy IRQ descriptors are already allocated by the arch. */
795 	if (gsi < nr_legacy_irqs())
796 		irq = gsi;
797 	else
798 		irq = irq_alloc_desc_at(gsi, -1);
799 
800 	xen_irq_init(irq);
801 
802 	return irq;
803 }
804 
805 static void xen_free_irq(unsigned irq)
806 {
807 	struct irq_info *info = info_for_irq(irq);
808 
809 	if (WARN_ON(!info))
810 		return;
811 
812 	if (!list_empty(&info->eoi_list))
813 		lateeoi_list_del(info);
814 
815 	list_del(&info->list);
816 
817 	WARN_ON(info->refcnt > 0);
818 
819 	queue_rcu_work(system_wq, &info->rwork);
820 }
821 
822 /* Not called for lateeoi events. */
823 static void event_handler_exit(struct irq_info *info)
824 {
825 	smp_store_release(&info->is_active, 0);
826 	clear_evtchn(info->evtchn);
827 }
828 
829 static void pirq_query_unmask(int irq)
830 {
831 	struct physdev_irq_status_query irq_status;
832 	struct irq_info *info = info_for_irq(irq);
833 
834 	BUG_ON(info->type != IRQT_PIRQ);
835 
836 	irq_status.irq = pirq_from_irq(irq);
837 	if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
838 		irq_status.flags = 0;
839 
840 	info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
841 	if (irq_status.flags & XENIRQSTAT_needs_eoi)
842 		info->u.pirq.flags |= PIRQ_NEEDS_EOI;
843 }
844 
845 static void eoi_pirq(struct irq_data *data)
846 {
847 	struct irq_info *info = info_for_irq(data->irq);
848 	evtchn_port_t evtchn = info ? info->evtchn : 0;
849 	struct physdev_eoi eoi = { .irq = pirq_from_irq(data->irq) };
850 	int rc = 0;
851 
852 	if (!VALID_EVTCHN(evtchn))
853 		return;
854 
855 	event_handler_exit(info);
856 
857 	if (pirq_needs_eoi(data->irq)) {
858 		rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
859 		WARN_ON(rc);
860 	}
861 }
862 
863 static void mask_ack_pirq(struct irq_data *data)
864 {
865 	disable_dynirq(data);
866 	eoi_pirq(data);
867 }
868 
869 static unsigned int __startup_pirq(unsigned int irq)
870 {
871 	struct evtchn_bind_pirq bind_pirq;
872 	struct irq_info *info = info_for_irq(irq);
873 	evtchn_port_t evtchn = evtchn_from_irq(irq);
874 	int rc;
875 
876 	BUG_ON(info->type != IRQT_PIRQ);
877 
878 	if (VALID_EVTCHN(evtchn))
879 		goto out;
880 
881 	bind_pirq.pirq = pirq_from_irq(irq);
882 	/* NB. We are happy to share unless we are probing. */
883 	bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
884 					BIND_PIRQ__WILL_SHARE : 0;
885 	rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
886 	if (rc != 0) {
887 		pr_warn("Failed to obtain physical IRQ %d\n", irq);
888 		return 0;
889 	}
890 	evtchn = bind_pirq.port;
891 
892 	pirq_query_unmask(irq);
893 
894 	rc = set_evtchn_to_irq(evtchn, irq);
895 	if (rc)
896 		goto err;
897 
898 	info->evtchn = evtchn;
899 	bind_evtchn_to_cpu(evtchn, 0, false);
900 
901 	rc = xen_evtchn_port_setup(evtchn);
902 	if (rc)
903 		goto err;
904 
905 out:
906 	do_unmask(info, EVT_MASK_REASON_EXPLICIT);
907 
908 	eoi_pirq(irq_get_irq_data(irq));
909 
910 	return 0;
911 
912 err:
913 	pr_err("irq%d: Failed to set port to irq mapping (%d)\n", irq, rc);
914 	xen_evtchn_close(evtchn);
915 	return 0;
916 }
917 
918 static unsigned int startup_pirq(struct irq_data *data)
919 {
920 	return __startup_pirq(data->irq);
921 }
922 
923 static void shutdown_pirq(struct irq_data *data)
924 {
925 	unsigned int irq = data->irq;
926 	struct irq_info *info = info_for_irq(irq);
927 	evtchn_port_t evtchn = evtchn_from_irq(irq);
928 
929 	BUG_ON(info->type != IRQT_PIRQ);
930 
931 	if (!VALID_EVTCHN(evtchn))
932 		return;
933 
934 	do_mask(info, EVT_MASK_REASON_EXPLICIT);
935 	xen_evtchn_close(evtchn);
936 	xen_irq_info_cleanup(info);
937 }
938 
939 static void enable_pirq(struct irq_data *data)
940 {
941 	enable_dynirq(data);
942 }
943 
944 static void disable_pirq(struct irq_data *data)
945 {
946 	disable_dynirq(data);
947 }
948 
949 int xen_irq_from_gsi(unsigned gsi)
950 {
951 	struct irq_info *info;
952 
953 	list_for_each_entry(info, &xen_irq_list_head, list) {
954 		if (info->type != IRQT_PIRQ)
955 			continue;
956 
957 		if (info->u.pirq.gsi == gsi)
958 			return info->irq;
959 	}
960 
961 	return -1;
962 }
963 EXPORT_SYMBOL_GPL(xen_irq_from_gsi);
964 
965 static void __unbind_from_irq(unsigned int irq)
966 {
967 	evtchn_port_t evtchn = evtchn_from_irq(irq);
968 	struct irq_info *info = info_for_irq(irq);
969 
970 	if (info->refcnt > 0) {
971 		info->refcnt--;
972 		if (info->refcnt != 0)
973 			return;
974 	}
975 
976 	if (VALID_EVTCHN(evtchn)) {
977 		unsigned int cpu = cpu_from_irq(irq);
978 		struct xenbus_device *dev;
979 
980 		if (!info->is_static)
981 			xen_evtchn_close(evtchn);
982 
983 		switch (type_from_irq(irq)) {
984 		case IRQT_VIRQ:
985 			per_cpu(virq_to_irq, cpu)[virq_from_irq(irq)] = -1;
986 			break;
987 		case IRQT_IPI:
988 			per_cpu(ipi_to_irq, cpu)[ipi_from_irq(irq)] = -1;
989 			per_cpu(ipi_to_evtchn, cpu)[ipi_from_irq(irq)] = 0;
990 			break;
991 		case IRQT_EVTCHN:
992 			dev = info->u.interdomain;
993 			if (dev)
994 				atomic_dec(&dev->event_channels);
995 			break;
996 		default:
997 			break;
998 		}
999 
1000 		xen_irq_info_cleanup(info);
1001 	}
1002 
1003 	xen_free_irq(irq);
1004 }
1005 
1006 /*
1007  * Do not make any assumptions regarding the relationship between the
1008  * IRQ number returned here and the Xen pirq argument.
1009  *
1010  * Note: We don't assign an event channel until the irq actually started
1011  * up.  Return an existing irq if we've already got one for the gsi.
1012  *
1013  * Shareable implies level triggered, not shareable implies edge
1014  * triggered here.
1015  */
1016 int xen_bind_pirq_gsi_to_irq(unsigned gsi,
1017 			     unsigned pirq, int shareable, char *name)
1018 {
1019 	int irq;
1020 	struct physdev_irq irq_op;
1021 	int ret;
1022 
1023 	mutex_lock(&irq_mapping_update_lock);
1024 
1025 	irq = xen_irq_from_gsi(gsi);
1026 	if (irq != -1) {
1027 		pr_info("%s: returning irq %d for gsi %u\n",
1028 			__func__, irq, gsi);
1029 		goto out;
1030 	}
1031 
1032 	irq = xen_allocate_irq_gsi(gsi);
1033 	if (irq < 0)
1034 		goto out;
1035 
1036 	irq_op.irq = irq;
1037 	irq_op.vector = 0;
1038 
1039 	/* Only the privileged domain can do this. For non-priv, the pcifront
1040 	 * driver provides a PCI bus that does the call to do exactly
1041 	 * this in the priv domain. */
1042 	if (xen_initial_domain() &&
1043 	    HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
1044 		xen_free_irq(irq);
1045 		irq = -ENOSPC;
1046 		goto out;
1047 	}
1048 
1049 	ret = xen_irq_info_pirq_setup(irq, 0, pirq, gsi, DOMID_SELF,
1050 			       shareable ? PIRQ_SHAREABLE : 0);
1051 	if (ret < 0) {
1052 		__unbind_from_irq(irq);
1053 		irq = ret;
1054 		goto out;
1055 	}
1056 
1057 	pirq_query_unmask(irq);
1058 	/* We try to use the handler with the appropriate semantic for the
1059 	 * type of interrupt: if the interrupt is an edge triggered
1060 	 * interrupt we use handle_edge_irq.
1061 	 *
1062 	 * On the other hand if the interrupt is level triggered we use
1063 	 * handle_fasteoi_irq like the native code does for this kind of
1064 	 * interrupts.
1065 	 *
1066 	 * Depending on the Xen version, pirq_needs_eoi might return true
1067 	 * not only for level triggered interrupts but for edge triggered
1068 	 * interrupts too. In any case Xen always honors the eoi mechanism,
1069 	 * not injecting any more pirqs of the same kind if the first one
1070 	 * hasn't received an eoi yet. Therefore using the fasteoi handler
1071 	 * is the right choice either way.
1072 	 */
1073 	if (shareable)
1074 		irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
1075 				handle_fasteoi_irq, name);
1076 	else
1077 		irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
1078 				handle_edge_irq, name);
1079 
1080 out:
1081 	mutex_unlock(&irq_mapping_update_lock);
1082 
1083 	return irq;
1084 }
1085 
1086 #ifdef CONFIG_PCI_MSI
1087 int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
1088 {
1089 	int rc;
1090 	struct physdev_get_free_pirq op_get_free_pirq;
1091 
1092 	op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
1093 	rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
1094 
1095 	WARN_ONCE(rc == -ENOSYS,
1096 		  "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
1097 
1098 	return rc ? -1 : op_get_free_pirq.pirq;
1099 }
1100 
1101 int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
1102 			     int pirq, int nvec, const char *name, domid_t domid)
1103 {
1104 	int i, irq, ret;
1105 
1106 	mutex_lock(&irq_mapping_update_lock);
1107 
1108 	irq = xen_allocate_irqs_dynamic(nvec);
1109 	if (irq < 0)
1110 		goto out;
1111 
1112 	for (i = 0; i < nvec; i++) {
1113 		irq_set_chip_and_handler_name(irq + i, &xen_pirq_chip, handle_edge_irq, name);
1114 
1115 		ret = xen_irq_info_pirq_setup(irq + i, 0, pirq + i, 0, domid,
1116 					      i == 0 ? 0 : PIRQ_MSI_GROUP);
1117 		if (ret < 0)
1118 			goto error_irq;
1119 	}
1120 
1121 	ret = irq_set_msi_desc(irq, msidesc);
1122 	if (ret < 0)
1123 		goto error_irq;
1124 out:
1125 	mutex_unlock(&irq_mapping_update_lock);
1126 	return irq;
1127 error_irq:
1128 	while (nvec--)
1129 		__unbind_from_irq(irq + nvec);
1130 	mutex_unlock(&irq_mapping_update_lock);
1131 	return ret;
1132 }
1133 #endif
1134 
1135 int xen_destroy_irq(int irq)
1136 {
1137 	struct physdev_unmap_pirq unmap_irq;
1138 	struct irq_info *info = info_for_irq(irq);
1139 	int rc = -ENOENT;
1140 
1141 	mutex_lock(&irq_mapping_update_lock);
1142 
1143 	/*
1144 	 * If trying to remove a vector in a MSI group different
1145 	 * than the first one skip the PIRQ unmap unless this vector
1146 	 * is the first one in the group.
1147 	 */
1148 	if (xen_initial_domain() && !(info->u.pirq.flags & PIRQ_MSI_GROUP)) {
1149 		unmap_irq.pirq = info->u.pirq.pirq;
1150 		unmap_irq.domid = info->u.pirq.domid;
1151 		rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
1152 		/* If another domain quits without making the pci_disable_msix
1153 		 * call, the Xen hypervisor takes care of freeing the PIRQs
1154 		 * (free_domain_pirqs).
1155 		 */
1156 		if ((rc == -ESRCH && info->u.pirq.domid != DOMID_SELF))
1157 			pr_info("domain %d does not have %d anymore\n",
1158 				info->u.pirq.domid, info->u.pirq.pirq);
1159 		else if (rc) {
1160 			pr_warn("unmap irq failed %d\n", rc);
1161 			goto out;
1162 		}
1163 	}
1164 
1165 	xen_free_irq(irq);
1166 
1167 out:
1168 	mutex_unlock(&irq_mapping_update_lock);
1169 	return rc;
1170 }
1171 
1172 int xen_irq_from_pirq(unsigned pirq)
1173 {
1174 	int irq;
1175 
1176 	struct irq_info *info;
1177 
1178 	mutex_lock(&irq_mapping_update_lock);
1179 
1180 	list_for_each_entry(info, &xen_irq_list_head, list) {
1181 		if (info->type != IRQT_PIRQ)
1182 			continue;
1183 		irq = info->irq;
1184 		if (info->u.pirq.pirq == pirq)
1185 			goto out;
1186 	}
1187 	irq = -1;
1188 out:
1189 	mutex_unlock(&irq_mapping_update_lock);
1190 
1191 	return irq;
1192 }
1193 
1194 
1195 int xen_pirq_from_irq(unsigned irq)
1196 {
1197 	return pirq_from_irq(irq);
1198 }
1199 EXPORT_SYMBOL_GPL(xen_pirq_from_irq);
1200 
1201 static int bind_evtchn_to_irq_chip(evtchn_port_t evtchn, struct irq_chip *chip,
1202 				   struct xenbus_device *dev)
1203 {
1204 	int irq;
1205 	int ret;
1206 
1207 	if (evtchn >= xen_evtchn_max_channels())
1208 		return -ENOMEM;
1209 
1210 	mutex_lock(&irq_mapping_update_lock);
1211 
1212 	irq = get_evtchn_to_irq(evtchn);
1213 
1214 	if (irq == -1) {
1215 		irq = xen_allocate_irq_dynamic();
1216 		if (irq < 0)
1217 			goto out;
1218 
1219 		irq_set_chip_and_handler_name(irq, chip,
1220 					      handle_edge_irq, "event");
1221 
1222 		ret = xen_irq_info_evtchn_setup(irq, evtchn, dev);
1223 		if (ret < 0) {
1224 			__unbind_from_irq(irq);
1225 			irq = ret;
1226 			goto out;
1227 		}
1228 		/*
1229 		 * New interdomain events are initially bound to vCPU0 This
1230 		 * is required to setup the event channel in the first
1231 		 * place and also important for UP guests because the
1232 		 * affinity setting is not invoked on them so nothing would
1233 		 * bind the channel.
1234 		 */
1235 		bind_evtchn_to_cpu(evtchn, 0, false);
1236 	} else {
1237 		struct irq_info *info = info_for_irq(irq);
1238 		WARN_ON(info == NULL || info->type != IRQT_EVTCHN);
1239 	}
1240 
1241 out:
1242 	mutex_unlock(&irq_mapping_update_lock);
1243 
1244 	return irq;
1245 }
1246 
1247 int bind_evtchn_to_irq(evtchn_port_t evtchn)
1248 {
1249 	return bind_evtchn_to_irq_chip(evtchn, &xen_dynamic_chip, NULL);
1250 }
1251 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
1252 
1253 int bind_evtchn_to_irq_lateeoi(evtchn_port_t evtchn)
1254 {
1255 	return bind_evtchn_to_irq_chip(evtchn, &xen_lateeoi_chip, NULL);
1256 }
1257 EXPORT_SYMBOL_GPL(bind_evtchn_to_irq_lateeoi);
1258 
1259 static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
1260 {
1261 	struct evtchn_bind_ipi bind_ipi;
1262 	evtchn_port_t evtchn;
1263 	int ret, irq;
1264 
1265 	mutex_lock(&irq_mapping_update_lock);
1266 
1267 	irq = per_cpu(ipi_to_irq, cpu)[ipi];
1268 
1269 	if (irq == -1) {
1270 		irq = xen_allocate_irq_dynamic();
1271 		if (irq < 0)
1272 			goto out;
1273 
1274 		irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
1275 					      handle_percpu_irq, "ipi");
1276 
1277 		bind_ipi.vcpu = xen_vcpu_nr(cpu);
1278 		if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1279 						&bind_ipi) != 0)
1280 			BUG();
1281 		evtchn = bind_ipi.port;
1282 
1283 		ret = xen_irq_info_ipi_setup(cpu, irq, evtchn, ipi);
1284 		if (ret < 0) {
1285 			__unbind_from_irq(irq);
1286 			irq = ret;
1287 			goto out;
1288 		}
1289 		/*
1290 		 * Force the affinity mask to the target CPU so proc shows
1291 		 * the correct target.
1292 		 */
1293 		bind_evtchn_to_cpu(evtchn, cpu, true);
1294 	} else {
1295 		struct irq_info *info = info_for_irq(irq);
1296 		WARN_ON(info == NULL || info->type != IRQT_IPI);
1297 	}
1298 
1299  out:
1300 	mutex_unlock(&irq_mapping_update_lock);
1301 	return irq;
1302 }
1303 
1304 static int bind_interdomain_evtchn_to_irq_chip(struct xenbus_device *dev,
1305 					       evtchn_port_t remote_port,
1306 					       struct irq_chip *chip)
1307 {
1308 	struct evtchn_bind_interdomain bind_interdomain;
1309 	int err;
1310 
1311 	bind_interdomain.remote_dom  = dev->otherend_id;
1312 	bind_interdomain.remote_port = remote_port;
1313 
1314 	err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
1315 					  &bind_interdomain);
1316 
1317 	return err ? : bind_evtchn_to_irq_chip(bind_interdomain.local_port,
1318 					       chip, dev);
1319 }
1320 
1321 int bind_interdomain_evtchn_to_irq_lateeoi(struct xenbus_device *dev,
1322 					   evtchn_port_t remote_port)
1323 {
1324 	return bind_interdomain_evtchn_to_irq_chip(dev, remote_port,
1325 						   &xen_lateeoi_chip);
1326 }
1327 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irq_lateeoi);
1328 
1329 static int find_virq(unsigned int virq, unsigned int cpu, evtchn_port_t *evtchn)
1330 {
1331 	struct evtchn_status status;
1332 	evtchn_port_t port;
1333 	int rc = -ENOENT;
1334 
1335 	memset(&status, 0, sizeof(status));
1336 	for (port = 0; port < xen_evtchn_max_channels(); port++) {
1337 		status.dom = DOMID_SELF;
1338 		status.port = port;
1339 		rc = HYPERVISOR_event_channel_op(EVTCHNOP_status, &status);
1340 		if (rc < 0)
1341 			continue;
1342 		if (status.status != EVTCHNSTAT_virq)
1343 			continue;
1344 		if (status.u.virq == virq && status.vcpu == xen_vcpu_nr(cpu)) {
1345 			*evtchn = port;
1346 			break;
1347 		}
1348 	}
1349 	return rc;
1350 }
1351 
1352 /**
1353  * xen_evtchn_nr_channels - number of usable event channel ports
1354  *
1355  * This may be less than the maximum supported by the current
1356  * hypervisor ABI. Use xen_evtchn_max_channels() for the maximum
1357  * supported.
1358  */
1359 unsigned xen_evtchn_nr_channels(void)
1360 {
1361         return evtchn_ops->nr_channels();
1362 }
1363 EXPORT_SYMBOL_GPL(xen_evtchn_nr_channels);
1364 
1365 int bind_virq_to_irq(unsigned int virq, unsigned int cpu, bool percpu)
1366 {
1367 	struct evtchn_bind_virq bind_virq;
1368 	evtchn_port_t evtchn = 0;
1369 	int irq, ret;
1370 
1371 	mutex_lock(&irq_mapping_update_lock);
1372 
1373 	irq = per_cpu(virq_to_irq, cpu)[virq];
1374 
1375 	if (irq == -1) {
1376 		irq = xen_allocate_irq_dynamic();
1377 		if (irq < 0)
1378 			goto out;
1379 
1380 		if (percpu)
1381 			irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
1382 						      handle_percpu_irq, "virq");
1383 		else
1384 			irq_set_chip_and_handler_name(irq, &xen_dynamic_chip,
1385 						      handle_edge_irq, "virq");
1386 
1387 		bind_virq.virq = virq;
1388 		bind_virq.vcpu = xen_vcpu_nr(cpu);
1389 		ret = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1390 						&bind_virq);
1391 		if (ret == 0)
1392 			evtchn = bind_virq.port;
1393 		else {
1394 			if (ret == -EEXIST)
1395 				ret = find_virq(virq, cpu, &evtchn);
1396 			BUG_ON(ret < 0);
1397 		}
1398 
1399 		ret = xen_irq_info_virq_setup(cpu, irq, evtchn, virq);
1400 		if (ret < 0) {
1401 			__unbind_from_irq(irq);
1402 			irq = ret;
1403 			goto out;
1404 		}
1405 
1406 		/*
1407 		 * Force the affinity mask for percpu interrupts so proc
1408 		 * shows the correct target.
1409 		 */
1410 		bind_evtchn_to_cpu(evtchn, cpu, percpu);
1411 	} else {
1412 		struct irq_info *info = info_for_irq(irq);
1413 		WARN_ON(info == NULL || info->type != IRQT_VIRQ);
1414 	}
1415 
1416 out:
1417 	mutex_unlock(&irq_mapping_update_lock);
1418 
1419 	return irq;
1420 }
1421 
1422 static void unbind_from_irq(unsigned int irq)
1423 {
1424 	mutex_lock(&irq_mapping_update_lock);
1425 	__unbind_from_irq(irq);
1426 	mutex_unlock(&irq_mapping_update_lock);
1427 }
1428 
1429 static int bind_evtchn_to_irqhandler_chip(evtchn_port_t evtchn,
1430 					  irq_handler_t handler,
1431 					  unsigned long irqflags,
1432 					  const char *devname, void *dev_id,
1433 					  struct irq_chip *chip)
1434 {
1435 	int irq, retval;
1436 
1437 	irq = bind_evtchn_to_irq_chip(evtchn, chip, NULL);
1438 	if (irq < 0)
1439 		return irq;
1440 	retval = request_irq(irq, handler, irqflags, devname, dev_id);
1441 	if (retval != 0) {
1442 		unbind_from_irq(irq);
1443 		return retval;
1444 	}
1445 
1446 	return irq;
1447 }
1448 
1449 int bind_evtchn_to_irqhandler(evtchn_port_t evtchn,
1450 			      irq_handler_t handler,
1451 			      unsigned long irqflags,
1452 			      const char *devname, void *dev_id)
1453 {
1454 	return bind_evtchn_to_irqhandler_chip(evtchn, handler, irqflags,
1455 					      devname, dev_id,
1456 					      &xen_dynamic_chip);
1457 }
1458 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
1459 
1460 int bind_evtchn_to_irqhandler_lateeoi(evtchn_port_t evtchn,
1461 				      irq_handler_t handler,
1462 				      unsigned long irqflags,
1463 				      const char *devname, void *dev_id)
1464 {
1465 	return bind_evtchn_to_irqhandler_chip(evtchn, handler, irqflags,
1466 					      devname, dev_id,
1467 					      &xen_lateeoi_chip);
1468 }
1469 EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler_lateeoi);
1470 
1471 static int bind_interdomain_evtchn_to_irqhandler_chip(
1472 		struct xenbus_device *dev, evtchn_port_t remote_port,
1473 		irq_handler_t handler, unsigned long irqflags,
1474 		const char *devname, void *dev_id, struct irq_chip *chip)
1475 {
1476 	int irq, retval;
1477 
1478 	irq = bind_interdomain_evtchn_to_irq_chip(dev, remote_port, chip);
1479 	if (irq < 0)
1480 		return irq;
1481 
1482 	retval = request_irq(irq, handler, irqflags, devname, dev_id);
1483 	if (retval != 0) {
1484 		unbind_from_irq(irq);
1485 		return retval;
1486 	}
1487 
1488 	return irq;
1489 }
1490 
1491 int bind_interdomain_evtchn_to_irqhandler_lateeoi(struct xenbus_device *dev,
1492 						  evtchn_port_t remote_port,
1493 						  irq_handler_t handler,
1494 						  unsigned long irqflags,
1495 						  const char *devname,
1496 						  void *dev_id)
1497 {
1498 	return bind_interdomain_evtchn_to_irqhandler_chip(dev,
1499 				remote_port, handler, irqflags, devname,
1500 				dev_id, &xen_lateeoi_chip);
1501 }
1502 EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler_lateeoi);
1503 
1504 int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
1505 			    irq_handler_t handler,
1506 			    unsigned long irqflags, const char *devname, void *dev_id)
1507 {
1508 	int irq, retval;
1509 
1510 	irq = bind_virq_to_irq(virq, cpu, irqflags & IRQF_PERCPU);
1511 	if (irq < 0)
1512 		return irq;
1513 	retval = request_irq(irq, handler, irqflags, devname, dev_id);
1514 	if (retval != 0) {
1515 		unbind_from_irq(irq);
1516 		return retval;
1517 	}
1518 
1519 	return irq;
1520 }
1521 EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
1522 
1523 int bind_ipi_to_irqhandler(enum ipi_vector ipi,
1524 			   unsigned int cpu,
1525 			   irq_handler_t handler,
1526 			   unsigned long irqflags,
1527 			   const char *devname,
1528 			   void *dev_id)
1529 {
1530 	int irq, retval;
1531 
1532 	irq = bind_ipi_to_irq(ipi, cpu);
1533 	if (irq < 0)
1534 		return irq;
1535 
1536 	irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME | IRQF_EARLY_RESUME;
1537 	retval = request_irq(irq, handler, irqflags, devname, dev_id);
1538 	if (retval != 0) {
1539 		unbind_from_irq(irq);
1540 		return retval;
1541 	}
1542 
1543 	return irq;
1544 }
1545 
1546 void unbind_from_irqhandler(unsigned int irq, void *dev_id)
1547 {
1548 	struct irq_info *info = info_for_irq(irq);
1549 
1550 	if (WARN_ON(!info))
1551 		return;
1552 	free_irq(irq, dev_id);
1553 	unbind_from_irq(irq);
1554 }
1555 EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
1556 
1557 /**
1558  * xen_set_irq_priority() - set an event channel priority.
1559  * @irq:irq bound to an event channel.
1560  * @priority: priority between XEN_IRQ_PRIORITY_MAX and XEN_IRQ_PRIORITY_MIN.
1561  */
1562 int xen_set_irq_priority(unsigned irq, unsigned priority)
1563 {
1564 	struct evtchn_set_priority set_priority;
1565 
1566 	set_priority.port = evtchn_from_irq(irq);
1567 	set_priority.priority = priority;
1568 
1569 	return HYPERVISOR_event_channel_op(EVTCHNOP_set_priority,
1570 					   &set_priority);
1571 }
1572 EXPORT_SYMBOL_GPL(xen_set_irq_priority);
1573 
1574 int evtchn_make_refcounted(evtchn_port_t evtchn, bool is_static)
1575 {
1576 	int irq = get_evtchn_to_irq(evtchn);
1577 	struct irq_info *info;
1578 
1579 	if (irq == -1)
1580 		return -ENOENT;
1581 
1582 	info = info_for_irq(irq);
1583 
1584 	if (!info)
1585 		return -ENOENT;
1586 
1587 	WARN_ON(info->refcnt != -1);
1588 
1589 	info->refcnt = 1;
1590 	info->is_static = is_static;
1591 
1592 	return 0;
1593 }
1594 EXPORT_SYMBOL_GPL(evtchn_make_refcounted);
1595 
1596 int evtchn_get(evtchn_port_t evtchn)
1597 {
1598 	int irq;
1599 	struct irq_info *info;
1600 	int err = -ENOENT;
1601 
1602 	if (evtchn >= xen_evtchn_max_channels())
1603 		return -EINVAL;
1604 
1605 	mutex_lock(&irq_mapping_update_lock);
1606 
1607 	irq = get_evtchn_to_irq(evtchn);
1608 	if (irq == -1)
1609 		goto done;
1610 
1611 	info = info_for_irq(irq);
1612 
1613 	if (!info)
1614 		goto done;
1615 
1616 	err = -EINVAL;
1617 	if (info->refcnt <= 0 || info->refcnt == SHRT_MAX)
1618 		goto done;
1619 
1620 	info->refcnt++;
1621 	err = 0;
1622  done:
1623 	mutex_unlock(&irq_mapping_update_lock);
1624 
1625 	return err;
1626 }
1627 EXPORT_SYMBOL_GPL(evtchn_get);
1628 
1629 void evtchn_put(evtchn_port_t evtchn)
1630 {
1631 	int irq = get_evtchn_to_irq(evtchn);
1632 	if (WARN_ON(irq == -1))
1633 		return;
1634 	unbind_from_irq(irq);
1635 }
1636 EXPORT_SYMBOL_GPL(evtchn_put);
1637 
1638 void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
1639 {
1640 	evtchn_port_t evtchn;
1641 
1642 #ifdef CONFIG_X86
1643 	if (unlikely(vector == XEN_NMI_VECTOR)) {
1644 		int rc =  HYPERVISOR_vcpu_op(VCPUOP_send_nmi, xen_vcpu_nr(cpu),
1645 					     NULL);
1646 		if (rc < 0)
1647 			printk(KERN_WARNING "Sending nmi to CPU%d failed (rc:%d)\n", cpu, rc);
1648 		return;
1649 	}
1650 #endif
1651 	evtchn = per_cpu(ipi_to_evtchn, cpu)[vector];
1652 	BUG_ON(evtchn == 0);
1653 	notify_remote_via_evtchn(evtchn);
1654 }
1655 
1656 struct evtchn_loop_ctrl {
1657 	ktime_t timeout;
1658 	unsigned count;
1659 	bool defer_eoi;
1660 };
1661 
1662 void handle_irq_for_port(evtchn_port_t port, struct evtchn_loop_ctrl *ctrl)
1663 {
1664 	int irq;
1665 	struct irq_info *info;
1666 	struct xenbus_device *dev;
1667 
1668 	irq = get_evtchn_to_irq(port);
1669 	if (irq == -1)
1670 		return;
1671 
1672 	/*
1673 	 * Check for timeout every 256 events.
1674 	 * We are setting the timeout value only after the first 256
1675 	 * events in order to not hurt the common case of few loop
1676 	 * iterations. The 256 is basically an arbitrary value.
1677 	 *
1678 	 * In case we are hitting the timeout we need to defer all further
1679 	 * EOIs in order to ensure to leave the event handling loop rather
1680 	 * sooner than later.
1681 	 */
1682 	if (!ctrl->defer_eoi && !(++ctrl->count & 0xff)) {
1683 		ktime_t kt = ktime_get();
1684 
1685 		if (!ctrl->timeout) {
1686 			kt = ktime_add_ms(kt,
1687 					  jiffies_to_msecs(event_loop_timeout));
1688 			ctrl->timeout = kt;
1689 		} else if (kt > ctrl->timeout) {
1690 			ctrl->defer_eoi = true;
1691 		}
1692 	}
1693 
1694 	info = info_for_irq(irq);
1695 	if (xchg_acquire(&info->is_active, 1))
1696 		return;
1697 
1698 	dev = (info->type == IRQT_EVTCHN) ? info->u.interdomain : NULL;
1699 	if (dev)
1700 		atomic_inc(&dev->events);
1701 
1702 	if (ctrl->defer_eoi) {
1703 		info->eoi_cpu = smp_processor_id();
1704 		info->irq_epoch = __this_cpu_read(irq_epoch);
1705 		info->eoi_time = get_jiffies_64() + event_eoi_delay;
1706 	}
1707 
1708 	generic_handle_irq(irq);
1709 }
1710 
1711 int xen_evtchn_do_upcall(void)
1712 {
1713 	struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
1714 	int ret = vcpu_info->evtchn_upcall_pending ? IRQ_HANDLED : IRQ_NONE;
1715 	int cpu = smp_processor_id();
1716 	struct evtchn_loop_ctrl ctrl = { 0 };
1717 
1718 	/*
1719 	 * When closing an event channel the associated IRQ must not be freed
1720 	 * until all cpus have left the event handling loop. This is ensured
1721 	 * by taking the rcu_read_lock() while handling events, as freeing of
1722 	 * the IRQ is handled via queue_rcu_work() _after_ closing the event
1723 	 * channel.
1724 	 */
1725 	rcu_read_lock();
1726 
1727 	do {
1728 		vcpu_info->evtchn_upcall_pending = 0;
1729 
1730 		xen_evtchn_handle_events(cpu, &ctrl);
1731 
1732 		BUG_ON(!irqs_disabled());
1733 
1734 		virt_rmb(); /* Hypervisor can set upcall pending. */
1735 
1736 	} while (vcpu_info->evtchn_upcall_pending);
1737 
1738 	rcu_read_unlock();
1739 
1740 	/*
1741 	 * Increment irq_epoch only now to defer EOIs only for
1742 	 * xen_irq_lateeoi() invocations occurring from inside the loop
1743 	 * above.
1744 	 */
1745 	__this_cpu_inc(irq_epoch);
1746 
1747 	return ret;
1748 }
1749 EXPORT_SYMBOL_GPL(xen_evtchn_do_upcall);
1750 
1751 /* Rebind a new event channel to an existing irq. */
1752 void rebind_evtchn_irq(evtchn_port_t evtchn, int irq)
1753 {
1754 	struct irq_info *info = info_for_irq(irq);
1755 
1756 	if (WARN_ON(!info))
1757 		return;
1758 
1759 	/* Make sure the irq is masked, since the new event channel
1760 	   will also be masked. */
1761 	disable_irq(irq);
1762 
1763 	mutex_lock(&irq_mapping_update_lock);
1764 
1765 	/* After resume the irq<->evtchn mappings are all cleared out */
1766 	BUG_ON(get_evtchn_to_irq(evtchn) != -1);
1767 	/* Expect irq to have been bound before,
1768 	   so there should be a proper type */
1769 	BUG_ON(info->type == IRQT_UNBOUND);
1770 
1771 	(void)xen_irq_info_evtchn_setup(irq, evtchn, NULL);
1772 
1773 	mutex_unlock(&irq_mapping_update_lock);
1774 
1775 	bind_evtchn_to_cpu(evtchn, info->cpu, false);
1776 
1777 	/* Unmask the event channel. */
1778 	enable_irq(irq);
1779 }
1780 
1781 /* Rebind an evtchn so that it gets delivered to a specific cpu */
1782 static int xen_rebind_evtchn_to_cpu(struct irq_info *info, unsigned int tcpu)
1783 {
1784 	struct evtchn_bind_vcpu bind_vcpu;
1785 	evtchn_port_t evtchn = info ? info->evtchn : 0;
1786 
1787 	if (!VALID_EVTCHN(evtchn))
1788 		return -1;
1789 
1790 	if (!xen_support_evtchn_rebind())
1791 		return -1;
1792 
1793 	/* Send future instances of this interrupt to other vcpu. */
1794 	bind_vcpu.port = evtchn;
1795 	bind_vcpu.vcpu = xen_vcpu_nr(tcpu);
1796 
1797 	/*
1798 	 * Mask the event while changing the VCPU binding to prevent
1799 	 * it being delivered on an unexpected VCPU.
1800 	 */
1801 	do_mask(info, EVT_MASK_REASON_TEMPORARY);
1802 
1803 	/*
1804 	 * If this fails, it usually just indicates that we're dealing with a
1805 	 * virq or IPI channel, which don't actually need to be rebound. Ignore
1806 	 * it, but don't do the xenlinux-level rebind in that case.
1807 	 */
1808 	if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1809 		bind_evtchn_to_cpu(evtchn, tcpu, false);
1810 
1811 	do_unmask(info, EVT_MASK_REASON_TEMPORARY);
1812 
1813 	return 0;
1814 }
1815 
1816 /*
1817  * Find the CPU within @dest mask which has the least number of channels
1818  * assigned. This is not precise as the per cpu counts can be modified
1819  * concurrently.
1820  */
1821 static unsigned int select_target_cpu(const struct cpumask *dest)
1822 {
1823 	unsigned int cpu, best_cpu = UINT_MAX, minch = UINT_MAX;
1824 
1825 	for_each_cpu_and(cpu, dest, cpu_online_mask) {
1826 		unsigned int curch = atomic_read(&channels_on_cpu[cpu]);
1827 
1828 		if (curch < minch) {
1829 			minch = curch;
1830 			best_cpu = cpu;
1831 		}
1832 	}
1833 
1834 	/*
1835 	 * Catch the unlikely case that dest contains no online CPUs. Can't
1836 	 * recurse.
1837 	 */
1838 	if (best_cpu == UINT_MAX)
1839 		return select_target_cpu(cpu_online_mask);
1840 
1841 	return best_cpu;
1842 }
1843 
1844 static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1845 			    bool force)
1846 {
1847 	unsigned int tcpu = select_target_cpu(dest);
1848 	int ret;
1849 
1850 	ret = xen_rebind_evtchn_to_cpu(info_for_irq(data->irq), tcpu);
1851 	if (!ret)
1852 		irq_data_update_effective_affinity(data, cpumask_of(tcpu));
1853 
1854 	return ret;
1855 }
1856 
1857 static void enable_dynirq(struct irq_data *data)
1858 {
1859 	struct irq_info *info = info_for_irq(data->irq);
1860 	evtchn_port_t evtchn = info ? info->evtchn : 0;
1861 
1862 	if (VALID_EVTCHN(evtchn))
1863 		do_unmask(info, EVT_MASK_REASON_EXPLICIT);
1864 }
1865 
1866 static void disable_dynirq(struct irq_data *data)
1867 {
1868 	struct irq_info *info = info_for_irq(data->irq);
1869 	evtchn_port_t evtchn = info ? info->evtchn : 0;
1870 
1871 	if (VALID_EVTCHN(evtchn))
1872 		do_mask(info, EVT_MASK_REASON_EXPLICIT);
1873 }
1874 
1875 static void ack_dynirq(struct irq_data *data)
1876 {
1877 	struct irq_info *info = info_for_irq(data->irq);
1878 	evtchn_port_t evtchn = info ? info->evtchn : 0;
1879 
1880 	if (VALID_EVTCHN(evtchn))
1881 		event_handler_exit(info);
1882 }
1883 
1884 static void mask_ack_dynirq(struct irq_data *data)
1885 {
1886 	disable_dynirq(data);
1887 	ack_dynirq(data);
1888 }
1889 
1890 static void lateeoi_ack_dynirq(struct irq_data *data)
1891 {
1892 	struct irq_info *info = info_for_irq(data->irq);
1893 	evtchn_port_t evtchn = info ? info->evtchn : 0;
1894 
1895 	if (VALID_EVTCHN(evtchn)) {
1896 		do_mask(info, EVT_MASK_REASON_EOI_PENDING);
1897 		/*
1898 		 * Don't call event_handler_exit().
1899 		 * Need to keep is_active non-zero in order to ignore re-raised
1900 		 * events after cpu affinity changes while a lateeoi is pending.
1901 		 */
1902 		clear_evtchn(evtchn);
1903 	}
1904 }
1905 
1906 static void lateeoi_mask_ack_dynirq(struct irq_data *data)
1907 {
1908 	struct irq_info *info = info_for_irq(data->irq);
1909 	evtchn_port_t evtchn = info ? info->evtchn : 0;
1910 
1911 	if (VALID_EVTCHN(evtchn)) {
1912 		do_mask(info, EVT_MASK_REASON_EXPLICIT);
1913 		event_handler_exit(info);
1914 	}
1915 }
1916 
1917 static int retrigger_dynirq(struct irq_data *data)
1918 {
1919 	struct irq_info *info = info_for_irq(data->irq);
1920 	evtchn_port_t evtchn = info ? info->evtchn : 0;
1921 
1922 	if (!VALID_EVTCHN(evtchn))
1923 		return 0;
1924 
1925 	do_mask(info, EVT_MASK_REASON_TEMPORARY);
1926 	set_evtchn(evtchn);
1927 	do_unmask(info, EVT_MASK_REASON_TEMPORARY);
1928 
1929 	return 1;
1930 }
1931 
1932 static void restore_pirqs(void)
1933 {
1934 	int pirq, rc, irq, gsi;
1935 	struct physdev_map_pirq map_irq;
1936 	struct irq_info *info;
1937 
1938 	list_for_each_entry(info, &xen_irq_list_head, list) {
1939 		if (info->type != IRQT_PIRQ)
1940 			continue;
1941 
1942 		pirq = info->u.pirq.pirq;
1943 		gsi = info->u.pirq.gsi;
1944 		irq = info->irq;
1945 
1946 		/* save/restore of PT devices doesn't work, so at this point the
1947 		 * only devices present are GSI based emulated devices */
1948 		if (!gsi)
1949 			continue;
1950 
1951 		map_irq.domid = DOMID_SELF;
1952 		map_irq.type = MAP_PIRQ_TYPE_GSI;
1953 		map_irq.index = gsi;
1954 		map_irq.pirq = pirq;
1955 
1956 		rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1957 		if (rc) {
1958 			pr_warn("xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1959 				gsi, irq, pirq, rc);
1960 			xen_free_irq(irq);
1961 			continue;
1962 		}
1963 
1964 		printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1965 
1966 		__startup_pirq(irq);
1967 	}
1968 }
1969 
1970 static void restore_cpu_virqs(unsigned int cpu)
1971 {
1972 	struct evtchn_bind_virq bind_virq;
1973 	evtchn_port_t evtchn;
1974 	int virq, irq;
1975 
1976 	for (virq = 0; virq < NR_VIRQS; virq++) {
1977 		if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1978 			continue;
1979 
1980 		BUG_ON(virq_from_irq(irq) != virq);
1981 
1982 		/* Get a new binding from Xen. */
1983 		bind_virq.virq = virq;
1984 		bind_virq.vcpu = xen_vcpu_nr(cpu);
1985 		if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1986 						&bind_virq) != 0)
1987 			BUG();
1988 		evtchn = bind_virq.port;
1989 
1990 		/* Record the new mapping. */
1991 		(void)xen_irq_info_virq_setup(cpu, irq, evtchn, virq);
1992 		/* The affinity mask is still valid */
1993 		bind_evtchn_to_cpu(evtchn, cpu, false);
1994 	}
1995 }
1996 
1997 static void restore_cpu_ipis(unsigned int cpu)
1998 {
1999 	struct evtchn_bind_ipi bind_ipi;
2000 	evtchn_port_t evtchn;
2001 	int ipi, irq;
2002 
2003 	for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
2004 		if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
2005 			continue;
2006 
2007 		BUG_ON(ipi_from_irq(irq) != ipi);
2008 
2009 		/* Get a new binding from Xen. */
2010 		bind_ipi.vcpu = xen_vcpu_nr(cpu);
2011 		if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
2012 						&bind_ipi) != 0)
2013 			BUG();
2014 		evtchn = bind_ipi.port;
2015 
2016 		/* Record the new mapping. */
2017 		(void)xen_irq_info_ipi_setup(cpu, irq, evtchn, ipi);
2018 		/* The affinity mask is still valid */
2019 		bind_evtchn_to_cpu(evtchn, cpu, false);
2020 	}
2021 }
2022 
2023 /* Clear an irq's pending state, in preparation for polling on it */
2024 void xen_clear_irq_pending(int irq)
2025 {
2026 	struct irq_info *info = info_for_irq(irq);
2027 	evtchn_port_t evtchn = info ? info->evtchn : 0;
2028 
2029 	if (VALID_EVTCHN(evtchn))
2030 		event_handler_exit(info);
2031 }
2032 EXPORT_SYMBOL(xen_clear_irq_pending);
2033 void xen_set_irq_pending(int irq)
2034 {
2035 	evtchn_port_t evtchn = evtchn_from_irq(irq);
2036 
2037 	if (VALID_EVTCHN(evtchn))
2038 		set_evtchn(evtchn);
2039 }
2040 
2041 bool xen_test_irq_pending(int irq)
2042 {
2043 	evtchn_port_t evtchn = evtchn_from_irq(irq);
2044 	bool ret = false;
2045 
2046 	if (VALID_EVTCHN(evtchn))
2047 		ret = test_evtchn(evtchn);
2048 
2049 	return ret;
2050 }
2051 
2052 /* Poll waiting for an irq to become pending with timeout.  In the usual case,
2053  * the irq will be disabled so it won't deliver an interrupt. */
2054 void xen_poll_irq_timeout(int irq, u64 timeout)
2055 {
2056 	evtchn_port_t evtchn = evtchn_from_irq(irq);
2057 
2058 	if (VALID_EVTCHN(evtchn)) {
2059 		struct sched_poll poll;
2060 
2061 		poll.nr_ports = 1;
2062 		poll.timeout = timeout;
2063 		set_xen_guest_handle(poll.ports, &evtchn);
2064 
2065 		if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
2066 			BUG();
2067 	}
2068 }
2069 EXPORT_SYMBOL(xen_poll_irq_timeout);
2070 /* Poll waiting for an irq to become pending.  In the usual case, the
2071  * irq will be disabled so it won't deliver an interrupt. */
2072 void xen_poll_irq(int irq)
2073 {
2074 	xen_poll_irq_timeout(irq, 0 /* no timeout */);
2075 }
2076 
2077 /* Check whether the IRQ line is shared with other guests. */
2078 int xen_test_irq_shared(int irq)
2079 {
2080 	struct irq_info *info = info_for_irq(irq);
2081 	struct physdev_irq_status_query irq_status;
2082 
2083 	if (WARN_ON(!info))
2084 		return -ENOENT;
2085 
2086 	irq_status.irq = info->u.pirq.pirq;
2087 
2088 	if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
2089 		return 0;
2090 	return !(irq_status.flags & XENIRQSTAT_shared);
2091 }
2092 EXPORT_SYMBOL_GPL(xen_test_irq_shared);
2093 
2094 void xen_irq_resume(void)
2095 {
2096 	unsigned int cpu;
2097 	struct irq_info *info;
2098 
2099 	/* New event-channel space is not 'live' yet. */
2100 	xen_evtchn_resume();
2101 
2102 	/* No IRQ <-> event-channel mappings. */
2103 	list_for_each_entry(info, &xen_irq_list_head, list) {
2104 		/* Zap event-channel binding */
2105 		info->evtchn = 0;
2106 		/* Adjust accounting */
2107 		channels_on_cpu_dec(info);
2108 	}
2109 
2110 	clear_evtchn_to_irq_all();
2111 
2112 	for_each_possible_cpu(cpu) {
2113 		restore_cpu_virqs(cpu);
2114 		restore_cpu_ipis(cpu);
2115 	}
2116 
2117 	restore_pirqs();
2118 }
2119 
2120 static struct irq_chip xen_dynamic_chip __read_mostly = {
2121 	.name			= "xen-dyn",
2122 
2123 	.irq_disable		= disable_dynirq,
2124 	.irq_mask		= disable_dynirq,
2125 	.irq_unmask		= enable_dynirq,
2126 
2127 	.irq_ack		= ack_dynirq,
2128 	.irq_mask_ack		= mask_ack_dynirq,
2129 
2130 	.irq_set_affinity	= set_affinity_irq,
2131 	.irq_retrigger		= retrigger_dynirq,
2132 };
2133 
2134 static struct irq_chip xen_lateeoi_chip __read_mostly = {
2135 	/* The chip name needs to contain "xen-dyn" for irqbalance to work. */
2136 	.name			= "xen-dyn-lateeoi",
2137 
2138 	.irq_disable		= disable_dynirq,
2139 	.irq_mask		= disable_dynirq,
2140 	.irq_unmask		= enable_dynirq,
2141 
2142 	.irq_ack		= lateeoi_ack_dynirq,
2143 	.irq_mask_ack		= lateeoi_mask_ack_dynirq,
2144 
2145 	.irq_set_affinity	= set_affinity_irq,
2146 	.irq_retrigger		= retrigger_dynirq,
2147 };
2148 
2149 static struct irq_chip xen_pirq_chip __read_mostly = {
2150 	.name			= "xen-pirq",
2151 
2152 	.irq_startup		= startup_pirq,
2153 	.irq_shutdown		= shutdown_pirq,
2154 	.irq_enable		= enable_pirq,
2155 	.irq_disable		= disable_pirq,
2156 
2157 	.irq_mask		= disable_dynirq,
2158 	.irq_unmask		= enable_dynirq,
2159 
2160 	.irq_ack		= eoi_pirq,
2161 	.irq_eoi		= eoi_pirq,
2162 	.irq_mask_ack		= mask_ack_pirq,
2163 
2164 	.irq_set_affinity	= set_affinity_irq,
2165 
2166 	.irq_retrigger		= retrigger_dynirq,
2167 };
2168 
2169 static struct irq_chip xen_percpu_chip __read_mostly = {
2170 	.name			= "xen-percpu",
2171 
2172 	.irq_disable		= disable_dynirq,
2173 	.irq_mask		= disable_dynirq,
2174 	.irq_unmask		= enable_dynirq,
2175 
2176 	.irq_ack		= ack_dynirq,
2177 };
2178 
2179 #ifdef CONFIG_X86
2180 #ifdef CONFIG_XEN_PVHVM
2181 /* Vector callbacks are better than PCI interrupts to receive event
2182  * channel notifications because we can receive vector callbacks on any
2183  * vcpu and we don't need PCI support or APIC interactions. */
2184 void xen_setup_callback_vector(void)
2185 {
2186 	uint64_t callback_via;
2187 
2188 	if (xen_have_vector_callback) {
2189 		callback_via = HVM_CALLBACK_VECTOR(HYPERVISOR_CALLBACK_VECTOR);
2190 		if (xen_set_callback_via(callback_via)) {
2191 			pr_err("Request for Xen HVM callback vector failed\n");
2192 			xen_have_vector_callback = false;
2193 		}
2194 	}
2195 }
2196 
2197 /*
2198  * Setup per-vCPU vector-type callbacks. If this setup is unavailable,
2199  * fallback to the global vector-type callback.
2200  */
2201 static __init void xen_init_setup_upcall_vector(void)
2202 {
2203 	if (!xen_have_vector_callback)
2204 		return;
2205 
2206 	if ((cpuid_eax(xen_cpuid_base() + 4) & XEN_HVM_CPUID_UPCALL_VECTOR) &&
2207 	    !xen_set_upcall_vector(0))
2208 		xen_percpu_upcall = true;
2209 	else if (xen_feature(XENFEAT_hvm_callback_vector))
2210 		xen_setup_callback_vector();
2211 	else
2212 		xen_have_vector_callback = false;
2213 }
2214 
2215 int xen_set_upcall_vector(unsigned int cpu)
2216 {
2217 	int rc;
2218 	xen_hvm_evtchn_upcall_vector_t op = {
2219 		.vector = HYPERVISOR_CALLBACK_VECTOR,
2220 		.vcpu = per_cpu(xen_vcpu_id, cpu),
2221 	};
2222 
2223 	rc = HYPERVISOR_hvm_op(HVMOP_set_evtchn_upcall_vector, &op);
2224 	if (rc)
2225 		return rc;
2226 
2227 	/* Trick toolstack to think we are enlightened. */
2228 	if (!cpu)
2229 		rc = xen_set_callback_via(1);
2230 
2231 	return rc;
2232 }
2233 
2234 static __init void xen_alloc_callback_vector(void)
2235 {
2236 	if (!xen_have_vector_callback)
2237 		return;
2238 
2239 	pr_info("Xen HVM callback vector for event delivery is enabled\n");
2240 	alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR, asm_sysvec_xen_hvm_callback);
2241 }
2242 #else
2243 void xen_setup_callback_vector(void) {}
2244 static inline void xen_init_setup_upcall_vector(void) {}
2245 int xen_set_upcall_vector(unsigned int cpu) {}
2246 static inline void xen_alloc_callback_vector(void) {}
2247 #endif /* CONFIG_XEN_PVHVM */
2248 #endif /* CONFIG_X86 */
2249 
2250 bool xen_fifo_events = true;
2251 module_param_named(fifo_events, xen_fifo_events, bool, 0);
2252 
2253 static int xen_evtchn_cpu_prepare(unsigned int cpu)
2254 {
2255 	int ret = 0;
2256 
2257 	xen_cpu_init_eoi(cpu);
2258 
2259 	if (evtchn_ops->percpu_init)
2260 		ret = evtchn_ops->percpu_init(cpu);
2261 
2262 	return ret;
2263 }
2264 
2265 static int xen_evtchn_cpu_dead(unsigned int cpu)
2266 {
2267 	int ret = 0;
2268 
2269 	if (evtchn_ops->percpu_deinit)
2270 		ret = evtchn_ops->percpu_deinit(cpu);
2271 
2272 	return ret;
2273 }
2274 
2275 void __init xen_init_IRQ(void)
2276 {
2277 	int ret = -EINVAL;
2278 	evtchn_port_t evtchn;
2279 
2280 	if (xen_fifo_events)
2281 		ret = xen_evtchn_fifo_init();
2282 	if (ret < 0) {
2283 		xen_evtchn_2l_init();
2284 		xen_fifo_events = false;
2285 	}
2286 
2287 	xen_cpu_init_eoi(smp_processor_id());
2288 
2289 	cpuhp_setup_state_nocalls(CPUHP_XEN_EVTCHN_PREPARE,
2290 				  "xen/evtchn:prepare",
2291 				  xen_evtchn_cpu_prepare, xen_evtchn_cpu_dead);
2292 
2293 	evtchn_to_irq = kcalloc(EVTCHN_ROW(xen_evtchn_max_channels()),
2294 				sizeof(*evtchn_to_irq), GFP_KERNEL);
2295 	BUG_ON(!evtchn_to_irq);
2296 
2297 	/* No event channels are 'live' right now. */
2298 	for (evtchn = 0; evtchn < xen_evtchn_nr_channels(); evtchn++)
2299 		mask_evtchn(evtchn);
2300 
2301 	pirq_needs_eoi = pirq_needs_eoi_flag;
2302 
2303 #ifdef CONFIG_X86
2304 	if (xen_pv_domain()) {
2305 		if (xen_initial_domain())
2306 			pci_xen_initial_domain();
2307 	}
2308 	xen_init_setup_upcall_vector();
2309 	xen_alloc_callback_vector();
2310 
2311 
2312 	if (xen_hvm_domain()) {
2313 		native_init_IRQ();
2314 		/* pci_xen_hvm_init must be called after native_init_IRQ so that
2315 		 * __acpi_register_gsi can point at the right function */
2316 		pci_xen_hvm_init();
2317 	} else {
2318 		int rc;
2319 		struct physdev_pirq_eoi_gmfn eoi_gmfn;
2320 
2321 		pirq_eoi_map = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
2322 		eoi_gmfn.gmfn = virt_to_gfn(pirq_eoi_map);
2323 		rc = HYPERVISOR_physdev_op(PHYSDEVOP_pirq_eoi_gmfn_v2, &eoi_gmfn);
2324 		if (rc != 0) {
2325 			free_page((unsigned long) pirq_eoi_map);
2326 			pirq_eoi_map = NULL;
2327 		} else
2328 			pirq_needs_eoi = pirq_check_eoi_map;
2329 	}
2330 #endif
2331 }
2332