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 #define restore_assert(COND) do { \
41 writeval(stuff[MIN].filp, origmin); \
42 writeval(stuff[MAX].filp, origmax); \
47 static const char sysfs_base_path[] = "/sys/class/drm/card%d/gt_%s_freq_mhz";
62 { "cur", "r", NULL }, { "min", "rb+", NULL }, { "max", "rb+", NULL }, { "RP0", "r", NULL }, { "RP1", "r", NULL }, { "RPn", "r", NULL }, { NULL, NULL, NULL }
65 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)
81 ret = fprintf(filp, "%d", val);
83 /* Expecting specific error */
84 igt_assert(ret == EOF && errno == lerrno);
86 /* Expecting no error */
87 igt_assert(ret != EOF);
92 #define writeval(filp, val) do_writeval(filp, val, 0)
93 #define writeval_inval(filp, val) do_writeval(filp, val, EINVAL)
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))
102 static void setfreq(int val)
105 writeval(stuff[MAX].filp, val);
106 writeval(stuff[MIN].filp, val);
108 writeval(stuff[MIN].filp, val);
109 writeval(stuff[MAX].filp, val);
113 static void checkit(void)
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);
126 static void dumpit(void)
128 struct junk *junk = stuff;
130 printf("gt frequency %s (MHz): %d\n", junk->name, readval(junk->filp));
132 } while(junk->name != NULL);
140 const int device = drm_get_card();
141 struct junk *junk = stuff;
144 igt_skip_on_simulation();
146 /* Use drm_open_any to verify device existence */
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);
159 val = readval(junk->filp);
160 igt_assert(val >= 0);
162 } while(junk->name != NULL);
168 printf("Original min = %d\nOriginal max = %d\n", origmin, origmax);
177 restore_assert(fcur == fmin);
181 restore_assert(fcur == fmax);
184 /* And some errors */
185 writeval_inval(stuff[MIN].filp, frpn - 1);
186 writeval_inval(stuff[MAX].filp, frp0 + 1000);
189 writeval_inval(stuff[MIN].filp, fmax + 1000);
190 writeval_inval(stuff[MAX].filp, fmin - 1);
193 writeval_inval(stuff[MIN].filp, 0x11111110);
194 writeval_inval(stuff[MAX].filp, 0);
196 writeval(stuff[MIN].filp, origmin);
197 writeval(stuff[MAX].filp, origmax);