blob: 8771b740e30fce82add0f8fbf9a85cbb874eed01 [file] [log] [blame]
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001/*
2 * Xen event channels
3 *
4 * Xen models interrupts with abstract event channels. Because each
5 * domain gets 1024 event channels, but NR_IRQ is not that large, we
6 * must dynamically map irqs<->event channels. The event channels
7 * interface with the rest of the kernel by defining a xen interrupt
Lucas De Marchi25985ed2011-03-30 22:57:33 -03008 * chip. When an event is received, it is mapped to an irq and sent
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07009 * through the normal interrupt processing path.
10 *
11 * There are four kinds of events which can be mapped to an event
12 * channel:
13 *
14 * 1. Inter-domain notifications. This includes all the virtual
15 * device events, since they're driven by front-ends in another domain
16 * (typically dom0).
17 * 2. VIRQs, typically used for timers. These are per-cpu events.
18 * 3. IPIs.
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -040019 * 4. PIRQs - Hardware interrupts.
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070020 *
21 * Jeremy Fitzhardinge <jeremy@xensource.com>, XenSource Inc, 2007
22 */
23
Joe Perches283c0972013-06-28 03:21:41 -070024#define pr_fmt(fmt) "xen:" KBUILD_MODNAME ": " fmt
25
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070026#include <linux/linkage.h>
27#include <linux/interrupt.h>
28#include <linux/irq.h>
29#include <linux/module.h>
30#include <linux/string.h>
Christophe Saout28e08862009-01-11 11:46:23 -080031#include <linux/bootmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -040033#include <linux/irqnr.h>
Qing Hef731e3ef2010-10-11 15:30:09 +010034#include <linux/pci.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070035
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +000036#ifdef CONFIG_X86
Sheng Yang38e20b02010-05-14 12:40:51 +010037#include <asm/desc.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070038#include <asm/ptrace.h>
39#include <asm/irq.h>
Jeremy Fitzhardinge792dc4f2009-02-06 14:09:43 -080040#include <asm/idle.h>
Konrad Rzeszutek Wilk0794bfc2010-10-18 10:41:08 -040041#include <asm/io_apic.h>
Stefano Stabellini9846ff12012-01-30 16:21:48 +000042#include <asm/xen/page.h>
Stefano Stabellini42a1de52010-06-24 16:42:04 +010043#include <asm/xen/pci.h>
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +000044#endif
45#include <asm/sync_bitops.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070046#include <asm/xen/hypercall.h>
Adrian Bunk8d1b8752007-07-20 00:31:44 -070047#include <asm/xen/hypervisor.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070048
Sheng Yang38e20b02010-05-14 12:40:51 +010049#include <xen/xen.h>
50#include <xen/hvm.h>
Isaku Yamahatae04d0d02008-04-02 10:53:55 -070051#include <xen/xen-ops.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070052#include <xen/events.h>
53#include <xen/interface/xen.h>
54#include <xen/interface/event_channel.h>
Sheng Yang38e20b02010-05-14 12:40:51 +010055#include <xen/interface/hvm/hvm_op.h>
56#include <xen/interface/hvm/params.h>
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +000057#include <xen/interface/physdev.h>
58#include <xen/interface/sched.h>
Konrad Rzeszutek Wilk6efa20e2013-07-19 11:51:31 -040059#include <xen/interface/vcpu.h>
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +000060#include <asm/hw_irq.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070061
David Vrabel9a489f42013-03-13 15:29:25 +000062#include "events_internal.h"
63
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070064/*
65 * This lock protects updates to the following mapping and reference-count
66 * arrays. The lock does not need to be acquired to read the mapping tables.
67 */
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -040068static DEFINE_MUTEX(irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070069
Ian Campbell6cb65372011-03-10 16:08:11 +000070static LIST_HEAD(xen_irq_list_head);
71
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070072/* IRQ <-> VIRQ mapping. */
Tejun Heo204fba42009-06-24 15:13:45 +090073static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070074
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070075/* IRQ <-> IPI mapping */
Tejun Heo204fba42009-06-24 15:13:45 +090076static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070077
David Vrabel9a489f42013-03-13 15:29:25 +000078int *evtchn_to_irq;
Ian Campbellbf86ad82012-10-17 09:39:12 +010079#ifdef CONFIG_X86
Stefano Stabellini9846ff12012-01-30 16:21:48 +000080static unsigned long *pirq_eoi_map;
Ian Campbellbf86ad82012-10-17 09:39:12 +010081#endif
Stefano Stabellini9846ff12012-01-30 16:21:48 +000082static bool (*pirq_needs_eoi)(unsigned irq);
Jeremy Fitzhardinge3b32f572009-08-13 12:50:37 -070083
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070084/* Xen will never allocate port zero for any purpose. */
85#define VALID_EVTCHN(chn) ((chn) != 0)
86
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070087static struct irq_chip xen_dynamic_chip;
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -070088static struct irq_chip xen_percpu_chip;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -040089static struct irq_chip xen_pirq_chip;
Stefano Stabellini7e186bd2011-05-06 12:27:50 +010090static void enable_dynirq(struct irq_data *data);
91static void disable_dynirq(struct irq_data *data);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070092
Ian Campbell9158c352011-03-10 16:08:09 +000093/* Get info for IRQ */
David Vrabel9a489f42013-03-13 15:29:25 +000094struct irq_info *info_for_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070095{
Thomas Gleixnerc442b802011-03-25 10:58:06 +010096 return irq_get_handler_data(irq);
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080097}
98
Ian Campbell9158c352011-03-10 16:08:09 +000099/* Constructors for packed IRQ information. */
100static void xen_irq_info_common_init(struct irq_info *info,
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000101 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000102 enum xen_irq_type type,
103 unsigned short evtchn,
104 unsigned short cpu)
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800105{
Ian Campbell9158c352011-03-10 16:08:09 +0000106
107 BUG_ON(info->type != IRQT_UNBOUND && info->type != type);
108
109 info->type = type;
Ian Campbell6cb65372011-03-10 16:08:11 +0000110 info->irq = irq;
Ian Campbell9158c352011-03-10 16:08:09 +0000111 info->evtchn = evtchn;
112 info->cpu = cpu;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000113
114 evtchn_to_irq[evtchn] = irq;
Julien Grall934f5852013-04-30 18:29:13 +0100115
116 irq_clear_status_flags(irq, IRQ_NOREQUEST|IRQ_NOAUTOEN);
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800117}
118
Ian Campbell9158c352011-03-10 16:08:09 +0000119static void xen_irq_info_evtchn_init(unsigned irq,
120 unsigned short evtchn)
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800121{
Ian Campbell9158c352011-03-10 16:08:09 +0000122 struct irq_info *info = info_for_irq(irq);
123
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000124 xen_irq_info_common_init(info, irq, IRQT_EVTCHN, evtchn, 0);
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800125}
126
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000127static void xen_irq_info_ipi_init(unsigned cpu,
128 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000129 unsigned short evtchn,
130 enum ipi_vector ipi)
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800131{
Ian Campbell9158c352011-03-10 16:08:09 +0000132 struct irq_info *info = info_for_irq(irq);
133
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000134 xen_irq_info_common_init(info, irq, IRQT_IPI, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000135
136 info->u.ipi = ipi;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000137
138 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800139}
140
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000141static void xen_irq_info_virq_init(unsigned cpu,
142 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000143 unsigned short evtchn,
144 unsigned short virq)
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800145{
Ian Campbell9158c352011-03-10 16:08:09 +0000146 struct irq_info *info = info_for_irq(irq);
147
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000148 xen_irq_info_common_init(info, irq, IRQT_VIRQ, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000149
150 info->u.virq = virq;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000151
152 per_cpu(virq_to_irq, cpu)[virq] = irq;
Ian Campbell9158c352011-03-10 16:08:09 +0000153}
154
155static void xen_irq_info_pirq_init(unsigned irq,
156 unsigned short evtchn,
157 unsigned short pirq,
158 unsigned short gsi,
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400159 uint16_t domid,
Ian Campbell9158c352011-03-10 16:08:09 +0000160 unsigned char flags)
161{
162 struct irq_info *info = info_for_irq(irq);
163
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000164 xen_irq_info_common_init(info, irq, IRQT_PIRQ, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000165
166 info->u.pirq.pirq = pirq;
167 info->u.pirq.gsi = gsi;
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400168 info->u.pirq.domid = domid;
Ian Campbell9158c352011-03-10 16:08:09 +0000169 info->u.pirq.flags = flags;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700170}
171
172/*
173 * Accessors for packed IRQ information.
174 */
David Vrabel9a489f42013-03-13 15:29:25 +0000175unsigned int evtchn_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700176{
Joe Jin110e7c72011-01-07 14:50:12 +0800177 if (unlikely(WARN(irq < 0 || irq >= nr_irqs, "Invalid irq %d!\n", irq)))
178 return 0;
179
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800180 return info_for_irq(irq)->evtchn;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700181}
182
Ian Campbelld4c04532009-02-06 19:20:31 -0800183unsigned irq_from_evtchn(unsigned int evtchn)
184{
185 return evtchn_to_irq[evtchn];
186}
187EXPORT_SYMBOL_GPL(irq_from_evtchn);
188
David Vrabel9a489f42013-03-13 15:29:25 +0000189int irq_from_virq(unsigned int cpu, unsigned int virq)
190{
191 return per_cpu(virq_to_irq, cpu)[virq];
192}
193
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800194static enum ipi_vector ipi_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700195{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800196 struct irq_info *info = info_for_irq(irq);
197
198 BUG_ON(info == NULL);
199 BUG_ON(info->type != IRQT_IPI);
200
201 return info->u.ipi;
202}
203
204static unsigned virq_from_irq(unsigned irq)
205{
206 struct irq_info *info = info_for_irq(irq);
207
208 BUG_ON(info == NULL);
209 BUG_ON(info->type != IRQT_VIRQ);
210
211 return info->u.virq;
212}
213
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100214static unsigned pirq_from_irq(unsigned irq)
215{
216 struct irq_info *info = info_for_irq(irq);
217
218 BUG_ON(info == NULL);
219 BUG_ON(info->type != IRQT_PIRQ);
220
221 return info->u.pirq.pirq;
222}
223
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800224static enum xen_irq_type type_from_irq(unsigned irq)
225{
226 return info_for_irq(irq)->type;
227}
228
David Vrabel9a489f42013-03-13 15:29:25 +0000229unsigned cpu_from_irq(unsigned irq)
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800230{
231 return info_for_irq(irq)->cpu;
232}
233
David Vrabel9a489f42013-03-13 15:29:25 +0000234unsigned int cpu_from_evtchn(unsigned int evtchn)
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800235{
236 int irq = evtchn_to_irq[evtchn];
237 unsigned ret = 0;
238
239 if (irq != -1)
240 ret = cpu_from_irq(irq);
241
242 return ret;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700243}
244
Ian Campbellbf86ad82012-10-17 09:39:12 +0100245#ifdef CONFIG_X86
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000246static bool pirq_check_eoi_map(unsigned irq)
247{
Stefano Stabellini521394e2012-04-25 16:11:38 +0100248 return test_bit(pirq_from_irq(irq), pirq_eoi_map);
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000249}
Ian Campbellbf86ad82012-10-17 09:39:12 +0100250#endif
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000251
252static bool pirq_needs_eoi_flag(unsigned irq)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400253{
254 struct irq_info *info = info_for_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400255 BUG_ON(info->type != IRQT_PIRQ);
256
257 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
258}
259
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700260static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
261{
262 int irq = evtchn_to_irq[chn];
David Vrabel9a489f42013-03-13 15:29:25 +0000263 struct irq_info *info = info_for_irq(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700264
265 BUG_ON(irq == -1);
266#ifdef CONFIG_SMP
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000267 cpumask_copy(irq_to_desc(irq)->irq_data.affinity, cpumask_of(cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700268#endif
269
David Vrabel9a489f42013-03-13 15:29:25 +0000270 xen_evtchn_port_bind_to_cpu(info, cpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700271
David Vrabel9a489f42013-03-13 15:29:25 +0000272 info->cpu = cpu;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700273}
274
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700275/**
276 * notify_remote_via_irq - send event to remote end of event channel via irq
277 * @irq: irq of event channel to send event to
278 *
279 * Unlike notify_remote_via_evtchn(), this is safe to use across
280 * save/restore. Notifications on a broken connection are silently
281 * dropped.
282 */
283void notify_remote_via_irq(int irq)
284{
285 int evtchn = evtchn_from_irq(irq);
286
287 if (VALID_EVTCHN(evtchn))
288 notify_remote_via_evtchn(evtchn);
289}
290EXPORT_SYMBOL_GPL(notify_remote_via_irq);
291
Ian Campbell6cb65372011-03-10 16:08:11 +0000292static void xen_irq_init(unsigned irq)
293{
294 struct irq_info *info;
Konrad Rzeszutek Wilkb5328cd2011-06-15 14:24:29 -0400295#ifdef CONFIG_SMP
Ian Campbell6cb65372011-03-10 16:08:11 +0000296 struct irq_desc *desc = irq_to_desc(irq);
297
298 /* By default all event channels notify CPU#0. */
299 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
Konrad Rzeszutek Wilk44626e42011-03-15 16:40:33 -0400300#endif
Ian Campbell6cb65372011-03-10 16:08:11 +0000301
Ian Campbellca62ce82011-03-10 16:08:12 +0000302 info = kzalloc(sizeof(*info), GFP_KERNEL);
303 if (info == NULL)
304 panic("Unable to allocate metadata for IRQ%d\n", irq);
Ian Campbell6cb65372011-03-10 16:08:11 +0000305
306 info->type = IRQT_UNBOUND;
Daniel De Graaf420eb552011-10-27 17:58:47 -0400307 info->refcnt = -1;
Ian Campbell6cb65372011-03-10 16:08:11 +0000308
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100309 irq_set_handler_data(irq, info);
Ian Campbellca62ce82011-03-10 16:08:12 +0000310
Ian Campbell6cb65372011-03-10 16:08:11 +0000311 list_add_tail(&info->list, &xen_irq_list_head);
312}
313
Ian Campbell7bee9762011-03-10 16:08:15 +0000314static int __must_check xen_allocate_irq_dynamic(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700315{
Ian Campbell89911502011-03-03 11:57:44 -0500316 int first = 0;
317 int irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700318
Ian Campbell89911502011-03-03 11:57:44 -0500319#ifdef CONFIG_X86_IO_APIC
320 /*
321 * For an HVM guest or domain 0 which see "real" (emulated or
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300322 * actual respectively) GSIs we allocate dynamic IRQs
Ian Campbell89911502011-03-03 11:57:44 -0500323 * e.g. those corresponding to event channels or MSIs
324 * etc. from the range above those "real" GSIs to avoid
325 * collisions.
Konrad Rzeszutek Wilkd1b758e2010-12-09 14:53:29 -0500326 */
Ian Campbell89911502011-03-03 11:57:44 -0500327 if (xen_initial_domain() || xen_hvm_domain())
328 first = get_nr_irqs_gsi();
329#endif
330
Ian Campbell89911502011-03-03 11:57:44 -0500331 irq = irq_alloc_desc_from(first, -1);
332
Konrad Rzeszutek Wilke6599222011-09-29 13:26:45 -0400333 if (irq >= 0)
334 xen_irq_init(irq);
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800335
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700336 return irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400337}
338
Ian Campbell7bee9762011-03-10 16:08:15 +0000339static int __must_check xen_allocate_irq_gsi(unsigned gsi)
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000340{
341 int irq;
342
Ian Campbell89911502011-03-03 11:57:44 -0500343 /*
344 * A PV guest has no concept of a GSI (since it has no ACPI
345 * nor access to/knowledge of the physical APICs). Therefore
346 * all IRQs are dynamically allocated from the entire IRQ
347 * space.
348 */
349 if (xen_pv_domain() && !xen_initial_domain())
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000350 return xen_allocate_irq_dynamic();
351
352 /* Legacy IRQ descriptors are already allocated by the arch. */
353 if (gsi < NR_IRQS_LEGACY)
Ian Campbell6cb65372011-03-10 16:08:11 +0000354 irq = gsi;
355 else
356 irq = irq_alloc_desc_at(gsi, -1);
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000357
Ian Campbell6cb65372011-03-10 16:08:11 +0000358 xen_irq_init(irq);
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000359
360 return irq;
361}
362
363static void xen_free_irq(unsigned irq)
364{
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100365 struct irq_info *info = irq_get_handler_data(irq);
Ian Campbell6cb65372011-03-10 16:08:11 +0000366
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -0400367 if (WARN_ON(!info))
368 return;
369
Ian Campbell6cb65372011-03-10 16:08:11 +0000370 list_del(&info->list);
Ian Campbell9158c352011-03-10 16:08:09 +0000371
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100372 irq_set_handler_data(irq, NULL);
Ian Campbellca62ce82011-03-10 16:08:12 +0000373
Daniel De Graaf420eb552011-10-27 17:58:47 -0400374 WARN_ON(info->refcnt > 0);
375
Ian Campbellca62ce82011-03-10 16:08:12 +0000376 kfree(info);
377
Ian Campbell72146102011-02-03 09:49:35 +0000378 /* Legacy IRQ descriptors are managed by the arch. */
379 if (irq < NR_IRQS_LEGACY)
380 return;
381
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000382 irq_free_desc(irq);
383}
384
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400385static void pirq_query_unmask(int irq)
386{
387 struct physdev_irq_status_query irq_status;
388 struct irq_info *info = info_for_irq(irq);
389
390 BUG_ON(info->type != IRQT_PIRQ);
391
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100392 irq_status.irq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400393 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
394 irq_status.flags = 0;
395
396 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
397 if (irq_status.flags & XENIRQSTAT_needs_eoi)
398 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
399}
400
401static bool probing_irq(int irq)
402{
403 struct irq_desc *desc = irq_to_desc(irq);
404
405 return desc && desc->action == NULL;
406}
407
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100408static void eoi_pirq(struct irq_data *data)
409{
410 int evtchn = evtchn_from_irq(data->irq);
411 struct physdev_eoi eoi = { .irq = pirq_from_irq(data->irq) };
412 int rc = 0;
413
414 irq_move_irq(data);
415
416 if (VALID_EVTCHN(evtchn))
417 clear_evtchn(evtchn);
418
419 if (pirq_needs_eoi(data->irq)) {
420 rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
421 WARN_ON(rc);
422 }
423}
424
425static void mask_ack_pirq(struct irq_data *data)
426{
427 disable_dynirq(data);
428 eoi_pirq(data);
429}
430
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000431static unsigned int __startup_pirq(unsigned int irq)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400432{
433 struct evtchn_bind_pirq bind_pirq;
434 struct irq_info *info = info_for_irq(irq);
435 int evtchn = evtchn_from_irq(irq);
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400436 int rc;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400437
438 BUG_ON(info->type != IRQT_PIRQ);
439
440 if (VALID_EVTCHN(evtchn))
441 goto out;
442
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100443 bind_pirq.pirq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400444 /* NB. We are happy to share unless we are probing. */
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400445 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
446 BIND_PIRQ__WILL_SHARE : 0;
447 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
448 if (rc != 0) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400449 if (!probing_irq(irq))
Joe Perches283c0972013-06-28 03:21:41 -0700450 pr_info("Failed to obtain physical IRQ %d\n", irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400451 return 0;
452 }
453 evtchn = bind_pirq.port;
454
455 pirq_query_unmask(irq);
456
457 evtchn_to_irq[evtchn] = irq;
458 bind_evtchn_to_cpu(evtchn, 0);
459 info->evtchn = evtchn;
460
461out:
462 unmask_evtchn(evtchn);
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100463 eoi_pirq(irq_get_irq_data(irq));
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400464
465 return 0;
466}
467
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000468static unsigned int startup_pirq(struct irq_data *data)
469{
470 return __startup_pirq(data->irq);
471}
472
473static void shutdown_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400474{
475 struct evtchn_close close;
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000476 unsigned int irq = data->irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400477 struct irq_info *info = info_for_irq(irq);
478 int evtchn = evtchn_from_irq(irq);
479
480 BUG_ON(info->type != IRQT_PIRQ);
481
482 if (!VALID_EVTCHN(evtchn))
483 return;
484
485 mask_evtchn(evtchn);
486
487 close.port = evtchn;
488 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
489 BUG();
490
491 bind_evtchn_to_cpu(evtchn, 0);
492 evtchn_to_irq[evtchn] = -1;
493 info->evtchn = 0;
494}
495
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000496static void enable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400497{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000498 startup_pirq(data);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400499}
500
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000501static void disable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400502{
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100503 disable_dynirq(data);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400504}
505
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100506int xen_irq_from_gsi(unsigned gsi)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400507{
Ian Campbell6cb65372011-03-10 16:08:11 +0000508 struct irq_info *info;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400509
Ian Campbell6cb65372011-03-10 16:08:11 +0000510 list_for_each_entry(info, &xen_irq_list_head, list) {
511 if (info->type != IRQT_PIRQ)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400512 continue;
513
Ian Campbell6cb65372011-03-10 16:08:11 +0000514 if (info->u.pirq.gsi == gsi)
515 return info->irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400516 }
517
518 return -1;
519}
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100520EXPORT_SYMBOL_GPL(xen_irq_from_gsi);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400521
Ian Campbell653378a2011-03-10 16:08:04 +0000522/*
523 * Do not make any assumptions regarding the relationship between the
524 * IRQ number returned here and the Xen pirq argument.
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100525 *
526 * Note: We don't assign an event channel until the irq actually started
527 * up. Return an existing irq if we've already got one for the gsi.
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100528 *
529 * Shareable implies level triggered, not shareable implies edge
530 * triggered here.
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400531 */
Ian Campbellf4d06352011-03-10 16:08:07 +0000532int xen_bind_pirq_gsi_to_irq(unsigned gsi,
533 unsigned pirq, int shareable, char *name)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400534{
Ian Campbella0e18112011-03-10 16:08:03 +0000535 int irq = -1;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400536 struct physdev_irq irq_op;
537
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400538 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400539
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100540 irq = xen_irq_from_gsi(gsi);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400541 if (irq != -1) {
Joe Perches283c0972013-06-28 03:21:41 -0700542 pr_info("%s: returning irq %d for gsi %u\n",
543 __func__, irq, gsi);
Daniel De Graaf420eb552011-10-27 17:58:47 -0400544 goto out;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400545 }
546
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000547 irq = xen_allocate_irq_gsi(gsi);
Ian Campbell7bee9762011-03-10 16:08:15 +0000548 if (irq < 0)
549 goto out;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400550
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400551 irq_op.irq = irq;
Alex Nixonb5401a92010-03-18 16:31:34 -0400552 irq_op.vector = 0;
553
554 /* Only the privileged domain can do this. For non-priv, the pcifront
555 * driver provides a PCI bus that does the call to do exactly
556 * this in the priv domain. */
557 if (xen_initial_domain() &&
558 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000559 xen_free_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400560 irq = -ENOSPC;
561 goto out;
562 }
563
Jan Beulichdec02de2013-04-03 15:52:50 +0100564 xen_irq_info_pirq_init(irq, 0, pirq, gsi, DOMID_SELF,
Ian Campbell9158c352011-03-10 16:08:09 +0000565 shareable ? PIRQ_SHAREABLE : 0);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400566
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100567 pirq_query_unmask(irq);
568 /* We try to use the handler with the appropriate semantic for the
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100569 * type of interrupt: if the interrupt is an edge triggered
570 * interrupt we use handle_edge_irq.
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100571 *
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100572 * On the other hand if the interrupt is level triggered we use
573 * handle_fasteoi_irq like the native code does for this kind of
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100574 * interrupts.
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100575 *
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100576 * Depending on the Xen version, pirq_needs_eoi might return true
577 * not only for level triggered interrupts but for edge triggered
578 * interrupts too. In any case Xen always honors the eoi mechanism,
579 * not injecting any more pirqs of the same kind if the first one
580 * hasn't received an eoi yet. Therefore using the fasteoi handler
581 * is the right choice either way.
582 */
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100583 if (shareable)
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100584 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
585 handle_fasteoi_irq, name);
586 else
587 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
588 handle_edge_irq, name);
589
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400590out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400591 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400592
593 return irq;
594}
595
Qing Hef731e3ef2010-10-11 15:30:09 +0100596#ifdef CONFIG_PCI_MSI
Ian Campbellbf480d92011-02-18 16:43:32 +0000597int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000598{
Ian Campbell5cad61a2011-02-18 16:43:31 +0000599 int rc;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000600 struct physdev_get_free_pirq op_get_free_pirq;
Ian Campbell5cad61a2011-02-18 16:43:31 +0000601
Ian Campbellbf480d92011-02-18 16:43:32 +0000602 op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000603 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000604
Ian Campbell5cad61a2011-02-18 16:43:31 +0000605 WARN_ONCE(rc == -ENOSYS,
606 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
607
608 return rc ? -1 : op_get_free_pirq.pirq;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000609}
610
Ian Campbellbf480d92011-02-18 16:43:32 +0000611int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
Jan Beulichdec02de2013-04-03 15:52:50 +0100612 int pirq, const char *name, domid_t domid)
Stefano Stabellini809f9262010-07-01 17:10:39 +0100613{
Ian Campbellbf480d92011-02-18 16:43:32 +0000614 int irq, ret;
Ian Campbell4b41df72011-02-18 16:43:29 +0000615
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400616 mutex_lock(&irq_mapping_update_lock);
Stefano Stabellini809f9262010-07-01 17:10:39 +0100617
Ian Campbell4b41df72011-02-18 16:43:29 +0000618 irq = xen_allocate_irq_dynamic();
Konrad Rzeszutek Wilke6599222011-09-29 13:26:45 -0400619 if (irq < 0)
Ian Campbellbb5d0792011-02-18 16:43:28 +0000620 goto out;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100621
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100622 irq_set_chip_and_handler_name(irq, &xen_pirq_chip, handle_edge_irq,
623 name);
Stefano Stabellini809f9262010-07-01 17:10:39 +0100624
Jan Beulichdec02de2013-04-03 15:52:50 +0100625 xen_irq_info_pirq_init(irq, 0, pirq, 0, domid, 0);
Linus Torvalds5f6fb452011-03-15 19:23:40 -0700626 ret = irq_set_msi_desc(irq, msidesc);
Ian Campbellbf480d92011-02-18 16:43:32 +0000627 if (ret < 0)
628 goto error_irq;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100629out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400630 mutex_unlock(&irq_mapping_update_lock);
Ian Campbell4b41df72011-02-18 16:43:29 +0000631 return irq;
Ian Campbellbf480d92011-02-18 16:43:32 +0000632error_irq:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400633 mutex_unlock(&irq_mapping_update_lock);
Ian Campbellbf480d92011-02-18 16:43:32 +0000634 xen_free_irq(irq);
Konrad Rzeszutek Wilke6599222011-09-29 13:26:45 -0400635 return ret;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100636}
Qing Hef731e3ef2010-10-11 15:30:09 +0100637#endif
638
Alex Nixonb5401a92010-03-18 16:31:34 -0400639int xen_destroy_irq(int irq)
640{
641 struct irq_desc *desc;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100642 struct physdev_unmap_pirq unmap_irq;
643 struct irq_info *info = info_for_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400644 int rc = -ENOENT;
645
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400646 mutex_lock(&irq_mapping_update_lock);
Alex Nixonb5401a92010-03-18 16:31:34 -0400647
648 desc = irq_to_desc(irq);
649 if (!desc)
650 goto out;
651
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100652 if (xen_initial_domain()) {
Konrad Rzeszutek Wilk12334712010-11-19 11:27:09 -0500653 unmap_irq.pirq = info->u.pirq.pirq;
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400654 unmap_irq.domid = info->u.pirq.domid;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100655 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
Konrad Rzeszutek Wilk1eff1ad2011-02-16 16:26:44 -0500656 /* If another domain quits without making the pci_disable_msix
657 * call, the Xen hypervisor takes care of freeing the PIRQs
658 * (free_domain_pirqs).
659 */
660 if ((rc == -ESRCH && info->u.pirq.domid != DOMID_SELF))
Joe Perches283c0972013-06-28 03:21:41 -0700661 pr_info("domain %d does not have %d anymore\n",
Konrad Rzeszutek Wilk1eff1ad2011-02-16 16:26:44 -0500662 info->u.pirq.domid, info->u.pirq.pirq);
663 else if (rc) {
Joe Perches283c0972013-06-28 03:21:41 -0700664 pr_warn("unmap irq failed %d\n", rc);
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100665 goto out;
666 }
667 }
Alex Nixonb5401a92010-03-18 16:31:34 -0400668
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000669 xen_free_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400670
671out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400672 mutex_unlock(&irq_mapping_update_lock);
Alex Nixonb5401a92010-03-18 16:31:34 -0400673 return rc;
674}
675
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000676int xen_irq_from_pirq(unsigned pirq)
677{
Ian Campbell69c358c2011-03-10 16:08:13 +0000678 int irq;
679
680 struct irq_info *info;
681
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400682 mutex_lock(&irq_mapping_update_lock);
Ian Campbell69c358c2011-03-10 16:08:13 +0000683
684 list_for_each_entry(info, &xen_irq_list_head, list) {
Konrad Rzeszutek Wilk9bb9efe2011-09-29 13:13:30 -0400685 if (info->type != IRQT_PIRQ)
Ian Campbell69c358c2011-03-10 16:08:13 +0000686 continue;
687 irq = info->irq;
688 if (info->u.pirq.pirq == pirq)
689 goto out;
690 }
691 irq = -1;
692out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400693 mutex_unlock(&irq_mapping_update_lock);
Ian Campbell69c358c2011-03-10 16:08:13 +0000694
695 return irq;
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000696}
697
Konrad Rzeszutek Wilke6197ac2011-02-24 14:20:12 -0500698
699int xen_pirq_from_irq(unsigned irq)
700{
701 return pirq_from_irq(irq);
702}
703EXPORT_SYMBOL_GPL(xen_pirq_from_irq);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700704int bind_evtchn_to_irq(unsigned int evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700705{
706 int irq;
707
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400708 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700709
710 irq = evtchn_to_irq[evtchn];
711
712 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000713 irq = xen_allocate_irq_dynamic();
Wei Liu68ba45f2013-01-31 14:46:56 +0000714 if (irq < 0)
Ian Campbell7bee9762011-03-10 16:08:15 +0000715 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700716
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100717 irq_set_chip_and_handler_name(irq, &xen_dynamic_chip,
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100718 handle_edge_irq, "event");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700719
Ian Campbell9158c352011-03-10 16:08:09 +0000720 xen_irq_info_evtchn_init(irq, evtchn);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400721 } else {
722 struct irq_info *info = info_for_irq(irq);
723 WARN_ON(info == NULL || info->type != IRQT_EVTCHN);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700724 }
725
Ian Campbell7bee9762011-03-10 16:08:15 +0000726out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400727 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700728
729 return irq;
730}
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700731EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700732
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700733static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
734{
735 struct evtchn_bind_ipi bind_ipi;
736 int evtchn, irq;
737
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400738 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700739
740 irq = per_cpu(ipi_to_irq, cpu)[ipi];
Ian Campbell90af9512009-02-06 16:55:58 -0800741
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700742 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000743 irq = xen_allocate_irq_dynamic();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700744 if (irq < 0)
745 goto out;
746
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100747 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700748 handle_percpu_irq, "ipi");
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700749
750 bind_ipi.vcpu = cpu;
751 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
752 &bind_ipi) != 0)
753 BUG();
754 evtchn = bind_ipi.port;
755
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000756 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700757
758 bind_evtchn_to_cpu(evtchn, cpu);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400759 } else {
760 struct irq_info *info = info_for_irq(irq);
761 WARN_ON(info == NULL || info->type != IRQT_IPI);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700762 }
763
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700764 out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400765 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700766 return irq;
767}
768
Ian Campbell2e820f52009-02-09 12:05:50 -0800769static int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
770 unsigned int remote_port)
771{
772 struct evtchn_bind_interdomain bind_interdomain;
773 int err;
774
775 bind_interdomain.remote_dom = remote_domain;
776 bind_interdomain.remote_port = remote_port;
777
778 err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
779 &bind_interdomain);
780
781 return err ? : bind_evtchn_to_irq(bind_interdomain.local_port);
782}
783
Olaf Hering62cc5fc2011-08-25 18:30:48 +0200784static int find_virq(unsigned int virq, unsigned int cpu)
785{
786 struct evtchn_status status;
787 int port, rc = -ENOENT;
788
789 memset(&status, 0, sizeof(status));
790 for (port = 0; port <= NR_EVENT_CHANNELS; port++) {
791 status.dom = DOMID_SELF;
792 status.port = port;
793 rc = HYPERVISOR_event_channel_op(EVTCHNOP_status, &status);
794 if (rc < 0)
795 continue;
796 if (status.status != EVTCHNSTAT_virq)
797 continue;
798 if (status.u.virq == virq && status.vcpu == cpu) {
799 rc = port;
800 break;
801 }
802 }
803 return rc;
804}
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700805
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100806int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700807{
808 struct evtchn_bind_virq bind_virq;
Olaf Hering62cc5fc2011-08-25 18:30:48 +0200809 int evtchn, irq, ret;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700810
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400811 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700812
813 irq = per_cpu(virq_to_irq, cpu)[virq];
814
815 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000816 irq = xen_allocate_irq_dynamic();
Wei Liu68ba45f2013-01-31 14:46:56 +0000817 if (irq < 0)
Ian Campbell7bee9762011-03-10 16:08:15 +0000818 goto out;
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700819
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100820 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700821 handle_percpu_irq, "virq");
822
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700823 bind_virq.virq = virq;
824 bind_virq.vcpu = cpu;
Olaf Hering62cc5fc2011-08-25 18:30:48 +0200825 ret = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
826 &bind_virq);
827 if (ret == 0)
828 evtchn = bind_virq.port;
829 else {
830 if (ret == -EEXIST)
831 ret = find_virq(virq, cpu);
832 BUG_ON(ret < 0);
833 evtchn = ret;
834 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700835
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000836 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700837
838 bind_evtchn_to_cpu(evtchn, cpu);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400839 } else {
840 struct irq_info *info = info_for_irq(irq);
841 WARN_ON(info == NULL || info->type != IRQT_VIRQ);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700842 }
843
Ian Campbell7bee9762011-03-10 16:08:15 +0000844out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400845 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700846
847 return irq;
848}
849
850static void unbind_from_irq(unsigned int irq)
851{
852 struct evtchn_close close;
853 int evtchn = evtchn_from_irq(irq);
Daniel De Graaf420eb552011-10-27 17:58:47 -0400854 struct irq_info *info = irq_get_handler_data(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700855
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -0400856 if (WARN_ON(!info))
857 return;
858
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400859 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700860
Daniel De Graaf420eb552011-10-27 17:58:47 -0400861 if (info->refcnt > 0) {
862 info->refcnt--;
863 if (info->refcnt != 0)
864 goto done;
865 }
866
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800867 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700868 close.port = evtchn;
869 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
870 BUG();
871
872 switch (type_from_irq(irq)) {
873 case IRQT_VIRQ:
874 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800875 [virq_from_irq(irq)] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700876 break;
Alex Nixond68d82a2008-08-22 11:52:15 +0100877 case IRQT_IPI:
878 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800879 [ipi_from_irq(irq)] = -1;
Alex Nixond68d82a2008-08-22 11:52:15 +0100880 break;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700881 default:
882 break;
883 }
884
885 /* Closed ports are implicitly re-bound to VCPU0. */
886 bind_evtchn_to_cpu(evtchn, 0);
887
888 evtchn_to_irq[evtchn] = -1;
Ian Campbellfed5ea82009-12-01 16:15:30 +0000889 }
890
Ian Campbellca62ce82011-03-10 16:08:12 +0000891 BUG_ON(info_for_irq(irq)->type == IRQT_UNBOUND);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700892
Ian Campbell9158c352011-03-10 16:08:09 +0000893 xen_free_irq(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700894
Daniel De Graaf420eb552011-10-27 17:58:47 -0400895 done:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400896 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700897}
898
899int bind_evtchn_to_irqhandler(unsigned int evtchn,
Jeff Garzik7c239972007-10-19 03:12:20 -0400900 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700901 unsigned long irqflags,
902 const char *devname, void *dev_id)
903{
Nicolas Kaiser361ae8c2011-03-30 21:14:26 +0200904 int irq, retval;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700905
906 irq = bind_evtchn_to_irq(evtchn);
Ian Campbell7bee9762011-03-10 16:08:15 +0000907 if (irq < 0)
908 return irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700909 retval = request_irq(irq, handler, irqflags, devname, dev_id);
910 if (retval != 0) {
911 unbind_from_irq(irq);
912 return retval;
913 }
914
915 return irq;
916}
917EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
918
Ian Campbell2e820f52009-02-09 12:05:50 -0800919int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
920 unsigned int remote_port,
921 irq_handler_t handler,
922 unsigned long irqflags,
923 const char *devname,
924 void *dev_id)
925{
926 int irq, retval;
927
928 irq = bind_interdomain_evtchn_to_irq(remote_domain, remote_port);
929 if (irq < 0)
930 return irq;
931
932 retval = request_irq(irq, handler, irqflags, devname, dev_id);
933 if (retval != 0) {
934 unbind_from_irq(irq);
935 return retval;
936 }
937
938 return irq;
939}
940EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler);
941
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700942int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
Jeff Garzik7c239972007-10-19 03:12:20 -0400943 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700944 unsigned long irqflags, const char *devname, void *dev_id)
945{
Nicolas Kaiser361ae8c2011-03-30 21:14:26 +0200946 int irq, retval;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700947
948 irq = bind_virq_to_irq(virq, cpu);
Ian Campbell7bee9762011-03-10 16:08:15 +0000949 if (irq < 0)
950 return irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700951 retval = request_irq(irq, handler, irqflags, devname, dev_id);
952 if (retval != 0) {
953 unbind_from_irq(irq);
954 return retval;
955 }
956
957 return irq;
958}
959EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
960
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700961int bind_ipi_to_irqhandler(enum ipi_vector ipi,
962 unsigned int cpu,
963 irq_handler_t handler,
964 unsigned long irqflags,
965 const char *devname,
966 void *dev_id)
967{
968 int irq, retval;
969
970 irq = bind_ipi_to_irq(ipi, cpu);
971 if (irq < 0)
972 return irq;
973
Ian Campbell9bab0b72011-10-03 15:37:00 +0100974 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME | IRQF_EARLY_RESUME;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700975 retval = request_irq(irq, handler, irqflags, devname, dev_id);
976 if (retval != 0) {
977 unbind_from_irq(irq);
978 return retval;
979 }
980
981 return irq;
982}
983
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700984void unbind_from_irqhandler(unsigned int irq, void *dev_id)
985{
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -0400986 struct irq_info *info = irq_get_handler_data(irq);
987
988 if (WARN_ON(!info))
989 return;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700990 free_irq(irq, dev_id);
991 unbind_from_irq(irq);
992}
993EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
994
Daniel De Graaf420eb552011-10-27 17:58:47 -0400995int evtchn_make_refcounted(unsigned int evtchn)
996{
997 int irq = evtchn_to_irq[evtchn];
998 struct irq_info *info;
999
1000 if (irq == -1)
1001 return -ENOENT;
1002
1003 info = irq_get_handler_data(irq);
1004
1005 if (!info)
1006 return -ENOENT;
1007
1008 WARN_ON(info->refcnt != -1);
1009
1010 info->refcnt = 1;
1011
1012 return 0;
1013}
1014EXPORT_SYMBOL_GPL(evtchn_make_refcounted);
1015
1016int evtchn_get(unsigned int evtchn)
1017{
1018 int irq;
1019 struct irq_info *info;
1020 int err = -ENOENT;
1021
Daniel De Graafc3b3f162011-11-28 11:49:09 -05001022 if (evtchn >= NR_EVENT_CHANNELS)
1023 return -EINVAL;
1024
Daniel De Graaf420eb552011-10-27 17:58:47 -04001025 mutex_lock(&irq_mapping_update_lock);
1026
1027 irq = evtchn_to_irq[evtchn];
1028 if (irq == -1)
1029 goto done;
1030
1031 info = irq_get_handler_data(irq);
1032
1033 if (!info)
1034 goto done;
1035
1036 err = -EINVAL;
1037 if (info->refcnt <= 0)
1038 goto done;
1039
1040 info->refcnt++;
1041 err = 0;
1042 done:
1043 mutex_unlock(&irq_mapping_update_lock);
1044
1045 return err;
1046}
1047EXPORT_SYMBOL_GPL(evtchn_get);
1048
1049void evtchn_put(unsigned int evtchn)
1050{
1051 int irq = evtchn_to_irq[evtchn];
1052 if (WARN_ON(irq == -1))
1053 return;
1054 unbind_from_irq(irq);
1055}
1056EXPORT_SYMBOL_GPL(evtchn_put);
1057
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001058void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
1059{
Konrad Rzeszutek Wilk6efa20e2013-07-19 11:51:31 -04001060 int irq;
1061
Stefano Stabellini072b2062013-08-13 16:57:06 +00001062#ifdef CONFIG_X86
Konrad Rzeszutek Wilk6efa20e2013-07-19 11:51:31 -04001063 if (unlikely(vector == XEN_NMI_VECTOR)) {
1064 int rc = HYPERVISOR_vcpu_op(VCPUOP_send_nmi, cpu, NULL);
1065 if (rc < 0)
1066 printk(KERN_WARNING "Sending nmi to CPU%d failed (rc:%d)\n", cpu, rc);
1067 return;
1068 }
Stefano Stabellini072b2062013-08-13 16:57:06 +00001069#endif
Konrad Rzeszutek Wilk6efa20e2013-07-19 11:51:31 -04001070 irq = per_cpu(ipi_to_irq, cpu)[vector];
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001071 BUG_ON(irq < 0);
1072 notify_remote_via_irq(irq);
1073}
1074
Tejun Heo245b2e72009-06-24 15:13:48 +09001075static DEFINE_PER_CPU(unsigned, xed_nesting_count);
1076
Sheng Yang38e20b02010-05-14 12:40:51 +01001077static void __xen_evtchn_do_upcall(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001078{
Christoph Lameter780f36d2010-12-06 11:16:29 -06001079 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
David Vrabel9a489f42013-03-13 15:29:25 +00001080 int cpu = get_cpu();
Ruslan Pisarev088c05a2011-07-26 14:16:13 +03001081 unsigned count;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001082
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001083 do {
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001084 vcpu_info->evtchn_upcall_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001085
Christoph Lameterb2e4ae62010-12-06 11:40:07 -06001086 if (__this_cpu_inc_return(xed_nesting_count) - 1)
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001087 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001088
David Vrabel9a489f42013-03-13 15:29:25 +00001089 xen_evtchn_handle_events(cpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001090
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001091 BUG_ON(!irqs_disabled());
1092
Christoph Lameter780f36d2010-12-06 11:16:29 -06001093 count = __this_cpu_read(xed_nesting_count);
1094 __this_cpu_write(xed_nesting_count, 0);
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001095 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001096
1097out:
Jeremy Fitzhardinge3445a8f2009-02-06 14:09:46 -08001098
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001099 put_cpu();
1100}
1101
Sheng Yang38e20b02010-05-14 12:40:51 +01001102void xen_evtchn_do_upcall(struct pt_regs *regs)
1103{
1104 struct pt_regs *old_regs = set_irq_regs(regs);
1105
Mojiong Qiu772aebc2012-11-06 16:08:15 +08001106 irq_enter();
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001107#ifdef CONFIG_X86
Sheng Yang38e20b02010-05-14 12:40:51 +01001108 exit_idle();
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001109#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001110
1111 __xen_evtchn_do_upcall();
1112
1113 irq_exit();
1114 set_irq_regs(old_regs);
1115}
1116
1117void xen_hvm_evtchn_do_upcall(void)
1118{
1119 __xen_evtchn_do_upcall();
1120}
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001121EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
Sheng Yang38e20b02010-05-14 12:40:51 +01001122
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001123/* Rebind a new event channel to an existing irq. */
1124void rebind_evtchn_irq(int evtchn, int irq)
1125{
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001126 struct irq_info *info = info_for_irq(irq);
1127
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -04001128 if (WARN_ON(!info))
1129 return;
1130
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001131 /* Make sure the irq is masked, since the new event channel
1132 will also be masked. */
1133 disable_irq(irq);
1134
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001135 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001136
1137 /* After resume the irq<->evtchn mappings are all cleared out */
1138 BUG_ON(evtchn_to_irq[evtchn] != -1);
1139 /* Expect irq to have been bound before,
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001140 so there should be a proper type */
1141 BUG_ON(info->type == IRQT_UNBOUND);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001142
Ian Campbell9158c352011-03-10 16:08:09 +00001143 xen_irq_info_evtchn_init(irq, evtchn);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001144
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001145 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001146
1147 /* new event channels are always bound to cpu 0 */
Rusty Russell0de26522008-12-13 21:20:26 +10301148 irq_set_affinity(irq, cpumask_of(0));
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001149
1150 /* Unmask the event channel. */
1151 enable_irq(irq);
1152}
1153
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001154/* Rebind an evtchn so that it gets delivered to a specific cpu */
Yinghai Lud5dedd42009-04-27 17:59:21 -07001155static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001156{
1157 struct evtchn_bind_vcpu bind_vcpu;
1158 int evtchn = evtchn_from_irq(irq);
David Vrabel4704fe42013-08-15 13:21:07 +01001159 int masked;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001160
Ian Campbellbe494722011-03-10 16:08:02 +00001161 if (!VALID_EVTCHN(evtchn))
1162 return -1;
1163
1164 /*
1165 * Events delivered via platform PCI interrupts are always
1166 * routed to vcpu 0 and hence cannot be rebound.
1167 */
1168 if (xen_hvm_domain() && !xen_have_vector_callback)
Yinghai Lud5dedd42009-04-27 17:59:21 -07001169 return -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001170
1171 /* Send future instances of this interrupt to other vcpu. */
1172 bind_vcpu.port = evtchn;
1173 bind_vcpu.vcpu = tcpu;
1174
1175 /*
David Vrabel4704fe42013-08-15 13:21:07 +01001176 * Mask the event while changing the VCPU binding to prevent
1177 * it being delivered on an unexpected VCPU.
1178 */
Wei Liu3f70fa82013-03-07 15:50:27 +00001179 masked = test_and_set_mask(evtchn);
David Vrabel4704fe42013-08-15 13:21:07 +01001180
1181 /*
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001182 * If this fails, it usually just indicates that we're dealing with a
1183 * virq or IPI channel, which don't actually need to be rebound. Ignore
1184 * it, but don't do the xenlinux-level rebind in that case.
1185 */
1186 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1187 bind_evtchn_to_cpu(evtchn, tcpu);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001188
David Vrabel4704fe42013-08-15 13:21:07 +01001189 if (!masked)
1190 unmask_evtchn(evtchn);
1191
Yinghai Lud5dedd42009-04-27 17:59:21 -07001192 return 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001193}
1194
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001195static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1196 bool force)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001197{
Rusty Russell0de26522008-12-13 21:20:26 +10301198 unsigned tcpu = cpumask_first(dest);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001199
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001200 return rebind_irq_to_cpu(data->irq, tcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001201}
1202
David Vrabel87295182013-03-12 18:28:04 +00001203static int retrigger_evtchn(int evtchn)
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001204{
David Vrabel87295182013-03-12 18:28:04 +00001205 int masked;
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001206
1207 if (!VALID_EVTCHN(evtchn))
David Vrabel87295182013-03-12 18:28:04 +00001208 return 0;
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001209
Wei Liu3f70fa82013-03-07 15:50:27 +00001210 masked = test_and_set_mask(evtchn);
Wei Liu76ec8d62013-03-07 15:50:28 +00001211 set_evtchn(evtchn);
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001212 if (!masked)
1213 unmask_evtchn(evtchn);
1214
1215 return 1;
1216}
1217
David Vrabel87295182013-03-12 18:28:04 +00001218int resend_irq_on_evtchn(unsigned int irq)
1219{
1220 return retrigger_evtchn(evtchn_from_irq(irq));
1221}
1222
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001223static void enable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001224{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001225 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001226
1227 if (VALID_EVTCHN(evtchn))
1228 unmask_evtchn(evtchn);
1229}
1230
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001231static void disable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001232{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001233 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001234
1235 if (VALID_EVTCHN(evtchn))
1236 mask_evtchn(evtchn);
1237}
1238
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001239static void ack_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001240{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001241 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001242
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001243 irq_move_irq(data);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001244
1245 if (VALID_EVTCHN(evtchn))
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001246 clear_evtchn(evtchn);
1247}
1248
1249static void mask_ack_dynirq(struct irq_data *data)
1250{
1251 disable_dynirq(data);
1252 ack_dynirq(data);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001253}
1254
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001255static int retrigger_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001256{
David Vrabel87295182013-03-12 18:28:04 +00001257 return retrigger_evtchn(evtchn_from_irq(data->irq));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001258}
1259
Ian Campbell0a852262011-03-10 16:08:06 +00001260static void restore_pirqs(void)
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001261{
1262 int pirq, rc, irq, gsi;
1263 struct physdev_map_pirq map_irq;
Ian Campbell69c358c2011-03-10 16:08:13 +00001264 struct irq_info *info;
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001265
Ian Campbell69c358c2011-03-10 16:08:13 +00001266 list_for_each_entry(info, &xen_irq_list_head, list) {
1267 if (info->type != IRQT_PIRQ)
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001268 continue;
1269
Ian Campbell69c358c2011-03-10 16:08:13 +00001270 pirq = info->u.pirq.pirq;
1271 gsi = info->u.pirq.gsi;
1272 irq = info->irq;
1273
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001274 /* save/restore of PT devices doesn't work, so at this point the
1275 * only devices present are GSI based emulated devices */
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001276 if (!gsi)
1277 continue;
1278
1279 map_irq.domid = DOMID_SELF;
1280 map_irq.type = MAP_PIRQ_TYPE_GSI;
1281 map_irq.index = gsi;
1282 map_irq.pirq = pirq;
1283
1284 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1285 if (rc) {
Joe Perches283c0972013-06-28 03:21:41 -07001286 pr_warn("xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1287 gsi, irq, pirq, rc);
Ian Campbell9158c352011-03-10 16:08:09 +00001288 xen_free_irq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001289 continue;
1290 }
1291
1292 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1293
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001294 __startup_pirq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001295 }
1296}
1297
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001298static void restore_cpu_virqs(unsigned int cpu)
1299{
1300 struct evtchn_bind_virq bind_virq;
1301 int virq, irq, evtchn;
1302
1303 for (virq = 0; virq < NR_VIRQS; virq++) {
1304 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1305 continue;
1306
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001307 BUG_ON(virq_from_irq(irq) != virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001308
1309 /* Get a new binding from Xen. */
1310 bind_virq.virq = virq;
1311 bind_virq.vcpu = cpu;
1312 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1313 &bind_virq) != 0)
1314 BUG();
1315 evtchn = bind_virq.port;
1316
1317 /* Record the new mapping. */
Ian Campbell3d4cfa32011-03-10 16:08:10 +00001318 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001319 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001320 }
1321}
1322
1323static void restore_cpu_ipis(unsigned int cpu)
1324{
1325 struct evtchn_bind_ipi bind_ipi;
1326 int ipi, irq, evtchn;
1327
1328 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1329 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1330 continue;
1331
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001332 BUG_ON(ipi_from_irq(irq) != ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001333
1334 /* Get a new binding from Xen. */
1335 bind_ipi.vcpu = cpu;
1336 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1337 &bind_ipi) != 0)
1338 BUG();
1339 evtchn = bind_ipi.port;
1340
1341 /* Record the new mapping. */
Ian Campbell3d4cfa32011-03-10 16:08:10 +00001342 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001343 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001344 }
1345}
1346
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001347/* Clear an irq's pending state, in preparation for polling on it */
1348void xen_clear_irq_pending(int irq)
1349{
1350 int evtchn = evtchn_from_irq(irq);
1351
1352 if (VALID_EVTCHN(evtchn))
1353 clear_evtchn(evtchn);
1354}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001355EXPORT_SYMBOL(xen_clear_irq_pending);
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -07001356void xen_set_irq_pending(int irq)
1357{
1358 int evtchn = evtchn_from_irq(irq);
1359
1360 if (VALID_EVTCHN(evtchn))
1361 set_evtchn(evtchn);
1362}
1363
1364bool xen_test_irq_pending(int irq)
1365{
1366 int evtchn = evtchn_from_irq(irq);
1367 bool ret = false;
1368
1369 if (VALID_EVTCHN(evtchn))
1370 ret = test_evtchn(evtchn);
1371
1372 return ret;
1373}
1374
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001375/* Poll waiting for an irq to become pending with timeout. In the usual case,
1376 * the irq will be disabled so it won't deliver an interrupt. */
1377void xen_poll_irq_timeout(int irq, u64 timeout)
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001378{
1379 evtchn_port_t evtchn = evtchn_from_irq(irq);
1380
1381 if (VALID_EVTCHN(evtchn)) {
1382 struct sched_poll poll;
1383
1384 poll.nr_ports = 1;
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001385 poll.timeout = timeout;
Isaku Yamahataff3c5362008-10-14 17:50:44 -07001386 set_xen_guest_handle(poll.ports, &evtchn);
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001387
1388 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1389 BUG();
1390 }
1391}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001392EXPORT_SYMBOL(xen_poll_irq_timeout);
1393/* Poll waiting for an irq to become pending. In the usual case, the
1394 * irq will be disabled so it won't deliver an interrupt. */
1395void xen_poll_irq(int irq)
1396{
1397 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1398}
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001399
Konrad Rzeszutek Wilkc7c2c3a282010-11-08 14:26:36 -05001400/* Check whether the IRQ line is shared with other guests. */
1401int xen_test_irq_shared(int irq)
1402{
1403 struct irq_info *info = info_for_irq(irq);
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -04001404 struct physdev_irq_status_query irq_status;
1405
1406 if (WARN_ON(!info))
1407 return -ENOENT;
1408
1409 irq_status.irq = info->u.pirq.pirq;
Konrad Rzeszutek Wilkc7c2c3a282010-11-08 14:26:36 -05001410
1411 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
1412 return 0;
1413 return !(irq_status.flags & XENIRQSTAT_shared);
1414}
1415EXPORT_SYMBOL_GPL(xen_test_irq_shared);
1416
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001417void xen_irq_resume(void)
1418{
Ian Campbell6cb65372011-03-10 16:08:11 +00001419 unsigned int cpu, evtchn;
1420 struct irq_info *info;
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001421
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001422 /* New event-channel space is not 'live' yet. */
1423 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1424 mask_evtchn(evtchn);
1425
1426 /* No IRQ <-> event-channel mappings. */
Ian Campbell6cb65372011-03-10 16:08:11 +00001427 list_for_each_entry(info, &xen_irq_list_head, list)
1428 info->evtchn = 0; /* zap event-channel binding */
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001429
1430 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1431 evtchn_to_irq[evtchn] = -1;
1432
1433 for_each_possible_cpu(cpu) {
1434 restore_cpu_virqs(cpu);
1435 restore_cpu_ipis(cpu);
1436 }
Ian Campbell69035912010-11-01 16:30:09 +00001437
Ian Campbell0a852262011-03-10 16:08:06 +00001438 restore_pirqs();
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001439}
1440
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001441static struct irq_chip xen_dynamic_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001442 .name = "xen-dyn",
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001443
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001444 .irq_disable = disable_dynirq,
1445 .irq_mask = disable_dynirq,
1446 .irq_unmask = enable_dynirq,
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001447
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001448 .irq_ack = ack_dynirq,
1449 .irq_mask_ack = mask_ack_dynirq,
1450
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001451 .irq_set_affinity = set_affinity_irq,
1452 .irq_retrigger = retrigger_dynirq,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001453};
1454
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001455static struct irq_chip xen_pirq_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001456 .name = "xen-pirq",
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001457
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001458 .irq_startup = startup_pirq,
1459 .irq_shutdown = shutdown_pirq,
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001460 .irq_enable = enable_pirq,
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001461 .irq_disable = disable_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001462
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001463 .irq_mask = disable_dynirq,
1464 .irq_unmask = enable_dynirq,
1465
1466 .irq_ack = eoi_pirq,
1467 .irq_eoi = eoi_pirq,
1468 .irq_mask_ack = mask_ack_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001469
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001470 .irq_set_affinity = set_affinity_irq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001471
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001472 .irq_retrigger = retrigger_dynirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001473};
1474
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001475static struct irq_chip xen_percpu_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001476 .name = "xen-percpu",
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001477
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001478 .irq_disable = disable_dynirq,
1479 .irq_mask = disable_dynirq,
1480 .irq_unmask = enable_dynirq,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001481
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001482 .irq_ack = ack_dynirq,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001483};
1484
Sheng Yang38e20b02010-05-14 12:40:51 +01001485int xen_set_callback_via(uint64_t via)
1486{
1487 struct xen_hvm_param a;
1488 a.domid = DOMID_SELF;
1489 a.index = HVM_PARAM_CALLBACK_IRQ;
1490 a.value = via;
1491 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1492}
1493EXPORT_SYMBOL_GPL(xen_set_callback_via);
1494
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001495#ifdef CONFIG_XEN_PVHVM
Sheng Yang38e20b02010-05-14 12:40:51 +01001496/* Vector callbacks are better than PCI interrupts to receive event
1497 * channel notifications because we can receive vector callbacks on any
1498 * vcpu and we don't need PCI support or APIC interactions. */
1499void xen_callback_vector(void)
1500{
1501 int rc;
1502 uint64_t callback_via;
1503 if (xen_have_vector_callback) {
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -08001504 callback_via = HVM_CALLBACK_VECTOR(HYPERVISOR_CALLBACK_VECTOR);
Sheng Yang38e20b02010-05-14 12:40:51 +01001505 rc = xen_set_callback_via(callback_via);
1506 if (rc) {
Joe Perches283c0972013-06-28 03:21:41 -07001507 pr_err("Request for Xen HVM callback vector failed\n");
Sheng Yang38e20b02010-05-14 12:40:51 +01001508 xen_have_vector_callback = 0;
1509 return;
1510 }
Joe Perches283c0972013-06-28 03:21:41 -07001511 pr_info("Xen HVM callback vector for event delivery is enabled\n");
Sheng Yang38e20b02010-05-14 12:40:51 +01001512 /* in the restore case the vector has already been allocated */
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -08001513 if (!test_bit(HYPERVISOR_CALLBACK_VECTOR, used_vectors))
1514 alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR,
1515 xen_hvm_callback_vector);
Sheng Yang38e20b02010-05-14 12:40:51 +01001516 }
1517}
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001518#else
1519void xen_callback_vector(void) {}
1520#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001521
Stefano Stabellini2e3d8862012-10-02 15:57:57 +01001522void __init xen_init_IRQ(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001523{
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001524 int i;
Mike Travisc7a35892009-01-10 21:58:11 -08001525
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001526 evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
1527 GFP_KERNEL);
Konrad Rzeszutek Wilk9d093e22011-09-29 13:31:21 -04001528 BUG_ON(!evtchn_to_irq);
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001529 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1530 evtchn_to_irq[i] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001531
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001532 /* No event channels are 'live' right now. */
1533 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1534 mask_evtchn(i);
1535
Stefano Stabellini9846ff12012-01-30 16:21:48 +00001536 pirq_needs_eoi = pirq_needs_eoi_flag;
1537
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001538#ifdef CONFIG_X86
Sheng Yang38e20b02010-05-14 12:40:51 +01001539 if (xen_hvm_domain()) {
1540 xen_callback_vector();
1541 native_init_IRQ();
Stefano Stabellini3942b742010-06-24 17:50:18 +01001542 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1543 * __acpi_register_gsi can point at the right function */
1544 pci_xen_hvm_init();
Sheng Yang38e20b02010-05-14 12:40:51 +01001545 } else {
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001546 int rc;
Stefano Stabellini9846ff12012-01-30 16:21:48 +00001547 struct physdev_pirq_eoi_gmfn eoi_gmfn;
1548
Sheng Yang38e20b02010-05-14 12:40:51 +01001549 irq_ctx_init(smp_processor_id());
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +01001550 if (xen_initial_domain())
Konrad Rzeszutek Wilka0ee0562011-06-09 09:49:13 -04001551 pci_xen_initial_domain();
Stefano Stabellini9846ff12012-01-30 16:21:48 +00001552
1553 pirq_eoi_map = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
1554 eoi_gmfn.gmfn = virt_to_mfn(pirq_eoi_map);
1555 rc = HYPERVISOR_physdev_op(PHYSDEVOP_pirq_eoi_gmfn_v2, &eoi_gmfn);
1556 if (rc != 0) {
1557 free_page((unsigned long) pirq_eoi_map);
1558 pirq_eoi_map = NULL;
1559 } else
1560 pirq_needs_eoi = pirq_check_eoi_map;
Sheng Yang38e20b02010-05-14 12:40:51 +01001561 }
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001562#endif
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001563}