summaryrefslogtreecommitdiff
path: root/src/hal-backend-power.c
blob: b25ad6fd7bc134a2d83d5f3d5288f9a075dad6aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
 * HAL backend for hal-api-power which is used for PASS daemon
 *
 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <hal/hal-power-interface.h>

#include "sysfs.h"

#define BUFF_MAX	255

/*************************
 * H/W Resource Defintions
 */
#define CPUFREQ_PATH_PREFIX			"/sys/devices/system/cpu/"
#define CPUFREQ_CURR_GOVERNOR_PATH_SUFFIX	"/cpufreq/scaling_governor"
#define CPUFREQ_AVAIL_GOVERNOR_PATH_SUFFIX	"/cpufreq/scaling_available_governors"

/*
 * The cpuinfo_cur_freq indicates the actual operating CPU freqeuncy
 * and scaling_cur_freq is the CPU frequency set by the CPUFREQ policy.
 */
#define CPUFREQ_CURR_FREQ_PATH_SUFFIX		"/cpufreq/cpuinfo_cur_freq"
#define CPUFREQ_AVAILABLE_MIN_FREQ_PATH_SUFFIX	"/cpufreq/cpuinfo_min_freq"
#define CPUFREQ_AVAILABLE_MAX_FREQ_PATH_SUFFIX	"/cpufreq/cpuinfo_max_freq"
#define CPUFREQ_MIN_FREQ_PATH_SUFFIX		"/cpufreq/scaling_min_freq"
#define CPUFREQ_MAX_FREQ_PATH_SUFFIX		"/cpufreq/scaling_max_freq"
#define CPUFREQ_UP_THRESHOLD_PATH_SUFFIX	"/cpufreq/ondemand/up_threshold"

#define CPU_ONLINE_PATH_PREFIX			"/sys/devices/system/cpu/cpu"
#define CPU_ONLINE_PATH_SUFFIX			"/online"
#define CPU_ONLINE_STATE_ON			1
#define CPU_ONLINE_STATE_OFF			0

#define TMU_PATH_PREFIX				"/sys/class/thermal/"
#define TMU_TEMP_PATH_SUFFIX			"/temp"
#define TMU_POLICY_PATH_SUFFIX			"/policy"
#define TMU_MAX_TEMP_MASK				0xFFFFF

#define DEVFREQ_BUS_PATH_PREFIX			"/sys/class/devfreq/"
#define DEVFREQ_BUS_CURR_GOVERNOR_PATH_SUFFIX	"/governor"
#define DEVFREQ_BUS_CURR_FREQ_PATH_SUFFIX	"/cur_freq"
#define DEVFREQ_BUS_MIN_FREQ_PATH_SUFFIX	"/min_freq"
#define DEVFREQ_BUS_MAX_FREQ_PATH_SUFFIX	"/max_freq"
#define DEVFREQ_BUS_AVAILABLE_FREQ_PATH_SUFFIX	"/available_frequencies"

#define FAULT_AROUND_BYTES_PATH			"/sys/kernel/debug/fault_around_bytes"

static int get_path(char *path, char *prefix, char *res_name, char *suffix)
{
	int ret;

	if (!path || !prefix || !res_name || !suffix)
		return -EINVAL;

	ret = snprintf(path, PATH_MAX, "%s%s%s", prefix, res_name, suffix);
	if (ret <= 0)
		return -EINVAL;

	path[PATH_MAX - 1] = '\0';

	return 0;
}

static int __handle_value_string(int is_write, char *prefix, char *res_name, char *suffix, char *buf)
{
	char path[PATH_MAX];
	int ret;

	if (!buf)
		return -EINVAL;

	ret = get_path(path, prefix, res_name, suffix);
	if (ret < 0)
		return ret;

	if (!!is_write)
		ret = sysfs_write_str(path, buf);
	else
		ret = sysfs_read_str(path, buf, BUFF_MAX);

	return (ret < 0) ? ret : 0;
}

static int get_value_string(char *prefix, char *res_name, char *suffix, char *buf)
{
	return __handle_value_string(0, prefix, res_name, suffix, buf);
}

static int set_value_string(char *prefix, char *res_name, char *suffix, char *buf)
{
	return __handle_value_string(1, prefix, res_name, suffix, buf);
}

