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