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"
45 static const char sysfs_base_path[] = "/sys/class/drm/card%d/gt_%s_freq_mhz";
56 static int origfreqs[NUMFREQ];
63 { "cur", "r", NULL }, { "min", "rb+", NULL }, { "max", "rb+", NULL }, { "RP0", "r", NULL }, { "RP1", "r", NULL }, { "RPn", "r", NULL }, { NULL, NULL, NULL }
66 static int readval(FILE *filp)
72 scanned = fscanf(filp, "%d", &val);
73 igt_assert(scanned == 1);
78 static void read_freqs(int *freqs)
82 for (i = 0; i < NUMFREQ; i++)
83 freqs[i] = readval(stuff[i].filp);
86 static int do_writeval(FILE *filp, int val, int lerrno)
92 ret = fprintf(filp, "%d", val);
95 /* Expecting specific error */
96 igt_assert(ret == EOF && errno == lerrno);
97 igt_assert(readval(filp) == orig);
99 /* Expecting no error */
100 igt_assert(ret != EOF);
101 igt_assert(readval(filp) == val);
106 #define writeval(filp, val) do_writeval(filp, val, 0)
107 #define writeval_inval(filp, val) do_writeval(filp, val, EINVAL)
109 static void checkit(const int *freqs)
111 igt_assert_cmpint(freqs[MIN], <=, freqs[MAX]);
112 igt_assert_cmpint(freqs[CUR], <=, freqs[MAX]);
113 igt_assert_cmpint(freqs[MIN], <=, freqs[CUR]);
114 igt_assert_cmpint(freqs[RPn], <=, freqs[MIN]);
115 igt_assert_cmpint(freqs[MAX], <=, freqs[RP0]);
116 igt_assert_cmpint(freqs[RP1], <=, freqs[RP0]);
117 igt_assert_cmpint(freqs[RPn], <=, freqs[RP1]);
118 igt_assert(freqs[RP0] != 0);
119 igt_assert(freqs[RP1] != 0);
122 static void matchit(const int *freqs1, const int *freqs2)
124 igt_assert_cmpint(freqs1[CUR], ==, freqs2[CUR]);
125 igt_assert_cmpint(freqs1[MIN], ==, freqs2[MIN]);
126 igt_assert_cmpint(freqs1[MAX], ==, freqs2[MAX]);
127 igt_assert_cmpint(freqs1[RP0], ==, freqs2[RP0]);
128 igt_assert_cmpint(freqs1[RP1], ==, freqs2[RP1]);
129 igt_assert_cmpint(freqs1[RPn], ==, freqs2[RPn]);
132 static void dump(const int *freqs)
136 igt_debug("gt freq (MHz):");
137 for (i = 0; i < NUMFREQ; i++)
138 igt_debug(" %s=%d", stuff[i].name, freqs[i]);
148 static struct load_helper {
151 drm_intel_bufmgr *bufmgr;
152 struct intel_batchbuffer *batch;
153 drm_intel_bo *target_buffer;
156 struct igt_helper_process igt_proc;
157 drm_intel_bo *src, *dst;
160 static void load_helper_signal_handler(int sig)
163 lh.load = lh.load == LOW ? HIGH : LOW;
168 static void emit_store_dword_imm(uint32_t val)
171 struct intel_batchbuffer *batch = lh.batch;
173 cmd = MI_STORE_DWORD_IMM;
175 cmd |= MI_MEM_VIRTUAL;
177 if (intel_gen(lh.devid) >= 8) {
180 OUT_RELOC(lh.target_buffer, I915_GEM_DOMAIN_INSTRUCTION,
181 I915_GEM_DOMAIN_INSTRUCTION, 0);
188 OUT_BATCH(0); /* reserved */
189 OUT_RELOC(lh.target_buffer, I915_GEM_DOMAIN_INSTRUCTION,
190 I915_GEM_DOMAIN_INSTRUCTION, 0);
196 #define LOAD_HELPER_PAUSE_USEC 500
197 #define LOAD_HELPER_BO_SIZE (16*1024*1024)
198 static void load_helper_set_load(enum load load)
200 igt_assert(lh.igt_proc.running);
206 kill(lh.igt_proc.pid, SIGUSR2);
209 static void load_helper_run(enum load load)
212 * FIXME fork helpers won't get cleaned up when started from within a
213 * subtest, so handle the case where it sticks around a bit too long.
215 if (lh.igt_proc.running) {
216 load_helper_set_load(load);
222 igt_fork_helper(&lh.igt_proc) {
225 signal(SIGUSR1, load_helper_signal_handler);
226 signal(SIGUSR2, load_helper_signal_handler);
230 intel_copy_bo(lh.batch, lh.dst, lh.dst,
231 LOAD_HELPER_BO_SIZE);
233 emit_store_dword_imm(val);
234 intel_batchbuffer_flush_on_ring(lh.batch, 0);
237 /* Lower the load by pausing after every submitted
240 usleep(LOAD_HELPER_PAUSE_USEC);
243 /* Map buffer to stall for write completion */
244 drm_intel_bo_map(lh.target_buffer, 0);
245 drm_intel_bo_unmap(lh.target_buffer);
247 igt_debug("load helper sent %u dword writes\n", val);
251 static void load_helper_stop(void)
253 kill(lh.igt_proc.pid, SIGUSR1);
254 igt_wait_helper(&lh.igt_proc);
257 static void load_helper_init(void)
259 lh.devid = intel_get_drm_devid(drm_fd);
260 lh.has_ppgtt = gem_uses_aliasing_ppgtt(drm_fd);
262 /* MI_STORE_DATA can only use GTT address on gen4+/g33 and needs
263 * snoopable mem on pre-gen6. Hence load-helper only works on gen6+, but
264 * that's also all we care about for the rps testcase*/
265 igt_assert(intel_gen(lh.devid) >= 6);
266 lh.bufmgr = drm_intel_bufmgr_gem_init(drm_fd, 4096);
267 igt_assert(lh.bufmgr);
269 drm_intel_bufmgr_gem_enable_reuse(lh.bufmgr);
271 lh.batch = intel_batchbuffer_alloc(lh.bufmgr, lh.devid);
272 igt_assert(lh.batch);
274 lh.target_buffer = drm_intel_bo_alloc(lh.bufmgr, "target bo",
276 igt_assert(lh.target_buffer);
278 lh.dst = drm_intel_bo_alloc(lh.bufmgr, "dst bo",
279 LOAD_HELPER_BO_SIZE, 4096);
281 lh.src = drm_intel_bo_alloc(lh.bufmgr, "src bo",
282 LOAD_HELPER_BO_SIZE, 4096);
286 static void load_helper_deinit(void)
288 if (lh.igt_proc.running)
291 if (lh.target_buffer)
292 drm_intel_bo_unreference(lh.target_buffer);
295 intel_batchbuffer_free(lh.batch);
298 drm_intel_bufmgr_destroy(lh.bufmgr);
301 static void stop_rings(void)
304 static const char data[] = "0xf";
306 fd = igt_debugfs_open("i915_ring_stop", O_WRONLY);
309 igt_debug("injecting ring stop\n");
310 igt_assert(write(fd, data, sizeof(data)) == sizeof(data));
315 static bool rings_stopped(void)
318 static char buf[128];
319 unsigned long long val;
321 fd = igt_debugfs_open("i915_ring_stop", O_RDONLY);
324 igt_assert(read(fd, buf, sizeof(buf)) > 0);
327 sscanf(buf, "%llx", &val);
332 static void min_max_config(void (*check)(void))
334 int fmid = (origfreqs[RPn] + origfreqs[RP0]) / 2;
336 /* hw (and so kernel) currently rounds to 50 MHz ... */
337 fmid = fmid / 50 * 50;
339 igt_debug("\nCheck original min and max...\n");
342 igt_debug("\nSet min=RPn and max=RP0...\n");
343 writeval(stuff[MIN].filp, origfreqs[RPn]);
344 writeval(stuff[MAX].filp, origfreqs[RP0]);
347 igt_debug("\nIncrease min to midpoint...\n");
348 writeval(stuff[MIN].filp, fmid);
351 igt_debug("\nIncrease min to RP0...\n");
352 writeval(stuff[MIN].filp, origfreqs[RP0]);
355 igt_debug("\nIncrease min above RP0 (invalid)...\n");
356 writeval_inval(stuff[MIN].filp, origfreqs[RP0] + 1000);
359 igt_debug("\nDecrease max to RPn (invalid)...\n");
360 writeval_inval(stuff[MAX].filp, origfreqs[RPn]);
363 igt_debug("\nDecrease min to midpoint...\n");
364 writeval(stuff[MIN].filp, fmid);
367 igt_debug("\nDecrease min to RPn...\n");
368 writeval(stuff[MIN].filp, origfreqs[RPn]);
371 igt_debug("\nDecrease min below RPn (invalid)...\n");
372 writeval_inval(stuff[MIN].filp, 0);
375 igt_debug("\nDecrease max to midpoint...\n");
376 writeval(stuff[MAX].filp, fmid);
379 igt_debug("\nDecrease max to RPn...\n");
380 writeval(stuff[MAX].filp, origfreqs[RPn]);
383 igt_debug("\nDecrease max below RPn (invalid)...\n");
384 writeval_inval(stuff[MAX].filp, 0);
387 igt_debug("\nIncrease min to RP0 (invalid)...\n");
388 writeval_inval(stuff[MIN].filp, origfreqs[RP0]);
391 igt_debug("\nIncrease max to midpoint...\n");
392 writeval(stuff[MAX].filp, fmid);
395 igt_debug("\nIncrease max to RP0...\n");
396 writeval(stuff[MAX].filp, origfreqs[RP0]);
399 igt_debug("\nIncrease max above RP0 (invalid)...\n");
400 writeval_inval(stuff[MAX].filp, origfreqs[RP0] + 1000);
403 writeval(stuff[MIN].filp, origfreqs[MIN]);
404 writeval(stuff[MAX].filp, origfreqs[MAX]);
407 static void basic_check(void)
416 #define IDLE_WAIT_TIMESTEP_MSEC 100
417 #define IDLE_WAIT_TIMEOUT_MSEC 10000
418 static void idle_check(void)
423 /* Monitor frequencies until cur settles down to min, which should
424 * happen within the allotted time */
429 if (freqs[CUR] == freqs[MIN])
431 usleep(1000 * IDLE_WAIT_TIMESTEP_MSEC);
432 wait += IDLE_WAIT_TIMESTEP_MSEC;
433 } while (wait < IDLE_WAIT_TIMEOUT_MSEC);
435 igt_assert_cmpint(freqs[CUR], ==, freqs[MIN]);
436 igt_debug("Required %d msec to reach cur=min\n", wait);
439 #define LOADED_WAIT_TIMESTEP_MSEC 100
440 #define LOADED_WAIT_TIMEOUT_MSEC 3000
441 static void loaded_check(void)
446 /* Monitor frequencies until cur increases to max, which should
447 * happen within the allotted time */
452 if (freqs[CUR] == freqs[MAX])
454 usleep(1000 * LOADED_WAIT_TIMESTEP_MSEC);
455 wait += LOADED_WAIT_TIMESTEP_MSEC;
456 } while (wait < LOADED_WAIT_TIMEOUT_MSEC);
458 igt_assert_cmpint(freqs[CUR], ==, freqs[MAX]);
459 igt_debug("Required %d msec to reach cur=max\n", wait);
462 #define STABILIZE_WAIT_TIMESTEP_MSEC 100
463 #define STABILIZE_WAIT_TIMEOUT_MSEC 10000
464 static void stabilize_check(int *freqs)
471 usleep(1000 * STABILIZE_WAIT_TIMESTEP_MSEC);
472 wait += STABILIZE_WAIT_TIMESTEP_MSEC;
473 } while (wait < STABILIZE_WAIT_TIMEOUT_MSEC);
475 igt_debug("Waited %d msec to stabilize cur\n", wait);
478 static void reset(void)
480 int pre_freqs[NUMFREQ];
481 int post_freqs[NUMFREQ];
483 igt_debug("Apply low load...\n");
484 load_helper_run(LOW);
485 stabilize_check(pre_freqs);
487 igt_debug("Stop rings...\n");
489 while (rings_stopped())
491 igt_debug("Ring stop cleared\n");
493 igt_debug("Apply high load...\n");
494 load_helper_set_load(HIGH);
497 igt_debug("Apply low load...\n");
498 load_helper_set_load(LOW);
499 stabilize_check(post_freqs);
500 matchit(pre_freqs, post_freqs);
502 igt_debug("Apply high load...\n");
503 load_helper_set_load(HIGH);
506 igt_debug("Removing load...\n");
511 static void pm_rps_exit_handler(int sig)
513 if (origfreqs[MIN] > readval(stuff[MAX].filp)) {
514 writeval(stuff[MAX].filp, origfreqs[MAX]);
515 writeval(stuff[MIN].filp, origfreqs[MIN]);
517 writeval(stuff[MIN].filp, origfreqs[MIN]);
518 writeval(stuff[MAX].filp, origfreqs[MAX]);
521 load_helper_deinit();
527 igt_skip_on_simulation();
530 const int device = drm_get_card();
531 struct junk *junk = stuff;
534 /* Use drm_open_any to verify device existence */
535 drm_fd = drm_open_any();
540 ret = asprintf(&path, sysfs_base_path, device, junk->name);
541 igt_assert(ret != -1);
542 junk->filp = fopen(path, junk->mode);
543 igt_require(junk->filp);
544 setbuf(junk->filp, NULL);
546 val = readval(junk->filp);
547 igt_assert(val >= 0);
549 } while(junk->name != NULL);
551 read_freqs(origfreqs);
553 igt_install_exit_handler(pm_rps_exit_handler);
558 igt_subtest("basic-api")
559 min_max_config(basic_check);
561 igt_subtest("min-max-config-idle")
562 min_max_config(idle_check);
564 igt_subtest("min-max-config-loaded") {
565 load_helper_run(HIGH);
566 min_max_config(loaded_check);