xref: /freebsd/sys/dev/xen/xenpci/xenpci.c (revision f7c32ed617858bcd22f8d1b03199099d50125721)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2008 Citrix Systems, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 
38 #include <machine/bus.h>
39 #include <machine/resource.h>
40 #include <sys/rman.h>
41 
42 #include <machine/stdarg.h>
43 
44 #include <xen/xen-os.h>
45 #include <xen/features.h>
46 #include <xen/hypervisor.h>
47 #include <xen/hvm.h>
48 #include <xen/xen_intr.h>
49 
50 #include <dev/pci/pcireg.h>
51 #include <dev/pci/pcivar.h>
52 
53 #include <dev/xen/xenpci/xenpcivar.h>
54 
55 /*
56  * This is used to find our platform device instance.
57  */
58 static devclass_t xenpci_devclass;
59 
60 static int
61 xenpci_intr_filter(void *trap_frame)
62 {
63 	xen_intr_handle_upcall(trap_frame);
64 	return (FILTER_HANDLED);
65 }
66 
67 static int
68 xenpci_irq_init(device_t device, struct xenpci_softc *scp)
69 {
70 	int error;
71 
72 	error = BUS_SETUP_INTR(device_get_parent(device), device,
73 			       scp->res_irq, INTR_MPSAFE|INTR_TYPE_MISC,
74 			       xenpci_intr_filter, NULL, /*trap_frame*/NULL,
75 			       &scp->intr_cookie);
76 	if (error)
77 		return error;
78 
79 #ifdef SMP
80 	/*
81 	 * When using the PCI event delivery callback we cannot assign
82 	 * events to specific vCPUs, so all events are delivered to vCPU#0 by
83 	 * Xen. Since the PCI interrupt can fire on any CPU by default, we
84 	 * need to bind it to vCPU#0 in order to ensure that
85 	 * xen_intr_handle_upcall always gets called on vCPU#0.
86 	 */
87 	error = BUS_BIND_INTR(device_get_parent(device), device,
88 	                      scp->res_irq, 0);
89 	if (error)
90 		return error;
91 #endif
92 
93 	xen_hvm_set_callback(device);
94 	return (0);
95 }
96 
97 /*
98  * Deallocate anything allocated by xenpci_allocate_resources.
99  */
100 static int
101 xenpci_deallocate_resources(device_t dev)
102 {
103 	struct xenpci_softc *scp = device_get_softc(dev);
104 
105 	if (scp->res_irq != 0) {
106 		bus_deactivate_resource(dev, SYS_RES_IRQ,
107 			scp->rid_irq, scp->res_irq);
108 		bus_release_resource(dev, SYS_RES_IRQ,
109 			scp->rid_irq, scp->res_irq);
110 		scp->res_irq = 0;
111 	}
112 
113 	return (0);
114 }
115 
116 /*
117  * Allocate irq and memory resources.
118  */
119 static int
120 xenpci_allocate_resources(device_t dev)
121 {
122 	struct xenpci_softc *scp = device_get_softc(dev);
123 
124 	scp->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
125 			&scp->rid_irq, RF_SHAREABLE|RF_ACTIVE);
126 	if (scp->res_irq == NULL) {
127 		printf("xenpci Could not allocate irq.\n");
128 		goto errexit;
129 	}
130 
131 	return (0);
132 
133 errexit:
134 	/* Cleanup anything we may have assigned. */
135 	xenpci_deallocate_resources(dev);
136 	return (ENXIO); /* For want of a better idea. */
137 }
138 
139 /*
140  * Probe - just check device ID.
141  */
142 static int
143 xenpci_probe(device_t dev)
144 {
145 
146 	if (pci_get_devid(dev) != 0x00015853)
147 		return (ENXIO);
148 
149 	device_set_desc(dev, "Xen Platform Device");
150 	return (BUS_PROBE_DEFAULT);
151 }
152 
153 /*
154  * Attach - find resources and talk to Xen.
155  */
156 static int
157 xenpci_attach(device_t dev)
158 {
159 	struct xenpci_softc *scp = device_get_softc(dev);
160 	int error;
161 
162 	error = xenpci_allocate_resources(dev);
163 	if (error) {
164 		device_printf(dev, "xenpci_allocate_resources failed(%d).\n",
165 		    error);
166 		goto errexit;
167 	}
168 
169 	/*
170 	 * Hook the irq up to evtchn
171 	 */
172 	error = xenpci_irq_init(dev, scp);
173 	if (error) {
174 		device_printf(dev, "xenpci_irq_init failed(%d).\n",
175 			error);
176 		goto errexit;
177 	}
178 
179 	return (0);
180 
181 errexit:
182 	/*
183 	 * Undo anything we may have done.
184 	 */
185 	xenpci_deallocate_resources(dev);
186 	return (error);
187 }
188 
189 /*
190  * Detach - reverse anything done by attach.
191  */
192 static int
193 xenpci_detach(device_t dev)
194 {
195 	struct xenpci_softc *scp = device_get_softc(dev);
196 	device_t parent = device_get_parent(dev);
197 
198 	/*
199 	 * Take our interrupt handler out of the list of handlers
200 	 * that can handle this irq.
201 	 */
202 	if (scp->intr_cookie != NULL) {
203 		if (BUS_TEARDOWN_INTR(parent, dev,
204 		    scp->res_irq, scp->intr_cookie) != 0)
205 			device_printf(dev,
206 			    "intr teardown failed.. continuing\n");
207 		scp->intr_cookie = NULL;
208 	}
209 
210 	/*
211 	 * Deallocate any system resources we may have
212 	 * allocated on behalf of this driver.
213 	 */
214 	return (xenpci_deallocate_resources(dev));
215 }
216 
217 static int
218 xenpci_resume(device_t dev)
219 {
220 	xen_hvm_set_callback(dev);
221 	return (0);
222 }
223 
224 static device_method_t xenpci_methods[] = {
225 	/* Device interface */
226 	DEVMETHOD(device_probe,		xenpci_probe),
227 	DEVMETHOD(device_attach,	xenpci_attach),
228 	DEVMETHOD(device_detach,	xenpci_detach),
229 	DEVMETHOD(device_resume,	xenpci_resume),
230 	{ 0, 0 }
231 };
232 
233 static driver_t xenpci_driver = {
234 	"xenpci",
235 	xenpci_methods,
236 	sizeof(struct xenpci_softc),
237 };
238 
239 DRIVER_MODULE(xenpci, pci, xenpci_driver, xenpci_devclass, 0, 0);
240