amdsmn.c revision 1.17 1 1.17 msaitoh /* $NetBSD: amdsmn.c,v 1.17 2023/07/28 02:28:33 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.17 msaitoh __KERNEL_RCSID(0, "$NetBSD: amdsmn.c,v 1.17 2023/07/28 02:28:33 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.4 msaitoh };
121 1.4 msaitoh
122 1.1 christos static int amdsmn_match(device_t, cfdata_t, void *);
123 1.1 christos static void amdsmn_attach(device_t, device_t, void *);
124 1.2 pgoyette static int amdsmn_rescan(device_t, const char *, const int *);
125 1.1 christos static int amdsmn_detach(device_t, int);
126 1.1 christos static int amdsmn_misc_search(device_t, cfdata_t, const int *, void *);
127 1.1 christos
128 1.2 pgoyette CFATTACH_DECL3_NEW(amdsmn, sizeof(struct amdsmn_softc), amdsmn_match,
129 1.2 pgoyette amdsmn_attach, amdsmn_detach, NULL, amdsmn_rescan, NULL, 0);
130 1.6 msaitoh
131 1.1 christos static int
132 1.6 msaitoh amdsmn_match(device_t parent, cfdata_t match, void *aux)
133 1.1 christos {
134 1.1 christos struct pci_attach_args *pa = aux;
135 1.9 simonb size_t i;
136 1.4 msaitoh
137 1.4 msaitoh if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_AMD)
138 1.4 msaitoh return 0;
139 1.4 msaitoh
140 1.5 msaitoh for (i = 0; i < __arraycount(amdsmn_ids); i++)
141 1.4 msaitoh if (PCI_PRODUCT(pa->pa_id) == amdsmn_ids[i].amdsmn_deviceid)
142 1.4 msaitoh return 2;
143 1.4 msaitoh
144 1.4 msaitoh return 0;
145 1.1 christos }
146 1.1 christos
147 1.6 msaitoh static int
148 1.6 msaitoh amdsmn_misc_search(device_t parent, cfdata_t cf, const int *locs, void *aux)
149 1.1 christos {
150 1.11 thorpej if (config_probe(parent, cf, aux))
151 1.11 thorpej config_attach(parent, cf, aux, NULL,
152 1.12 thorpej CFARGS(.locators = locs));
153 1.1 christos
154 1.1 christos return 0;
155 1.1 christos }
156 1.1 christos
157 1.6 msaitoh static void
158 1.6 msaitoh amdsmn_attach(device_t parent, device_t self, void *aux)
159 1.1 christos {
160 1.1 christos struct amdsmn_softc *sc = device_private(self);
161 1.1 christos struct pci_attach_args *pa = aux;
162 1.8 joerg size_t i;
163 1.1 christos
164 1.1 christos mutex_init(&sc->smn_lock, MUTEX_DEFAULT, IPL_NONE);
165 1.1 christos sc->pa = *pa;
166 1.1 christos sc->pc = pa->pa_pc;
167 1.1 christos sc->pcitag = pa->pa_tag;
168 1.7 simonb
169 1.7 simonb for (i = 0; i < __arraycount(amdsmn_ids); i++)
170 1.7 simonb if (PCI_PRODUCT(pa->pa_id) == amdsmn_ids[i].amdsmn_deviceid) {
171 1.7 simonb sc->smn_addr_reg = amdsmn_ids[i].amdsmn_addr_reg;
172 1.7 simonb sc->smn_data_reg = amdsmn_ids[i].amdsmn_data_reg;
173 1.7 simonb }
174 1.7 simonb
175 1.7 simonb // aprint_normal(": AMD Family 17h System Management Network\n");
176 1.7 simonb aprint_normal(": AMD System Management Network\n");
177 1.15 reinoud
178 1.15 reinoud pmf_device_register(self, NULL, NULL);
179 1.11 thorpej amdsmn_rescan(self, NULL, NULL);
180 1.2 pgoyette }
181 1.2 pgoyette
182 1.2 pgoyette static int
183 1.11 thorpej amdsmn_rescan(device_t self, const char *ifattr, const int *locators)
184 1.2 pgoyette {
185 1.2 pgoyette struct amdsmn_softc *sc = device_private(self);
186 1.2 pgoyette
187 1.11 thorpej config_search(self, &sc->pa,
188 1.12 thorpej CFARGS(.search = amdsmn_misc_search));
189 1.2 pgoyette
190 1.2 pgoyette return 0;
191 1.1 christos }
192 1.1 christos
193 1.1 christos static int
194 1.6 msaitoh amdsmn_detach(device_t self, int flags)
195 1.1 christos {
196 1.1 christos struct amdsmn_softc *sc = device_private(self);
197 1.1 christos
198 1.15 reinoud pmf_device_deregister(self);
199 1.15 reinoud
200 1.1 christos mutex_destroy(&sc->smn_lock);
201 1.1 christos aprint_normal_dev(self,"detach!\n");
202 1.1 christos
203 1.1 christos return 0;
204 1.1 christos }
205 1.1 christos
206 1.1 christos int
207 1.1 christos amdsmn_read(device_t dev, uint32_t addr, uint32_t *value)
208 1.1 christos {
209 1.1 christos struct amdsmn_softc *sc = device_private(dev);
210 1.1 christos
211 1.1 christos mutex_enter(&sc->smn_lock);
212 1.7 simonb pci_conf_write(sc->pc, sc->pcitag, sc->smn_addr_reg, addr);
213 1.7 simonb *value = pci_conf_read(sc->pc, sc->pcitag, sc->smn_data_reg);
214 1.1 christos mutex_exit(&sc->smn_lock);
215 1.1 christos
216 1.1 christos return 0;
217 1.1 christos }
218 1.1 christos
219 1.1 christos int
220 1.1 christos amdsmn_write(device_t dev, uint32_t addr, uint32_t value)
221 1.1 christos {
222 1.1 christos struct amdsmn_softc *sc = device_private(dev);
223 1.1 christos
224 1.1 christos mutex_enter(&sc->smn_lock);
225 1.7 simonb pci_conf_write(sc->pc, sc->pcitag, sc->smn_addr_reg, addr);
226 1.7 simonb pci_conf_write(sc->pc, sc->pcitag, sc->smn_data_reg, value);
227 1.1 christos mutex_exit(&sc->smn_lock);
228 1.1 christos
229 1.1 christos return 0;
230 1.1 christos }
231 1.2 pgoyette
232 1.2 pgoyette MODULE(MODULE_CLASS_DRIVER, amdsmn, "pci");
233 1.2 pgoyette
234 1.2 pgoyette #ifdef _MODULE
235 1.2 pgoyette #include "ioconf.c"
236 1.2 pgoyette #endif
237 1.2 pgoyette
238 1.2 pgoyette static int
239 1.2 pgoyette amdsmn_modcmd(modcmd_t cmd, void *opaque)
240 1.2 pgoyette {
241 1.2 pgoyette int error = 0;
242 1.2 pgoyette
243 1.2 pgoyette #ifdef _MODULE
244 1.2 pgoyette switch (cmd) {
245 1.2 pgoyette case MODULE_CMD_INIT:
246 1.2 pgoyette error = config_init_component(cfdriver_ioconf_amdsmn,
247 1.2 pgoyette cfattach_ioconf_amdsmn, cfdata_ioconf_amdsmn);
248 1.2 pgoyette break;
249 1.2 pgoyette case MODULE_CMD_FINI:
250 1.2 pgoyette error = config_fini_component(cfdriver_ioconf_amdsmn,
251 1.2 pgoyette cfattach_ioconf_amdsmn, cfdata_ioconf_amdsmn);
252 1.2 pgoyette break;
253 1.2 pgoyette default:
254 1.2 pgoyette error = ENOTTY;
255 1.2 pgoyette break;
256 1.2 pgoyette }
257 1.2 pgoyette #endif
258 1.2 pgoyette
259 1.2 pgoyette return error;
260 1.2 pgoyette }
261 1.2 pgoyette
262