2 * Copyright (c) 2017 Conrad Meyer <cem@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 * Driver for the AMD Family 17h CPU System Management Network.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/sysctl.h>
42 #include <sys/systm.h>
44 #include <machine/cpufunc.h>
45 #include <machine/md_var.h>
46 #include <machine/specialreg.h>
48 #include <dev/pci/pcivar.h>
49 #include <x86/pci_cfgreg.h>
51 #include <dev/amdsmn/amdsmn.h>
53 #define SMN_ADDR_REG 0x60
54 #define SMN_DATA_REG 0x64
69 static void amdsmn_identify(driver_t *driver, device_t parent);
70 static int amdsmn_probe(device_t dev);
71 static int amdsmn_attach(device_t dev);
72 static int amdsmn_detach(device_t dev);
74 static device_method_t amdsmn_methods[] = {
75 /* Device interface */
76 DEVMETHOD(device_identify, amdsmn_identify),
77 DEVMETHOD(device_probe, amdsmn_probe),
78 DEVMETHOD(device_attach, amdsmn_attach),
79 DEVMETHOD(device_detach, amdsmn_detach),
83 static driver_t amdsmn_driver = {
86 sizeof(struct amdsmn_softc),
89 static devclass_t amdsmn_devclass;
90 DRIVER_MODULE(amdsmn, hostb, amdsmn_driver, amdsmn_devclass, NULL, NULL);
91 MODULE_VERSION(amdsmn, 1);
92 MODULE_PNP_INFO("W32:vendor/device", pci, amdsmn, amdsmn_ids,
93 sizeof(amdsmn_ids[0]), nitems(amdsmn_ids));
96 amdsmn_match(device_t parent)
101 devid = pci_get_devid(parent);
102 for (i = 0; i < nitems(amdsmn_ids); i++)
103 if (amdsmn_ids[i].device_id == devid)
109 amdsmn_identify(driver_t *driver, device_t parent)
113 /* Make sure we're not being doubly invoked. */
114 if (device_find_child(parent, "amdsmn", -1) != NULL)
116 if (!amdsmn_match(parent))
119 child = device_add_child(parent, "amdsmn", -1);
121 device_printf(parent, "add amdsmn child failed\n");
125 amdsmn_probe(device_t dev)
129 if (resource_disabled("amdsmn", 0))
131 if (!amdsmn_match(device_get_parent(dev)))
134 family = CPUID_TO_FAMILY(cpu_id);
142 device_set_desc(dev, "AMD Family 17h System Management Network");
144 return (BUS_PROBE_GENERIC);
148 amdsmn_attach(device_t dev)
150 struct amdsmn_softc *sc = device_get_softc(dev);
152 mtx_init(&sc->smn_lock, "SMN mtx", "SMN", MTX_DEF);
157 amdsmn_detach(device_t dev)
159 struct amdsmn_softc *sc = device_get_softc(dev);
161 mtx_destroy(&sc->smn_lock);
166 amdsmn_read(device_t dev, uint32_t addr, uint32_t *value)
168 struct amdsmn_softc *sc = device_get_softc(dev);
171 parent = device_get_parent(dev);
173 mtx_lock(&sc->smn_lock);
174 pci_write_config(parent, SMN_ADDR_REG, addr, 4);
175 *value = pci_read_config(parent, SMN_DATA_REG, 4);
176 mtx_unlock(&sc->smn_lock);
182 amdsmn_write(device_t dev, uint32_t addr, uint32_t value)
184 struct amdsmn_softc *sc = device_get_softc(dev);
187 parent = device_get_parent(dev);
189 mtx_lock(&sc->smn_lock);
190 pci_write_config(parent, SMN_ADDR_REG, addr, 4);
191 pci_write_config(parent, SMN_DATA_REG, value, 4);
192 mtx_unlock(&sc->smn_lock);