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>
38 #include "intel_gpu_tools.h"
39 #include "intel_bufmgr.h"
40 #include "intel_batchbuffer.h"
41 #include "igt_debugfs.h"
43 static bool verbose = false;
47 static const char sysfs_base_path[] = "/sys/class/drm/card%d/gt_%s_freq_mhz";
58 static int origfreqs[NUMFREQ];
65 { "cur", "r", NULL }, { "min", "rb+", NULL }, { "max", "rb+", NULL }, { "RP0", "r", NULL }, { "RP1", "r", NULL }, { "RPn", "r", NULL }, { NULL, NULL, NULL }
68 static igt_debugfs_t dfs;
70 static int readval(FILE *filp)
76 scanned = fscanf(filp, "%d", &val);
77 igt_assert(scanned == 1);
82 static void read_freqs(int *freqs)
86 for (i = 0; i < NUMFREQ; i++)
87 freqs[i] = readval(stuff[i].filp);
90 static int do_writeval(FILE *filp, int val, int lerrno)
96 ret = fprintf(filp, "%d", val);
99 /* Expecting specific error */
100 igt_assert(ret == EOF && errno == lerrno);
101 igt_assert(readval(filp) == orig);
103 /* Expecting no error */
104 igt_assert(ret != EOF);
105 igt_assert(readval(filp) == val);
110 #define writeval(filp, val) do_writeval(filp, val, 0)
111 #define writeval_inval(filp, val) do_writeval(filp, val, EINVAL)
113 static void setfreq(int val)
115 if (val > readval(stuff[MAX].filp)) {
116 writeval(stuff[MAX].filp, val);
117 writeval(stuff[MIN].filp, val);
119 writeval(stuff[MIN].filp, val);
120 writeval(stuff[MAX].filp, val);
124 static void checkit(const int *freqs)
126 igt_assert(freqs[MIN] <= freqs[MAX]);
127 igt_assert(freqs[CUR] <= freqs[MAX]);
128 igt_assert(freqs[MIN] <= freqs[CUR]);
129 igt_assert(freqs[RPn] <= freqs[MIN]);
130 igt_assert(freqs[MAX] <= freqs[RP0]);
131 igt_assert(freqs[RP1] <= freqs[RP0]);
132 igt_assert(freqs[RPn] <= freqs[RP1]);
133 igt_assert(freqs[RP0] != 0);
134 igt_assert(freqs[RP1] != 0);
137 static void matchit(const int *freqs1, const int *freqs2)
139 igt_assert(freqs1[CUR] == freqs2[CUR]);
140 igt_assert(freqs1[MIN] == freqs2[MIN]);
141 igt_assert(freqs1[MAX] == freqs2[MAX]);
142 igt_assert(freqs1[RP0] == freqs2[RP0]);
143 igt_assert(freqs1[RP1] == freqs2[RP1]);
144 igt_assert(freqs1[RPn] == freqs2[RPn]);
147 static void dumpit(const int *freqs)
151 printf("gt freq (MHz):");
152 for (i = 0; i < NUMFREQ; i++)
153 printf(" %s=%d", stuff[i].name, freqs[i]);
157 #define dump(x) if (verbose) dumpit(x)
158 #define log(...) if (verbose) printf(__VA_ARGS__)
165 static struct load_helper {
168 drm_intel_bufmgr *bufmgr;
169 struct intel_batchbuffer *batch;
170 drm_intel_bo *target_buffer;
174 struct igt_helper_process igt_proc;
177 static void load_helper_signal_handler(int sig)
180 lh.load = lh.load == LOW ? HIGH : LOW;
185 static void emit_store_dword_imm(uint32_t val)
188 struct intel_batchbuffer *batch = lh.batch;
190 cmd = MI_STORE_DWORD_IMM;
192 cmd |= MI_MEM_VIRTUAL;
194 if (intel_gen(lh.devid) >= 8) {
197 OUT_RELOC(lh.target_buffer, I915_GEM_DOMAIN_INSTRUCTION,
198 I915_GEM_DOMAIN_INSTRUCTION, 0);
205 OUT_BATCH(0); /* reserved */
206 OUT_RELOC(lh.target_buffer, I915_GEM_DOMAIN_INSTRUCTION,
207 I915_GEM_DOMAIN_INSTRUCTION, 0);
213 #define LOAD_HELPER_PAUSE_USEC 500
214 static void load_helper_run(enum load load)
216 assert(!lh.igt_proc.running);
218 igt_require(lh.ready == true);
222 igt_fork_helper(&lh.igt_proc) {
225 signal(SIGUSR1, load_helper_signal_handler);
226 signal(SIGUSR2, load_helper_signal_handler);
229 emit_store_dword_imm(val);
230 intel_batchbuffer_flush_on_ring(lh.batch, 0);
233 /* Lower the load by pausing after every submitted
236 usleep(LOAD_HELPER_PAUSE_USEC);
239 /* Map buffer to stall for write completion */
240 drm_intel_bo_map(lh.target_buffer, 0);
241 drm_intel_bo_unmap(lh.target_buffer);
243 log("load helper sent %u dword writes\n", val);
247 static void load_helper_set_load(enum load load)
249 assert(lh.igt_proc.running);
255 kill(lh.igt_proc.pid, SIGUSR2);
258 static void load_helper_stop(void)
260 assert(lh.igt_proc.running);
261 kill(lh.igt_proc.pid, SIGUSR1);
262 igt_wait_helper(&lh.igt_proc);
265 /* The load helper resource is used by only some subtests. We attempt to
266 * initialize in igt_fixture but do our igt_require check only if a
267 * subtest attempts to run it */
268 static void load_helper_init(void)
270 lh.devid = intel_get_drm_devid(drm_fd);
271 lh.has_ppgtt = gem_uses_aliasing_ppgtt(drm_fd);
273 /* MI_STORE_DATA can only use GTT address on gen4+/g33 and needs
274 * snoopable mem on pre-gen6. */
275 if (intel_gen(lh.devid) < 6) {
276 log("load helper init failed: pre-gen6 not supported\n");
280 lh.bufmgr = drm_intel_bufmgr_gem_init(drm_fd, 4096);
282 log("load helper init failed: buffer manager init\n");
285 drm_intel_bufmgr_gem_enable_reuse(lh.bufmgr);
287 lh.batch = intel_batchbuffer_alloc(lh.bufmgr, lh.devid);
289 log("load helper init failed: batch buffer alloc\n");
293 lh.target_buffer = drm_intel_bo_alloc(lh.bufmgr, "target bo",
295 if (!lh.target_buffer) {
296 log("load helper init failed: target buffer alloc\n");
303 static void load_helper_deinit(void)
305 if (lh.igt_proc.running)
308 if (lh.target_buffer)
309 drm_intel_bo_unreference(lh.target_buffer);
312 intel_batchbuffer_free(lh.batch);
315 drm_intel_bufmgr_destroy(lh.bufmgr);
318 static void stop_rings(void)
321 static const char data[] = "0xf";
323 fd = igt_debugfs_open(&dfs, "i915_ring_stop", O_WRONLY);
326 log("injecting ring stop\n");
327 igt_assert(write(fd, data, sizeof(data)) == sizeof(data));
332 static bool rings_stopped(void)
335 static char buf[128];
336 unsigned long long val;
338 fd = igt_debugfs_open(&dfs, "i915_ring_stop", O_RDONLY);
341 igt_assert(read(fd, buf, sizeof(buf)) > 0);
344 sscanf(buf, "%llx", &val);
349 static void min_max_config(void (*check)(void))
351 int fmid = (origfreqs[RPn] + origfreqs[RP0]) / 2;
353 /* hw (and so kernel) currently rounds to 50 MHz ... */
354 fmid = fmid / 50 * 50;
356 log("\nCheck original min and max...\n");
359 log("\nSet min=RPn and max=RP0...\n");
360 writeval(stuff[MIN].filp, origfreqs[RPn]);
361 writeval(stuff[MAX].filp, origfreqs[RP0]);
364 log("\nIncrease min to midpoint...\n");
365 writeval(stuff[MIN].filp, fmid);
368 log("\nIncrease min to RP0...\n");
369 writeval(stuff[MIN].filp, origfreqs[RP0]);
372 log("\nIncrease min above RP0 (invalid)...\n");
373 writeval_inval(stuff[MIN].filp, origfreqs[RP0] + 1000);
376 log("\nDecrease max to RPn (invalid)...\n");
377 writeval_inval(stuff[MAX].filp, origfreqs[RPn]);
380 log("\nDecrease min to midpoint...\n");
381 writeval(stuff[MIN].filp, fmid);
384 log("\nDecrease min to RPn...\n");
385 writeval(stuff[MIN].filp, origfreqs[RPn]);
388 log("\nDecrease min below RPn (invalid)...\n");
389 writeval_inval(stuff[MIN].filp, 0);
392 log("\nDecrease max to midpoint...\n");
393 writeval(stuff[MAX].filp, fmid);
396 log("\nDecrease max to RPn...\n");
397 writeval(stuff[MAX].filp, origfreqs[RPn]);
400 log("\nDecrease max below RPn (invalid)...\n");
401 writeval_inval(stuff[MAX].filp, 0);
404 log("\nIncrease min to RP0 (invalid)...\n");
405 writeval_inval(stuff[MIN].filp, origfreqs[RP0]);
408 log("\nIncrease max to midpoint...\n");
409 writeval(stuff[MAX].filp, fmid);
412 log("\nIncrease max to RP0...\n");
413 writeval(stuff[MAX].filp, origfreqs[RP0]);
416 log("\nIncrease max above RP0 (invalid)...\n");
417 writeval_inval(stuff[MAX].filp, origfreqs[RP0] + 1000);
420 writeval(stuff[MIN].filp, origfreqs[MIN]);
421 writeval(stuff[MAX].filp, origfreqs[MAX]);
424 static void basic_check(void)
433 #define IDLE_WAIT_TIMESTEP_MSEC 100
434 #define IDLE_WAIT_TIMEOUT_MSEC 3000
435 static void idle_check(void)
440 /* Monitor frequencies until cur settles down to min, which should
441 * happen within the allotted time */
446 if (freqs[CUR] == freqs[MIN])
448 usleep(1000 * IDLE_WAIT_TIMESTEP_MSEC);
449 wait += IDLE_WAIT_TIMESTEP_MSEC;
450 } while (wait < IDLE_WAIT_TIMEOUT_MSEC);
452 igt_assert(freqs[CUR] == freqs[MIN]);
453 log("Required %d msec to reach cur=min\n", wait);
456 #define LOADED_WAIT_TIMESTEP_MSEC 100
457 #define LOADED_WAIT_TIMEOUT_MSEC 3000
458 static void loaded_check(void)
463 /* Monitor frequencies until cur increases to max, which should
464 * happen within the allotted time */
469 if (freqs[CUR] == freqs[MAX])
471 usleep(1000 * LOADED_WAIT_TIMESTEP_MSEC);
472 wait += LOADED_WAIT_TIMESTEP_MSEC;
473 } while (wait < LOADED_WAIT_TIMEOUT_MSEC);
475 igt_assert(freqs[CUR] == freqs[MAX]);
476 log("Required %d msec to reach cur=max\n", wait);
479 #define STABILIZE_WAIT_TIMESTEP_MSEC 100
480 #define STABILIZE_WAIT_TIMEOUT_MSEC 2000
481 static void stabilize_check(int *freqs)
488 usleep(1000 * STABILIZE_WAIT_TIMESTEP_MSEC);
489 wait += STABILIZE_WAIT_TIMESTEP_MSEC;
490 } while (wait < STABILIZE_WAIT_TIMEOUT_MSEC);
492 log("Waited %d msec to stabilize cur\n", wait);
495 static void reset(void)
497 int pre_freqs[NUMFREQ];
498 int post_freqs[NUMFREQ];
500 log("Apply low load...\n");
501 load_helper_run(LOW);
502 stabilize_check(pre_freqs);
504 log("Stop rings...\n");
506 while (rings_stopped())
508 log("Ring stop cleared\n");
510 log("Apply high load...\n");
511 load_helper_set_load(HIGH);
514 log("Apply low load...\n");
515 load_helper_set_load(LOW);
516 stabilize_check(post_freqs);
517 matchit(pre_freqs, post_freqs);
519 log("Apply high load...\n");
520 load_helper_set_load(HIGH);
523 log("Removing load...\n");
528 static void pm_rps_exit_handler(int sig)
530 if (origfreqs[MIN] > readval(stuff[MAX].filp)) {
531 writeval(stuff[MAX].filp, origfreqs[MAX]);
532 writeval(stuff[MIN].filp, origfreqs[MIN]);
534 writeval(stuff[MIN].filp, origfreqs[MIN]);
535 writeval(stuff[MAX].filp, origfreqs[MAX]);
538 load_helper_deinit();
542 static int opt_handler(int opt, int opt_index)
555 /* Mod of igt_subtest_init that adds our extra options */
556 static void subtest_init(int argc, char **argv)
558 struct option long_opts[] = {
559 {"verbose", 0, 0, 'v'}
561 const char *help_str = " -v, --verbose";
564 ret = igt_subtest_init_parse_opts(argc, argv, "v", long_opts,
565 help_str, opt_handler);
568 /* exit with no error for -h/--help */
569 exit(ret == -1 ? 0 : ret);
572 int main(int argc, char **argv)
574 subtest_init(argc, argv);
576 igt_skip_on_simulation();
579 const int device = drm_get_card();
580 struct junk *junk = stuff;
583 /* Use drm_open_any to verify device existence */
584 drm_fd = drm_open_any();
589 ret = asprintf(&path, sysfs_base_path, device, junk->name);
590 igt_assert(ret != -1);
591 junk->filp = fopen(path, junk->mode);
592 igt_require(junk->filp);
593 setbuf(junk->filp, NULL);
595 val = readval(junk->filp);
596 igt_assert(val >= 0);
598 } while(junk->name != NULL);
600 read_freqs(origfreqs);
602 igt_install_exit_handler(pm_rps_exit_handler);
606 igt_debugfs_init(&dfs);
609 igt_subtest("basic-api")
610 min_max_config(basic_check);
612 igt_subtest("min-max-config-idle")
613 min_max_config(idle_check);
615 igt_subtest("min-max-config-loaded") {
616 load_helper_run(HIGH);
617 min_max_config(loaded_check);