Home | History | Annotate | Line # | Download | only in fdt
cpufreq_dt.c revision 1.3.4.1
      1  1.3.4.1  christos /* $NetBSD: cpufreq_dt.c,v 1.3.4.1 2019/06/10 22:07:07 christos Exp $ */
      2      1.1  jmcneill 
      3      1.1  jmcneill /*-
      4      1.1  jmcneill  * Copyright (c) 2015-2017 Jared McNeill <jmcneill (at) invisible.ca>
      5      1.1  jmcneill  * All rights reserved.
      6      1.1  jmcneill  *
      7      1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8      1.1  jmcneill  * modification, are permitted provided that the following conditions
      9      1.1  jmcneill  * are met:
     10      1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11      1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12      1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14      1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15      1.1  jmcneill  *
     16      1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17      1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18      1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19      1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20      1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21      1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22      1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23      1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24      1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25      1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26      1.1  jmcneill  * SUCH DAMAGE.
     27      1.1  jmcneill  */
     28      1.1  jmcneill 
     29      1.1  jmcneill #include <sys/cdefs.h>
     30  1.3.4.1  christos __KERNEL_RCSID(0, "$NetBSD: cpufreq_dt.c,v 1.3.4.1 2019/06/10 22:07:07 christos Exp $");
     31      1.1  jmcneill 
     32      1.1  jmcneill #include <sys/param.h>
     33      1.1  jmcneill #include <sys/systm.h>
     34      1.1  jmcneill #include <sys/device.h>
     35      1.1  jmcneill #include <sys/kmem.h>
     36      1.1  jmcneill #include <sys/bus.h>
     37      1.1  jmcneill #include <sys/atomic.h>
     38      1.1  jmcneill #include <sys/xcall.h>
     39      1.1  jmcneill #include <sys/sysctl.h>
     40  1.3.4.1  christos #include <sys/queue.h>
     41  1.3.4.1  christos #include <sys/once.h>
     42      1.1  jmcneill 
     43      1.1  jmcneill #include <dev/fdt/fdtvar.h>
     44      1.1  jmcneill 
     45  1.3.4.1  christos struct cpufreq_dt_table {
     46  1.3.4.1  christos 	int			phandle;
     47  1.3.4.1  christos 	TAILQ_ENTRY(cpufreq_dt_table) next;
     48  1.3.4.1  christos };
     49  1.3.4.1  christos 
     50  1.3.4.1  christos static TAILQ_HEAD(, cpufreq_dt_table) cpufreq_dt_tables =
     51  1.3.4.1  christos     TAILQ_HEAD_INITIALIZER(cpufreq_dt_tables);
     52  1.3.4.1  christos static kmutex_t cpufreq_dt_tables_lock;
     53  1.3.4.1  christos 
     54      1.1  jmcneill struct cpufreq_dt_opp {
     55  1.3.4.1  christos 	u_int			freq_khz;
     56  1.3.4.1  christos 	u_int			voltage_uv;
     57  1.3.4.1  christos 	u_int			latency_ns;
     58      1.1  jmcneill };
     59      1.1  jmcneill 
     60      1.1  jmcneill struct cpufreq_dt_softc {
     61      1.1  jmcneill 	device_t		sc_dev;
     62      1.1  jmcneill 	int			sc_phandle;
     63      1.1  jmcneill 	struct clk		*sc_clk;
     64      1.1  jmcneill 	struct fdtbus_regulator	*sc_supply;
     65      1.1  jmcneill 
     66      1.1  jmcneill 	struct cpufreq_dt_opp	*sc_opp;
     67      1.1  jmcneill 	ssize_t			sc_nopp;
     68      1.1  jmcneill 
     69      1.2  jmcneill 	u_int			sc_freq_target;
     70      1.2  jmcneill 	bool			sc_freq_throttle;
     71      1.2  jmcneill 
     72      1.1  jmcneill 	u_int			sc_busy;
     73      1.1  jmcneill 
     74      1.1  jmcneill 	char			*sc_freq_available;
     75      1.1  jmcneill 	int			sc_node_target;
     76      1.1  jmcneill 	int			sc_node_current;
     77      1.1  jmcneill 	int			sc_node_available;
     78  1.3.4.1  christos 
     79  1.3.4.1  christos 	struct cpufreq_dt_table	sc_table;
     80      1.1  jmcneill };
     81      1.1  jmcneill 
     82      1.1  jmcneill static void
     83      1.1  jmcneill cpufreq_dt_change_cb(void *arg1, void *arg2)
     84      1.1  jmcneill {
     85      1.1  jmcneill #if notyet
     86      1.1  jmcneill 	struct cpu_info *ci = curcpu();
     87      1.1  jmcneill 	ci->ci_data.cpu_cc_freq = cpufreq_get_rate() * 1000000;
     88      1.1  jmcneill #endif
     89      1.1  jmcneill }
     90      1.1  jmcneill 
     91      1.1  jmcneill static int
     92      1.1  jmcneill cpufreq_dt_set_rate(struct cpufreq_dt_softc *sc, u_int freq_khz)
     93      1.1  jmcneill {
     94      1.1  jmcneill 	struct cpufreq_dt_opp *opp = NULL;
     95      1.1  jmcneill 	u_int old_rate, new_rate, old_uv, new_uv;
     96      1.2  jmcneill 	uint64_t xc;
     97      1.1  jmcneill 	int error;
     98      1.1  jmcneill 	ssize_t n;
     99      1.1  jmcneill 
    100      1.1  jmcneill 	for (n = 0; n < sc->sc_nopp; n++)
    101      1.1  jmcneill 		if (sc->sc_opp[n].freq_khz == freq_khz) {
    102      1.1  jmcneill 			opp = &sc->sc_opp[n];
    103      1.1  jmcneill 			break;
    104      1.1  jmcneill 		}
    105      1.1  jmcneill 	if (opp == NULL)
    106      1.1  jmcneill 		return EINVAL;
    107      1.1  jmcneill 
    108      1.1  jmcneill 	old_rate = clk_get_rate(sc->sc_clk);
    109      1.1  jmcneill 	new_rate = freq_khz * 1000;
    110      1.3  jmcneill 	new_uv = opp->voltage_uv;
    111      1.1  jmcneill 
    112      1.1  jmcneill 	if (old_rate == new_rate)
    113      1.1  jmcneill 		return 0;
    114      1.1  jmcneill 
    115      1.3  jmcneill 	if (sc->sc_supply != NULL) {
    116      1.3  jmcneill 		error = fdtbus_regulator_get_voltage(sc->sc_supply, &old_uv);
    117      1.1  jmcneill 		if (error != 0)
    118      1.1  jmcneill 			return error;
    119      1.3  jmcneill 
    120      1.3  jmcneill 		if (new_uv > old_uv) {
    121      1.3  jmcneill 			error = fdtbus_regulator_set_voltage(sc->sc_supply,
    122      1.3  jmcneill 			    new_uv, new_uv);
    123      1.3  jmcneill 			if (error != 0)
    124      1.3  jmcneill 				return error;
    125      1.3  jmcneill 		}
    126      1.1  jmcneill 	}
    127      1.1  jmcneill 
    128      1.1  jmcneill 	error = clk_set_rate(sc->sc_clk, new_rate);
    129      1.1  jmcneill 	if (error != 0)
    130      1.1  jmcneill 		return error;
    131      1.1  jmcneill 
    132  1.3.4.1  christos 	const u_int latency_us = howmany(opp->latency_ns, 1000);
    133  1.3.4.1  christos 	if (latency_us > 0)
    134  1.3.4.1  christos 		delay(latency_us);
    135  1.3.4.1  christos 
    136      1.3  jmcneill 	if (sc->sc_supply != NULL) {
    137      1.3  jmcneill 		if (new_uv < old_uv) {
    138      1.3  jmcneill 			error = fdtbus_regulator_set_voltage(sc->sc_supply,
    139      1.3  jmcneill 			    new_uv, new_uv);
    140      1.3  jmcneill 			if (error != 0)
    141      1.3  jmcneill 				return error;
    142      1.3  jmcneill 		}
    143      1.1  jmcneill 	}
    144      1.1  jmcneill 
    145      1.2  jmcneill 	if (error == 0) {
    146      1.2  jmcneill 		xc = xc_broadcast(0, cpufreq_dt_change_cb, sc, NULL);
    147      1.2  jmcneill 		xc_wait(xc);
    148      1.2  jmcneill 
    149      1.2  jmcneill 		pmf_event_inject(NULL, PMFE_SPEED_CHANGED);
    150      1.2  jmcneill 	}
    151      1.2  jmcneill 
    152      1.1  jmcneill 	return 0;
    153      1.1  jmcneill }
    154      1.1  jmcneill 
    155      1.2  jmcneill static void
    156      1.2  jmcneill cpufreq_dt_throttle_enable(device_t dev)
    157      1.2  jmcneill {
    158      1.2  jmcneill 	struct cpufreq_dt_softc * const sc = device_private(dev);
    159      1.2  jmcneill 
    160      1.2  jmcneill 	if (sc->sc_freq_throttle)
    161      1.2  jmcneill 		return;
    162      1.2  jmcneill 
    163      1.2  jmcneill 	const u_int freq_khz = sc->sc_opp[sc->sc_nopp - 1].freq_khz;
    164      1.2  jmcneill 
    165      1.2  jmcneill 	while (atomic_cas_uint(&sc->sc_busy, 0, 1) != 0)
    166      1.2  jmcneill 		kpause("throttle", false, 1, NULL);
    167      1.2  jmcneill 
    168      1.2  jmcneill 	if (cpufreq_dt_set_rate(sc, freq_khz) == 0) {
    169      1.2  jmcneill 		aprint_debug_dev(sc->sc_dev, "throttle enabled (%u.%03u MHz)\n",
    170      1.2  jmcneill 		    freq_khz / 1000, freq_khz % 1000);
    171      1.2  jmcneill 		sc->sc_freq_throttle = true;
    172      1.2  jmcneill 		if (sc->sc_freq_target == 0)
    173      1.2  jmcneill 			sc->sc_freq_target = clk_get_rate(sc->sc_clk) / 1000000;
    174      1.2  jmcneill 	}
    175      1.2  jmcneill 
    176      1.2  jmcneill 	atomic_dec_uint(&sc->sc_busy);
    177      1.2  jmcneill }
    178      1.2  jmcneill 
    179      1.2  jmcneill static void
    180      1.2  jmcneill cpufreq_dt_throttle_disable(device_t dev)
    181      1.2  jmcneill {
    182      1.2  jmcneill 	struct cpufreq_dt_softc * const sc = device_private(dev);
    183      1.2  jmcneill 
    184      1.2  jmcneill 	if (!sc->sc_freq_throttle)
    185      1.2  jmcneill 		return;
    186      1.2  jmcneill 
    187      1.2  jmcneill 	while (atomic_cas_uint(&sc->sc_busy, 0, 1) != 0)
    188      1.2  jmcneill 		kpause("throttle", false, 1, NULL);
    189      1.2  jmcneill 
    190      1.2  jmcneill 	const u_int freq_khz = sc->sc_freq_target * 1000;
    191      1.2  jmcneill 
    192      1.2  jmcneill 	if (cpufreq_dt_set_rate(sc, freq_khz) == 0) {
    193      1.2  jmcneill 		aprint_debug_dev(sc->sc_dev, "throttle disabled (%u.%03u MHz)\n",
    194      1.2  jmcneill 		    freq_khz / 1000, freq_khz % 1000);
    195      1.2  jmcneill 		sc->sc_freq_throttle = false;
    196      1.2  jmcneill 	}
    197      1.2  jmcneill 
    198      1.2  jmcneill 	atomic_dec_uint(&sc->sc_busy);
    199      1.2  jmcneill }
    200      1.2  jmcneill 
    201      1.1  jmcneill static int
    202      1.1  jmcneill cpufreq_dt_sysctl_helper(SYSCTLFN_ARGS)
    203      1.1  jmcneill {
    204      1.1  jmcneill 	struct cpufreq_dt_softc * const sc = rnode->sysctl_data;
    205      1.1  jmcneill 	struct sysctlnode node;
    206      1.1  jmcneill 	u_int fq, oldfq = 0;
    207      1.2  jmcneill 	int error, n;
    208      1.1  jmcneill 
    209      1.1  jmcneill 	node = *rnode;
    210      1.1  jmcneill 	node.sysctl_data = &fq;
    211      1.1  jmcneill 
    212      1.2  jmcneill 	if (rnode->sysctl_num == sc->sc_node_target) {
    213      1.2  jmcneill 		if (sc->sc_freq_target == 0)
    214      1.2  jmcneill 			sc->sc_freq_target = clk_get_rate(sc->sc_clk) / 1000000;
    215      1.2  jmcneill 		fq = sc->sc_freq_target;
    216      1.2  jmcneill 	} else
    217      1.2  jmcneill 		fq = clk_get_rate(sc->sc_clk) / 1000000;
    218      1.2  jmcneill 
    219      1.1  jmcneill 	if (rnode->sysctl_num == sc->sc_node_target)
    220      1.1  jmcneill 		oldfq = fq;
    221      1.1  jmcneill 
    222      1.2  jmcneill 	if (sc->sc_freq_target == 0)
    223      1.2  jmcneill 		sc->sc_freq_target = fq;
    224      1.2  jmcneill 
    225      1.1  jmcneill 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    226      1.1  jmcneill 	if (error || newp == NULL)
    227      1.1  jmcneill 		return error;
    228      1.1  jmcneill 
    229      1.1  jmcneill 	if (fq == oldfq || rnode->sysctl_num != sc->sc_node_target)
    230      1.1  jmcneill 		return 0;
    231      1.1  jmcneill 
    232      1.2  jmcneill 	for (n = 0; n < sc->sc_nopp; n++)
    233      1.2  jmcneill 		if (sc->sc_opp[n].freq_khz / 1000 == fq)
    234      1.2  jmcneill 			break;
    235      1.2  jmcneill 	if (n == sc->sc_nopp)
    236      1.2  jmcneill 		return EINVAL;
    237      1.2  jmcneill 
    238      1.1  jmcneill 	if (atomic_cas_uint(&sc->sc_busy, 0, 1) != 0)
    239      1.1  jmcneill 		return EBUSY;
    240      1.1  jmcneill 
    241      1.2  jmcneill 	sc->sc_freq_target = fq;
    242      1.1  jmcneill 
    243      1.2  jmcneill 	if (sc->sc_freq_throttle)
    244      1.2  jmcneill 		error = 0;
    245      1.2  jmcneill 	else
    246      1.2  jmcneill 		error = cpufreq_dt_set_rate(sc, fq * 1000);
    247      1.1  jmcneill 
    248      1.1  jmcneill 	atomic_dec_uint(&sc->sc_busy);
    249      1.1  jmcneill 
    250      1.1  jmcneill 	return error;
    251      1.1  jmcneill }
    252      1.1  jmcneill 
    253      1.1  jmcneill static void
    254      1.1  jmcneill cpufreq_dt_init_sysctl(struct cpufreq_dt_softc *sc)
    255      1.1  jmcneill {
    256      1.1  jmcneill 	const struct sysctlnode *node, *cpunode, *freqnode;
    257      1.1  jmcneill 	struct sysctllog *cpufreq_log = NULL;
    258  1.3.4.1  christos 	const char *cpunodename;
    259      1.1  jmcneill 	int error, i;
    260      1.1  jmcneill 
    261      1.1  jmcneill 	sc->sc_freq_available = kmem_zalloc(strlen("XXXX ") * sc->sc_nopp, KM_SLEEP);
    262      1.1  jmcneill 	for (i = 0; i < sc->sc_nopp; i++) {
    263      1.1  jmcneill 		char buf[6];
    264      1.1  jmcneill 		snprintf(buf, sizeof(buf), i ? " %u" : "%u", sc->sc_opp[i].freq_khz / 1000);
    265      1.1  jmcneill 		strcat(sc->sc_freq_available, buf);
    266      1.1  jmcneill 	}
    267      1.1  jmcneill 
    268  1.3.4.1  christos 	if (device_unit(sc->sc_dev) == 0)
    269  1.3.4.1  christos 		cpunodename = "cpu";
    270  1.3.4.1  christos 	else
    271  1.3.4.1  christos 		cpunodename = device_xname(sc->sc_dev);
    272  1.3.4.1  christos 
    273      1.1  jmcneill 	error = sysctl_createv(&cpufreq_log, 0, NULL, &node,
    274      1.1  jmcneill 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
    275      1.1  jmcneill 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
    276      1.1  jmcneill 	if (error)
    277      1.1  jmcneill 		goto sysctl_failed;
    278      1.1  jmcneill 	error = sysctl_createv(&cpufreq_log, 0, &node, &cpunode,
    279  1.3.4.1  christos 	    0, CTLTYPE_NODE, cpunodename, NULL,
    280      1.1  jmcneill 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    281      1.1  jmcneill 	if (error)
    282      1.1  jmcneill 		goto sysctl_failed;
    283      1.1  jmcneill 	error = sysctl_createv(&cpufreq_log, 0, &cpunode, &freqnode,
    284      1.1  jmcneill 	    0, CTLTYPE_NODE, "frequency", NULL,
    285      1.1  jmcneill 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    286      1.1  jmcneill 	if (error)
    287      1.1  jmcneill 		goto sysctl_failed;
    288      1.1  jmcneill 
    289      1.1  jmcneill 	error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node,
    290      1.1  jmcneill 	    CTLFLAG_READWRITE, CTLTYPE_INT, "target", NULL,
    291      1.1  jmcneill 	    cpufreq_dt_sysctl_helper, 0, (void *)sc, 0,
    292      1.1  jmcneill 	    CTL_CREATE, CTL_EOL);
    293      1.1  jmcneill 	if (error)
    294      1.1  jmcneill 		goto sysctl_failed;
    295      1.1  jmcneill 	sc->sc_node_target = node->sysctl_num;
    296      1.1  jmcneill 
    297      1.1  jmcneill 	error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node,
    298      1.1  jmcneill 	    CTLFLAG_READWRITE, CTLTYPE_INT, "current", NULL,
    299      1.1  jmcneill 	    cpufreq_dt_sysctl_helper, 0, (void *)sc, 0,
    300      1.1  jmcneill 	    CTL_CREATE, CTL_EOL);
    301      1.1  jmcneill 	if (error)
    302      1.1  jmcneill 		goto sysctl_failed;
    303      1.1  jmcneill 	sc->sc_node_current = node->sysctl_num;
    304      1.1  jmcneill 
    305      1.1  jmcneill 	error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node,
    306      1.1  jmcneill 	    0, CTLTYPE_STRING, "available", NULL,
    307      1.1  jmcneill 	    NULL, 0, sc->sc_freq_available, 0,
    308      1.1  jmcneill 	    CTL_CREATE, CTL_EOL);
    309      1.1  jmcneill 	if (error)
    310      1.1  jmcneill 		goto sysctl_failed;
    311      1.1  jmcneill 	sc->sc_node_available = node->sysctl_num;
    312      1.1  jmcneill 
    313      1.1  jmcneill 	return;
    314      1.1  jmcneill 
    315      1.1  jmcneill sysctl_failed:
    316      1.1  jmcneill 	aprint_error_dev(sc->sc_dev, "couldn't create sysctl nodes: %d\n", error);
    317      1.1  jmcneill 	sysctl_teardown(&cpufreq_log);
    318      1.1  jmcneill }
    319      1.1  jmcneill 
    320      1.1  jmcneill static int
    321  1.3.4.1  christos cpufreq_dt_parse_opp(struct cpufreq_dt_softc *sc)
    322      1.1  jmcneill {
    323      1.1  jmcneill 	const int phandle = sc->sc_phandle;
    324      1.1  jmcneill 	const u_int *opp;
    325      1.1  jmcneill 	int len, i;
    326  1.3.4.1  christos 
    327  1.3.4.1  christos 	opp = fdtbus_get_prop(phandle, "operating-points", &len);
    328  1.3.4.1  christos 	if (len < 8)
    329  1.3.4.1  christos 		return ENXIO;
    330  1.3.4.1  christos 
    331  1.3.4.1  christos 	sc->sc_nopp = len / 8;
    332  1.3.4.1  christos 	sc->sc_opp = kmem_zalloc(sizeof(*sc->sc_opp) * sc->sc_nopp, KM_SLEEP);
    333  1.3.4.1  christos 	for (i = 0; i < sc->sc_nopp; i++, opp += 2) {
    334  1.3.4.1  christos 		sc->sc_opp[i].freq_khz = be32toh(opp[0]);
    335  1.3.4.1  christos 		sc->sc_opp[i].voltage_uv = be32toh(opp[1]);
    336  1.3.4.1  christos 	}
    337  1.3.4.1  christos 
    338  1.3.4.1  christos 	return 0;
    339  1.3.4.1  christos }
    340  1.3.4.1  christos 
    341  1.3.4.1  christos static int
    342  1.3.4.1  christos cpufreq_dt_parse_opp_v2(struct cpufreq_dt_softc *sc)
    343  1.3.4.1  christos {
    344  1.3.4.1  christos 	const int phandle = sc->sc_phandle;
    345  1.3.4.1  christos 	struct cpufreq_dt_table *table;
    346  1.3.4.1  christos 	const u_int *opp_uv;
    347  1.3.4.1  christos 	uint64_t opp_hz;
    348  1.3.4.1  christos 	int opp_node, len, i;
    349  1.3.4.1  christos 
    350  1.3.4.1  christos 	const int opp_table = fdtbus_get_phandle(phandle, "operating-points-v2");
    351  1.3.4.1  christos 	if (opp_table < 0)
    352  1.3.4.1  christos 		return ENOENT;
    353  1.3.4.1  christos 
    354  1.3.4.1  christos 	/* If the table is shared, only setup a single instance */
    355  1.3.4.1  christos 	if (of_hasprop(opp_table, "opp-shared")) {
    356  1.3.4.1  christos 		TAILQ_FOREACH(table, &cpufreq_dt_tables, next)
    357  1.3.4.1  christos 			if (table->phandle == opp_table)
    358  1.3.4.1  christos 				return EEXIST;
    359  1.3.4.1  christos 		sc->sc_table.phandle = opp_table;
    360  1.3.4.1  christos 		TAILQ_INSERT_TAIL(&cpufreq_dt_tables, &sc->sc_table, next);
    361  1.3.4.1  christos 	}
    362  1.3.4.1  christos 
    363  1.3.4.1  christos 	for (opp_node = OF_child(opp_table); opp_node; opp_node = OF_peer(opp_node)) {
    364  1.3.4.1  christos 		if (fdtbus_status_okay(opp_node))
    365  1.3.4.1  christos 			sc->sc_nopp++;
    366  1.3.4.1  christos 	}
    367  1.3.4.1  christos 
    368  1.3.4.1  christos 	if (sc->sc_nopp == 0)
    369  1.3.4.1  christos 		return EINVAL;
    370  1.3.4.1  christos 
    371  1.3.4.1  christos 	sc->sc_opp = kmem_zalloc(sizeof(*sc->sc_opp) * sc->sc_nopp, KM_SLEEP);
    372  1.3.4.1  christos 	for (opp_node = OF_child(opp_table), i = 0; opp_node; opp_node = OF_peer(opp_node), i++) {
    373  1.3.4.1  christos 		if (!fdtbus_status_okay(opp_node))
    374  1.3.4.1  christos 			continue;
    375  1.3.4.1  christos 		if (of_getprop_uint64(opp_node, "opp-hz", &opp_hz) != 0)
    376  1.3.4.1  christos 			return EINVAL;
    377  1.3.4.1  christos 		opp_uv = fdtbus_get_prop(opp_node, "opp-microvolt", &len);
    378  1.3.4.1  christos 		if (opp_uv == NULL || len < 1)
    379  1.3.4.1  christos 			return EINVAL;
    380  1.3.4.1  christos 		/* Table is in reverse order */
    381  1.3.4.1  christos 		const int index = sc->sc_nopp - i - 1;
    382  1.3.4.1  christos 		sc->sc_opp[index].freq_khz = (u_int)(opp_hz / 1000);
    383  1.3.4.1  christos 		sc->sc_opp[index].voltage_uv = be32toh(opp_uv[0]);
    384  1.3.4.1  christos 		of_getprop_uint32(opp_node, "clock-latency-ns", &sc->sc_opp[index].latency_ns);
    385  1.3.4.1  christos 	}
    386  1.3.4.1  christos 
    387  1.3.4.1  christos 	return 0;
    388  1.3.4.1  christos }
    389  1.3.4.1  christos 
    390  1.3.4.1  christos static int
    391  1.3.4.1  christos cpufreq_dt_parse(struct cpufreq_dt_softc *sc)
    392  1.3.4.1  christos {
    393  1.3.4.1  christos 	const int phandle = sc->sc_phandle;
    394  1.3.4.1  christos 	int error, i;
    395      1.1  jmcneill 
    396      1.3  jmcneill 	if (of_hasprop(phandle, "cpu-supply")) {
    397      1.3  jmcneill 		sc->sc_supply = fdtbus_regulator_acquire(phandle, "cpu-supply");
    398      1.3  jmcneill 		if (sc->sc_supply == NULL) {
    399      1.3  jmcneill 			aprint_error_dev(sc->sc_dev,
    400      1.3  jmcneill 			    "couldn't acquire cpu-supply\n");
    401      1.3  jmcneill 			return ENXIO;
    402      1.3  jmcneill 		}
    403      1.1  jmcneill 	}
    404      1.1  jmcneill 	sc->sc_clk = fdtbus_clock_get_index(phandle, 0);
    405      1.1  jmcneill 	if (sc->sc_clk == NULL) {
    406      1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "couldn't acquire clock\n");
    407      1.1  jmcneill 		return ENXIO;
    408      1.1  jmcneill 	}
    409      1.1  jmcneill 
    410  1.3.4.1  christos 	mutex_enter(&cpufreq_dt_tables_lock);
    411  1.3.4.1  christos 	if (of_hasprop(phandle, "operating-points"))
    412  1.3.4.1  christos 		error = cpufreq_dt_parse_opp(sc);
    413  1.3.4.1  christos 	else if (of_hasprop(phandle, "operating-points-v2"))
    414  1.3.4.1  christos 		error = cpufreq_dt_parse_opp_v2(sc);
    415  1.3.4.1  christos 	else
    416  1.3.4.1  christos 		error = EINVAL;
    417  1.3.4.1  christos 	mutex_exit(&cpufreq_dt_tables_lock);
    418      1.1  jmcneill 
    419  1.3.4.1  christos 	if (error) {
    420  1.3.4.1  christos 		if (error != EEXIST)
    421  1.3.4.1  christos 			aprint_error_dev(sc->sc_dev,
    422  1.3.4.1  christos 			    "couldn't parse operating points: %d\n", error);
    423  1.3.4.1  christos 		return error;
    424  1.3.4.1  christos 	}
    425      1.1  jmcneill 
    426  1.3.4.1  christos 	for (i = 0; i < sc->sc_nopp; i++) {
    427      1.1  jmcneill 		aprint_verbose_dev(sc->sc_dev, "%u.%03u MHz, %u uV\n",
    428      1.1  jmcneill 		    sc->sc_opp[i].freq_khz / 1000,
    429      1.1  jmcneill 		    sc->sc_opp[i].freq_khz % 1000,
    430      1.1  jmcneill 		    sc->sc_opp[i].voltage_uv);
    431      1.1  jmcneill 	}
    432      1.1  jmcneill 
    433      1.1  jmcneill 	return 0;
    434      1.1  jmcneill }
    435      1.1  jmcneill 
    436      1.1  jmcneill static int
    437      1.1  jmcneill cpufreq_dt_match(device_t parent, cfdata_t cf, void *aux)
    438      1.1  jmcneill {
    439      1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    440      1.1  jmcneill 	const int phandle = faa->faa_phandle;
    441      1.1  jmcneill 	bus_addr_t addr;
    442      1.1  jmcneill 
    443      1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, NULL) != 0)
    444      1.1  jmcneill 		return 0;
    445  1.3.4.1  christos 
    446  1.3.4.1  christos 	if (!of_hasprop(phandle, "clocks"))
    447      1.1  jmcneill 		return 0;
    448      1.1  jmcneill 
    449  1.3.4.1  christos 	if (!of_hasprop(phandle, "operating-points") &&
    450  1.3.4.1  christos 	    !of_hasprop(phandle, "operating-points-v2"))
    451      1.1  jmcneill 		return 0;
    452      1.1  jmcneill 
    453      1.1  jmcneill 	return 1;
    454      1.1  jmcneill }
    455      1.1  jmcneill 
    456      1.1  jmcneill static void
    457      1.1  jmcneill cpufreq_dt_init(device_t self)
    458      1.1  jmcneill {
    459      1.1  jmcneill 	struct cpufreq_dt_softc * const sc = device_private(self);
    460      1.1  jmcneill 	int error;
    461      1.1  jmcneill 
    462      1.1  jmcneill 	if ((error = cpufreq_dt_parse(sc)) != 0)
    463      1.1  jmcneill 		return;
    464      1.1  jmcneill 
    465  1.3.4.1  christos 	pmf_event_register(sc->sc_dev, PMFE_THROTTLE_ENABLE, cpufreq_dt_throttle_enable, true);
    466  1.3.4.1  christos 	pmf_event_register(sc->sc_dev, PMFE_THROTTLE_DISABLE, cpufreq_dt_throttle_disable, true);
    467  1.3.4.1  christos 
    468      1.1  jmcneill 	cpufreq_dt_init_sysctl(sc);
    469      1.1  jmcneill }
    470      1.1  jmcneill 
    471  1.3.4.1  christos static int
    472  1.3.4.1  christos cpufreq_dt_lock_init(void)
    473  1.3.4.1  christos {
    474  1.3.4.1  christos 	mutex_init(&cpufreq_dt_tables_lock, MUTEX_DEFAULT, IPL_NONE);
    475  1.3.4.1  christos 	return 0;
    476  1.3.4.1  christos }
    477  1.3.4.1  christos 
    478      1.1  jmcneill static void
    479      1.1  jmcneill cpufreq_dt_attach(device_t parent, device_t self, void *aux)
    480      1.1  jmcneill {
    481  1.3.4.1  christos 	static ONCE_DECL(locks);
    482      1.1  jmcneill 	struct cpufreq_dt_softc * const sc = device_private(self);
    483      1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    484      1.1  jmcneill 
    485  1.3.4.1  christos 	RUN_ONCE(&locks, cpufreq_dt_lock_init);
    486  1.3.4.1  christos 
    487      1.1  jmcneill 	sc->sc_dev = self;
    488      1.1  jmcneill 	sc->sc_phandle = faa->faa_phandle;
    489      1.1  jmcneill 
    490      1.1  jmcneill 	aprint_naive("\n");
    491      1.1  jmcneill 	aprint_normal("\n");
    492      1.1  jmcneill 
    493      1.1  jmcneill 	config_interrupts(self, cpufreq_dt_init);
    494      1.1  jmcneill }
    495      1.1  jmcneill 
    496      1.1  jmcneill CFATTACH_DECL_NEW(cpufreq_dt, sizeof(struct cpufreq_dt_softc),
    497      1.1  jmcneill     cpufreq_dt_match, cpufreq_dt_attach, NULL, NULL);
    498