2 * PASS (Power Aware System Service)
4 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 #include <pass/hal-log.h>
26 #include "../shared/sysfs.h"
29 #define HAL_VERSION MAKE_2B_CODE_4(VER_MAJOR,VER_MINOR,VER_REVISION,VER_RELEASE)
30 #define DEV_VERSION_CPU MAKE_2B_CODE_2(1,0)
32 #define CPUFREQ_PATH_PREFIX "/sys/devices/system/cpu/"
33 #define CPUFREQ_CURR_GOVERNOR_PATH_SUFFIX "/cpufreq/scaling_governor"
34 #define CPUFREQ_AVAIL_GOVERNOR_PATH_SUFFIX "/cpufreq/scaling_available_governors"
37 * The cpuinfo_cur_freq indicates the actual operating CPU freqeuncy
38 * and scaling_cur_freq is the CPU frequency set by the CPUFREQ policy.
40 #define CPUFREQ_CURR_FREQ_PATH_SUFFIX "/cpufreq/cpuinfo_cur_freq"
41 #define CPUFREQ_MIN_FREQ_PATH_SUFFIX "/cpufreq/scaling_min_freq"
42 #define CPUFREQ_MAX_FREQ_PATH_SUFFIX "/cpufreq/scaling_max_freq"
43 #define CPUFREQ_UP_THRESHOLD_PATH_SUFFIX "/cpufreq/ondemand/up_threshold"
45 #define LOADTABLE_PATH_PREFIX "/sys/kernel/debug/cpufreq/"
46 #define LOADTABLE_PATH_SUFFIX "/load_table"
48 #define CPU_ONLINE_PATH_PREFIX "/sys/devices/system/cpu/cpu"
49 #define CPU_ONLINE_PATH_SUFFIX "/online"
50 #define CPU_ONLINE_STATE_ON 1
51 #define CPU_ONLINE_STATE_OFF 0
53 #define TMU_PATH_PREFIX "/sys/class/thermal/"
54 #define TMU_TEMP_PATH_SUFFIX "/temp"
55 #define TMU_POLICY_PATH_SUFFIX "/policy"
57 static int tm2_dvfs_get_curr_governor(char *res_name, char *governor)
62 if ((!res_name) || (!governor))
65 snprintf(path, PATH_MAX, "%s%s%s",
68 CPUFREQ_CURR_GOVERNOR_PATH_SUFFIX);
70 ret = sysfs_read_str(path, governor, BUFF_MAX);
77 static int tm2_dvfs_set_curr_governor(char *res_name, char *governor)
82 if ((!res_name) || (!governor))
85 snprintf(path, PATH_MAX, "%s%s%s",
88 CPUFREQ_CURR_GOVERNOR_PATH_SUFFIX);
90 ret = sysfs_write_str(path, governor);
97 static int tm2_dvfs_get_curr_freq(char *res_name)
105 snprintf(path, PATH_MAX, "%s%s%s",
108 CPUFREQ_CURR_FREQ_PATH_SUFFIX);
110 ret = sysfs_read_int(path, &freq);
117 static int tm2_dvfs_get_min_freq(char *res_name)
125 snprintf(path, PATH_MAX, "%s%s%s",
128 CPUFREQ_MIN_FREQ_PATH_SUFFIX);
130 ret = sysfs_read_int(path, &freq);
137 static int tm2_dvfs_set_min_freq(char *res_name, int freq)
142 if ((!res_name) || (freq < 0))
145 snprintf(path, PATH_MAX, "%s%s%s",
148 CPUFREQ_MIN_FREQ_PATH_SUFFIX);
150 ret = sysfs_write_int(path, freq);
157 static int tm2_dvfs_get_max_freq(char *res_name)
165 snprintf(path, PATH_MAX, "%s%s%s",
168 CPUFREQ_MAX_FREQ_PATH_SUFFIX);
170 ret = sysfs_read_int(path, &freq);
177 static int tm2_dvfs_set_max_freq(char *res_name, int freq)
182 if ((!res_name) || (freq < 0))
185 snprintf(path, PATH_MAX, "%s%s%s",
188 CPUFREQ_MAX_FREQ_PATH_SUFFIX);
190 ret = sysfs_write_int(path, freq);
196 static int tm2_dvfs_get_up_threshold(char *res_name)
204 snprintf(path, PATH_MAX, "%s%s%s",
207 CPUFREQ_UP_THRESHOLD_PATH_SUFFIX);
209 ret = sysfs_read_int(path, &val);
216 static int tm2_dvfs_set_up_threshold(char *res_name, int up_threshold)
221 if ((!res_name) || (up_threshold < 0))
224 snprintf(path, PATH_MAX, "%s%s%s",
227 CPUFREQ_UP_THRESHOLD_PATH_SUFFIX);
229 ret = sysfs_write_int(path, up_threshold);
236 static int tm2_dvfs_get_load_table(char *res_name, void *pass_cpu_load_table)
241 static struct pass_resource_dvfs_ops tm2_cpu_dvfs_ops = {
242 .get_curr_governor = tm2_dvfs_get_curr_governor,
243 .set_curr_governor = tm2_dvfs_set_curr_governor,
244 .get_avail_governor = NULL,
245 .get_curr_freq = tm2_dvfs_get_curr_freq,
246 .get_min_freq = tm2_dvfs_get_min_freq,
247 .set_min_freq = tm2_dvfs_set_min_freq,
248 .get_max_freq = tm2_dvfs_get_max_freq,
249 .set_max_freq = tm2_dvfs_set_max_freq,
250 .get_up_threshold = tm2_dvfs_get_up_threshold,
251 .set_up_threshold = tm2_dvfs_set_up_threshold,
252 .get_load_table = tm2_dvfs_get_load_table,
255 static int tm2_hotplug_get_online_state(char *res_name, int cpu)
263 snprintf(path, PATH_MAX, "%s%d%s",
264 CPU_ONLINE_PATH_PREFIX,
266 CPU_ONLINE_PATH_SUFFIX);
268 ret = sysfs_read_int(path, &online);
275 static int tm2_hotplug_set_online_state(char *res_name, int cpu, int on)
282 if ((on != CPU_ONLINE_STATE_ON) && (on != CPU_ONLINE_STATE_OFF))
286 * NOTE: Exynos SoC series cannot turn off the CPU0
287 * because of h/w design. To prevent the critical problem,
288 * if someone try to turn off the CPU0, just return without any
291 if (on == 0 && cpu == 0) {
292 _E("cannot turn off the CPU0");
296 snprintf(path, PATH_MAX, "%s%d%s",
297 CPU_ONLINE_PATH_PREFIX,
299 CPU_ONLINE_PATH_SUFFIX);
301 ret = sysfs_write_int(path, on);
308 static struct pass_resource_hotplug_ops tm2_cpu_hotplus_ops = {
309 .get_online_state = tm2_hotplug_get_online_state,
310 .set_online_state = tm2_hotplug_set_online_state,
311 .get_online_min_num = NULL,
312 .set_online_min_num = NULL,
313 .get_online_max_num = NULL,
314 .set_online_max_num = NULL,
317 static int tm2_tmu_get_temp(char *res_thermal_name)
322 if (!res_thermal_name)
325 snprintf(path, PATH_MAX, "%s%s%s",
328 TMU_TEMP_PATH_SUFFIX);
330 ret = sysfs_read_int(path, &temp);
337 static int tm2_tmu_get_policy(char *res_thermal_name, char *policy)
342 if ((!res_thermal_name) || (!policy))
345 snprintf(path, PATH_MAX, "%s%s%s",
348 TMU_POLICY_PATH_SUFFIX);
350 ret = sysfs_read_str(path, policy, BUFF_MAX);
357 static struct pass_resource_tmu_ops tm2_cpu_tmu_ops = {
358 .get_temp = tm2_tmu_get_temp,
359 .get_policy = tm2_tmu_get_policy,
362 static int tm2_cpu_open(char *res_name, struct pass_resource_info *info,
363 struct pass_resource_common **common)
365 struct pass_resource_cpu *cpu_res;
370 /* TODO: Possibility of a memory leak */
371 cpu_res = calloc(1, sizeof(struct pass_resource_cpu));
375 cpu_res->common.info = info;
376 cpu_res->dvfs = tm2_cpu_dvfs_ops;
377 cpu_res->hotplug = tm2_cpu_hotplus_ops;
378 cpu_res->tmu = tm2_cpu_tmu_ops;
380 *common = (struct pass_resource_common *) cpu_res;
385 static int tm2_cpu_close(char *res_name, struct pass_resource_common *common)
395 HAL_MODULE_STRUCTURE = {
396 .magic = HAL_INFO_TAG,
397 .hal_version = HAL_VERSION,
398 .device_version = DEV_VERSION_CPU,
399 .id = PASS_RESOURCE_CPU_ID,
400 .name = PASS_RESOURCE_CPU_NAME,
401 .open = tm2_cpu_open,
402 .close = tm2_cpu_close,