tests/pm_rps: remove setfreq
[platform/upstream/intel-gpu-tools.git] / tests / pm_rps.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  *
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:
10  *
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
13  * Software.
14  *
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
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Ben Widawsky <ben@bwidawsk.net>
25  *    Jeff McGee <jeff.mcgee@intel.com>
26  *
27  */
28
29 #define _GNU_SOURCE
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <getopt.h>
35 #include <fcntl.h>
36 #include <signal.h>
37 #include "drmtest.h"
38 #include "intel_gpu_tools.h"
39 #include "intel_bufmgr.h"
40 #include "intel_batchbuffer.h"
41 #include "igt_debugfs.h"
42
43 static bool verbose = false;
44
45 static int drm_fd;
46
47 static const char sysfs_base_path[] = "/sys/class/drm/card%d/gt_%s_freq_mhz";
48 enum {
49         CUR,
50         MIN,
51         MAX,
52         RP0,
53         RP1,
54         RPn,
55         NUMFREQ
56 };
57
58 static int origfreqs[NUMFREQ];
59
60 struct junk {
61         const char *name;
62         const char *mode;
63         FILE *filp;
64 } stuff[] = {
65         { "cur", "r", NULL }, { "min", "rb+", NULL }, { "max", "rb+", NULL }, { "RP0", "r", NULL }, { "RP1", "r", NULL }, { "RPn", "r", NULL }, { NULL, NULL, NULL }
66 };
67
68 static igt_debugfs_t dfs;
69
70 static int readval(FILE *filp)
71 {
72         int val;
73         int scanned;
74
75         rewind(filp);
76         scanned = fscanf(filp, "%d", &val);
77         igt_assert(scanned == 1);
78
79         return val;
80 }
81
82 static void read_freqs(int *freqs)
83 {
84         int i;
85
86         for (i = 0; i < NUMFREQ; i++)
87                 freqs[i] = readval(stuff[i].filp);
88 }
89
90 static int do_writeval(FILE *filp, int val, int lerrno)
91 {
92         int ret, orig;
93
94         orig = readval(filp);
95         rewind(filp);
96         ret = fprintf(filp, "%d", val);
97
98         if (lerrno) {
99                 /* Expecting specific error */
100                 igt_assert(ret == EOF && errno == lerrno);
101                 igt_assert(readval(filp) == orig);
102         } else {
103                 /* Expecting no error */
104                 igt_assert(ret != EOF);
105                 igt_assert(readval(filp) == val);
106         }
107
108         return ret;
109 }
110 #define writeval(filp, val) do_writeval(filp, val, 0)
111 #define writeval_inval(filp, val) do_writeval(filp, val, EINVAL)
112
113 static void checkit(const int *freqs)
114 {
115         igt_assert(freqs[MIN] <= freqs[MAX]);
116         igt_assert(freqs[CUR] <= freqs[MAX]);
117         igt_assert(freqs[MIN] <= freqs[CUR]);
118         igt_assert(freqs[RPn] <= freqs[MIN]);
119         igt_assert(freqs[MAX] <= freqs[RP0]);
120         igt_assert(freqs[RP1] <= freqs[RP0]);
121         igt_assert(freqs[RPn] <= freqs[RP1]);
122         igt_assert(freqs[RP0] != 0);
123         igt_assert(freqs[RP1] != 0);
124 }
125
126 static void matchit(const int *freqs1, const int *freqs2)
127 {
128         igt_assert(freqs1[CUR] == freqs2[CUR]);
129         igt_assert(freqs1[MIN] == freqs2[MIN]);
130         igt_assert(freqs1[MAX] == freqs2[MAX]);
131         igt_assert(freqs1[RP0] == freqs2[RP0]);
132         igt_assert(freqs1[RP1] == freqs2[RP1]);
133         igt_assert(freqs1[RPn] == freqs2[RPn]);
134 }
135
136 static void dumpit(const int *freqs)
137 {
138         int i;
139
140         printf("gt freq (MHz):");
141         for (i = 0; i < NUMFREQ; i++)
142                 printf("  %s=%d", stuff[i].name, freqs[i]);
143
144         printf("\n");
145 }
146 #define dump(x) if (verbose) dumpit(x)
147 #define log(...) if (verbose) printf(__VA_ARGS__)
148
149 enum load {
150         LOW,
151         HIGH
152 };
153
154 static struct load_helper {
155         int devid;
156         int has_ppgtt;
157         drm_intel_bufmgr *bufmgr;
158         struct intel_batchbuffer *batch;
159         drm_intel_bo *target_buffer;
160         bool ready;
161         enum load load;
162         bool exit;
163         struct igt_helper_process igt_proc;
164 } lh;
165
166 static void load_helper_signal_handler(int sig)
167 {
168         if (sig == SIGUSR2)
169                 lh.load = lh.load == LOW ? HIGH : LOW;
170         else
171                 lh.exit = true;
172 }
173
174 static void emit_store_dword_imm(uint32_t val)
175 {
176         int cmd;
177         struct intel_batchbuffer *batch = lh.batch;
178
179         cmd = MI_STORE_DWORD_IMM;
180         if (!lh.has_ppgtt)
181                 cmd |= MI_MEM_VIRTUAL;
182
183         if (intel_gen(lh.devid) >= 8) {
184                 BEGIN_BATCH(4);
185                 OUT_BATCH(cmd);
186                 OUT_RELOC(lh.target_buffer, I915_GEM_DOMAIN_INSTRUCTION,
187                           I915_GEM_DOMAIN_INSTRUCTION, 0);
188                 OUT_BATCH(0);
189                 OUT_BATCH(val);
190                 ADVANCE_BATCH();
191         } else {
192                 BEGIN_BATCH(4);
193                 OUT_BATCH(cmd);
194                 OUT_BATCH(0); /* reserved */
195                 OUT_RELOC(lh.target_buffer, I915_GEM_DOMAIN_INSTRUCTION,
196                           I915_GEM_DOMAIN_INSTRUCTION, 0);
197                 OUT_BATCH(val);
198                 ADVANCE_BATCH();
199         }
200 }
201
202 #define LOAD_HELPER_PAUSE_USEC 500
203 static void load_helper_run(enum load load)
204 {
205         assert(!lh.igt_proc.running);
206
207         igt_require(lh.ready == true);
208
209         lh.load = load;
210
211         igt_fork_helper(&lh.igt_proc) {
212                 uint32_t val = 0;
213
214                 signal(SIGUSR1, load_helper_signal_handler);
215                 signal(SIGUSR2, load_helper_signal_handler);
216
217                 while (!lh.exit) {
218                         emit_store_dword_imm(val);
219                         intel_batchbuffer_flush_on_ring(lh.batch, 0);
220                         val++;
221
222                         /* Lower the load by pausing after every submitted
223                          * write. */
224                         if (lh.load == LOW)
225                                 usleep(LOAD_HELPER_PAUSE_USEC);
226                 }
227
228                 /* Map buffer to stall for write completion */
229                 drm_intel_bo_map(lh.target_buffer, 0);
230                 drm_intel_bo_unmap(lh.target_buffer);
231
232                 log("load helper sent %u dword writes\n", val);
233         }
234 }
235
236 static void load_helper_set_load(enum load load)
237 {
238         assert(lh.igt_proc.running);
239
240         if (lh.load == load)
241                 return;
242
243         lh.load = load;
244         kill(lh.igt_proc.pid, SIGUSR2);
245 }
246
247 static void load_helper_stop(void)
248 {
249         assert(lh.igt_proc.running);
250         kill(lh.igt_proc.pid, SIGUSR1);
251         igt_wait_helper(&lh.igt_proc);
252 }
253
254 /* The load helper resource is used by only some subtests. We attempt to
255  * initialize in igt_fixture but do our igt_require check only if a
256  * subtest attempts to run it */
257 static void load_helper_init(void)
258 {
259         lh.devid = intel_get_drm_devid(drm_fd);
260         lh.has_ppgtt = gem_uses_aliasing_ppgtt(drm_fd);
261
262         /* MI_STORE_DATA can only use GTT address on gen4+/g33 and needs
263          * snoopable mem on pre-gen6. */
264         if (intel_gen(lh.devid) < 6) {
265                 log("load helper init failed: pre-gen6 not supported\n");
266                 return;
267         }
268
269         lh.bufmgr = drm_intel_bufmgr_gem_init(drm_fd, 4096);
270         if (!lh.bufmgr) {
271                 log("load helper init failed: buffer manager init\n");
272                 return;
273         }
274         drm_intel_bufmgr_gem_enable_reuse(lh.bufmgr);
275
276         lh.batch = intel_batchbuffer_alloc(lh.bufmgr, lh.devid);
277         if (!lh.batch) {
278                 log("load helper init failed: batch buffer alloc\n");
279                 return;
280         }
281
282         lh.target_buffer = drm_intel_bo_alloc(lh.bufmgr, "target bo",
283                                               4096, 4096);
284         if (!lh.target_buffer) {
285                 log("load helper init failed: target buffer alloc\n");
286                 return;
287         }
288
289         lh.ready = true;
290 }
291
292 static void load_helper_deinit(void)
293 {
294         if (lh.igt_proc.running)
295                 load_helper_stop();
296
297         if (lh.target_buffer)
298                 drm_intel_bo_unreference(lh.target_buffer);
299
300         if (lh.batch)
301                 intel_batchbuffer_free(lh.batch);
302
303         if (lh.bufmgr)
304                 drm_intel_bufmgr_destroy(lh.bufmgr);
305 }
306
307 static void stop_rings(void)
308 {
309         int fd;
310         static const char data[] = "0xf";
311
312         fd = igt_debugfs_open(&dfs, "i915_ring_stop", O_WRONLY);
313         igt_assert(fd >= 0);
314
315         log("injecting ring stop\n");
316         igt_assert(write(fd, data, sizeof(data)) == sizeof(data));
317
318         close(fd);
319 }
320
321 static bool rings_stopped(void)
322 {
323         int fd;
324         static char buf[128];
325         unsigned long long val;
326
327         fd = igt_debugfs_open(&dfs, "i915_ring_stop", O_RDONLY);
328         igt_assert(fd >= 0);
329
330         igt_assert(read(fd, buf, sizeof(buf)) > 0);
331         close(fd);
332
333         sscanf(buf, "%llx", &val);
334
335         return (bool)val;
336 }
337
338 static void min_max_config(void (*check)(void))
339 {
340         int fmid = (origfreqs[RPn] + origfreqs[RP0]) / 2;
341
342         /* hw (and so kernel) currently rounds to 50 MHz ... */
343         fmid = fmid / 50 * 50;
344
345         log("\nCheck original min and max...\n");
346         check();
347
348         log("\nSet min=RPn and max=RP0...\n");
349         writeval(stuff[MIN].filp, origfreqs[RPn]);
350         writeval(stuff[MAX].filp, origfreqs[RP0]);
351         check();
352
353         log("\nIncrease min to midpoint...\n");
354         writeval(stuff[MIN].filp, fmid);
355         check();
356
357         log("\nIncrease min to RP0...\n");
358         writeval(stuff[MIN].filp, origfreqs[RP0]);
359         check();
360
361         log("\nIncrease min above RP0 (invalid)...\n");
362         writeval_inval(stuff[MIN].filp, origfreqs[RP0] + 1000);
363         check();
364
365         log("\nDecrease max to RPn (invalid)...\n");
366         writeval_inval(stuff[MAX].filp, origfreqs[RPn]);
367         check();
368
369         log("\nDecrease min to midpoint...\n");
370         writeval(stuff[MIN].filp, fmid);
371         check();
372
373         log("\nDecrease min to RPn...\n");
374         writeval(stuff[MIN].filp, origfreqs[RPn]);
375         check();
376
377         log("\nDecrease min below RPn (invalid)...\n");
378         writeval_inval(stuff[MIN].filp, 0);
379         check();
380
381         log("\nDecrease max to midpoint...\n");
382         writeval(stuff[MAX].filp, fmid);
383         check();
384
385         log("\nDecrease max to RPn...\n");
386         writeval(stuff[MAX].filp, origfreqs[RPn]);
387         check();
388
389         log("\nDecrease max below RPn (invalid)...\n");
390         writeval_inval(stuff[MAX].filp, 0);
391         check();
392
393         log("\nIncrease min to RP0 (invalid)...\n");
394         writeval_inval(stuff[MIN].filp, origfreqs[RP0]);
395         check();
396
397         log("\nIncrease max to midpoint...\n");
398         writeval(stuff[MAX].filp, fmid);
399         check();
400
401         log("\nIncrease max to RP0...\n");
402         writeval(stuff[MAX].filp, origfreqs[RP0]);
403         check();
404
405         log("\nIncrease max above RP0 (invalid)...\n");
406         writeval_inval(stuff[MAX].filp, origfreqs[RP0] + 1000);
407         check();
408
409         writeval(stuff[MIN].filp, origfreqs[MIN]);
410         writeval(stuff[MAX].filp, origfreqs[MAX]);
411 }
412
413 static void basic_check(void)
414 {
415         int freqs[NUMFREQ];
416
417         read_freqs(freqs);
418         dump(freqs);
419         checkit(freqs);
420 }
421
422 #define IDLE_WAIT_TIMESTEP_MSEC 100
423 #define IDLE_WAIT_TIMEOUT_MSEC 3000
424 static void idle_check(void)
425 {
426         int freqs[NUMFREQ];
427         int wait = 0;
428
429         /* Monitor frequencies until cur settles down to min, which should
430          * happen within the allotted time */
431         do {
432                 read_freqs(freqs);
433                 dump(freqs);
434                 checkit(freqs);
435                 if (freqs[CUR] == freqs[MIN])
436                         break;
437                 usleep(1000 * IDLE_WAIT_TIMESTEP_MSEC);
438                 wait += IDLE_WAIT_TIMESTEP_MSEC;
439         } while (wait < IDLE_WAIT_TIMEOUT_MSEC);
440
441         igt_assert(freqs[CUR] == freqs[MIN]);
442         log("Required %d msec to reach cur=min\n", wait);
443 }
444
445 #define LOADED_WAIT_TIMESTEP_MSEC 100
446 #define LOADED_WAIT_TIMEOUT_MSEC 3000
447 static void loaded_check(void)
448 {
449         int freqs[NUMFREQ];
450         int wait = 0;
451
452         /* Monitor frequencies until cur increases to max, which should
453          * happen within the allotted time */
454         do {
455                 read_freqs(freqs);
456                 dump(freqs);
457                 checkit(freqs);
458                 if (freqs[CUR] == freqs[MAX])
459                         break;
460                 usleep(1000 * LOADED_WAIT_TIMESTEP_MSEC);
461                 wait += LOADED_WAIT_TIMESTEP_MSEC;
462         } while (wait < LOADED_WAIT_TIMEOUT_MSEC);
463
464         igt_assert(freqs[CUR] == freqs[MAX]);
465         log("Required %d msec to reach cur=max\n", wait);
466 }
467
468 #define STABILIZE_WAIT_TIMESTEP_MSEC 100
469 #define STABILIZE_WAIT_TIMEOUT_MSEC 2000
470 static void stabilize_check(int *freqs)
471 {
472         int wait = 0;
473
474         do {
475                 read_freqs(freqs);
476                 dump(freqs);
477                 usleep(1000 * STABILIZE_WAIT_TIMESTEP_MSEC);
478                 wait += STABILIZE_WAIT_TIMESTEP_MSEC;
479         } while (wait < STABILIZE_WAIT_TIMEOUT_MSEC);
480
481         log("Waited %d msec to stabilize cur\n", wait);
482 }
483
484 static void reset(void)
485 {
486         int pre_freqs[NUMFREQ];
487         int post_freqs[NUMFREQ];
488
489         log("Apply low load...\n");
490         load_helper_run(LOW);
491         stabilize_check(pre_freqs);
492
493         log("Stop rings...\n");
494         stop_rings();
495         while (rings_stopped())
496                 usleep(1000 * 100);
497         log("Ring stop cleared\n");
498
499         log("Apply high load...\n");
500         load_helper_set_load(HIGH);
501         loaded_check();
502
503         log("Apply low load...\n");
504         load_helper_set_load(LOW);
505         stabilize_check(post_freqs);
506         matchit(pre_freqs, post_freqs);
507
508         log("Apply high load...\n");
509         load_helper_set_load(HIGH);
510         loaded_check();
511
512         log("Removing load...\n");
513         load_helper_stop();
514         idle_check();
515 }
516
517 static void pm_rps_exit_handler(int sig)
518 {
519         if (origfreqs[MIN] > readval(stuff[MAX].filp)) {
520                 writeval(stuff[MAX].filp, origfreqs[MAX]);
521                 writeval(stuff[MIN].filp, origfreqs[MIN]);
522         } else {
523                 writeval(stuff[MIN].filp, origfreqs[MIN]);
524                 writeval(stuff[MAX].filp, origfreqs[MAX]);
525         }
526
527         load_helper_deinit();
528         close(drm_fd);
529 }
530
531 static int opt_handler(int opt, int opt_index)
532 {
533         switch (opt) {
534         case 'v':
535                 verbose = true;
536                 break;
537         default:
538                 assert(0);
539         }
540
541         return 0;
542 }
543
544 /* Mod of igt_subtest_init that adds our extra options */
545 static void subtest_init(int argc, char **argv)
546 {
547         struct option long_opts[] = {
548                 {"verbose", 0, 0, 'v'}
549         };
550         const char *help_str = "  -v, --verbose";
551         int ret;
552
553         ret = igt_subtest_init_parse_opts(argc, argv, "v", long_opts,
554                                           help_str, opt_handler);
555
556         if (ret < 0)
557                 /* exit with no error for -h/--help */
558                 exit(ret == -1 ? 0 : ret);
559 }
560
561 int main(int argc, char **argv)
562 {
563         subtest_init(argc, argv);
564
565         igt_skip_on_simulation();
566
567         igt_fixture {
568                 const int device = drm_get_card();
569                 struct junk *junk = stuff;
570                 int ret;
571
572                 /* Use drm_open_any to verify device existence */
573                 drm_fd = drm_open_any();
574
575                 do {
576                         int val = -1;
577                         char *path;
578                         ret = asprintf(&path, sysfs_base_path, device, junk->name);
579                         igt_assert(ret != -1);
580                         junk->filp = fopen(path, junk->mode);
581                         igt_require(junk->filp);
582                         setbuf(junk->filp, NULL);
583
584                         val = readval(junk->filp);
585                         igt_assert(val >= 0);
586                         junk++;
587                 } while(junk->name != NULL);
588
589                 read_freqs(origfreqs);
590
591                 igt_install_exit_handler(pm_rps_exit_handler);
592
593                 load_helper_init();
594
595                 igt_debugfs_init(&dfs);
596         }
597
598         igt_subtest("basic-api")
599                 min_max_config(basic_check);
600
601         igt_subtest("min-max-config-idle")
602                 min_max_config(idle_check);
603
604         igt_subtest("min-max-config-loaded") {
605                 load_helper_run(HIGH);
606                 min_max_config(loaded_check);
607                 load_helper_stop();
608         }
609
610         igt_subtest("reset")
611                 reset();
612
613         igt_exit();
614 }