xref: /linux/tools/testing/selftests/powerpc/papr_attributes/attr_test.c (revision 702648721db590b3425c31ade294000e18808345)
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 <errno.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <dirent.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <unistd.h>
17 #include <stdlib.h>
18 
19 #include "utils.h"
20 
21 enum energy_freq_attrs {
22 	POWER_PERFORMANCE_MODE = 1,
23 	IDLE_POWER_SAVER_STATUS = 2,
24 	MIN_FREQ = 3,
25 	STAT_FREQ = 4,
26 	MAX_FREQ = 6,
27 	PROC_FOLDING_STATUS = 8
28 };
29 
30 enum type {
31 	INVALID,
32 	STR_VAL,
33 	NUM_VAL
34 };
35 
36 static int value_type(int id)
37 {
38 	int val_type;
39 
40 	switch (id) {
41 	case POWER_PERFORMANCE_MODE:
42 	case IDLE_POWER_SAVER_STATUS:
43 		val_type = STR_VAL;
44 		break;
45 	case MIN_FREQ:
46 	case STAT_FREQ:
47 	case MAX_FREQ:
48 	case PROC_FOLDING_STATUS:
49 		val_type = NUM_VAL;
50 		break;
51 	default:
52 		val_type = INVALID;
53 	}
54 
55 	return val_type;
56 }
57 
58 static int verify_energy_info(void)
59 {
60 	const char *path = "/sys/firmware/papr/energy_scale_info";
61 	struct dirent *entry;
62 	struct stat s;
63 	DIR *dirp;
64 
65 	errno = 0;
66 	if (stat(path, &s)) {
67 		SKIP_IF(errno == ENOENT);
68 		FAIL_IF(errno);
69 	}
70 
71 	FAIL_IF(!S_ISDIR(s.st_mode));
72 
73 	dirp = opendir(path);
74 
75 	while ((entry = readdir(dirp)) != NULL) {
76 		char file_name[64];
77 		int id, attr_type;
78 		FILE *f;
79 
80 		if (strcmp(entry->d_name, ".") == 0 ||
81 		    strcmp(entry->d_name, "..") == 0)
82 			continue;
83 
84 		id = atoi(entry->d_name);
85 		attr_type = value_type(id);
86 		FAIL_IF(attr_type == INVALID);
87 
88 		/* Check if the files exist and have data in them */
89 		sprintf(file_name, "%s/%d/desc", path, id);
90 		f = fopen(file_name, "r");
91 		FAIL_IF(!f);
92 		FAIL_IF(fgetc(f) == EOF);
93 
94 		sprintf(file_name, "%s/%d/value", path, id);
95 		f = fopen(file_name, "r");
96 		FAIL_IF(!f);
97 		FAIL_IF(fgetc(f) == EOF);
98 
99 		if (attr_type == STR_VAL) {
100 			sprintf(file_name, "%s/%d/value_desc", path, id);
101 			f = fopen(file_name, "r");
102 			FAIL_IF(!f);
103 			FAIL_IF(fgetc(f) == EOF);
104 		}
105 	}
106 
107 	return 0;
108 }
109 
110 int main(void)
111 {
112 	return test_harness(verify_energy_info, "papr_attributes");
113 }
114