1 /*- 2 * Copyright (c) 2017-2020 Conrad Meyer <cem@FreeBSD.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 * 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 ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 23 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 * POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 /* 28 * Driver for the AMD Family 15h and 17h CPU System Management Network. 29 */ 30 31 #include <sys/param.h> 32 #include <sys/bus.h> 33 #include <sys/conf.h> 34 #include <sys/lock.h> 35 #include <sys/kernel.h> 36 #include <sys/module.h> 37 #include <sys/mutex.h> 38 #include <sys/sysctl.h> 39 #include <sys/systm.h> 40 41 #include <machine/cpufunc.h> 42 #include <machine/cputypes.h> 43 #include <machine/md_var.h> 44 #include <machine/specialreg.h> 45 46 #include <dev/pci/pcivar.h> 47 #include <x86/pci_cfgreg.h> 48 49 #include <dev/amdsmn/amdsmn.h> 50 51 #define F15H_SMN_ADDR_REG 0xb8 52 #define F15H_SMN_DATA_REG 0xbc 53 #define F17H_SMN_ADDR_REG 0x60 54 #define F17H_SMN_DATA_REG 0x64 55 56 #define PCI_DEVICE_ID_AMD_15H_M60H_ROOT 0x1576 57 #define PCI_DEVICE_ID_AMD_17H_ROOT 0x1450 58 #define PCI_DEVICE_ID_AMD_17H_M10H_ROOT 0x15d0 59 #define PCI_DEVICE_ID_AMD_17H_M30H_ROOT 0x1480 /* Also M70H, F19H M00H/M20H */ 60 #define PCI_DEVICE_ID_AMD_17H_M60H_ROOT 0x1630 61 #define PCI_DEVICE_ID_AMD_19H_M10H_ROOT 0x14a4 62 #define PCI_DEVICE_ID_AMD_19H_M60H_ROOT 0x14d8 63 #define PCI_DEVICE_ID_AMD_19H_M70H_ROOT 0x14e8 64 65 struct pciid; 66 struct amdsmn_softc { 67 struct mtx smn_lock; 68 const struct pciid *smn_pciid; 69 }; 70 71 static const struct pciid { 72 uint16_t amdsmn_vendorid; 73 uint16_t amdsmn_deviceid; 74 uint8_t amdsmn_addr_reg; 75 uint8_t amdsmn_data_reg; 76 } amdsmn_ids[] = { 77 { 78 .amdsmn_vendorid = CPU_VENDOR_AMD, 79 .amdsmn_deviceid = PCI_DEVICE_ID_AMD_15H_M60H_ROOT, 80 .amdsmn_addr_reg = F15H_SMN_ADDR_REG, 81 .amdsmn_data_reg = F15H_SMN_DATA_REG, 82 }, 83 { 84 .amdsmn_vendorid = CPU_VENDOR_AMD, 85 .amdsmn_deviceid = PCI_DEVICE_ID_AMD_17H_ROOT, 86 .amdsmn_addr_reg = F17H_SMN_ADDR_REG, 87 .amdsmn_data_reg = F17H_SMN_DATA_REG, 88 }, 89 { 90 .amdsmn_vendorid = CPU_VENDOR_AMD, 91 .amdsmn_deviceid = PCI_DEVICE_ID_AMD_17H_M10H_ROOT, 92 .amdsmn_addr_reg = F17H_SMN_ADDR_REG, 93 .amdsmn_data_reg = F17H_SMN_DATA_REG, 94 }, 95 { 96 .amdsmn_vendorid = CPU_VENDOR_AMD, 97 .amdsmn_deviceid = PCI_DEVICE_ID_AMD_17H_M30H_ROOT, 98 .amdsmn_addr_reg = F17H_SMN_ADDR_REG, 99 .amdsmn_data_reg = F17H_SMN_DATA_REG, 100 }, 101 { 102 .amdsmn_vendorid = CPU_VENDOR_AMD, 103 .amdsmn_deviceid = PCI_DEVICE_ID_AMD_17H_M60H_ROOT, 104 .amdsmn_addr_reg = F17H_SMN_ADDR_REG, 105 .amdsmn_data_reg = F17H_SMN_DATA_REG, 106 }, 107 { 108 .amdsmn_vendorid = CPU_VENDOR_AMD, 109 .amdsmn_deviceid = PCI_DEVICE_ID_AMD_19H_M10H_ROOT, 110 .amdsmn_addr_reg = F17H_SMN_ADDR_REG, 111 .amdsmn_data_reg = F17H_SMN_DATA_REG, 112 }, 113 { 114 .amdsmn_vendorid = CPU_VENDOR_AMD, 115 .amdsmn_deviceid = PCI_DEVICE_ID_AMD_19H_M60H_ROOT, 116 .amdsmn_addr_reg = F17H_SMN_ADDR_REG, 117 .amdsmn_data_reg = F17H_SMN_DATA_REG, 118 }, 119 { 120 .amdsmn_vendorid = CPU_VENDOR_AMD, 121 .amdsmn_deviceid = PCI_DEVICE_ID_AMD_19H_M70H_ROOT, 122 .amdsmn_addr_reg = F17H_SMN_ADDR_REG, 123 .amdsmn_data_reg = F17H_SMN_DATA_REG, 124 }, 125 }; 126 127 /* 128 * Device methods. 129 */ 130 static void amdsmn_identify(driver_t *driver, device_t parent); 131 static int amdsmn_probe(device_t dev); 132 static int amdsmn_attach(device_t dev); 133 static int amdsmn_detach(device_t dev); 134 135 static device_method_t amdsmn_methods[] = { 136 /* Device interface */ 137 DEVMETHOD(device_identify, amdsmn_identify), 138 DEVMETHOD(device_probe, amdsmn_probe), 139 DEVMETHOD(device_attach, amdsmn_attach), 140 DEVMETHOD(device_detach, amdsmn_detach), 141 DEVMETHOD_END 142 }; 143 144 static driver_t amdsmn_driver = { 145 "amdsmn", 146 amdsmn_methods, 147 sizeof(struct amdsmn_softc), 148 }; 149 150 DRIVER_MODULE(amdsmn, hostb, amdsmn_driver, NULL, NULL); 151 MODULE_VERSION(amdsmn, 1); 152 MODULE_PNP_INFO("U16:vendor;U16:device", pci, amdsmn, amdsmn_ids, 153 nitems(amdsmn_ids)); 154 155 static bool 156 amdsmn_match(device_t parent, const struct pciid **pciid_out) 157 { 158 uint16_t vendor, device; 159 size_t i; 160 161 vendor = pci_get_vendor(parent); 162 device = pci_get_device(parent); 163 164 for (i = 0; i < nitems(amdsmn_ids); i++) { 165 if (vendor == amdsmn_ids[i].amdsmn_vendorid && 166 device == amdsmn_ids[i].amdsmn_deviceid) { 167 if (pciid_out != NULL) 168 *pciid_out = &amdsmn_ids[i]; 169 return (true); 170 } 171 } 172 return (false); 173 } 174 175 static void 176 amdsmn_identify(driver_t *driver, device_t parent) 177 { 178 device_t child; 179 180 /* Make sure we're not being doubly invoked. */ 181 if (device_find_child(parent, "amdsmn", -1) != NULL) 182 return; 183 if (!amdsmn_match(parent, NULL)) 184 return; 185 186 child = device_add_child(parent, "amdsmn", DEVICE_UNIT_ANY); 187 if (child == NULL) 188 device_printf(parent, "add amdsmn child failed\n"); 189 } 190 191 static int 192 amdsmn_probe(device_t dev) 193 { 194 uint32_t family; 195 196 if (resource_disabled("amdsmn", 0)) 197 return (ENXIO); 198 if (!amdsmn_match(device_get_parent(dev), NULL)) 199 return (ENXIO); 200 201 family = CPUID_TO_FAMILY(cpu_id); 202 203 switch (family) { 204 case 0x15: 205 case 0x17: 206 case 0x19: 207 break; 208 default: 209 return (ENXIO); 210 } 211 device_set_descf(dev, "AMD Family %xh System Management Network", 212 family); 213 214 return (BUS_PROBE_GENERIC); 215 } 216 217 static int 218 amdsmn_attach(device_t dev) 219 { 220 struct amdsmn_softc *sc = device_get_softc(dev); 221 222 if (!amdsmn_match(device_get_parent(dev), &sc->smn_pciid)) 223 return (ENXIO); 224 225 mtx_init(&sc->smn_lock, "SMN mtx", "SMN", MTX_DEF); 226 return (0); 227 } 228 229 int 230 amdsmn_detach(device_t dev) 231 { 232 struct amdsmn_softc *sc = device_get_softc(dev); 233 234 mtx_destroy(&sc->smn_lock); 235 return (0); 236 } 237 238 int 239 amdsmn_read(device_t dev, uint32_t addr, uint32_t *value) 240 { 241 struct amdsmn_softc *sc = device_get_softc(dev); 242 device_t parent; 243 244 parent = device_get_parent(dev); 245 246 mtx_lock(&sc->smn_lock); 247 pci_write_config(parent, sc->smn_pciid->amdsmn_addr_reg, addr, 4); 248 *value = pci_read_config(parent, sc->smn_pciid->amdsmn_data_reg, 4); 249 mtx_unlock(&sc->smn_lock); 250 251 return (0); 252 } 253 254 int 255 amdsmn_write(device_t dev, uint32_t addr, uint32_t value) 256 { 257 struct amdsmn_softc *sc = device_get_softc(dev); 258 device_t parent; 259 260 parent = device_get_parent(dev); 261 262 mtx_lock(&sc->smn_lock); 263 pci_write_config(parent, sc->smn_pciid->amdsmn_addr_reg, addr, 4); 264 pci_write_config(parent, sc->smn_pciid->amdsmn_data_reg, value, 4); 265 mtx_unlock(&sc->smn_lock); 266 267 return (0); 268 } 269