static int get_value_integer(char *prefix, char *res_name, char *suffix, int *value)
{
	char path[PATH_MAX];
	int ret;

	if (!value)
		return -EINVAL;

	ret = get_path(path, prefix, res_name, suffix);
	if (ret < 0)
		return ret;

	ret = sysfs_read_int(path, value);
	if (ret < 0)
		return ret;

	return 0;
}

static int set_value_integer(char *prefix, char *res_name, char *suffix, int value)
{
	char path[PATH_MAX];
	int ret;

	ret = get_path(path, prefix, res_name, suffix);
	if (ret < 0)
		return ret;

	ret = sysfs_write_int(path, value);
	if (ret < 0)
		return ret;

	return 0;
}

/*************************************************
 * HAL backend implementation for CPU H/W Resource
 */
static int cpufreq_dvfs_get_curr_governor(char *res_name, char *governor)
{
	int ret;

	ret = get_value_string(CPUFREQ_PATH_PREFIX, res_name,
				CPUFREQ_CURR_GOVERNOR_PATH_SUFFIX, governor);
	return (ret < 0) ? ret : 0;
}

static int cpufreq_dvfs_set_curr_governor(char *res_name, char *governor)
{
	int ret;

	ret = set_value_string(CPUFREQ_PATH_PREFIX, res_name,
				CPUFREQ_CURR_GOVERNOR_PATH_SUFFIX, governor);
	return (ret < 0) ? ret : 0;
}

static int cpufreq_dvfs_get_curr_freq(char *res_name)
{
	int freq, ret;

	ret = get_value_integer(CPUFREQ_PATH_PREFIX, res_name,
				CPUFREQ_CURR_FREQ_PATH_SUFFIX, &freq);
	return (ret < 0) ? ret : freq;
}

static int cpufreq_dvfs_get_min_freq(char *res_name)
{
	int freq, ret;

	ret = get_value_integer(CPUFREQ_PATH_PREFIX, res_name,
				CPUFREQ_MIN_FREQ_PATH_SUFFIX, &freq);
	return (ret < 0) ? ret : freq;
}

static int cpufreq_dvfs_set_min_freq(char *res_name, int freq)
{
	int ret;

	if (freq < 0)
		return -EINVAL;

	ret = set_value_integer(CPUFREQ_PATH_PREFIX, res_name,
				CPUFREQ_MIN_FREQ_PATH_SUFFIX, freq);
	return (ret < 0) ? ret : 0;
}

static int cpufreq_dvfs_get_max_freq(char *res_name)
{
	int freq, ret;

	ret = get_value_integer(CPUFREQ_PATH_PREFIX, res_name,
				CPUFREQ_MAX_FREQ_PATH_SUFFIX, &freq);
	return (ret < 0) ? ret : freq;
}

static int cpufreq_dvfs_set_max_freq(char *res_name, int freq)
{
	int ret;

	if (freq < 0)
		return -EINVAL;

	ret = set_value_integer(CPUFREQ_PATH_PREFIX, res_name,
				CPUFREQ_MAX_FREQ_PATH_SUFFIX, freq);
	return (ret < 0) ? ret : 0;
}

static int cpufreq_dvfs_get_available_min_freq(char *res_name)
{
	int val, ret;

	ret = get_value_integer(CPUFREQ_PATH_PREFIX, res_name,
				CPUFREQ_AVAILABLE_MIN_FREQ_PATH_SUFFIX, &val);
	return (ret < 0) ? ret : val;
}

static int cpufreq_dvfs_get_available_max_freq(char *res_name)
{
	int val, ret;

	ret = get_value_integer(CPUFREQ_PATH_PREFIX, res_name,
				CPUFREQ_AVAILABLE_MAX_FREQ_PATH_SUFFIX, &val);
	return (ret < 0) ? ret : val;
}

static int cpufreq_dvfs_get_up_threshold(char *res_name)
{
	int val, ret;

	ret = get_value_integer(CPUFREQ_PATH_PREFIX, res_name,
				CPUFREQ_UP_THRESHOLD_PATH_SUFFIX, &val);
	return (ret < 0) ? ret : val;
}

