ia64/xen-unstable

view unmodified_drivers/linux-2.6/platform-pci/evtchn.c @ 13234:dbddea768e29

[PV-ON-HVM] Update evtchn interface to match new PV Linux interfaces.
Also fix unbind_from_irqhandler() to close event-channel port where
that is appropriate.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@localhost.localdomain
date Sun Dec 31 11:59:47 2006 +0000 (2006-12-31)
parents d37b210bb8a7
children d3e40fd6038e
line source
1 /******************************************************************************
2 * evtchn.c
3 *
4 * A simplified event channel for para-drivers in unmodified linux
5 *
6 * Copyright (c) 2002-2005, K A Fraser
7 * Copyright (c) 2005, Intel Corporation <xiaofeng.ling@intel.com>
8 *
9 * This file may be distributed separately from the Linux kernel, or
10 * incorporated into other software packages, subject to the following license:
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this source file (the "Software"), to deal in the Software without
14 * restriction, including without limitation the rights to use, copy, modify,
15 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
16 * and to permit persons to whom the Software is furnished to do so, subject to
17 * the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 * IN THE SOFTWARE.
29 */
31 #include <linux/config.h>
32 #include <linux/module.h>
33 #include <linux/kernel.h>
34 #include <xen/evtchn.h>
35 #include <xen/interface/hvm/ioreq.h>
36 #include <xen/features.h>
37 #include "platform-pci.h"
39 #ifdef HAVE_XEN_PLATFORM_COMPAT_H
40 #include <xen/platform-compat.h>
41 #endif
43 void *shared_info_area;
45 #define MAX_EVTCHN 256
46 static struct {
47 irqreturn_t(*handler) (int, void *, struct pt_regs *);
48 void *dev_id;
49 int close; /* close on unbind_from_irqhandler()? */
50 } evtchns[MAX_EVTCHN];
52 void mask_evtchn(int port)
53 {
54 shared_info_t *s = shared_info_area;
55 synch_set_bit(port, &s->evtchn_mask[0]);
56 }
57 EXPORT_SYMBOL(mask_evtchn);
59 void unmask_evtchn(int port)
60 {
61 unsigned int cpu;
62 shared_info_t *s = shared_info_area;
63 vcpu_info_t *vcpu_info;
65 preempt_disable();
66 cpu = smp_processor_id();
67 vcpu_info = &s->vcpu_info[cpu];
69 /* Slow path (hypercall) if this is a non-local port. We only
70 ever bind event channels to vcpu 0 in HVM guests. */
71 if (unlikely(cpu != 0)) {
72 evtchn_unmask_t op = { .port = port };
73 (void)HYPERVISOR_event_channel_op(EVTCHNOP_unmask,
74 &op);
75 preempt_enable();
76 return;
77 }
79 synch_clear_bit(port, &s->evtchn_mask[0]);
81 /*
82 * The following is basically the equivalent of
83 * 'hw_resend_irq'. Just like a real IO-APIC we 'lose the
84 * interrupt edge' if the channel is masked.
85 */
86 if (synch_test_bit(port, &s->evtchn_pending[0]) &&
87 !synch_test_and_set_bit(port / BITS_PER_LONG,
88 &vcpu_info->evtchn_pending_sel)) {
89 vcpu_info->evtchn_upcall_pending = 1;
90 if (!vcpu_info->evtchn_upcall_mask)
91 force_evtchn_callback();
92 }
93 preempt_enable();
94 }
95 EXPORT_SYMBOL(unmask_evtchn);
97 int bind_listening_port_to_irqhandler(
98 unsigned int remote_domain,
99 irqreturn_t (*handler)(int, void *, struct pt_regs *),
100 unsigned long irqflags,
101 const char *devname,
102 void *dev_id)
103 {
104 struct evtchn_alloc_unbound alloc_unbound;
105 int err;
107 alloc_unbound.dom = DOMID_SELF;
108 alloc_unbound.remote_dom = remote_domain;
110 err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
111 &alloc_unbound);
112 if (err)
113 return err;
115 evtchns[alloc_unbound.port].handler = handler;
116 evtchns[alloc_unbound.port].dev_id = dev_id;
117 evtchns[alloc_unbound.port].close = 1;
118 unmask_evtchn(alloc_unbound.port);
119 return alloc_unbound.port;
120 }
121 EXPORT_SYMBOL(bind_listening_port_to_irqhandler);
123 int bind_caller_port_to_irqhandler(
124 unsigned int caller_port,
125 irqreturn_t (*handler)(int, void *, struct pt_regs *),
126 unsigned long irqflags,
127 const char *devname,
128 void *dev_id)
129 {
130 if (caller_port >= MAX_EVTCHN)
131 return -EINVAL;
132 evtchns[caller_port].handler = handler;
133 evtchns[caller_port].dev_id = dev_id;
134 evtchns[caller_port].close = 0;
135 unmask_evtchn(caller_port);
136 return caller_port;
137 }
138 EXPORT_SYMBOL(bind_caller_port_to_irqhandler);
140 void unbind_from_irqhandler(unsigned int evtchn, void *dev_id)
141 {
142 if (evtchn >= MAX_EVTCHN)
143 return;
145 mask_evtchn(evtchn);
146 evtchns[evtchn].handler = NULL;
148 if (evtchns[evtchn].close) {
149 struct evtchn_close close = { .port = evtchn };
150 HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
151 }
152 }
153 EXPORT_SYMBOL(unbind_from_irqhandler);
155 void notify_remote_via_irq(int irq)
156 {
157 int evtchn = irq;
158 notify_remote_via_evtchn(evtchn);
159 }
160 EXPORT_SYMBOL(notify_remote_via_irq);
162 irqreturn_t evtchn_interrupt(int irq, void *dev_id, struct pt_regs *regs)
163 {
164 unsigned int l1i, port;
165 int cpu = smp_processor_id();
166 irqreturn_t(*handler) (int, void *, struct pt_regs *);
167 shared_info_t *s = shared_info_area;
168 vcpu_info_t *v = &s->vcpu_info[cpu];
169 unsigned long l1, l2;
171 v->evtchn_upcall_pending = 0;
172 /* NB. No need for a barrier here -- XCHG is a barrier on x86. */
173 l1 = xchg(&v->evtchn_pending_sel, 0);
174 while (l1 != 0) {
175 l1i = __ffs(l1);
176 l1 &= ~(1 << l1i);
177 while ((l2 = s->evtchn_pending[l1i] & ~s->evtchn_mask[l1i])) {
178 port = (l1i * BITS_PER_LONG) + __ffs(l2);
179 synch_clear_bit(port, &s->evtchn_pending[0]);
180 if ((handler = evtchns[port].handler) != NULL)
181 handler(port, evtchns[port].dev_id,
182 regs);
183 else
184 printk(KERN_WARNING "unexpected event channel "
185 "upcall on port %d!\n", port);
186 }
187 }
189 return IRQ_HANDLED;
190 }
192 void force_evtchn_callback(void)
193 {
194 (void)HYPERVISOR_xen_version(0, NULL);
195 }
196 EXPORT_SYMBOL(force_evtchn_callback);