xref: /freebsd/sys/powerpc/cpufreq/pmcr.c (revision 4d846d260e2b9a3d4d0a701462568268cbfe7a5b)
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/cdefs.h>
2922c1b4c0SJustin Hibbits __FBSDID("$FreeBSD$");
3022c1b4c0SJustin Hibbits 
3122c1b4c0SJustin Hibbits #include <sys/param.h>
3222c1b4c0SJustin Hibbits #include <sys/systm.h>
3322c1b4c0SJustin Hibbits #include <sys/bus.h>
3422c1b4c0SJustin Hibbits #include <sys/cpu.h>
3522c1b4c0SJustin Hibbits #include <sys/kernel.h>
3622c1b4c0SJustin Hibbits #include <sys/module.h>
3722c1b4c0SJustin Hibbits 
3822c1b4c0SJustin Hibbits #include <dev/ofw/openfirm.h>
3922c1b4c0SJustin Hibbits 
4022c1b4c0SJustin Hibbits #include "cpufreq_if.h"
4122c1b4c0SJustin Hibbits 
4222c1b4c0SJustin Hibbits static int pstate_ids[256];
4322c1b4c0SJustin Hibbits static int pstate_freqs[256];
4422c1b4c0SJustin Hibbits static int npstates;
4522c1b4c0SJustin Hibbits 
4622c1b4c0SJustin Hibbits static void parse_pstates(void)
4722c1b4c0SJustin Hibbits {
4822c1b4c0SJustin Hibbits 	phandle_t node;
4922c1b4c0SJustin Hibbits 
5022c1b4c0SJustin Hibbits 	node = OF_finddevice("/ibm,opal/power-mgt");
5122c1b4c0SJustin Hibbits 
5222c1b4c0SJustin Hibbits 	/* If this fails, npstates will remain 0, and any attachment will bail. */
5322c1b4c0SJustin Hibbits 	if (node == -1)
5422c1b4c0SJustin Hibbits 		return;
5522c1b4c0SJustin Hibbits 
5622c1b4c0SJustin Hibbits 	npstates = OF_getencprop(node, "ibm,pstate-ids", pstate_ids,
5722c1b4c0SJustin Hibbits 	    sizeof(pstate_ids));
5822c1b4c0SJustin Hibbits 	if (npstates < 0) {
5922c1b4c0SJustin Hibbits 		npstates = 0;
6022c1b4c0SJustin Hibbits 		return;
6122c1b4c0SJustin Hibbits 	}
6222c1b4c0SJustin Hibbits 
6322c1b4c0SJustin Hibbits 	if (OF_getencprop(node, "ibm,pstate-frequencies-mhz", pstate_freqs,
6422c1b4c0SJustin Hibbits 	    sizeof(pstate_freqs)) != npstates) {
6522c1b4c0SJustin Hibbits 		npstates = 0;
6622c1b4c0SJustin Hibbits 		return;
6722c1b4c0SJustin Hibbits 	}
6822c1b4c0SJustin Hibbits 	npstates /= sizeof(cell_t);
6922c1b4c0SJustin Hibbits 
7022c1b4c0SJustin Hibbits }
7122c1b4c0SJustin Hibbits 
7222c1b4c0SJustin Hibbits /* Make this a sysinit so it runs before the cpufreq driver attaches. */
7322c1b4c0SJustin Hibbits SYSINIT(parse_pstates, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, parse_pstates, NULL);
7422c1b4c0SJustin Hibbits 
7522c1b4c0SJustin Hibbits #define	PMCR_UPPERPS_MASK	0xff00000000000000UL
7622c1b4c0SJustin Hibbits #define	PMCR_UPPERPS_SHIFT	56
7722c1b4c0SJustin Hibbits #define	PMCR_LOWERPS_MASK	0x00ff000000000000UL
7822c1b4c0SJustin Hibbits #define	PMCR_LOWERPS_SHIFT	48
7922c1b4c0SJustin Hibbits #define	PMCR_VERSION_MASK	0x0000000f
8022c1b4c0SJustin Hibbits #define	  PMCR_VERSION_1	  1
8122c1b4c0SJustin Hibbits 
8222c1b4c0SJustin Hibbits struct pmcr_softc {
8322c1b4c0SJustin Hibbits 	device_t dev;
8422c1b4c0SJustin Hibbits };
8522c1b4c0SJustin Hibbits 
8622c1b4c0SJustin Hibbits static void	pmcr_identify(driver_t *driver, device_t parent);
8722c1b4c0SJustin Hibbits static int	pmcr_probe(device_t dev);
8822c1b4c0SJustin Hibbits static int	pmcr_attach(device_t dev);
8922c1b4c0SJustin Hibbits static int	pmcr_settings(device_t dev, struct cf_setting *sets, int *count);
9022c1b4c0SJustin Hibbits static int	pmcr_set(device_t dev, const struct cf_setting *set);
9122c1b4c0SJustin Hibbits static int	pmcr_get(device_t dev, struct cf_setting *set);
9222c1b4c0SJustin Hibbits static int	pmcr_type(device_t dev, int *type);
9322c1b4c0SJustin Hibbits 
9422c1b4c0SJustin Hibbits static device_method_t pmcr_methods[] = {
9522c1b4c0SJustin Hibbits 	/* Device interface */
9622c1b4c0SJustin Hibbits 	DEVMETHOD(device_identify,	pmcr_identify),
9722c1b4c0SJustin Hibbits 	DEVMETHOD(device_probe,		pmcr_probe),
9822c1b4c0SJustin Hibbits 	DEVMETHOD(device_attach,	pmcr_attach),
9922c1b4c0SJustin Hibbits 
10022c1b4c0SJustin Hibbits 	/* cpufreq interface */
10122c1b4c0SJustin Hibbits 	DEVMETHOD(cpufreq_drv_set,	pmcr_set),
10222c1b4c0SJustin Hibbits 	DEVMETHOD(cpufreq_drv_get,	pmcr_get),
10322c1b4c0SJustin Hibbits 	DEVMETHOD(cpufreq_drv_type,	pmcr_type),
10422c1b4c0SJustin Hibbits 	DEVMETHOD(cpufreq_drv_settings,	pmcr_settings),
10522c1b4c0SJustin Hibbits 	{0, 0}
10622c1b4c0SJustin Hibbits };
10722c1b4c0SJustin Hibbits 
10822c1b4c0SJustin Hibbits static driver_t pmcr_driver = {
10922c1b4c0SJustin Hibbits 	"pmcr",
11022c1b4c0SJustin Hibbits 	pmcr_methods,
11122c1b4c0SJustin Hibbits 	sizeof(struct pmcr_softc)
11222c1b4c0SJustin Hibbits };
11322c1b4c0SJustin Hibbits 
114b3407dccSJohn Baldwin DRIVER_MODULE(pmcr, cpu, pmcr_driver, 0, 0);
11522c1b4c0SJustin Hibbits 
11622c1b4c0SJustin Hibbits static void
11722c1b4c0SJustin Hibbits pmcr_identify(driver_t *driver, device_t parent)
11822c1b4c0SJustin Hibbits {
11922c1b4c0SJustin Hibbits 
12022c1b4c0SJustin Hibbits 	/* Make sure we're not being doubly invoked. */
12122c1b4c0SJustin Hibbits 	if (device_find_child(parent, "pmcr", -1) != NULL)
12222c1b4c0SJustin Hibbits 		return;
12322c1b4c0SJustin Hibbits 
12422c1b4c0SJustin Hibbits 	/*
12522c1b4c0SJustin Hibbits 	 * We attach a child for every CPU since settings need to
12622c1b4c0SJustin Hibbits 	 * be performed on every CPU in the SMP case.
12722c1b4c0SJustin Hibbits 	 */
12822c1b4c0SJustin Hibbits 	if (BUS_ADD_CHILD(parent, 10, "pmcr", -1) == NULL)
12922c1b4c0SJustin Hibbits 		device_printf(parent, "add pmcr child failed\n");
13022c1b4c0SJustin Hibbits }
13122c1b4c0SJustin Hibbits 
13222c1b4c0SJustin Hibbits static int
13322c1b4c0SJustin Hibbits pmcr_probe(device_t dev)
13422c1b4c0SJustin Hibbits {
13522c1b4c0SJustin Hibbits 	if (resource_disabled("pmcr", 0))
13622c1b4c0SJustin Hibbits 		return (ENXIO);
13722c1b4c0SJustin Hibbits 
13822c1b4c0SJustin Hibbits 	if (npstates == 0)
13922c1b4c0SJustin Hibbits 		return (ENXIO);
14022c1b4c0SJustin Hibbits 
14122c1b4c0SJustin Hibbits 	device_set_desc(dev, "Power Management Control Register");
14222c1b4c0SJustin Hibbits 	return (0);
14322c1b4c0SJustin Hibbits }
14422c1b4c0SJustin Hibbits 
14522c1b4c0SJustin Hibbits static int
14622c1b4c0SJustin Hibbits pmcr_attach(device_t dev)
14722c1b4c0SJustin Hibbits {
14822c1b4c0SJustin Hibbits 	struct pmcr_softc *sc;
14922c1b4c0SJustin Hibbits 
15022c1b4c0SJustin Hibbits 	sc = device_get_softc(dev);
15122c1b4c0SJustin Hibbits 	sc->dev = dev;
15222c1b4c0SJustin Hibbits 
15322c1b4c0SJustin Hibbits 	cpufreq_register(dev);
15422c1b4c0SJustin Hibbits 	return (0);
15522c1b4c0SJustin Hibbits }
15622c1b4c0SJustin Hibbits 
15722c1b4c0SJustin Hibbits static int
15822c1b4c0SJustin Hibbits pmcr_settings(device_t dev, struct cf_setting *sets, int *count)
15922c1b4c0SJustin Hibbits {
16022c1b4c0SJustin Hibbits 	int i;
16122c1b4c0SJustin Hibbits 
16222c1b4c0SJustin Hibbits 	if (sets == NULL || count == NULL)
16322c1b4c0SJustin Hibbits 		return (EINVAL);
16422c1b4c0SJustin Hibbits 	if (*count < npstates)
16522c1b4c0SJustin Hibbits 		return (E2BIG);
16622c1b4c0SJustin Hibbits 
16722c1b4c0SJustin Hibbits 	/* Return a list of valid settings for this driver. */
16822c1b4c0SJustin Hibbits 	memset(sets, CPUFREQ_VAL_UNKNOWN, sizeof(*sets) * npstates);
16922c1b4c0SJustin Hibbits 
17022c1b4c0SJustin Hibbits 	for (i = 0; i < npstates; i++) {
17122c1b4c0SJustin Hibbits 		sets[i].freq = pstate_freqs[i];
17222c1b4c0SJustin Hibbits 		sets[i].spec[0] = pstate_ids[i];
173d86e0b33SConrad Meyer 		sets[i].spec[1] = i;
17422c1b4c0SJustin Hibbits 		sets[i].dev = dev;
17522c1b4c0SJustin Hibbits 	}
17622c1b4c0SJustin Hibbits 	*count = npstates;
17722c1b4c0SJustin Hibbits 
17822c1b4c0SJustin Hibbits 	return (0);
17922c1b4c0SJustin Hibbits }
18022c1b4c0SJustin Hibbits 
18122c1b4c0SJustin Hibbits static int
18222c1b4c0SJustin Hibbits pmcr_set(device_t dev, const struct cf_setting *set)
18322c1b4c0SJustin Hibbits {
18422c1b4c0SJustin Hibbits 	register_t pmcr;
18522c1b4c0SJustin Hibbits 
18622c1b4c0SJustin Hibbits 	if (set == NULL)
18722c1b4c0SJustin Hibbits 		return (EINVAL);
18822c1b4c0SJustin Hibbits 
189d86e0b33SConrad Meyer 	if (set->spec[1] < 0 || set->spec[1] >= npstates)
19022c1b4c0SJustin Hibbits 		return (EINVAL);
19122c1b4c0SJustin Hibbits 
192d86e0b33SConrad Meyer 	pmcr = ((long)set->spec[0] << PMCR_LOWERPS_SHIFT) & PMCR_LOWERPS_MASK;
193d86e0b33SConrad Meyer 	pmcr |= ((long)set->spec[0] << PMCR_UPPERPS_SHIFT) & PMCR_UPPERPS_MASK;
19422c1b4c0SJustin Hibbits 	pmcr |= PMCR_VERSION_1;
19522c1b4c0SJustin Hibbits 
19622c1b4c0SJustin Hibbits 	mtspr(SPR_PMCR, pmcr);
19722c1b4c0SJustin Hibbits 	powerpc_sync(); isync();
19822c1b4c0SJustin Hibbits 
19922c1b4c0SJustin Hibbits 	return (0);
20022c1b4c0SJustin Hibbits }
20122c1b4c0SJustin Hibbits 
20222c1b4c0SJustin Hibbits static int
20322c1b4c0SJustin Hibbits pmcr_get(device_t dev, struct cf_setting *set)
20422c1b4c0SJustin Hibbits {
20522c1b4c0SJustin Hibbits 	register_t pmcr;
20622c1b4c0SJustin Hibbits 	int i, pstate;
20722c1b4c0SJustin Hibbits 
20822c1b4c0SJustin Hibbits 	if (set == NULL)
20922c1b4c0SJustin Hibbits 		return (EINVAL);
21022c1b4c0SJustin Hibbits 
21122c1b4c0SJustin Hibbits 	memset(set, CPUFREQ_VAL_UNKNOWN, sizeof(*set));
21222c1b4c0SJustin Hibbits 
21322c1b4c0SJustin Hibbits 	pmcr = mfspr(SPR_PMCR);
21422c1b4c0SJustin Hibbits 
21522c1b4c0SJustin Hibbits 	pstate = (pmcr & PMCR_LOWERPS_MASK) >> PMCR_LOWERPS_SHIFT;
21622c1b4c0SJustin Hibbits 
21722c1b4c0SJustin Hibbits 	for (i = 0; i < npstates && pstate_ids[i] != pstate; i++)
21822c1b4c0SJustin Hibbits 		;
21922c1b4c0SJustin Hibbits 
22022c1b4c0SJustin Hibbits 	if (i == npstates)
22122c1b4c0SJustin Hibbits 		return (EINVAL);
22222c1b4c0SJustin Hibbits 
22322c1b4c0SJustin Hibbits 	set->spec[0] = pstate;
224d86e0b33SConrad Meyer 	set->spec[1] = i;
22522c1b4c0SJustin Hibbits 	set->freq = pstate_freqs[i];
22622c1b4c0SJustin Hibbits 
22722c1b4c0SJustin Hibbits 	set->dev = dev;
22822c1b4c0SJustin Hibbits 
22922c1b4c0SJustin Hibbits 	return (0);
23022c1b4c0SJustin Hibbits }
23122c1b4c0SJustin Hibbits 
23222c1b4c0SJustin Hibbits static int
23322c1b4c0SJustin Hibbits pmcr_type(device_t dev, int *type)
23422c1b4c0SJustin Hibbits {
23522c1b4c0SJustin Hibbits 
23622c1b4c0SJustin Hibbits 	if (type == NULL)
23722c1b4c0SJustin Hibbits 		return (EINVAL);
23822c1b4c0SJustin Hibbits 
23922c1b4c0SJustin Hibbits 	*type = CPUFREQ_TYPE_ABSOLUTE;
24022c1b4c0SJustin Hibbits 	return (0);
24122c1b4c0SJustin Hibbits }
242