static int cpufreq_dvfs_set_up_threshold(char *res_name, int up_threshold)
{
	int ret;

	if (up_threshold < 0)
		return -EINVAL;

	ret = set_value_integer(CPUFREQ_PATH_PREFIX, res_name,
				CPUFREQ_UP_THRESHOLD_PATH_SUFFIX, up_threshold);
	return (ret < 0) ? ret : 0;
}

static struct pass_resource_dvfs_ops cpufreq_dvfs_ops =  {
	.get_curr_governor = cpufreq_dvfs_get_curr_governor,
	.set_curr_governor = cpufreq_dvfs_set_curr_governor,
	.get_curr_freq = cpufreq_dvfs_get_curr_freq,
	.get_min_freq = cpufreq_dvfs_get_min_freq,
	.set_min_freq = cpufreq_dvfs_set_min_freq,
	.get_max_freq = cpufreq_dvfs_get_max_freq,
	.set_max_freq = cpufreq_dvfs_set_max_freq,
	.get_available_min_freq = cpufreq_dvfs_get_available_min_freq,
	.get_available_max_freq = cpufreq_dvfs_get_available_max_freq,
	.get_up_threshold = cpufreq_dvfs_get_up_threshold,
	.set_up_threshold = cpufreq_dvfs_set_up_threshold,
};

static int cpu_hotplug_get_online_state(char *res_name, int cpu)
{
	char path[PATH_MAX];
	int ret, online;

	if ((!res_name))
		return -EINVAL;

	snprintf(path, PATH_MAX, "%s%d%s",
		CPU_ONLINE_PATH_PREFIX,
		cpu,
		CPU_ONLINE_PATH_SUFFIX);

	ret = sysfs_read_int(path, &online);
	if (ret < 0)
		return ret;

	return online;
}

static int cpu_hotplug_set_online_state(char *res_name, int cpu, int on)
{
	char path[PATH_MAX];
	int ret;

	if ((!res_name))
		return -EINVAL;
	if ((on != CPU_ONLINE_STATE_ON) && (on != CPU_ONLINE_STATE_OFF))
		return -EINVAL;

	/*
	 * NOTE: Exynos SoC series cannot turn off the CPU0
	 * because of h/w design. To prevent the critical problem,
	 * if someone try to turn off the CPU0, just return without any
	 * opertaion.
	 */
	if (on == 0 && cpu == 0) {
		return 0;
	}

	snprintf(path, PATH_MAX, "%s%d%s",
		CPU_ONLINE_PATH_PREFIX,
		cpu,
		CPU_ONLINE_PATH_SUFFIX);

	ret = sysfs_write_int(path, on);
	if (ret < 0)
		return ret;

	return 0;
}

static struct pass_resource_hotplug_ops cpu_hotplus_ops = {
	.get_online_state = cpu_hotplug_get_online_state,
	.set_online_state = cpu_hotplug_set_online_state,
	.get_online_min_num = NULL,
	.set_online_min_num = NULL,
	.get_online_max_num = NULL,
	.set_online_max_num = NULL,
};

static int tmu_get_temp(char *res_thermal_name)
{
	int temp, ret;

	ret = get_value_integer(TMU_PATH_PREFIX, res_thermal_name,
				TMU_TEMP_PATH_SUFFIX, &temp);
	if (ret <  0)
		return ret;

	/*
	 * Thermal framework provides the current temperature
	 * as five digits interger like 54430 when temperature is 54.430
	 * degrees centigrade. But, Thermal Monitor in Tizen usually
	 * use two digits interger without decimal point.
	 * So that round temperature value. It constraints the maximume
	 * temperature as 1048 degrees centigrade for preventing integer
	 * overflow. Usually, the embedded device never over 1000 degrees
	 * centigrade.
	 */
	return (((temp & TMU_MAX_TEMP_MASK) + 500) / 1000);
}

static int tmu_get_policy(char *res_thermal_name, char *policy)
{
	int ret;

	if (!policy)
		return -EINVAL;

	ret = get_value_string(TMU_PATH_PREFIX, res_thermal_name,
				TMU_POLICY_PATH_SUFFIX, policy);
	return (ret < 0) ? ret : 0;
}

