2 * Copyright (c) 2015 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Sepherosa Ziehau <sepherosa@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/bitops.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/sensors.h>
43 #include <bus/pci/pcivar.h>
44 #include <bus/pci/pcireg.h>
45 #include <bus/pci/pci_cfgreg.h>
47 #include "coremctl_if.h"
50 #include <dev/misc/coremctl/coremctl_reg.h>
51 #include <dev/misc/dimm/dimm.h>
53 struct memtemp_core_softc
;
55 struct memtemp_core_dimm
{
56 TAILQ_ENTRY(memtemp_core_dimm
) dimm_link
;
57 struct ksensor dimm_sensor
;
58 struct memtemp_core_softc
*dimm_parent
;
62 struct dimm_softc
*dimm_softc
;
65 struct memtemp_core_type
{
70 struct memtemp_core_softc
{
73 TAILQ_HEAD(, memtemp_core_dimm
) temp_dimm
;
76 static int memtemp_core_probe(device_t
);
77 static int memtemp_core_attach(device_t
);
78 static int memtemp_core_detach(device_t
);
80 static void memtemp_core_chan_attach(struct memtemp_core_softc
*, int);
81 static void memtemp_core_dimm_attach(struct memtemp_core_softc
*,
83 static void memtemp_core_sensor_task(void *);
85 static const struct memtemp_core_type memtemp_core_types
[] = {
86 { PCI_E3V3_MEMCTL_DID
,
87 "Intel E3 v3 memory thermal sensor" },
89 { PCI_COREV3_MEMCTL_DID
,
90 "Intel i3/i5/i7 Haswell memory thermal sensor" },
92 { 0, NULL
} /* required last entry */
95 static device_method_t memtemp_core_methods
[] = {
96 /* Device interface */
97 DEVMETHOD(device_probe
, memtemp_core_probe
),
98 DEVMETHOD(device_attach
, memtemp_core_attach
),
99 DEVMETHOD(device_detach
, memtemp_core_detach
),
100 DEVMETHOD(device_shutdown
, bus_generic_shutdown
),
101 DEVMETHOD(device_suspend
, bus_generic_suspend
),
102 DEVMETHOD(device_resume
, bus_generic_resume
),
106 static driver_t memtemp_core_driver
= {
108 memtemp_core_methods
,
109 sizeof(struct memtemp_core_softc
)
111 static devclass_t memtemp_devclass
;
112 DRIVER_MODULE(memtemp_core
, coremctl
, memtemp_core_driver
, memtemp_devclass
,
114 MODULE_DEPEND(memtemp_core
, pci
, 1, 1, 1);
115 MODULE_DEPEND(memtemp_core
, coremctl
, 1, 1, 1);
116 MODULE_DEPEND(memtemp_core
, dimm
, 1, 1, 1);
118 static __inline
uint32_t
119 CSR_READ_4(struct memtemp_core_softc
*sc
, int ofs
)
124 error
= COREMCTL_MCH_READ(sc
->temp_parent
, ofs
, &val
);
125 KASSERT(!error
, ("mch read failed"));
131 CSR_WRITE_4(struct memtemp_core_softc
*sc
, int ofs
, uint32_t val
)
135 error
= COREMCTL_MCH_WRITE(sc
->temp_parent
, ofs
, val
);
136 KASSERT(!error
, ("mch write failed"));
140 memtemp_core_probe(device_t dev
)
142 const struct memtemp_core_type
*t
;
145 if (pci_get_vendor(dev
) != PCI_CORE_MEMCTL_VID
)
148 did
= pci_get_device(dev
);
149 for (t
= memtemp_core_types
; t
->desc
!= NULL
; ++t
) {
151 device_set_desc(dev
, t
->desc
);
159 memtemp_core_attach(device_t dev
)
161 struct memtemp_core_softc
*sc
= device_get_softc(dev
);
165 sc
->temp_parent
= device_get_parent(dev
);
166 TAILQ_INIT(&sc
->temp_dimm
);
168 for (i
= 0; i
< PCI_CORE_MEMCTL_CHN_MAX
; ++i
)
169 memtemp_core_chan_attach(sc
, i
);
175 memtemp_core_chan_attach(struct memtemp_core_softc
*sc
, int chan
)
177 int dimm_ch_reg
, dimm_chtemp_reg
;
178 int dimma_id
, dimmb_id
;
183 dimm_ch_reg
= MCH_CORE_DIMM_CH0
;
184 dimm_chtemp_reg
= MCH_CORE_DIMM_TEMP_CH0
;
186 KASSERT(chan
== 1, ("unsupport channel%d", chan
));
187 dimm_ch_reg
= MCH_CORE_DIMM_CH1
;
188 dimm_chtemp_reg
= MCH_CORE_DIMM_TEMP_CH1
;
191 dimm_ch
= CSR_READ_4(sc
, dimm_ch_reg
);
193 size_a
= __SHIFTOUT(dimm_ch
, MCH_CORE_DIMM_A_SIZE
);
194 size_b
= __SHIFTOUT(dimm_ch
, MCH_CORE_DIMM_B_SIZE
);
195 if (size_a
== 0 && size_b
== 0)
200 if (dimm_ch
& MCH_CORE_DIMM_A_SELECT
) {
206 memtemp_core_dimm_attach(sc
, chan
, dimma_id
, dimm_chtemp_reg
);
208 memtemp_core_dimm_attach(sc
, chan
, dimmb_id
, dimm_chtemp_reg
);
212 memtemp_core_dimm_attach(struct memtemp_core_softc
*sc
, int chan
, int dimm_id
,
215 struct memtemp_core_dimm
*dimm_sc
;
216 struct ksensor
*sens
;
218 dimm_sc
= kmalloc(sizeof(*dimm_sc
), M_DEVBUF
, M_WAITOK
| M_ZERO
);
219 dimm_sc
->dimm_parent
= sc
;
220 dimm_sc
->dimm_reg
= dimm_reg
;
222 dimm_sc
->dimm_mask
= MCH_CORE_DIMM_TEMP_DIMM0
;
224 KASSERT(dimm_id
== 1, ("unsupported DIMM%d", dimm_id
));
225 dimm_sc
->dimm_mask
= MCH_CORE_DIMM_TEMP_DIMM1
;
228 dimm_sc
->dimm_softc
= dimm_create(0, chan
, dimm_id
);
230 sens
= &dimm_sc
->dimm_sensor
;
231 ksnprintf(sens
->desc
, sizeof(sens
->desc
), "chan%d DIMM%d temp",
233 sens
->type
= SENSOR_TEMP
;
234 sensor_set_unknown(sens
);
235 dimm_sensor_attach(dimm_sc
->dimm_softc
, sens
);
236 sensor_task_register(dimm_sc
, memtemp_core_sensor_task
, 5);
238 TAILQ_INSERT_TAIL(&sc
->temp_dimm
, dimm_sc
, dimm_link
);
242 memtemp_core_detach(device_t dev
)
244 struct memtemp_core_softc
*sc
= device_get_softc(dev
);
245 struct memtemp_core_dimm
*dimm_sc
;
247 while ((dimm_sc
= TAILQ_FIRST(&sc
->temp_dimm
)) != NULL
) {
248 TAILQ_REMOVE(&sc
->temp_dimm
, dimm_sc
, dimm_link
);
250 sensor_task_unregister(dimm_sc
);
251 dimm_sensor_detach(dimm_sc
->dimm_softc
, &dimm_sc
->dimm_sensor
);
252 dimm_destroy(dimm_sc
->dimm_softc
);
254 kfree(dimm_sc
, M_DEVBUF
);
260 memtemp_core_sensor_task(void *xdimm_sc
)
262 struct memtemp_core_dimm
*dimm_sc
= xdimm_sc
;
266 val
= CSR_READ_4(dimm_sc
->dimm_parent
, dimm_sc
->dimm_reg
);
267 temp
= __SHIFTOUT(val
, dimm_sc
->dimm_mask
);
269 dimm_sensor_temp(dimm_sc
->dimm_softc
, &dimm_sc
->dimm_sensor
, temp
);