2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2018 Justin Hibbits
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 WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
38 #include <dev/ofw/openfirm.h>
40 #include "cpufreq_if.h"
42 static int pstate_ids[256];
43 static int pstate_freqs[256];
46 static void parse_pstates(void)
50 node = OF_finddevice("/ibm,opal/power-mgt");
52 /* If this fails, npstates will remain 0, and any attachment will bail. */
56 npstates = OF_getencprop(node, "ibm,pstate-ids", pstate_ids,
63 if (OF_getencprop(node, "ibm,pstate-frequencies-mhz", pstate_freqs,
64 sizeof(pstate_freqs)) != npstates) {
68 npstates /= sizeof(cell_t);
72 /* Make this a sysinit so it runs before the cpufreq driver attaches. */
73 SYSINIT(parse_pstates, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, parse_pstates, NULL);
75 #define PMCR_UPPERPS_MASK 0xff00000000000000UL
76 #define PMCR_UPPERPS_SHIFT 56
77 #define PMCR_LOWERPS_MASK 0x00ff000000000000UL
78 #define PMCR_LOWERPS_SHIFT 48
79 #define PMCR_VERSION_MASK 0x0000000f
80 #define PMCR_VERSION_1 1
86 static void pmcr_identify(driver_t *driver, device_t parent);
87 static int pmcr_probe(device_t dev);
88 static int pmcr_attach(device_t dev);
89 static int pmcr_settings(device_t dev, struct cf_setting *sets, int *count);
90 static int pmcr_set(device_t dev, const struct cf_setting *set);
91 static int pmcr_get(device_t dev, struct cf_setting *set);
92 static int pmcr_type(device_t dev, int *type);
94 static device_method_t pmcr_methods[] = {
95 /* Device interface */
96 DEVMETHOD(device_identify, pmcr_identify),
97 DEVMETHOD(device_probe, pmcr_probe),
98 DEVMETHOD(device_attach, pmcr_attach),
100 /* cpufreq interface */
101 DEVMETHOD(cpufreq_drv_set, pmcr_set),
102 DEVMETHOD(cpufreq_drv_get, pmcr_get),
103 DEVMETHOD(cpufreq_drv_type, pmcr_type),
104 DEVMETHOD(cpufreq_drv_settings, pmcr_settings),
108 static driver_t pmcr_driver = {
111 sizeof(struct pmcr_softc)
114 DRIVER_MODULE(pmcr, cpu, pmcr_driver, 0, 0);
117 pmcr_identify(driver_t *driver, device_t parent)
120 /* Make sure we're not being doubly invoked. */
121 if (device_find_child(parent, "pmcr", -1) != NULL)
125 * We attach a child for every CPU since settings need to
126 * be performed on every CPU in the SMP case.
128 if (BUS_ADD_CHILD(parent, 10, "pmcr", -1) == NULL)
129 device_printf(parent, "add pmcr child failed\n");
133 pmcr_probe(device_t dev)
135 if (resource_disabled("pmcr", 0))
141 device_set_desc(dev, "Power Management Control Register");
146 pmcr_attach(device_t dev)
148 struct pmcr_softc *sc;
150 sc = device_get_softc(dev);
153 cpufreq_register(dev);
158 pmcr_settings(device_t dev, struct cf_setting *sets, int *count)
162 if (sets == NULL || count == NULL)
164 if (*count < npstates)
167 /* Return a list of valid settings for this driver. */
168 memset(sets, CPUFREQ_VAL_UNKNOWN, sizeof(*sets) * npstates);
170 for (i = 0; i < npstates; i++) {
171 sets[i].freq = pstate_freqs[i];
172 sets[i].spec[0] = pstate_ids[i];
182 pmcr_set(device_t dev, const struct cf_setting *set)
189 if (set->spec[1] < 0 || set->spec[1] >= npstates)
192 pmcr = ((long)set->spec[0] << PMCR_LOWERPS_SHIFT) & PMCR_LOWERPS_MASK;
193 pmcr |= ((long)set->spec[0] << PMCR_UPPERPS_SHIFT) & PMCR_UPPERPS_MASK;
194 pmcr |= PMCR_VERSION_1;
196 mtspr(SPR_PMCR, pmcr);
197 powerpc_sync(); isync();
203 pmcr_get(device_t dev, struct cf_setting *set)
211 memset(set, CPUFREQ_VAL_UNKNOWN, sizeof(*set));
213 pmcr = mfspr(SPR_PMCR);
215 pstate = (pmcr & PMCR_LOWERPS_MASK) >> PMCR_LOWERPS_SHIFT;
217 for (i = 0; i < npstates && pstate_ids[i] != pstate; i++)
223 set->spec[0] = pstate;
225 set->freq = pstate_freqs[i];
233 pmcr_type(device_t dev, int *type)
239 *type = CPUFREQ_TYPE_ABSOLUTE;