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 | de2c97b | 2013-08-18 16:42:25 +0100 | [diff] [blame] | 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" |
Chris Wilson | 9574cb1 | 2013-08-23 15:51:21 +0100 | [diff] [blame^] | 32 | #include "debugfs.h" |
Chris Wilson | de2c97b | 2013-08-18 16:42:25 +0100 | [diff] [blame] | 33 | |
| 34 | int gpu_freq_init(struct gpu_freq *gf) |
| 35 | { |
| 36 | char buf[4096], *s; |
| 37 | int fd, len = -1; |
| 38 | |
| 39 | memset(gf, 0, sizeof(*gf)); |
| 40 | |
Chris Wilson | 9574cb1 | 2013-08-23 15:51:21 +0100 | [diff] [blame^] | 41 | sprintf(buf, "%s/i915_cur_delayinfo", debugfs_path); |
| 42 | fd = open(buf, 0); |
Chris Wilson | de2c97b | 2013-08-18 16:42:25 +0100 | [diff] [blame] | 43 | if (fd < 0) |
Chris Wilson | 5c81cda | 2013-08-20 10:04:23 +0100 | [diff] [blame] | 44 | return gf->error = errno; |
Chris Wilson | de2c97b | 2013-08-18 16:42:25 +0100 | [diff] [blame] | 45 | |
| 46 | len = read(fd, buf, sizeof(buf)-1); |
| 47 | close(fd); |
| 48 | if (len < 0) |
Chris Wilson | 5c81cda | 2013-08-20 10:04:23 +0100 | [diff] [blame] | 49 | goto err; |
Chris Wilson | de2c97b | 2013-08-18 16:42:25 +0100 | [diff] [blame] | 50 | |
| 51 | buf[len] = '\0'; |
| 52 | |
| 53 | s = strstr(buf, "(RPN)"); |
| 54 | if (s == NULL) |
Chris Wilson | 5c81cda | 2013-08-20 10:04:23 +0100 | [diff] [blame] | 55 | goto err; |
Chris Wilson | de2c97b | 2013-08-18 16:42:25 +0100 | [diff] [blame] | 56 | sscanf(s, "(RPN) frequency: %dMHz", &gf->rpn); |
| 57 | |
| 58 | s = strstr(s, "(RP1)"); |
| 59 | if (s == NULL) |
Chris Wilson | 5c81cda | 2013-08-20 10:04:23 +0100 | [diff] [blame] | 60 | goto err; |
Chris Wilson | de2c97b | 2013-08-18 16:42:25 +0100 | [diff] [blame] | 61 | sscanf(s, "(RP1) frequency: %dMHz", &gf->rp1); |
| 62 | |
| 63 | s = strstr(s, "(RP0)"); |
| 64 | if (s == NULL) |
Chris Wilson | 5c81cda | 2013-08-20 10:04:23 +0100 | [diff] [blame] | 65 | goto err; |
Chris Wilson | de2c97b | 2013-08-18 16:42:25 +0100 | [diff] [blame] | 66 | sscanf(s, "(RP0) frequency: %dMHz", &gf->rp0); |
| 67 | |
| 68 | s = strstr(s, "Max"); |
| 69 | if (s == NULL) |
Chris Wilson | 5c81cda | 2013-08-20 10:04:23 +0100 | [diff] [blame] | 70 | goto err; |
Chris Wilson | de2c97b | 2013-08-18 16:42:25 +0100 | [diff] [blame] | 71 | sscanf(s, "Max overclocked frequency: %dMHz", &gf->max); |
Chris Wilson | 7d950fa | 2013-08-18 18:27:47 +0100 | [diff] [blame] | 72 | gf->min = gf->rpn; |
Chris Wilson | de2c97b | 2013-08-18 16:42:25 +0100 | [diff] [blame] | 73 | |
| 74 | return 0; |
Chris Wilson | 5c81cda | 2013-08-20 10:04:23 +0100 | [diff] [blame] | 75 | |
| 76 | err: |
| 77 | return gf->error = EIO; |
Chris Wilson | de2c97b | 2013-08-18 16:42:25 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | int gpu_freq_update(struct gpu_freq *gf) |
| 81 | { |
| 82 | char buf[4096], *s; |
| 83 | int fd, len = -1; |
| 84 | |
Chris Wilson | 5c81cda | 2013-08-20 10:04:23 +0100 | [diff] [blame] | 85 | if (gf->error) |
| 86 | return gf->error; |
| 87 | |
Chris Wilson | 9574cb1 | 2013-08-23 15:51:21 +0100 | [diff] [blame^] | 88 | sprintf(buf, "%s/i915_cur_delayinfo", debugfs_path); |
| 89 | fd = open(buf, 0); |
Chris Wilson | de2c97b | 2013-08-18 16:42:25 +0100 | [diff] [blame] | 90 | if (fd < 0) |
Chris Wilson | 5c81cda | 2013-08-20 10:04:23 +0100 | [diff] [blame] | 91 | return gf->error = errno; |
Chris Wilson | de2c97b | 2013-08-18 16:42:25 +0100 | [diff] [blame] | 92 | |
| 93 | len = read(fd, buf, sizeof(buf)-1); |
| 94 | close(fd); |
| 95 | if (len < 0) |
Chris Wilson | 5c81cda | 2013-08-20 10:04:23 +0100 | [diff] [blame] | 96 | return gf->error = EIO; |
Chris Wilson | de2c97b | 2013-08-18 16:42:25 +0100 | [diff] [blame] | 97 | |
| 98 | buf[len] = '\0'; |
| 99 | |
| 100 | s = strstr(buf, "RPNSWREQ:"); |
| 101 | if (s) |
| 102 | sscanf(s, "RPNSWREQ: %dMHz", &gf->request); |
| 103 | |
| 104 | s = strstr(buf, "CAGF:"); |
| 105 | if (s) |
| 106 | sscanf(s, "CAGF: %dMHz", &gf->current); |
| 107 | |
| 108 | return 0; |
| 109 | } |