1 /* $NetBSD: amdsmn.c,v 1.6 2019/08/06 05:32:44 msaitoh Exp $ */ 2 3 /*- 4 * Copyright (c) 2017 Conrad Meyer <cem@FreeBSD.org> 5 * All rights reserved. 6 * 7 * NetBSD port by Ian Clark <mrrooster@gmail.com> 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 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __KERNEL_RCSID(0, "$NetBSD: amdsmn.c,v 1.6 2019/08/06 05:32:44 msaitoh Exp $ "); 33 34 /* 35 * Driver for the AMD Family 17h CPU System Management Network. 36 */ 37 38 #include <sys/param.h> 39 #include <sys/device.h> 40 #include <sys/errno.h> 41 #include <sys/mutex.h> 42 #include <sys/systm.h> 43 #include <sys/cpu.h> 44 #include <sys/module.h> 45 46 #include <machine/specialreg.h> 47 48 #include <dev/pci/pcireg.h> 49 #include <dev/pci/pcivar.h> 50 #include <dev/pci/pcidevs.h> 51 52 #include "amdsmn.h" 53 #include "ioconf.h" 54 55 #define SMN_ADDR_REG 0x60 56 #define SMN_DATA_REG 0x64 57 58 struct amdsmn_softc { 59 kmutex_t smn_lock; 60 struct pci_attach_args pa; 61 pci_chipset_tag_t pc; 62 pcitag_t pcitag; 63 }; 64 65 static const struct pciid { 66 uint16_t amdsmn_deviceid; 67 } amdsmn_ids[] = { 68 { PCI_PRODUCT_AMD_F17_RC }, 69 { PCI_PRODUCT_AMD_F17_1X_RC }, 70 { PCI_PRODUCT_AMD_F17_7X_RC }, 71 }; 72 73 static int amdsmn_match(device_t, cfdata_t, void *); 74 static void amdsmn_attach(device_t, device_t, void *); 75 static int amdsmn_rescan(device_t, const char *, const int *); 76 static int amdsmn_detach(device_t, int); 77 static int amdsmn_misc_search(device_t, cfdata_t, const int *, void *); 78 79 CFATTACH_DECL3_NEW(amdsmn, sizeof(struct amdsmn_softc), amdsmn_match, 80 amdsmn_attach, amdsmn_detach, NULL, amdsmn_rescan, NULL, 0); 81 82 static int 83 amdsmn_match(device_t parent, cfdata_t match, void *aux) 84 { 85 struct pci_attach_args *pa = aux; 86 unsigned int i; 87 88 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_AMD) 89 return 0; 90 91 for (i = 0; i < __arraycount(amdsmn_ids); i++) 92 if (PCI_PRODUCT(pa->pa_id) == amdsmn_ids[i].amdsmn_deviceid) 93 return 2; 94 95 return 0; 96 } 97 98 static int 99 amdsmn_misc_search(device_t parent, cfdata_t cf, const int *locs, void *aux) 100 { 101 if (config_match(parent, cf, aux)) 102 config_attach_loc(parent, cf, locs, aux, NULL); 103 104 return 0; 105 } 106 107 static void 108 amdsmn_attach(device_t parent, device_t self, void *aux) 109 { 110 struct amdsmn_softc *sc = device_private(self); 111 struct pci_attach_args *pa = aux; 112 int flags = 0; 113 114 mutex_init(&sc->smn_lock, MUTEX_DEFAULT, IPL_NONE); 115 sc->pa = *pa; 116 sc->pc = pa->pa_pc; 117 sc->pcitag = pa->pa_tag; 118 aprint_normal(": AMD Family 17h System Management Network\n"); 119 amdsmn_rescan(self, "amdsmnbus", &flags); 120 } 121 122 static int 123 amdsmn_rescan(device_t self, const char *ifattr, const int *flags) 124 { 125 struct amdsmn_softc *sc = device_private(self); 126 127 config_search_loc(amdsmn_misc_search, self, ifattr, NULL, &sc->pa); 128 129 return 0; 130 } 131 132 static int 133 amdsmn_detach(device_t self, int flags) 134 { 135 struct amdsmn_softc *sc = device_private(self); 136 137 mutex_destroy(&sc->smn_lock); 138 aprint_normal_dev(self,"detach!\n"); 139 140 return 0; 141 } 142 143 int 144 amdsmn_read(device_t dev, uint32_t addr, uint32_t *value) 145 { 146 struct amdsmn_softc *sc = device_private(dev); 147 148 mutex_enter(&sc->smn_lock); 149 pci_conf_write(sc->pc, sc->pcitag, SMN_ADDR_REG, addr); 150 *value = pci_conf_read(sc->pc, sc->pcitag, SMN_DATA_REG); 151 mutex_exit(&sc->smn_lock); 152 153 return 0; 154 } 155 156 int 157 amdsmn_write(device_t dev, uint32_t addr, uint32_t value) 158 { 159 struct amdsmn_softc *sc = device_private(dev); 160 161 mutex_enter(&sc->smn_lock); 162 pci_conf_write(sc->pc, sc->pcitag, SMN_ADDR_REG, addr); 163 pci_conf_write(sc->pc, sc->pcitag, SMN_DATA_REG, value); 164 mutex_exit(&sc->smn_lock); 165 166 return 0; 167 } 168 169 MODULE(MODULE_CLASS_DRIVER, amdsmn, "pci"); 170 171 #ifdef _MODULE 172 #include "ioconf.c" 173 #endif 174 175 static int 176 amdsmn_modcmd(modcmd_t cmd, void *opaque) 177 { 178 int error = 0; 179 180 #ifdef _MODULE 181 switch (cmd) { 182 case MODULE_CMD_INIT: 183 error = config_init_component(cfdriver_ioconf_amdsmn, 184 cfattach_ioconf_amdsmn, cfdata_ioconf_amdsmn); 185 break; 186 case MODULE_CMD_FINI: 187 error = config_fini_component(cfdriver_ioconf_amdsmn, 188 cfattach_ioconf_amdsmn, cfdata_ioconf_amdsmn); 189 break; 190 default: 191 error = ENOTTY; 192 break; 193 } 194 #endif 195 196 return error; 197 } 198 199