pm_rps: Fix verbose option and streamline its use
[platform/upstream/intel-gpu-tools.git] / tests / pm_rps.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Ben Widawsky <ben@bwidawsk.net>
25  *    Jeff McGee <jeff.mcgee@intel.com>
26  *
27  */
28
29 #define _GNU_SOURCE
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <getopt.h>
35 #include "drmtest.h"
36
37 static bool verbose = false;
38 static int origmin, origmax;
39
40 static const char sysfs_base_path[] = "/sys/class/drm/card%d/gt_%s_freq_mhz";
41 enum {
42         CUR,
43         MIN,
44         MAX,
45         RP0,
46         RP1,
47         RPn
48 };
49
50 struct junk {
51         const char *name;
52         const char *mode;
53         FILE *filp;
54 } stuff[] = {
55         { "cur", "r", NULL }, { "min", "rb+", NULL }, { "max", "rb+", NULL }, { "RP0", "r", NULL }, { "RP1", "r", NULL }, { "RPn", "r", NULL }, { NULL, NULL, NULL }
56 };
57
58 static int readval(FILE *filp)
59 {
60         int val;
61         int scanned;
62
63         rewind(filp);
64         scanned = fscanf(filp, "%d", &val);
65         igt_assert(scanned == 1);
66
67         return val;
68 }
69
70 static int do_writeval(FILE *filp, int val, int lerrno)
71 {
72         int ret, orig;
73
74         orig = readval(filp);
75         rewind(filp);
76         ret = fprintf(filp, "%d", val);
77
78         if (lerrno) {
79                 /* Expecting specific error */
80                 igt_assert(ret == EOF && errno == lerrno);
81                 igt_assert(readval(filp) == orig);
82         } else {
83                 /* Expecting no error */
84                 igt_assert(ret != EOF);
85                 igt_assert(readval(filp) == val);
86         }
87
88         return ret;
89 }
90 #define writeval(filp, val) do_writeval(filp, val, 0)
91 #define writeval_inval(filp, val) do_writeval(filp, val, EINVAL)
92
93 #define fcur (readval(stuff[CUR].filp))
94 #define fmin (readval(stuff[MIN].filp))
95 #define fmax (readval(stuff[MAX].filp))
96 #define frp0 (readval(stuff[RP0].filp))
97 #define frp1 (readval(stuff[RP1].filp))
98 #define frpn (readval(stuff[RPn].filp))
99
100 static void setfreq(int val)
101 {
102         if (val > fmax) {
103                 writeval(stuff[MAX].filp, val);
104                 writeval(stuff[MIN].filp, val);
105         } else {
106                 writeval(stuff[MIN].filp, val);
107                 writeval(stuff[MAX].filp, val);
108         }
109 }
110
111 static void checkit(void)
112 {
113         igt_assert(fmin <= fmax);
114         igt_assert(fcur <= fmax);
115         igt_assert(fmin <= fcur);
116         igt_assert(frpn <= fmin);
117         igt_assert(fmax <= frp0);
118         igt_assert(frp1 <= frp0);
119         igt_assert(frpn <= frp1);
120         igt_assert(frp0 != 0);
121         igt_assert(frp1 != 0);
122 }
123
124 static void dumpit(void)
125 {
126         struct junk *junk = stuff;
127         do {
128                 printf("gt frequency %s (MHz):  %d\n", junk->name, readval(junk->filp));
129                 junk++;
130         } while(junk->name != NULL);
131
132         printf("\n");
133 }
134 #define dump() if (verbose) dumpit()
135 #define log(...) if (verbose) printf(__VA_ARGS__)
136
137 static void pm_rps_exit_handler(int sig)
138 {
139         if (origmin > fmax) {
140                 writeval(stuff[MAX].filp, origmax);
141                 writeval(stuff[MIN].filp, origmin);
142         } else {
143                 writeval(stuff[MIN].filp, origmin);
144                 writeval(stuff[MAX].filp, origmax);
145         }
146 }
147
148 static int opt_handler(int opt, int opt_index)
149 {
150         switch (opt) {
151         case 'v':
152                 verbose = true;
153                 break;
154         default:
155                 assert(0);
156         }
157
158         return 0;
159 }
160
161 /* Mod of igt_subtest_init that adds our extra options */
162 void subtest_init(int argc, char **argv)
163 {
164         struct option long_opts[] = {
165                 {"verbose", 0, 0, 'v'}
166         };
167         const char *help_str = "  -v, --verbose";
168         int ret;
169
170         ret = igt_subtest_init_parse_opts(argc, argv, "v", long_opts,
171                                           help_str, opt_handler);
172
173         if (ret < 0)
174                 /* exit with no error for -h/--help */
175                 exit(ret == -1 ? 0 : ret);
176 }
177
178 int main(int argc, char **argv)
179 {
180         subtest_init(argc, argv);
181
182         igt_skip_on_simulation();
183
184         igt_fixture {
185                 const int device = drm_get_card();
186                 struct junk *junk = stuff;
187                 int fd, ret;
188
189                 /* Use drm_open_any to verify device existence */
190                 fd = drm_open_any();
191                 close(fd);
192
193                 do {
194                         int val = -1;
195                         char *path;
196                         ret = asprintf(&path, sysfs_base_path, device, junk->name);
197                         igt_assert(ret != -1);
198                         junk->filp = fopen(path, junk->mode);
199                         igt_require(junk->filp);
200                         setbuf(junk->filp, NULL);
201
202                         val = readval(junk->filp);
203                         igt_assert(val >= 0);
204                         junk++;
205                 } while(junk->name != NULL);
206
207                 origmin = fmin;
208                 origmax = fmax;
209
210                 igt_install_exit_handler(pm_rps_exit_handler);
211         }
212
213         igt_subtest("min-max-config-at-idle") {
214                 log("Original min = %d\nOriginal max = %d\n", origmin, origmax);
215
216                 dump();
217
218                 checkit();
219                 setfreq(origmin);
220                 dump();
221                 igt_assert(fcur == fmin);
222                 setfreq(origmax);
223                 dump();
224                 igt_assert(fcur == fmax);
225                 checkit();
226
227                 /* And some errors */
228                 writeval_inval(stuff[MIN].filp, frpn - 1);
229                 writeval_inval(stuff[MAX].filp, frp0 + 1000);
230                 checkit();
231
232                 writeval_inval(stuff[MIN].filp, fmax + 1000);
233                 writeval_inval(stuff[MAX].filp, fmin - 1);
234                 checkit();
235
236                 writeval_inval(stuff[MIN].filp, 0x11111110);
237                 writeval_inval(stuff[MAX].filp, 0);
238
239                 writeval(stuff[MIN].filp, origmin);
240                 writeval(stuff[MAX].filp, origmax);
241         }
242
243         igt_exit();
244 }