amdzentemp.c revision 1.3 1 1.3 pgoyette /* $NetBSD: amdzentemp.c,v 1.3 2018/01/25 21:23:36 pgoyette 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.3 pgoyette __KERNEL_RCSID(0, "$NetBSD: amdzentemp.c,v 1.3 2018/01/25 21:23:36 pgoyette 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.1 christos };
85 1.1 christos
86 1.1 christos
87 1.1 christos static int amdzentemp_match(device_t, cfdata_t, void *);
88 1.1 christos static void amdzentemp_attach(device_t, device_t, void *);
89 1.1 christos static int amdzentemp_detach(device_t, int);
90 1.1 christos
91 1.1 christos static void amdzentemp_family17_init(struct amdzentemp_softc *);
92 1.1 christos static void amdzentemp_family17_setup_sensors(struct amdzentemp_softc *, int);
93 1.1 christos static void amdzentemp_family17_refresh(struct sysmon_envsys *, envsys_data_t *);
94 1.1 christos
95 1.1 christos CFATTACH_DECL_NEW(amdzentemp, sizeof(struct amdzentemp_softc),
96 1.1 christos amdzentemp_match, amdzentemp_attach, amdzentemp_detach, NULL);
97 1.1 christos
98 1.1 christos static int
99 1.1 christos amdzentemp_match(device_t parent, cfdata_t match, void *aux)
100 1.1 christos {
101 1.3 pgoyette
102 1.1 christos KASSERT(PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD);
103 1.1 christos
104 1.1 christos cfdata_t parent_cfdata = device_cfdata(parent);
105 1.1 christos
106 1.1 christos /* Got AMD family 17h system management network */
107 1.1 christos return parent_cfdata->cf_name &&
108 1.1 christos memcmp(parent_cfdata->cf_name, "amdsmn", 6) == 0;
109 1.1 christos }
110 1.1 christos
111 1.1 christos static void
112 1.1 christos amdzentemp_attach(device_t parent, device_t self, void *aux)
113 1.1 christos {
114 1.1 christos struct amdzentemp_softc *sc = device_private(self);
115 1.1 christos struct pci_attach_args *pa = aux;
116 1.1 christos int error;
117 1.1 christos size_t i;
118 1.1 christos
119 1.1 christos aprint_naive("\n");
120 1.1 christos aprint_normal(": AMD CPU Temperature Sensors (Family17h)");
121 1.1 christos
122 1.1 christos sc->sc_pc = pa->pa_pc;
123 1.1 christos sc->sc_pcitag = pa->pa_tag;
124 1.1 christos sc->sc_smn = parent;
125 1.1 christos
126 1.1 christos amdzentemp_family17_init(sc);
127 1.1 christos
128 1.1 christos aprint_normal("\n");
129 1.1 christos
130 1.1 christos sc->sc_sme = sysmon_envsys_create();
131 1.1 christos sc->sc_sensor_len = sizeof(envsys_data_t) * sc->sc_numsensors;
132 1.1 christos sc->sc_sensor = kmem_zalloc(sc->sc_sensor_len, KM_SLEEP);
133 1.1 christos
134 1.1 christos amdzentemp_family17_setup_sensors(sc, device_unit(self));
135 1.1 christos
136 1.1 christos /*
137 1.1 christos * Set properties in sensors.
138 1.1 christos */
139 1.1 christos for (i = 0; i < sc->sc_numsensors; i++) {
140 1.1 christos if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor[i]))
141 1.1 christos goto bad;
142 1.1 christos }
143 1.1 christos
144 1.1 christos /*
145 1.1 christos * Register the sysmon_envsys device.
146 1.1 christos */
147 1.1 christos sc->sc_sme->sme_name = device_xname(self);
148 1.1 christos sc->sc_sme->sme_cookie = sc;
149 1.1 christos
150 1.1 christos sc->sc_sme->sme_refresh = amdzentemp_family17_refresh;
151 1.1 christos
152 1.1 christos error = sysmon_envsys_register(sc->sc_sme);
153 1.1 christos if (error) {
154 1.1 christos aprint_error_dev(self, "unable to register with sysmon "
155 1.1 christos "(error=%d)\n", error);
156 1.1 christos goto bad;
157 1.1 christos }
158 1.1 christos
159 1.1 christos (void)pmf_device_register(self, NULL, NULL);
160 1.1 christos
161 1.1 christos return;
162 1.1 christos
163 1.1 christos bad:
164 1.1 christos if (sc->sc_sme != NULL) {
165 1.1 christos sysmon_envsys_destroy(sc->sc_sme);
166 1.1 christos sc->sc_sme = NULL;
167 1.1 christos }
168 1.1 christos
169 1.1 christos if (sc->sc_sensor != NULL) {
170 1.1 christos kmem_free(sc->sc_sensor, sc->sc_sensor_len);
171 1.1 christos sc->sc_sensor = NULL;
172 1.1 christos }
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.1 christos sc->sc_numsensors = 1;
195 1.1 christos }
196 1.1 christos
197 1.1 christos static void
198 1.1 christos amdzentemp_family17_setup_sensors(struct amdzentemp_softc *sc, int dv_unit)
199 1.1 christos {
200 1.1 christos sc->sc_sensor[0].units = ENVSYS_STEMP;
201 1.1 christos sc->sc_sensor[0].state = ENVSYS_SVALID;
202 1.1 christos sc->sc_sensor[0].flags = ENVSYS_FHAS_ENTROPY;
203 1.1 christos
204 1.1 christos snprintf(sc->sc_sensor[0].desc, sizeof(sc->sc_sensor[0].desc),
205 1.1 christos "cpu%u temperature", dv_unit);
206 1.1 christos }
207 1.1 christos
208 1.1 christos static void
209 1.1 christos amdzentemp_family17_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
210 1.1 christos {
211 1.1 christos struct amdzentemp_softc *sc = sme->sme_cookie;
212 1.1 christos uint32_t temp;
213 1.1 christos int error;
214 1.1 christos
215 1.1 christos error = amdsmn_read(sc->sc_smn, AMD_17H_CUR_TMP, &temp);
216 1.1 christos if (error) {
217 1.1 christos edata->state = ENVSYS_SINVALID;
218 1.1 christos return;
219 1.1 christos }
220 1.1 christos edata->state = ENVSYS_SVALID;
221 1.1 christos /* From C to uK. */
222 1.1 christos edata->value_cur = ((temp >> 21) * 125000) + 273150000;
223 1.1 christos }
224 1.1 christos
225 1.1 christos MODULE(MODULE_CLASS_DRIVER, amdzentemp, "sysmon_envsys");
226 1.1 christos
227 1.1 christos #ifdef _MODULE
228 1.1 christos #include "ioconf.c"
229 1.1 christos #endif
230 1.1 christos
231 1.1 christos static int
232 1.1 christos amdzentemp_modcmd(modcmd_t cmd, void *aux)
233 1.1 christos {
234 1.1 christos int error = 0;
235 1.1 christos
236 1.1 christos switch (cmd) {
237 1.1 christos case MODULE_CMD_INIT:
238 1.1 christos #ifdef _MODULE
239 1.1 christos error = config_init_component(cfdriver_ioconf_amdzentemp,
240 1.1 christos cfattach_ioconf_amdzentemp, cfdata_ioconf_amdzentemp);
241 1.1 christos #endif
242 1.1 christos return error;
243 1.1 christos case MODULE_CMD_FINI:
244 1.1 christos #ifdef _MODULE
245 1.1 christos error = config_fini_component(cfdriver_ioconf_amdzentemp,
246 1.1 christos cfattach_ioconf_amdzentemp, cfdata_ioconf_amdzentemp);
247 1.1 christos #endif
248 1.1 christos return error;
249 1.1 christos default:
250 1.1 christos return ENOTTY;
251 1.1 christos }
252 1.1 christos }
253 1.1 christos
254