2 * Copyright © 2012 Intel Corporation
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:
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
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
24 * Ben Widawsky <ben@bwidawsk.net>
25 * Jeff McGee <jeff.mcgee@intel.com>
36 static bool verbose = false;
37 static int origmin, origmax;
39 static const char sysfs_base_path[] = "/sys/class/drm/card%d/gt_%s_freq_mhz";
54 { "cur", "r", NULL }, { "min", "rb+", NULL }, { "max", "rb+", NULL }, { "RP0", "r", NULL }, { "RP1", "r", NULL }, { "RPn", "r", NULL }, { NULL, NULL, NULL }
57 static int readval(FILE *filp)
63 scanned = fscanf(filp, "%d", &val);
64 igt_assert(scanned == 1);
69 static int do_writeval(FILE *filp, int val, int lerrno)
73 ret = fprintf(filp, "%d", val);
75 /* Expecting specific error */
76 igt_assert(ret == EOF && errno == lerrno);
78 /* Expecting no error */
79 igt_assert(ret != EOF);
84 #define writeval(filp, val) do_writeval(filp, val, 0)
85 #define writeval_inval(filp, val) do_writeval(filp, val, EINVAL)
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))
94 static void setfreq(int val)
97 writeval(stuff[MAX].filp, val);
98 writeval(stuff[MIN].filp, val);
100 writeval(stuff[MIN].filp, val);
101 writeval(stuff[MAX].filp, val);
105 static void checkit(void)
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);
118 static void dumpit(void)
120 struct junk *junk = stuff;
122 printf("gt frequency %s (MHz): %d\n", junk->name, readval(junk->filp));
124 } while(junk->name != NULL);
129 static void pm_rps_exit_handler(int sig)
131 if (origmin > fmax) {
132 writeval(stuff[MAX].filp, origmax);
133 writeval(stuff[MIN].filp, origmin);
135 writeval(stuff[MIN].filp, origmin);
136 writeval(stuff[MAX].filp, origmax);
142 const int device = drm_get_card();
143 struct junk *junk = stuff;
146 igt_skip_on_simulation();
148 /* Use drm_open_any to verify device existence */
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);
161 val = readval(junk->filp);
162 igt_assert(val >= 0);
164 } while(junk->name != NULL);
169 igt_install_exit_handler(pm_rps_exit_handler);
172 printf("Original min = %d\nOriginal max = %d\n", origmin, origmax);
181 igt_assert(fcur == fmin);
185 igt_assert(fcur == fmax);
188 /* And some errors */
189 writeval_inval(stuff[MIN].filp, frpn - 1);
190 writeval_inval(stuff[MAX].filp, frp0 + 1000);
193 writeval_inval(stuff[MIN].filp, fmax + 1000);
194 writeval_inval(stuff[MAX].filp, fmin - 1);
197 writeval_inval(stuff[MIN].filp, 0x11111110);
198 writeval_inval(stuff[MAX].filp, 0);
200 writeval(stuff[MIN].filp, origmin);
201 writeval(stuff[MAX].filp, origmax);