xref: /netbsd/sys/arch/x86/x86/viac7temp.c (revision 6550d01e)
1 /* $NetBSD: viac7temp.c,v 1.2 2010/03/14 18:04:29 pgoyette Exp $ */
2 
3 /*-
4  * Copyright (c) 2009 Jared D. McNeill <jmcneill@invisible.ca>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: viac7temp.c,v 1.2 2010/03/14 18:04:29 pgoyette Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/kmem.h>
34 #include <sys/xcall.h>
35 #include <sys/cpu.h>
36 #include <sys/device.h>
37 
38 #include <dev/sysmon/sysmonvar.h>
39 
40 #include <machine/cpuvar.h>
41 #include <machine/specialreg.h>
42 #include <machine/cpufunc.h>
43 
44 struct viac7temp_softc {
45 	struct cpu_info		*sc_ci;
46 	struct sysmon_envsys 	*sc_sme;
47 	envsys_data_t 		sc_sensor;
48 };
49 
50 static void	viac7temp_refresh(struct sysmon_envsys *, envsys_data_t *);
51 static void	viac7temp_refresh_xcall(void *, void *);
52 
53 void
54 viac7temp_register(struct cpu_info *ci)
55 {
56 	struct viac7temp_softc *sc;
57 
58 	sc = kmem_zalloc(sizeof(struct viac7temp_softc), KM_SLEEP);
59 
60 	sc->sc_sensor.units = ENVSYS_STEMP;
61 	sc->sc_sensor.flags = ENVSYS_FMONLIMITS;
62 	strlcpy(sc->sc_sensor.desc, "temperature",
63 	    sizeof(sc->sc_sensor.desc));
64 
65 	sc->sc_sme = sysmon_envsys_create();
66 	if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor)) {
67 		sysmon_envsys_destroy(sc->sc_sme);
68 		goto bad;
69 	}
70 
71 	/*
72 	 * Hook into the system monitor.
73 	 */
74 	sc->sc_sme->sme_name = device_xname(ci->ci_dev);
75 	sc->sc_sme->sme_cookie = sc;
76 	sc->sc_sme->sme_refresh = viac7temp_refresh;
77 
78 	if (sysmon_envsys_register(sc->sc_sme)) {
79 		aprint_error_dev(ci->ci_dev,
80 		    "unable to register with sysmon\n");
81 		sysmon_envsys_destroy(sc->sc_sme);
82 		goto bad;
83 	}
84 
85 	return;
86 
87 bad:
88 	kmem_free(sc, sizeof(struct viac7temp_softc));
89 }
90 
91 static void
92 viac7temp_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
93 {
94 	struct viac7temp_softc *sc = sme->sme_cookie;
95 	uint64_t where;
96 
97 	where = xc_unicast(0, viac7temp_refresh_xcall, sc, edata, sc->sc_ci);
98 	xc_wait(where);
99 }
100 
101 static void
102 viac7temp_refresh_xcall(void *arg0, void *arg1)
103 {
104 	/* struct viac7temp_softc *sc = (struct viac7temp_softc *)arg0; */
105 	envsys_data_t *edata = (envsys_data_t *)arg1;
106 	uint32_t descs[4];
107 
108 	x86_cpuid(0xc0000002, descs);
109 
110 	edata->value_cur = descs[0] >> 8;
111 	edata->value_cur *= 1000000;
112 	edata->value_cur += 273150000;
113 	edata->state = ENVSYS_SVALID;
114 }
115