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