1 /*
2 * Copyright © 2013 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 */
24
25 #include <sys/stat.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <time.h>
32 #include <errno.h>
33
34 #include "igt_perf.h"
35
36 #include "rc6.h"
37
rc6_init(struct rc6 * rc6)38 int rc6_init(struct rc6 *rc6)
39 {
40 memset(rc6, 0, sizeof(*rc6));
41
42 rc6->fd = perf_i915_open(I915_PMU_RC6_RESIDENCY);
43 if (rc6->fd < 0) {
44 struct stat st;
45 if (stat("/sys/class/drm/card0/power", &st) < 0)
46 return rc6->error = errno;
47 }
48
49 return 0;
50 }
51
file_to_u64(const char * path)52 static uint64_t file_to_u64(const char *path)
53 {
54 char buf[4096];
55 int fd, len;
56
57 fd = open(path, 0);
58 if (fd < 0)
59 return -1;
60
61 len = read(fd, buf, sizeof(buf)-1);
62 close(fd);
63
64 if (len < 0)
65 return -1;
66
67 buf[len] = '\0';
68
69 return strtoull(buf, 0, 0);
70 }
71
clock_ms_to_u64(void)72 static uint64_t clock_ms_to_u64(void)
73 {
74 struct timespec tv;
75
76 if (clock_gettime(CLOCK_MONOTONIC, &tv) < 0)
77 return 0;
78
79 return (uint64_t)tv.tv_sec * 1000 + tv.tv_nsec / 1000000;
80 }
81
rc6_update(struct rc6 * rc6)82 int rc6_update(struct rc6 *rc6)
83 {
84 struct rc6_stat *s = &rc6->stat[rc6->count++&1];
85 struct rc6_stat *d = &rc6->stat[rc6->count&1];
86 uint64_t d_time, d_rc6, d_rc6p, d_rc6pp;
87
88 if (rc6->error)
89 return rc6->error;
90
91 if (rc6->fd < 0) {
92 struct stat st;
93
94 if (stat("/sys/class/drm/card0/power/rc6_residency_ms", &st) < 0)
95 return rc6->error = ENOENT;
96
97 s->rc6_residency = file_to_u64("/sys/class/drm/card0/power/rc6_residency_ms");
98 s->rc6p_residency = file_to_u64("/sys/class/drm/card0/power/rc6p_residency_ms");
99 s->rc6pp_residency = file_to_u64("/sys/class/drm/card0/power/rc6pp_residency_ms");
100 s->timestamp = clock_ms_to_u64();
101 } else {
102 uint64_t data[2];
103
104 if (read(rc6->fd, data, sizeof(data)) < sizeof(data))
105 return rc6->error = errno;
106
107 s->timestamp = data[1] / 1e6;
108 s->rc6_residency = data[0] / 1e6;
109 }
110
111 if (rc6->count == 1)
112 return EAGAIN;
113
114 d_time = s->timestamp - d->timestamp;
115 if (d_time == 0) {
116 rc6->count--;
117 return EAGAIN;
118 }
119
120 d_rc6 = s->rc6_residency - d->rc6_residency;
121 rc6->rc6 = 100 * d_rc6 / d_time;
122
123 d_rc6p = s->rc6p_residency - d->rc6p_residency;
124 rc6->rc6p = 100 * d_rc6p / d_time;
125
126 d_rc6pp = s->rc6pp_residency - d->rc6pp_residency;
127 rc6->rc6pp = 100 * d_rc6pp / d_time;
128
129 rc6->rc6_combined = 100 * (d_rc6 + d_rc6p + d_rc6pp) / d_time;
130 return 0;
131 }
132