xref: /dragonfly/sys/dev/powermng/amdsmn/amdsmn.c (revision 7d3e9a5b)
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  * $FreeBSD: head/sys/dev/amdsmn/amdsmn.c 366136 2020-09-25 04:16:28Z 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 #define	PCI_DEVICE_ID_AMD_17H_M60H_ROOT		0x1630
63 
64 struct pciid;
65 struct amdsmn_softc {
66 	struct lock 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 
108 /*
109  * Device methods.
110  */
111 static void 	amdsmn_identify(driver_t *driver, device_t parent);
112 static int	amdsmn_probe(device_t dev);
113 static int	amdsmn_attach(device_t dev);
114 static int	amdsmn_detach(device_t dev);
115 
116 static device_method_t amdsmn_methods[] = {
117 	/* Device interface */
118 	DEVMETHOD(device_identify,	amdsmn_identify),
119 	DEVMETHOD(device_probe,		amdsmn_probe),
120 	DEVMETHOD(device_attach,	amdsmn_attach),
121 	DEVMETHOD(device_detach,	amdsmn_detach),
122 	DEVMETHOD_END
123 };
124 
125 static driver_t amdsmn_driver = {
126 	"amdsmn",
127 	amdsmn_methods,
128 	sizeof(struct amdsmn_softc),
129 };
130 
131 static devclass_t amdsmn_devclass;
132 DRIVER_MODULE(amdsmn, hostb, amdsmn_driver, amdsmn_devclass, NULL, NULL);
133 MODULE_VERSION(amdsmn, 1);
134 #if !defined(__DragonFly__)
135 MODULE_PNP_INFO("U16:vendor;U16:device", pci, amdsmn, amdsmn_ids,
136     nitems(amdsmn_ids));
137 #endif
138 
139 static bool
140 amdsmn_match(device_t parent, const struct pciid **pciid_out)
141 {
142 	uint16_t vendor, device;
143 	size_t i;
144 
145 	vendor = pci_get_vendor(parent);
146 	device = pci_get_device(parent);
147 
148 	for (i = 0; i < nitems(amdsmn_ids); i++) {
149 		if (vendor == amdsmn_ids[i].amdsmn_vendorid &&
150 		    device == amdsmn_ids[i].amdsmn_deviceid) {
151 			if (pciid_out != NULL)
152 				*pciid_out = &amdsmn_ids[i];
153 			return (true);
154 		}
155 	}
156 	return (false);
157 }
158 
159 static void
160 amdsmn_identify(driver_t *driver, device_t parent)
161 {
162 	device_t child;
163 
164 	/* Make sure we're not being doubly invoked. */
165 	if (device_find_child(parent, "amdsmn", -1) != NULL)
166 		return;
167 	if (!amdsmn_match(parent, NULL))
168 		return;
169 
170 	child = device_add_child(parent, "amdsmn", -1);
171 	if (child == NULL)
172 		device_printf(parent, "add amdsmn child failed\n");
173 }
174 
175 static int
176 amdsmn_probe(device_t dev)
177 {
178 	uint32_t family;
179 	char buf[64];
180 
181 	if (resource_disabled("amdsmn", 0))
182 		return (ENXIO);
183 	if (!amdsmn_match(device_get_parent(dev), NULL))
184 		return (ENXIO);
185 
186 	family = CPUID_TO_FAMILY(cpu_id);
187 
188 	switch (family) {
189 	case 0x15:
190 	case 0x17:
191 	case 0x19:
192 		break;
193 	default:
194 		return (ENXIO);
195 	}
196 	ksnprintf(buf, sizeof(buf), "AMD Family %xh System Management Network",
197 	    family);
198 	device_set_desc_copy(dev, buf);
199 
200 	return (BUS_PROBE_GENERIC);
201 }
202 
203 static int
204 amdsmn_attach(device_t dev)
205 {
206 	struct amdsmn_softc *sc = device_get_softc(dev);
207 
208 	if (!amdsmn_match(device_get_parent(dev), &sc->smn_pciid))
209 		return (ENXIO);
210 
211 	lockinit(&sc->smn_lock, device_get_nameunit(dev), 0, 0);
212 	return (0);
213 }
214 
215 int
216 amdsmn_detach(device_t dev)
217 {
218 	struct amdsmn_softc *sc = device_get_softc(dev);
219 
220 	lockuninit(&sc->smn_lock);
221 	return (0);
222 }
223 
224 int
225 amdsmn_read(device_t dev, uint32_t addr, uint32_t *value)
226 {
227 	struct amdsmn_softc *sc = device_get_softc(dev);
228 	device_t parent;
229 
230 	parent = device_get_parent(dev);
231 
232 	lockmgr(&sc->smn_lock, LK_EXCLUSIVE);
233 	pci_write_config(parent, sc->smn_pciid->amdsmn_addr_reg, addr, 4);
234 	*value = pci_read_config(parent, sc->smn_pciid->amdsmn_data_reg, 4);
235 	lockmgr(&sc->smn_lock, LK_RELEASE);
236 
237 	return (0);
238 }
239 
240 int
241 amdsmn_write(device_t dev, uint32_t addr, uint32_t value)
242 {
243 	struct amdsmn_softc *sc = device_get_softc(dev);
244 	device_t parent;
245 
246 	parent = device_get_parent(dev);
247 
248 	lockmgr(&sc->smn_lock, LK_EXCLUSIVE);
249 	pci_write_config(parent, sc->smn_pciid->amdsmn_addr_reg, addr, 4);
250 	pci_write_config(parent, sc->smn_pciid->amdsmn_data_reg, value, 4);
251 	lockmgr(&sc->smn_lock, LK_RELEASE);
252 
253 	return (0);
254 }
255