1 /*- 2 * Copyright (c) 2017-2019 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 * $FreeBSD: head/sys/dev/amdsmn/amdsmn.c 357190 2020-01-28 01:39:50Z cem $ 27 */ 28 29 /* 30 * Driver for the AMD Family 15h and 17h CPU System Management Network. 31 */ 32 33 #include <sys/param.h> 34 #include <sys/bus.h> 35 #include <sys/conf.h> 36 #include <sys/lock.h> 37 #include <sys/kernel.h> 38 #include <sys/module.h> 39 #include <sys/mutex.h> 40 #include <sys/sysctl.h> 41 #include <sys/systm.h> 42 43 #include <machine/cpufunc.h> 44 #include <machine/cputypes.h> 45 #include <machine/md_var.h> 46 #include <machine/specialreg.h> 47 48 #include <bus/pci/pcivar.h> 49 #include <bus/pci/pci_cfgreg.h> 50 51 #include <dev/powermng/amdsmn/amdsmn.h> 52 53 #define F15H_SMN_ADDR_REG 0xb8 54 #define F15H_SMN_DATA_REG 0xbc 55 #define F17H_SMN_ADDR_REG 0x60 56 #define F17H_SMN_DATA_REG 0x64 57 58 #define PCI_DEVICE_ID_AMD_15H_M60H_ROOT 0x1576 59 #define PCI_DEVICE_ID_AMD_17H_ROOT 0x1450 60 #define PCI_DEVICE_ID_AMD_17H_M10H_ROOT 0x15d0 61 #define PCI_DEVICE_ID_AMD_17H_M30H_ROOT 0x1480 /* Also M70H. */ 62 63 struct pciid; 64 struct amdsmn_softc { 65 struct lock smn_lock; 66 const struct pciid *smn_pciid; 67 }; 68 69 static const struct pciid { 70 uint16_t amdsmn_vendorid; 71 uint16_t amdsmn_deviceid; 72 uint8_t amdsmn_addr_reg; 73 uint8_t amdsmn_data_reg; 74 } amdsmn_ids[] = { 75 { 76 .amdsmn_vendorid = CPU_VENDOR_AMD, 77 .amdsmn_deviceid = PCI_DEVICE_ID_AMD_15H_M60H_ROOT, 78 .amdsmn_addr_reg = F15H_SMN_ADDR_REG, 79 .amdsmn_data_reg = F15H_SMN_DATA_REG, 80 }, 81 { 82 .amdsmn_vendorid = CPU_VENDOR_AMD, 83 .amdsmn_deviceid = PCI_DEVICE_ID_AMD_17H_ROOT, 84 .amdsmn_addr_reg = F17H_SMN_ADDR_REG, 85 .amdsmn_data_reg = F17H_SMN_DATA_REG, 86 }, 87 { 88 .amdsmn_vendorid = CPU_VENDOR_AMD, 89 .amdsmn_deviceid = PCI_DEVICE_ID_AMD_17H_M10H_ROOT, 90 .amdsmn_addr_reg = F17H_SMN_ADDR_REG, 91 .amdsmn_data_reg = F17H_SMN_DATA_REG, 92 }, 93 { 94 .amdsmn_vendorid = CPU_VENDOR_AMD, 95 .amdsmn_deviceid = PCI_DEVICE_ID_AMD_17H_M30H_ROOT, 96 .amdsmn_addr_reg = F17H_SMN_ADDR_REG, 97 .amdsmn_data_reg = F17H_SMN_DATA_REG, 98 }, 99 }; 100 101 /* 102 * Device methods. 103 */ 104 static void amdsmn_identify(driver_t *driver, device_t parent); 105 static int amdsmn_probe(device_t dev); 106 static int amdsmn_attach(device_t dev); 107 static int amdsmn_detach(device_t dev); 108 109 static device_method_t amdsmn_methods[] = { 110 /* Device interface */ 111 DEVMETHOD(device_identify, amdsmn_identify), 112 DEVMETHOD(device_probe, amdsmn_probe), 113 DEVMETHOD(device_attach, amdsmn_attach), 114 DEVMETHOD(device_detach, amdsmn_detach), 115 DEVMETHOD_END 116 }; 117 118 static driver_t amdsmn_driver = { 119 "amdsmn", 120 amdsmn_methods, 121 sizeof(struct amdsmn_softc), 122 }; 123 124 static devclass_t amdsmn_devclass; 125 DRIVER_MODULE(amdsmn, hostb, amdsmn_driver, amdsmn_devclass, NULL, NULL); 126 MODULE_VERSION(amdsmn, 1); 127 #if !defined(__DragonFly__) 128 MODULE_PNP_INFO("U16:vendor;U16:device", pci, amdsmn, amdsmn_ids, 129 nitems(amdsmn_ids)); 130 #endif 131 132 static bool 133 amdsmn_match(device_t parent, const struct pciid **pciid_out) 134 { 135 uint16_t vendor, device; 136 size_t i; 137 138 vendor = pci_get_vendor(parent); 139 device = pci_get_device(parent); 140 141 for (i = 0; i < nitems(amdsmn_ids); i++) { 142 if (vendor == amdsmn_ids[i].amdsmn_vendorid && 143 device == amdsmn_ids[i].amdsmn_deviceid) { 144 if (pciid_out != NULL) 145 *pciid_out = &amdsmn_ids[i]; 146 return (true); 147 } 148 } 149 return (false); 150 } 151 152 static void 153 amdsmn_identify(driver_t *driver, device_t parent) 154 { 155 device_t child; 156 157 /* Make sure we're not being doubly invoked. */ 158 if (device_find_child(parent, "amdsmn", -1) != NULL) 159 return; 160 if (!amdsmn_match(parent, NULL)) 161 return; 162 163 child = device_add_child(parent, "amdsmn", -1); 164 if (child == NULL) 165 device_printf(parent, "add amdsmn child failed\n"); 166 } 167 168 static int 169 amdsmn_probe(device_t dev) 170 { 171 uint32_t family; 172 char buf[64]; 173 174 if (resource_disabled("amdsmn", 0)) 175 return (ENXIO); 176 if (!amdsmn_match(device_get_parent(dev), NULL)) 177 return (ENXIO); 178 179 family = CPUID_TO_FAMILY(cpu_id); 180 181 switch (family) { 182 case 0x15: 183 case 0x17: 184 break; 185 default: 186 return (ENXIO); 187 } 188 ksnprintf(buf, sizeof(buf), "AMD Family %xh System Management Network", 189 family); 190 device_set_desc_copy(dev, buf); 191 192 return (BUS_PROBE_GENERIC); 193 } 194 195 static int 196 amdsmn_attach(device_t dev) 197 { 198 struct amdsmn_softc *sc = device_get_softc(dev); 199 200 if (!amdsmn_match(device_get_parent(dev), &sc->smn_pciid)) 201 return (ENXIO); 202 203 lockinit(&sc->smn_lock, device_get_nameunit(dev), 0, 0); 204 return (0); 205 } 206 207 int 208 amdsmn_detach(device_t dev) 209 { 210 struct amdsmn_softc *sc = device_get_softc(dev); 211 212 lockuninit(&sc->smn_lock); 213 return (0); 214 } 215 216 int 217 amdsmn_read(device_t dev, uint32_t addr, uint32_t *value) 218 { 219 struct amdsmn_softc *sc = device_get_softc(dev); 220 device_t parent; 221 222 parent = device_get_parent(dev); 223 224 lockmgr(&sc->smn_lock, LK_EXCLUSIVE); 225 pci_write_config(parent, sc->smn_pciid->amdsmn_addr_reg, addr, 4); 226 *value = pci_read_config(parent, sc->smn_pciid->amdsmn_data_reg, 4); 227 lockmgr(&sc->smn_lock, LK_RELEASE); 228 229 return (0); 230 } 231 232 int 233 amdsmn_write(device_t dev, uint32_t addr, uint32_t value) 234 { 235 struct amdsmn_softc *sc = device_get_softc(dev); 236 device_t parent; 237 238 parent = device_get_parent(dev); 239 240 lockmgr(&sc->smn_lock, LK_EXCLUSIVE); 241 pci_write_config(parent, sc->smn_pciid->amdsmn_addr_reg, addr, 4); 242 pci_write_config(parent, sc->smn_pciid->amdsmn_data_reg, value, 4); 243 lockmgr(&sc->smn_lock, LK_RELEASE); 244 245 return (0); 246 } 247