blob: 6a6bbe4ede92c67afe4c88efd105839f14b3a240 [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
24#include <linux/linkage.h>
25#include <linux/interrupt.h>
26#include <linux/irq.h>
27#include <linux/module.h>
28#include <linux/string.h>
Christophe Saout28e08862009-01-11 11:46:23 -080029#include <linux/bootmem.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -040031#include <linux/irqnr.h>
Qing Hef731e3ef2010-10-11 15:30:09 +010032#include <linux/pci.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070033
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +000034#ifdef CONFIG_X86
Sheng Yang38e20b02010-05-14 12:40:51 +010035#include <asm/desc.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070036#include <asm/ptrace.h>
37#include <asm/irq.h>
Jeremy Fitzhardinge792dc4f2009-02-06 14:09:43 -080038#include <asm/idle.h>
Konrad Rzeszutek Wilk0794bfc2010-10-18 10:41:08 -040039#include <asm/io_apic.h>
Stefano Stabellini9846ff12012-01-30 16:21:48 +000040#include <asm/xen/page.h>
Stefano Stabellini42a1de52010-06-24 16:42:04 +010041#include <asm/xen/pci.h>
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +000042#endif
43#include <asm/sync_bitops.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070044#include <asm/xen/hypercall.h>
Adrian Bunk8d1b8752007-07-20 00:31:44 -070045#include <asm/xen/hypervisor.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070046
Sheng Yang38e20b02010-05-14 12:40:51 +010047#include <xen/xen.h>
48#include <xen/hvm.h>
Isaku Yamahatae04d0d02008-04-02 10:53:55 -070049#include <xen/xen-ops.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070050#include <xen/events.h>
51#include <xen/interface/xen.h>
52#include <xen/interface/event_channel.h>
Sheng Yang38e20b02010-05-14 12:40:51 +010053#include <xen/interface/hvm/hvm_op.h>
54#include <xen/interface/hvm/params.h>
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +000055#include <xen/interface/physdev.h>
56#include <xen/interface/sched.h>
57#include <asm/hw_irq.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070058
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070059/*
60 * This lock protects updates to the following mapping and reference-count
61 * arrays. The lock does not need to be acquired to read the mapping tables.
62 */
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -040063static DEFINE_MUTEX(irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070064
Ian Campbell6cb65372011-03-10 16:08:11 +000065static LIST_HEAD(xen_irq_list_head);
66
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070067/* IRQ <-> VIRQ mapping. */
Tejun Heo204fba42009-06-24 15:13:45 +090068static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070069
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070070/* IRQ <-> IPI mapping */
Tejun Heo204fba42009-06-24 15:13:45 +090071static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070072
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080073/* Interrupt types. */
74enum xen_irq_type {
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -080075 IRQT_UNBOUND = 0,
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070076 IRQT_PIRQ,
77 IRQT_VIRQ,
78 IRQT_IPI,
79 IRQT_EVTCHN
80};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070081
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080082/*
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:
Jan Beulichdec02de2013-04-03 15:52:50 +010088 * PIRQ - physical IRQ, GSI, flags, and owner domain
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080089 * VIRQ - virq number
90 * IPI - IPI vector
91 * EVTCHN -
92 */
Ruslan Pisarev088c05a2011-07-26 14:16:13 +030093struct irq_info {
Ian Campbell6cb65372011-03-10 16:08:11 +000094 struct list_head list;
Daniel De Graaf420eb552011-10-27 17:58:47 -040095 int refcnt;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080096 enum xen_irq_type type; /* type */
Ian Campbell6cb65372011-03-10 16:08:11 +000097 unsigned irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080098 unsigned short evtchn; /* event channel */
99 unsigned short cpu; /* cpu bound */
100
101 union {
102 unsigned short virq;
103 enum ipi_vector ipi;
104 struct {
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100105 unsigned short pirq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800106 unsigned short gsi;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400107 unsigned char flags;
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400108 uint16_t domid;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800109 } pirq;
110 } u;
111};
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400112#define PIRQ_NEEDS_EOI (1 << 0)
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400113#define PIRQ_SHAREABLE (1 << 1)
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800114
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -0400115static int *evtchn_to_irq;
Ian Campbellbf86ad82012-10-17 09:39:12 +0100116#ifdef CONFIG_X86
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000117static unsigned long *pirq_eoi_map;
Ian Campbellbf86ad82012-10-17 09:39:12 +0100118#endif
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000119static bool (*pirq_needs_eoi)(unsigned irq);
Jeremy Fitzhardinge3b32f572009-08-13 12:50:37 -0700120
Ian Campbellc81611c2013-02-20 11:48:06 +0000121/*
122 * Note sizeof(xen_ulong_t) can be more than sizeof(unsigned long). Be
123 * careful to only use bitops which allow for this (e.g
124 * test_bit/find_first_bit and friends but not __ffs) and to pass
125 * BITS_PER_EVTCHN_WORD as the bitmask length.
126 */
127#define BITS_PER_EVTCHN_WORD (sizeof(xen_ulong_t)*8)
128/*
129 * Make a bitmask (i.e. unsigned long *) of a xen_ulong_t
130 * array. Primarily to avoid long lines (hence the terse name).
131 */
132#define BM(x) (unsigned long *)(x)
133/* Find the first set bit in a evtchn mask */
134#define EVTCHN_FIRST_BIT(w) find_first_bit(BM(&(w)), BITS_PER_EVTCHN_WORD)
135
136static DEFINE_PER_CPU(xen_ulong_t [NR_EVENT_CHANNELS/BITS_PER_EVTCHN_WORD],
Ian Campbellcb60d112011-03-10 16:08:08 +0000137 cpu_evtchn_mask);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700138
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700139/* Xen will never allocate port zero for any purpose. */
140#define VALID_EVTCHN(chn) ((chn) != 0)
141
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700142static struct irq_chip xen_dynamic_chip;
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700143static struct irq_chip xen_percpu_chip;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400144static struct irq_chip xen_pirq_chip;
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100145static void enable_dynirq(struct irq_data *data);
146static void disable_dynirq(struct irq_data *data);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700147
Ian Campbell9158c352011-03-10 16:08:09 +0000148/* Get info for IRQ */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800149static struct irq_info *info_for_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700150{
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100151 return irq_get_handler_data(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700152}
153
Ian Campbell9158c352011-03-10 16:08:09 +0000154/* Constructors for packed IRQ information. */
155static void xen_irq_info_common_init(struct irq_info *info,
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000156 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000157 enum xen_irq_type type,
158 unsigned short evtchn,
159 unsigned short cpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700160{
Ian Campbell9158c352011-03-10 16:08:09 +0000161
162 BUG_ON(info->type != IRQT_UNBOUND && info->type != type);
163
164 info->type = type;
Ian Campbell6cb65372011-03-10 16:08:11 +0000165 info->irq = irq;
Ian Campbell9158c352011-03-10 16:08:09 +0000166 info->evtchn = evtchn;
167 info->cpu = cpu;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000168
169 evtchn_to_irq[evtchn] = irq;
Julien Grall934f5852013-04-30 18:29:13 +0100170
171 irq_clear_status_flags(irq, IRQ_NOREQUEST|IRQ_NOAUTOEN);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700172}
173
Ian Campbell9158c352011-03-10 16:08:09 +0000174static void xen_irq_info_evtchn_init(unsigned irq,
175 unsigned short evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700176{
Ian Campbell9158c352011-03-10 16:08:09 +0000177 struct irq_info *info = info_for_irq(irq);
178
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000179 xen_irq_info_common_init(info, irq, IRQT_EVTCHN, evtchn, 0);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700180}
181
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000182static void xen_irq_info_ipi_init(unsigned cpu,
183 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000184 unsigned short evtchn,
185 enum ipi_vector ipi)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700186{
Ian Campbell9158c352011-03-10 16:08:09 +0000187 struct irq_info *info = info_for_irq(irq);
188
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000189 xen_irq_info_common_init(info, irq, IRQT_IPI, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000190
191 info->u.ipi = ipi;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000192
193 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700194}
195
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000196static void xen_irq_info_virq_init(unsigned cpu,
197 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000198 unsigned short evtchn,
199 unsigned short virq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700200{
Ian Campbell9158c352011-03-10 16:08:09 +0000201 struct irq_info *info = info_for_irq(irq);
202
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000203 xen_irq_info_common_init(info, irq, IRQT_VIRQ, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000204
205 info->u.virq = virq;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000206
207 per_cpu(virq_to_irq, cpu)[virq] = irq;
Ian Campbell9158c352011-03-10 16:08:09 +0000208}
209
210static void xen_irq_info_pirq_init(unsigned irq,
211 unsigned short evtchn,
212 unsigned short pirq,
213 unsigned short gsi,
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400214 uint16_t domid,
Ian Campbell9158c352011-03-10 16:08:09 +0000215 unsigned char flags)
216{
217 struct irq_info *info = info_for_irq(irq);
218
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000219 xen_irq_info_common_init(info, irq, IRQT_PIRQ, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000220
221 info->u.pirq.pirq = pirq;
222 info->u.pirq.gsi = gsi;
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400223 info->u.pirq.domid = domid;
Ian Campbell9158c352011-03-10 16:08:09 +0000224 info->u.pirq.flags = flags;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700225}
226
227/*
228 * Accessors for packed IRQ information.
229 */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800230static unsigned int evtchn_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700231{
Joe Jin110e7c72011-01-07 14:50:12 +0800232 if (unlikely(WARN(irq < 0 || irq >= nr_irqs, "Invalid irq %d!\n", irq)))
233 return 0;
234
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800235 return info_for_irq(irq)->evtchn;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700236}
237
Ian Campbelld4c04532009-02-06 19:20:31 -0800238unsigned irq_from_evtchn(unsigned int evtchn)
239{
240 return evtchn_to_irq[evtchn];
241}
242EXPORT_SYMBOL_GPL(irq_from_evtchn);
243
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800244static enum ipi_vector ipi_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700245{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800246 struct irq_info *info = info_for_irq(irq);
247
248 BUG_ON(info == NULL);
249 BUG_ON(info->type != IRQT_IPI);
250
251 return info->u.ipi;
252}
253
254static unsigned virq_from_irq(unsigned irq)
255{
256 struct irq_info *info = info_for_irq(irq);
257
258 BUG_ON(info == NULL);
259 BUG_ON(info->type != IRQT_VIRQ);
260
261 return info->u.virq;
262}
263
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100264static unsigned pirq_from_irq(unsigned irq)
265{
266 struct irq_info *info = info_for_irq(irq);
267
268 BUG_ON(info == NULL);
269 BUG_ON(info->type != IRQT_PIRQ);
270
271 return info->u.pirq.pirq;
272}
273
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800274static enum xen_irq_type type_from_irq(unsigned irq)
275{
276 return info_for_irq(irq)->type;
277}
278
279static unsigned cpu_from_irq(unsigned irq)
280{
281 return info_for_irq(irq)->cpu;
282}
283
284static unsigned int cpu_from_evtchn(unsigned int evtchn)
285{
286 int irq = evtchn_to_irq[evtchn];
287 unsigned ret = 0;
288
289 if (irq != -1)
290 ret = cpu_from_irq(irq);
291
292 return ret;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700293}
294
Ian Campbellbf86ad82012-10-17 09:39:12 +0100295#ifdef CONFIG_X86
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000296static bool pirq_check_eoi_map(unsigned irq)
297{
Stefano Stabellini521394e2012-04-25 16:11:38 +0100298 return test_bit(pirq_from_irq(irq), pirq_eoi_map);
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000299}
Ian Campbellbf86ad82012-10-17 09:39:12 +0100300#endif
Stefano Stabellini9846ff12012-01-30 16:21:48 +0000301
302static bool pirq_needs_eoi_flag(unsigned irq)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400303{
304 struct irq_info *info = info_for_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400305 BUG_ON(info->type != IRQT_PIRQ);
306
307 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
308}
309
Ian Campbellc81611c2013-02-20 11:48:06 +0000310static inline xen_ulong_t active_evtchns(unsigned int cpu,
311 struct shared_info *sh,
312 unsigned int idx)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700313{
Ruslan Pisarev088c05a2011-07-26 14:16:13 +0300314 return sh->evtchn_pending[idx] &
Ian Campbellcb60d112011-03-10 16:08:08 +0000315 per_cpu(cpu_evtchn_mask, cpu)[idx] &
Ruslan Pisarev088c05a2011-07-26 14:16:13 +0300316 ~sh->evtchn_mask[idx];
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700317}
318
319static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
320{
321 int irq = evtchn_to_irq[chn];
322
323 BUG_ON(irq == -1);
324#ifdef CONFIG_SMP
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000325 cpumask_copy(irq_to_desc(irq)->irq_data.affinity, cpumask_of(cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700326#endif
327
Ian Campbellc81611c2013-02-20 11:48:06 +0000328 clear_bit(chn, BM(per_cpu(cpu_evtchn_mask, cpu_from_irq(irq))));
329 set_bit(chn, BM(per_cpu(cpu_evtchn_mask, cpu)));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700330
Ian Campbellca62ce82011-03-10 16:08:12 +0000331 info_for_irq(irq)->cpu = cpu;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700332}
333
334static void init_evtchn_cpu_bindings(void)
335{
Jan Beulich1c6969e2010-11-16 14:55:33 -0800336 int i;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700337#ifdef CONFIG_SMP
Ian Campbell6cb65372011-03-10 16:08:11 +0000338 struct irq_info *info;
Thomas Gleixner10e58082008-10-16 14:19:04 +0200339
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700340 /* By default all event channels notify CPU#0. */
Ian Campbell6cb65372011-03-10 16:08:11 +0000341 list_for_each_entry(info, &xen_irq_list_head, list) {
342 struct irq_desc *desc = irq_to_desc(info->irq);
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000343 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800344 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700345#endif
346
Jan Beulich1c6969e2010-11-16 14:55:33 -0800347 for_each_possible_cpu(i)
Ian Campbellcb60d112011-03-10 16:08:08 +0000348 memset(per_cpu(cpu_evtchn_mask, i),
349 (i == 0) ? ~0 : 0, sizeof(*per_cpu(cpu_evtchn_mask, i)));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700350}
351
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700352static inline void clear_evtchn(int port)
353{
354 struct shared_info *s = HYPERVISOR_shared_info;
Ian Campbellc81611c2013-02-20 11:48:06 +0000355 sync_clear_bit(port, BM(&s->evtchn_pending[0]));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700356}
357
358static inline void set_evtchn(int port)
359{
360 struct shared_info *s = HYPERVISOR_shared_info;
Ian Campbellc81611c2013-02-20 11:48:06 +0000361 sync_set_bit(port, BM(&s->evtchn_pending[0]));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700362}
363
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700364static inline int test_evtchn(int port)
365{
366 struct shared_info *s = HYPERVISOR_shared_info;
Ian Campbellc81611c2013-02-20 11:48:06 +0000367 return sync_test_bit(port, BM(&s->evtchn_pending[0]));
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700368}
369
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700370
371/**
372 * notify_remote_via_irq - send event to remote end of event channel via irq
373 * @irq: irq of event channel to send event to
374 *
375 * Unlike notify_remote_via_evtchn(), this is safe to use across
376 * save/restore. Notifications on a broken connection are silently
377 * dropped.
378 */
379void notify_remote_via_irq(int irq)
380{
381 int evtchn = evtchn_from_irq(irq);
382
383 if (VALID_EVTCHN(evtchn))
384 notify_remote_via_evtchn(evtchn);
385}
386EXPORT_SYMBOL_GPL(notify_remote_via_irq);
387
388static void mask_evtchn(int port)
389{
390 struct shared_info *s = HYPERVISOR_shared_info;
Ian Campbellc81611c2013-02-20 11:48:06 +0000391 sync_set_bit(port, BM(&s->evtchn_mask[0]));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700392}
393
394static void unmask_evtchn(int port)
395{
396 struct shared_info *s = HYPERVISOR_shared_info;
397 unsigned int cpu = get_cpu();
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100398 int do_hypercall = 0, evtchn_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700399
400 BUG_ON(!irqs_disabled());
401
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100402 if (unlikely((cpu != cpu_from_evtchn(port))))
403 do_hypercall = 1;
David Vrabelc26377e2013-03-25 14:11:19 +0000404 else {
405 /*
406 * Need to clear the mask before checking pending to
407 * avoid a race with an event becoming pending.
408 *
409 * EVTCHNOP_unmask will only trigger an upcall if the
410 * mask bit was set, so if a hypercall is needed
411 * remask the event.
412 */
413 sync_clear_bit(port, BM(&s->evtchn_mask[0]));
Ian Campbellc81611c2013-02-20 11:48:06 +0000414 evtchn_pending = sync_test_bit(port, BM(&s->evtchn_pending[0]));
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100415
David Vrabelc26377e2013-03-25 14:11:19 +0000416 if (unlikely(evtchn_pending && xen_hvm_domain())) {
417 sync_set_bit(port, BM(&s->evtchn_mask[0]));
418 do_hypercall = 1;
419 }
420 }
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100421
422 /* Slow path (hypercall) if this is a non-local port or if this is
423 * an hvm domain and an event is pending (hvm domains don't have
424 * their own implementation of irq_enable). */
425 if (do_hypercall) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700426 struct evtchn_unmask unmask = { .port = port };
427 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
428 } else {
Christoph Lameter780f36d2010-12-06 11:16:29 -0600429 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700430
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700431 /*
432 * The following is basically the equivalent of
433 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
434 * the interrupt edge' if the channel is masked.
435 */
Stefano Stabellinib5e57922012-08-22 17:20:11 +0100436 if (evtchn_pending &&
Ian Campbellc81611c2013-02-20 11:48:06 +0000437 !sync_test_and_set_bit(port / BITS_PER_EVTCHN_WORD,
438 BM(&vcpu_info->evtchn_pending_sel)))
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700439 vcpu_info->evtchn_upcall_pending = 1;
440 }
441
442 put_cpu();
443}
444
Ian Campbell6cb65372011-03-10 16:08:11 +0000445static void xen_irq_init(unsigned irq)
446{
447 struct irq_info *info;
Konrad Rzeszutek Wilkb5328cd2011-06-15 14:24:29 -0400448#ifdef CONFIG_SMP
Ian Campbell6cb65372011-03-10 16:08:11 +0000449 struct irq_desc *desc = irq_to_desc(irq);
450
451 /* By default all event channels notify CPU#0. */
452 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
Konrad Rzeszutek Wilk44626e42011-03-15 16:40:33 -0400453#endif
Ian Campbell6cb65372011-03-10 16:08:11 +0000454
Ian Campbellca62ce82011-03-10 16:08:12 +0000455 info = kzalloc(sizeof(*info), GFP_KERNEL);
456 if (info == NULL)
457 panic("Unable to allocate metadata for IRQ%d\n", irq);
Ian Campbell6cb65372011-03-10 16:08:11 +0000458
459 info->type = IRQT_UNBOUND;
Daniel De Graaf420eb552011-10-27 17:58:47 -0400460 info->refcnt = -1;
Ian Campbell6cb65372011-03-10 16:08:11 +0000461
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100462 irq_set_handler_data(irq, info);
Ian Campbellca62ce82011-03-10 16:08:12 +0000463
Ian Campbell6cb65372011-03-10 16:08:11 +0000464 list_add_tail(&info->list, &xen_irq_list_head);
465}
466
Ian Campbell7bee9762011-03-10 16:08:15 +0000467static int __must_check xen_allocate_irq_dynamic(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700468{
Ian Campbell89911502011-03-03 11:57:44 -0500469 int first = 0;
470 int irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700471
Ian Campbell89911502011-03-03 11:57:44 -0500472#ifdef CONFIG_X86_IO_APIC
473 /*
474 * For an HVM guest or domain 0 which see "real" (emulated or
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300475 * actual respectively) GSIs we allocate dynamic IRQs
Ian Campbell89911502011-03-03 11:57:44 -0500476 * e.g. those corresponding to event channels or MSIs
477 * etc. from the range above those "real" GSIs to avoid
478 * collisions.
Konrad Rzeszutek Wilkd1b758e2010-12-09 14:53:29 -0500479 */
Ian Campbell89911502011-03-03 11:57:44 -0500480 if (xen_initial_domain() || xen_hvm_domain())
481 first = get_nr_irqs_gsi();
482#endif
483
Ian Campbell89911502011-03-03 11:57:44 -0500484 irq = irq_alloc_desc_from(first, -1);
485
Konrad Rzeszutek Wilke6599222011-09-29 13:26:45 -0400486 if (irq >= 0)
487 xen_irq_init(irq);
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800488
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700489 return irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400490}
491
Ian Campbell7bee9762011-03-10 16:08:15 +0000492static int __must_check xen_allocate_irq_gsi(unsigned gsi)
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000493{
494 int irq;
495
Ian Campbell89911502011-03-03 11:57:44 -0500496 /*
497 * A PV guest has no concept of a GSI (since it has no ACPI
498 * nor access to/knowledge of the physical APICs). Therefore
499 * all IRQs are dynamically allocated from the entire IRQ
500 * space.
501 */
502 if (xen_pv_domain() && !xen_initial_domain())
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000503 return xen_allocate_irq_dynamic();
504
505 /* Legacy IRQ descriptors are already allocated by the arch. */
506 if (gsi < NR_IRQS_LEGACY)
Ian Campbell6cb65372011-03-10 16:08:11 +0000507 irq = gsi;
508 else
509 irq = irq_alloc_desc_at(gsi, -1);
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000510
Ian Campbell6cb65372011-03-10 16:08:11 +0000511 xen_irq_init(irq);
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000512
513 return irq;
514}
515
516static void xen_free_irq(unsigned irq)
517{
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100518 struct irq_info *info = irq_get_handler_data(irq);
Ian Campbell6cb65372011-03-10 16:08:11 +0000519
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -0400520 if (WARN_ON(!info))
521 return;
522
Ian Campbell6cb65372011-03-10 16:08:11 +0000523 list_del(&info->list);
Ian Campbell9158c352011-03-10 16:08:09 +0000524
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100525 irq_set_handler_data(irq, NULL);
Ian Campbellca62ce82011-03-10 16:08:12 +0000526
Daniel De Graaf420eb552011-10-27 17:58:47 -0400527 WARN_ON(info->refcnt > 0);
528
Ian Campbellca62ce82011-03-10 16:08:12 +0000529 kfree(info);
530
Ian Campbell72146102011-02-03 09:49:35 +0000531 /* Legacy IRQ descriptors are managed by the arch. */
532 if (irq < NR_IRQS_LEGACY)
533 return;
534
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000535 irq_free_desc(irq);
536}
537
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400538static void pirq_query_unmask(int irq)
539{
540 struct physdev_irq_status_query irq_status;
541 struct irq_info *info = info_for_irq(irq);
542
543 BUG_ON(info->type != IRQT_PIRQ);
544
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100545 irq_status.irq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400546 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
547 irq_status.flags = 0;
548
549 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
550 if (irq_status.flags & XENIRQSTAT_needs_eoi)
551 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
552}
553
554static bool probing_irq(int irq)
555{
556 struct irq_desc *desc = irq_to_desc(irq);
557
558 return desc && desc->action == NULL;
559}
560
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100561static void eoi_pirq(struct irq_data *data)
562{
563 int evtchn = evtchn_from_irq(data->irq);
564 struct physdev_eoi eoi = { .irq = pirq_from_irq(data->irq) };
565 int rc = 0;
566
567 irq_move_irq(data);
568
569 if (VALID_EVTCHN(evtchn))
570 clear_evtchn(evtchn);
571
572 if (pirq_needs_eoi(data->irq)) {
573 rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
574 WARN_ON(rc);
575 }
576}
577
578static void mask_ack_pirq(struct irq_data *data)
579{
580 disable_dynirq(data);
581 eoi_pirq(data);
582}
583
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000584static unsigned int __startup_pirq(unsigned int irq)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400585{
586 struct evtchn_bind_pirq bind_pirq;
587 struct irq_info *info = info_for_irq(irq);
588 int evtchn = evtchn_from_irq(irq);
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400589 int rc;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400590
591 BUG_ON(info->type != IRQT_PIRQ);
592
593 if (VALID_EVTCHN(evtchn))
594 goto out;
595
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100596 bind_pirq.pirq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400597 /* NB. We are happy to share unless we are probing. */
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400598 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
599 BIND_PIRQ__WILL_SHARE : 0;
600 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
601 if (rc != 0) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400602 if (!probing_irq(irq))
603 printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
604 irq);
605 return 0;
606 }
607 evtchn = bind_pirq.port;
608
609 pirq_query_unmask(irq);
610
611 evtchn_to_irq[evtchn] = irq;
612 bind_evtchn_to_cpu(evtchn, 0);
613 info->evtchn = evtchn;
614
615out:
616 unmask_evtchn(evtchn);
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100617 eoi_pirq(irq_get_irq_data(irq));
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400618
619 return 0;
620}
621
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000622static unsigned int startup_pirq(struct irq_data *data)
623{
624 return __startup_pirq(data->irq);
625}
626
627static void shutdown_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400628{
629 struct evtchn_close close;
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000630 unsigned int irq = data->irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400631 struct irq_info *info = info_for_irq(irq);
632 int evtchn = evtchn_from_irq(irq);
633
634 BUG_ON(info->type != IRQT_PIRQ);
635
636 if (!VALID_EVTCHN(evtchn))
637 return;
638
639 mask_evtchn(evtchn);
640
641 close.port = evtchn;
642 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
643 BUG();
644
645 bind_evtchn_to_cpu(evtchn, 0);
646 evtchn_to_irq[evtchn] = -1;
647 info->evtchn = 0;
648}
649
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000650static void enable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400651{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000652 startup_pirq(data);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400653}
654
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000655static void disable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400656{
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100657 disable_dynirq(data);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400658}
659
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100660int xen_irq_from_gsi(unsigned gsi)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400661{
Ian Campbell6cb65372011-03-10 16:08:11 +0000662 struct irq_info *info;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400663
Ian Campbell6cb65372011-03-10 16:08:11 +0000664 list_for_each_entry(info, &xen_irq_list_head, list) {
665 if (info->type != IRQT_PIRQ)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400666 continue;
667
Ian Campbell6cb65372011-03-10 16:08:11 +0000668 if (info->u.pirq.gsi == gsi)
669 return info->irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400670 }
671
672 return -1;
673}
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100674EXPORT_SYMBOL_GPL(xen_irq_from_gsi);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400675
Ian Campbell653378a2011-03-10 16:08:04 +0000676/*
677 * Do not make any assumptions regarding the relationship between the
678 * IRQ number returned here and the Xen pirq argument.
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100679 *
680 * Note: We don't assign an event channel until the irq actually started
681 * up. Return an existing irq if we've already got one for the gsi.
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100682 *
683 * Shareable implies level triggered, not shareable implies edge
684 * triggered here.
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400685 */
Ian Campbellf4d06352011-03-10 16:08:07 +0000686int xen_bind_pirq_gsi_to_irq(unsigned gsi,
687 unsigned pirq, int shareable, char *name)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400688{
Ian Campbella0e18112011-03-10 16:08:03 +0000689 int irq = -1;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400690 struct physdev_irq irq_op;
691
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400692 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400693
Stefano Stabellini68c2c392012-05-21 16:54:10 +0100694 irq = xen_irq_from_gsi(gsi);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400695 if (irq != -1) {
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100696 printk(KERN_INFO "xen_map_pirq_gsi: returning irq %d for gsi %u\n",
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400697 irq, gsi);
Daniel De Graaf420eb552011-10-27 17:58:47 -0400698 goto out;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400699 }
700
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000701 irq = xen_allocate_irq_gsi(gsi);
Ian Campbell7bee9762011-03-10 16:08:15 +0000702 if (irq < 0)
703 goto out;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400704
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400705 irq_op.irq = irq;
Alex Nixonb5401a92010-03-18 16:31:34 -0400706 irq_op.vector = 0;
707
708 /* Only the privileged domain can do this. For non-priv, the pcifront
709 * driver provides a PCI bus that does the call to do exactly
710 * this in the priv domain. */
711 if (xen_initial_domain() &&
712 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000713 xen_free_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400714 irq = -ENOSPC;
715 goto out;
716 }
717
Jan Beulichdec02de2013-04-03 15:52:50 +0100718 xen_irq_info_pirq_init(irq, 0, pirq, gsi, DOMID_SELF,
Ian Campbell9158c352011-03-10 16:08:09 +0000719 shareable ? PIRQ_SHAREABLE : 0);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400720
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100721 pirq_query_unmask(irq);
722 /* We try to use the handler with the appropriate semantic for the
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100723 * type of interrupt: if the interrupt is an edge triggered
724 * interrupt we use handle_edge_irq.
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100725 *
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100726 * On the other hand if the interrupt is level triggered we use
727 * handle_fasteoi_irq like the native code does for this kind of
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100728 * interrupts.
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100729 *
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100730 * Depending on the Xen version, pirq_needs_eoi might return true
731 * not only for level triggered interrupts but for edge triggered
732 * interrupts too. In any case Xen always honors the eoi mechanism,
733 * not injecting any more pirqs of the same kind if the first one
734 * hasn't received an eoi yet. Therefore using the fasteoi handler
735 * is the right choice either way.
736 */
Stefano Stabellinie5ac0bd2011-05-25 12:33:23 +0100737 if (shareable)
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100738 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
739 handle_fasteoi_irq, name);
740 else
741 irq_set_chip_and_handler_name(irq, &xen_pirq_chip,
742 handle_edge_irq, name);
743
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400744out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400745 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400746
747 return irq;
748}
749
Qing Hef731e3ef2010-10-11 15:30:09 +0100750#ifdef CONFIG_PCI_MSI
Ian Campbellbf480d92011-02-18 16:43:32 +0000751int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000752{
Ian Campbell5cad61a2011-02-18 16:43:31 +0000753 int rc;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000754 struct physdev_get_free_pirq op_get_free_pirq;
Ian Campbell5cad61a2011-02-18 16:43:31 +0000755
Ian Campbellbf480d92011-02-18 16:43:32 +0000756 op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000757 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000758
Ian Campbell5cad61a2011-02-18 16:43:31 +0000759 WARN_ONCE(rc == -ENOSYS,
760 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
761
762 return rc ? -1 : op_get_free_pirq.pirq;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000763}
764
Ian Campbellbf480d92011-02-18 16:43:32 +0000765int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
Jan Beulichdec02de2013-04-03 15:52:50 +0100766 int pirq, const char *name, domid_t domid)
Stefano Stabellini809f9262010-07-01 17:10:39 +0100767{
Ian Campbellbf480d92011-02-18 16:43:32 +0000768 int irq, ret;
Ian Campbell4b41df72011-02-18 16:43:29 +0000769
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400770 mutex_lock(&irq_mapping_update_lock);
Stefano Stabellini809f9262010-07-01 17:10:39 +0100771
Ian Campbell4b41df72011-02-18 16:43:29 +0000772 irq = xen_allocate_irq_dynamic();
Konrad Rzeszutek Wilke6599222011-09-29 13:26:45 -0400773 if (irq < 0)
Ian Campbellbb5d0792011-02-18 16:43:28 +0000774 goto out;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100775
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100776 irq_set_chip_and_handler_name(irq, &xen_pirq_chip, handle_edge_irq,
777 name);
Stefano Stabellini809f9262010-07-01 17:10:39 +0100778
Jan Beulichdec02de2013-04-03 15:52:50 +0100779 xen_irq_info_pirq_init(irq, 0, pirq, 0, domid, 0);
Linus Torvalds5f6fb452011-03-15 19:23:40 -0700780 ret = irq_set_msi_desc(irq, msidesc);
Ian Campbellbf480d92011-02-18 16:43:32 +0000781 if (ret < 0)
782 goto error_irq;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100783out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400784 mutex_unlock(&irq_mapping_update_lock);
Ian Campbell4b41df72011-02-18 16:43:29 +0000785 return irq;
Ian Campbellbf480d92011-02-18 16:43:32 +0000786error_irq:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400787 mutex_unlock(&irq_mapping_update_lock);
Ian Campbellbf480d92011-02-18 16:43:32 +0000788 xen_free_irq(irq);
Konrad Rzeszutek Wilke6599222011-09-29 13:26:45 -0400789 return ret;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100790}
Qing Hef731e3ef2010-10-11 15:30:09 +0100791#endif
792
Alex Nixonb5401a92010-03-18 16:31:34 -0400793int xen_destroy_irq(int irq)
794{
795 struct irq_desc *desc;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100796 struct physdev_unmap_pirq unmap_irq;
797 struct irq_info *info = info_for_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400798 int rc = -ENOENT;
799
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400800 mutex_lock(&irq_mapping_update_lock);
Alex Nixonb5401a92010-03-18 16:31:34 -0400801
802 desc = irq_to_desc(irq);
803 if (!desc)
804 goto out;
805
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100806 if (xen_initial_domain()) {
Konrad Rzeszutek Wilk12334712010-11-19 11:27:09 -0500807 unmap_irq.pirq = info->u.pirq.pirq;
Konrad Rzeszutek Wilkbeafbdc2011-04-14 11:17:36 -0400808 unmap_irq.domid = info->u.pirq.domid;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100809 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
Konrad Rzeszutek Wilk1eff1ad2011-02-16 16:26:44 -0500810 /* If another domain quits without making the pci_disable_msix
811 * call, the Xen hypervisor takes care of freeing the PIRQs
812 * (free_domain_pirqs).
813 */
814 if ((rc == -ESRCH && info->u.pirq.domid != DOMID_SELF))
815 printk(KERN_INFO "domain %d does not have %d anymore\n",
816 info->u.pirq.domid, info->u.pirq.pirq);
817 else if (rc) {
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100818 printk(KERN_WARNING "unmap irq failed %d\n", rc);
819 goto out;
820 }
821 }
Alex Nixonb5401a92010-03-18 16:31:34 -0400822
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000823 xen_free_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400824
825out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400826 mutex_unlock(&irq_mapping_update_lock);
Alex Nixonb5401a92010-03-18 16:31:34 -0400827 return rc;
828}
829
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000830int xen_irq_from_pirq(unsigned pirq)
831{
Ian Campbell69c358c2011-03-10 16:08:13 +0000832 int irq;
833
834 struct irq_info *info;
835
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400836 mutex_lock(&irq_mapping_update_lock);
Ian Campbell69c358c2011-03-10 16:08:13 +0000837
838 list_for_each_entry(info, &xen_irq_list_head, list) {
Konrad Rzeszutek Wilk9bb9efe2011-09-29 13:13:30 -0400839 if (info->type != IRQT_PIRQ)
Ian Campbell69c358c2011-03-10 16:08:13 +0000840 continue;
841 irq = info->irq;
842 if (info->u.pirq.pirq == pirq)
843 goto out;
844 }
845 irq = -1;
846out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400847 mutex_unlock(&irq_mapping_update_lock);
Ian Campbell69c358c2011-03-10 16:08:13 +0000848
849 return irq;
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000850}
851
Konrad Rzeszutek Wilke6197ac2011-02-24 14:20:12 -0500852
853int xen_pirq_from_irq(unsigned irq)
854{
855 return pirq_from_irq(irq);
856}
857EXPORT_SYMBOL_GPL(xen_pirq_from_irq);
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700858int bind_evtchn_to_irq(unsigned int evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700859{
860 int irq;
861
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400862 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700863
864 irq = evtchn_to_irq[evtchn];
865
866 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000867 irq = xen_allocate_irq_dynamic();
Wei Liu68ba45f2013-01-31 14:46:56 +0000868 if (irq < 0)
Ian Campbell7bee9762011-03-10 16:08:15 +0000869 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700870
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100871 irq_set_chip_and_handler_name(irq, &xen_dynamic_chip,
Stefano Stabellini7e186bd2011-05-06 12:27:50 +0100872 handle_edge_irq, "event");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700873
Ian Campbell9158c352011-03-10 16:08:09 +0000874 xen_irq_info_evtchn_init(irq, evtchn);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400875 } else {
876 struct irq_info *info = info_for_irq(irq);
877 WARN_ON(info == NULL || info->type != IRQT_EVTCHN);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700878 }
879
Ian Campbell7bee9762011-03-10 16:08:15 +0000880out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400881 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700882
883 return irq;
884}
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700885EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700886
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700887static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
888{
889 struct evtchn_bind_ipi bind_ipi;
890 int evtchn, irq;
891
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400892 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700893
894 irq = per_cpu(ipi_to_irq, cpu)[ipi];
Ian Campbell90af9512009-02-06 16:55:58 -0800895
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700896 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000897 irq = xen_allocate_irq_dynamic();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700898 if (irq < 0)
899 goto out;
900
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100901 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700902 handle_percpu_irq, "ipi");
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700903
904 bind_ipi.vcpu = cpu;
905 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
906 &bind_ipi) != 0)
907 BUG();
908 evtchn = bind_ipi.port;
909
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000910 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700911
912 bind_evtchn_to_cpu(evtchn, cpu);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400913 } else {
914 struct irq_info *info = info_for_irq(irq);
915 WARN_ON(info == NULL || info->type != IRQT_IPI);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700916 }
917
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700918 out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400919 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700920 return irq;
921}
922
Ian Campbell2e820f52009-02-09 12:05:50 -0800923static int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
924 unsigned int remote_port)
925{
926 struct evtchn_bind_interdomain bind_interdomain;
927 int err;
928
929 bind_interdomain.remote_dom = remote_domain;
930 bind_interdomain.remote_port = remote_port;
931
932 err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
933 &bind_interdomain);
934
935 return err ? : bind_evtchn_to_irq(bind_interdomain.local_port);
936}
937
Olaf Hering62cc5fc2011-08-25 18:30:48 +0200938static int find_virq(unsigned int virq, unsigned int cpu)
939{
940 struct evtchn_status status;
941 int port, rc = -ENOENT;
942
943 memset(&status, 0, sizeof(status));
944 for (port = 0; port <= NR_EVENT_CHANNELS; port++) {
945 status.dom = DOMID_SELF;
946 status.port = port;
947 rc = HYPERVISOR_event_channel_op(EVTCHNOP_status, &status);
948 if (rc < 0)
949 continue;
950 if (status.status != EVTCHNSTAT_virq)
951 continue;
952 if (status.u.virq == virq && status.vcpu == cpu) {
953 rc = port;
954 break;
955 }
956 }
957 return rc;
958}
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700959
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100960int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700961{
962 struct evtchn_bind_virq bind_virq;
Olaf Hering62cc5fc2011-08-25 18:30:48 +0200963 int evtchn, irq, ret;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700964
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400965 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700966
967 irq = per_cpu(virq_to_irq, cpu)[virq];
968
969 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000970 irq = xen_allocate_irq_dynamic();
Wei Liu68ba45f2013-01-31 14:46:56 +0000971 if (irq < 0)
Ian Campbell7bee9762011-03-10 16:08:15 +0000972 goto out;
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700973
Thomas Gleixnerc442b802011-03-25 10:58:06 +0100974 irq_set_chip_and_handler_name(irq, &xen_percpu_chip,
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700975 handle_percpu_irq, "virq");
976
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700977 bind_virq.virq = virq;
978 bind_virq.vcpu = cpu;
Olaf Hering62cc5fc2011-08-25 18:30:48 +0200979 ret = HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
980 &bind_virq);
981 if (ret == 0)
982 evtchn = bind_virq.port;
983 else {
984 if (ret == -EEXIST)
985 ret = find_virq(virq, cpu);
986 BUG_ON(ret < 0);
987 evtchn = ret;
988 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700989
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000990 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700991
992 bind_evtchn_to_cpu(evtchn, cpu);
Konrad Rzeszutek Wilk5e152e62012-05-23 13:28:44 -0400993 } else {
994 struct irq_info *info = info_for_irq(irq);
995 WARN_ON(info == NULL || info->type != IRQT_VIRQ);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700996 }
997
Ian Campbell7bee9762011-03-10 16:08:15 +0000998out:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -0400999 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001000
1001 return irq;
1002}
1003
1004static void unbind_from_irq(unsigned int irq)
1005{
1006 struct evtchn_close close;
1007 int evtchn = evtchn_from_irq(irq);
Daniel De Graaf420eb552011-10-27 17:58:47 -04001008 struct irq_info *info = irq_get_handler_data(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001009
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -04001010 if (WARN_ON(!info))
1011 return;
1012
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001013 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001014
Daniel De Graaf420eb552011-10-27 17:58:47 -04001015 if (info->refcnt > 0) {
1016 info->refcnt--;
1017 if (info->refcnt != 0)
1018 goto done;
1019 }
1020
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001021 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001022 close.port = evtchn;
1023 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
1024 BUG();
1025
1026 switch (type_from_irq(irq)) {
1027 case IRQT_VIRQ:
1028 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001029 [virq_from_irq(irq)] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001030 break;
Alex Nixond68d82a2008-08-22 11:52:15 +01001031 case IRQT_IPI:
1032 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001033 [ipi_from_irq(irq)] = -1;
Alex Nixond68d82a2008-08-22 11:52:15 +01001034 break;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001035 default:
1036 break;
1037 }
1038
1039 /* Closed ports are implicitly re-bound to VCPU0. */
1040 bind_evtchn_to_cpu(evtchn, 0);
1041
1042 evtchn_to_irq[evtchn] = -1;
Ian Campbellfed5ea82009-12-01 16:15:30 +00001043 }
1044
Ian Campbellca62ce82011-03-10 16:08:12 +00001045 BUG_ON(info_for_irq(irq)->type == IRQT_UNBOUND);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001046
Ian Campbell9158c352011-03-10 16:08:09 +00001047 xen_free_irq(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001048
Daniel De Graaf420eb552011-10-27 17:58:47 -04001049 done:
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001050 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001051}
1052
1053int bind_evtchn_to_irqhandler(unsigned int evtchn,
Jeff Garzik7c239972007-10-19 03:12:20 -04001054 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001055 unsigned long irqflags,
1056 const char *devname, void *dev_id)
1057{
Nicolas Kaiser361ae8c2011-03-30 21:14:26 +02001058 int irq, retval;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001059
1060 irq = bind_evtchn_to_irq(evtchn);
Ian Campbell7bee9762011-03-10 16:08:15 +00001061 if (irq < 0)
1062 return irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001063 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1064 if (retval != 0) {
1065 unbind_from_irq(irq);
1066 return retval;
1067 }
1068
1069 return irq;
1070}
1071EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
1072
Ian Campbell2e820f52009-02-09 12:05:50 -08001073int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
1074 unsigned int remote_port,
1075 irq_handler_t handler,
1076 unsigned long irqflags,
1077 const char *devname,
1078 void *dev_id)
1079{
1080 int irq, retval;
1081
1082 irq = bind_interdomain_evtchn_to_irq(remote_domain, remote_port);
1083 if (irq < 0)
1084 return irq;
1085
1086 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1087 if (retval != 0) {
1088 unbind_from_irq(irq);
1089 return retval;
1090 }
1091
1092 return irq;
1093}
1094EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler);
1095
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001096int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
Jeff Garzik7c239972007-10-19 03:12:20 -04001097 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001098 unsigned long irqflags, const char *devname, void *dev_id)
1099{
Nicolas Kaiser361ae8c2011-03-30 21:14:26 +02001100 int irq, retval;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001101
1102 irq = bind_virq_to_irq(virq, cpu);
Ian Campbell7bee9762011-03-10 16:08:15 +00001103 if (irq < 0)
1104 return irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001105 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1106 if (retval != 0) {
1107 unbind_from_irq(irq);
1108 return retval;
1109 }
1110
1111 return irq;
1112}
1113EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
1114
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001115int bind_ipi_to_irqhandler(enum ipi_vector ipi,
1116 unsigned int cpu,
1117 irq_handler_t handler,
1118 unsigned long irqflags,
1119 const char *devname,
1120 void *dev_id)
1121{
1122 int irq, retval;
1123
1124 irq = bind_ipi_to_irq(ipi, cpu);
1125 if (irq < 0)
1126 return irq;
1127
Ian Campbell9bab0b72011-10-03 15:37:00 +01001128 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME | IRQF_EARLY_RESUME;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001129 retval = request_irq(irq, handler, irqflags, devname, dev_id);
1130 if (retval != 0) {
1131 unbind_from_irq(irq);
1132 return retval;
1133 }
1134
1135 return irq;
1136}
1137
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001138void unbind_from_irqhandler(unsigned int irq, void *dev_id)
1139{
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -04001140 struct irq_info *info = irq_get_handler_data(irq);
1141
1142 if (WARN_ON(!info))
1143 return;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001144 free_irq(irq, dev_id);
1145 unbind_from_irq(irq);
1146}
1147EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
1148
Daniel De Graaf420eb552011-10-27 17:58:47 -04001149int evtchn_make_refcounted(unsigned int evtchn)
1150{
1151 int irq = evtchn_to_irq[evtchn];
1152 struct irq_info *info;
1153
1154 if (irq == -1)
1155 return -ENOENT;
1156
1157 info = irq_get_handler_data(irq);
1158
1159 if (!info)
1160 return -ENOENT;
1161
1162 WARN_ON(info->refcnt != -1);
1163
1164 info->refcnt = 1;
1165
1166 return 0;
1167}
1168EXPORT_SYMBOL_GPL(evtchn_make_refcounted);
1169
1170int evtchn_get(unsigned int evtchn)
1171{
1172 int irq;
1173 struct irq_info *info;
1174 int err = -ENOENT;
1175
Daniel De Graafc3b3f162011-11-28 11:49:09 -05001176 if (evtchn >= NR_EVENT_CHANNELS)
1177 return -EINVAL;
1178
Daniel De Graaf420eb552011-10-27 17:58:47 -04001179 mutex_lock(&irq_mapping_update_lock);
1180
1181 irq = evtchn_to_irq[evtchn];
1182 if (irq == -1)
1183 goto done;
1184
1185 info = irq_get_handler_data(irq);
1186
1187 if (!info)
1188 goto done;
1189
1190 err = -EINVAL;
1191 if (info->refcnt <= 0)
1192 goto done;
1193
1194 info->refcnt++;
1195 err = 0;
1196 done:
1197 mutex_unlock(&irq_mapping_update_lock);
1198
1199 return err;
1200}
1201EXPORT_SYMBOL_GPL(evtchn_get);
1202
1203void evtchn_put(unsigned int evtchn)
1204{
1205 int irq = evtchn_to_irq[evtchn];
1206 if (WARN_ON(irq == -1))
1207 return;
1208 unbind_from_irq(irq);
1209}
1210EXPORT_SYMBOL_GPL(evtchn_put);
1211
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001212void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
1213{
1214 int irq = per_cpu(ipi_to_irq, cpu)[vector];
1215 BUG_ON(irq < 0);
1216 notify_remote_via_irq(irq);
1217}
1218
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001219irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
1220{
1221 struct shared_info *sh = HYPERVISOR_shared_info;
1222 int cpu = smp_processor_id();
Ian Campbellc81611c2013-02-20 11:48:06 +00001223 xen_ulong_t *cpu_evtchn = per_cpu(cpu_evtchn_mask, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001224 int i;
1225 unsigned long flags;
1226 static DEFINE_SPINLOCK(debug_lock);
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001227 struct vcpu_info *v;
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001228
1229 spin_lock_irqsave(&debug_lock, flags);
1230
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001231 printk("\nvcpu %d\n ", cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001232
1233 for_each_online_cpu(i) {
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001234 int pending;
1235 v = per_cpu(xen_vcpu, i);
1236 pending = (get_irq_regs() && i == cpu)
1237 ? xen_irqs_disabled(get_irq_regs())
1238 : v->evtchn_upcall_mask;
Ian Campbellc81611c2013-02-20 11:48:06 +00001239 printk("%d: masked=%d pending=%d event_sel %0*"PRI_xen_ulong"\n ", i,
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001240 pending, v->evtchn_upcall_pending,
1241 (int)(sizeof(v->evtchn_pending_sel)*2),
1242 v->evtchn_pending_sel);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001243 }
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001244 v = per_cpu(xen_vcpu, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001245
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001246 printk("\npending:\n ");
1247 for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
Ian Campbellc81611c2013-02-20 11:48:06 +00001248 printk("%0*"PRI_xen_ulong"%s",
1249 (int)sizeof(sh->evtchn_pending[0])*2,
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001250 sh->evtchn_pending[i],
1251 i % 8 == 0 ? "\n " : " ");
1252 printk("\nglobal mask:\n ");
1253 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
Ian Campbellc81611c2013-02-20 11:48:06 +00001254 printk("%0*"PRI_xen_ulong"%s",
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001255 (int)(sizeof(sh->evtchn_mask[0])*2),
1256 sh->evtchn_mask[i],
1257 i % 8 == 0 ? "\n " : " ");
1258
1259 printk("\nglobally unmasked:\n ");
1260 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
Ian Campbellc81611c2013-02-20 11:48:06 +00001261 printk("%0*"PRI_xen_ulong"%s",
1262 (int)(sizeof(sh->evtchn_mask[0])*2),
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001263 sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
1264 i % 8 == 0 ? "\n " : " ");
1265
1266 printk("\nlocal cpu%d mask:\n ", cpu);
Ian Campbellc81611c2013-02-20 11:48:06 +00001267 for (i = (NR_EVENT_CHANNELS/BITS_PER_EVTCHN_WORD)-1; i >= 0; i--)
1268 printk("%0*"PRI_xen_ulong"%s", (int)(sizeof(cpu_evtchn[0])*2),
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001269 cpu_evtchn[i],
1270 i % 8 == 0 ? "\n " : " ");
1271
1272 printk("\nlocally unmasked:\n ");
1273 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) {
Ian Campbellc81611c2013-02-20 11:48:06 +00001274 xen_ulong_t pending = sh->evtchn_pending[i]
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001275 & ~sh->evtchn_mask[i]
1276 & cpu_evtchn[i];
Ian Campbellc81611c2013-02-20 11:48:06 +00001277 printk("%0*"PRI_xen_ulong"%s",
1278 (int)(sizeof(sh->evtchn_mask[0])*2),
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001279 pending, i % 8 == 0 ? "\n " : " ");
1280 }
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001281
1282 printk("\npending list:\n");
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001283 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
Ian Campbellc81611c2013-02-20 11:48:06 +00001284 if (sync_test_bit(i, BM(sh->evtchn_pending))) {
1285 int word_idx = i / BITS_PER_EVTCHN_WORD;
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001286 printk(" %d: event %d -> irq %d%s%s%s\n",
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001287 cpu_from_evtchn(i), i,
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001288 evtchn_to_irq[i],
Ian Campbellc81611c2013-02-20 11:48:06 +00001289 sync_test_bit(word_idx, BM(&v->evtchn_pending_sel))
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001290 ? "" : " l2-clear",
Ian Campbellc81611c2013-02-20 11:48:06 +00001291 !sync_test_bit(i, BM(sh->evtchn_mask))
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001292 ? "" : " globally-masked",
Ian Campbellc81611c2013-02-20 11:48:06 +00001293 sync_test_bit(i, BM(cpu_evtchn))
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001294 ? "" : " locally-masked");
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001295 }
1296 }
1297
1298 spin_unlock_irqrestore(&debug_lock, flags);
1299
1300 return IRQ_HANDLED;
1301}
1302
Tejun Heo245b2e72009-06-24 15:13:48 +09001303static DEFINE_PER_CPU(unsigned, xed_nesting_count);
Keir Fraserada68142011-03-03 10:01:11 +00001304static DEFINE_PER_CPU(unsigned int, current_word_idx);
1305static DEFINE_PER_CPU(unsigned int, current_bit_idx);
Tejun Heo245b2e72009-06-24 15:13:48 +09001306
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001307/*
Scott Rixnerab7f8632011-03-03 09:30:08 +00001308 * Mask out the i least significant bits of w
1309 */
Ian Campbellc81611c2013-02-20 11:48:06 +00001310#define MASK_LSBS(w, i) (w & ((~((xen_ulong_t)0UL)) << i))
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001311
1312/*
1313 * Search the CPUs pending events bitmasks. For each one found, map
1314 * the event number to an irq, and feed it into do_IRQ() for
1315 * handling.
1316 *
1317 * Xen uses a two-level bitmap to speed searching. The first level is
1318 * a bitset of words which contain pending event bits. The second
1319 * level is a bitset of pending events themselves.
1320 */
Sheng Yang38e20b02010-05-14 12:40:51 +01001321static void __xen_evtchn_do_upcall(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001322{
Keir Fraser24b51c22011-03-03 11:06:28 +00001323 int start_word_idx, start_bit_idx;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001324 int word_idx, bit_idx;
Keir Fraserbee980d2013-03-28 10:03:36 -04001325 int i, irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001326 int cpu = get_cpu();
1327 struct shared_info *s = HYPERVISOR_shared_info;
Christoph Lameter780f36d2010-12-06 11:16:29 -06001328 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
Ruslan Pisarev088c05a2011-07-26 14:16:13 +03001329 unsigned count;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001330
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001331 do {
Ian Campbellc81611c2013-02-20 11:48:06 +00001332 xen_ulong_t pending_words;
Keir Fraserbee980d2013-03-28 10:03:36 -04001333 xen_ulong_t pending_bits;
1334 struct irq_desc *desc;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001335
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001336 vcpu_info->evtchn_upcall_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001337
Christoph Lameterb2e4ae62010-12-06 11:40:07 -06001338 if (__this_cpu_inc_return(xed_nesting_count) - 1)
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001339 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001340
Ian Campbellc81611c2013-02-20 11:48:06 +00001341 /*
1342 * Master flag must be cleared /before/ clearing
1343 * selector flag. xchg_xen_ulong must contain an
1344 * appropriate barrier.
1345 */
Keir Fraserbee980d2013-03-28 10:03:36 -04001346 if ((irq = per_cpu(virq_to_irq, cpu)[VIRQ_TIMER]) != -1) {
1347 int evtchn = evtchn_from_irq(irq);
1348 word_idx = evtchn / BITS_PER_LONG;
1349 pending_bits = evtchn % BITS_PER_LONG;
1350 if (active_evtchns(cpu, s, word_idx) & (1ULL << pending_bits)) {
1351 desc = irq_to_desc(irq);
1352 if (desc)
1353 generic_handle_irq_desc(irq, desc);
1354 }
1355 }
1356
Ian Campbellc81611c2013-02-20 11:48:06 +00001357 pending_words = xchg_xen_ulong(&vcpu_info->evtchn_pending_sel, 0);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001358
Keir Fraser24b51c22011-03-03 11:06:28 +00001359 start_word_idx = __this_cpu_read(current_word_idx);
1360 start_bit_idx = __this_cpu_read(current_bit_idx);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001361
Keir Fraser24b51c22011-03-03 11:06:28 +00001362 word_idx = start_word_idx;
1363
1364 for (i = 0; pending_words != 0; i++) {
Ian Campbellc81611c2013-02-20 11:48:06 +00001365 xen_ulong_t words;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001366
Scott Rixnerab7f8632011-03-03 09:30:08 +00001367 words = MASK_LSBS(pending_words, word_idx);
1368
1369 /*
Keir Fraserada68142011-03-03 10:01:11 +00001370 * If we masked out all events, wrap to beginning.
Scott Rixnerab7f8632011-03-03 09:30:08 +00001371 */
1372 if (words == 0) {
Keir Fraserada68142011-03-03 10:01:11 +00001373 word_idx = 0;
1374 bit_idx = 0;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001375 continue;
1376 }
Ian Campbellc81611c2013-02-20 11:48:06 +00001377 word_idx = EVTCHN_FIRST_BIT(words);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001378
Keir Fraser24b51c22011-03-03 11:06:28 +00001379 pending_bits = active_evtchns(cpu, s, word_idx);
1380 bit_idx = 0; /* usually scan entire word from start */
1381 if (word_idx == start_word_idx) {
1382 /* We scan the starting word in two parts */
1383 if (i == 0)
1384 /* 1st time: start in the middle */
1385 bit_idx = start_bit_idx;
1386 else
1387 /* 2nd time: mask bits done already */
1388 bit_idx &= (1UL << start_bit_idx) - 1;
1389 }
1390
Scott Rixnerab7f8632011-03-03 09:30:08 +00001391 do {
Ian Campbellc81611c2013-02-20 11:48:06 +00001392 xen_ulong_t bits;
Keir Fraserbee980d2013-03-28 10:03:36 -04001393 int port;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001394
Scott Rixnerab7f8632011-03-03 09:30:08 +00001395 bits = MASK_LSBS(pending_bits, bit_idx);
1396
1397 /* If we masked out all events, move on. */
Keir Fraserada68142011-03-03 10:01:11 +00001398 if (bits == 0)
Scott Rixnerab7f8632011-03-03 09:30:08 +00001399 break;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001400
Ian Campbellc81611c2013-02-20 11:48:06 +00001401 bit_idx = EVTCHN_FIRST_BIT(bits);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001402
1403 /* Process port. */
Ian Campbellc81611c2013-02-20 11:48:06 +00001404 port = (word_idx * BITS_PER_EVTCHN_WORD) + bit_idx;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001405 irq = evtchn_to_irq[port];
1406
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -08001407 if (irq != -1) {
1408 desc = irq_to_desc(irq);
1409 if (desc)
1410 generic_handle_irq_desc(irq, desc);
1411 }
Scott Rixnerab7f8632011-03-03 09:30:08 +00001412
Ian Campbellc81611c2013-02-20 11:48:06 +00001413 bit_idx = (bit_idx + 1) % BITS_PER_EVTCHN_WORD;
Keir Fraserada68142011-03-03 10:01:11 +00001414
1415 /* Next caller starts at last processed + 1 */
1416 __this_cpu_write(current_word_idx,
1417 bit_idx ? word_idx :
Ian Campbellc81611c2013-02-20 11:48:06 +00001418 (word_idx+1) % BITS_PER_EVTCHN_WORD);
Keir Fraserada68142011-03-03 10:01:11 +00001419 __this_cpu_write(current_bit_idx, bit_idx);
1420 } while (bit_idx != 0);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001421
Keir Fraser24b51c22011-03-03 11:06:28 +00001422 /* Scan start_l1i twice; all others once. */
1423 if ((word_idx != start_word_idx) || (i != 0))
Scott Rixnerab7f8632011-03-03 09:30:08 +00001424 pending_words &= ~(1UL << word_idx);
Keir Fraserada68142011-03-03 10:01:11 +00001425
Ian Campbellc81611c2013-02-20 11:48:06 +00001426 word_idx = (word_idx + 1) % BITS_PER_EVTCHN_WORD;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001427 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001428
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001429 BUG_ON(!irqs_disabled());
1430
Christoph Lameter780f36d2010-12-06 11:16:29 -06001431 count = __this_cpu_read(xed_nesting_count);
1432 __this_cpu_write(xed_nesting_count, 0);
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001433 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001434
1435out:
Jeremy Fitzhardinge3445a8f2009-02-06 14:09:46 -08001436
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001437 put_cpu();
1438}
1439
Sheng Yang38e20b02010-05-14 12:40:51 +01001440void xen_evtchn_do_upcall(struct pt_regs *regs)
1441{
1442 struct pt_regs *old_regs = set_irq_regs(regs);
1443
Mojiong Qiu772aebc2012-11-06 16:08:15 +08001444 irq_enter();
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001445#ifdef CONFIG_X86
Sheng Yang38e20b02010-05-14 12:40:51 +01001446 exit_idle();
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001447#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001448
1449 __xen_evtchn_do_upcall();
1450
1451 irq_exit();
1452 set_irq_regs(old_regs);
1453}
1454
1455void xen_hvm_evtchn_do_upcall(void)
1456{
1457 __xen_evtchn_do_upcall();
1458}
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001459EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
Sheng Yang38e20b02010-05-14 12:40:51 +01001460
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001461/* Rebind a new event channel to an existing irq. */
1462void rebind_evtchn_irq(int evtchn, int irq)
1463{
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001464 struct irq_info *info = info_for_irq(irq);
1465
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -04001466 if (WARN_ON(!info))
1467 return;
1468
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001469 /* Make sure the irq is masked, since the new event channel
1470 will also be masked. */
1471 disable_irq(irq);
1472
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001473 mutex_lock(&irq_mapping_update_lock);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001474
1475 /* After resume the irq<->evtchn mappings are all cleared out */
1476 BUG_ON(evtchn_to_irq[evtchn] != -1);
1477 /* Expect irq to have been bound before,
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001478 so there should be a proper type */
1479 BUG_ON(info->type == IRQT_UNBOUND);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001480
Ian Campbell9158c352011-03-10 16:08:09 +00001481 xen_irq_info_evtchn_init(irq, evtchn);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001482
Konrad Rzeszutek Wilk77365942011-09-14 05:10:00 -04001483 mutex_unlock(&irq_mapping_update_lock);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001484
1485 /* new event channels are always bound to cpu 0 */
Rusty Russell0de26522008-12-13 21:20:26 +10301486 irq_set_affinity(irq, cpumask_of(0));
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001487
1488 /* Unmask the event channel. */
1489 enable_irq(irq);
1490}
1491
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001492/* Rebind an evtchn so that it gets delivered to a specific cpu */
Yinghai Lud5dedd42009-04-27 17:59:21 -07001493static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001494{
1495 struct evtchn_bind_vcpu bind_vcpu;
1496 int evtchn = evtchn_from_irq(irq);
1497
Ian Campbellbe494722011-03-10 16:08:02 +00001498 if (!VALID_EVTCHN(evtchn))
1499 return -1;
1500
1501 /*
1502 * Events delivered via platform PCI interrupts are always
1503 * routed to vcpu 0 and hence cannot be rebound.
1504 */
1505 if (xen_hvm_domain() && !xen_have_vector_callback)
Yinghai Lud5dedd42009-04-27 17:59:21 -07001506 return -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001507
1508 /* Send future instances of this interrupt to other vcpu. */
1509 bind_vcpu.port = evtchn;
1510 bind_vcpu.vcpu = tcpu;
1511
1512 /*
1513 * If this fails, it usually just indicates that we're dealing with a
1514 * virq or IPI channel, which don't actually need to be rebound. Ignore
1515 * it, but don't do the xenlinux-level rebind in that case.
1516 */
1517 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1518 bind_evtchn_to_cpu(evtchn, tcpu);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001519
1520 return 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001521}
1522
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001523static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1524 bool force)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001525{
Rusty Russell0de26522008-12-13 21:20:26 +10301526 unsigned tcpu = cpumask_first(dest);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001527
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001528 return rebind_irq_to_cpu(data->irq, tcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001529}
1530
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001531int resend_irq_on_evtchn(unsigned int irq)
1532{
1533 int masked, evtchn = evtchn_from_irq(irq);
1534 struct shared_info *s = HYPERVISOR_shared_info;
1535
1536 if (!VALID_EVTCHN(evtchn))
1537 return 1;
1538
Ian Campbellc81611c2013-02-20 11:48:06 +00001539 masked = sync_test_and_set_bit(evtchn, BM(s->evtchn_mask));
1540 sync_set_bit(evtchn, BM(s->evtchn_pending));
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001541 if (!masked)
1542 unmask_evtchn(evtchn);
1543
1544 return 1;
1545}
1546
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001547static void enable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001548{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001549 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001550
1551 if (VALID_EVTCHN(evtchn))
1552 unmask_evtchn(evtchn);
1553}
1554
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001555static void disable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001556{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001557 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001558
1559 if (VALID_EVTCHN(evtchn))
1560 mask_evtchn(evtchn);
1561}
1562
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001563static void ack_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001564{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001565 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001566
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001567 irq_move_irq(data);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001568
1569 if (VALID_EVTCHN(evtchn))
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001570 clear_evtchn(evtchn);
1571}
1572
1573static void mask_ack_dynirq(struct irq_data *data)
1574{
1575 disable_dynirq(data);
1576 ack_dynirq(data);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001577}
1578
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001579static int retrigger_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001580{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001581 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001582 struct shared_info *sh = HYPERVISOR_shared_info;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001583 int ret = 0;
1584
1585 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001586 int masked;
1587
Ian Campbellc81611c2013-02-20 11:48:06 +00001588 masked = sync_test_and_set_bit(evtchn, BM(sh->evtchn_mask));
1589 sync_set_bit(evtchn, BM(sh->evtchn_pending));
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001590 if (!masked)
1591 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001592 ret = 1;
1593 }
1594
1595 return ret;
1596}
1597
Ian Campbell0a852262011-03-10 16:08:06 +00001598static void restore_pirqs(void)
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001599{
1600 int pirq, rc, irq, gsi;
1601 struct physdev_map_pirq map_irq;
Ian Campbell69c358c2011-03-10 16:08:13 +00001602 struct irq_info *info;
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001603
Ian Campbell69c358c2011-03-10 16:08:13 +00001604 list_for_each_entry(info, &xen_irq_list_head, list) {
1605 if (info->type != IRQT_PIRQ)
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001606 continue;
1607
Ian Campbell69c358c2011-03-10 16:08:13 +00001608 pirq = info->u.pirq.pirq;
1609 gsi = info->u.pirq.gsi;
1610 irq = info->irq;
1611
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001612 /* save/restore of PT devices doesn't work, so at this point the
1613 * only devices present are GSI based emulated devices */
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001614 if (!gsi)
1615 continue;
1616
1617 map_irq.domid = DOMID_SELF;
1618 map_irq.type = MAP_PIRQ_TYPE_GSI;
1619 map_irq.index = gsi;
1620 map_irq.pirq = pirq;
1621
1622 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1623 if (rc) {
1624 printk(KERN_WARNING "xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1625 gsi, irq, pirq, rc);
Ian Campbell9158c352011-03-10 16:08:09 +00001626 xen_free_irq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001627 continue;
1628 }
1629
1630 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1631
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001632 __startup_pirq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001633 }
1634}
1635
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001636static void restore_cpu_virqs(unsigned int cpu)
1637{
1638 struct evtchn_bind_virq bind_virq;
1639 int virq, irq, evtchn;
1640
1641 for (virq = 0; virq < NR_VIRQS; virq++) {
1642 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1643 continue;
1644
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001645 BUG_ON(virq_from_irq(irq) != virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001646
1647 /* Get a new binding from Xen. */
1648 bind_virq.virq = virq;
1649 bind_virq.vcpu = cpu;
1650 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1651 &bind_virq) != 0)
1652 BUG();
1653 evtchn = bind_virq.port;
1654
1655 /* Record the new mapping. */
Ian Campbell3d4cfa32011-03-10 16:08:10 +00001656 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001657 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001658 }
1659}
1660
1661static void restore_cpu_ipis(unsigned int cpu)
1662{
1663 struct evtchn_bind_ipi bind_ipi;
1664 int ipi, irq, evtchn;
1665
1666 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1667 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1668 continue;
1669
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001670 BUG_ON(ipi_from_irq(irq) != ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001671
1672 /* Get a new binding from Xen. */
1673 bind_ipi.vcpu = cpu;
1674 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1675 &bind_ipi) != 0)
1676 BUG();
1677 evtchn = bind_ipi.port;
1678
1679 /* Record the new mapping. */
Ian Campbell3d4cfa32011-03-10 16:08:10 +00001680 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001681 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001682 }
1683}
1684
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001685/* Clear an irq's pending state, in preparation for polling on it */
1686void xen_clear_irq_pending(int irq)
1687{
1688 int evtchn = evtchn_from_irq(irq);
1689
1690 if (VALID_EVTCHN(evtchn))
1691 clear_evtchn(evtchn);
1692}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001693EXPORT_SYMBOL(xen_clear_irq_pending);
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -07001694void xen_set_irq_pending(int irq)
1695{
1696 int evtchn = evtchn_from_irq(irq);
1697
1698 if (VALID_EVTCHN(evtchn))
1699 set_evtchn(evtchn);
1700}
1701
1702bool xen_test_irq_pending(int irq)
1703{
1704 int evtchn = evtchn_from_irq(irq);
1705 bool ret = false;
1706
1707 if (VALID_EVTCHN(evtchn))
1708 ret = test_evtchn(evtchn);
1709
1710 return ret;
1711}
1712
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001713/* Poll waiting for an irq to become pending with timeout. In the usual case,
1714 * the irq will be disabled so it won't deliver an interrupt. */
1715void xen_poll_irq_timeout(int irq, u64 timeout)
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001716{
1717 evtchn_port_t evtchn = evtchn_from_irq(irq);
1718
1719 if (VALID_EVTCHN(evtchn)) {
1720 struct sched_poll poll;
1721
1722 poll.nr_ports = 1;
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001723 poll.timeout = timeout;
Isaku Yamahataff3c5362008-10-14 17:50:44 -07001724 set_xen_guest_handle(poll.ports, &evtchn);
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001725
1726 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1727 BUG();
1728 }
1729}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001730EXPORT_SYMBOL(xen_poll_irq_timeout);
1731/* Poll waiting for an irq to become pending. In the usual case, the
1732 * irq will be disabled so it won't deliver an interrupt. */
1733void xen_poll_irq(int irq)
1734{
1735 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1736}
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001737
Konrad Rzeszutek Wilkc7c2c3a2010-11-08 14:26:36 -05001738/* Check whether the IRQ line is shared with other guests. */
1739int xen_test_irq_shared(int irq)
1740{
1741 struct irq_info *info = info_for_irq(irq);
Konrad Rzeszutek Wilk94032c52013-04-16 10:55:18 -04001742 struct physdev_irq_status_query irq_status;
1743
1744 if (WARN_ON(!info))
1745 return -ENOENT;
1746
1747 irq_status.irq = info->u.pirq.pirq;
Konrad Rzeszutek Wilkc7c2c3a2010-11-08 14:26:36 -05001748
1749 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
1750 return 0;
1751 return !(irq_status.flags & XENIRQSTAT_shared);
1752}
1753EXPORT_SYMBOL_GPL(xen_test_irq_shared);
1754
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001755void xen_irq_resume(void)
1756{
Ian Campbell6cb65372011-03-10 16:08:11 +00001757 unsigned int cpu, evtchn;
1758 struct irq_info *info;
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001759
1760 init_evtchn_cpu_bindings();
1761
1762 /* New event-channel space is not 'live' yet. */
1763 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1764 mask_evtchn(evtchn);
1765
1766 /* No IRQ <-> event-channel mappings. */
Ian Campbell6cb65372011-03-10 16:08:11 +00001767 list_for_each_entry(info, &xen_irq_list_head, list)
1768 info->evtchn = 0; /* zap event-channel binding */
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001769
1770 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1771 evtchn_to_irq[evtchn] = -1;
1772
1773 for_each_possible_cpu(cpu) {
1774 restore_cpu_virqs(cpu);
1775 restore_cpu_ipis(cpu);
1776 }
Ian Campbell69035912010-11-01 16:30:09 +00001777
Ian Campbell0a852262011-03-10 16:08:06 +00001778 restore_pirqs();
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001779}
1780
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001781static struct irq_chip xen_dynamic_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001782 .name = "xen-dyn",
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001783
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001784 .irq_disable = disable_dynirq,
1785 .irq_mask = disable_dynirq,
1786 .irq_unmask = enable_dynirq,
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001787
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001788 .irq_ack = ack_dynirq,
1789 .irq_mask_ack = mask_ack_dynirq,
1790
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001791 .irq_set_affinity = set_affinity_irq,
1792 .irq_retrigger = retrigger_dynirq,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001793};
1794
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001795static struct irq_chip xen_pirq_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001796 .name = "xen-pirq",
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001797
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001798 .irq_startup = startup_pirq,
1799 .irq_shutdown = shutdown_pirq,
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001800 .irq_enable = enable_pirq,
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001801 .irq_disable = disable_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001802
Stefano Stabellini7e186bd2011-05-06 12:27:50 +01001803 .irq_mask = disable_dynirq,
1804 .irq_unmask = enable_dynirq,
1805
1806 .irq_ack = eoi_pirq,
1807 .irq_eoi = eoi_pirq,
1808 .irq_mask_ack = mask_ack_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001809
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001810 .irq_set_affinity = set_affinity_irq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001811
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001812 .irq_retrigger = retrigger_dynirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001813};
1814
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001815static struct irq_chip xen_percpu_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001816 .name = "xen-percpu",
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001817
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001818 .irq_disable = disable_dynirq,
1819 .irq_mask = disable_dynirq,
1820 .irq_unmask = enable_dynirq,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001821
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001822 .irq_ack = ack_dynirq,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001823};
1824
Sheng Yang38e20b02010-05-14 12:40:51 +01001825int xen_set_callback_via(uint64_t via)
1826{
1827 struct xen_hvm_param a;
1828 a.domid = DOMID_SELF;
1829 a.index = HVM_PARAM_CALLBACK_IRQ;
1830 a.value = via;
1831 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1832}
1833EXPORT_SYMBOL_GPL(xen_set_callback_via);
1834
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001835#ifdef CONFIG_XEN_PVHVM
Sheng Yang38e20b02010-05-14 12:40:51 +01001836/* Vector callbacks are better than PCI interrupts to receive event
1837 * channel notifications because we can receive vector callbacks on any
1838 * vcpu and we don't need PCI support or APIC interactions. */
1839void xen_callback_vector(void)
1840{
1841 int rc;
1842 uint64_t callback_via;
1843 if (xen_have_vector_callback) {
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -08001844 callback_via = HVM_CALLBACK_VECTOR(HYPERVISOR_CALLBACK_VECTOR);
Sheng Yang38e20b02010-05-14 12:40:51 +01001845 rc = xen_set_callback_via(callback_via);
1846 if (rc) {
1847 printk(KERN_ERR "Request for Xen HVM callback vector"
1848 " failed.\n");
1849 xen_have_vector_callback = 0;
1850 return;
1851 }
1852 printk(KERN_INFO "Xen HVM callback vector for event delivery is "
1853 "enabled\n");
1854 /* in the restore case the vector has already been allocated */
K. Y. Srinivasanbc2b0332013-02-03 17:22:39 -08001855 if (!test_bit(HYPERVISOR_CALLBACK_VECTOR, used_vectors))
1856 alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR,
1857 xen_hvm_callback_vector);
Sheng Yang38e20b02010-05-14 12:40:51 +01001858 }
1859}
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001860#else
1861void xen_callback_vector(void) {}
1862#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001863
Stefano Stabellini2e3d8862012-10-02 15:57:57 +01001864void __init xen_init_IRQ(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001865{
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001866 int i;
Mike Travisc7a35892009-01-10 21:58:11 -08001867
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001868 evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
1869 GFP_KERNEL);
Konrad Rzeszutek Wilk9d093e22011-09-29 13:31:21 -04001870 BUG_ON(!evtchn_to_irq);
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001871 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1872 evtchn_to_irq[i] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001873
1874 init_evtchn_cpu_bindings();
1875
1876 /* No event channels are 'live' right now. */
1877 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1878 mask_evtchn(i);
1879
Stefano Stabellini9846ff12012-01-30 16:21:48 +00001880 pirq_needs_eoi = pirq_needs_eoi_flag;
1881
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001882#ifdef CONFIG_X86
Sheng Yang38e20b02010-05-14 12:40:51 +01001883 if (xen_hvm_domain()) {
1884 xen_callback_vector();
1885 native_init_IRQ();
Stefano Stabellini3942b742010-06-24 17:50:18 +01001886 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1887 * __acpi_register_gsi can point at the right function */
1888 pci_xen_hvm_init();
Sheng Yang38e20b02010-05-14 12:40:51 +01001889 } else {
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001890 int rc;
Stefano Stabellini9846ff12012-01-30 16:21:48 +00001891 struct physdev_pirq_eoi_gmfn eoi_gmfn;
1892
Sheng Yang38e20b02010-05-14 12:40:51 +01001893 irq_ctx_init(smp_processor_id());
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +01001894 if (xen_initial_domain())
Konrad Rzeszutek Wilka0ee0562011-06-09 09:49:13 -04001895 pci_xen_initial_domain();
Stefano Stabellini9846ff12012-01-30 16:21:48 +00001896
1897 pirq_eoi_map = (void *)__get_free_page(GFP_KERNEL|__GFP_ZERO);
1898 eoi_gmfn.gmfn = virt_to_mfn(pirq_eoi_map);
1899 rc = HYPERVISOR_physdev_op(PHYSDEVOP_pirq_eoi_gmfn_v2, &eoi_gmfn);
1900 if (rc != 0) {
1901 free_page((unsigned long) pirq_eoi_map);
1902 pirq_eoi_map = NULL;
1903 } else
1904 pirq_needs_eoi = pirq_check_eoi_map;
Sheng Yang38e20b02010-05-14 12:40:51 +01001905 }
Stefano Stabellini0ec53ec2012-09-14 13:37:32 +00001906#endif
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001907}