xref: /freebsd/sys/dev/xen/xenpci/xenpci.c (revision 4d846d26)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 static int
56 xenpci_irq_init(device_t device, struct xenpci_softc *scp)
57 {
58 	int error;
59 
60 	error = BUS_SETUP_INTR(device_get_parent(device), device,
61 			       scp->res_irq, INTR_MPSAFE|INTR_TYPE_MISC,
62 			       xen_intr_handle_upcall, NULL, NULL,
63 			       &scp->intr_cookie);
64 	if (error)
65 		return error;
66 
67 #ifdef SMP
68 	/*
69 	 * When using the PCI event delivery callback we cannot assign
70 	 * events to specific vCPUs, so all events are delivered to vCPU#0 by
71 	 * Xen. Since the PCI interrupt can fire on any CPU by default, we
72 	 * need to bind it to vCPU#0 in order to ensure that
73 	 * xen_intr_handle_upcall always gets called on vCPU#0.
74 	 */
75 	error = BUS_BIND_INTR(device_get_parent(device), device,
76 	                      scp->res_irq, 0);
77 	if (error)
78 		return error;
79 #endif
80 
81 	xen_hvm_set_callback(device);
82 	return (0);
83 }
84 
85 /*
86  * Deallocate anything allocated by xenpci_allocate_resources.
87  */
88 static int
89 xenpci_deallocate_resources(device_t dev)
90 {
91 	struct xenpci_softc *scp = device_get_softc(dev);
92 
93 	if (scp->res_irq != 0) {
94 		bus_deactivate_resource(dev, SYS_RES_IRQ,
95 			scp->rid_irq, scp->res_irq);
96 		bus_release_resource(dev, SYS_RES_IRQ,
97 			scp->rid_irq, scp->res_irq);
98 		scp->res_irq = 0;
99 	}
100 
101 	return (0);
102 }
103 
104 /*
105  * Allocate irq and memory resources.
106  */
107 static int
108 xenpci_allocate_resources(device_t dev)
109 {
110 	struct xenpci_softc *scp = device_get_softc(dev);
111 
112 	scp->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
113 			&scp->rid_irq, RF_SHAREABLE|RF_ACTIVE);
114 	if (scp->res_irq == NULL) {
115 		printf("xenpci Could not allocate irq.\n");
116 		goto errexit;
117 	}
118 
119 	return (0);
120 
121 errexit:
122 	/* Cleanup anything we may have assigned. */
123 	xenpci_deallocate_resources(dev);
124 	return (ENXIO); /* For want of a better idea. */
125 }
126 
127 /*
128  * Probe - just check device ID.
129  */
130 static int
131 xenpci_probe(device_t dev)
132 {
133 
134 	if (pci_get_devid(dev) != 0x00015853)
135 		return (ENXIO);
136 
137 	device_set_desc(dev, "Xen Platform Device");
138 	return (BUS_PROBE_DEFAULT);
139 }
140 
141 /*
142  * Attach - find resources and talk to Xen.
143  */
144 static int
145 xenpci_attach(device_t dev)
146 {
147 	struct xenpci_softc *scp = device_get_softc(dev);
148 	int error;
149 
150 	error = xenpci_allocate_resources(dev);
151 	if (error) {
152 		device_printf(dev, "xenpci_allocate_resources failed(%d).\n",
153 		    error);
154 		goto errexit;
155 	}
156 
157 	/*
158 	 * Hook the irq up to evtchn
159 	 */
160 	error = xenpci_irq_init(dev, scp);
161 	if (error) {
162 		device_printf(dev, "xenpci_irq_init failed(%d).\n",
163 			error);
164 		goto errexit;
165 	}
166 
167 	return (0);
168 
169 errexit:
170 	/*
171 	 * Undo anything we may have done.
172 	 */
173 	xenpci_deallocate_resources(dev);
174 	return (error);
175 }
176 
177 /*
178  * Detach - reverse anything done by attach.
179  */
180 static int
181 xenpci_detach(device_t dev)
182 {
183 	struct xenpci_softc *scp = device_get_softc(dev);
184 	device_t parent = device_get_parent(dev);
185 
186 	/*
187 	 * Take our interrupt handler out of the list of handlers
188 	 * that can handle this irq.
189 	 */
190 	if (scp->intr_cookie != NULL) {
191 		if (BUS_TEARDOWN_INTR(parent, dev,
192 		    scp->res_irq, scp->intr_cookie) != 0)
193 			device_printf(dev,
194 			    "intr teardown failed.. continuing\n");
195 		scp->intr_cookie = NULL;
196 	}
197 
198 	/*
199 	 * Deallocate any system resources we may have
200 	 * allocated on behalf of this driver.
201 	 */
202 	return (xenpci_deallocate_resources(dev));
203 }
204 
205 static int
206 xenpci_resume(device_t dev)
207 {
208 	xen_hvm_set_callback(dev);
209 	return (0);
210 }
211 
212 static device_method_t xenpci_methods[] = {
213 	/* Device interface */
214 	DEVMETHOD(device_probe,		xenpci_probe),
215 	DEVMETHOD(device_attach,	xenpci_attach),
216 	DEVMETHOD(device_detach,	xenpci_detach),
217 	DEVMETHOD(device_resume,	xenpci_resume),
218 	{ 0, 0 }
219 };
220 
221 static driver_t xenpci_driver = {
222 	"xenpci",
223 	xenpci_methods,
224 	sizeof(struct xenpci_softc),
225 };
226 
227 DRIVER_MODULE(xenpci, pci, xenpci_driver, 0, 0);
228