1 /*- 2 * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org> 3 * Copyright (c) 2017 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by Landon Fuller 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer, 14 * without modification. 15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 16 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 17 * redistribution must be conditioned upon including a substantially 18 * similar Disclaimer requirement for further binary redistribution. 19 * 20 * NO WARRANTY 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 24 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 25 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 26 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 29 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 31 * THE POSSIBILITY OF SUCH DAMAGES. 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include <sys/param.h> 38 #include <sys/kernel.h> 39 #include <sys/bus.h> 40 #include <sys/module.h> 41 #include <sys/systm.h> 42 43 #include <dev/bhnd/bhnd_ids.h> 44 #include <dev/bhnd/bhndb/bhndbvar.h> 45 #include <dev/bhnd/bhndb/bhndb_hwdata.h> 46 47 #include "sibareg.h" 48 #include "sibavar.h" 49 50 /* 51 * Supports attachment of siba(4) bus devices via a bhndb bridge. 52 */ 53 54 // 55 // TODO: PCI rev < 6 interrupt handling 56 // 57 // On early PCI cores (rev < 6) interrupt masking is handled via interconnect 58 // configuration registers (SBINTVEC), rather than the PCI_INT_MASK 59 // config register. 60 // 61 // On those devices, we should handle interrupts locally using SBINTVEC, rather 62 // than delegating to our parent bhndb device. 63 // 64 65 static int siba_bhndb_wars_hwup(struct siba_softc *sc); 66 67 /* Bridge-specific core device quirks */ 68 enum { 69 /** When PCIe-bridged, the D11 core's initiator request 70 * timeout must be disabled to prevent D11 from entering a 71 * RESP_TIMEOUT error state. */ 72 SIBA_QUIRK_PCIE_D11_SB_TIMEOUT = (1<<0) 73 }; 74 75 static struct bhnd_device_quirk bridge_quirks[] = { 76 BHND_CHIP_QUIRK(4311, HWREV_EQ(2), SIBA_QUIRK_PCIE_D11_SB_TIMEOUT), 77 BHND_CHIP_QUIRK(4312, HWREV_EQ(0), SIBA_QUIRK_PCIE_D11_SB_TIMEOUT), 78 BHND_DEVICE_QUIRK_END 79 }; 80 81 static struct bhnd_device bridge_devs[] = { 82 BHND_DEVICE(BCM, PCI, NULL, bridge_quirks), 83 BHND_DEVICE_END 84 }; 85 86 static int 87 siba_bhndb_probe(device_t dev) 88 { 89 const struct bhnd_chipid *cid; 90 int error; 91 92 /* Defer to default probe implementation */ 93 if ((error = siba_probe(dev)) > 0) 94 return (error); 95 96 /* Check bus type */ 97 cid = BHNDB_GET_CHIPID(device_get_parent(dev), dev); 98 if (cid->chip_type != BHND_CHIPTYPE_SIBA) 99 return (ENXIO); 100 101 /* Set device description */ 102 bhnd_set_default_bus_desc(dev, cid); 103 104 return (error); 105 } 106 107 static int 108 siba_bhndb_attach(device_t dev) 109 { 110 struct siba_softc *sc; 111 int error; 112 113 sc = device_get_softc(dev); 114 115 /* Perform initial attach and enumerate our children. */ 116 if ((error = siba_attach(dev))) 117 goto failed; 118 119 /* Apply attach/resume workarounds before any child drivers attach */ 120 if ((error = siba_bhndb_wars_hwup(sc))) 121 goto failed; 122 123 /* Delegate remainder to standard bhnd method implementation */ 124 if ((error = bhnd_generic_attach(dev))) 125 goto failed; 126 127 return (0); 128 129 failed: 130 device_delete_children(dev); 131 return (error); 132 } 133 134 static int 135 siba_bhndb_resume(device_t dev) 136 { 137 struct siba_softc *sc; 138 int error; 139 140 sc = device_get_softc(dev); 141 142 /* Apply attach/resume work-arounds */ 143 if ((error = siba_bhndb_wars_hwup(sc))) 144 return (error); 145 146 /* Call our superclass' implementation */ 147 return (siba_resume(dev)); 148 } 149 150 /* Suspend all references to the device's cfg register blocks */ 151 static void 152 siba_bhndb_suspend_cfgblocks(device_t dev, struct siba_devinfo *dinfo) { 153 for (u_int i = 0; i < dinfo->core_id.num_cfg_blocks; i++) { 154 if (dinfo->cfg[i] == NULL) 155 continue; 156 157 BHNDB_SUSPEND_RESOURCE(device_get_parent(dev), dev, 158 SYS_RES_MEMORY, dinfo->cfg[i]->res); 159 } 160 } 161 162 static int 163 siba_bhndb_suspend_child(device_t dev, device_t child) 164 { 165 struct siba_devinfo *dinfo; 166 int error; 167 168 if (device_get_parent(child) != dev) 169 BUS_SUSPEND_CHILD(device_get_parent(dev), child); 170 171 dinfo = device_get_ivars(child); 172 173 /* Suspend the child */ 174 if ((error = bhnd_generic_br_suspend_child(dev, child))) 175 return (error); 176 177 /* Suspend resource references to the child's config registers */ 178 siba_bhndb_suspend_cfgblocks(dev, dinfo); 179 180 return (0); 181 } 182 183 static int 184 siba_bhndb_resume_child(device_t dev, device_t child) 185 { 186 struct siba_devinfo *dinfo; 187 int error; 188 189 if (device_get_parent(child) != dev) 190 BUS_SUSPEND_CHILD(device_get_parent(dev), child); 191 192 if (!device_is_suspended(child)) 193 return (EBUSY); 194 195 dinfo = device_get_ivars(child); 196 197 /* Resume all resource references to the child's config registers */ 198 for (u_int i = 0; i < dinfo->core_id.num_cfg_blocks; i++) { 199 if (dinfo->cfg[i] == NULL) 200 continue; 201 202 error = BHNDB_RESUME_RESOURCE(device_get_parent(dev), dev, 203 SYS_RES_MEMORY, dinfo->cfg[i]->res); 204 if (error) { 205 siba_bhndb_suspend_cfgblocks(dev, dinfo); 206 return (error); 207 } 208 } 209 210 /* Resume the child */ 211 if ((error = bhnd_generic_br_resume_child(dev, child))) { 212 siba_bhndb_suspend_cfgblocks(dev, dinfo); 213 return (error); 214 } 215 216 return (0); 217 } 218 219 /* Work-around implementation for SIBA_QUIRK_PCIE_D11_SB_TIMEOUT */ 220 static int 221 siba_bhndb_wars_pcie_clear_d11_timeout(struct siba_softc *sc) 222 { 223 struct siba_devinfo *dinfo; 224 device_t hostb_dev; 225 device_t d11; 226 uint32_t imcfg; 227 228 /* Only applies when bridged by PCIe */ 229 if ((hostb_dev = bhnd_bus_find_hostb_device(sc->dev)) == NULL) 230 return (ENXIO); 231 232 if (bhnd_get_class(hostb_dev) != BHND_DEVCLASS_PCIE) 233 return (0); 234 235 /* Only applies if there's a D11 core */ 236 d11 = bhnd_bus_match_child(sc->dev, &(struct bhnd_core_match) { 237 BHND_MATCH_CORE(BHND_MFGID_BCM, BHND_COREID_D11), 238 BHND_MATCH_CORE_UNIT(0) 239 }); 240 if (d11 == NULL) 241 return (0); 242 243 /* Clear initiator timeout in D11's CFG0 block */ 244 dinfo = device_get_ivars(d11); 245 KASSERT(dinfo->cfg[0] != NULL, ("missing core config mapping")); 246 247 imcfg = bhnd_bus_read_4(dinfo->cfg[0], SIBA_CFG0_IMCONFIGLOW); 248 imcfg &= ~SIBA_IMCL_RTO_MASK; 249 250 bhnd_bus_write_4(dinfo->cfg[0], SIBA_CFG0_IMCONFIGLOW, imcfg); 251 252 return (0); 253 } 254 255 /** 256 * Apply any hardware workarounds that are required upon attach or resume 257 * of the bus. 258 */ 259 static int 260 siba_bhndb_wars_hwup(struct siba_softc *sc) 261 { 262 device_t hostb_dev; 263 uint32_t quirks; 264 int error; 265 266 if ((hostb_dev = bhnd_bus_find_hostb_device(sc->dev)) == NULL) 267 return (ENXIO); 268 269 quirks = bhnd_device_quirks(hostb_dev, bridge_devs, 270 sizeof(bridge_devs[0])); 271 272 if (quirks & SIBA_QUIRK_PCIE_D11_SB_TIMEOUT) { 273 if ((error = siba_bhndb_wars_pcie_clear_d11_timeout(sc))) 274 return (error); 275 } 276 277 return (0); 278 } 279 280 static device_method_t siba_bhndb_methods[] = { 281 /* Device interface */ 282 DEVMETHOD(device_probe, siba_bhndb_probe), 283 DEVMETHOD(device_attach, siba_bhndb_attach), 284 DEVMETHOD(device_resume, siba_bhndb_resume), 285 286 /* Bus interface */ 287 DEVMETHOD(bus_suspend_child, siba_bhndb_suspend_child), 288 DEVMETHOD(bus_resume_child, siba_bhndb_resume_child), 289 290 DEVMETHOD_END 291 }; 292 293 DEFINE_CLASS_2(bhnd, siba_bhndb_driver, siba_bhndb_methods, 294 sizeof(struct siba_softc), bhnd_bhndb_driver, siba_driver); 295 296 DRIVER_MODULE(siba_bhndb, bhndb, siba_bhndb_driver, bhnd_devclass, NULL, NULL); 297 298 MODULE_VERSION(siba_bhndb, 1); 299 MODULE_DEPEND(siba_bhndb, siba, 1, 1, 1); 300 MODULE_DEPEND(siba_bhndb, bhnd, 1, 1, 1); 301 MODULE_DEPEND(siba_bhndb, bhndb, 1, 1, 1); 302