122c1b4c0SJustin Hibbits /*- 222c1b4c0SJustin Hibbits * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 10622c1b4c0SJustin Hibbits {0, 0} 10722c1b4c0SJustin Hibbits }; 10822c1b4c0SJustin Hibbits 10922c1b4c0SJustin Hibbits static driver_t pmcr_driver = { 11022c1b4c0SJustin Hibbits "pmcr", 11122c1b4c0SJustin Hibbits pmcr_methods, 11222c1b4c0SJustin Hibbits sizeof(struct pmcr_softc) 11322c1b4c0SJustin Hibbits }; 11422c1b4c0SJustin Hibbits 11522c1b4c0SJustin Hibbits static devclass_t pmcr_devclass; 11622c1b4c0SJustin Hibbits DRIVER_MODULE(pmcr, cpu, pmcr_driver, pmcr_devclass, 0, 0); 11722c1b4c0SJustin Hibbits 11822c1b4c0SJustin Hibbits static void 11922c1b4c0SJustin Hibbits pmcr_identify(driver_t *driver, device_t parent) 12022c1b4c0SJustin Hibbits { 12122c1b4c0SJustin Hibbits 12222c1b4c0SJustin Hibbits /* Make sure we're not being doubly invoked. */ 12322c1b4c0SJustin Hibbits if (device_find_child(parent, "pmcr", -1) != NULL) 12422c1b4c0SJustin Hibbits return; 12522c1b4c0SJustin Hibbits 12622c1b4c0SJustin Hibbits /* 12722c1b4c0SJustin Hibbits * We attach a child for every CPU since settings need to 12822c1b4c0SJustin Hibbits * be performed on every CPU in the SMP case. 12922c1b4c0SJustin Hibbits */ 13022c1b4c0SJustin Hibbits if (BUS_ADD_CHILD(parent, 10, "pmcr", -1) == NULL) 13122c1b4c0SJustin Hibbits device_printf(parent, "add pmcr child failed\n"); 13222c1b4c0SJustin Hibbits } 13322c1b4c0SJustin Hibbits 13422c1b4c0SJustin Hibbits static int 13522c1b4c0SJustin Hibbits pmcr_probe(device_t dev) 13622c1b4c0SJustin Hibbits { 13722c1b4c0SJustin Hibbits if (resource_disabled("pmcr", 0)) 13822c1b4c0SJustin Hibbits return (ENXIO); 13922c1b4c0SJustin Hibbits 14022c1b4c0SJustin Hibbits if (npstates == 0) 14122c1b4c0SJustin Hibbits return (ENXIO); 14222c1b4c0SJustin Hibbits 14322c1b4c0SJustin Hibbits device_set_desc(dev, "Power Management Control Register"); 14422c1b4c0SJustin Hibbits return (0); 14522c1b4c0SJustin Hibbits } 14622c1b4c0SJustin Hibbits 14722c1b4c0SJustin Hibbits static int 14822c1b4c0SJustin Hibbits pmcr_attach(device_t dev) 14922c1b4c0SJustin Hibbits { 15022c1b4c0SJustin Hibbits struct pmcr_softc *sc; 15122c1b4c0SJustin Hibbits 15222c1b4c0SJustin Hibbits sc = device_get_softc(dev); 15322c1b4c0SJustin Hibbits sc->dev = dev; 15422c1b4c0SJustin Hibbits 15522c1b4c0SJustin Hibbits cpufreq_register(dev); 15622c1b4c0SJustin Hibbits return (0); 15722c1b4c0SJustin Hibbits } 15822c1b4c0SJustin Hibbits 15922c1b4c0SJustin Hibbits static int 16022c1b4c0SJustin Hibbits pmcr_settings(device_t dev, struct cf_setting *sets, int *count) 16122c1b4c0SJustin Hibbits { 16222c1b4c0SJustin Hibbits struct pmcr_softc *sc; 16322c1b4c0SJustin Hibbits int i; 16422c1b4c0SJustin Hibbits 16522c1b4c0SJustin Hibbits sc = device_get_softc(dev); 16622c1b4c0SJustin Hibbits if (sets == NULL || count == NULL) 16722c1b4c0SJustin Hibbits return (EINVAL); 16822c1b4c0SJustin Hibbits if (*count < npstates) 16922c1b4c0SJustin Hibbits return (E2BIG); 17022c1b4c0SJustin Hibbits 17122c1b4c0SJustin Hibbits /* Return a list of valid settings for this driver. */ 17222c1b4c0SJustin Hibbits memset(sets, CPUFREQ_VAL_UNKNOWN, sizeof(*sets) * npstates); 17322c1b4c0SJustin Hibbits 17422c1b4c0SJustin Hibbits for (i = 0; i < npstates; i++) { 17522c1b4c0SJustin Hibbits sets[i].freq = pstate_freqs[i]; 17622c1b4c0SJustin Hibbits sets[i].spec[0] = pstate_ids[i]; 17722c1b4c0SJustin Hibbits sets[i].dev = dev; 17822c1b4c0SJustin Hibbits } 17922c1b4c0SJustin Hibbits *count = npstates; 18022c1b4c0SJustin Hibbits 18122c1b4c0SJustin Hibbits return (0); 18222c1b4c0SJustin Hibbits } 18322c1b4c0SJustin Hibbits 18422c1b4c0SJustin Hibbits static int 18522c1b4c0SJustin Hibbits pmcr_set(device_t dev, const struct cf_setting *set) 18622c1b4c0SJustin Hibbits { 18722c1b4c0SJustin Hibbits register_t pmcr; 18822c1b4c0SJustin Hibbits 18922c1b4c0SJustin Hibbits if (set == NULL) 19022c1b4c0SJustin Hibbits return (EINVAL); 19122c1b4c0SJustin Hibbits 19222c1b4c0SJustin Hibbits if (set->spec[0] < 0 || set->spec[0] > npstates) 19322c1b4c0SJustin Hibbits return (EINVAL); 19422c1b4c0SJustin Hibbits 19522c1b4c0SJustin Hibbits pmcr = ((long)pstate_ids[set->spec[0]] << PMCR_LOWERPS_SHIFT) & 19622c1b4c0SJustin Hibbits PMCR_LOWERPS_MASK; 19722c1b4c0SJustin Hibbits pmcr |= ((long)pstate_ids[set->spec[0]] << PMCR_UPPERPS_SHIFT) & 19822c1b4c0SJustin Hibbits PMCR_UPPERPS_MASK 19922c1b4c0SJustin Hibbits pmcr |= PMCR_VERSION_1; 20022c1b4c0SJustin Hibbits 20122c1b4c0SJustin Hibbits mtspr(SPR_PMCR, pmcr); 20222c1b4c0SJustin Hibbits powerpc_sync(); isync(); 20322c1b4c0SJustin Hibbits 20422c1b4c0SJustin Hibbits return (0); 20522c1b4c0SJustin Hibbits } 20622c1b4c0SJustin Hibbits 20722c1b4c0SJustin Hibbits static int 20822c1b4c0SJustin Hibbits pmcr_get(device_t dev, struct cf_setting *set) 20922c1b4c0SJustin Hibbits { 21022c1b4c0SJustin Hibbits struct pmcr_softc *sc; 21122c1b4c0SJustin Hibbits register_t pmcr; 21222c1b4c0SJustin Hibbits int i, pstate; 21322c1b4c0SJustin Hibbits 21422c1b4c0SJustin Hibbits if (set == NULL) 21522c1b4c0SJustin Hibbits return (EINVAL); 21622c1b4c0SJustin Hibbits sc = device_get_softc(dev); 21722c1b4c0SJustin Hibbits 21822c1b4c0SJustin Hibbits memset(set, CPUFREQ_VAL_UNKNOWN, sizeof(*set)); 21922c1b4c0SJustin Hibbits 22022c1b4c0SJustin Hibbits pmcr = mfspr(SPR_PMCR); 22122c1b4c0SJustin Hibbits 22222c1b4c0SJustin Hibbits pstate = (pmcr & PMCR_LOWERPS_MASK) >> PMCR_LOWERPS_SHIFT; 22322c1b4c0SJustin Hibbits 22422c1b4c0SJustin Hibbits for (i = 0; i < npstates && pstate_ids[i] != pstate; i++) 22522c1b4c0SJustin Hibbits ; 22622c1b4c0SJustin Hibbits 22722c1b4c0SJustin Hibbits if (i == npstates) 22822c1b4c0SJustin Hibbits return (EINVAL); 22922c1b4c0SJustin Hibbits 23022c1b4c0SJustin Hibbits set->spec[0] = pstate; 23122c1b4c0SJustin Hibbits set->freq = pstate_freqs[i]; 23222c1b4c0SJustin Hibbits 23322c1b4c0SJustin Hibbits set->dev = dev; 23422c1b4c0SJustin Hibbits 23522c1b4c0SJustin Hibbits return (0); 23622c1b4c0SJustin Hibbits } 23722c1b4c0SJustin Hibbits 23822c1b4c0SJustin Hibbits static int 23922c1b4c0SJustin Hibbits pmcr_type(device_t dev, int *type) 24022c1b4c0SJustin Hibbits { 24122c1b4c0SJustin Hibbits 24222c1b4c0SJustin Hibbits if (type == NULL) 24322c1b4c0SJustin Hibbits return (EINVAL); 24422c1b4c0SJustin Hibbits 24522c1b4c0SJustin Hibbits *type = CPUFREQ_TYPE_ABSOLUTE; 24622c1b4c0SJustin Hibbits return (0); 24722c1b4c0SJustin Hibbits } 24822c1b4c0SJustin Hibbits 249