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>
41 #include "intel_bufmgr.h"
42 #include "intel_batchbuffer.h"
43 #include "intel_chipset.h"
44 #include "igt_debugfs.h"
45 #include "ioctl_wrappers.h"
49 static const char sysfs_base_path[] = "/sys/class/drm/card%d/gt_%s_freq_mhz";
60 static int origfreqs[NUMFREQ];
67 { "cur", "r", NULL }, { "min", "rb+", NULL }, { "max", "rb+", NULL }, { "RP0", "r", NULL }, { "RP1", "r", NULL }, { "RPn", "r", NULL }, { NULL, NULL, NULL }
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_neq(ret, 0);
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 checkit(const int *freqs)
115 igt_assert_lte(freqs[MIN], freqs[MAX]);
116 igt_assert_lte(freqs[CUR], freqs[MAX]);
117 igt_assert_lte(freqs[MIN], freqs[CUR]);
118 igt_assert_lte(freqs[RPn], freqs[MIN]);
119 igt_assert_lte(freqs[MAX], freqs[RP0]);
120 igt_assert_lte(freqs[RP1], freqs[RP0]);
121 igt_assert_lte(freqs[RPn], freqs[RP1]);
122 igt_assert_neq(freqs[RP0], 0);
123 igt_assert_neq(freqs[RP1], 0);
126 static void matchit(const int *freqs1, const int *freqs2)
128 igt_assert_eq(freqs1[CUR], freqs2[CUR]);
129 igt_assert_eq(freqs1[MIN], freqs2[MIN]);
130 igt_assert_eq(freqs1[MAX], freqs2[MAX]);
131 igt_assert_eq(freqs1[RP0], freqs2[RP0]);
132 igt_assert_eq(freqs1[RP1], freqs2[RP1]);
133 igt_assert_eq(freqs1[RPn], freqs2[RPn]);
136 static void dump(const int *freqs)
140 igt_debug("gt freq (MHz):");
141 for (i = 0; i < NUMFREQ; i++)
142 igt_debug(" %s=%d", stuff[i].name, freqs[i]);
152 static struct load_helper {
155 drm_intel_bufmgr *bufmgr;
156 struct intel_batchbuffer *batch;
157 drm_intel_bo *target_buffer;
160 struct igt_helper_process igt_proc;
161 drm_intel_bo *src, *dst;
164 static void load_helper_signal_handler(int sig)
167 lh.load = lh.load == LOW ? HIGH : LOW;
172 static void emit_store_dword_imm(uint32_t val)
175 struct intel_batchbuffer *batch = lh.batch;
177 cmd = MI_STORE_DWORD_IMM;
179 cmd |= MI_MEM_VIRTUAL;
183 if (batch->gen >= 8) {
184 OUT_RELOC(lh.target_buffer, I915_GEM_DOMAIN_INSTRUCTION,
185 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.src,
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_assert(igt_wait_helper(&lh.igt_proc) == 0);
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);
294 drm_intel_bo_unreference(lh.src);
296 drm_intel_bo_unreference(lh.dst);
299 intel_batchbuffer_free(lh.batch);
302 drm_intel_bufmgr_destroy(lh.bufmgr);
305 static void min_max_config(void (*check)(void))
307 int fmid = (origfreqs[RPn] + origfreqs[RP0]) / 2;
309 /* hw (and so kernel) currently rounds to 50 MHz ... */
310 fmid = fmid / 50 * 50;
312 igt_debug("\nCheck original min and max...\n");
315 igt_debug("\nSet min=RPn and max=RP0...\n");
316 writeval(stuff[MIN].filp, origfreqs[RPn]);
317 writeval(stuff[MAX].filp, origfreqs[RP0]);
320 igt_debug("\nIncrease min to midpoint...\n");
321 writeval(stuff[MIN].filp, fmid);
324 igt_debug("\nIncrease min to RP0...\n");
325 writeval(stuff[MIN].filp, origfreqs[RP0]);
328 igt_debug("\nIncrease min above RP0 (invalid)...\n");
329 writeval_inval(stuff[MIN].filp, origfreqs[RP0] + 1000);
332 igt_debug("\nDecrease max to RPn (invalid)...\n");
333 writeval_inval(stuff[MAX].filp, origfreqs[RPn]);
336 igt_debug("\nDecrease min to midpoint...\n");
337 writeval(stuff[MIN].filp, fmid);
340 igt_debug("\nDecrease min to RPn...\n");
341 writeval(stuff[MIN].filp, origfreqs[RPn]);
344 igt_debug("\nDecrease min below RPn (invalid)...\n");
345 writeval_inval(stuff[MIN].filp, 0);
348 igt_debug("\nDecrease max to midpoint...\n");
349 writeval(stuff[MAX].filp, fmid);
352 igt_debug("\nDecrease max to RPn...\n");
353 writeval(stuff[MAX].filp, origfreqs[RPn]);
356 igt_debug("\nDecrease max below RPn (invalid)...\n");
357 writeval_inval(stuff[MAX].filp, 0);
360 igt_debug("\nIncrease min to RP0 (invalid)...\n");
361 writeval_inval(stuff[MIN].filp, origfreqs[RP0]);
364 igt_debug("\nIncrease max to midpoint...\n");
365 writeval(stuff[MAX].filp, fmid);
368 igt_debug("\nIncrease max to RP0...\n");
369 writeval(stuff[MAX].filp, origfreqs[RP0]);
372 igt_debug("\nIncrease max above RP0 (invalid)...\n");
373 writeval_inval(stuff[MAX].filp, origfreqs[RP0] + 1000);
376 writeval(stuff[MIN].filp, origfreqs[MIN]);
377 writeval(stuff[MAX].filp, origfreqs[MAX]);
380 static void basic_check(void)
389 #define IDLE_WAIT_TIMESTEP_MSEC 100
390 #define IDLE_WAIT_TIMEOUT_MSEC 10000
391 static void idle_check(void)
396 /* Monitor frequencies until cur settles down to min, which should
397 * happen within the allotted time */
402 if (freqs[CUR] == freqs[MIN])
404 usleep(1000 * IDLE_WAIT_TIMESTEP_MSEC);
405 wait += IDLE_WAIT_TIMESTEP_MSEC;
406 } while (wait < IDLE_WAIT_TIMEOUT_MSEC);
408 igt_assert_eq(freqs[CUR], freqs[MIN]);
409 igt_debug("Required %d msec to reach cur=min\n", wait);
412 #define LOADED_WAIT_TIMESTEP_MSEC 100
413 #define LOADED_WAIT_TIMEOUT_MSEC 3000
414 static void loaded_check(void)
419 /* Monitor frequencies until cur increases to max, which should
420 * happen within the allotted time */
425 if (freqs[CUR] == freqs[MAX])
427 usleep(1000 * LOADED_WAIT_TIMESTEP_MSEC);
428 wait += LOADED_WAIT_TIMESTEP_MSEC;
429 } while (wait < LOADED_WAIT_TIMEOUT_MSEC);
431 igt_assert_eq(freqs[CUR], freqs[MAX]);
432 igt_debug("Required %d msec to reach cur=max\n", wait);
435 #define STABILIZE_WAIT_TIMESTEP_MSEC 100
436 #define STABILIZE_WAIT_TIMEOUT_MSEC 10000
437 static void stabilize_check(int *freqs)
444 usleep(1000 * STABILIZE_WAIT_TIMESTEP_MSEC);
445 wait += STABILIZE_WAIT_TIMESTEP_MSEC;
446 } while (wait < STABILIZE_WAIT_TIMEOUT_MSEC);
448 igt_debug("Waited %d msec to stabilize cur\n", wait);
451 static void reset(void)
453 int pre_freqs[NUMFREQ];
454 int post_freqs[NUMFREQ];
457 * quiescent_gpu upsets the gpu and makes it get pegged to max somehow.
462 igt_debug("Apply low load...\n");
463 load_helper_run(LOW);
464 stabilize_check(pre_freqs);
466 igt_debug("Stop rings...\n");
467 igt_set_stop_rings(STOP_RING_DEFAULTS);
468 while (igt_get_stop_rings())
470 igt_debug("Ring stop cleared\n");
472 igt_debug("Apply high load...\n");
473 load_helper_set_load(HIGH);
476 igt_debug("Apply low load...\n");
477 load_helper_set_load(LOW);
478 stabilize_check(post_freqs);
479 matchit(pre_freqs, post_freqs);
481 igt_debug("Apply high load...\n");
482 load_helper_set_load(HIGH);
485 igt_debug("Removing load...\n");
490 static void blocking(void)
492 int pre_freqs[NUMFREQ];
493 int post_freqs[NUMFREQ];
495 int fd = drm_open_any();
499 * quiescent_gpu upsets the gpu and makes it get pegged to max somehow.
504 igt_debug("Apply low load...\n");
505 load_helper_run(LOW);
506 stabilize_check(pre_freqs);
511 igt_debug("Kick gpu hard ...\n");
512 /* This relies on the blocking waits in quiescent_gpu and the kernel
513 * boost logic to ramp the gpu to full load. */
514 gem_quiescent_gpu(fd);
515 gem_quiescent_gpu(fd);
517 igt_debug("Apply low load again...\n");
518 load_helper_run(LOW);
519 stabilize_check(post_freqs);
521 matchit(pre_freqs, post_freqs);
523 igt_debug("Removing load...\n");
527 static void pm_rps_exit_handler(int sig)
529 if (origfreqs[MIN] > readval(stuff[MAX].filp)) {
530 writeval(stuff[MAX].filp, origfreqs[MAX]);
531 writeval(stuff[MIN].filp, origfreqs[MIN]);
533 writeval(stuff[MIN].filp, origfreqs[MIN]);
534 writeval(stuff[MAX].filp, origfreqs[MAX]);
537 load_helper_deinit();
543 igt_skip_on_simulation();
546 const int device = drm_get_card();
547 struct junk *junk = stuff;
550 /* Use drm_open_any to verify device existence */
551 drm_fd = drm_open_any();
556 ret = asprintf(&path, sysfs_base_path, device, junk->name);
557 igt_assert(ret != -1);
558 junk->filp = fopen(path, junk->mode);
559 igt_require(junk->filp);
560 setbuf(junk->filp, NULL);
562 val = readval(junk->filp);
563 igt_assert(val >= 0);
565 } while(junk->name != NULL);
567 read_freqs(origfreqs);
569 igt_install_exit_handler(pm_rps_exit_handler);
574 igt_subtest("basic-api")
575 min_max_config(basic_check);
577 igt_subtest("min-max-config-idle")
578 min_max_config(idle_check);
580 igt_subtest("min-max-config-loaded") {
581 load_helper_run(HIGH);
582 min_max_config(loaded_check);
589 igt_subtest("blocking")