lib/igt_kms: Unify pipe name helpers
[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 <fcntl.h>
35 #include <signal.h>
36 #include <errno.h>
37 #include <sys/wait.h>
38
39 #include "drmtest.h"
40 #include "intel_io.h"
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"
46
47 static int drm_fd;
48
49 static const char sysfs_base_path[] = "/sys/class/drm/card%d/gt_%s_freq_mhz";
50 enum {
51         CUR,
52         MIN,
53         MAX,
54         RP0,
55         RP1,
56         RPn,
57         NUMFREQ
58 };
59
60 static int origfreqs[NUMFREQ];
61
62 struct junk {
63         const char *name;
64         const char *mode;
65         FILE *filp;
66 } stuff[] = {
67         { "cur", "r", NULL }, { "min", "rb+", NULL }, { "max", "rb+", NULL }, { "RP0", "r", NULL }, { "RP1", "r", NULL }, { "RPn", "r", NULL }, { NULL, NULL, NULL }
68 };
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_cmpint(freqs[MIN], <=, freqs[MAX]);
116         igt_assert_cmpint(freqs[CUR], <=, freqs[MAX]);
117         igt_assert_cmpint(freqs[MIN], <=, freqs[CUR]);
118         igt_assert_cmpint(freqs[RPn], <=, freqs[MIN]);
119         igt_assert_cmpint(freqs[MAX], <=, freqs[RP0]);
120         igt_assert_cmpint(freqs[RP1], <=, freqs[RP0]);
121         igt_assert_cmpint(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_cmpint(freqs1[CUR], ==, freqs2[CUR]);
129         igt_assert_cmpint(freqs1[MIN], ==, freqs2[MIN]);
130         igt_assert_cmpint(freqs1[MAX], ==, freqs2[MAX]);
131         igt_assert_cmpint(freqs1[RP0], ==, freqs2[RP0]);
132         igt_assert_cmpint(freqs1[RP1], ==, freqs2[RP1]);
133         igt_assert_cmpint(freqs1[RPn], ==, freqs2[RPn]);
134 }
135
136 static void dump(const int *freqs)
137 {
138         int i;
139
140         igt_debug("gt freq (MHz):");
141         for (i = 0; i < NUMFREQ; i++)
142                 igt_debug("  %s=%d", stuff[i].name, freqs[i]);
143
144         igt_debug("\n");
145 }
146
147 enum load {
148         LOW,
149         HIGH
150 };
151
152 static struct load_helper {
153         int devid;
154         int has_ppgtt;
155         drm_intel_bufmgr *bufmgr;
156         struct intel_batchbuffer *batch;
157         drm_intel_bo *target_buffer;
158         enum load load;
159         bool exit;
160         struct igt_helper_process igt_proc;
161         drm_intel_bo *src, *dst;
162 } lh;
163
164 static void load_helper_signal_handler(int sig)
165 {
166         if (sig == SIGUSR2)
167                 lh.load = lh.load == LOW ? HIGH : LOW;
168         else
169                 lh.exit = true;
170 }
171
172 static void emit_store_dword_imm(uint32_t val)
173 {
174         int cmd;
175         struct intel_batchbuffer *batch = lh.batch;
176
177         cmd = MI_STORE_DWORD_IMM;
178         if (!lh.has_ppgtt)
179                 cmd |= MI_MEM_VIRTUAL;
180
181         if (intel_gen(lh.devid) >= 8) {
182                 BEGIN_BATCH(4);
183                 OUT_BATCH(cmd);
184                 OUT_RELOC(lh.target_buffer, I915_GEM_DOMAIN_INSTRUCTION,
185                           I915_GEM_DOMAIN_INSTRUCTION, 0);
186                 OUT_BATCH(0);
187                 OUT_BATCH(val);
188                 ADVANCE_BATCH();
189         } else {
190                 BEGIN_BATCH(4);
191                 OUT_BATCH(cmd);
192                 OUT_BATCH(0); /* reserved */
193                 OUT_RELOC(lh.target_buffer, I915_GEM_DOMAIN_INSTRUCTION,
194                           I915_GEM_DOMAIN_INSTRUCTION, 0);
195                 OUT_BATCH(val);
196                 ADVANCE_BATCH();
197         }
198 }
199
200 #define LOAD_HELPER_PAUSE_USEC 500
201 #define LOAD_HELPER_BO_SIZE (16*1024*1024)
202 static void load_helper_set_load(enum load load)
203 {
204         igt_assert(lh.igt_proc.running);
205
206         if (lh.load == load)
207                 return;
208
209         lh.load = load;
210         kill(lh.igt_proc.pid, SIGUSR2);
211 }
212
213 static void load_helper_run(enum load load)
214 {
215         /*
216          * FIXME fork helpers won't get cleaned up when started from within a
217          * subtest, so handle the case where it sticks around a bit too long.
218          */
219         if (lh.igt_proc.running) {
220                 load_helper_set_load(load);
221                 return;
222         }
223
224         lh.load = load;
225
226         igt_fork_helper(&lh.igt_proc) {
227                 uint32_t val = 0;
228
229                 signal(SIGUSR1, load_helper_signal_handler);
230                 signal(SIGUSR2, load_helper_signal_handler);
231
232                 while (!lh.exit) {
233                         if (lh.load == HIGH)
234                                 intel_copy_bo(lh.batch, lh.dst, lh.src,
235                                               LOAD_HELPER_BO_SIZE);
236
237                         emit_store_dword_imm(val);
238                         intel_batchbuffer_flush_on_ring(lh.batch, 0);
239                         val++;
240
241                         /* Lower the load by pausing after every submitted
242                          * write. */
243                         if (lh.load == LOW)
244                                 usleep(LOAD_HELPER_PAUSE_USEC);
245                 }
246
247                 /* Map buffer to stall for write completion */
248                 drm_intel_bo_map(lh.target_buffer, 0);
249                 drm_intel_bo_unmap(lh.target_buffer);
250
251                 igt_debug("load helper sent %u dword writes\n", val);
252         }
253 }
254
255 static void load_helper_stop(void)
256 {
257         kill(lh.igt_proc.pid, SIGUSR1);
258         igt_assert(igt_wait_helper(&lh.igt_proc) == 0);
259 }
260
261 static void load_helper_init(void)
262 {
263         lh.devid = intel_get_drm_devid(drm_fd);
264         lh.has_ppgtt = gem_uses_aliasing_ppgtt(drm_fd);
265
266         /* MI_STORE_DATA can only use GTT address on gen4+/g33 and needs
267          * snoopable mem on pre-gen6. Hence load-helper only works on gen6+, but
268          * that's also all we care about for the rps testcase*/
269         igt_assert(intel_gen(lh.devid) >= 6);
270         lh.bufmgr = drm_intel_bufmgr_gem_init(drm_fd, 4096);
271         igt_assert(lh.bufmgr);
272
273         drm_intel_bufmgr_gem_enable_reuse(lh.bufmgr);
274
275         lh.batch = intel_batchbuffer_alloc(lh.bufmgr, lh.devid);
276         igt_assert(lh.batch);
277
278         lh.target_buffer = drm_intel_bo_alloc(lh.bufmgr, "target bo",
279                                               4096, 4096);
280         igt_assert(lh.target_buffer);
281
282         lh.dst = drm_intel_bo_alloc(lh.bufmgr, "dst bo",
283                                     LOAD_HELPER_BO_SIZE, 4096);
284         igt_assert(lh.dst);
285         lh.src = drm_intel_bo_alloc(lh.bufmgr, "src bo",
286                                     LOAD_HELPER_BO_SIZE, 4096);
287         igt_assert(lh.src);
288 }
289
290 static void load_helper_deinit(void)
291 {
292         if (lh.igt_proc.running)
293                 load_helper_stop();
294
295         if (lh.target_buffer)
296                 drm_intel_bo_unreference(lh.target_buffer);
297         if (lh.src)
298                 drm_intel_bo_unreference(lh.src);
299         if (lh.dst)
300                 drm_intel_bo_unreference(lh.dst);
301
302         if (lh.batch)
303                 intel_batchbuffer_free(lh.batch);
304
305         if (lh.bufmgr)
306                 drm_intel_bufmgr_destroy(lh.bufmgr);
307 }
308
309 static void min_max_config(void (*check)(void))
310 {
311         int fmid = (origfreqs[RPn] + origfreqs[RP0]) / 2;
312
313         /* hw (and so kernel) currently rounds to 50 MHz ... */
314         fmid = fmid / 50 * 50;
315
316         igt_debug("\nCheck original min and max...\n");
317         check();
318
319         igt_debug("\nSet min=RPn and max=RP0...\n");
320         writeval(stuff[MIN].filp, origfreqs[RPn]);
321         writeval(stuff[MAX].filp, origfreqs[RP0]);
322         check();
323
324         igt_debug("\nIncrease min to midpoint...\n");
325         writeval(stuff[MIN].filp, fmid);
326         check();
327
328         igt_debug("\nIncrease min to RP0...\n");
329         writeval(stuff[MIN].filp, origfreqs[RP0]);
330         check();
331
332         igt_debug("\nIncrease min above RP0 (invalid)...\n");
333         writeval_inval(stuff[MIN].filp, origfreqs[RP0] + 1000);
334         check();
335
336         igt_debug("\nDecrease max to RPn (invalid)...\n");
337         writeval_inval(stuff[MAX].filp, origfreqs[RPn]);
338         check();
339
340         igt_debug("\nDecrease min to midpoint...\n");
341         writeval(stuff[MIN].filp, fmid);
342         check();
343
344         igt_debug("\nDecrease min to RPn...\n");
345         writeval(stuff[MIN].filp, origfreqs[RPn]);
346         check();
347
348         igt_debug("\nDecrease min below RPn (invalid)...\n");
349         writeval_inval(stuff[MIN].filp, 0);
350         check();
351
352         igt_debug("\nDecrease max to midpoint...\n");
353         writeval(stuff[MAX].filp, fmid);
354         check();
355
356         igt_debug("\nDecrease max to RPn...\n");
357         writeval(stuff[MAX].filp, origfreqs[RPn]);
358         check();
359
360         igt_debug("\nDecrease max below RPn (invalid)...\n");
361         writeval_inval(stuff[MAX].filp, 0);
362         check();
363
364         igt_debug("\nIncrease min to RP0 (invalid)...\n");
365         writeval_inval(stuff[MIN].filp, origfreqs[RP0]);
366         check();
367
368         igt_debug("\nIncrease max to midpoint...\n");
369         writeval(stuff[MAX].filp, fmid);
370         check();
371
372         igt_debug("\nIncrease max to RP0...\n");
373         writeval(stuff[MAX].filp, origfreqs[RP0]);
374         check();
375
376         igt_debug("\nIncrease max above RP0 (invalid)...\n");
377         writeval_inval(stuff[MAX].filp, origfreqs[RP0] + 1000);
378         check();
379
380         writeval(stuff[MIN].filp, origfreqs[MIN]);
381         writeval(stuff[MAX].filp, origfreqs[MAX]);
382 }
383
384 static void basic_check(void)
385 {
386         int freqs[NUMFREQ];
387
388         read_freqs(freqs);
389         dump(freqs);
390         checkit(freqs);
391 }
392
393 #define IDLE_WAIT_TIMESTEP_MSEC 100
394 #define IDLE_WAIT_TIMEOUT_MSEC 10000
395 static void idle_check(void)
396 {
397         int freqs[NUMFREQ];
398         int wait = 0;
399
400         /* Monitor frequencies until cur settles down to min, which should
401          * happen within the allotted time */
402         do {
403                 read_freqs(freqs);
404                 dump(freqs);
405                 checkit(freqs);
406                 if (freqs[CUR] == freqs[MIN])
407                         break;
408                 usleep(1000 * IDLE_WAIT_TIMESTEP_MSEC);
409                 wait += IDLE_WAIT_TIMESTEP_MSEC;
410         } while (wait < IDLE_WAIT_TIMEOUT_MSEC);
411
412         igt_assert_cmpint(freqs[CUR], ==, freqs[MIN]);
413         igt_debug("Required %d msec to reach cur=min\n", wait);
414 }
415
416 #define LOADED_WAIT_TIMESTEP_MSEC 100
417 #define LOADED_WAIT_TIMEOUT_MSEC 3000
418 static void loaded_check(void)
419 {
420         int freqs[NUMFREQ];
421         int wait = 0;
422
423         /* Monitor frequencies until cur increases to max, which should
424          * happen within the allotted time */
425         do {
426                 read_freqs(freqs);
427                 dump(freqs);
428                 checkit(freqs);
429                 if (freqs[CUR] == freqs[MAX])
430                         break;
431                 usleep(1000 * LOADED_WAIT_TIMESTEP_MSEC);
432                 wait += LOADED_WAIT_TIMESTEP_MSEC;
433         } while (wait < LOADED_WAIT_TIMEOUT_MSEC);
434
435         igt_assert_cmpint(freqs[CUR], ==, freqs[MAX]);
436         igt_debug("Required %d msec to reach cur=max\n", wait);
437 }
438
439 #define STABILIZE_WAIT_TIMESTEP_MSEC 100
440 #define STABILIZE_WAIT_TIMEOUT_MSEC 10000
441 static void stabilize_check(int *freqs)
442 {
443         int wait = 0;
444
445         do {
446                 read_freqs(freqs);
447                 dump(freqs);
448                 usleep(1000 * STABILIZE_WAIT_TIMESTEP_MSEC);
449                 wait += STABILIZE_WAIT_TIMESTEP_MSEC;
450         } while (wait < STABILIZE_WAIT_TIMEOUT_MSEC);
451
452         igt_debug("Waited %d msec to stabilize cur\n", wait);
453 }
454
455 static void reset(void)
456 {
457         int pre_freqs[NUMFREQ];
458         int post_freqs[NUMFREQ];
459
460         /*
461          * quiescent_gpu upsets the gpu and makes it get pegged to max somehow.
462          * Don't ask.
463          */
464         sleep(10);
465
466         igt_debug("Apply low load...\n");
467         load_helper_run(LOW);
468         stabilize_check(pre_freqs);
469
470         igt_debug("Stop rings...\n");
471         igt_set_stop_rings(STOP_RING_DEFAULTS);
472         while (igt_get_stop_rings())
473                 usleep(1000 * 100);
474         igt_debug("Ring stop cleared\n");
475
476         igt_debug("Apply high load...\n");
477         load_helper_set_load(HIGH);
478         loaded_check();
479
480         igt_debug("Apply low load...\n");
481         load_helper_set_load(LOW);
482         stabilize_check(post_freqs);
483         matchit(pre_freqs, post_freqs);
484
485         igt_debug("Apply high load...\n");
486         load_helper_set_load(HIGH);
487         loaded_check();
488
489         igt_debug("Removing load...\n");
490         load_helper_stop();
491         idle_check();
492 }
493
494 static void blocking(void)
495 {
496         int pre_freqs[NUMFREQ];
497         int post_freqs[NUMFREQ];
498
499         int fd = drm_open_any();
500         igt_assert(fd >= 0);
501
502         /*
503          * quiescent_gpu upsets the gpu and makes it get pegged to max somehow.
504          * Don't ask.
505          */
506         sleep(10);
507
508         igt_debug("Apply low load...\n");
509         load_helper_run(LOW);
510         stabilize_check(pre_freqs);
511         load_helper_stop();
512
513         sleep(5);
514
515         igt_debug("Kick gpu hard ...\n");
516         /* This relies on the blocking waits in quiescent_gpu and the kernel
517          * boost logic to ramp the gpu to full load. */
518         gem_quiescent_gpu(fd);
519         gem_quiescent_gpu(fd);
520
521         igt_debug("Apply low load again...\n");
522         load_helper_run(LOW);
523         stabilize_check(post_freqs);
524         load_helper_stop();
525         matchit(pre_freqs, post_freqs);
526
527         igt_debug("Removing load...\n");
528         idle_check();
529 }
530
531 static void pm_rps_exit_handler(int sig)
532 {
533         if (origfreqs[MIN] > readval(stuff[MAX].filp)) {
534                 writeval(stuff[MAX].filp, origfreqs[MAX]);
535                 writeval(stuff[MIN].filp, origfreqs[MIN]);
536         } else {
537                 writeval(stuff[MIN].filp, origfreqs[MIN]);
538                 writeval(stuff[MAX].filp, origfreqs[MAX]);
539         }
540
541         load_helper_deinit();
542         close(drm_fd);
543 }
544
545 igt_main
546 {
547         igt_skip_on_simulation();
548
549         igt_fixture {
550                 const int device = drm_get_card();
551                 struct junk *junk = stuff;
552                 int ret;
553
554                 /* Use drm_open_any to verify device existence */
555                 drm_fd = drm_open_any();
556
557                 do {
558                         int val = -1;
559                         char *path;
560                         ret = asprintf(&path, sysfs_base_path, device, junk->name);
561                         igt_assert(ret != -1);
562                         junk->filp = fopen(path, junk->mode);
563                         igt_require(junk->filp);
564                         setbuf(junk->filp, NULL);
565
566                         val = readval(junk->filp);
567                         igt_assert(val >= 0);
568                         junk++;
569                 } while(junk->name != NULL);
570
571                 read_freqs(origfreqs);
572
573                 igt_install_exit_handler(pm_rps_exit_handler);
574
575                 load_helper_init();
576         }
577
578         igt_subtest("basic-api")
579                 min_max_config(basic_check);
580
581         igt_subtest("min-max-config-idle")
582                 min_max_config(idle_check);
583
584         igt_subtest("min-max-config-loaded") {
585                 load_helper_run(HIGH);
586                 min_max_config(loaded_check);
587                 load_helper_stop();
588         }
589
590         igt_subtest("reset")
591                 reset();
592
593         igt_subtest("blocking")
594                 blocking();
595 }