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