static struct pass_resource_tmu_ops tmu_ops = {
	.get_temp = tmu_get_temp,
	.get_policy = tmu_get_policy,
};

/*****************************************************
 * HAL backend implementation for BUS/GPU H/W Resource
 */
static int devfreq_dvfs_get_curr_governor(char *res_name, char *governor)
{
	int ret;

	if (!governor)
		return -EINVAL;

	ret = get_value_string(DEVFREQ_BUS_PATH_PREFIX, res_name,
				DEVFREQ_BUS_CURR_GOVERNOR_PATH_SUFFIX, governor);
	return (ret < 0) ? ret : 0;
}

static int devfreq_dvfs_set_curr_governor(char *res_name, char *governor)
{
	int ret;

	if (!governor)
		return -EINVAL;

	ret = set_value_string(DEVFREQ_BUS_PATH_PREFIX, res_name,
				DEVFREQ_BUS_CURR_GOVERNOR_PATH_SUFFIX, governor);
	return (ret < 0) ? ret : 0;
}

static int devfreq_dvfs_get_curr_freq(char *res_name)
{
	int freq, ret;

	ret = get_value_integer(DEVFREQ_BUS_PATH_PREFIX, res_name,
				DEVFREQ_BUS_CURR_FREQ_PATH_SUFFIX, &freq);
	return (ret < 0) ? ret : freq;
}

static int devfreq_dvfs_get_min_freq(char *res_name)
{
	int freq, ret;

	ret = get_value_integer(DEVFREQ_BUS_PATH_PREFIX, res_name,
				DEVFREQ_BUS_MIN_FREQ_PATH_SUFFIX, &freq);
	return (ret < 0) ? ret : freq;
}

static int devfreq_dvfs_set_min_freq(char *res_name, int freq)
{
	int ret;

	if (freq < 0)
		return -EINVAL;

	ret = set_value_integer(DEVFREQ_BUS_PATH_PREFIX, res_name,
				DEVFREQ_BUS_MIN_FREQ_PATH_SUFFIX, freq);
	return (ret < 0) ? ret : 0;
}

static int devfreq_dvfs_get_max_freq(char *res_name)
{
	int freq, ret;

	ret = get_value_integer(DEVFREQ_BUS_PATH_PREFIX, res_name,
				DEVFREQ_BUS_MAX_FREQ_PATH_SUFFIX, &freq);
	return (ret < 0) ? ret : freq;
}

static int devfreq_dvfs_set_max_freq(char *res_name, int freq)
{
	int ret;

	if (freq < 0)
		return -EINVAL;

	ret = set_value_integer(DEVFREQ_BUS_PATH_PREFIX, res_name,
				DEVFREQ_BUS_MAX_FREQ_PATH_SUFFIX, freq);
	return (ret < 0) ? ret : 0;
}

static int devfreq_dvfs_get_available_min_freq(char *res_name)
{
	char buf[MAX_BUF_SIZE + 1];
	char *p;
	int ret;

	ret = get_value_string(DEVFREQ_BUS_PATH_PREFIX, res_name,
				DEVFREQ_BUS_AVAILABLE_FREQ_PATH_SUFFIX, buf);
	if (ret <  0)
		return ret;

	p = strchr(buf, ' ');
	if (p)
		*p = '\0';

	ret = strtoul(buf, NULL, 10);
	if (!ret)
		return -EINVAL;

	return ret;
}

static int devfreq_dvfs_get_available_max_freq(char *res_name)
{
	char buf[MAX_BUF_SIZE + 1];
	char *p;
	int ret;

	ret = get_value_string(DEVFREQ_BUS_PATH_PREFIX, res_name,
				DEVFREQ_BUS_AVAILABLE_FREQ_PATH_SUFFIX, buf);
	if (ret <  0)
		return ret;

	p = strrchr(buf, ' ');
	p = p ? p + 1 : buf;

	ret = strtoul(p, NULL, 10);
	if (!ret)
		return -EINVAL;

	return ret;
}

