overlay: Baytrail needs a custom GPU frequency parser
[platform/upstream/intel-gpu-tools.git] / overlay / gpu-freq.c
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 <unistd.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <stdio.h>
30
31 #include "gpu-freq.h"
32 #include "debugfs.h"
33 #include "perf.h"
34
35 static int perf_i915_open(int config, int group)
36 {
37         struct perf_event_attr attr;
38
39         memset(&attr, 0, sizeof (attr));
40
41         attr.type = i915_type_id();
42         if (attr.type == 0)
43                 return -ENOENT;
44         attr.config = config;
45
46         attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED;
47         if (group == -1)
48                 attr.read_format |= PERF_FORMAT_GROUP;
49
50         return perf_event_open(&attr, -1, 0, group, 0);
51 }
52
53 static int perf_open(void)
54 {
55         int fd;
56
57         fd = perf_i915_open(I915_PERF_ACTUAL_FREQUENCY, -1);
58         if (perf_i915_open(I915_PERF_REQUESTED_FREQUENCY, fd) < 0) {
59                 close(fd);
60                 fd = -1;
61         }
62
63         return fd;
64 }
65
66 int gpu_freq_init(struct gpu_freq *gf)
67 {
68         char buf[4096], *s;
69         int fd, len = -1;
70
71         memset(gf, 0, sizeof(*gf));
72
73         gf->fd = perf_open();
74
75         sprintf(buf, "%s/i915_frequency_info", debugfs_dri_path);
76         fd = open(buf, 0);
77         if (fd < 0) {
78                 sprintf(buf, "%s/i915_cur_delayinfo", debugfs_dri_path);
79                 fd = open(buf, 0);
80         }
81         if (fd < 0)
82                 return gf->error = errno;
83
84         len = read(fd, buf, sizeof(buf)-1);
85         close(fd);
86         if (len < 0)
87                 goto err;
88
89         buf[len] = '\0';
90
91         if (strstr(buf, "PUNIT_REG_GPU_FREQ_STS")) {
92                 /* Baytrail is special, ofc. */
93                 gf->is_byt = 1;
94                 s = strstr(buf, "max");
95                 if (s == NULL)
96                         goto err;
97                 sscanf(s, "max GPU freq: %d MHz", &gf->max);
98                 sscanf(s, "min GPU freq: %d MHz", &gf->min);
99
100                 gf->rp0 = gf->rp1 = gf->max;
101                 gf->rpn = gf->min;
102         } else {
103                 s = strstr(buf, "(RPN)");
104                 if (s == NULL)
105                         goto err;
106                 sscanf(s, "(RPN) frequency: %dMHz", &gf->rpn);
107
108                 s = strstr(s, "(RP1)");
109                 if (s == NULL)
110                         goto err;
111                 sscanf(s, "(RP1) frequency: %dMHz", &gf->rp1);
112
113                 s = strstr(s, "(RP0)");
114                 if (s == NULL)
115                         goto err;
116                 sscanf(s, "(RP0) frequency: %dMHz", &gf->rp0);
117
118                 s = strstr(s, "Max");
119                 if (s == NULL)
120                         goto err;
121                 sscanf(s, "Max overclocked frequency: %dMHz", &gf->max);
122                 gf->min = gf->rpn;
123         }
124
125         return 0;
126
127 err:
128         return gf->error = EIO;
129 }
130
131 int gpu_freq_update(struct gpu_freq *gf)
132 {
133         if (gf->error)
134                 return gf->error;
135
136         if (gf->fd < 0) {
137                 char buf[4096], *s;
138                 int fd, len = -1;
139
140                 sprintf(buf, "%s/i915_frequency_info", debugfs_dri_path);
141                 fd = open(buf, 0);
142                 if (fd < 0) {
143                         sprintf(buf, "%s/i915_cur_delayinfo", debugfs_dri_path);
144                         fd = open(buf, 0);
145                 }
146                 if (fd < 0)
147                         return gf->error = errno;
148
149                 len = read(fd, buf, sizeof(buf)-1);
150                 close(fd);
151                 if (len < 0)
152                         return gf->error = EIO;
153
154                 buf[len] = '\0';
155
156                 if (gf->is_byt) {
157                         s = strstr(buf, "current");
158                         if (s)
159                                 sscanf(s, "current GPU freq: %d MHz", &gf->current);
160                         gf->request = gf->current;
161                 } else {
162                         s = strstr(buf, "RPNSWREQ:");
163                         if (s)
164                                 sscanf(s, "RPNSWREQ: %dMHz", &gf->request);
165
166                         s = strstr(buf, "CAGF:");
167                         if (s)
168                                 sscanf(s, "CAGF: %dMHz", &gf->current);
169                 }
170         } else {
171                 struct gpu_freq_stat *s = &gf->stat[gf->count++&1];
172                 struct gpu_freq_stat *d = &gf->stat[gf->count&1];
173                 uint64_t data[4], d_time;
174                 int len;
175
176                 len = read(gf->fd, data, sizeof(data));
177                 if (len < 0)
178                         return gf->error = errno;
179
180                 s->timestamp = data[1];
181                 s->act = data[2];
182                 s->req = data[3];
183
184                 if (gf->count == 1)
185                         return EAGAIN;
186
187                 d_time = s->timestamp - d->timestamp;
188                 if (d_time == 0) {
189                         gf->count--;
190                         return EAGAIN;
191                 }
192
193                 gf->current = (s->act - d->act) / d_time;
194                 gf->request = (s->req - d->req) / d_time;
195         }
196
197         return 0;
198 }