blob: 02b5a9c05cfab1fc32033f669ff3a2c912db3828 [file] [log] [blame]
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001/*
2 * Xen event channels
3 *
4 * Xen models interrupts with abstract event channels. Because each
5 * domain gets 1024 event channels, but NR_IRQ is not that large, we
6 * must dynamically map irqs<->event channels. The event channels
7 * interface with the rest of the kernel by defining a xen interrupt
8 * chip. When an event is recieved, it is mapped to an irq and sent
9 * through the normal interrupt processing path.
10 *
11 * There are four kinds of events which can be mapped to an event
12 * channel:
13 *
14 * 1. Inter-domain notifications. This includes all the virtual
15 * device events, since they're driven by front-ends in another domain
16 * (typically dom0).
17 * 2. VIRQs, typically used for timers. These are per-cpu events.
18 * 3. IPIs.
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
Sheng Yang38e20b02010-05-14 12:40:51 +010034#include <asm/desc.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070035#include <asm/ptrace.h>
36#include <asm/irq.h>
Jeremy Fitzhardinge792dc4f2009-02-06 14:09:43 -080037#include <asm/idle.h>
Konrad Rzeszutek Wilk0794bfc2010-10-18 10:41:08 -040038#include <asm/io_apic.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070039#include <asm/sync_bitops.h>
Stefano Stabellini42a1de52010-06-24 16:42:04 +010040#include <asm/xen/pci.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070041#include <asm/xen/hypercall.h>
Adrian Bunk8d1b8752007-07-20 00:31:44 -070042#include <asm/xen/hypervisor.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070043
Sheng Yang38e20b02010-05-14 12:40:51 +010044#include <xen/xen.h>
45#include <xen/hvm.h>
Isaku Yamahatae04d0d02008-04-02 10:53:55 -070046#include <xen/xen-ops.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070047#include <xen/events.h>
48#include <xen/interface/xen.h>
49#include <xen/interface/event_channel.h>
Sheng Yang38e20b02010-05-14 12:40:51 +010050#include <xen/interface/hvm/hvm_op.h>
51#include <xen/interface/hvm/params.h>
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070052
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070053/*
54 * This lock protects updates to the following mapping and reference-count
55 * arrays. The lock does not need to be acquired to read the mapping tables.
56 */
57static DEFINE_SPINLOCK(irq_mapping_update_lock);
58
Ian Campbell6cb65372011-03-10 16:08:11 +000059static LIST_HEAD(xen_irq_list_head);
60
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070061/* IRQ <-> VIRQ mapping. */
Tejun Heo204fba42009-06-24 15:13:45 +090062static DEFINE_PER_CPU(int [NR_VIRQS], virq_to_irq) = {[0 ... NR_VIRQS-1] = -1};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070063
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070064/* IRQ <-> IPI mapping */
Tejun Heo204fba42009-06-24 15:13:45 +090065static DEFINE_PER_CPU(int [XEN_NR_IPIS], ipi_to_irq) = {[0 ... XEN_NR_IPIS-1] = -1};
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070066
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080067/* Interrupt types. */
68enum xen_irq_type {
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -080069 IRQT_UNBOUND = 0,
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -070070 IRQT_PIRQ,
71 IRQT_VIRQ,
72 IRQT_IPI,
73 IRQT_EVTCHN
74};
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -070075
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080076/*
77 * Packed IRQ information:
78 * type - enum xen_irq_type
79 * event channel - irq->event channel mapping
80 * cpu - cpu this event channel is bound to
81 * index - type-specific information:
Stefano Stabellini42a1de52010-06-24 16:42:04 +010082 * PIRQ - vector, with MSB being "needs EIO", or physical IRQ of the HVM
83 * guest, or GSI (real passthrough IRQ) of the device.
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080084 * VIRQ - virq number
85 * IPI - IPI vector
86 * EVTCHN -
87 */
88struct irq_info
89{
Ian Campbell6cb65372011-03-10 16:08:11 +000090 struct list_head list;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080091 enum xen_irq_type type; /* type */
Ian Campbell6cb65372011-03-10 16:08:11 +000092 unsigned irq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -080093 unsigned short evtchn; /* event channel */
94 unsigned short cpu; /* cpu bound */
95
96 union {
97 unsigned short virq;
98 enum ipi_vector ipi;
99 struct {
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100100 unsigned short pirq;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800101 unsigned short gsi;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400102 unsigned char vector;
103 unsigned char flags;
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800104 } pirq;
105 } u;
106};
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400107#define PIRQ_NEEDS_EOI (1 << 0)
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400108#define PIRQ_SHAREABLE (1 << 1)
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800109
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -0400110static int *evtchn_to_irq;
Jeremy Fitzhardinge3b32f572009-08-13 12:50:37 -0700111
Ian Campbellcb60d112011-03-10 16:08:08 +0000112static DEFINE_PER_CPU(unsigned long [NR_EVENT_CHANNELS/BITS_PER_LONG],
113 cpu_evtchn_mask);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700114
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700115/* Xen will never allocate port zero for any purpose. */
116#define VALID_EVTCHN(chn) ((chn) != 0)
117
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700118static struct irq_chip xen_dynamic_chip;
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700119static struct irq_chip xen_percpu_chip;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400120static struct irq_chip xen_pirq_chip;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700121
Ian Campbell9158c352011-03-10 16:08:09 +0000122/* Get info for IRQ */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800123static struct irq_info *info_for_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700124{
Ian Campbellca62ce82011-03-10 16:08:12 +0000125 return get_irq_data(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700126}
127
Ian Campbell9158c352011-03-10 16:08:09 +0000128/* Constructors for packed IRQ information. */
129static void xen_irq_info_common_init(struct irq_info *info,
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000130 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000131 enum xen_irq_type type,
132 unsigned short evtchn,
133 unsigned short cpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700134{
Ian Campbell9158c352011-03-10 16:08:09 +0000135
136 BUG_ON(info->type != IRQT_UNBOUND && info->type != type);
137
138 info->type = type;
Ian Campbell6cb65372011-03-10 16:08:11 +0000139 info->irq = irq;
Ian Campbell9158c352011-03-10 16:08:09 +0000140 info->evtchn = evtchn;
141 info->cpu = cpu;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000142
143 evtchn_to_irq[evtchn] = irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700144}
145
Ian Campbell9158c352011-03-10 16:08:09 +0000146static void xen_irq_info_evtchn_init(unsigned irq,
147 unsigned short evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700148{
Ian Campbell9158c352011-03-10 16:08:09 +0000149 struct irq_info *info = info_for_irq(irq);
150
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000151 xen_irq_info_common_init(info, irq, IRQT_EVTCHN, evtchn, 0);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700152}
153
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000154static void xen_irq_info_ipi_init(unsigned cpu,
155 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000156 unsigned short evtchn,
157 enum ipi_vector ipi)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700158{
Ian Campbell9158c352011-03-10 16:08:09 +0000159 struct irq_info *info = info_for_irq(irq);
160
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000161 xen_irq_info_common_init(info, irq, IRQT_IPI, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000162
163 info->u.ipi = ipi;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000164
165 per_cpu(ipi_to_irq, cpu)[ipi] = irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700166}
167
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000168static void xen_irq_info_virq_init(unsigned cpu,
169 unsigned irq,
Ian Campbell9158c352011-03-10 16:08:09 +0000170 unsigned short evtchn,
171 unsigned short virq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700172{
Ian Campbell9158c352011-03-10 16:08:09 +0000173 struct irq_info *info = info_for_irq(irq);
174
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000175 xen_irq_info_common_init(info, irq, IRQT_VIRQ, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000176
177 info->u.virq = virq;
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000178
179 per_cpu(virq_to_irq, cpu)[virq] = irq;
Ian Campbell9158c352011-03-10 16:08:09 +0000180}
181
182static void xen_irq_info_pirq_init(unsigned irq,
183 unsigned short evtchn,
184 unsigned short pirq,
185 unsigned short gsi,
186 unsigned short vector,
187 unsigned char flags)
188{
189 struct irq_info *info = info_for_irq(irq);
190
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000191 xen_irq_info_common_init(info, irq, IRQT_PIRQ, evtchn, 0);
Ian Campbell9158c352011-03-10 16:08:09 +0000192
193 info->u.pirq.pirq = pirq;
194 info->u.pirq.gsi = gsi;
195 info->u.pirq.vector = vector;
196 info->u.pirq.flags = flags;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700197}
198
199/*
200 * Accessors for packed IRQ information.
201 */
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800202static unsigned int evtchn_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700203{
Joe Jin110e7c72011-01-07 14:50:12 +0800204 if (unlikely(WARN(irq < 0 || irq >= nr_irqs, "Invalid irq %d!\n", irq)))
205 return 0;
206
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800207 return info_for_irq(irq)->evtchn;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700208}
209
Ian Campbelld4c04532009-02-06 19:20:31 -0800210unsigned irq_from_evtchn(unsigned int evtchn)
211{
212 return evtchn_to_irq[evtchn];
213}
214EXPORT_SYMBOL_GPL(irq_from_evtchn);
215
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800216static enum ipi_vector ipi_from_irq(unsigned irq)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700217{
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800218 struct irq_info *info = info_for_irq(irq);
219
220 BUG_ON(info == NULL);
221 BUG_ON(info->type != IRQT_IPI);
222
223 return info->u.ipi;
224}
225
226static unsigned virq_from_irq(unsigned irq)
227{
228 struct irq_info *info = info_for_irq(irq);
229
230 BUG_ON(info == NULL);
231 BUG_ON(info->type != IRQT_VIRQ);
232
233 return info->u.virq;
234}
235
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100236static unsigned pirq_from_irq(unsigned irq)
237{
238 struct irq_info *info = info_for_irq(irq);
239
240 BUG_ON(info == NULL);
241 BUG_ON(info->type != IRQT_PIRQ);
242
243 return info->u.pirq.pirq;
244}
245
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800246static enum xen_irq_type type_from_irq(unsigned irq)
247{
248 return info_for_irq(irq)->type;
249}
250
251static unsigned cpu_from_irq(unsigned irq)
252{
253 return info_for_irq(irq)->cpu;
254}
255
256static unsigned int cpu_from_evtchn(unsigned int evtchn)
257{
258 int irq = evtchn_to_irq[evtchn];
259 unsigned ret = 0;
260
261 if (irq != -1)
262 ret = cpu_from_irq(irq);
263
264 return ret;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700265}
266
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400267static bool pirq_needs_eoi(unsigned irq)
268{
269 struct irq_info *info = info_for_irq(irq);
270
271 BUG_ON(info->type != IRQT_PIRQ);
272
273 return info->u.pirq.flags & PIRQ_NEEDS_EOI;
274}
275
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700276static inline unsigned long active_evtchns(unsigned int cpu,
277 struct shared_info *sh,
278 unsigned int idx)
279{
280 return (sh->evtchn_pending[idx] &
Ian Campbellcb60d112011-03-10 16:08:08 +0000281 per_cpu(cpu_evtchn_mask, cpu)[idx] &
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700282 ~sh->evtchn_mask[idx]);
283}
284
285static void bind_evtchn_to_cpu(unsigned int chn, unsigned int cpu)
286{
287 int irq = evtchn_to_irq[chn];
288
289 BUG_ON(irq == -1);
290#ifdef CONFIG_SMP
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000291 cpumask_copy(irq_to_desc(irq)->irq_data.affinity, cpumask_of(cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700292#endif
293
Ian Campbellcb60d112011-03-10 16:08:08 +0000294 clear_bit(chn, per_cpu(cpu_evtchn_mask, cpu_from_irq(irq)));
295 set_bit(chn, per_cpu(cpu_evtchn_mask, cpu));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700296
Ian Campbellca62ce82011-03-10 16:08:12 +0000297 info_for_irq(irq)->cpu = cpu;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700298}
299
300static void init_evtchn_cpu_bindings(void)
301{
Jan Beulich1c6969e2010-11-16 14:55:33 -0800302 int i;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700303#ifdef CONFIG_SMP
Ian Campbell6cb65372011-03-10 16:08:11 +0000304 struct irq_info *info;
Thomas Gleixner10e58082008-10-16 14:19:04 +0200305
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700306 /* By default all event channels notify CPU#0. */
Ian Campbell6cb65372011-03-10 16:08:11 +0000307 list_for_each_entry(info, &xen_irq_list_head, list) {
308 struct irq_desc *desc = irq_to_desc(info->irq);
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000309 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800310 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700311#endif
312
Jan Beulich1c6969e2010-11-16 14:55:33 -0800313 for_each_possible_cpu(i)
Ian Campbellcb60d112011-03-10 16:08:08 +0000314 memset(per_cpu(cpu_evtchn_mask, i),
315 (i == 0) ? ~0 : 0, sizeof(*per_cpu(cpu_evtchn_mask, i)));
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700316}
317
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700318static inline void clear_evtchn(int port)
319{
320 struct shared_info *s = HYPERVISOR_shared_info;
321 sync_clear_bit(port, &s->evtchn_pending[0]);
322}
323
324static inline void set_evtchn(int port)
325{
326 struct shared_info *s = HYPERVISOR_shared_info;
327 sync_set_bit(port, &s->evtchn_pending[0]);
328}
329
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -0700330static inline int test_evtchn(int port)
331{
332 struct shared_info *s = HYPERVISOR_shared_info;
333 return sync_test_bit(port, &s->evtchn_pending[0]);
334}
335
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700336
337/**
338 * notify_remote_via_irq - send event to remote end of event channel via irq
339 * @irq: irq of event channel to send event to
340 *
341 * Unlike notify_remote_via_evtchn(), this is safe to use across
342 * save/restore. Notifications on a broken connection are silently
343 * dropped.
344 */
345void notify_remote_via_irq(int irq)
346{
347 int evtchn = evtchn_from_irq(irq);
348
349 if (VALID_EVTCHN(evtchn))
350 notify_remote_via_evtchn(evtchn);
351}
352EXPORT_SYMBOL_GPL(notify_remote_via_irq);
353
354static void mask_evtchn(int port)
355{
356 struct shared_info *s = HYPERVISOR_shared_info;
357 sync_set_bit(port, &s->evtchn_mask[0]);
358}
359
360static void unmask_evtchn(int port)
361{
362 struct shared_info *s = HYPERVISOR_shared_info;
363 unsigned int cpu = get_cpu();
364
365 BUG_ON(!irqs_disabled());
366
367 /* Slow path (hypercall) if this is a non-local port. */
368 if (unlikely(cpu != cpu_from_evtchn(port))) {
369 struct evtchn_unmask unmask = { .port = port };
370 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask, &unmask);
371 } else {
Christoph Lameter780f36d2010-12-06 11:16:29 -0600372 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700373
374 sync_clear_bit(port, &s->evtchn_mask[0]);
375
376 /*
377 * The following is basically the equivalent of
378 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose
379 * the interrupt edge' if the channel is masked.
380 */
381 if (sync_test_bit(port, &s->evtchn_pending[0]) &&
382 !sync_test_and_set_bit(port / BITS_PER_LONG,
383 &vcpu_info->evtchn_pending_sel))
384 vcpu_info->evtchn_upcall_pending = 1;
385 }
386
387 put_cpu();
388}
389
Ian Campbell6cb65372011-03-10 16:08:11 +0000390static void xen_irq_init(unsigned irq)
391{
392 struct irq_info *info;
393 struct irq_desc *desc = irq_to_desc(irq);
394
Konrad Rzeszutek Wilk44626e42011-03-15 16:40:33 -0400395#ifdef CONFIG_SMP
Ian Campbell6cb65372011-03-10 16:08:11 +0000396 /* By default all event channels notify CPU#0. */
397 cpumask_copy(desc->irq_data.affinity, cpumask_of(0));
Konrad Rzeszutek Wilk44626e42011-03-15 16:40:33 -0400398#endif
Ian Campbell6cb65372011-03-10 16:08:11 +0000399
Ian Campbellca62ce82011-03-10 16:08:12 +0000400 info = kzalloc(sizeof(*info), GFP_KERNEL);
401 if (info == NULL)
402 panic("Unable to allocate metadata for IRQ%d\n", irq);
Ian Campbell6cb65372011-03-10 16:08:11 +0000403
404 info->type = IRQT_UNBOUND;
405
Ian Campbellca62ce82011-03-10 16:08:12 +0000406 set_irq_data(irq, info);
407
Ian Campbell6cb65372011-03-10 16:08:11 +0000408 list_add_tail(&info->list, &xen_irq_list_head);
409}
410
Ian Campbell7bee9762011-03-10 16:08:15 +0000411static int __must_check xen_allocate_irq_dynamic(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700412{
Ian Campbell89911502011-03-03 11:57:44 -0500413 int first = 0;
414 int irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700415
Ian Campbell89911502011-03-03 11:57:44 -0500416#ifdef CONFIG_X86_IO_APIC
417 /*
418 * For an HVM guest or domain 0 which see "real" (emulated or
419 * actual repectively) GSIs we allocate dynamic IRQs
420 * e.g. those corresponding to event channels or MSIs
421 * etc. from the range above those "real" GSIs to avoid
422 * collisions.
Konrad Rzeszutek Wilkd1b758e2010-12-09 14:53:29 -0500423 */
Ian Campbell89911502011-03-03 11:57:44 -0500424 if (xen_initial_domain() || xen_hvm_domain())
425 first = get_nr_irqs_gsi();
426#endif
427
Ian Campbell89911502011-03-03 11:57:44 -0500428 irq = irq_alloc_desc_from(first, -1);
429
Ian Campbell6cb65372011-03-10 16:08:11 +0000430 xen_irq_init(irq);
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800431
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700432 return irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400433}
434
Ian Campbell7bee9762011-03-10 16:08:15 +0000435static int __must_check xen_allocate_irq_gsi(unsigned gsi)
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000436{
437 int irq;
438
Ian Campbell89911502011-03-03 11:57:44 -0500439 /*
440 * A PV guest has no concept of a GSI (since it has no ACPI
441 * nor access to/knowledge of the physical APICs). Therefore
442 * all IRQs are dynamically allocated from the entire IRQ
443 * space.
444 */
445 if (xen_pv_domain() && !xen_initial_domain())
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000446 return xen_allocate_irq_dynamic();
447
448 /* Legacy IRQ descriptors are already allocated by the arch. */
449 if (gsi < NR_IRQS_LEGACY)
Ian Campbell6cb65372011-03-10 16:08:11 +0000450 irq = gsi;
451 else
452 irq = irq_alloc_desc_at(gsi, -1);
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000453
Ian Campbell6cb65372011-03-10 16:08:11 +0000454 xen_irq_init(irq);
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000455
456 return irq;
457}
458
459static void xen_free_irq(unsigned irq)
460{
Ian Campbellca62ce82011-03-10 16:08:12 +0000461 struct irq_info *info = get_irq_data(irq);
Ian Campbell6cb65372011-03-10 16:08:11 +0000462
463 list_del(&info->list);
Ian Campbell9158c352011-03-10 16:08:09 +0000464
Ian Campbellca62ce82011-03-10 16:08:12 +0000465 set_irq_data(irq, NULL);
466
467 kfree(info);
468
Ian Campbell72146102011-02-03 09:49:35 +0000469 /* Legacy IRQ descriptors are managed by the arch. */
470 if (irq < NR_IRQS_LEGACY)
471 return;
472
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000473 irq_free_desc(irq);
474}
475
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400476static void pirq_unmask_notify(int irq)
477{
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100478 struct physdev_eoi eoi = { .irq = pirq_from_irq(irq) };
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400479
480 if (unlikely(pirq_needs_eoi(irq))) {
481 int rc = HYPERVISOR_physdev_op(PHYSDEVOP_eoi, &eoi);
482 WARN_ON(rc);
483 }
484}
485
486static void pirq_query_unmask(int irq)
487{
488 struct physdev_irq_status_query irq_status;
489 struct irq_info *info = info_for_irq(irq);
490
491 BUG_ON(info->type != IRQT_PIRQ);
492
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100493 irq_status.irq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400494 if (HYPERVISOR_physdev_op(PHYSDEVOP_irq_status_query, &irq_status))
495 irq_status.flags = 0;
496
497 info->u.pirq.flags &= ~PIRQ_NEEDS_EOI;
498 if (irq_status.flags & XENIRQSTAT_needs_eoi)
499 info->u.pirq.flags |= PIRQ_NEEDS_EOI;
500}
501
502static bool probing_irq(int irq)
503{
504 struct irq_desc *desc = irq_to_desc(irq);
505
506 return desc && desc->action == NULL;
507}
508
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000509static unsigned int __startup_pirq(unsigned int irq)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400510{
511 struct evtchn_bind_pirq bind_pirq;
512 struct irq_info *info = info_for_irq(irq);
513 int evtchn = evtchn_from_irq(irq);
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400514 int rc;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400515
516 BUG_ON(info->type != IRQT_PIRQ);
517
518 if (VALID_EVTCHN(evtchn))
519 goto out;
520
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100521 bind_pirq.pirq = pirq_from_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400522 /* NB. We are happy to share unless we are probing. */
Konrad Rzeszutek Wilk15ebbb82010-10-04 13:43:27 -0400523 bind_pirq.flags = info->u.pirq.flags & PIRQ_SHAREABLE ?
524 BIND_PIRQ__WILL_SHARE : 0;
525 rc = HYPERVISOR_event_channel_op(EVTCHNOP_bind_pirq, &bind_pirq);
526 if (rc != 0) {
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400527 if (!probing_irq(irq))
528 printk(KERN_INFO "Failed to obtain physical IRQ %d\n",
529 irq);
530 return 0;
531 }
532 evtchn = bind_pirq.port;
533
534 pirq_query_unmask(irq);
535
536 evtchn_to_irq[evtchn] = irq;
537 bind_evtchn_to_cpu(evtchn, 0);
538 info->evtchn = evtchn;
539
540out:
541 unmask_evtchn(evtchn);
542 pirq_unmask_notify(irq);
543
544 return 0;
545}
546
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000547static unsigned int startup_pirq(struct irq_data *data)
548{
549 return __startup_pirq(data->irq);
550}
551
552static void shutdown_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400553{
554 struct evtchn_close close;
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000555 unsigned int irq = data->irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400556 struct irq_info *info = info_for_irq(irq);
557 int evtchn = evtchn_from_irq(irq);
558
559 BUG_ON(info->type != IRQT_PIRQ);
560
561 if (!VALID_EVTCHN(evtchn))
562 return;
563
564 mask_evtchn(evtchn);
565
566 close.port = evtchn;
567 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
568 BUG();
569
570 bind_evtchn_to_cpu(evtchn, 0);
571 evtchn_to_irq[evtchn] = -1;
572 info->evtchn = 0;
573}
574
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000575static void enable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400576{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000577 startup_pirq(data);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400578}
579
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000580static void disable_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400581{
582}
583
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000584static void ack_pirq(struct irq_data *data)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400585{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +0000586 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400587
Ian Campbellaa673c12011-02-07 11:08:39 +0000588 move_native_irq(data->irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400589
590 if (VALID_EVTCHN(evtchn)) {
591 mask_evtchn(evtchn);
592 clear_evtchn(evtchn);
593 }
594}
595
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400596static int find_irq_by_gsi(unsigned gsi)
597{
Ian Campbell6cb65372011-03-10 16:08:11 +0000598 struct irq_info *info;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400599
Ian Campbell6cb65372011-03-10 16:08:11 +0000600 list_for_each_entry(info, &xen_irq_list_head, list) {
601 if (info->type != IRQT_PIRQ)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400602 continue;
603
Ian Campbell6cb65372011-03-10 16:08:11 +0000604 if (info->u.pirq.gsi == gsi)
605 return info->irq;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400606 }
607
608 return -1;
609}
610
Ian Campbellf4d06352011-03-10 16:08:07 +0000611int xen_allocate_pirq_gsi(unsigned gsi)
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100612{
Ian Campbellf4d06352011-03-10 16:08:07 +0000613 return gsi;
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100614}
615
Ian Campbell653378a2011-03-10 16:08:04 +0000616/*
617 * Do not make any assumptions regarding the relationship between the
618 * IRQ number returned here and the Xen pirq argument.
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100619 *
620 * Note: We don't assign an event channel until the irq actually started
621 * up. Return an existing irq if we've already got one for the gsi.
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400622 */
Ian Campbellf4d06352011-03-10 16:08:07 +0000623int xen_bind_pirq_gsi_to_irq(unsigned gsi,
624 unsigned pirq, int shareable, char *name)
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400625{
Ian Campbella0e18112011-03-10 16:08:03 +0000626 int irq = -1;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400627 struct physdev_irq irq_op;
628
629 spin_lock(&irq_mapping_update_lock);
630
631 irq = find_irq_by_gsi(gsi);
632 if (irq != -1) {
Stefano Stabellini7a043f12010-07-01 17:08:14 +0100633 printk(KERN_INFO "xen_map_pirq_gsi: returning irq %d for gsi %u\n",
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400634 irq, gsi);
635 goto out; /* XXX need refcount? */
636 }
637
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000638 irq = xen_allocate_irq_gsi(gsi);
Ian Campbell7bee9762011-03-10 16:08:15 +0000639 if (irq < 0)
640 goto out;
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400641
642 set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
Gerd Hoffmann1a60d052010-10-04 13:42:27 -0400643 handle_level_irq, name);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400644
645 irq_op.irq = irq;
Alex Nixonb5401a92010-03-18 16:31:34 -0400646 irq_op.vector = 0;
647
648 /* Only the privileged domain can do this. For non-priv, the pcifront
649 * driver provides a PCI bus that does the call to do exactly
650 * this in the priv domain. */
651 if (xen_initial_domain() &&
652 HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op)) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000653 xen_free_irq(irq);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400654 irq = -ENOSPC;
655 goto out;
656 }
657
Ian Campbell9158c352011-03-10 16:08:09 +0000658 xen_irq_info_pirq_init(irq, 0, pirq, gsi, irq_op.vector,
659 shareable ? PIRQ_SHAREABLE : 0);
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -0400660
661out:
662 spin_unlock(&irq_mapping_update_lock);
663
664 return irq;
665}
666
Qing Hef731e3ef2010-10-11 15:30:09 +0100667#ifdef CONFIG_PCI_MSI
Ian Campbellbf480d92011-02-18 16:43:32 +0000668int xen_allocate_pirq_msi(struct pci_dev *dev, struct msi_desc *msidesc)
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000669{
Ian Campbell5cad61a2011-02-18 16:43:31 +0000670 int rc;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000671 struct physdev_get_free_pirq op_get_free_pirq;
Ian Campbell5cad61a2011-02-18 16:43:31 +0000672
Ian Campbellbf480d92011-02-18 16:43:32 +0000673 op_get_free_pirq.type = MAP_PIRQ_TYPE_MSI;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000674 rc = HYPERVISOR_physdev_op(PHYSDEVOP_get_free_pirq, &op_get_free_pirq);
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000675
Ian Campbell5cad61a2011-02-18 16:43:31 +0000676 WARN_ONCE(rc == -ENOSYS,
677 "hypervisor does not support the PHYSDEVOP_get_free_pirq interface\n");
678
679 return rc ? -1 : op_get_free_pirq.pirq;
Ian Campbellcbf6aa82011-01-11 17:20:14 +0000680}
681
Ian Campbellbf480d92011-02-18 16:43:32 +0000682int xen_bind_pirq_msi_to_irq(struct pci_dev *dev, struct msi_desc *msidesc,
Ian Campbellca1d8fe2011-02-18 16:43:36 +0000683 int pirq, int vector, const char *name)
Stefano Stabellini809f9262010-07-01 17:10:39 +0100684{
Ian Campbellbf480d92011-02-18 16:43:32 +0000685 int irq, ret;
Ian Campbell4b41df72011-02-18 16:43:29 +0000686
Stefano Stabellini809f9262010-07-01 17:10:39 +0100687 spin_lock(&irq_mapping_update_lock);
688
Ian Campbell4b41df72011-02-18 16:43:29 +0000689 irq = xen_allocate_irq_dynamic();
690 if (irq == -1)
Ian Campbellbb5d0792011-02-18 16:43:28 +0000691 goto out;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100692
Ian Campbell4b41df72011-02-18 16:43:29 +0000693 set_irq_chip_and_handler_name(irq, &xen_pirq_chip,
Stefano Stabellini809f9262010-07-01 17:10:39 +0100694 handle_level_irq, name);
695
Ian Campbell9158c352011-03-10 16:08:09 +0000696 xen_irq_info_pirq_init(irq, 0, pirq, 0, vector, 0);
Linus Torvalds5f6fb452011-03-15 19:23:40 -0700697 ret = irq_set_msi_desc(irq, msidesc);
Ian Campbellbf480d92011-02-18 16:43:32 +0000698 if (ret < 0)
699 goto error_irq;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100700out:
701 spin_unlock(&irq_mapping_update_lock);
Ian Campbell4b41df72011-02-18 16:43:29 +0000702 return irq;
Ian Campbellbf480d92011-02-18 16:43:32 +0000703error_irq:
704 spin_unlock(&irq_mapping_update_lock);
705 xen_free_irq(irq);
706 return -1;
Stefano Stabellini809f9262010-07-01 17:10:39 +0100707}
Qing Hef731e3ef2010-10-11 15:30:09 +0100708#endif
709
Alex Nixonb5401a92010-03-18 16:31:34 -0400710int xen_destroy_irq(int irq)
711{
712 struct irq_desc *desc;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100713 struct physdev_unmap_pirq unmap_irq;
714 struct irq_info *info = info_for_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400715 int rc = -ENOENT;
716
717 spin_lock(&irq_mapping_update_lock);
718
719 desc = irq_to_desc(irq);
720 if (!desc)
721 goto out;
722
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100723 if (xen_initial_domain()) {
Konrad Rzeszutek Wilk12334712010-11-19 11:27:09 -0500724 unmap_irq.pirq = info->u.pirq.pirq;
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +0100725 unmap_irq.domid = DOMID_SELF;
726 rc = HYPERVISOR_physdev_op(PHYSDEVOP_unmap_pirq, &unmap_irq);
727 if (rc) {
728 printk(KERN_WARNING "unmap irq failed %d\n", rc);
729 goto out;
730 }
731 }
Alex Nixonb5401a92010-03-18 16:31:34 -0400732
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000733 xen_free_irq(irq);
Alex Nixonb5401a92010-03-18 16:31:34 -0400734
735out:
736 spin_unlock(&irq_mapping_update_lock);
737 return rc;
738}
739
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000740int xen_irq_from_pirq(unsigned pirq)
741{
Ian Campbell69c358c2011-03-10 16:08:13 +0000742 int irq;
743
744 struct irq_info *info;
745
746 spin_lock(&irq_mapping_update_lock);
747
748 list_for_each_entry(info, &xen_irq_list_head, list) {
749 if (info == NULL || info->type != IRQT_PIRQ)
750 continue;
751 irq = info->irq;
752 if (info->u.pirq.pirq == pirq)
753 goto out;
754 }
755 irq = -1;
756out:
Ian Campbella7b807c2011-03-14 09:50:39 +0000757 spin_unlock(&irq_mapping_update_lock);
Ian Campbell69c358c2011-03-10 16:08:13 +0000758
759 return irq;
Stefano Stabelliniaf42b8d2010-12-01 14:51:44 +0000760}
761
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700762int bind_evtchn_to_irq(unsigned int evtchn)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700763{
764 int irq;
765
766 spin_lock(&irq_mapping_update_lock);
767
768 irq = evtchn_to_irq[evtchn];
769
770 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000771 irq = xen_allocate_irq_dynamic();
Ian Campbell7bee9762011-03-10 16:08:15 +0000772 if (irq == -1)
773 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700774
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700775 set_irq_chip_and_handler_name(irq, &xen_dynamic_chip,
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -0700776 handle_fasteoi_irq, "event");
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700777
Ian Campbell9158c352011-03-10 16:08:09 +0000778 xen_irq_info_evtchn_init(irq, evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700779 }
780
Ian Campbell7bee9762011-03-10 16:08:15 +0000781out:
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700782 spin_unlock(&irq_mapping_update_lock);
783
784 return irq;
785}
Jeremy Fitzhardingeb536b4b2007-07-17 18:37:06 -0700786EXPORT_SYMBOL_GPL(bind_evtchn_to_irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700787
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700788static int bind_ipi_to_irq(unsigned int ipi, unsigned int cpu)
789{
790 struct evtchn_bind_ipi bind_ipi;
791 int evtchn, irq;
792
793 spin_lock(&irq_mapping_update_lock);
794
795 irq = per_cpu(ipi_to_irq, cpu)[ipi];
Ian Campbell90af9512009-02-06 16:55:58 -0800796
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700797 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000798 irq = xen_allocate_irq_dynamic();
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700799 if (irq < 0)
800 goto out;
801
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -0700802 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
803 handle_percpu_irq, "ipi");
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700804
805 bind_ipi.vcpu = cpu;
806 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
807 &bind_ipi) != 0)
808 BUG();
809 evtchn = bind_ipi.port;
810
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000811 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700812
813 bind_evtchn_to_cpu(evtchn, cpu);
814 }
815
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700816 out:
817 spin_unlock(&irq_mapping_update_lock);
818 return irq;
819}
820
Ian Campbell2e820f52009-02-09 12:05:50 -0800821static int bind_interdomain_evtchn_to_irq(unsigned int remote_domain,
822 unsigned int remote_port)
823{
824 struct evtchn_bind_interdomain bind_interdomain;
825 int err;
826
827 bind_interdomain.remote_dom = remote_domain;
828 bind_interdomain.remote_port = remote_port;
829
830 err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
831 &bind_interdomain);
832
833 return err ? : bind_evtchn_to_irq(bind_interdomain.local_port);
834}
835
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700836
Jeremy Fitzhardinge4fe7d5a2010-09-02 16:17:06 +0100837int bind_virq_to_irq(unsigned int virq, unsigned int cpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700838{
839 struct evtchn_bind_virq bind_virq;
840 int evtchn, irq;
841
842 spin_lock(&irq_mapping_update_lock);
843
844 irq = per_cpu(virq_to_irq, cpu)[virq];
845
846 if (irq == -1) {
Ian Campbellc9df1ce2011-01-11 17:20:15 +0000847 irq = xen_allocate_irq_dynamic();
Ian Campbell7bee9762011-03-10 16:08:15 +0000848 if (irq == -1)
849 goto out;
Jeremy Fitzhardingea52521f2010-09-22 15:28:52 -0700850
851 set_irq_chip_and_handler_name(irq, &xen_percpu_chip,
852 handle_percpu_irq, "virq");
853
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700854 bind_virq.virq = virq;
855 bind_virq.vcpu = cpu;
856 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
857 &bind_virq) != 0)
858 BUG();
859 evtchn = bind_virq.port;
860
Ian Campbell3d4cfa32011-03-10 16:08:10 +0000861 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700862
863 bind_evtchn_to_cpu(evtchn, cpu);
864 }
865
Ian Campbell7bee9762011-03-10 16:08:15 +0000866out:
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700867 spin_unlock(&irq_mapping_update_lock);
868
869 return irq;
870}
871
872static void unbind_from_irq(unsigned int irq)
873{
874 struct evtchn_close close;
875 int evtchn = evtchn_from_irq(irq);
876
877 spin_lock(&irq_mapping_update_lock);
878
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -0800879 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700880 close.port = evtchn;
881 if (HYPERVISOR_event_channel_op(EVTCHNOP_close, &close) != 0)
882 BUG();
883
884 switch (type_from_irq(irq)) {
885 case IRQT_VIRQ:
886 per_cpu(virq_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800887 [virq_from_irq(irq)] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700888 break;
Alex Nixond68d82a2008-08-22 11:52:15 +0100889 case IRQT_IPI:
890 per_cpu(ipi_to_irq, cpu_from_evtchn(evtchn))
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -0800891 [ipi_from_irq(irq)] = -1;
Alex Nixond68d82a2008-08-22 11:52:15 +0100892 break;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700893 default:
894 break;
895 }
896
897 /* Closed ports are implicitly re-bound to VCPU0. */
898 bind_evtchn_to_cpu(evtchn, 0);
899
900 evtchn_to_irq[evtchn] = -1;
Ian Campbellfed5ea82009-12-01 16:15:30 +0000901 }
902
Ian Campbellca62ce82011-03-10 16:08:12 +0000903 BUG_ON(info_for_irq(irq)->type == IRQT_UNBOUND);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700904
Ian Campbell9158c352011-03-10 16:08:09 +0000905 xen_free_irq(irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700906
907 spin_unlock(&irq_mapping_update_lock);
908}
909
910int bind_evtchn_to_irqhandler(unsigned int evtchn,
Jeff Garzik7c239972007-10-19 03:12:20 -0400911 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700912 unsigned long irqflags,
913 const char *devname, void *dev_id)
914{
915 unsigned int irq;
916 int retval;
917
918 irq = bind_evtchn_to_irq(evtchn);
Ian Campbell7bee9762011-03-10 16:08:15 +0000919 if (irq < 0)
920 return irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700921 retval = request_irq(irq, handler, irqflags, devname, dev_id);
922 if (retval != 0) {
923 unbind_from_irq(irq);
924 return retval;
925 }
926
927 return irq;
928}
929EXPORT_SYMBOL_GPL(bind_evtchn_to_irqhandler);
930
Ian Campbell2e820f52009-02-09 12:05:50 -0800931int bind_interdomain_evtchn_to_irqhandler(unsigned int remote_domain,
932 unsigned int remote_port,
933 irq_handler_t handler,
934 unsigned long irqflags,
935 const char *devname,
936 void *dev_id)
937{
938 int irq, retval;
939
940 irq = bind_interdomain_evtchn_to_irq(remote_domain, remote_port);
941 if (irq < 0)
942 return irq;
943
944 retval = request_irq(irq, handler, irqflags, devname, dev_id);
945 if (retval != 0) {
946 unbind_from_irq(irq);
947 return retval;
948 }
949
950 return irq;
951}
952EXPORT_SYMBOL_GPL(bind_interdomain_evtchn_to_irqhandler);
953
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700954int bind_virq_to_irqhandler(unsigned int virq, unsigned int cpu,
Jeff Garzik7c239972007-10-19 03:12:20 -0400955 irq_handler_t handler,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700956 unsigned long irqflags, const char *devname, void *dev_id)
957{
958 unsigned int irq;
959 int retval;
960
961 irq = bind_virq_to_irq(virq, cpu);
Ian Campbell7bee9762011-03-10 16:08:15 +0000962 if (irq < 0)
963 return irq;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700964 retval = request_irq(irq, handler, irqflags, devname, dev_id);
965 if (retval != 0) {
966 unbind_from_irq(irq);
967 return retval;
968 }
969
970 return irq;
971}
972EXPORT_SYMBOL_GPL(bind_virq_to_irqhandler);
973
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700974int bind_ipi_to_irqhandler(enum ipi_vector ipi,
975 unsigned int cpu,
976 irq_handler_t handler,
977 unsigned long irqflags,
978 const char *devname,
979 void *dev_id)
980{
981 int irq, retval;
982
983 irq = bind_ipi_to_irq(ipi, cpu);
984 if (irq < 0)
985 return irq;
986
Thomas Gleixner676dc3c2011-02-05 20:08:59 +0000987 irqflags |= IRQF_NO_SUSPEND | IRQF_FORCE_RESUME;
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -0700988 retval = request_irq(irq, handler, irqflags, devname, dev_id);
989 if (retval != 0) {
990 unbind_from_irq(irq);
991 return retval;
992 }
993
994 return irq;
995}
996
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -0700997void unbind_from_irqhandler(unsigned int irq, void *dev_id)
998{
999 free_irq(irq, dev_id);
1000 unbind_from_irq(irq);
1001}
1002EXPORT_SYMBOL_GPL(unbind_from_irqhandler);
1003
Jeremy Fitzhardingef87e4ca2007-07-17 18:37:06 -07001004void xen_send_IPI_one(unsigned int cpu, enum ipi_vector vector)
1005{
1006 int irq = per_cpu(ipi_to_irq, cpu)[vector];
1007 BUG_ON(irq < 0);
1008 notify_remote_via_irq(irq);
1009}
1010
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001011irqreturn_t xen_debug_interrupt(int irq, void *dev_id)
1012{
1013 struct shared_info *sh = HYPERVISOR_shared_info;
1014 int cpu = smp_processor_id();
Ian Campbellcb60d112011-03-10 16:08:08 +00001015 unsigned long *cpu_evtchn = per_cpu(cpu_evtchn_mask, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001016 int i;
1017 unsigned long flags;
1018 static DEFINE_SPINLOCK(debug_lock);
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001019 struct vcpu_info *v;
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001020
1021 spin_lock_irqsave(&debug_lock, flags);
1022
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001023 printk("\nvcpu %d\n ", cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001024
1025 for_each_online_cpu(i) {
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001026 int pending;
1027 v = per_cpu(xen_vcpu, i);
1028 pending = (get_irq_regs() && i == cpu)
1029 ? xen_irqs_disabled(get_irq_regs())
1030 : v->evtchn_upcall_mask;
1031 printk("%d: masked=%d pending=%d event_sel %0*lx\n ", i,
1032 pending, v->evtchn_upcall_pending,
1033 (int)(sizeof(v->evtchn_pending_sel)*2),
1034 v->evtchn_pending_sel);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001035 }
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001036 v = per_cpu(xen_vcpu, cpu);
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001037
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001038 printk("\npending:\n ");
1039 for (i = ARRAY_SIZE(sh->evtchn_pending)-1; i >= 0; i--)
1040 printk("%0*lx%s", (int)sizeof(sh->evtchn_pending[0])*2,
1041 sh->evtchn_pending[i],
1042 i % 8 == 0 ? "\n " : " ");
1043 printk("\nglobal mask:\n ");
1044 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1045 printk("%0*lx%s",
1046 (int)(sizeof(sh->evtchn_mask[0])*2),
1047 sh->evtchn_mask[i],
1048 i % 8 == 0 ? "\n " : " ");
1049
1050 printk("\nglobally unmasked:\n ");
1051 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--)
1052 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
1053 sh->evtchn_pending[i] & ~sh->evtchn_mask[i],
1054 i % 8 == 0 ? "\n " : " ");
1055
1056 printk("\nlocal cpu%d mask:\n ", cpu);
1057 for (i = (NR_EVENT_CHANNELS/BITS_PER_LONG)-1; i >= 0; i--)
1058 printk("%0*lx%s", (int)(sizeof(cpu_evtchn[0])*2),
1059 cpu_evtchn[i],
1060 i % 8 == 0 ? "\n " : " ");
1061
1062 printk("\nlocally unmasked:\n ");
1063 for (i = ARRAY_SIZE(sh->evtchn_mask)-1; i >= 0; i--) {
1064 unsigned long pending = sh->evtchn_pending[i]
1065 & ~sh->evtchn_mask[i]
1066 & cpu_evtchn[i];
1067 printk("%0*lx%s", (int)(sizeof(sh->evtchn_mask[0])*2),
1068 pending, i % 8 == 0 ? "\n " : " ");
1069 }
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001070
1071 printk("\npending list:\n");
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001072 for (i = 0; i < NR_EVENT_CHANNELS; i++) {
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001073 if (sync_test_bit(i, sh->evtchn_pending)) {
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001074 int word_idx = i / BITS_PER_LONG;
1075 printk(" %d: event %d -> irq %d%s%s%s\n",
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001076 cpu_from_evtchn(i), i,
Ian Campbellcb52e6d2010-10-15 11:52:46 +01001077 evtchn_to_irq[i],
1078 sync_test_bit(word_idx, &v->evtchn_pending_sel)
1079 ? "" : " l2-clear",
1080 !sync_test_bit(i, sh->evtchn_mask)
1081 ? "" : " globally-masked",
1082 sync_test_bit(i, cpu_evtchn)
1083 ? "" : " locally-masked");
Jeremy Fitzhardingeee523ca2008-03-17 16:37:18 -07001084 }
1085 }
1086
1087 spin_unlock_irqrestore(&debug_lock, flags);
1088
1089 return IRQ_HANDLED;
1090}
1091
Tejun Heo245b2e72009-06-24 15:13:48 +09001092static DEFINE_PER_CPU(unsigned, xed_nesting_count);
Keir Fraserada68142011-03-03 10:01:11 +00001093static DEFINE_PER_CPU(unsigned int, current_word_idx);
1094static DEFINE_PER_CPU(unsigned int, current_bit_idx);
Tejun Heo245b2e72009-06-24 15:13:48 +09001095
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001096/*
Scott Rixnerab7f8632011-03-03 09:30:08 +00001097 * Mask out the i least significant bits of w
1098 */
1099#define MASK_LSBS(w, i) (w & ((~0UL) << i))
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001100
1101/*
1102 * Search the CPUs pending events bitmasks. For each one found, map
1103 * the event number to an irq, and feed it into do_IRQ() for
1104 * handling.
1105 *
1106 * Xen uses a two-level bitmap to speed searching. The first level is
1107 * a bitset of words which contain pending event bits. The second
1108 * level is a bitset of pending events themselves.
1109 */
Sheng Yang38e20b02010-05-14 12:40:51 +01001110static void __xen_evtchn_do_upcall(void)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001111{
Keir Fraser24b51c22011-03-03 11:06:28 +00001112 int start_word_idx, start_bit_idx;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001113 int word_idx, bit_idx;
Keir Fraser24b51c22011-03-03 11:06:28 +00001114 int i;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001115 int cpu = get_cpu();
1116 struct shared_info *s = HYPERVISOR_shared_info;
Christoph Lameter780f36d2010-12-06 11:16:29 -06001117 struct vcpu_info *vcpu_info = __this_cpu_read(xen_vcpu);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001118 unsigned count;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001119
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001120 do {
1121 unsigned long pending_words;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001122
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001123 vcpu_info->evtchn_upcall_pending = 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001124
Christoph Lameterb2e4ae62010-12-06 11:40:07 -06001125 if (__this_cpu_inc_return(xed_nesting_count) - 1)
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001126 goto out;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001127
Isaku Yamahatae849c3e2008-04-02 10:53:56 -07001128#ifndef CONFIG_X86 /* No need for a barrier -- XCHG is a barrier on x86. */
1129 /* Clear master flag /before/ clearing selector flag. */
Isaku Yamahata6673cf62008-06-16 14:58:13 -07001130 wmb();
Isaku Yamahatae849c3e2008-04-02 10:53:56 -07001131#endif
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001132 pending_words = xchg(&vcpu_info->evtchn_pending_sel, 0);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001133
Keir Fraser24b51c22011-03-03 11:06:28 +00001134 start_word_idx = __this_cpu_read(current_word_idx);
1135 start_bit_idx = __this_cpu_read(current_bit_idx);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001136
Keir Fraser24b51c22011-03-03 11:06:28 +00001137 word_idx = start_word_idx;
1138
1139 for (i = 0; pending_words != 0; i++) {
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001140 unsigned long pending_bits;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001141 unsigned long words;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001142
Scott Rixnerab7f8632011-03-03 09:30:08 +00001143 words = MASK_LSBS(pending_words, word_idx);
1144
1145 /*
Keir Fraserada68142011-03-03 10:01:11 +00001146 * If we masked out all events, wrap to beginning.
Scott Rixnerab7f8632011-03-03 09:30:08 +00001147 */
1148 if (words == 0) {
Keir Fraserada68142011-03-03 10:01:11 +00001149 word_idx = 0;
1150 bit_idx = 0;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001151 continue;
1152 }
1153 word_idx = __ffs(words);
1154
Keir Fraser24b51c22011-03-03 11:06:28 +00001155 pending_bits = active_evtchns(cpu, s, word_idx);
1156 bit_idx = 0; /* usually scan entire word from start */
1157 if (word_idx == start_word_idx) {
1158 /* We scan the starting word in two parts */
1159 if (i == 0)
1160 /* 1st time: start in the middle */
1161 bit_idx = start_bit_idx;
1162 else
1163 /* 2nd time: mask bits done already */
1164 bit_idx &= (1UL << start_bit_idx) - 1;
1165 }
1166
Scott Rixnerab7f8632011-03-03 09:30:08 +00001167 do {
1168 unsigned long bits;
1169 int port, irq;
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -08001170 struct irq_desc *desc;
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001171
Scott Rixnerab7f8632011-03-03 09:30:08 +00001172 bits = MASK_LSBS(pending_bits, bit_idx);
1173
1174 /* If we masked out all events, move on. */
Keir Fraserada68142011-03-03 10:01:11 +00001175 if (bits == 0)
Scott Rixnerab7f8632011-03-03 09:30:08 +00001176 break;
Scott Rixnerab7f8632011-03-03 09:30:08 +00001177
1178 bit_idx = __ffs(bits);
1179
1180 /* Process port. */
1181 port = (word_idx * BITS_PER_LONG) + bit_idx;
1182 irq = evtchn_to_irq[port];
1183
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -07001184 mask_evtchn(port);
1185 clear_evtchn(port);
1186
Eric W. Biedermanca4dbc62010-02-17 18:49:54 -08001187 if (irq != -1) {
1188 desc = irq_to_desc(irq);
1189 if (desc)
1190 generic_handle_irq_desc(irq, desc);
1191 }
Scott Rixnerab7f8632011-03-03 09:30:08 +00001192
Keir Fraserada68142011-03-03 10:01:11 +00001193 bit_idx = (bit_idx + 1) % BITS_PER_LONG;
1194
1195 /* Next caller starts at last processed + 1 */
1196 __this_cpu_write(current_word_idx,
1197 bit_idx ? word_idx :
1198 (word_idx+1) % BITS_PER_LONG);
1199 __this_cpu_write(current_bit_idx, bit_idx);
1200 } while (bit_idx != 0);
Scott Rixnerab7f8632011-03-03 09:30:08 +00001201
Keir Fraser24b51c22011-03-03 11:06:28 +00001202 /* Scan start_l1i twice; all others once. */
1203 if ((word_idx != start_word_idx) || (i != 0))
Scott Rixnerab7f8632011-03-03 09:30:08 +00001204 pending_words &= ~(1UL << word_idx);
Keir Fraserada68142011-03-03 10:01:11 +00001205
1206 word_idx = (word_idx + 1) % BITS_PER_LONG;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001207 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001208
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001209 BUG_ON(!irqs_disabled());
1210
Christoph Lameter780f36d2010-12-06 11:16:29 -06001211 count = __this_cpu_read(xed_nesting_count);
1212 __this_cpu_write(xed_nesting_count, 0);
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001213 } while (count != 1 || vcpu_info->evtchn_upcall_pending);
Jeremy Fitzhardinge229664b2008-03-17 16:37:20 -07001214
1215out:
Jeremy Fitzhardinge3445a8f2009-02-06 14:09:46 -08001216
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001217 put_cpu();
1218}
1219
Sheng Yang38e20b02010-05-14 12:40:51 +01001220void xen_evtchn_do_upcall(struct pt_regs *regs)
1221{
1222 struct pt_regs *old_regs = set_irq_regs(regs);
1223
1224 exit_idle();
1225 irq_enter();
1226
1227 __xen_evtchn_do_upcall();
1228
1229 irq_exit();
1230 set_irq_regs(old_regs);
1231}
1232
1233void xen_hvm_evtchn_do_upcall(void)
1234{
1235 __xen_evtchn_do_upcall();
1236}
Stefano Stabellini183d03c2010-05-17 17:08:21 +01001237EXPORT_SYMBOL_GPL(xen_hvm_evtchn_do_upcall);
Sheng Yang38e20b02010-05-14 12:40:51 +01001238
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001239/* Rebind a new event channel to an existing irq. */
1240void rebind_evtchn_irq(int evtchn, int irq)
1241{
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001242 struct irq_info *info = info_for_irq(irq);
1243
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001244 /* Make sure the irq is masked, since the new event channel
1245 will also be masked. */
1246 disable_irq(irq);
1247
1248 spin_lock(&irq_mapping_update_lock);
1249
1250 /* After resume the irq<->evtchn mappings are all cleared out */
1251 BUG_ON(evtchn_to_irq[evtchn] != -1);
1252 /* Expect irq to have been bound before,
Jeremy Fitzhardinged77bbd42009-02-06 14:09:45 -08001253 so there should be a proper type */
1254 BUG_ON(info->type == IRQT_UNBOUND);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001255
Ian Campbell9158c352011-03-10 16:08:09 +00001256 xen_irq_info_evtchn_init(irq, evtchn);
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001257
1258 spin_unlock(&irq_mapping_update_lock);
1259
1260 /* new event channels are always bound to cpu 0 */
Rusty Russell0de26522008-12-13 21:20:26 +10301261 irq_set_affinity(irq, cpumask_of(0));
Jeremy Fitzhardingeeb1e3052008-05-26 23:31:23 +01001262
1263 /* Unmask the event channel. */
1264 enable_irq(irq);
1265}
1266
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001267/* Rebind an evtchn so that it gets delivered to a specific cpu */
Yinghai Lud5dedd42009-04-27 17:59:21 -07001268static int rebind_irq_to_cpu(unsigned irq, unsigned tcpu)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001269{
1270 struct evtchn_bind_vcpu bind_vcpu;
1271 int evtchn = evtchn_from_irq(irq);
1272
Ian Campbellbe494722011-03-10 16:08:02 +00001273 if (!VALID_EVTCHN(evtchn))
1274 return -1;
1275
1276 /*
1277 * Events delivered via platform PCI interrupts are always
1278 * routed to vcpu 0 and hence cannot be rebound.
1279 */
1280 if (xen_hvm_domain() && !xen_have_vector_callback)
Yinghai Lud5dedd42009-04-27 17:59:21 -07001281 return -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001282
1283 /* Send future instances of this interrupt to other vcpu. */
1284 bind_vcpu.port = evtchn;
1285 bind_vcpu.vcpu = tcpu;
1286
1287 /*
1288 * If this fails, it usually just indicates that we're dealing with a
1289 * virq or IPI channel, which don't actually need to be rebound. Ignore
1290 * it, but don't do the xenlinux-level rebind in that case.
1291 */
1292 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_vcpu, &bind_vcpu) >= 0)
1293 bind_evtchn_to_cpu(evtchn, tcpu);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001294
1295 return 0;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001296}
1297
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001298static int set_affinity_irq(struct irq_data *data, const struct cpumask *dest,
1299 bool force)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001300{
Rusty Russell0de26522008-12-13 21:20:26 +10301301 unsigned tcpu = cpumask_first(dest);
Yinghai Lud5dedd42009-04-27 17:59:21 -07001302
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001303 return rebind_irq_to_cpu(data->irq, tcpu);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001304}
1305
Isaku Yamahata642e0c82008-04-02 10:53:57 -07001306int resend_irq_on_evtchn(unsigned int irq)
1307{
1308 int masked, evtchn = evtchn_from_irq(irq);
1309 struct shared_info *s = HYPERVISOR_shared_info;
1310
1311 if (!VALID_EVTCHN(evtchn))
1312 return 1;
1313
1314 masked = sync_test_and_set_bit(evtchn, s->evtchn_mask);
1315 sync_set_bit(evtchn, s->evtchn_pending);
1316 if (!masked)
1317 unmask_evtchn(evtchn);
1318
1319 return 1;
1320}
1321
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001322static void enable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001323{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001324 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001325
1326 if (VALID_EVTCHN(evtchn))
1327 unmask_evtchn(evtchn);
1328}
1329
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001330static void disable_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001331{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001332 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001333
1334 if (VALID_EVTCHN(evtchn))
1335 mask_evtchn(evtchn);
1336}
1337
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001338static void ack_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001339{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001340 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001341
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001342 move_masked_irq(data->irq);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001343
1344 if (VALID_EVTCHN(evtchn))
Jeremy Fitzhardinge3588fe22010-08-27 17:30:24 -07001345 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001346}
1347
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001348static int retrigger_dynirq(struct irq_data *data)
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001349{
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001350 int evtchn = evtchn_from_irq(data->irq);
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001351 struct shared_info *sh = HYPERVISOR_shared_info;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001352 int ret = 0;
1353
1354 if (VALID_EVTCHN(evtchn)) {
Jeremy Fitzhardingeee8fa1c2008-03-17 16:37:19 -07001355 int masked;
1356
1357 masked = sync_test_and_set_bit(evtchn, sh->evtchn_mask);
1358 sync_set_bit(evtchn, sh->evtchn_pending);
1359 if (!masked)
1360 unmask_evtchn(evtchn);
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001361 ret = 1;
1362 }
1363
1364 return ret;
1365}
1366
Ian Campbell0a852262011-03-10 16:08:06 +00001367static void restore_pirqs(void)
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001368{
1369 int pirq, rc, irq, gsi;
1370 struct physdev_map_pirq map_irq;
Ian Campbell69c358c2011-03-10 16:08:13 +00001371 struct irq_info *info;
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001372
Ian Campbell69c358c2011-03-10 16:08:13 +00001373 list_for_each_entry(info, &xen_irq_list_head, list) {
1374 if (info->type != IRQT_PIRQ)
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001375 continue;
1376
Ian Campbell69c358c2011-03-10 16:08:13 +00001377 pirq = info->u.pirq.pirq;
1378 gsi = info->u.pirq.gsi;
1379 irq = info->irq;
1380
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001381 /* save/restore of PT devices doesn't work, so at this point the
1382 * only devices present are GSI based emulated devices */
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001383 if (!gsi)
1384 continue;
1385
1386 map_irq.domid = DOMID_SELF;
1387 map_irq.type = MAP_PIRQ_TYPE_GSI;
1388 map_irq.index = gsi;
1389 map_irq.pirq = pirq;
1390
1391 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
1392 if (rc) {
1393 printk(KERN_WARNING "xen map irq failed gsi=%d irq=%d pirq=%d rc=%d\n",
1394 gsi, irq, pirq, rc);
Ian Campbell9158c352011-03-10 16:08:09 +00001395 xen_free_irq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001396 continue;
1397 }
1398
1399 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
1400
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001401 __startup_pirq(irq);
Stefano Stabellini9a069c32010-12-01 14:51:44 +00001402 }
1403}
1404
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001405static void restore_cpu_virqs(unsigned int cpu)
1406{
1407 struct evtchn_bind_virq bind_virq;
1408 int virq, irq, evtchn;
1409
1410 for (virq = 0; virq < NR_VIRQS; virq++) {
1411 if ((irq = per_cpu(virq_to_irq, cpu)[virq]) == -1)
1412 continue;
1413
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001414 BUG_ON(virq_from_irq(irq) != virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001415
1416 /* Get a new binding from Xen. */
1417 bind_virq.virq = virq;
1418 bind_virq.vcpu = cpu;
1419 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_virq,
1420 &bind_virq) != 0)
1421 BUG();
1422 evtchn = bind_virq.port;
1423
1424 /* Record the new mapping. */
Ian Campbell3d4cfa32011-03-10 16:08:10 +00001425 xen_irq_info_virq_init(cpu, irq, evtchn, virq);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001426 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001427 }
1428}
1429
1430static void restore_cpu_ipis(unsigned int cpu)
1431{
1432 struct evtchn_bind_ipi bind_ipi;
1433 int ipi, irq, evtchn;
1434
1435 for (ipi = 0; ipi < XEN_NR_IPIS; ipi++) {
1436 if ((irq = per_cpu(ipi_to_irq, cpu)[ipi]) == -1)
1437 continue;
1438
Jeremy Fitzhardingeced40d02009-02-06 14:09:44 -08001439 BUG_ON(ipi_from_irq(irq) != ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001440
1441 /* Get a new binding from Xen. */
1442 bind_ipi.vcpu = cpu;
1443 if (HYPERVISOR_event_channel_op(EVTCHNOP_bind_ipi,
1444 &bind_ipi) != 0)
1445 BUG();
1446 evtchn = bind_ipi.port;
1447
1448 /* Record the new mapping. */
Ian Campbell3d4cfa32011-03-10 16:08:10 +00001449 xen_irq_info_ipi_init(cpu, irq, evtchn, ipi);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001450 bind_evtchn_to_cpu(evtchn, cpu);
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001451 }
1452}
1453
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001454/* Clear an irq's pending state, in preparation for polling on it */
1455void xen_clear_irq_pending(int irq)
1456{
1457 int evtchn = evtchn_from_irq(irq);
1458
1459 if (VALID_EVTCHN(evtchn))
1460 clear_evtchn(evtchn);
1461}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001462EXPORT_SYMBOL(xen_clear_irq_pending);
Jeremy Fitzhardinge168d2f42008-08-20 17:02:18 -07001463void xen_set_irq_pending(int irq)
1464{
1465 int evtchn = evtchn_from_irq(irq);
1466
1467 if (VALID_EVTCHN(evtchn))
1468 set_evtchn(evtchn);
1469}
1470
1471bool xen_test_irq_pending(int irq)
1472{
1473 int evtchn = evtchn_from_irq(irq);
1474 bool ret = false;
1475
1476 if (VALID_EVTCHN(evtchn))
1477 ret = test_evtchn(evtchn);
1478
1479 return ret;
1480}
1481
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001482/* Poll waiting for an irq to become pending with timeout. In the usual case,
1483 * the irq will be disabled so it won't deliver an interrupt. */
1484void xen_poll_irq_timeout(int irq, u64 timeout)
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001485{
1486 evtchn_port_t evtchn = evtchn_from_irq(irq);
1487
1488 if (VALID_EVTCHN(evtchn)) {
1489 struct sched_poll poll;
1490
1491 poll.nr_ports = 1;
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001492 poll.timeout = timeout;
Isaku Yamahataff3c5362008-10-14 17:50:44 -07001493 set_xen_guest_handle(poll.ports, &evtchn);
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001494
1495 if (HYPERVISOR_sched_op(SCHEDOP_poll, &poll) != 0)
1496 BUG();
1497 }
1498}
Konrad Rzeszutek Wilkd9a88142009-11-05 16:33:09 -05001499EXPORT_SYMBOL(xen_poll_irq_timeout);
1500/* Poll waiting for an irq to become pending. In the usual case, the
1501 * irq will be disabled so it won't deliver an interrupt. */
1502void xen_poll_irq(int irq)
1503{
1504 xen_poll_irq_timeout(irq, 0 /* no timeout */);
1505}
Jeremy Fitzhardinge2d9e1e22008-07-07 12:07:53 -07001506
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001507void xen_irq_resume(void)
1508{
Ian Campbell6cb65372011-03-10 16:08:11 +00001509 unsigned int cpu, evtchn;
1510 struct irq_info *info;
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001511
1512 init_evtchn_cpu_bindings();
1513
1514 /* New event-channel space is not 'live' yet. */
1515 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1516 mask_evtchn(evtchn);
1517
1518 /* No IRQ <-> event-channel mappings. */
Ian Campbell6cb65372011-03-10 16:08:11 +00001519 list_for_each_entry(info, &xen_irq_list_head, list)
1520 info->evtchn = 0; /* zap event-channel binding */
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001521
1522 for (evtchn = 0; evtchn < NR_EVENT_CHANNELS; evtchn++)
1523 evtchn_to_irq[evtchn] = -1;
1524
1525 for_each_possible_cpu(cpu) {
1526 restore_cpu_virqs(cpu);
1527 restore_cpu_ipis(cpu);
1528 }
Ian Campbell69035912010-11-01 16:30:09 +00001529
Ian Campbell0a852262011-03-10 16:08:06 +00001530 restore_pirqs();
Jeremy Fitzhardinge0e913982008-05-26 23:31:27 +01001531}
1532
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001533static struct irq_chip xen_dynamic_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001534 .name = "xen-dyn",
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001535
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001536 .irq_disable = disable_dynirq,
1537 .irq_mask = disable_dynirq,
1538 .irq_unmask = enable_dynirq,
Jeremy Fitzhardinge54a353a2009-02-06 14:09:42 -08001539
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001540 .irq_eoi = ack_dynirq,
1541 .irq_set_affinity = set_affinity_irq,
1542 .irq_retrigger = retrigger_dynirq,
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001543};
1544
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001545static struct irq_chip xen_pirq_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001546 .name = "xen-pirq",
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001547
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001548 .irq_startup = startup_pirq,
1549 .irq_shutdown = shutdown_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001550
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001551 .irq_enable = enable_pirq,
1552 .irq_unmask = enable_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001553
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001554 .irq_disable = disable_pirq,
1555 .irq_mask = disable_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001556
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001557 .irq_ack = ack_pirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001558
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001559 .irq_set_affinity = set_affinity_irq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001560
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001561 .irq_retrigger = retrigger_dynirq,
Jeremy Fitzhardinged46a78b2010-10-01 12:20:09 -04001562};
1563
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001564static struct irq_chip xen_percpu_chip __read_mostly = {
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001565 .name = "xen-percpu",
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001566
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001567 .irq_disable = disable_dynirq,
1568 .irq_mask = disable_dynirq,
1569 .irq_unmask = enable_dynirq,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001570
Thomas Gleixnerc9e265e2011-02-05 20:08:54 +00001571 .irq_ack = ack_dynirq,
Jeremy Fitzhardingeaaca4962010-08-20 18:57:53 -07001572};
1573
Sheng Yang38e20b02010-05-14 12:40:51 +01001574int xen_set_callback_via(uint64_t via)
1575{
1576 struct xen_hvm_param a;
1577 a.domid = DOMID_SELF;
1578 a.index = HVM_PARAM_CALLBACK_IRQ;
1579 a.value = via;
1580 return HYPERVISOR_hvm_op(HVMOP_set_param, &a);
1581}
1582EXPORT_SYMBOL_GPL(xen_set_callback_via);
1583
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001584#ifdef CONFIG_XEN_PVHVM
Sheng Yang38e20b02010-05-14 12:40:51 +01001585/* Vector callbacks are better than PCI interrupts to receive event
1586 * channel notifications because we can receive vector callbacks on any
1587 * vcpu and we don't need PCI support or APIC interactions. */
1588void xen_callback_vector(void)
1589{
1590 int rc;
1591 uint64_t callback_via;
1592 if (xen_have_vector_callback) {
1593 callback_via = HVM_CALLBACK_VECTOR(XEN_HVM_EVTCHN_CALLBACK);
1594 rc = xen_set_callback_via(callback_via);
1595 if (rc) {
1596 printk(KERN_ERR "Request for Xen HVM callback vector"
1597 " failed.\n");
1598 xen_have_vector_callback = 0;
1599 return;
1600 }
1601 printk(KERN_INFO "Xen HVM callback vector for event delivery is "
1602 "enabled\n");
1603 /* in the restore case the vector has already been allocated */
1604 if (!test_bit(XEN_HVM_EVTCHN_CALLBACK, used_vectors))
1605 alloc_intr_gate(XEN_HVM_EVTCHN_CALLBACK, xen_hvm_callback_vector);
1606 }
1607}
Stefano Stabellinica65f9f2010-07-29 14:37:48 +01001608#else
1609void xen_callback_vector(void) {}
1610#endif
Sheng Yang38e20b02010-05-14 12:40:51 +01001611
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001612void __init xen_init_IRQ(void)
1613{
Stefano Stabellinie5fc7342010-12-01 14:51:44 +00001614 int i;
Mike Travisc7a35892009-01-10 21:58:11 -08001615
Jeremy Fitzhardingeb21ddbf2010-06-07 16:28:49 -04001616 evtchn_to_irq = kcalloc(NR_EVENT_CHANNELS, sizeof(*evtchn_to_irq),
1617 GFP_KERNEL);
1618 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1619 evtchn_to_irq[i] = -1;
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001620
1621 init_evtchn_cpu_bindings();
1622
1623 /* No event channels are 'live' right now. */
1624 for (i = 0; i < NR_EVENT_CHANNELS; i++)
1625 mask_evtchn(i);
1626
Sheng Yang38e20b02010-05-14 12:40:51 +01001627 if (xen_hvm_domain()) {
1628 xen_callback_vector();
1629 native_init_IRQ();
Stefano Stabellini3942b742010-06-24 17:50:18 +01001630 /* pci_xen_hvm_init must be called after native_init_IRQ so that
1631 * __acpi_register_gsi can point at the right function */
1632 pci_xen_hvm_init();
Sheng Yang38e20b02010-05-14 12:40:51 +01001633 } else {
1634 irq_ctx_init(smp_processor_id());
Jeremy Fitzhardinge38aa66f2010-09-02 14:51:39 +01001635 if (xen_initial_domain())
1636 xen_setup_pirqs();
Sheng Yang38e20b02010-05-14 12:40:51 +01001637 }
Jeremy Fitzhardingee46cdb62007-07-17 18:37:05 -07001638}