1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * PAPR Energy attributes sniff test
4  * This checks if the papr folders and contents are populated relating to
5  * the energy and frequency attributes
6  *
7  * Copyright 2022, Pratik Rajesh Sampat, IBM Corp.
8  */
9 
10 #include <stdio.h>
11 #include <string.h>
12 #include <dirent.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 
18 #include "utils.h"
19 
20 enum energy_freq_attrs {
21 	POWER_PERFORMANCE_MODE = 1,
22 	IDLE_POWER_SAVER_STATUS = 2,
23 	MIN_FREQ = 3,
24 	STAT_FREQ = 4,
25 	MAX_FREQ = 6,
26 	PROC_FOLDING_STATUS = 8
27 };
28 
29 enum type {
30 	INVALID,
31 	STR_VAL,
32 	NUM_VAL
33 };
34 
35 int value_type(int id)
36 {
37 	int val_type;
38 
39 	switch (id) {
40 	case POWER_PERFORMANCE_MODE:
41 	case IDLE_POWER_SAVER_STATUS:
42 		val_type = STR_VAL;
43 		break;
44 	case MIN_FREQ:
45 	case STAT_FREQ:
46 	case MAX_FREQ:
47 	case PROC_FOLDING_STATUS:
48 		val_type = NUM_VAL;
49 		break;
50 	default:
51 		val_type = INVALID;
52 	}
53 
54 	return val_type;
55 }
56 
57 int verify_energy_info(void)
58 {
59 	const char *path = "/sys/firmware/papr/energy_scale_info";
60 	struct dirent *entry;
61 	struct stat s;
62 	DIR *dirp;
63 
64 	if (stat(path, &s) || !S_ISDIR(s.st_mode))
65 		return -1;
66 	dirp = opendir(path);
67 
68 	while ((entry = readdir(dirp)) != NULL) {
69 		char file_name[64];
70 		int id, attr_type;
71 		FILE *f;
72 
73 		if (strcmp(entry->d_name, ".") == 0 ||
74 		    strcmp(entry->d_name, "..") == 0)
75 			continue;
76 
77 		id = atoi(entry->d_name);
78 		attr_type = value_type(id);
79 		if (attr_type == INVALID)
80 			return -1;
81 
82 		/* Check if the files exist and have data in them */
83 		sprintf(file_name, "%s/%d/desc", path, id);
84 		f = fopen(file_name, "r");
85 		if (!f || fgetc(f) == EOF)
86 			return -1;
87 
88 		sprintf(file_name, "%s/%d/value", path, id);
89 		f = fopen(file_name, "r");
90 		if (!f || fgetc(f) == EOF)
91 			return -1;
92 
93 		if (attr_type == STR_VAL) {
94 			sprintf(file_name, "%s/%d/value_desc", path, id);
95 			f = fopen(file_name, "r");
96 			if (!f || fgetc(f) == EOF)
97 				return -1;
98 		}
99 	}
100 
101 	return 0;
102 }
103 
104 int main(void)
105 {
106 	return test_harness(verify_energy_info, "papr_attributes");
107 }
108