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/param.h> 35 #include <sys/kernel.h> 36 #include <sys/bus.h> 37 #include <sys/module.h> 38 #include <sys/systm.h> 39 40 #include <dev/bhnd/bhnd_ids.h> 41 #include <dev/bhnd/bhndb/bhndbvar.h> 42 #include <dev/bhnd/bhndb/bhndb_hwdata.h> 43 44 #include "sibareg.h" 45 #include "sibavar.h" 46 47 /* 48 * Supports attachment of siba(4) bus devices via a bhndb bridge. 49 */ 50 51 struct siba_bhndb_softc; 52 53 static int siba_bhndb_wars_hwup(struct siba_bhndb_softc *sc); 54 55 /* siba_bhndb per-instance state */ 56 struct siba_bhndb_softc { 57 struct siba_softc siba; /**< common siba per-instance state */ 58 uint32_t quirks; /**< bus-level quirks */ 59 }; 60 61 /* siba_bhndb quirks */ 62 enum { 63 /** When PCIe-bridged, the D11 core's initiator request 64 * timeout must be disabled to prevent D11 from entering a 65 * RESP_TIMEOUT error state. */ 66 SIBA_QUIRK_PCIE_D11_SB_TIMEOUT = (1<<0) 67 }; 68 69 /* Bus-level quirks when bridged via a PCI host bridge core */ 70 static struct bhnd_device_quirk pci_bridge_quirks[] = { 71 BHND_DEVICE_QUIRK_END 72 }; 73 74 /* Bus-level quirks when bridged via a PCIe host bridge core */ 75 static struct bhnd_device_quirk pcie_bridge_quirks[] = { 76 BHND_CHIP_QUIRK (4311, HWREV_EQ(2), SIBA_QUIRK_PCIE_D11_SB_TIMEOUT), 77 BHND_CHIP_QUIRK (4312, HWREV_ANY, SIBA_QUIRK_PCIE_D11_SB_TIMEOUT), 78 BHND_DEVICE_QUIRK_END 79 }; 80 81 /* Bus-level quirks specific to a particular host bridge core */ 82 static struct bhnd_device bridge_devs[] = { 83 BHND_DEVICE(BCM, PCI, NULL, pci_bridge_quirks), 84 BHND_DEVICE(BCM, PCIE, NULL, pcie_bridge_quirks), 85 BHND_DEVICE_END 86 }; 87 88 static int 89 siba_bhndb_probe(device_t dev) 90 { 91 const struct bhnd_chipid *cid; 92 int error; 93 94 /* Defer to default probe implementation */ 95 if ((error = siba_probe(dev)) > 0) 96 return (error); 97 98 /* Check bus type */ 99 cid = BHNDB_GET_CHIPID(device_get_parent(dev), dev); 100 if (cid->chip_type != BHND_CHIPTYPE_SIBA) 101 return (ENXIO); 102 103 /* Set device description */ 104 bhnd_set_default_bus_desc(dev, cid); 105 106 return (error); 107 } 108 109 static int 110 siba_bhndb_attach(device_t dev) 111 { 112 struct siba_bhndb_softc *sc; 113 device_t hostb; 114 int error; 115 116 sc = device_get_softc(dev); 117 sc->quirks = 0; 118 119 /* Perform initial attach and enumerate our children. */ 120 if ((error = siba_attach(dev))) 121 return (error); 122 123 /* Fetch bus-level quirks required by the host bridge core */ 124 if ((hostb = bhnd_bus_find_hostb_device(dev)) != NULL) { 125 sc->quirks |= bhnd_device_quirks(hostb, bridge_devs, 126 sizeof(bridge_devs[0])); 127 } 128 129 /* Apply attach/resume workarounds before any child drivers attach */ 130 if ((error = siba_bhndb_wars_hwup(sc))) 131 goto failed; 132 133 /* Delegate remainder to standard bhnd method implementation */ 134 if ((error = bhnd_generic_attach(dev))) 135 goto failed; 136 137 return (0); 138 139 failed: 140 siba_detach(dev); 141 return (error); 142 } 143 144 static int 145 siba_bhndb_resume(device_t dev) 146 { 147 struct siba_bhndb_softc *sc; 148 int error; 149 150 sc = device_get_softc(dev); 151 152 /* Apply attach/resume work-arounds */ 153 if ((error = siba_bhndb_wars_hwup(sc))) 154 return (error); 155 156 /* Call our superclass' implementation */ 157 return (siba_resume(dev)); 158 } 159 160 /* Suspend all references to the device's cfg register blocks */ 161 static void 162 siba_bhndb_suspend_cfgblocks(device_t dev, struct siba_devinfo *dinfo) { 163 for (u_int i = 0; i < dinfo->core_id.num_cfg_blocks; i++) { 164 if (dinfo->cfg_res[i] == NULL) 165 continue; 166 167 BHNDB_SUSPEND_RESOURCE(device_get_parent(dev), dev, 168 SYS_RES_MEMORY, dinfo->cfg_res[i]->res); 169 } 170 } 171 172 static int 173 siba_bhndb_suspend_child(device_t dev, device_t child) 174 { 175 struct siba_devinfo *dinfo; 176 int error; 177 178 if (device_get_parent(child) != dev) 179 BUS_SUSPEND_CHILD(device_get_parent(dev), child); 180 181 dinfo = device_get_ivars(child); 182 183 /* Suspend the child */ 184 if ((error = bhnd_generic_br_suspend_child(dev, child))) 185 return (error); 186 187 /* Suspend resource references to the child's config registers */ 188 siba_bhndb_suspend_cfgblocks(dev, dinfo); 189 190 return (0); 191 } 192 193 static int 194 siba_bhndb_resume_child(device_t dev, device_t child) 195 { 196 struct siba_devinfo *dinfo; 197 int error; 198 199 if (device_get_parent(child) != dev) 200 BUS_SUSPEND_CHILD(device_get_parent(dev), child); 201 202 if (!device_is_suspended(child)) 203 return (EBUSY); 204 205 dinfo = device_get_ivars(child); 206 207 /* Resume all resource references to the child's config registers */ 208 for (u_int i = 0; i < dinfo->core_id.num_cfg_blocks; i++) { 209 if (dinfo->cfg_res[i] == NULL) 210 continue; 211 212 error = BHNDB_RESUME_RESOURCE(device_get_parent(dev), dev, 213 SYS_RES_MEMORY, dinfo->cfg_res[i]->res); 214 if (error) { 215 siba_bhndb_suspend_cfgblocks(dev, dinfo); 216 return (error); 217 } 218 } 219 220 /* Resume the child */ 221 if ((error = bhnd_generic_br_resume_child(dev, child))) { 222 siba_bhndb_suspend_cfgblocks(dev, dinfo); 223 return (error); 224 } 225 226 return (0); 227 } 228 229 /* Work-around implementation for SIBA_QUIRK_PCIE_D11_SB_TIMEOUT */ 230 static int 231 siba_bhndb_wars_pcie_clear_d11_timeout(struct siba_bhndb_softc *sc) 232 { 233 struct siba_devinfo *dinfo; 234 device_t d11; 235 uint32_t imcfg; 236 237 /* Only applicable if there's a D11 core */ 238 d11 = bhnd_bus_match_child(sc->siba.dev, &(struct bhnd_core_match) { 239 BHND_MATCH_CORE(BHND_MFGID_BCM, BHND_COREID_D11), 240 BHND_MATCH_CORE_UNIT(0) 241 }); 242 if (d11 == NULL) 243 return (0); 244 245 /* Clear initiator timeout in D11's CFG0 block */ 246 dinfo = device_get_ivars(d11); 247 KASSERT(dinfo->cfg_res[0] != NULL, ("missing core config mapping")); 248 249 imcfg = bhnd_bus_read_4(dinfo->cfg_res[0], SIBA_CFG0_IMCONFIGLOW); 250 imcfg &= ~(SIBA_IMCL_RTO_MASK|SIBA_IMCL_STO_MASK); 251 252 bhnd_bus_write_4(dinfo->cfg_res[0], SIBA_CFG0_IMCONFIGLOW, imcfg); 253 254 return (0); 255 } 256 257 /** 258 * Apply any hardware workarounds that are required upon attach or resume 259 * of the bus. 260 */ 261 static int 262 siba_bhndb_wars_hwup(struct siba_bhndb_softc *sc) 263 { 264 int error; 265 266 if (sc->quirks & SIBA_QUIRK_PCIE_D11_SB_TIMEOUT) { 267 if ((error = siba_bhndb_wars_pcie_clear_d11_timeout(sc))) 268 return (error); 269 } 270 271 return (0); 272 } 273 274 static device_method_t siba_bhndb_methods[] = { 275 /* Device interface */ 276 DEVMETHOD(device_probe, siba_bhndb_probe), 277 DEVMETHOD(device_attach, siba_bhndb_attach), 278 DEVMETHOD(device_resume, siba_bhndb_resume), 279 280 /* Bus interface */ 281 DEVMETHOD(bus_suspend_child, siba_bhndb_suspend_child), 282 DEVMETHOD(bus_resume_child, siba_bhndb_resume_child), 283 284 DEVMETHOD_END 285 }; 286 287 DEFINE_CLASS_2(bhnd, siba_bhndb_driver, siba_bhndb_methods, 288 sizeof(struct siba_bhndb_softc), bhnd_bhndb_driver, siba_driver); 289 290 DRIVER_MODULE(siba_bhndb, bhndb, siba_bhndb_driver, NULL, NULL); 291 292 MODULE_VERSION(siba_bhndb, 1); 293 MODULE_DEPEND(siba_bhndb, siba, 1, 1, 1); 294 MODULE_DEPEND(siba_bhndb, bhnd, 1, 1, 1); 295 MODULE_DEPEND(siba_bhndb, bhndb, 1, 1, 1); 296