2 * Copyright (c) 2008, 2009 Rui Paulo <rpaulo@FreeBSD.org>
3 * Copyright (c) 2009 Norikatsu Shigemura <nork@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
29 * Driver for the AMD K8/K10/K11 thermal sensors. Initially based on the
30 * k8temp Linux driver.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/types.h>
40 #include <sys/module.h>
42 #include <sys/kernel.h>
43 #include <sys/sysctl.h>
45 #include <machine/specialreg.h>
46 #include <machine/cpufunc.h>
47 #include <machine/md_var.h>
49 #include <dev/pci/pcireg.h>
50 #include <dev/pci/pcivar.h>
61 struct amdtemp_softc {
65 struct sysctl_oid *sc_oid;
66 struct sysctl_oid *sc_sysctl_cpu[2];
67 struct intr_config_hook sc_ich;
68 int32_t (*sc_gettemp)(device_t, amdsensor_t);
71 #define VENDORID_AMD 0x1022
72 #define DEVICEID_AMD_MISC0F 0x1103
73 #define DEVICEID_AMD_MISC10 0x1203
74 #define DEVICEID_AMD_MISC11 0x1303
76 static struct amdtemp_product {
77 uint16_t amdtemp_vendorid;
78 uint16_t amdtemp_deviceid;
79 } amdtemp_products[] = {
80 { VENDORID_AMD, DEVICEID_AMD_MISC0F },
81 { VENDORID_AMD, DEVICEID_AMD_MISC10 },
82 { VENDORID_AMD, DEVICEID_AMD_MISC11 },
87 * Register control (K8 family)
89 #define AMDTEMP_REG0F 0xe4
90 #define AMDTEMP_REG_SELSENSOR 0x40
91 #define AMDTEMP_REG_SELCORE 0x04
94 * Register control (K10 & K11) family
96 #define AMDTEMP_REG 0xa4
100 /* -49 C is the mininum temperature */
101 #define AMDTEMP_OFFSET0F (TZ_ZEROC-490)
102 #define AMDTEMP_OFFSET (TZ_ZEROC)
107 static void amdtemp_identify(driver_t *driver, device_t parent);
108 static int amdtemp_probe(device_t dev);
109 static int amdtemp_attach(device_t dev);
110 static void amdtemp_intrhook(void *arg);
111 static int amdtemp_detach(device_t dev);
112 static int amdtemp_match(device_t dev);
113 static int32_t amdtemp_gettemp0f(device_t dev, amdsensor_t sensor);
114 static int32_t amdtemp_gettemp(device_t dev, amdsensor_t sensor);
115 static int amdtemp_sysctl(SYSCTL_HANDLER_ARGS);
117 static device_method_t amdtemp_methods[] = {
118 /* Device interface */
119 DEVMETHOD(device_identify, amdtemp_identify),
120 DEVMETHOD(device_probe, amdtemp_probe),
121 DEVMETHOD(device_attach, amdtemp_attach),
122 DEVMETHOD(device_detach, amdtemp_detach),
127 static driver_t amdtemp_driver = {
130 sizeof(struct amdtemp_softc),
133 static devclass_t amdtemp_devclass;
134 DRIVER_MODULE(amdtemp, hostb, amdtemp_driver, amdtemp_devclass, NULL, NULL);
137 amdtemp_match(device_t dev)
140 uint16_t vendor, devid;
142 vendor = pci_get_vendor(dev);
143 devid = pci_get_device(dev);
145 for (i = 0; amdtemp_products[i].amdtemp_vendorid != 0; i++) {
146 if (vendor == amdtemp_products[i].amdtemp_vendorid &&
147 devid == amdtemp_products[i].amdtemp_deviceid)
155 amdtemp_identify(driver_t *driver, device_t parent)
159 /* Make sure we're not being doubly invoked. */
160 if (device_find_child(parent, "amdtemp", -1) != NULL)
163 if (amdtemp_match(parent)) {
164 child = device_add_child(parent, "amdtemp", -1);
166 device_printf(parent, "add amdtemp child failed\n");
172 amdtemp_probe(device_t dev)
176 if (resource_disabled("amdtemp", 0))
186 device_set_desc(dev, "AMD K8 Thermal Sensors");
188 return (BUS_PROBE_GENERIC);
192 amdtemp_attach(device_t dev)
194 struct amdtemp_softc *sc = device_get_softc(dev);
195 struct sysctl_ctx_list *sysctlctx;
196 struct sysctl_oid *sysctlnode;
200 * Setup intrhook function to create dev.cpu sysctl entries. This is
201 * needed because the cpu driver may be loaded late on boot, after
204 sc->sc_ich.ich_func = amdtemp_intrhook;
205 sc->sc_ich.ich_arg = dev;
206 if (config_intrhook_establish(&sc->sc_ich) != 0) {
207 device_printf(dev, "config_intrhook_establish "
212 if (pci_get_device(dev) == DEVICEID_AMD_MISC0F)
213 sc->sc_gettemp = amdtemp_gettemp0f;
215 sc->sc_gettemp = amdtemp_gettemp;
220 * dev.amdtemp.N tree.
222 sysctlctx = device_get_sysctl_ctx(dev);
223 sysctlnode = SYSCTL_ADD_NODE(sysctlctx,
224 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensor0",
225 CTLFLAG_RD, 0, "Sensor 0");
227 SYSCTL_ADD_PROC(sysctlctx,
228 SYSCTL_CHILDREN(sysctlnode),
229 OID_AUTO, "core0", CTLTYPE_INT | CTLFLAG_RD,
230 dev, SENSOR0_CORE0, amdtemp_sysctl, "IK",
231 "Sensor 0 / Core 0 temperature");
233 SYSCTL_ADD_PROC(sysctlctx,
234 SYSCTL_CHILDREN(sysctlnode),
235 OID_AUTO, "core1", CTLTYPE_INT | CTLFLAG_RD,
236 dev, SENSOR0_CORE1, amdtemp_sysctl, "IK",
237 "Sensor 0 / Core 1 temperature");
239 sysctlnode = SYSCTL_ADD_NODE(sysctlctx,
240 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensor1",
241 CTLFLAG_RD, 0, "Sensor 1");
243 SYSCTL_ADD_PROC(sysctlctx,
244 SYSCTL_CHILDREN(sysctlnode),
245 OID_AUTO, "core0", CTLTYPE_INT | CTLFLAG_RD,
246 dev, SENSOR1_CORE0, amdtemp_sysctl, "IK",
247 "Sensor 1 / Core 0 temperature");
249 SYSCTL_ADD_PROC(sysctlctx,
250 SYSCTL_CHILDREN(sysctlnode),
251 OID_AUTO, "core1", CTLTYPE_INT | CTLFLAG_RD,
252 dev, SENSOR1_CORE1, amdtemp_sysctl, "IK",
253 "Sensor 1 / Core 1 temperature");
259 amdtemp_intrhook(void *arg)
262 device_t nexus, acpi, cpu;
263 device_t dev = (device_t) arg;
264 struct amdtemp_softc *sc;
265 struct sysctl_ctx_list *sysctlctx;
267 sc = device_get_softc(dev);
270 * dev.cpu.N.temperature.
272 nexus = device_find_child(root_bus, "nexus", 0);
273 acpi = device_find_child(nexus, "acpi", 0);
275 for (i = 0; i < 2; i++) {
276 cpu = device_find_child(acpi, "cpu",
277 device_get_unit(dev) * 2 + i);
279 sysctlctx = device_get_sysctl_ctx(cpu);
281 sc->sc_sysctl_cpu[i] = SYSCTL_ADD_PROC(sysctlctx,
282 SYSCTL_CHILDREN(device_get_sysctl_tree(cpu)),
283 OID_AUTO, "temperature", CTLTYPE_INT | CTLFLAG_RD,
284 dev, CORE0, amdtemp_sysctl, "IK",
285 "Max of sensor 0 / 1");
288 config_intrhook_disestablish(&sc->sc_ich);
292 amdtemp_detach(device_t dev)
295 struct amdtemp_softc *sc = device_get_softc(dev);
297 for (i = 0; i < 2; i++) {
298 if (sc->sc_sysctl_cpu[i])
299 sysctl_remove_oid(sc->sc_sysctl_cpu[i], 1, 0);
302 /* NewBus removes the dev.amdtemp.N tree by itself. */
308 amdtemp_sysctl(SYSCTL_HANDLER_ARGS)
310 device_t dev = (device_t) arg1;
311 struct amdtemp_softc *sc = device_get_softc(dev);
313 int32_t temp, auxtemp[2];
317 auxtemp[0] = sc->sc_gettemp(dev, SENSOR0_CORE0);
318 auxtemp[1] = sc->sc_gettemp(dev, SENSOR1_CORE0);
319 temp = imax(auxtemp[0], auxtemp[1]);
322 auxtemp[0] = sc->sc_gettemp(dev, SENSOR0_CORE1);
323 auxtemp[1] = sc->sc_gettemp(dev, SENSOR1_CORE1);
324 temp = imax(auxtemp[0], auxtemp[1]);
327 temp = sc->sc_gettemp(dev, arg2);
330 error = sysctl_handle_int(oidp, &temp, 0, req);
336 amdtemp_gettemp0f(device_t dev, amdsensor_t sensor)
341 cfg = pci_read_config(dev, AMDTEMP_REG0F, 1);
344 cfg &= ~(AMDTEMP_REG_SELSENSOR | AMDTEMP_REG_SELCORE);
347 cfg &= ~AMDTEMP_REG_SELSENSOR;
348 cfg |= AMDTEMP_REG_SELCORE;
351 cfg &= ~AMDTEMP_REG_SELCORE;
352 cfg |= AMDTEMP_REG_SELSENSOR;
355 cfg |= (AMDTEMP_REG_SELSENSOR | AMDTEMP_REG_SELCORE);
361 pci_write_config(dev, AMDTEMP_REG0F, cfg, 1);
362 temp = pci_read_config(dev, AMDTEMP_REG0F, 4);
363 temp = ((temp >> 16) & 0xff) * 10 + AMDTEMP_OFFSET0F;
369 amdtemp_gettemp(device_t dev, amdsensor_t sensor)
373 temp = pci_read_config(dev, AMDTEMP_REG, 4);
374 temp = ((temp >> 21) & 0x3ff) * 10 / 8 + AMDTEMP_OFFSET;