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>
35 static bool verbose = false;
36 static int origmin, origmax;
38 #define restore_assert(COND) do { \
40 writeval(stuff[MIN].filp, origmin); \
41 writeval(stuff[MAX].filp, origmax); \
46 static const char sysfs_base_path[] = "/sys/class/drm/card%d/gt_%s_freq_mhz";
61 { "cur", "r", NULL }, { "min", "rb+", NULL }, { "max", "rb+", NULL }, { "RP0", "r", NULL }, { "RP1", "r", NULL }, { "RPn", "r", NULL }, { NULL, NULL, NULL }
64 static int readval(FILE *filp)
71 scanned = fscanf(filp, "%d", &val);
72 igt_assert(scanned == 1);
77 static int do_writeval(FILE *filp, int val, int lerrno)
79 /* Must write twice to sysfs since the first one simply calculates the size and won't return the error */
82 ret = fprintf(filp, "%d", val);
84 ret = fprintf(filp, "%d", val);
86 igt_assert(errno = lerrno);
90 #define writeval(filp, val) do_writeval(filp, val, 0)
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))
99 static void setfreq(int val)
101 writeval(stuff[MIN].filp, val);
102 writeval(stuff[MAX].filp, val);
105 static void checkit(void)
107 restore_assert(fmin <= fmax);
108 restore_assert(fcur <= fmax);
109 restore_assert(fmin <= fcur);
110 restore_assert(frpn <= fmin);
111 restore_assert(fmax <= frp0);
112 restore_assert(frp1 <= frp0);
113 restore_assert(frpn <= frp1);
114 restore_assert(frp0 != 0);
115 restore_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);
132 const int device = drm_get_card();
133 struct junk *junk = stuff;
136 igt_skip_on_simulation();
138 /* Use drm_open_any to verify device existence */
145 ret = asprintf(&path, sysfs_base_path, device, junk->name);
146 igt_assert(ret != -1);
147 junk->filp = fopen(path, junk->mode);
148 igt_require(junk->filp);
150 val = readval(junk->filp);
151 igt_assert(val >= 0);
153 } while(junk->name != NULL);
159 printf("Original min = %d\nOriginal max = %d\n", origmin, origmax);
168 restore_assert(fcur == fmin);
172 restore_assert(fcur == fmax);
175 /* And some errors */
176 writeval(stuff[MIN].filp, frpn - 1);
177 writeval(stuff[MAX].filp, frp0 + 1000);
180 writeval(stuff[MIN].filp, fmax + 1000);
181 writeval(stuff[MAX].filp, fmin - 1);
184 do_writeval(stuff[MIN].filp, 0x11111110, EINVAL);
185 do_writeval(stuff[MAX].filp, 0, EINVAL);
187 writeval(stuff[MIN].filp, origmin);
188 writeval(stuff[MAX].filp, origmax);