pm_rps: Convert to subtest structure
[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 "drmtest.h"
35
36 static bool verbose = false;
37 static int origmin, origmax;
38
39 static const char sysfs_base_path[] = "/sys/class/drm/card%d/gt_%s_freq_mhz";
40 enum {
41         CUR,
42         MIN,
43         MAX,
44         RP0,
45         RP1,
46         RPn
47 };
48
49 struct junk {
50         const char *name;
51         const char *mode;
52         FILE *filp;
53 } stuff[] = {
54         { "cur", "r", NULL }, { "min", "rb+", NULL }, { "max", "rb+", NULL }, { "RP0", "r", NULL }, { "RP1", "r", NULL }, { "RPn", "r", NULL }, { NULL, NULL, NULL }
55 };
56
57 static int readval(FILE *filp)
58 {
59         int val;
60         int scanned;
61
62         rewind(filp);
63         scanned = fscanf(filp, "%d", &val);
64         igt_assert(scanned == 1);
65
66         return val;
67 }
68
69 static int do_writeval(FILE *filp, int val, int lerrno)
70 {
71         int ret, orig;
72
73         orig = readval(filp);
74         rewind(filp);
75         ret = fprintf(filp, "%d", val);
76
77         if (lerrno) {
78                 /* Expecting specific error */
79                 igt_assert(ret == EOF && errno == lerrno);
80                 igt_assert(readval(filp) == orig);
81         } else {
82                 /* Expecting no error */
83                 igt_assert(ret != EOF);
84                 igt_assert(readval(filp) == val);
85         }
86
87         return ret;
88 }
89 #define writeval(filp, val) do_writeval(filp, val, 0)
90 #define writeval_inval(filp, val) do_writeval(filp, val, EINVAL)
91
92 #define fcur (readval(stuff[CUR].filp))
93 #define fmin (readval(stuff[MIN].filp))
94 #define fmax (readval(stuff[MAX].filp))
95 #define frp0 (readval(stuff[RP0].filp))
96 #define frp1 (readval(stuff[RP1].filp))
97 #define frpn (readval(stuff[RPn].filp))
98
99 static void setfreq(int val)
100 {
101         if (val > fmax) {
102                 writeval(stuff[MAX].filp, val);
103                 writeval(stuff[MIN].filp, val);
104         } else {
105                 writeval(stuff[MIN].filp, val);
106                 writeval(stuff[MAX].filp, val);
107         }
108 }
109
110 static void checkit(void)
111 {
112         igt_assert(fmin <= fmax);
113         igt_assert(fcur <= fmax);
114         igt_assert(fmin <= fcur);
115         igt_assert(frpn <= fmin);
116         igt_assert(fmax <= frp0);
117         igt_assert(frp1 <= frp0);
118         igt_assert(frpn <= frp1);
119         igt_assert(frp0 != 0);
120         igt_assert(frp1 != 0);
121 }
122
123 static void dumpit(void)
124 {
125         struct junk *junk = stuff;
126         do {
127                 printf("gt frequency %s (MHz):  %d\n", junk->name, readval(junk->filp));
128                 junk++;
129         } while(junk->name != NULL);
130
131         printf("\n");
132 }
133
134 static void pm_rps_exit_handler(int sig)
135 {
136         if (origmin > fmax) {
137                 writeval(stuff[MAX].filp, origmax);
138                 writeval(stuff[MIN].filp, origmin);
139         } else {
140                 writeval(stuff[MIN].filp, origmin);
141                 writeval(stuff[MAX].filp, origmax);
142         }
143 }
144
145 igt_main
146 {
147         igt_skip_on_simulation();
148
149         igt_fixture {
150                 const int device = drm_get_card();
151                 struct junk *junk = stuff;
152                 int fd, ret;
153
154                 /* Use drm_open_any to verify device existence */
155                 fd = drm_open_any();
156                 close(fd);
157
158                 do {
159                         int val = -1;
160                         char *path;
161                         ret = asprintf(&path, sysfs_base_path, device, junk->name);
162                         igt_assert(ret != -1);
163                         junk->filp = fopen(path, junk->mode);
164                         igt_require(junk->filp);
165                         setbuf(junk->filp, NULL);
166
167                         val = readval(junk->filp);
168                         igt_assert(val >= 0);
169                         junk++;
170                 } while(junk->name != NULL);
171
172                 origmin = fmin;
173                 origmax = fmax;
174
175                 igt_install_exit_handler(pm_rps_exit_handler);
176         }
177
178         igt_subtest("min-max-config-at-idle") {
179                 if (verbose)
180                         printf("Original min = %d\nOriginal max = %d\n",
181                                 origmin, origmax);
182
183                 if (verbose)
184                         dumpit();
185
186                 checkit();
187                 setfreq(origmin);
188                 if (verbose)
189                         dumpit();
190                 igt_assert(fcur == fmin);
191                 setfreq(origmax);
192                 if (verbose)
193                         dumpit();
194                 igt_assert(fcur == fmax);
195                 checkit();
196
197                 /* And some errors */
198                 writeval_inval(stuff[MIN].filp, frpn - 1);
199                 writeval_inval(stuff[MAX].filp, frp0 + 1000);
200                 checkit();
201
202                 writeval_inval(stuff[MIN].filp, fmax + 1000);
203                 writeval_inval(stuff[MAX].filp, fmin - 1);
204                 checkit();
205
206                 writeval_inval(stuff[MIN].filp, 0x11111110);
207                 writeval_inval(stuff[MAX].filp, 0);
208
209                 writeval(stuff[MIN].filp, origmin);
210                 writeval(stuff[MAX].filp, origmax);
211         }
212 }