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