pm_rps: Expand on min and max config testing
[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 min_max_config(void (*check)(void))
138 {
139         int fmid = (frpn + frp0) / 2;
140
141         log("Check original min and max...\n");
142         check();
143
144         log("Set min=RPn and max=RP0...\n");
145         writeval(stuff[MIN].filp, frpn);
146         writeval(stuff[MAX].filp, frp0);
147         check();
148
149         log("Increase min to midpoint...\n");
150         writeval(stuff[MIN].filp, fmid);
151         check();
152
153         log("Increase min to RP0...\n");
154         writeval(stuff[MIN].filp, frp0);
155         check();
156
157         log("Increase min above RP0 (invalid)...\n");
158         writeval_inval(stuff[MIN].filp, frp0 + 1000);
159         check();
160
161         log("Decrease max to RPn (invalid)...\n");
162         writeval_inval(stuff[MAX].filp, frpn);
163         check();
164
165         log("Decrease min to midpoint...\n");
166         writeval(stuff[MIN].filp, fmid);
167         check();
168
169         log("Decrease min to RPn...\n");
170         writeval(stuff[MIN].filp, frpn);
171         check();
172
173         log("Decrease min below RPn (invalid)...\n");
174         writeval_inval(stuff[MIN].filp, 0);
175         check();
176
177         log("Decrease max to midpoint...\n");
178         writeval(stuff[MAX].filp, fmid);
179         check();
180
181         log("Decrease max to RPn...\n");
182         writeval(stuff[MAX].filp, frpn);
183         check();
184
185         log("Decrease max below RPn (invalid)...\n");
186         writeval_inval(stuff[MAX].filp, 0);
187         check();
188
189         log("Increase min to RP0 (invalid)...\n");
190         writeval_inval(stuff[MIN].filp, frp0);
191         check();
192
193         log("Increase max to midpoint...\n");
194         writeval(stuff[MAX].filp, fmid);
195         check();
196
197         log("Increase max to RP0...\n");
198         writeval(stuff[MAX].filp, frp0);
199         check();
200
201         log("Increase max above RP0 (invalid)...\n");
202         writeval_inval(stuff[MAX].filp, frp0 + 1000);
203         check();
204 }
205
206 static void idle_check(void)
207 {
208         dump();
209         checkit();
210 }
211
212 static void pm_rps_exit_handler(int sig)
213 {
214         if (origmin > fmax) {
215                 writeval(stuff[MAX].filp, origmax);
216                 writeval(stuff[MIN].filp, origmin);
217         } else {
218                 writeval(stuff[MIN].filp, origmin);
219                 writeval(stuff[MAX].filp, origmax);
220         }
221 }
222
223 static int opt_handler(int opt, int opt_index)
224 {
225         switch (opt) {
226         case 'v':
227                 verbose = true;
228                 break;
229         default:
230                 assert(0);
231         }
232
233         return 0;
234 }
235
236 /* Mod of igt_subtest_init that adds our extra options */
237 static void subtest_init(int argc, char **argv)
238 {
239         struct option long_opts[] = {
240                 {"verbose", 0, 0, 'v'}
241         };
242         const char *help_str = "  -v, --verbose";
243         int ret;
244
245         ret = igt_subtest_init_parse_opts(argc, argv, "v", long_opts,
246                                           help_str, opt_handler);
247
248         if (ret < 0)
249                 /* exit with no error for -h/--help */
250                 exit(ret == -1 ? 0 : ret);
251 }
252
253 int main(int argc, char **argv)
254 {
255         subtest_init(argc, argv);
256
257         igt_skip_on_simulation();
258
259         igt_fixture {
260                 const int device = drm_get_card();
261                 struct junk *junk = stuff;
262                 int fd, ret;
263
264                 /* Use drm_open_any to verify device existence */
265                 fd = drm_open_any();
266                 close(fd);
267
268                 do {
269                         int val = -1;
270                         char *path;
271                         ret = asprintf(&path, sysfs_base_path, device, junk->name);
272                         igt_assert(ret != -1);
273                         junk->filp = fopen(path, junk->mode);
274                         igt_require(junk->filp);
275                         setbuf(junk->filp, NULL);
276
277                         val = readval(junk->filp);
278                         igt_assert(val >= 0);
279                         junk++;
280                 } while(junk->name != NULL);
281
282                 origmin = fmin;
283                 origmax = fmax;
284
285                 igt_install_exit_handler(pm_rps_exit_handler);
286         }
287
288         igt_subtest("min-max-config-at-idle")
289                 min_max_config(idle_check);
290
291         igt_exit();
292 }