2 * Copyright © 2013 Intel Corporation
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:
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
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
35 static int perf_i915_open(int config, int group)
37 struct perf_event_attr attr;
39 memset(&attr, 0, sizeof (attr));
41 attr.type = i915_type_id();
46 attr.read_format = PERF_FORMAT_TOTAL_TIME_ENABLED;
48 attr.read_format |= PERF_FORMAT_GROUP;
50 return perf_event_open(&attr, -1, 0, group, 0);
53 static int perf_open(void)
57 fd = perf_i915_open(I915_PERF_ACTUAL_FREQUENCY, -1);
58 if (perf_i915_open(I915_PERF_REQUESTED_FREQUENCY, fd) < 0) {
66 int gpu_freq_init(struct gpu_freq *gf)
71 memset(gf, 0, sizeof(*gf));
75 sprintf(buf, "%s/i915_frequency_info", debugfs_dri_path);
78 sprintf(buf, "%s/i915_cur_delayinfo", debugfs_dri_path);
82 return gf->error = errno;
84 len = read(fd, buf, sizeof(buf)-1);
91 if (strstr(buf, "PUNIT_REG_GPU_FREQ_STS")) {
92 /* Baytrail is special, ofc. */
94 s = strstr(buf, "max");
97 sscanf(s, "max GPU freq: %d MHz", &gf->max);
98 sscanf(s, "min GPU freq: %d MHz", &gf->min);
100 gf->rp0 = gf->rp1 = gf->max;
103 s = strstr(buf, "(RPN)");
106 sscanf(s, "(RPN) frequency: %dMHz", &gf->rpn);
108 s = strstr(s, "(RP1)");
111 sscanf(s, "(RP1) frequency: %dMHz", &gf->rp1);
113 s = strstr(s, "(RP0)");
116 sscanf(s, "(RP0) frequency: %dMHz", &gf->rp0);
118 s = strstr(s, "Max");
121 sscanf(s, "Max overclocked frequency: %dMHz", &gf->max);
128 return gf->error = EIO;
131 int gpu_freq_update(struct gpu_freq *gf)
140 sprintf(buf, "%s/i915_frequency_info", debugfs_dri_path);
143 sprintf(buf, "%s/i915_cur_delayinfo", debugfs_dri_path);
147 return gf->error = errno;
149 len = read(fd, buf, sizeof(buf)-1);
152 return gf->error = EIO;
157 s = strstr(buf, "current");
159 sscanf(s, "current GPU freq: %d MHz", &gf->current);
160 gf->request = gf->current;
162 s = strstr(buf, "RPNSWREQ:");
164 sscanf(s, "RPNSWREQ: %dMHz", &gf->request);
166 s = strstr(buf, "CAGF:");
168 sscanf(s, "CAGF: %dMHz", &gf->current);
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;
176 len = read(gf->fd, data, sizeof(data));
178 return gf->error = errno;
180 s->timestamp = data[1];
187 d_time = s->timestamp - d->timestamp;
193 gf->current = (s->act - d->act) / d_time;
194 gf->request = (s->req - d->req) / d_time;