diff options
Diffstat (limited to 'firmware/target/hosted')
-rw-r--r-- | firmware/target/hosted/cpufreq-linux.c | 151 | ||||
-rw-r--r-- | firmware/target/hosted/cpufreq-linux.h | 58 |
2 files changed, 209 insertions, 0 deletions
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 | |||
32 | static 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 | |||
44 | void 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 | |||
84 | static 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 | |||
96 | static 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 | |||
122 | void 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 | */ | ||
42 | void 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 | */ | ||
52 | void cpufreq_set_governor(const char* governor, int cpu); | ||
53 | |||
54 | |||
55 | static const int CPUFREQ_ALL_CPUS = -1; | ||
56 | |||
57 | |||
58 | #endif | ||