summaryrefslogtreecommitdiff
path: root/overlay/gpu-freq.c
blob: 23af1d485ad61bb26cf3801ac927e2cb6b204473 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>

#include "gpu-freq.h"

int gpu_freq_init(struct gpu_freq *gf)
{
	char buf[4096], *s;
	int fd, len = -1;

	memset(gf, 0, sizeof(*gf));

	fd = open("/sys/kernel/debug/dri/0/i915_cur_delayinfo", 0);
	if (fd < 0)
		return gf->error = errno;

	len = read(fd, buf, sizeof(buf)-1);
	close(fd);
	if (len < 0)
		goto err;

	buf[len] = '\0';

	s = strstr(buf, "(RPN)");
	if (s == NULL)
		goto err;
	sscanf(s, "(RPN) frequency: %dMHz", &gf->rpn);

	s = strstr(s, "(RP1)");
	if (s == NULL)
		goto err;
	sscanf(s, "(RP1) frequency: %dMHz", &gf->rp1);

	s = strstr(s, "(RP0)");
	if (s == NULL)
		goto err;
	sscanf(s, "(RP0) frequency: %dMHz", &gf->rp0);

	s = strstr(s, "Max");
	if (s == NULL)
		goto err;
	sscanf(s, "Max overclocked frequency: %dMHz", &gf->max);
	gf->min = gf->rpn;

	return 0;

err:
	return gf->error = EIO;
}

int gpu_freq_update(struct gpu_freq *gf)
{
	char buf[4096], *s;
	int fd, len = -1;

	if (gf->error)
		return gf->error;

	fd = open("/sys/kernel/debug/dri/0/i915_cur_delayinfo", 0);
	if (fd < 0)
		return gf->error = errno;

	len = read(fd, buf, sizeof(buf)-1);
	close(fd);
	if (len < 0)
		return gf->error = EIO;

	buf[len] = '\0';

	s = strstr(buf, "RPNSWREQ:");
	if (s)
		sscanf(s, "RPNSWREQ: %dMHz", &gf->request);

	s = strstr(buf, "CAGF:");
	if (s)
		sscanf(s, "CAGF: %dMHz", &gf->current);

	return 0;
}