Chris Wilson | b98bade | 2013-08-20 21:39:27 +0100 | [diff] [blame] | 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 | |
Chris Wilson | baa5be0 | 2013-08-20 09:27:34 +0100 | [diff] [blame] | 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 "rc6.h" |
| 35 | |
| 36 | int rc6_init(struct rc6 *rc6) |
| 37 | { |
| 38 | struct stat st; |
| 39 | int fd; |
| 40 | |
| 41 | memset(rc6, 0, sizeof(*rc6)); |
| 42 | |
| 43 | fd = stat("/sys/class/drm/card0/power", &st); |
| 44 | if (fd == -1) |
| 45 | return rc6->error = errno; |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | static uint64_t file_to_u64(const char *path) |
| 51 | { |
| 52 | char buf[4096]; |
| 53 | int fd, len; |
| 54 | |
| 55 | fd = open(path, 0); |
| 56 | if (fd < 0) |
| 57 | return 0; |
| 58 | |
| 59 | len = read(fd, buf, sizeof(buf)-1); |
| 60 | close(fd); |
| 61 | |
| 62 | if (len < 0) |
| 63 | return 0; |
| 64 | |
| 65 | buf[len] = '\0'; |
| 66 | |
| 67 | return strtoull(buf, 0, 0); |
| 68 | } |
| 69 | |
| 70 | static uint64_t clock_ms_to_u64(void) |
| 71 | { |
| 72 | struct timespec tv; |
| 73 | |
| 74 | if (clock_gettime(CLOCK_MONOTONIC, &tv) < 0) |
| 75 | return 0; |
| 76 | |
Chris Wilson | cf62d52 | 2013-08-20 10:58:02 +0100 | [diff] [blame] | 77 | return (uint64_t)tv.tv_sec * 1000 + tv.tv_nsec / 1000000; |
Chris Wilson | baa5be0 | 2013-08-20 09:27:34 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | int rc6_update(struct rc6 *rc6) |
| 81 | { |
| 82 | struct rc6_stat *s = &rc6->stat[rc6->count++&1]; |
| 83 | struct rc6_stat *d = &rc6->stat[rc6->count&1]; |
| 84 | uint64_t d_time, d_rc6, d_rc6p, d_rc6pp; |
| 85 | struct stat st; |
| 86 | |
| 87 | if (rc6->error) |
| 88 | return rc6->error; |
| 89 | |
Chris Wilson | baa5be0 | 2013-08-20 09:27:34 +0100 | [diff] [blame] | 90 | if (stat("/sys/class/drm/card0/power/rc6_residency_ms", &st) < 0) |
Chris Wilson | c888507 | 2013-08-20 11:08:13 +0100 | [diff] [blame] | 91 | return rc6->error = ENOENT; |
| 92 | |
| 93 | rc6->enabled = file_to_u64("/sys/class/drm/card0/power/rc6_enable"); |
| 94 | if (rc6->enabled == 0) |
| 95 | return EAGAIN; |
Chris Wilson | baa5be0 | 2013-08-20 09:27:34 +0100 | [diff] [blame] | 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 | |
| 102 | if (rc6->count == 1) |
| 103 | return EAGAIN; |
| 104 | |
| 105 | d_time = s->timestamp - d->timestamp; |
| 106 | |
| 107 | d_rc6 = s->rc6_residency - d->rc6_residency; |
| 108 | rc6->rc6 = 100 * d_rc6 / d_time; |
| 109 | |
| 110 | d_rc6p = s->rc6p_residency - d->rc6p_residency; |
| 111 | rc6->rc6p = 100 * d_rc6p / d_time; |
| 112 | |
| 113 | d_rc6pp = s->rc6pp_residency - d->rc6pp_residency; |
| 114 | rc6->rc6pp = 100 * d_rc6pp / d_time; |
| 115 | |
| 116 | rc6->rc6_combined = 100 * (d_rc6 + d_rc6p + d_rc6pp) / d_time; |
| 117 | return 0; |
| 118 | } |