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