blob: e69b1a19a50c76340ae2c71a7d8b08dcc801e3c2 [file] [log] [blame]
Chris Wilsonb98bade2013-08-20 21:39:27 +01001/*
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
Chris Wilson5c81cda2013-08-20 10:04:23 +010025#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28#include <unistd.h>
29#include <fcntl.h>
30#include <time.h>
31#include <errno.h>
32
33#include "power.h"
Chris Wilson9574cb12013-08-23 15:51:21 +010034#include "debugfs.h"
Chris Wilson5c81cda2013-08-20 10:04:23 +010035
36/* XXX Is this exposed through RAPL? */
37
38int power_init(struct power *power)
39{
40 char buf[4096];
41 int fd, len;
42
43 memset(power, 0, sizeof(*power));
44
Chris Wilson9574cb12013-08-23 15:51:21 +010045 sprintf(buf, "%s/i915_energy_uJ", debugfs_path);
46 fd = open(buf, 0);
Chris Wilson5c81cda2013-08-20 10:04:23 +010047 if (fd < 0)
48 return power->error = errno;
49
50 len = read(fd, buf, sizeof(buf));
51 close(fd);
52
53 if (len < 0)
54 return power->error = errno;
55
56 return 0;
57}
58
Chris Wilson9574cb12013-08-23 15:51:21 +010059static uint64_t file_to_u64(const char *name)
Chris Wilson5c81cda2013-08-20 10:04:23 +010060{
61 char buf[4096];
62 int fd, len;
63
Chris Wilson9574cb12013-08-23 15:51:21 +010064 sprintf(buf, "%s/i915_energy_uJ", name);
65 fd = open(buf, 0);
Chris Wilson5c81cda2013-08-20 10:04:23 +010066 if (fd < 0)
67 return 0;
68
69 len = read(fd, buf, sizeof(buf)-1);
70 close(fd);
71
72 if (len < 0)
73 return 0;
74
75 buf[len] = '\0';
76
77 return strtoull(buf, 0, 0);
78}
79
80static uint64_t clock_ms_to_u64(void)
81{
82 struct timespec tv;
83
84 if (clock_gettime(CLOCK_MONOTONIC, &tv) < 0)
85 return 0;
86
Chris Wilsoncf62d522013-08-20 10:58:02 +010087 return (uint64_t)tv.tv_sec * 1000 + tv.tv_nsec / 1000000;
Chris Wilson5c81cda2013-08-20 10:04:23 +010088}
89
90int power_update(struct power *power)
91{
92 struct power_stat *s = &power->stat[power->count++&1];
93 struct power_stat *d = &power->stat[power->count&1];
94 uint64_t d_time;
95
96 if (power->error)
97 return power->error;
98
Chris Wilson9574cb12013-08-23 15:51:21 +010099 s->energy = file_to_u64("i915_energy_uJ");
Chris Wilson5c81cda2013-08-20 10:04:23 +0100100 s->timestamp = clock_ms_to_u64();
101 if (power->count == 1)
102 return EAGAIN;
103
104 d_time = s->timestamp - d->timestamp;
Chris Wilsonc6482b72013-08-20 11:09:11 +0100105 if (d_time < 900) { /* HW sample rate seems to be stable ~1Hz */
Chris Wilson5c81cda2013-08-20 10:04:23 +0100106 power->count--;
107 return power->count <= 1 ? EAGAIN : 0;
108 }
109
110 power->power_mW = (s->energy - d->energy) / d_time;
111 power->new_sample = 1;
112 return 0;
113}