Home | History | Annotate | Line # | Download | only in fdt
cpufreq_dt.c revision 1.2
      1 /* $NetBSD: cpufreq_dt.c,v 1.2 2017/10/05 01:28:01 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2015-2017 Jared McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: cpufreq_dt.c,v 1.2 2017/10/05 01:28:01 jmcneill Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 #include <sys/device.h>
     35 #include <sys/kmem.h>
     36 #include <sys/bus.h>
     37 #include <sys/atomic.h>
     38 #include <sys/xcall.h>
     39 #include <sys/sysctl.h>
     40 
     41 #include <dev/fdt/fdtvar.h>
     42 
     43 struct cpufreq_dt_opp {
     44 	u_int		freq_khz;
     45 	u_int		voltage_uv;
     46 };
     47 
     48 struct cpufreq_dt_softc {
     49 	device_t		sc_dev;
     50 	int			sc_phandle;
     51 	struct clk		*sc_clk;
     52 	struct fdtbus_regulator	*sc_supply;
     53 
     54 	struct cpufreq_dt_opp	*sc_opp;
     55 	ssize_t			sc_nopp;
     56 	int			sc_latency;
     57 
     58 	u_int			sc_freq_target;
     59 	bool			sc_freq_throttle;
     60 
     61 	u_int			sc_busy;
     62 
     63 	char			*sc_freq_available;
     64 	int			sc_node_target;
     65 	int			sc_node_current;
     66 	int			sc_node_available;
     67 };
     68 
     69 static void
     70 cpufreq_dt_change_cb(void *arg1, void *arg2)
     71 {
     72 #if notyet
     73 	struct cpu_info *ci = curcpu();
     74 	ci->ci_data.cpu_cc_freq = cpufreq_get_rate() * 1000000;
     75 #endif
     76 }
     77 
     78 static int
     79 cpufreq_dt_set_rate(struct cpufreq_dt_softc *sc, u_int freq_khz)
     80 {
     81 	struct cpufreq_dt_opp *opp = NULL;
     82 	u_int old_rate, new_rate, old_uv, new_uv;
     83 	uint64_t xc;
     84 	int error;
     85 	ssize_t n;
     86 
     87 	for (n = 0; n < sc->sc_nopp; n++)
     88 		if (sc->sc_opp[n].freq_khz == freq_khz) {
     89 			opp = &sc->sc_opp[n];
     90 			break;
     91 		}
     92 	if (opp == NULL)
     93 		return EINVAL;
     94 
     95 	old_rate = clk_get_rate(sc->sc_clk);
     96 	new_rate = freq_khz * 1000;
     97 
     98 	if (old_rate == new_rate)
     99 		return 0;
    100 
    101 	error = fdtbus_regulator_get_voltage(sc->sc_supply, &old_uv);
    102 	if (error != 0)
    103 		return error;
    104 	new_uv = opp->voltage_uv;
    105 
    106 	if (new_uv > old_uv) {
    107 		error = fdtbus_regulator_set_voltage(sc->sc_supply,
    108 		    new_uv, new_uv);
    109 		if (error != 0)
    110 			return error;
    111 	}
    112 
    113 	error = clk_set_rate(sc->sc_clk, new_rate);
    114 	if (error != 0)
    115 		return error;
    116 
    117 	if (new_uv < old_uv) {
    118 		error = fdtbus_regulator_set_voltage(sc->sc_supply,
    119 		    new_uv, new_uv);
    120 		if (error != 0)
    121 			return error;
    122 	}
    123 
    124 	if (error == 0) {
    125 		xc = xc_broadcast(0, cpufreq_dt_change_cb, sc, NULL);
    126 		xc_wait(xc);
    127 
    128 		pmf_event_inject(NULL, PMFE_SPEED_CHANGED);
    129 	}
    130 
    131 	return 0;
    132 }
    133 
    134 static void
    135 cpufreq_dt_throttle_enable(device_t dev)
    136 {
    137 	struct cpufreq_dt_softc * const sc = device_private(dev);
    138 
    139 	if (sc->sc_freq_throttle)
    140 		return;
    141 
    142 	const u_int freq_khz = sc->sc_opp[sc->sc_nopp - 1].freq_khz;
    143 
    144 	while (atomic_cas_uint(&sc->sc_busy, 0, 1) != 0)
    145 		kpause("throttle", false, 1, NULL);
    146 
    147 	if (cpufreq_dt_set_rate(sc, freq_khz) == 0) {
    148 		aprint_debug_dev(sc->sc_dev, "throttle enabled (%u.%03u MHz)\n",
    149 		    freq_khz / 1000, freq_khz % 1000);
    150 		sc->sc_freq_throttle = true;
    151 		if (sc->sc_freq_target == 0)
    152 			sc->sc_freq_target = clk_get_rate(sc->sc_clk) / 1000000;
    153 	}
    154 
    155 	atomic_dec_uint(&sc->sc_busy);
    156 }
    157 
    158 static void
    159 cpufreq_dt_throttle_disable(device_t dev)
    160 {
    161 	struct cpufreq_dt_softc * const sc = device_private(dev);
    162 
    163 	if (!sc->sc_freq_throttle)
    164 		return;
    165 
    166 	while (atomic_cas_uint(&sc->sc_busy, 0, 1) != 0)
    167 		kpause("throttle", false, 1, NULL);
    168 
    169 	const u_int freq_khz = sc->sc_freq_target * 1000;
    170 
    171 	if (cpufreq_dt_set_rate(sc, freq_khz) == 0) {
    172 		aprint_debug_dev(sc->sc_dev, "throttle disabled (%u.%03u MHz)\n",
    173 		    freq_khz / 1000, freq_khz % 1000);
    174 		sc->sc_freq_throttle = false;
    175 	}
    176 
    177 	atomic_dec_uint(&sc->sc_busy);
    178 }
    179 
    180 static int
    181 cpufreq_dt_sysctl_helper(SYSCTLFN_ARGS)
    182 {
    183 	struct cpufreq_dt_softc * const sc = rnode->sysctl_data;
    184 	struct sysctlnode node;
    185 	u_int fq, oldfq = 0;
    186 	int error, n;
    187 
    188 	node = *rnode;
    189 	node.sysctl_data = &fq;
    190 
    191 	if (rnode->sysctl_num == sc->sc_node_target) {
    192 		if (sc->sc_freq_target == 0)
    193 			sc->sc_freq_target = clk_get_rate(sc->sc_clk) / 1000000;
    194 		fq = sc->sc_freq_target;
    195 	} else
    196 		fq = clk_get_rate(sc->sc_clk) / 1000000;
    197 
    198 	if (rnode->sysctl_num == sc->sc_node_target)
    199 		oldfq = fq;
    200 
    201 	if (sc->sc_freq_target == 0)
    202 		sc->sc_freq_target = fq;
    203 
    204 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    205 	if (error || newp == NULL)
    206 		return error;
    207 
    208 	if (fq == oldfq || rnode->sysctl_num != sc->sc_node_target)
    209 		return 0;
    210 
    211 	for (n = 0; n < sc->sc_nopp; n++)
    212 		if (sc->sc_opp[n].freq_khz / 1000 == fq)
    213 			break;
    214 	if (n == sc->sc_nopp)
    215 		return EINVAL;
    216 
    217 	if (atomic_cas_uint(&sc->sc_busy, 0, 1) != 0)
    218 		return EBUSY;
    219 
    220 	sc->sc_freq_target = fq;
    221 
    222 	if (sc->sc_freq_throttle)
    223 		error = 0;
    224 	else
    225 		error = cpufreq_dt_set_rate(sc, fq * 1000);
    226 
    227 	atomic_dec_uint(&sc->sc_busy);
    228 
    229 	return error;
    230 }
    231 
    232 static void
    233 cpufreq_dt_init_sysctl(struct cpufreq_dt_softc *sc)
    234 {
    235 	const struct sysctlnode *node, *cpunode, *freqnode;
    236 	struct sysctllog *cpufreq_log = NULL;
    237 	int error, i;
    238 
    239 	sc->sc_freq_available = kmem_zalloc(strlen("XXXX ") * sc->sc_nopp, KM_SLEEP);
    240 	for (i = 0; i < sc->sc_nopp; i++) {
    241 		char buf[6];
    242 		snprintf(buf, sizeof(buf), i ? " %u" : "%u", sc->sc_opp[i].freq_khz / 1000);
    243 		strcat(sc->sc_freq_available, buf);
    244 	}
    245 
    246 	error = sysctl_createv(&cpufreq_log, 0, NULL, &node,
    247 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL,
    248 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL);
    249 	if (error)
    250 		goto sysctl_failed;
    251 	error = sysctl_createv(&cpufreq_log, 0, &node, &cpunode,
    252 	    0, CTLTYPE_NODE, "cpu", NULL,
    253 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    254 	if (error)
    255 		goto sysctl_failed;
    256 	error = sysctl_createv(&cpufreq_log, 0, &cpunode, &freqnode,
    257 	    0, CTLTYPE_NODE, "frequency", NULL,
    258 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
    259 	if (error)
    260 		goto sysctl_failed;
    261 
    262 	error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node,
    263 	    CTLFLAG_READWRITE, CTLTYPE_INT, "target", NULL,
    264 	    cpufreq_dt_sysctl_helper, 0, (void *)sc, 0,
    265 	    CTL_CREATE, CTL_EOL);
    266 	if (error)
    267 		goto sysctl_failed;
    268 	sc->sc_node_target = node->sysctl_num;
    269 
    270 	error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node,
    271 	    CTLFLAG_READWRITE, CTLTYPE_INT, "current", NULL,
    272 	    cpufreq_dt_sysctl_helper, 0, (void *)sc, 0,
    273 	    CTL_CREATE, CTL_EOL);
    274 	if (error)
    275 		goto sysctl_failed;
    276 	sc->sc_node_current = node->sysctl_num;
    277 
    278 	error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node,
    279 	    0, CTLTYPE_STRING, "available", NULL,
    280 	    NULL, 0, sc->sc_freq_available, 0,
    281 	    CTL_CREATE, CTL_EOL);
    282 	if (error)
    283 		goto sysctl_failed;
    284 	sc->sc_node_available = node->sysctl_num;
    285 
    286 	return;
    287 
    288 sysctl_failed:
    289 	aprint_error_dev(sc->sc_dev, "couldn't create sysctl nodes: %d\n", error);
    290 	sysctl_teardown(&cpufreq_log);
    291 }
    292 
    293 static int
    294 cpufreq_dt_parse(struct cpufreq_dt_softc *sc)
    295 {
    296 	const int phandle = sc->sc_phandle;
    297 	const u_int *opp;
    298 	int len, i;
    299 	u_int lat;
    300 
    301 	sc->sc_supply = fdtbus_regulator_acquire(phandle, "cpu-supply");
    302 	if (sc->sc_supply == NULL) {
    303 		aprint_error_dev(sc->sc_dev, "couldn't acquire cpu-supply\n");
    304 		return ENXIO;
    305 	}
    306 	sc->sc_clk = fdtbus_clock_get_index(phandle, 0);
    307 	if (sc->sc_clk == NULL) {
    308 		aprint_error_dev(sc->sc_dev, "couldn't acquire clock\n");
    309 		return ENXIO;
    310 	}
    311 
    312 	opp = fdtbus_get_prop(phandle, "operating-points", &len);
    313 	if (len < 8)
    314 		return ENXIO;
    315 
    316 	sc->sc_nopp = len / 8;
    317 	sc->sc_opp = kmem_zalloc(sizeof(*sc->sc_opp) * sc->sc_nopp, KM_SLEEP);
    318 	for (i = 0; i < sc->sc_nopp; i++, opp += 2) {
    319 		sc->sc_opp[i].freq_khz = be32toh(opp[0]);
    320 		sc->sc_opp[i].voltage_uv = be32toh(opp[1]);
    321 
    322 		aprint_verbose_dev(sc->sc_dev, "%u.%03u MHz, %u uV\n",
    323 		    sc->sc_opp[i].freq_khz / 1000,
    324 		    sc->sc_opp[i].freq_khz % 1000,
    325 		    sc->sc_opp[i].voltage_uv);
    326 	}
    327 
    328 	if (of_getprop_uint32(phandle, "clock-latency", &lat) == 0)
    329 		sc->sc_latency = lat;
    330 	else
    331 		sc->sc_latency = -1;
    332 
    333 	return 0;
    334 }
    335 
    336 static int
    337 cpufreq_dt_match(device_t parent, cfdata_t cf, void *aux)
    338 {
    339 	struct fdt_attach_args * const faa = aux;
    340 	const int phandle = faa->faa_phandle;
    341 	bus_addr_t addr;
    342 
    343 	if (fdtbus_get_reg(phandle, 0, &addr, NULL) != 0)
    344 		return 0;
    345 	/* Generic DT cpufreq driver properties must be defined under /cpus/cpu@0 */
    346 	if (addr != 0)
    347 		return 0;
    348 
    349 	if (!of_hasprop(phandle, "operating-points") ||
    350 	    !of_hasprop(phandle, "clocks") ||
    351 	    !of_hasprop(phandle, "cpu-supply"))
    352 		return 0;
    353 
    354 	return 1;
    355 }
    356 
    357 static void
    358 cpufreq_dt_init(device_t self)
    359 {
    360 	struct cpufreq_dt_softc * const sc = device_private(self);
    361 	int error;
    362 
    363 	if ((error = cpufreq_dt_parse(sc)) != 0)
    364 		return;
    365 
    366 	cpufreq_dt_init_sysctl(sc);
    367 }
    368 
    369 static void
    370 cpufreq_dt_attach(device_t parent, device_t self, void *aux)
    371 {
    372 	struct cpufreq_dt_softc * const sc = device_private(self);
    373 	struct fdt_attach_args * const faa = aux;
    374 
    375 	sc->sc_dev = self;
    376 	sc->sc_phandle = faa->faa_phandle;
    377 
    378 	aprint_naive("\n");
    379 	aprint_normal("\n");
    380 
    381 	pmf_event_register(self, PMFE_THROTTLE_ENABLE, cpufreq_dt_throttle_enable, true);
    382 	pmf_event_register(self, PMFE_THROTTLE_DISABLE, cpufreq_dt_throttle_disable, true);
    383 
    384 	config_interrupts(self, cpufreq_dt_init);
    385 }
    386 
    387 CFATTACH_DECL_NEW(cpufreq_dt, sizeof(struct cpufreq_dt_softc),
    388     cpufreq_dt_match, cpufreq_dt_attach, NULL, NULL);
    389