kms_rotation_crc: Remove useless comments
[platform/upstream/intel-gpu-tools.git] / tests / pm_rc6_residency.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  *
26  */
27
28 #define _GNU_SOURCE
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <errno.h>
34
35 #include "drmtest.h"
36
37 #define NUMBER_OF_RC6_RESIDENCY 3
38 #define SLEEP_DURATION 3000 // in milliseconds
39 #define RC6_FUDGE 900 // in milliseconds
40
41 static unsigned int readit(const char *path)
42 {
43         unsigned int ret;
44         int scanned;
45
46         FILE *file;
47         file = fopen(path, "r");
48         igt_assert(file);
49         scanned = fscanf(file, "%u", &ret);
50         igt_assert(scanned == 1);
51
52         fclose(file);
53
54         return ret;
55 }
56
57 static void read_rc6_residency( int value[], const char *name_of_rc6_residency[])
58 {
59         unsigned int i;
60         const int device = drm_get_card();
61         char *path ;
62         int  ret;
63         FILE *file;
64
65         /* For some reason my ivb isn't idle even after syncing up with the gpu.
66          * Let's add a sleept just to make it happy. */
67         sleep(5);
68
69         ret = asprintf(&path, "/sys/class/drm/card%d/power/rc6_enable", device);
70         igt_assert(ret != -1);
71
72         file = fopen(path, "r");
73         igt_require(file);
74
75         /* claim success if no rc6 enabled. */
76         if (readit(path) == 0)
77                 igt_success();
78
79         for(i = 0; i < 6; i++)
80         {
81                 if(i == 3)
82                         sleep(SLEEP_DURATION / 1000);
83                 ret = asprintf(&path, "/sys/class/drm/card%d/power/%s_residency_ms",device,name_of_rc6_residency[i % 3]);
84                 igt_assert(ret != -1);
85                 value[i] = readit(path);
86         }
87         free(path);
88 }
89
90 static void rc6_residency_counter(int value[],const char *name_of_rc6_residency[])
91 {
92         int flag;
93         unsigned int flag_counter,flag_support;
94         double counter_result = 0;
95         flag_counter = 0;
96         flag_support = 0;
97
98         for(flag = NUMBER_OF_RC6_RESIDENCY-1; flag >= 0; flag --)
99         {
100                 unsigned int  tmp_counter, tmp_support;
101                 double counter;
102                 counter = ((double)value[flag + 3] - (double)value[flag]) /(double) SLEEP_DURATION;
103
104                 if( counter > 0.9 ){
105                         counter_result = counter;
106                         tmp_counter = 1;
107                 }
108                 else
109                         tmp_counter = 0;
110
111                 if( value [flag + 3] == 0){
112                         tmp_support = 0;
113                         igt_info("This machine doesn't support %s\n", name_of_rc6_residency[flag]);
114                 }
115                 else
116                         tmp_support = 1;
117
118                 flag_counter = flag_counter + tmp_counter;
119                 flag_counter = flag_counter << 1;
120
121                 flag_support = flag_support + tmp_support;
122                 flag_support = flag_support << 1;
123         }
124
125         igt_info("The residency counter: %f \n", counter_result);
126
127         igt_skip_on_f(flag_support == 0 , "This machine didn't entry any RC6 state.\n");
128         igt_assert_f((flag_counter != 0) && (counter_result <=1) , "Sysfs RC6 residency counter is inaccurate.\n");
129
130         igt_info("This machine entry %s state.\n", name_of_rc6_residency[(flag_counter / 2) - 1]);
131 }
132
133 static void rc6_residency_check(int value[])
134 {
135         unsigned int diff;
136         diff = (value[3] - value[0]) +
137                         (value[4] - value[1]) +
138                         (value[5] - value[2]);
139
140         igt_assert_f(diff <= (SLEEP_DURATION + RC6_FUDGE),"Diff was too high. That is unpossible\n");
141         igt_assert_f(diff >= (SLEEP_DURATION - RC6_FUDGE),"GPU was not in RC6 long enough. Check that "
142                                                         "the GPU is as idle as possible(ie. no X, "
143                                                         "running and running no other tests)\n");
144 }
145
146 igt_main
147 {
148         int fd;
149         int value[6];
150         const char *name_of_rc6_residency[3]={"rc6","rc6p","rc6pp"};
151
152         igt_skip_on_simulation();
153
154         /* Use drm_open_any to verify device existence */
155         igt_fixture {
156                 fd = drm_open_any();
157                 close(fd);
158
159                 read_rc6_residency(value, name_of_rc6_residency);
160         }
161
162         igt_subtest("rc6-residency-check")
163                 rc6_residency_check(value);
164
165         igt_subtest("rc6-residency-counter")
166                 rc6_residency_counter(value, name_of_rc6_residency);
167 }