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