amdsmn.c revision 1.18 1 1.18 msaitoh /* $NetBSD: amdsmn.c,v 1.18 2024/10/17 14:02:40 msaitoh Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.7 simonb * Copyright (c) 2017, 2019 Conrad Meyer <cem (at) FreeBSD.org>
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * NetBSD port by Ian Clark <mrrooster (at) gmail.com>
8 1.1 christos *
9 1.1 christos * Redistribution and use in source and binary forms, with or without
10 1.1 christos * modification, are permitted provided that the following conditions
11 1.1 christos * are met:
12 1.1 christos * 1. Redistributions of source code must retain the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer.
14 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 christos * notice, this list of conditions and the following disclaimer in the
16 1.1 christos * documentation and/or other materials provided with the distribution.
17 1.1 christos *
18 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 1.1 christos * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 1.1 christos * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22 1.1 christos * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 1.1 christos * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 1.1 christos * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 1.1 christos * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 1.1 christos * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
29 1.1 christos */
30 1.1 christos
31 1.1 christos #include <sys/cdefs.h>
32 1.18 msaitoh __KERNEL_RCSID(0, "$NetBSD: amdsmn.c,v 1.18 2024/10/17 14:02:40 msaitoh Exp $ ");
33 1.1 christos
34 1.1 christos /*
35 1.7 simonb * Driver for the AMD Family 15h (model 60+) and 17h CPU
36 1.7 simonb * System Management Network.
37 1.1 christos */
38 1.1 christos
39 1.1 christos #include <sys/param.h>
40 1.1 christos #include <sys/device.h>
41 1.1 christos #include <sys/errno.h>
42 1.1 christos #include <sys/mutex.h>
43 1.1 christos #include <sys/systm.h>
44 1.1 christos #include <sys/cpu.h>
45 1.2 pgoyette #include <sys/module.h>
46 1.1 christos
47 1.1 christos #include <machine/specialreg.h>
48 1.1 christos
49 1.1 christos #include <dev/pci/pcireg.h>
50 1.1 christos #include <dev/pci/pcivar.h>
51 1.1 christos #include <dev/pci/pcidevs.h>
52 1.1 christos
53 1.1 christos #include "amdsmn.h"
54 1.2 pgoyette #include "ioconf.h"
55 1.1 christos
56 1.7 simonb #define F15H_SMN_ADDR_REG 0xb8
57 1.7 simonb #define F15H_SMN_DATA_REG 0xbc
58 1.7 simonb #define F17H_SMN_ADDR_REG 0x60
59 1.7 simonb #define F17H_SMN_DATA_REG 0x64
60 1.1 christos
61 1.1 christos struct amdsmn_softc {
62 1.1 christos kmutex_t smn_lock;
63 1.7 simonb uint8_t smn_addr_reg;
64 1.7 simonb uint8_t smn_data_reg;
65 1.1 christos struct pci_attach_args pa;
66 1.1 christos pci_chipset_tag_t pc;
67 1.1 christos pcitag_t pcitag;
68 1.1 christos };
69 1.1 christos
70 1.4 msaitoh static const struct pciid {
71 1.4 msaitoh uint16_t amdsmn_deviceid;
72 1.7 simonb uint8_t amdsmn_addr_reg;
73 1.7 simonb uint8_t amdsmn_data_reg;
74 1.4 msaitoh } amdsmn_ids[] = {
75 1.7 simonb {
76 1.13 msaitoh .amdsmn_deviceid = PCI_PRODUCT_AMD_F15_6X_RC,
77 1.7 simonb .amdsmn_addr_reg = F15H_SMN_ADDR_REG,
78 1.7 simonb .amdsmn_data_reg = F15H_SMN_DATA_REG,
79 1.7 simonb },
80 1.7 simonb {
81 1.7 simonb .amdsmn_deviceid = PCI_PRODUCT_AMD_F17_RC,
82 1.7 simonb .amdsmn_addr_reg = F17H_SMN_ADDR_REG,
83 1.7 simonb .amdsmn_data_reg = F17H_SMN_DATA_REG,
84 1.7 simonb },
85 1.7 simonb {
86 1.7 simonb .amdsmn_deviceid = PCI_PRODUCT_AMD_F17_1X_RC,
87 1.7 simonb .amdsmn_addr_reg = F17H_SMN_ADDR_REG,
88 1.7 simonb .amdsmn_data_reg = F17H_SMN_DATA_REG,
89 1.7 simonb },
90 1.7 simonb {
91 1.14 msaitoh .amdsmn_deviceid = PCI_PRODUCT_AMD_F17_6X_RC,
92 1.14 msaitoh .amdsmn_addr_reg = F17H_SMN_ADDR_REG,
93 1.14 msaitoh .amdsmn_data_reg = F17H_SMN_DATA_REG,
94 1.14 msaitoh },
95 1.14 msaitoh {
96 1.16 msaitoh .amdsmn_deviceid = PCI_PRODUCT_AMD_F17_7X_RC, /* or F19_0X */
97 1.16 msaitoh .amdsmn_addr_reg = F17H_SMN_ADDR_REG,
98 1.16 msaitoh .amdsmn_data_reg = F17H_SMN_DATA_REG,
99 1.16 msaitoh },
100 1.16 msaitoh {
101 1.16 msaitoh .amdsmn_deviceid = PCI_PRODUCT_AMD_F17_AX_RC, /* or F19_4X */
102 1.16 msaitoh .amdsmn_addr_reg = F17H_SMN_ADDR_REG,
103 1.16 msaitoh .amdsmn_data_reg = F17H_SMN_DATA_REG,
104 1.16 msaitoh },
105 1.16 msaitoh {
106 1.16 msaitoh .amdsmn_deviceid = PCI_PRODUCT_AMD_F19_1X_RC,
107 1.7 simonb .amdsmn_addr_reg = F17H_SMN_ADDR_REG,
108 1.7 simonb .amdsmn_data_reg = F17H_SMN_DATA_REG,
109 1.7 simonb },
110 1.14 msaitoh {
111 1.14 msaitoh .amdsmn_deviceid = PCI_PRODUCT_AMD_F19_6X_RC,
112 1.14 msaitoh .amdsmn_addr_reg = F17H_SMN_ADDR_REG,
113 1.14 msaitoh .amdsmn_data_reg = F17H_SMN_DATA_REG,
114 1.14 msaitoh },
115 1.17 msaitoh {
116 1.17 msaitoh .amdsmn_deviceid = PCI_PRODUCT_AMD_F19_7X_RC,
117 1.17 msaitoh .amdsmn_addr_reg = F17H_SMN_ADDR_REG,
118 1.17 msaitoh .amdsmn_data_reg = F17H_SMN_DATA_REG,
119 1.17 msaitoh },
120 1.18 msaitoh {
121 1.18 msaitoh .amdsmn_deviceid = PCI_PRODUCT_AMD_F1A_0X_RC,
122 1.18 msaitoh .amdsmn_addr_reg = F17H_SMN_ADDR_REG,
123 1.18 msaitoh .amdsmn_data_reg = F17H_SMN_DATA_REG,
124 1.18 msaitoh },
125 1.4 msaitoh };
126 1.4 msaitoh
127 1.1 christos static int amdsmn_match(device_t, cfdata_t, void *);
128 1.1 christos static void amdsmn_attach(device_t, device_t, void *);
129 1.2 pgoyette static int amdsmn_rescan(device_t, const char *, const int *);
130 1.1 christos static int amdsmn_detach(device_t, int);
131 1.1 christos static int amdsmn_misc_search(device_t, cfdata_t, const int *, void *);
132 1.1 christos
133 1.2 pgoyette CFATTACH_DECL3_NEW(amdsmn, sizeof(struct amdsmn_softc), amdsmn_match,
134 1.2 pgoyette amdsmn_attach, amdsmn_detach, NULL, amdsmn_rescan, NULL, 0);
135 1.6 msaitoh
136 1.1 christos static int
137 1.6 msaitoh amdsmn_match(device_t parent, cfdata_t match, void *aux)
138 1.1 christos {
139 1.1 christos struct pci_attach_args *pa = aux;
140 1.9 simonb size_t i;
141 1.4 msaitoh
142 1.4 msaitoh if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_AMD)
143 1.4 msaitoh return 0;
144 1.4 msaitoh
145 1.5 msaitoh for (i = 0; i < __arraycount(amdsmn_ids); i++)
146 1.4 msaitoh if (PCI_PRODUCT(pa->pa_id) == amdsmn_ids[i].amdsmn_deviceid)
147 1.4 msaitoh return 2;
148 1.4 msaitoh
149 1.4 msaitoh return 0;
150 1.1 christos }
151 1.1 christos
152 1.6 msaitoh static int
153 1.6 msaitoh amdsmn_misc_search(device_t parent, cfdata_t cf, const int *locs, void *aux)
154 1.1 christos {
155 1.11 thorpej if (config_probe(parent, cf, aux))
156 1.11 thorpej config_attach(parent, cf, aux, NULL,
157 1.12 thorpej CFARGS(.locators = locs));
158 1.1 christos
159 1.1 christos return 0;
160 1.1 christos }
161 1.1 christos
162 1.6 msaitoh static void
163 1.6 msaitoh amdsmn_attach(device_t parent, device_t self, void *aux)
164 1.1 christos {
165 1.1 christos struct amdsmn_softc *sc = device_private(self);
166 1.1 christos struct pci_attach_args *pa = aux;
167 1.8 joerg size_t i;
168 1.1 christos
169 1.1 christos mutex_init(&sc->smn_lock, MUTEX_DEFAULT, IPL_NONE);
170 1.1 christos sc->pa = *pa;
171 1.1 christos sc->pc = pa->pa_pc;
172 1.1 christos sc->pcitag = pa->pa_tag;
173 1.7 simonb
174 1.7 simonb for (i = 0; i < __arraycount(amdsmn_ids); i++)
175 1.7 simonb if (PCI_PRODUCT(pa->pa_id) == amdsmn_ids[i].amdsmn_deviceid) {
176 1.7 simonb sc->smn_addr_reg = amdsmn_ids[i].amdsmn_addr_reg;
177 1.7 simonb sc->smn_data_reg = amdsmn_ids[i].amdsmn_data_reg;
178 1.7 simonb }
179 1.7 simonb
180 1.7 simonb // aprint_normal(": AMD Family 17h System Management Network\n");
181 1.7 simonb aprint_normal(": AMD System Management Network\n");
182 1.15 reinoud
183 1.15 reinoud pmf_device_register(self, NULL, NULL);
184 1.11 thorpej amdsmn_rescan(self, NULL, NULL);
185 1.2 pgoyette }
186 1.2 pgoyette
187 1.2 pgoyette static int
188 1.11 thorpej amdsmn_rescan(device_t self, const char *ifattr, const int *locators)
189 1.2 pgoyette {
190 1.2 pgoyette struct amdsmn_softc *sc = device_private(self);
191 1.2 pgoyette
192 1.11 thorpej config_search(self, &sc->pa,
193 1.12 thorpej CFARGS(.search = amdsmn_misc_search));
194 1.2 pgoyette
195 1.2 pgoyette return 0;
196 1.1 christos }
197 1.1 christos
198 1.1 christos static int
199 1.6 msaitoh amdsmn_detach(device_t self, int flags)
200 1.1 christos {
201 1.1 christos struct amdsmn_softc *sc = device_private(self);
202 1.1 christos
203 1.15 reinoud pmf_device_deregister(self);
204 1.15 reinoud
205 1.1 christos mutex_destroy(&sc->smn_lock);
206 1.1 christos aprint_normal_dev(self,"detach!\n");
207 1.1 christos
208 1.1 christos return 0;
209 1.1 christos }
210 1.1 christos
211 1.1 christos int
212 1.1 christos amdsmn_read(device_t dev, uint32_t addr, uint32_t *value)
213 1.1 christos {
214 1.1 christos struct amdsmn_softc *sc = device_private(dev);
215 1.1 christos
216 1.1 christos mutex_enter(&sc->smn_lock);
217 1.7 simonb pci_conf_write(sc->pc, sc->pcitag, sc->smn_addr_reg, addr);
218 1.7 simonb *value = pci_conf_read(sc->pc, sc->pcitag, sc->smn_data_reg);
219 1.1 christos mutex_exit(&sc->smn_lock);
220 1.1 christos
221 1.1 christos return 0;
222 1.1 christos }
223 1.1 christos
224 1.1 christos int
225 1.1 christos amdsmn_write(device_t dev, uint32_t addr, uint32_t value)
226 1.1 christos {
227 1.1 christos struct amdsmn_softc *sc = device_private(dev);
228 1.1 christos
229 1.1 christos mutex_enter(&sc->smn_lock);
230 1.7 simonb pci_conf_write(sc->pc, sc->pcitag, sc->smn_addr_reg, addr);
231 1.7 simonb pci_conf_write(sc->pc, sc->pcitag, sc->smn_data_reg, value);
232 1.1 christos mutex_exit(&sc->smn_lock);
233 1.1 christos
234 1.1 christos return 0;
235 1.1 christos }
236 1.2 pgoyette
237 1.2 pgoyette MODULE(MODULE_CLASS_DRIVER, amdsmn, "pci");
238 1.2 pgoyette
239 1.2 pgoyette #ifdef _MODULE
240 1.2 pgoyette #include "ioconf.c"
241 1.2 pgoyette #endif
242 1.2 pgoyette
243 1.2 pgoyette static int
244 1.2 pgoyette amdsmn_modcmd(modcmd_t cmd, void *opaque)
245 1.2 pgoyette {
246 1.2 pgoyette int error = 0;
247 1.2 pgoyette
248 1.2 pgoyette #ifdef _MODULE
249 1.2 pgoyette switch (cmd) {
250 1.2 pgoyette case MODULE_CMD_INIT:
251 1.2 pgoyette error = config_init_component(cfdriver_ioconf_amdsmn,
252 1.2 pgoyette cfattach_ioconf_amdsmn, cfdata_ioconf_amdsmn);
253 1.2 pgoyette break;
254 1.2 pgoyette case MODULE_CMD_FINI:
255 1.2 pgoyette error = config_fini_component(cfdriver_ioconf_amdsmn,
256 1.2 pgoyette cfattach_ioconf_amdsmn, cfdata_ioconf_amdsmn);
257 1.2 pgoyette break;
258 1.2 pgoyette default:
259 1.2 pgoyette error = ENOTTY;
260 1.2 pgoyette break;
261 1.2 pgoyette }
262 1.2 pgoyette #endif
263 1.2 pgoyette
264 1.2 pgoyette return error;
265 1.2 pgoyette }
266 1.2 pgoyette
267