blob: b34f66f2984de6e52d837f5a4755317e1f356c27 [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 Wilsonde2c97b2013-08-18 16:42:25 +010025#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 Wilson9574cb12013-08-23 15:51:21 +010032#include "debugfs.h"
Chris Wilsonde2c97b2013-08-18 16:42:25 +010033
34int 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 Wilson9574cb12013-08-23 15:51:21 +010041 sprintf(buf, "%s/i915_cur_delayinfo", debugfs_path);
42 fd = open(buf, 0);
Chris Wilsonde2c97b2013-08-18 16:42:25 +010043 if (fd < 0)
Chris Wilson5c81cda2013-08-20 10:04:23 +010044 return gf->error = errno;
Chris Wilsonde2c97b2013-08-18 16:42:25 +010045
46 len = read(fd, buf, sizeof(buf)-1);
47 close(fd);
48 if (len < 0)
Chris Wilson5c81cda2013-08-20 10:04:23 +010049 goto err;
Chris Wilsonde2c97b2013-08-18 16:42:25 +010050
51 buf[len] = '\0';
52
53 s = strstr(buf, "(RPN)");
54 if (s == NULL)
Chris Wilson5c81cda2013-08-20 10:04:23 +010055 goto err;
Chris Wilsonde2c97b2013-08-18 16:42:25 +010056 sscanf(s, "(RPN) frequency: %dMHz", &gf->rpn);
57
58 s = strstr(s, "(RP1)");
59 if (s == NULL)
Chris Wilson5c81cda2013-08-20 10:04:23 +010060 goto err;
Chris Wilsonde2c97b2013-08-18 16:42:25 +010061 sscanf(s, "(RP1) frequency: %dMHz", &gf->rp1);
62
63 s = strstr(s, "(RP0)");
64 if (s == NULL)
Chris Wilson5c81cda2013-08-20 10:04:23 +010065 goto err;
Chris Wilsonde2c97b2013-08-18 16:42:25 +010066 sscanf(s, "(RP0) frequency: %dMHz", &gf->rp0);
67
68 s = strstr(s, "Max");
69 if (s == NULL)
Chris Wilson5c81cda2013-08-20 10:04:23 +010070 goto err;
Chris Wilsonde2c97b2013-08-18 16:42:25 +010071 sscanf(s, "Max overclocked frequency: %dMHz", &gf->max);
Chris Wilson7d950fa2013-08-18 18:27:47 +010072 gf->min = gf->rpn;
Chris Wilsonde2c97b2013-08-18 16:42:25 +010073
74 return 0;
Chris Wilson5c81cda2013-08-20 10:04:23 +010075
76err:
77 return gf->error = EIO;
Chris Wilsonde2c97b2013-08-18 16:42:25 +010078}
79
80int gpu_freq_update(struct gpu_freq *gf)
81{
82 char buf[4096], *s;
83 int fd, len = -1;
84
Chris Wilson5c81cda2013-08-20 10:04:23 +010085 if (gf->error)
86 return gf->error;
87
Chris Wilson9574cb12013-08-23 15:51:21 +010088 sprintf(buf, "%s/i915_cur_delayinfo", debugfs_path);
89 fd = open(buf, 0);
Chris Wilsonde2c97b2013-08-18 16:42:25 +010090 if (fd < 0)
Chris Wilson5c81cda2013-08-20 10:04:23 +010091 return gf->error = errno;
Chris Wilsonde2c97b2013-08-18 16:42:25 +010092
93 len = read(fd, buf, sizeof(buf)-1);
94 close(fd);
95 if (len < 0)
Chris Wilson5c81cda2013-08-20 10:04:23 +010096 return gf->error = EIO;
Chris Wilsonde2c97b2013-08-18 16:42:25 +010097
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}