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