122c1b4c0SJustin Hibbits /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
322c1b4c0SJustin Hibbits *
422c1b4c0SJustin Hibbits * Copyright (c) 2018 Justin Hibbits
522c1b4c0SJustin Hibbits *
622c1b4c0SJustin Hibbits * Redistribution and use in source and binary forms, with or without
722c1b4c0SJustin Hibbits * modification, are permitted provided that the following conditions
822c1b4c0SJustin Hibbits * are met:
922c1b4c0SJustin Hibbits * 1. Redistributions of source code must retain the above copyright
1022c1b4c0SJustin Hibbits * notice, this list of conditions and the following disclaimer.
1122c1b4c0SJustin Hibbits * 2. Redistributions in binary form must reproduce the above copyright
1222c1b4c0SJustin Hibbits * notice, this list of conditions and the following disclaimer in the
1322c1b4c0SJustin Hibbits * documentation and/or other materials provided with the distribution.
1422c1b4c0SJustin Hibbits *
1522c1b4c0SJustin Hibbits * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1622c1b4c0SJustin Hibbits * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1722c1b4c0SJustin Hibbits * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1822c1b4c0SJustin Hibbits * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1922c1b4c0SJustin Hibbits * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2022c1b4c0SJustin Hibbits * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2122c1b4c0SJustin Hibbits * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2222c1b4c0SJustin Hibbits * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2322c1b4c0SJustin Hibbits * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2422c1b4c0SJustin Hibbits * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2522c1b4c0SJustin Hibbits * SUCH DAMAGE.
2622c1b4c0SJustin Hibbits */
2722c1b4c0SJustin Hibbits
2822c1b4c0SJustin Hibbits #include <sys/param.h>
2922c1b4c0SJustin Hibbits #include <sys/systm.h>
3022c1b4c0SJustin Hibbits #include <sys/bus.h>
3122c1b4c0SJustin Hibbits #include <sys/cpu.h>
3222c1b4c0SJustin Hibbits #include <sys/kernel.h>
3322c1b4c0SJustin Hibbits #include <sys/module.h>
3422c1b4c0SJustin Hibbits
3522c1b4c0SJustin Hibbits #include <dev/ofw/openfirm.h>
3622c1b4c0SJustin Hibbits
3722c1b4c0SJustin Hibbits #include "cpufreq_if.h"
3822c1b4c0SJustin Hibbits
3922c1b4c0SJustin Hibbits static int pstate_ids[256];
4022c1b4c0SJustin Hibbits static int pstate_freqs[256];
4122c1b4c0SJustin Hibbits static int npstates;
4222c1b4c0SJustin Hibbits
parse_pstates(void)4322c1b4c0SJustin Hibbits static void parse_pstates(void)
4422c1b4c0SJustin Hibbits {
4522c1b4c0SJustin Hibbits phandle_t node;
4622c1b4c0SJustin Hibbits
4722c1b4c0SJustin Hibbits node = OF_finddevice("/ibm,opal/power-mgt");
4822c1b4c0SJustin Hibbits
4922c1b4c0SJustin Hibbits /* If this fails, npstates will remain 0, and any attachment will bail. */
5022c1b4c0SJustin Hibbits if (node == -1)
5122c1b4c0SJustin Hibbits return;
5222c1b4c0SJustin Hibbits
5322c1b4c0SJustin Hibbits npstates = OF_getencprop(node, "ibm,pstate-ids", pstate_ids,
5422c1b4c0SJustin Hibbits sizeof(pstate_ids));
5522c1b4c0SJustin Hibbits if (npstates < 0) {
5622c1b4c0SJustin Hibbits npstates = 0;
5722c1b4c0SJustin Hibbits return;
5822c1b4c0SJustin Hibbits }
5922c1b4c0SJustin Hibbits
6022c1b4c0SJustin Hibbits if (OF_getencprop(node, "ibm,pstate-frequencies-mhz", pstate_freqs,
6122c1b4c0SJustin Hibbits sizeof(pstate_freqs)) != npstates) {
6222c1b4c0SJustin Hibbits npstates = 0;
6322c1b4c0SJustin Hibbits return;
6422c1b4c0SJustin Hibbits }
6522c1b4c0SJustin Hibbits npstates /= sizeof(cell_t);
6622c1b4c0SJustin Hibbits
6722c1b4c0SJustin Hibbits }
6822c1b4c0SJustin Hibbits
6922c1b4c0SJustin Hibbits /* Make this a sysinit so it runs before the cpufreq driver attaches. */
7022c1b4c0SJustin Hibbits SYSINIT(parse_pstates, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, parse_pstates, NULL);
7122c1b4c0SJustin Hibbits
7222c1b4c0SJustin Hibbits #define PMCR_UPPERPS_MASK 0xff00000000000000UL
7322c1b4c0SJustin Hibbits #define PMCR_UPPERPS_SHIFT 56
7422c1b4c0SJustin Hibbits #define PMCR_LOWERPS_MASK 0x00ff000000000000UL
7522c1b4c0SJustin Hibbits #define PMCR_LOWERPS_SHIFT 48
7622c1b4c0SJustin Hibbits #define PMCR_VERSION_MASK 0x0000000f
7722c1b4c0SJustin Hibbits #define PMCR_VERSION_1 1
7822c1b4c0SJustin Hibbits
7922c1b4c0SJustin Hibbits struct pmcr_softc {
8022c1b4c0SJustin Hibbits device_t dev;
8122c1b4c0SJustin Hibbits };
8222c1b4c0SJustin Hibbits
8322c1b4c0SJustin Hibbits static void pmcr_identify(driver_t *driver, device_t parent);
8422c1b4c0SJustin Hibbits static int pmcr_probe(device_t dev);
8522c1b4c0SJustin Hibbits static int pmcr_attach(device_t dev);
8622c1b4c0SJustin Hibbits static int pmcr_settings(device_t dev, struct cf_setting *sets, int *count);
8722c1b4c0SJustin Hibbits static int pmcr_set(device_t dev, const struct cf_setting *set);
8822c1b4c0SJustin Hibbits static int pmcr_get(device_t dev, struct cf_setting *set);
8922c1b4c0SJustin Hibbits static int pmcr_type(device_t dev, int *type);
9022c1b4c0SJustin Hibbits
9122c1b4c0SJustin Hibbits static device_method_t pmcr_methods[] = {
9222c1b4c0SJustin Hibbits /* Device interface */
9322c1b4c0SJustin Hibbits DEVMETHOD(device_identify, pmcr_identify),
9422c1b4c0SJustin Hibbits DEVMETHOD(device_probe, pmcr_probe),
9522c1b4c0SJustin Hibbits DEVMETHOD(device_attach, pmcr_attach),
9622c1b4c0SJustin Hibbits
9722c1b4c0SJustin Hibbits /* cpufreq interface */
9822c1b4c0SJustin Hibbits DEVMETHOD(cpufreq_drv_set, pmcr_set),
9922c1b4c0SJustin Hibbits DEVMETHOD(cpufreq_drv_get, pmcr_get),
10022c1b4c0SJustin Hibbits DEVMETHOD(cpufreq_drv_type, pmcr_type),
10122c1b4c0SJustin Hibbits DEVMETHOD(cpufreq_drv_settings, pmcr_settings),
10222c1b4c0SJustin Hibbits {0, 0}
10322c1b4c0SJustin Hibbits };
10422c1b4c0SJustin Hibbits
10522c1b4c0SJustin Hibbits static driver_t pmcr_driver = {
10622c1b4c0SJustin Hibbits "pmcr",
10722c1b4c0SJustin Hibbits pmcr_methods,
10822c1b4c0SJustin Hibbits sizeof(struct pmcr_softc)
10922c1b4c0SJustin Hibbits };
11022c1b4c0SJustin Hibbits
111b3407dccSJohn Baldwin DRIVER_MODULE(pmcr, cpu, pmcr_driver, 0, 0);
11222c1b4c0SJustin Hibbits
11322c1b4c0SJustin Hibbits static void
pmcr_identify(driver_t * driver,device_t parent)11422c1b4c0SJustin Hibbits pmcr_identify(driver_t *driver, device_t parent)
11522c1b4c0SJustin Hibbits {
11622c1b4c0SJustin Hibbits
11722c1b4c0SJustin Hibbits /* Make sure we're not being doubly invoked. */
11822c1b4c0SJustin Hibbits if (device_find_child(parent, "pmcr", -1) != NULL)
11922c1b4c0SJustin Hibbits return;
12022c1b4c0SJustin Hibbits
12122c1b4c0SJustin Hibbits /*
12222c1b4c0SJustin Hibbits * We attach a child for every CPU since settings need to
12322c1b4c0SJustin Hibbits * be performed on every CPU in the SMP case.
12422c1b4c0SJustin Hibbits */
12522c1b4c0SJustin Hibbits if (BUS_ADD_CHILD(parent, 10, "pmcr", -1) == NULL)
12622c1b4c0SJustin Hibbits device_printf(parent, "add pmcr child failed\n");
12722c1b4c0SJustin Hibbits }
12822c1b4c0SJustin Hibbits
12922c1b4c0SJustin Hibbits static int
pmcr_probe(device_t dev)13022c1b4c0SJustin Hibbits pmcr_probe(device_t dev)
13122c1b4c0SJustin Hibbits {
13222c1b4c0SJustin Hibbits if (resource_disabled("pmcr", 0))
13322c1b4c0SJustin Hibbits return (ENXIO);
13422c1b4c0SJustin Hibbits
13522c1b4c0SJustin Hibbits if (npstates == 0)
13622c1b4c0SJustin Hibbits return (ENXIO);
13722c1b4c0SJustin Hibbits
13822c1b4c0SJustin Hibbits device_set_desc(dev, "Power Management Control Register");
13922c1b4c0SJustin Hibbits return (0);
14022c1b4c0SJustin Hibbits }
14122c1b4c0SJustin Hibbits
14222c1b4c0SJustin Hibbits static int
pmcr_attach(device_t dev)14322c1b4c0SJustin Hibbits pmcr_attach(device_t dev)
14422c1b4c0SJustin Hibbits {
14522c1b4c0SJustin Hibbits struct pmcr_softc *sc;
14622c1b4c0SJustin Hibbits
14722c1b4c0SJustin Hibbits sc = device_get_softc(dev);
14822c1b4c0SJustin Hibbits sc->dev = dev;
14922c1b4c0SJustin Hibbits
15022c1b4c0SJustin Hibbits cpufreq_register(dev);
15122c1b4c0SJustin Hibbits return (0);
15222c1b4c0SJustin Hibbits }
15322c1b4c0SJustin Hibbits
15422c1b4c0SJustin Hibbits static int
pmcr_settings(device_t dev,struct cf_setting * sets,int * count)15522c1b4c0SJustin Hibbits pmcr_settings(device_t dev, struct cf_setting *sets, int *count)
15622c1b4c0SJustin Hibbits {
15722c1b4c0SJustin Hibbits int i;
15822c1b4c0SJustin Hibbits
15922c1b4c0SJustin Hibbits if (sets == NULL || count == NULL)
16022c1b4c0SJustin Hibbits return (EINVAL);
16122c1b4c0SJustin Hibbits if (*count < npstates)
16222c1b4c0SJustin Hibbits return (E2BIG);
16322c1b4c0SJustin Hibbits
16422c1b4c0SJustin Hibbits /* Return a list of valid settings for this driver. */
16522c1b4c0SJustin Hibbits memset(sets, CPUFREQ_VAL_UNKNOWN, sizeof(*sets) * npstates);
16622c1b4c0SJustin Hibbits
16722c1b4c0SJustin Hibbits for (i = 0; i < npstates; i++) {
16822c1b4c0SJustin Hibbits sets[i].freq = pstate_freqs[i];
16922c1b4c0SJustin Hibbits sets[i].spec[0] = pstate_ids[i];
170d86e0b33SConrad Meyer sets[i].spec[1] = i;
17122c1b4c0SJustin Hibbits sets[i].dev = dev;
17222c1b4c0SJustin Hibbits }
17322c1b4c0SJustin Hibbits *count = npstates;
17422c1b4c0SJustin Hibbits
17522c1b4c0SJustin Hibbits return (0);
17622c1b4c0SJustin Hibbits }
17722c1b4c0SJustin Hibbits
17822c1b4c0SJustin Hibbits static int
pmcr_set(device_t dev,const struct cf_setting * set)17922c1b4c0SJustin Hibbits pmcr_set(device_t dev, const struct cf_setting *set)
18022c1b4c0SJustin Hibbits {
18122c1b4c0SJustin Hibbits register_t pmcr;
18222c1b4c0SJustin Hibbits
18322c1b4c0SJustin Hibbits if (set == NULL)
18422c1b4c0SJustin Hibbits return (EINVAL);
18522c1b4c0SJustin Hibbits
186d86e0b33SConrad Meyer if (set->spec[1] < 0 || set->spec[1] >= npstates)
18722c1b4c0SJustin Hibbits return (EINVAL);
18822c1b4c0SJustin Hibbits
189d86e0b33SConrad Meyer pmcr = ((long)set->spec[0] << PMCR_LOWERPS_SHIFT) & PMCR_LOWERPS_MASK;
190d86e0b33SConrad Meyer pmcr |= ((long)set->spec[0] << PMCR_UPPERPS_SHIFT) & PMCR_UPPERPS_MASK;
19122c1b4c0SJustin Hibbits pmcr |= PMCR_VERSION_1;
19222c1b4c0SJustin Hibbits
19322c1b4c0SJustin Hibbits mtspr(SPR_PMCR, pmcr);
19422c1b4c0SJustin Hibbits powerpc_sync(); isync();
19522c1b4c0SJustin Hibbits
19622c1b4c0SJustin Hibbits return (0);
19722c1b4c0SJustin Hibbits }
19822c1b4c0SJustin Hibbits
19922c1b4c0SJustin Hibbits static int
pmcr_get(device_t dev,struct cf_setting * set)20022c1b4c0SJustin Hibbits pmcr_get(device_t dev, struct cf_setting *set)
20122c1b4c0SJustin Hibbits {
20222c1b4c0SJustin Hibbits register_t pmcr;
20322c1b4c0SJustin Hibbits int i, pstate;
20422c1b4c0SJustin Hibbits
20522c1b4c0SJustin Hibbits if (set == NULL)
20622c1b4c0SJustin Hibbits return (EINVAL);
20722c1b4c0SJustin Hibbits
20822c1b4c0SJustin Hibbits memset(set, CPUFREQ_VAL_UNKNOWN, sizeof(*set));
20922c1b4c0SJustin Hibbits
21022c1b4c0SJustin Hibbits pmcr = mfspr(SPR_PMCR);
21122c1b4c0SJustin Hibbits
21222c1b4c0SJustin Hibbits pstate = (pmcr & PMCR_LOWERPS_MASK) >> PMCR_LOWERPS_SHIFT;
21322c1b4c0SJustin Hibbits
21422c1b4c0SJustin Hibbits for (i = 0; i < npstates && pstate_ids[i] != pstate; i++)
21522c1b4c0SJustin Hibbits ;
21622c1b4c0SJustin Hibbits
21722c1b4c0SJustin Hibbits if (i == npstates)
21822c1b4c0SJustin Hibbits return (EINVAL);
21922c1b4c0SJustin Hibbits
22022c1b4c0SJustin Hibbits set->spec[0] = pstate;
221d86e0b33SConrad Meyer set->spec[1] = i;
22222c1b4c0SJustin Hibbits set->freq = pstate_freqs[i];
22322c1b4c0SJustin Hibbits
22422c1b4c0SJustin Hibbits set->dev = dev;
22522c1b4c0SJustin Hibbits
22622c1b4c0SJustin Hibbits return (0);
22722c1b4c0SJustin Hibbits }
22822c1b4c0SJustin Hibbits
22922c1b4c0SJustin Hibbits static int
pmcr_type(device_t dev,int * type)23022c1b4c0SJustin Hibbits pmcr_type(device_t dev, int *type)
23122c1b4c0SJustin Hibbits {
23222c1b4c0SJustin Hibbits
23322c1b4c0SJustin Hibbits if (type == NULL)
23422c1b4c0SJustin Hibbits return (EINVAL);
23522c1b4c0SJustin Hibbits
23622c1b4c0SJustin Hibbits *type = CPUFREQ_TYPE_ABSOLUTE;
23722c1b4c0SJustin Hibbits return (0);
23822c1b4c0SJustin Hibbits }
239