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