summaryrefslogtreecommitdiff
path: root/firmware
diff options
context:
space:
mode:
authorUdo Schläpfer <rockbox-2014.10@desktopwarrior.net>2015-01-21 20:35:03 +0100
committerGerrit Rockbox <gerrit@rockbox.org>2015-01-30 20:15:58 +0100
commita312ca1c5002f70c7e8cf9fcb802bbf7689dfca2 (patch)
treef31f5f61c90859d4283903d2c06faa9032b9681a /firmware
parent6d3dc8fce0401da24ad45593e4eb9a68e2cde297 (diff)
downloadrockbox-a312ca1c5002f70c7e8cf9fcb802bbf7689dfca2.tar.gz
rockbox-a312ca1c5002f70c7e8cf9fcb802bbf7689dfca2.zip
CPUFreq scaling governor interface for Linux/Android hosted devices.
For a usage example see http://gerrit.rockbox.org/r/#/c/1074/ Change-Id: I1d61e0eba6552a9b5d6e15a2e3169435b2f7079d
Diffstat (limited to 'firmware')
-rw-r--r--firmware/SOURCES1
-rw-r--r--firmware/target/hosted/cpufreq-linux.c151
-rw-r--r--firmware/target/hosted/cpufreq-linux.h58
3 files changed, 210 insertions, 0 deletions
diff --git a/firmware/SOURCES b/firmware/SOURCES
index cf8a59bd00..999d92012c 100644
--- a/firmware/SOURCES
+++ b/firmware/SOURCES
@@ -12,6 +12,7 @@ powermgmt.c
12 12
13#ifdef __linux__ 13#ifdef __linux__
14target/hosted/cpuinfo-linux.c 14target/hosted/cpuinfo-linux.c
15target/hosted/cpufreq-linux.c
15#endif 16#endif
16 17
17#if !defined(SAMSUNG_YPR0) || defined(SIMULATOR) /* uses as3514 rtc */ 18#if !defined(SAMSUNG_YPR0) || defined(SIMULATOR) /* uses as3514 rtc */
diff --git a/firmware/target/hosted/cpufreq-linux.c b/firmware/target/hosted/cpufreq-linux.c
new file mode 100644
index 0000000000..55a0bf3292
--- /dev/null
+++ b/firmware/target/hosted/cpufreq-linux.c
@@ -0,0 +1,151 @@
1/***************************************************************************
2 * __________ __ ___
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 *
9 * Copyright (C) 2015 by Udo Schläpfer
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
18 *
19 ****************************************************************************/
20
21
22#include <stdio.h>
23#include <string.h>
24
25#include "config.h"
26#include "cpuinfo-linux.h"
27#include "debug.h"
28
29#include "cpufreq-linux.h"
30
31
32static FILE* open_read(const char* file_name)
33{
34 FILE *f = fopen(file_name, "r");
35 if(f == NULL)
36 {
37 DEBUGF("ERROR %s: Can not open %s for reading.", __func__, file_name);
38 }
39
40 return f;
41}
42
43
44void cpufreq_available_governors(char* governors, int governors_size, int cpu)
45{
46 if(governors_size <= 0)
47 {
48 DEBUGF("ERROR %s: Invalid governors_size: %d.", __func__, governors_size);
49 return;
50 }
51
52 if(cpu < 0)
53 {
54 DEBUGF("ERROR %s: Invalid cpu: %d.", __func__, cpu);
55 return;
56 }
57
58 char available_governors_interface[70];
59
60 snprintf(available_governors_interface,
61 sizeof(available_governors_interface),
62 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_available_governors",
63 cpu);
64
65 FILE *f = open_read(available_governors_interface);
66 if(f == NULL)
67 {
68 return;
69 }
70
71 if(fgets(governors, governors_size, f) == NULL)
72 {
73 DEBUGF("ERROR %s: Read failed for %s.", __func__, available_governors_interface);
74 fclose(f);
75 return;
76 }
77
78 DEBUGF("DEBUG %s: Available governors for cpu %d: %s.", __func__, cpu, governors);
79
80 fclose(f);
81}
82
83
84static FILE* open_write(const char* file_name)
85{
86 FILE *f = fopen(file_name, "w");
87 if(f == NULL)
88 {
89 DEBUGF("ERROR %s: Can not open %s for writing.", __func__, file_name);
90 }
91
92 return f;
93}
94
95
96static void set_governor_for_cpu(const char* governor, int cpu)
97{
98 DEBUGF("DEBUG %s: governor: %s, cpu: %d.", __func__, governor, cpu);
99
100 char scaling_governor_interface[64];
101
102 snprintf(scaling_governor_interface,
103 sizeof(scaling_governor_interface),
104 "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
105 cpu);
106
107 FILE *f = open_write(scaling_governor_interface);
108 if(f == NULL)
109 {
110 return;
111 }
112
113 if(fprintf(f, "%s", governor) < 1)
114 {
115 DEBUGF("ERROR %s: Write failed for %s.", __func__, scaling_governor_interface);
116 }
117
118 fclose(f);
119}
120
121
122void cpufreq_set_governor(const char* governor, int cpu)
123{
124 if(governor == NULL)
125 {
126 DEBUGF("ERROR %s: Invalid governor.", __func__);
127 return;
128 }
129
130 if(cpu < CPUFREQ_ALL_CPUS)
131 {
132 DEBUGF("ERROR %s: Invalid cpu: %d.", __func__, cpu);
133 return;
134 }
135
136 DEBUGF("DEBUG %s: governor: %s, cpu: %d.", __func__, governor, cpu);
137
138 if(cpu == CPUFREQ_ALL_CPUS)
139 {
140 int max_cpu = cpucount_linux();
141
142 for(int count = 0; count < max_cpu; ++count)
143 {
144 set_governor_for_cpu(governor, count);
145 }
146 }
147 else
148 {
149 set_governor_for_cpu(governor, cpu);
150 }
151}
diff --git a/firmware/target/hosted/cpufreq-linux.h b/firmware/target/hosted/cpufreq-linux.h
new file mode 100644
index 0000000000..f62e0225dd
--- /dev/null
+++ b/firmware/target/hosted/cpufreq-linux.h
@@ -0,0 +1,58 @@
1/***************************************************************************
2 * __________ __ ___
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 *
9 * Copyright (C) 2015 by Udo Schläpfer
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
18 *
19 *
20 * Interface for the Linux CPUFreq kernel modul.
21 * See https://www.kernel.org/doc/Documentation/cpu-freq/
22 *
23 ****************************************************************************/
24
25
26#ifndef __CPUFREQ_LINUX_H__
27#define __CPUFREQ_LINUX_H__
28
29
30#include <stdbool.h>
31
32
33/*
34 Get the available governors for cpu.
35 governors Preallocated string for the available governors. On succes this will look like
36 "performance ondemand powersave", each word beeing a governor that can be used with
37 cpufreq_set_governor.
38 governors_size Size of governors.
39 cpu The cpu, starting with 0, for which to get the available governors. CPUFREQ_ALL_CPUS is not
40 supported, you must supply a cpu. See cpucount_linux in cpuinfo-linux.h.
41*/
42void cpufreq_available_governors(char* governors, int governors_size, int cpu);
43
44
45/*
46 Set the cpufreq governor for cpu.
47 governor The governor to set. This must be one of the available governors returned by
48 cpufreq_available_governors.
49 cpu The cpu, starting with 0, for which to set the governor. CPUFREQ_ALL_CPUS to set
50 the governor for all cpus. See cpucount_linux in cpuinfo-linux.h.
51*/
52void cpufreq_set_governor(const char* governor, int cpu);
53
54
55static const int CPUFREQ_ALL_CPUS = -1;
56
57
58#endif