amdzentemp.c revision 1.9 1 1.9 mlelstv /* $NetBSD: amdzentemp.c,v 1.9 2019/06/16 09:12:51 mlelstv Exp $ */
2 1.1 christos /* $OpenBSD: kate.c,v 1.2 2008/03/27 04:52:03 cnst Exp $ */
3 1.1 christos
4 1.1 christos /*
5 1.1 christos * Copyright (c) 2008 The NetBSD Foundation, Inc.
6 1.1 christos * All rights reserved.
7 1.1 christos *
8 1.1 christos * This code is derived from software contributed to The NetBSD Foundation
9 1.1 christos * by Christoph Egger.
10 1.1 christos *
11 1.1 christos * NetBSD port by Ian Clark <mrrooster (at) gmail.com>
12 1.1 christos *
13 1.1 christos * Redistribution and use in source and binary forms, with or without
14 1.1 christos * modification, are permitted provided that the following conditions
15 1.1 christos * are met:
16 1.1 christos * 1. Redistributions of source code must retain the above copyright
17 1.1 christos * notice, this list of conditions and the following disclaimer.
18 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
19 1.1 christos * notice, this list of conditions and the following disclaimer in the
20 1.1 christos * documentation and/or other materials provided with the distribution.
21 1.1 christos *
22 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23 1.1 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24 1.1 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26 1.1 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 1.1 christos * POSSIBILITY OF SUCH DAMAGE.
33 1.1 christos */
34 1.1 christos
35 1.1 christos /*
36 1.1 christos * Copyright (c) 2008 Constantine A. Murenin <cnst+openbsd (at) bugmail.mojo.ru>
37 1.1 christos *
38 1.1 christos * Permission to use, copy, modify, and distribute this software for any
39 1.1 christos * purpose with or without fee is hereby granted, provided that the above
40 1.1 christos * copyright notice and this permission notice appear in all copies.
41 1.1 christos *
42 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
43 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
44 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
45 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
46 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
47 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
48 1.1 christos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
49 1.1 christos */
50 1.1 christos
51 1.1 christos
52 1.1 christos #include <sys/cdefs.h>
53 1.9 mlelstv __KERNEL_RCSID(0, "$NetBSD: amdzentemp.c,v 1.9 2019/06/16 09:12:51 mlelstv Exp $ ");
54 1.1 christos
55 1.1 christos #include <sys/param.h>
56 1.1 christos #include <sys/bus.h>
57 1.1 christos #include <sys/cpu.h>
58 1.1 christos #include <sys/systm.h>
59 1.1 christos #include <sys/device.h>
60 1.1 christos #include <sys/kmem.h>
61 1.1 christos #include <sys/module.h>
62 1.1 christos
63 1.1 christos #include <machine/specialreg.h>
64 1.1 christos
65 1.1 christos #include <dev/pci/pcireg.h>
66 1.1 christos #include <dev/pci/pcivar.h>
67 1.1 christos #include <dev/pci/pcidevs.h>
68 1.1 christos
69 1.1 christos #include <dev/sysmon/sysmonvar.h>
70 1.1 christos
71 1.1 christos #include "amdsmn.h"
72 1.1 christos
73 1.1 christos /* Address to query for temp on family 17h */
74 1.1 christos #define AMD_17H_CUR_TMP 0x59800
75 1.1 christos
76 1.1 christos struct amdzentemp_softc {
77 1.1 christos pci_chipset_tag_t sc_pc;
78 1.1 christos pcitag_t sc_pcitag;
79 1.1 christos struct sysmon_envsys *sc_sme;
80 1.1 christos device_t sc_smn;
81 1.1 christos envsys_data_t *sc_sensor;
82 1.1 christos size_t sc_sensor_len;
83 1.1 christos size_t sc_numsensors;
84 1.9 mlelstv int32_t sc_offset;
85 1.1 christos };
86 1.1 christos
87 1.1 christos
88 1.1 christos static int amdzentemp_match(device_t, cfdata_t, void *);
89 1.1 christos static void amdzentemp_attach(device_t, device_t, void *);
90 1.1 christos static int amdzentemp_detach(device_t, int);
91 1.1 christos
92 1.1 christos static void amdzentemp_family17_init(struct amdzentemp_softc *);
93 1.1 christos static void amdzentemp_family17_setup_sensors(struct amdzentemp_softc *, int);
94 1.1 christos static void amdzentemp_family17_refresh(struct sysmon_envsys *, envsys_data_t *);
95 1.1 christos
96 1.1 christos CFATTACH_DECL_NEW(amdzentemp, sizeof(struct amdzentemp_softc),
97 1.1 christos amdzentemp_match, amdzentemp_attach, amdzentemp_detach, NULL);
98 1.1 christos
99 1.1 christos static int
100 1.1 christos amdzentemp_match(device_t parent, cfdata_t match, void *aux)
101 1.1 christos {
102 1.5 pgoyette struct pci_attach_args *pa __diagused = aux;
103 1.3 pgoyette
104 1.1 christos KASSERT(PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD);
105 1.1 christos
106 1.1 christos cfdata_t parent_cfdata = device_cfdata(parent);
107 1.1 christos
108 1.1 christos /* Got AMD family 17h system management network */
109 1.1 christos return parent_cfdata->cf_name &&
110 1.1 christos memcmp(parent_cfdata->cf_name, "amdsmn", 6) == 0;
111 1.1 christos }
112 1.1 christos
113 1.1 christos static void
114 1.1 christos amdzentemp_attach(device_t parent, device_t self, void *aux)
115 1.1 christos {
116 1.1 christos struct amdzentemp_softc *sc = device_private(self);
117 1.1 christos struct pci_attach_args *pa = aux;
118 1.1 christos int error;
119 1.1 christos size_t i;
120 1.1 christos
121 1.1 christos aprint_naive("\n");
122 1.1 christos aprint_normal(": AMD CPU Temperature Sensors (Family17h)");
123 1.1 christos
124 1.1 christos sc->sc_pc = pa->pa_pc;
125 1.1 christos sc->sc_pcitag = pa->pa_tag;
126 1.1 christos sc->sc_smn = parent;
127 1.9 mlelstv
128 1.1 christos amdzentemp_family17_init(sc);
129 1.1 christos
130 1.1 christos aprint_normal("\n");
131 1.1 christos
132 1.1 christos sc->sc_sme = sysmon_envsys_create();
133 1.1 christos sc->sc_sensor_len = sizeof(envsys_data_t) * sc->sc_numsensors;
134 1.1 christos sc->sc_sensor = kmem_zalloc(sc->sc_sensor_len, KM_SLEEP);
135 1.1 christos
136 1.1 christos amdzentemp_family17_setup_sensors(sc, device_unit(self));
137 1.1 christos
138 1.1 christos /*
139 1.1 christos * Set properties in sensors.
140 1.1 christos */
141 1.1 christos for (i = 0; i < sc->sc_numsensors; i++) {
142 1.1 christos if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor[i]))
143 1.1 christos goto bad;
144 1.1 christos }
145 1.1 christos
146 1.1 christos /*
147 1.1 christos * Register the sysmon_envsys device.
148 1.1 christos */
149 1.1 christos sc->sc_sme->sme_name = device_xname(self);
150 1.1 christos sc->sc_sme->sme_cookie = sc;
151 1.1 christos
152 1.1 christos sc->sc_sme->sme_refresh = amdzentemp_family17_refresh;
153 1.1 christos
154 1.1 christos error = sysmon_envsys_register(sc->sc_sme);
155 1.1 christos if (error) {
156 1.1 christos aprint_error_dev(self, "unable to register with sysmon "
157 1.1 christos "(error=%d)\n", error);
158 1.1 christos goto bad;
159 1.1 christos }
160 1.1 christos
161 1.1 christos (void)pmf_device_register(self, NULL, NULL);
162 1.1 christos
163 1.1 christos return;
164 1.1 christos
165 1.1 christos bad:
166 1.1 christos if (sc->sc_sme != NULL) {
167 1.1 christos sysmon_envsys_destroy(sc->sc_sme);
168 1.1 christos sc->sc_sme = NULL;
169 1.1 christos }
170 1.1 christos
171 1.7 pgoyette kmem_free(sc->sc_sensor, sc->sc_sensor_len);
172 1.7 pgoyette sc->sc_sensor = NULL;
173 1.1 christos }
174 1.1 christos
175 1.1 christos static int
176 1.1 christos amdzentemp_detach(device_t self, int flags)
177 1.1 christos {
178 1.1 christos struct amdzentemp_softc *sc = device_private(self);
179 1.1 christos
180 1.1 christos pmf_device_deregister(self);
181 1.1 christos if (sc->sc_sme != NULL)
182 1.1 christos sysmon_envsys_unregister(sc->sc_sme);
183 1.1 christos
184 1.1 christos if (sc->sc_sensor != NULL)
185 1.1 christos kmem_free(sc->sc_sensor, sc->sc_sensor_len);
186 1.1 christos
187 1.1 christos return 0;
188 1.1 christos }
189 1.1 christos
190 1.1 christos
191 1.1 christos static void
192 1.1 christos amdzentemp_family17_init(struct amdzentemp_softc *sc)
193 1.1 christos {
194 1.9 mlelstv
195 1.1 christos sc->sc_numsensors = 1;
196 1.9 mlelstv sc->sc_offset = 0;
197 1.9 mlelstv
198 1.9 mlelstv if (strstr(cpu_brand_string, "AMD Ryzen 5 1600X")
199 1.9 mlelstv || strstr(cpu_brand_string, "AMD Ryzen 7 1700X")
200 1.9 mlelstv || strstr(cpu_brand_string, "AMD Ryzen 7 1800X"))
201 1.9 mlelstv sc->sc_offset = -20000000;
202 1.9 mlelstv else if (strstr(cpu_brand_string, "AMD Ryzen 7 2700X"))
203 1.9 mlelstv sc->sc_offset = -10000000;
204 1.9 mlelstv else if (strstr(cpu_brand_string, "AMD Ryzen Threadripper 19")
205 1.9 mlelstv || strstr(cpu_brand_string, "AMD Ryzen Threadripper 29"))
206 1.9 mlelstv sc->sc_offset = -27000000;
207 1.1 christos }
208 1.1 christos
209 1.1 christos static void
210 1.1 christos amdzentemp_family17_setup_sensors(struct amdzentemp_softc *sc, int dv_unit)
211 1.1 christos {
212 1.1 christos sc->sc_sensor[0].units = ENVSYS_STEMP;
213 1.1 christos sc->sc_sensor[0].state = ENVSYS_SVALID;
214 1.1 christos sc->sc_sensor[0].flags = ENVSYS_FHAS_ENTROPY;
215 1.1 christos
216 1.1 christos snprintf(sc->sc_sensor[0].desc, sizeof(sc->sc_sensor[0].desc),
217 1.1 christos "cpu%u temperature", dv_unit);
218 1.1 christos }
219 1.1 christos
220 1.1 christos static void
221 1.1 christos amdzentemp_family17_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
222 1.1 christos {
223 1.1 christos struct amdzentemp_softc *sc = sme->sme_cookie;
224 1.1 christos uint32_t temp;
225 1.1 christos int error;
226 1.1 christos
227 1.1 christos error = amdsmn_read(sc->sc_smn, AMD_17H_CUR_TMP, &temp);
228 1.1 christos if (error) {
229 1.1 christos edata->state = ENVSYS_SINVALID;
230 1.1 christos return;
231 1.1 christos }
232 1.1 christos edata->state = ENVSYS_SVALID;
233 1.1 christos /* From C to uK. */
234 1.1 christos edata->value_cur = ((temp >> 21) * 125000) + 273150000;
235 1.8 para /* adjust for possible offset of 49K */
236 1.8 para if (temp & 0x80000)
237 1.8 para edata->value_cur -= 49000000;
238 1.9 mlelstv edata->value_cur += sc->sc_offset;
239 1.1 christos }
240 1.1 christos
241 1.6 pgoyette MODULE(MODULE_CLASS_DRIVER, amdzentemp, "sysmon_envsys,amdsmn");
242 1.1 christos
243 1.1 christos #ifdef _MODULE
244 1.1 christos #include "ioconf.c"
245 1.1 christos #endif
246 1.1 christos
247 1.1 christos static int
248 1.1 christos amdzentemp_modcmd(modcmd_t cmd, void *aux)
249 1.1 christos {
250 1.1 christos int error = 0;
251 1.1 christos
252 1.1 christos switch (cmd) {
253 1.1 christos case MODULE_CMD_INIT:
254 1.1 christos #ifdef _MODULE
255 1.1 christos error = config_init_component(cfdriver_ioconf_amdzentemp,
256 1.1 christos cfattach_ioconf_amdzentemp, cfdata_ioconf_amdzentemp);
257 1.1 christos #endif
258 1.1 christos return error;
259 1.1 christos case MODULE_CMD_FINI:
260 1.1 christos #ifdef _MODULE
261 1.1 christos error = config_fini_component(cfdriver_ioconf_amdzentemp,
262 1.1 christos cfattach_ioconf_amdzentemp, cfdata_ioconf_amdzentemp);
263 1.1 christos #endif
264 1.1 christos return error;
265 1.1 christos default:
266 1.1 christos return ENOTTY;
267 1.1 christos }
268 1.1 christos }
269 1.1 christos
270