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