static struct pass_resource_dvfs_ops devfreq_dvfs_ops =  {
	.get_curr_governor = devfreq_dvfs_get_curr_governor,
	.set_curr_governor = devfreq_dvfs_set_curr_governor,
	.get_curr_freq = devfreq_dvfs_get_curr_freq,
	.get_min_freq = devfreq_dvfs_get_min_freq,
	.set_min_freq = devfreq_dvfs_set_min_freq,
	.get_max_freq = devfreq_dvfs_get_max_freq,
	.set_max_freq = devfreq_dvfs_set_max_freq,
	.get_available_min_freq = devfreq_dvfs_get_available_min_freq,
	.get_available_max_freq = devfreq_dvfs_get_available_max_freq,
};

/****************************************************
 * HAL backend implementation for Memory H/W Resource
 */
static int memory_get_fault_around_bytes(char *res_name)
{
	int ret, fault_around_bytes;

	if (!res_name)
		return -EINVAL;

	ret = sysfs_read_int(FAULT_AROUND_BYTES_PATH, &fault_around_bytes);
	if (ret < 0)
		return ret;

	return fault_around_bytes;
}

static int memory_set_fault_around_bytes(char *res_name,
						int fault_around_bytes)
{
	int ret;

	if ((!res_name) || (fault_around_bytes <= 0))
		return -EINVAL;

	ret = sysfs_write_int(FAULT_AROUND_BYTES_PATH, fault_around_bytes);
	if (ret < 0)
		return ret;

	return 0;
}

/************************
 * HAL backend power data
 */
static int power_init(void **data)
{
	hal_backend_power_funcs *power_funcs = NULL;
	struct pass_resource_cpu *cpu = NULL;
	struct pass_resource_bus *bus = NULL;
	struct pass_resource_gpu *gpu = NULL;
	struct pass_resource_memory *memory = NULL;
	int ret;

	/* Allocate memory */
	power_funcs = calloc(1, sizeof(hal_backend_power_funcs));
	if (!power_funcs)
		return -ENOMEM;

	cpu = calloc(1, sizeof(struct pass_resource_cpu));
	if (!cpu) {
		ret = -ENOMEM;
		goto err_funcs;
	}

	bus = calloc(1, sizeof(struct pass_resource_bus));
	if (!bus) {
		ret = -ENOMEM;
		goto err_cpu;
	}

	gpu = calloc(1, sizeof(struct pass_resource_gpu));
	if (!gpu) {
		ret = -ENOMEM;
		goto err_bus;
	}

	memory = calloc(1, sizeof(struct pass_resource_memory));
	if (!memory) {
		ret = -ENOMEM;
		goto err_gpu;
	}

	/* Initialize each h/w resource */
	cpu->dvfs = cpufreq_dvfs_ops;
	cpu->hotplug = cpu_hotplus_ops;
	cpu->tmu = tmu_ops;

	bus->dvfs = devfreq_dvfs_ops;
	bus->tmu = tmu_ops;

	gpu->dvfs = devfreq_dvfs_ops;
	gpu->tmu = tmu_ops;

	memory->get_fault_around_bytes = memory_get_fault_around_bytes;
	memory->set_fault_around_bytes = memory_set_fault_around_bytes;

	/* Initialize hal_backend_power_funcs  */
	power_funcs->cpu = cpu;
	power_funcs->bus = bus;
	power_funcs->gpu = gpu;
	power_funcs->memory = memory;

	*data = (void *)power_funcs;

	return 0;

err_gpu:
	if (gpu)
		free(gpu);
err_bus:
	if (bus)
		free(bus);
err_cpu:
	if (cpu)
		free(cpu);
err_funcs:
	free(power_funcs);

	return ret;
}

static int power_exit(void *data)
{
	hal_backend_power_funcs *funcs;

	if (!data)
		return -EINVAL;

	funcs = (hal_backend_power_funcs *)data;

	if (funcs->cpu)
		free(funcs->cpu);
	if (funcs->bus)
		free(funcs->bus);
	if (funcs->gpu)
		free(funcs->gpu);
	if (funcs->memory)
		free(funcs->memory);

	free(funcs);

	return 0;
}

hal_backend hal_backend_power_data = {
	.name = "power-cpu",
	.vendor = "",
	.abi_version = HAL_ABI_VERSION_TIZEN_6_5,
	.init = power_init,
	.exit = power_exit,
};