1 /*- 2 * Copyright (c) 2015 Landon Fuller <landon@landonf.org> 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 * without modification. 11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 12 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 13 * redistribution must be conditioned upon including a substantially 14 * similar Disclaimer requirement for further binary redistribution. 15 * 16 * NO WARRANTY 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 20 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 21 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 22 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 27 * THE POSSIBILITY OF SUCH DAMAGES. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/kernel.h> 35 #include <sys/bus.h> 36 #include <sys/module.h> 37 38 #include <dev/bhnd/bhnd_ids.h> 39 #include <dev/bhnd/bhndb/bhndbvar.h> 40 #include <dev/bhnd/bhndb/bhndb_hwdata.h> 41 42 #include "sibavar.h" 43 44 /* 45 * Supports attachment of siba(4) bus devices via a bhndb bridge. 46 */ 47 48 // 49 // TODO: PCI rev < 6 interrupt handling 50 // 51 // On early PCI cores (rev < 6) interrupt masking is handled via interconnect 52 // configuration registers (SBINTVEC), rather than the PCI_INT_MASK 53 // config register. 54 // 55 // On those devices, we should handle interrupts locally using SBINTVEC, rather 56 // than delegating to our parent bhndb device. 57 // 58 59 static int 60 siba_bhndb_probe(device_t dev) 61 { 62 const struct bhnd_chipid *cid; 63 64 /* Check bus type */ 65 cid = BHNDB_GET_CHIPID(device_get_parent(dev), dev); 66 if (cid->chip_type != BHND_CHIPTYPE_SIBA) 67 return (ENXIO); 68 69 /* Delegate to default probe implementation */ 70 return (siba_probe(dev)); 71 } 72 73 static int 74 siba_bhndb_attach(device_t dev) 75 { 76 struct siba_softc *sc; 77 const struct bhnd_chipid *chipid; 78 int error; 79 80 sc = device_get_softc(dev); 81 82 /* Enumerate our children. */ 83 chipid = BHNDB_GET_CHIPID(device_get_parent(dev), dev); 84 if ((error = siba_add_children(dev, chipid))) 85 return (error); 86 87 /* Initialize full bridge configuration */ 88 error = BHNDB_INIT_FULL_CONFIG(device_get_parent(dev), dev, 89 bhndb_siba_priority_table); 90 if (error) 91 return (error); 92 93 /* Ask our parent bridge to find the corresponding bridge core */ 94 sc->hostb_dev = BHNDB_FIND_HOSTB_DEVICE(device_get_parent(dev), dev); 95 96 /* Call our superclass' implementation */ 97 return (siba_attach(dev)); 98 } 99 100 /* Suspend all references to the device's cfg register blocks */ 101 static void 102 siba_bhndb_suspend_cfgblocks(device_t dev, struct siba_devinfo *dinfo) { 103 for (u_int i = 0; i < dinfo->core_id.num_cfg_blocks; i++) { 104 if (dinfo->cfg[i] == NULL) 105 continue; 106 107 BHNDB_SUSPEND_RESOURCE(device_get_parent(dev), dev, 108 SYS_RES_MEMORY, dinfo->cfg[i]->res); 109 } 110 } 111 112 static int 113 siba_bhndb_suspend_child(device_t dev, device_t child) 114 { 115 struct siba_devinfo *dinfo; 116 int error; 117 118 if (device_get_parent(child) != dev) 119 BUS_SUSPEND_CHILD(device_get_parent(dev), child); 120 121 dinfo = device_get_ivars(child); 122 123 /* Suspend the child */ 124 if ((error = bhnd_generic_br_suspend_child(dev, child))) 125 return (error); 126 127 /* Suspend resource references to the child's config registers */ 128 siba_bhndb_suspend_cfgblocks(dev, dinfo); 129 130 return (0); 131 } 132 133 static int 134 siba_bhndb_resume_child(device_t dev, device_t child) 135 { 136 struct siba_devinfo *dinfo; 137 int error; 138 139 if (device_get_parent(child) != dev) 140 BUS_SUSPEND_CHILD(device_get_parent(dev), child); 141 142 if (!device_is_suspended(child)) 143 return (EBUSY); 144 145 dinfo = device_get_ivars(child); 146 147 /* Resume all resource references to the child's config registers */ 148 for (u_int i = 0; i < dinfo->core_id.num_cfg_blocks; i++) { 149 if (dinfo->cfg[i] == NULL) 150 continue; 151 152 error = BHNDB_RESUME_RESOURCE(device_get_parent(dev), dev, 153 SYS_RES_MEMORY, dinfo->cfg[i]->res); 154 if (error) { 155 siba_bhndb_suspend_cfgblocks(dev, dinfo); 156 return (error); 157 } 158 } 159 160 /* Resume the child */ 161 if ((error = bhnd_generic_br_resume_child(dev, child))) { 162 siba_bhndb_suspend_cfgblocks(dev, dinfo); 163 return (error); 164 } 165 166 return (0); 167 } 168 169 static device_method_t siba_bhndb_methods[] = { 170 /* Device interface */ 171 DEVMETHOD(device_probe, siba_bhndb_probe), 172 DEVMETHOD(device_attach, siba_bhndb_attach), 173 174 /* Bus interface */ 175 DEVMETHOD(bus_suspend_child, siba_bhndb_suspend_child), 176 DEVMETHOD(bus_resume_child, siba_bhndb_resume_child), 177 178 DEVMETHOD_END 179 }; 180 181 DEFINE_CLASS_1(bhnd, siba_bhndb_driver, siba_bhndb_methods, 182 sizeof(struct siba_softc), siba_driver); 183 184 DRIVER_MODULE(siba_bhndb, bhndb, siba_bhndb_driver, bhnd_devclass, NULL, NULL); 185 186 MODULE_VERSION(siba_bhndb, 1); 187 MODULE_DEPEND(siba_bhndb, siba, 1, 1, 1); 188 MODULE_DEPEND(siba_bhndb, bhnd, 1, 1, 1); 189 MODULE_DEPEND(siba_bhndb, bhndb, 1, 1, 1); 190