pm_rps: Use igt exit handler for restore
[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;
72         rewind(filp);
73         ret = fprintf(filp, "%d", val);
74         if (lerrno) {
75                 /* Expecting specific error */
76                 igt_assert(ret == EOF && errno == lerrno);
77         } else {
78                 /* Expecting no error */
79                 igt_assert(ret != EOF);
80         }
81
82         return ret;
83 }
84 #define writeval(filp, val) do_writeval(filp, val, 0)
85 #define writeval_inval(filp, val) do_writeval(filp, val, EINVAL)
86
87 #define fcur (readval(stuff[CUR].filp))
88 #define fmin (readval(stuff[MIN].filp))
89 #define fmax (readval(stuff[MAX].filp))
90 #define frp0 (readval(stuff[RP0].filp))
91 #define frp1 (readval(stuff[RP1].filp))
92 #define frpn (readval(stuff[RPn].filp))
93
94 static void setfreq(int val)
95 {
96         if (val > fmax) {
97                 writeval(stuff[MAX].filp, val);
98                 writeval(stuff[MIN].filp, val);
99         } else {
100                 writeval(stuff[MIN].filp, val);
101                 writeval(stuff[MAX].filp, val);
102         }
103 }
104
105 static void checkit(void)
106 {
107         igt_assert(fmin <= fmax);
108         igt_assert(fcur <= fmax);
109         igt_assert(fmin <= fcur);
110         igt_assert(frpn <= fmin);
111         igt_assert(fmax <= frp0);
112         igt_assert(frp1 <= frp0);
113         igt_assert(frpn <= frp1);
114         igt_assert(frp0 != 0);
115         igt_assert(frp1 != 0);
116 }
117
118 static void dumpit(void)
119 {
120         struct junk *junk = stuff;
121         do {
122                 printf("gt frequency %s (MHz):  %d\n", junk->name, readval(junk->filp));
123                 junk++;
124         } while(junk->name != NULL);
125
126         printf("\n");
127 }
128
129 static void pm_rps_exit_handler(int sig)
130 {
131         if (origmin > fmax) {
132                 writeval(stuff[MAX].filp, origmax);
133                 writeval(stuff[MIN].filp, origmin);
134         } else {
135                 writeval(stuff[MIN].filp, origmin);
136                 writeval(stuff[MAX].filp, origmax);
137         }
138 }
139
140 igt_simple_main
141 {
142         const int device = drm_get_card();
143         struct junk *junk = stuff;
144         int fd, ret;
145
146         igt_skip_on_simulation();
147
148         /* Use drm_open_any to verify device existence */
149         fd = drm_open_any();
150         close(fd);
151
152         do {
153                 int val = -1;
154                 char *path;
155                 ret = asprintf(&path, sysfs_base_path, device, junk->name);
156                 igt_assert(ret != -1);
157                 junk->filp = fopen(path, junk->mode);
158                 igt_require(junk->filp);
159                 setbuf(junk->filp, NULL);
160
161                 val = readval(junk->filp);
162                 igt_assert(val >= 0);
163                 junk++;
164         } while(junk->name != NULL);
165
166         origmin = fmin;
167         origmax = fmax;
168
169         igt_install_exit_handler(pm_rps_exit_handler);
170
171         if (verbose)
172                 printf("Original min = %d\nOriginal max = %d\n", origmin, origmax);
173
174         if (verbose)
175                 dumpit();
176
177         checkit();
178         setfreq(origmin);
179         if (verbose)
180                 dumpit();
181         igt_assert(fcur == fmin);
182         setfreq(origmax);
183         if (verbose)
184                 dumpit();
185         igt_assert(fcur == fmax);
186         checkit();
187
188         /* And some errors */
189         writeval_inval(stuff[MIN].filp, frpn - 1);
190         writeval_inval(stuff[MAX].filp, frp0 + 1000);
191         checkit();
192
193         writeval_inval(stuff[MIN].filp, fmax + 1000);
194         writeval_inval(stuff[MAX].filp, fmin - 1);
195         checkit();
196
197         writeval_inval(stuff[MIN].filp, 0x11111110);
198         writeval_inval(stuff[MAX].filp, 0);
199
200         writeval(stuff[MIN].filp, origmin);
201         writeval(stuff[MAX].filp, origmax);
202 }