tests/pm_rps: Fix compilation on Linux
[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 setfreq(int val)
114 {
115         if (val > readval(stuff[MAX].filp)) {
116                 writeval(stuff[MAX].filp, val);
117                 writeval(stuff[MIN].filp, val);
118         } else {
119                 writeval(stuff[MIN].filp, val);
120                 writeval(stuff[MAX].filp, val);
121         }
122 }
123
124 static void checkit(const int *freqs)
125 {
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);
135 }
136
137 static void matchit(const int *freqs1, const int *freqs2)
138 {
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]);
145 }
146
147 static void dumpit(const int *freqs)
148 {
149         int i;
150
151         printf("gt freq (MHz):");
152         for (i = 0; i < NUMFREQ; i++)
153                 printf("  %s=%d", stuff[i].name, freqs[i]);
154
155         printf("\n");
156 }
157 #define dump(x) if (verbose) dumpit(x)
158 #define log(...) if (verbose) printf(__VA_ARGS__)
159
160 enum load {
161         LOW,
162         HIGH
163 };
164
165 static struct load_helper {
166         int devid;
167         int has_ppgtt;
168         drm_intel_bufmgr *bufmgr;
169         struct intel_batchbuffer *batch;
170         drm_intel_bo *target_buffer;
171         bool ready;
172         enum load load;
173         bool exit;
174         struct igt_helper_process igt_proc;
175 } lh;
176
177 static void load_helper_signal_handler(int sig)
178 {
179         if (sig == SIGUSR2)
180                 lh.load = lh.load == LOW ? HIGH : LOW;
181         else
182                 lh.exit = true;
183 }
184
185 static void emit_store_dword_imm(uint32_t val)
186 {
187         int cmd;
188         struct intel_batchbuffer *batch = lh.batch;
189
190         cmd = MI_STORE_DWORD_IMM;
191         if (!lh.has_ppgtt)
192                 cmd |= MI_MEM_VIRTUAL;
193
194         if (intel_gen(lh.devid) >= 8) {
195                 BEGIN_BATCH(4);
196                 OUT_BATCH(cmd);
197                 OUT_RELOC(lh.target_buffer, I915_GEM_DOMAIN_INSTRUCTION,
198                           I915_GEM_DOMAIN_INSTRUCTION, 0);
199                 OUT_BATCH(0);
200                 OUT_BATCH(val);
201                 ADVANCE_BATCH();
202         } else {
203                 BEGIN_BATCH(4);
204                 OUT_BATCH(cmd);
205                 OUT_BATCH(0); /* reserved */
206                 OUT_RELOC(lh.target_buffer, I915_GEM_DOMAIN_INSTRUCTION,
207                           I915_GEM_DOMAIN_INSTRUCTION, 0);
208                 OUT_BATCH(val);
209                 ADVANCE_BATCH();
210         }
211 }
212
213 #define LOAD_HELPER_PAUSE_USEC 500
214 static void load_helper_run(enum load load)
215 {
216         assert(!lh.igt_proc.running);
217
218         igt_require(lh.ready == true);
219
220         lh.load = load;
221
222         igt_fork_helper(&lh.igt_proc) {
223                 uint32_t val = 0;
224
225                 signal(SIGUSR1, load_helper_signal_handler);
226                 signal(SIGUSR2, load_helper_signal_handler);
227
228                 while (!lh.exit) {
229                         emit_store_dword_imm(val);
230                         intel_batchbuffer_flush_on_ring(lh.batch, 0);
231                         val++;
232
233                         /* Lower the load by pausing after every submitted
234                          * write. */
235                         if (lh.load == LOW)
236                                 usleep(LOAD_HELPER_PAUSE_USEC);
237                 }
238
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);
242
243                 log("load helper sent %u dword writes\n", val);
244         }
245 }
246
247 static void load_helper_set_load(enum load load)
248 {
249         assert(lh.igt_proc.running);
250
251         if (lh.load == load)
252                 return;
253
254         lh.load = load;
255         kill(lh.igt_proc.pid, SIGUSR2);
256 }
257
258 static void load_helper_stop(void)
259 {
260         assert(lh.igt_proc.running);
261         kill(lh.igt_proc.pid, SIGUSR1);
262         igt_wait_helper(&lh.igt_proc);
263 }
264
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)
269 {
270         lh.devid = intel_get_drm_devid(drm_fd);
271         lh.has_ppgtt = gem_uses_aliasing_ppgtt(drm_fd);
272
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");
277                 return;
278         }
279
280         lh.bufmgr = drm_intel_bufmgr_gem_init(drm_fd, 4096);
281         if (!lh.bufmgr) {
282                 log("load helper init failed: buffer manager init\n");
283                 return;
284         }
285         drm_intel_bufmgr_gem_enable_reuse(lh.bufmgr);
286
287         lh.batch = intel_batchbuffer_alloc(lh.bufmgr, lh.devid);
288         if (!lh.batch) {
289                 log("load helper init failed: batch buffer alloc\n");
290                 return;
291         }
292
293         lh.target_buffer = drm_intel_bo_alloc(lh.bufmgr, "target bo",
294                                               4096, 4096);
295         if (!lh.target_buffer) {
296                 log("load helper init failed: target buffer alloc\n");
297                 return;
298         }
299
300         lh.ready = true;
301 }
302
303 static void load_helper_deinit(void)
304 {
305         if (lh.igt_proc.running)
306                 load_helper_stop();
307
308         if (lh.target_buffer)
309                 drm_intel_bo_unreference(lh.target_buffer);
310
311         if (lh.batch)
312                 intel_batchbuffer_free(lh.batch);
313
314         if (lh.bufmgr)
315                 drm_intel_bufmgr_destroy(lh.bufmgr);
316 }
317
318 static void stop_rings(void)
319 {
320         int fd;
321         static const char data[] = "0xf";
322
323         fd = igt_debugfs_open(&dfs, "i915_ring_stop", O_WRONLY);
324         igt_assert(fd >= 0);
325
326         log("injecting ring stop\n");
327         igt_assert(write(fd, data, sizeof(data)) == sizeof(data));
328
329         close(fd);
330 }
331
332 static bool rings_stopped(void)
333 {
334         int fd;
335         static char buf[128];
336         unsigned long long val;
337
338         fd = igt_debugfs_open(&dfs, "i915_ring_stop", O_RDONLY);
339         igt_assert(fd >= 0);
340
341         igt_assert(read(fd, buf, sizeof(buf)) > 0);
342         close(fd);
343
344         sscanf(buf, "%llx", &val);
345
346         return (bool)val;
347 }
348
349 static void min_max_config(void (*check)(void))
350 {
351         int fmid = (origfreqs[RPn] + origfreqs[RP0]) / 2;
352
353         log("\nCheck original min and max...\n");
354         check();
355
356         log("\nSet min=RPn and max=RP0...\n");
357         writeval(stuff[MIN].filp, origfreqs[RPn]);
358         writeval(stuff[MAX].filp, origfreqs[RP0]);
359         check();
360
361         log("\nIncrease min to midpoint...\n");
362         writeval(stuff[MIN].filp, fmid);
363         check();
364
365         log("\nIncrease min to RP0...\n");
366         writeval(stuff[MIN].filp, origfreqs[RP0]);
367         check();
368
369         log("\nIncrease min above RP0 (invalid)...\n");
370         writeval_inval(stuff[MIN].filp, origfreqs[RP0] + 1000);
371         check();
372
373         log("\nDecrease max to RPn (invalid)...\n");
374         writeval_inval(stuff[MAX].filp, origfreqs[RPn]);
375         check();
376
377         log("\nDecrease min to midpoint...\n");
378         writeval(stuff[MIN].filp, fmid);
379         check();
380
381         log("\nDecrease min to RPn...\n");
382         writeval(stuff[MIN].filp, origfreqs[RPn]);
383         check();
384
385         log("\nDecrease min below RPn (invalid)...\n");
386         writeval_inval(stuff[MIN].filp, 0);
387         check();
388
389         log("\nDecrease max to midpoint...\n");
390         writeval(stuff[MAX].filp, fmid);
391         check();
392
393         log("\nDecrease max to RPn...\n");
394         writeval(stuff[MAX].filp, origfreqs[RPn]);
395         check();
396
397         log("\nDecrease max below RPn (invalid)...\n");
398         writeval_inval(stuff[MAX].filp, 0);
399         check();
400
401         log("\nIncrease min to RP0 (invalid)...\n");
402         writeval_inval(stuff[MIN].filp, origfreqs[RP0]);
403         check();
404
405         log("\nIncrease max to midpoint...\n");
406         writeval(stuff[MAX].filp, fmid);
407         check();
408
409         log("\nIncrease max to RP0...\n");
410         writeval(stuff[MAX].filp, origfreqs[RP0]);
411         check();
412
413         log("\nIncrease max above RP0 (invalid)...\n");
414         writeval_inval(stuff[MAX].filp, origfreqs[RP0] + 1000);
415         check();
416
417         writeval(stuff[MIN].filp, origfreqs[MIN]);
418         writeval(stuff[MAX].filp, origfreqs[MAX]);
419 }
420
421 static void basic_check(void)
422 {
423         int freqs[NUMFREQ];
424
425         read_freqs(freqs);
426         dump(freqs);
427         checkit(freqs);
428 }
429
430 #define IDLE_WAIT_TIMESTEP_MSEC 100
431 #define IDLE_WAIT_TIMEOUT_MSEC 3000
432 static void idle_check(void)
433 {
434         int freqs[NUMFREQ];
435         int wait = 0;
436
437         /* Monitor frequencies until cur settles down to min, which should
438          * happen within the allotted time */
439         do {
440                 read_freqs(freqs);
441                 dump(freqs);
442                 checkit(freqs);
443                 if (freqs[CUR] == freqs[MIN])
444                         break;
445                 usleep(1000 * IDLE_WAIT_TIMESTEP_MSEC);
446                 wait += IDLE_WAIT_TIMESTEP_MSEC;
447         } while (wait < IDLE_WAIT_TIMEOUT_MSEC);
448
449         igt_assert(freqs[CUR] == freqs[MIN]);
450         log("Required %d msec to reach cur=min\n", wait);
451 }
452
453 #define LOADED_WAIT_TIMESTEP_MSEC 100
454 #define LOADED_WAIT_TIMEOUT_MSEC 3000
455 static void loaded_check(void)
456 {
457         int freqs[NUMFREQ];
458         int wait = 0;
459
460         /* Monitor frequencies until cur increases to max, which should
461          * happen within the allotted time */
462         do {
463                 read_freqs(freqs);
464                 dump(freqs);
465                 checkit(freqs);
466                 if (freqs[CUR] == freqs[MAX])
467                         break;
468                 usleep(1000 * LOADED_WAIT_TIMESTEP_MSEC);
469                 wait += LOADED_WAIT_TIMESTEP_MSEC;
470         } while (wait < LOADED_WAIT_TIMEOUT_MSEC);
471
472         igt_assert(freqs[CUR] == freqs[MAX]);
473         log("Required %d msec to reach cur=max\n", wait);
474 }
475
476 #define STABILIZE_WAIT_TIMESTEP_MSEC 100
477 #define STABILIZE_WAIT_TIMEOUT_MSEC 2000
478 static void stabilize_check(int *freqs)
479 {
480         int wait = 0;
481
482         do {
483                 read_freqs(freqs);
484                 dump(freqs);
485                 usleep(1000 * STABILIZE_WAIT_TIMESTEP_MSEC);
486                 wait += STABILIZE_WAIT_TIMESTEP_MSEC;
487         } while (wait < STABILIZE_WAIT_TIMEOUT_MSEC);
488
489         log("Waited %d msec to stabilize cur\n", wait);
490 }
491
492 static void reset(void)
493 {
494         int pre_freqs[NUMFREQ];
495         int post_freqs[NUMFREQ];
496
497         log("Apply low load...\n");
498         load_helper_run(LOW);
499         stabilize_check(pre_freqs);
500
501         log("Stop rings...\n");
502         stop_rings();
503         while (rings_stopped())
504                 usleep(1000 * 100);
505         log("Ring stop cleared\n");
506
507         log("Apply high load...\n");
508         load_helper_set_load(HIGH);
509         loaded_check();
510
511         log("Apply low load...\n");
512         load_helper_set_load(LOW);
513         stabilize_check(post_freqs);
514         matchit(pre_freqs, post_freqs);
515
516         log("Apply high load...\n");
517         load_helper_set_load(HIGH);
518         loaded_check();
519
520         log("Removing load...\n");
521         load_helper_stop();
522         idle_check();
523 }
524
525 static void pm_rps_exit_handler(int sig)
526 {
527         if (origfreqs[MIN] > readval(stuff[MAX].filp)) {
528                 writeval(stuff[MAX].filp, origfreqs[MAX]);
529                 writeval(stuff[MIN].filp, origfreqs[MIN]);
530         } else {
531                 writeval(stuff[MIN].filp, origfreqs[MIN]);
532                 writeval(stuff[MAX].filp, origfreqs[MAX]);
533         }
534
535         load_helper_deinit();
536         close(drm_fd);
537 }
538
539 static int opt_handler(int opt, int opt_index)
540 {
541         switch (opt) {
542         case 'v':
543                 verbose = true;
544                 break;
545         default:
546                 assert(0);
547         }
548
549         return 0;
550 }
551
552 /* Mod of igt_subtest_init that adds our extra options */
553 static void subtest_init(int argc, char **argv)
554 {
555         struct option long_opts[] = {
556                 {"verbose", 0, 0, 'v'}
557         };
558         const char *help_str = "  -v, --verbose";
559         int ret;
560
561         ret = igt_subtest_init_parse_opts(argc, argv, "v", long_opts,
562                                           help_str, opt_handler);
563
564         if (ret < 0)
565                 /* exit with no error for -h/--help */
566                 exit(ret == -1 ? 0 : ret);
567 }
568
569 int main(int argc, char **argv)
570 {
571         subtest_init(argc, argv);
572
573         igt_skip_on_simulation();
574
575         igt_fixture {
576                 const int device = drm_get_card();
577                 struct junk *junk = stuff;
578                 int ret;
579
580                 /* Use drm_open_any to verify device existence */
581                 drm_fd = drm_open_any();
582
583                 do {
584                         int val = -1;
585                         char *path;
586                         ret = asprintf(&path, sysfs_base_path, device, junk->name);
587                         igt_assert(ret != -1);
588                         junk->filp = fopen(path, junk->mode);
589                         igt_require(junk->filp);
590                         setbuf(junk->filp, NULL);
591
592                         val = readval(junk->filp);
593                         igt_assert(val >= 0);
594                         junk++;
595                 } while(junk->name != NULL);
596
597                 read_freqs(origfreqs);
598
599                 igt_install_exit_handler(pm_rps_exit_handler);
600
601                 load_helper_init();
602
603                 igt_debugfs_init(&dfs);
604         }
605
606         igt_subtest("basic-api")
607                 min_max_config(basic_check);
608
609         igt_subtest("min-max-config-idle")
610                 min_max_config(idle_check);
611
612         igt_subtest("min-max-config-loaded") {
613                 load_helper_run(HIGH);
614                 min_max_config(loaded_check);
615                 load_helper_stop();
616         }
617
618         igt_subtest("reset")
619                 reset();
620
621         igt_exit();
622 }