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