]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/amdsmn/amdsmn.c
Merge OpenSSL 1.1.1a.
[FreeBSD/FreeBSD.git] / sys / dev / amdsmn / amdsmn.c
1 /*-
2  * Copyright (c) 2017 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 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 SMN_ADDR_REG    0x60
55 #define SMN_DATA_REG    0x64
56
57 #define PCI_DEVICE_ID_AMD_17H_ROOT              0x1450
58 #define PCI_DEVICE_ID_AMD_17H_ROOT_DF_F3        0x1463
59 #define PCI_DEVICE_ID_AMD_17H_M10H_ROOT         0x15d0
60 #define PCI_DEVICE_ID_AMD_17H_M10H_ROOT_DF_F3   0x15eb
61
62 struct amdsmn_softc {
63         struct mtx smn_lock;
64 };
65
66 static struct pciid {
67         uint16_t        amdsmn_vendorid;
68         uint16_t        amdsmn_deviceid;
69 } amdsmn_ids[] = {
70         { CPU_VENDOR_AMD, PCI_DEVICE_ID_AMD_17H_ROOT },
71         { CPU_VENDOR_AMD, PCI_DEVICE_ID_AMD_17H_M10H_ROOT },
72 };
73
74 /*
75  * Device methods.
76  */
77 static void     amdsmn_identify(driver_t *driver, device_t parent);
78 static int      amdsmn_probe(device_t dev);
79 static int      amdsmn_attach(device_t dev);
80 static int      amdsmn_detach(device_t dev);
81
82 static device_method_t amdsmn_methods[] = {
83         /* Device interface */
84         DEVMETHOD(device_identify,      amdsmn_identify),
85         DEVMETHOD(device_probe,         amdsmn_probe),
86         DEVMETHOD(device_attach,        amdsmn_attach),
87         DEVMETHOD(device_detach,        amdsmn_detach),
88         DEVMETHOD_END
89 };
90
91 static driver_t amdsmn_driver = {
92         "amdsmn",
93         amdsmn_methods,
94         sizeof(struct amdsmn_softc),
95 };
96
97 static devclass_t amdsmn_devclass;
98 DRIVER_MODULE(amdsmn, hostb, amdsmn_driver, amdsmn_devclass, NULL, NULL);
99 MODULE_VERSION(amdsmn, 1);
100 MODULE_PNP_INFO("U16:vendor;U16:device", pci, amdsmn, amdsmn_ids,
101     nitems(amdsmn_ids));
102
103 static bool
104 amdsmn_match(device_t parent)
105 {
106         uint16_t vendor, device;
107         size_t i;
108
109         vendor = pci_get_vendor(parent);
110         device = pci_get_device(parent);
111
112         for (i = 0; i < nitems(amdsmn_ids); i++)
113                 if (vendor == amdsmn_ids[i].amdsmn_vendorid &&
114                     device == amdsmn_ids[i].amdsmn_deviceid)
115                         return (true);
116         return (false);
117 }
118
119 static void
120 amdsmn_identify(driver_t *driver, device_t parent)
121 {
122         device_t child;
123
124         /* Make sure we're not being doubly invoked. */
125         if (device_find_child(parent, "amdsmn", -1) != NULL)
126                 return;
127         if (!amdsmn_match(parent))
128                 return;
129
130         child = device_add_child(parent, "amdsmn", -1);
131         if (child == NULL)
132                 device_printf(parent, "add amdsmn child failed\n");
133 }
134
135 static int
136 amdsmn_probe(device_t dev)
137 {
138         uint32_t family;
139
140         if (resource_disabled("amdsmn", 0))
141                 return (ENXIO);
142         if (!amdsmn_match(device_get_parent(dev)))
143                 return (ENXIO);
144
145         family = CPUID_TO_FAMILY(cpu_id);
146
147         switch (family) {
148         case 0x17:
149                 break;
150         default:
151                 return (ENXIO);
152         }
153         device_set_desc(dev, "AMD Family 17h System Management Network");
154
155         return (BUS_PROBE_GENERIC);
156 }
157
158 static int
159 amdsmn_attach(device_t dev)
160 {
161         struct amdsmn_softc *sc = device_get_softc(dev);
162
163         mtx_init(&sc->smn_lock, "SMN mtx", "SMN", MTX_DEF);
164         return (0);
165 }
166
167 int
168 amdsmn_detach(device_t dev)
169 {
170         struct amdsmn_softc *sc = device_get_softc(dev);
171
172         mtx_destroy(&sc->smn_lock);
173         return (0);
174 }
175
176 int
177 amdsmn_read(device_t dev, uint32_t addr, uint32_t *value)
178 {
179         struct amdsmn_softc *sc = device_get_softc(dev);
180         device_t parent;
181
182         parent = device_get_parent(dev);
183
184         mtx_lock(&sc->smn_lock);
185         pci_write_config(parent, SMN_ADDR_REG, addr, 4);
186         *value = pci_read_config(parent, SMN_DATA_REG, 4);
187         mtx_unlock(&sc->smn_lock);
188
189         return (0);
190 }
191
192 int
193 amdsmn_write(device_t dev, uint32_t addr, uint32_t value)
194 {
195         struct amdsmn_softc *sc = device_get_softc(dev);
196         device_t parent;
197
198         parent = device_get_parent(dev);
199
200         mtx_lock(&sc->smn_lock);
201         pci_write_config(parent, SMN_ADDR_REG, addr, 4);
202         pci_write_config(parent, SMN_DATA_REG, value, 4);
203         mtx_unlock(&sc->smn_lock);
204
205         return (0);
206 }