| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: cpufreq_dt.c,v 1.8 2019/05/21 22:15:26 jmcneill Exp $ */ | | 1 | /* $NetBSD: cpufreq_dt.c,v 1.9 2019/10/06 11:28:24 jmcneill Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2015-2017 Jared McNeill <jmcneill@invisible.ca> | | 4 | * Copyright (c) 2015-2017 Jared McNeill <jmcneill@invisible.ca> |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | | 14 | * documentation and/or other materials provided with the distribution. |
| @@ -17,38 +17,39 @@ | | | @@ -17,38 +17,39 @@ |
17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | | 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | | 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | | 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | | 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | | 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | | 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | | 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | | 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 | | 25 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
26 | * SUCH DAMAGE. | | 26 | * SUCH DAMAGE. |
27 | */ | | 27 | */ |
28 | | | 28 | |
29 | #include <sys/cdefs.h> | | 29 | #include <sys/cdefs.h> |
30 | __KERNEL_RCSID(0, "$NetBSD: cpufreq_dt.c,v 1.8 2019/05/21 22:15:26 jmcneill Exp $"); | | 30 | __KERNEL_RCSID(0, "$NetBSD: cpufreq_dt.c,v 1.9 2019/10/06 11:28:24 jmcneill Exp $"); |
31 | | | 31 | |
32 | #include <sys/param.h> | | 32 | #include <sys/param.h> |
33 | #include <sys/systm.h> | | 33 | #include <sys/systm.h> |
34 | #include <sys/device.h> | | 34 | #include <sys/device.h> |
35 | #include <sys/kmem.h> | | 35 | #include <sys/kmem.h> |
36 | #include <sys/bus.h> | | 36 | #include <sys/bus.h> |
37 | #include <sys/atomic.h> | | 37 | #include <sys/atomic.h> |
38 | #include <sys/xcall.h> | | 38 | #include <sys/xcall.h> |
39 | #include <sys/sysctl.h> | | 39 | #include <sys/sysctl.h> |
40 | #include <sys/queue.h> | | 40 | #include <sys/queue.h> |
41 | #include <sys/once.h> | | 41 | #include <sys/once.h> |
| | | 42 | #include <sys/cpu.h> |
42 | | | 43 | |
43 | #include <dev/fdt/fdtvar.h> | | 44 | #include <dev/fdt/fdtvar.h> |
44 | | | 45 | |
45 | struct cpufreq_dt_table { | | 46 | struct cpufreq_dt_table { |
46 | int phandle; | | 47 | int phandle; |
47 | TAILQ_ENTRY(cpufreq_dt_table) next; | | 48 | TAILQ_ENTRY(cpufreq_dt_table) next; |
48 | }; | | 49 | }; |
49 | | | 50 | |
50 | static TAILQ_HEAD(, cpufreq_dt_table) cpufreq_dt_tables = | | 51 | static TAILQ_HEAD(, cpufreq_dt_table) cpufreq_dt_tables = |
51 | TAILQ_HEAD_INITIALIZER(cpufreq_dt_tables); | | 52 | TAILQ_HEAD_INITIALIZER(cpufreq_dt_tables); |
52 | static kmutex_t cpufreq_dt_tables_lock; | | 53 | static kmutex_t cpufreq_dt_tables_lock; |
53 | | | 54 | |
54 | struct cpufreq_dt_opp { | | 55 | struct cpufreq_dt_opp { |
| @@ -240,79 +241,96 @@ cpufreq_dt_sysctl_helper(SYSCTLFN_ARGS) | | | @@ -240,79 +241,96 @@ cpufreq_dt_sysctl_helper(SYSCTLFN_ARGS) |
240 | | | 241 | |
241 | sc->sc_freq_target = fq; | | 242 | sc->sc_freq_target = fq; |
242 | | | 243 | |
243 | if (sc->sc_freq_throttle) | | 244 | if (sc->sc_freq_throttle) |
244 | error = 0; | | 245 | error = 0; |
245 | else | | 246 | else |
246 | error = cpufreq_dt_set_rate(sc, fq * 1000); | | 247 | error = cpufreq_dt_set_rate(sc, fq * 1000); |
247 | | | 248 | |
248 | atomic_dec_uint(&sc->sc_busy); | | 249 | atomic_dec_uint(&sc->sc_busy); |
249 | | | 250 | |
250 | return error; | | 251 | return error; |
251 | } | | 252 | } |
252 | | | 253 | |
| | | 254 | static struct cpu_info * |
| | | 255 | cpufreq_dt_cpu_lookup(cpuid_t mpidr) |
| | | 256 | { |
| | | 257 | CPU_INFO_ITERATOR cii; |
| | | 258 | struct cpu_info *ci; |
| | | 259 | |
| | | 260 | for (CPU_INFO_FOREACH(cii, ci)) { |
| | | 261 | if (ci->ci_cpuid == mpidr) |
| | | 262 | return ci; |
| | | 263 | } |
| | | 264 | |
| | | 265 | return NULL; |
| | | 266 | } |
| | | 267 | |
253 | static void | | 268 | static void |
254 | cpufreq_dt_init_sysctl(struct cpufreq_dt_softc *sc) | | 269 | cpufreq_dt_init_sysctl(struct cpufreq_dt_softc *sc) |
255 | { | | 270 | { |
256 | const struct sysctlnode *node, *cpunode, *freqnode; | | 271 | const struct sysctlnode *node, *cpunode; |
257 | struct sysctllog *cpufreq_log = NULL; | | 272 | struct sysctllog *cpufreq_log = NULL; |
258 | const char *cpunodename; | | 273 | struct cpu_info *ci; |
| | | 274 | uint64_t mpidr; |
259 | int error, i; | | 275 | int error, i; |
260 | | | 276 | |
| | | 277 | if (fdtbus_get_reg(sc->sc_phandle, 0, &mpidr, 0) != 0) |
| | | 278 | return; |
| | | 279 | |
| | | 280 | ci = cpufreq_dt_cpu_lookup(mpidr); |
| | | 281 | if (ci == NULL) |
| | | 282 | return; |
| | | 283 | |
261 | sc->sc_freq_available = kmem_zalloc(strlen("XXXX ") * sc->sc_nopp, KM_SLEEP); | | 284 | sc->sc_freq_available = kmem_zalloc(strlen("XXXX ") * sc->sc_nopp, KM_SLEEP); |
262 | for (i = 0; i < sc->sc_nopp; i++) { | | 285 | for (i = 0; i < sc->sc_nopp; i++) { |
263 | char buf[6]; | | 286 | char buf[6]; |
264 | snprintf(buf, sizeof(buf), i ? " %u" : "%u", sc->sc_opp[i].freq_khz / 1000); | | 287 | snprintf(buf, sizeof(buf), i ? " %u" : "%u", sc->sc_opp[i].freq_khz / 1000); |
265 | strcat(sc->sc_freq_available, buf); | | 288 | strcat(sc->sc_freq_available, buf); |
266 | } | | 289 | } |
267 | | | 290 | |
268 | if (device_unit(sc->sc_dev) == 0) | | | |
269 | cpunodename = "cpu"; | | | |
270 | else | | | |
271 | cpunodename = device_xname(sc->sc_dev); | | | |
272 | | | | |
273 | error = sysctl_createv(&cpufreq_log, 0, NULL, &node, | | 291 | error = sysctl_createv(&cpufreq_log, 0, NULL, &node, |
274 | CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL, | | 292 | CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL, |
275 | NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL); | | 293 | NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL); |
276 | if (error) | | 294 | if (error) |
277 | goto sysctl_failed; | | 295 | goto sysctl_failed; |
278 | error = sysctl_createv(&cpufreq_log, 0, &node, &cpunode, | | 296 | error = sysctl_createv(&cpufreq_log, 0, &node, &node, |
279 | 0, CTLTYPE_NODE, cpunodename, NULL, | | 297 | 0, CTLTYPE_NODE, "cpufreq", NULL, |
280 | NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL); | | 298 | NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL); |
281 | if (error) | | 299 | if (error) |
282 | goto sysctl_failed; | | 300 | goto sysctl_failed; |
283 | error = sysctl_createv(&cpufreq_log, 0, &cpunode, &freqnode, | | 301 | error = sysctl_createv(&cpufreq_log, 0, &node, &cpunode, |
284 | 0, CTLTYPE_NODE, "frequency", NULL, | | 302 | 0, CTLTYPE_NODE, cpu_name(ci), NULL, |
285 | NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL); | | 303 | NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL); |
286 | if (error) | | 304 | if (error) |
287 | goto sysctl_failed; | | 305 | goto sysctl_failed; |
288 | | | 306 | |
289 | error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node, | | 307 | error = sysctl_createv(&cpufreq_log, 0, &cpunode, &node, |
290 | CTLFLAG_READWRITE, CTLTYPE_INT, "target", NULL, | | 308 | CTLFLAG_READWRITE, CTLTYPE_INT, "target", NULL, |
291 | cpufreq_dt_sysctl_helper, 0, (void *)sc, 0, | | 309 | cpufreq_dt_sysctl_helper, 0, (void *)sc, 0, |
292 | CTL_CREATE, CTL_EOL); | | 310 | CTL_CREATE, CTL_EOL); |
293 | if (error) | | 311 | if (error) |
294 | goto sysctl_failed; | | 312 | goto sysctl_failed; |
295 | sc->sc_node_target = node->sysctl_num; | | 313 | sc->sc_node_target = node->sysctl_num; |
296 | | | 314 | |
297 | error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node, | | 315 | error = sysctl_createv(&cpufreq_log, 0, &cpunode, &node, |
298 | CTLFLAG_READWRITE, CTLTYPE_INT, "current", NULL, | | 316 | CTLFLAG_READWRITE, CTLTYPE_INT, "current", NULL, |
299 | cpufreq_dt_sysctl_helper, 0, (void *)sc, 0, | | 317 | cpufreq_dt_sysctl_helper, 0, (void *)sc, 0, |
300 | CTL_CREATE, CTL_EOL); | | 318 | CTL_CREATE, CTL_EOL); |
301 | if (error) | | 319 | if (error) |
302 | goto sysctl_failed; | | 320 | goto sysctl_failed; |
303 | sc->sc_node_current = node->sysctl_num; | | 321 | sc->sc_node_current = node->sysctl_num; |
304 | | | 322 | |
305 | error = sysctl_createv(&cpufreq_log, 0, &freqnode, &node, | | 323 | error = sysctl_createv(&cpufreq_log, 0, &cpunode, &node, |
306 | 0, CTLTYPE_STRING, "available", NULL, | | 324 | 0, CTLTYPE_STRING, "available", NULL, |
307 | NULL, 0, sc->sc_freq_available, 0, | | 325 | NULL, 0, sc->sc_freq_available, 0, |
308 | CTL_CREATE, CTL_EOL); | | 326 | CTL_CREATE, CTL_EOL); |
309 | if (error) | | 327 | if (error) |
310 | goto sysctl_failed; | | 328 | goto sysctl_failed; |
311 | sc->sc_node_available = node->sysctl_num; | | 329 | sc->sc_node_available = node->sysctl_num; |
312 | | | 330 | |
313 | return; | | 331 | return; |
314 | | | 332 | |
315 | sysctl_failed: | | 333 | sysctl_failed: |
316 | aprint_error_dev(sc->sc_dev, "couldn't create sysctl nodes: %d\n", error); | | 334 | aprint_error_dev(sc->sc_dev, "couldn't create sysctl nodes: %d\n", error); |
317 | sysctl_teardown(&cpufreq_log); | | 335 | sysctl_teardown(&cpufreq_log); |
318 | } | | 336 | } |