File: if_linux.c

package info (click to toggle)
gkrellongrun 2.2.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 132 kB
  • ctags: 140
  • sloc: ansic: 975; makefile: 51
file content (269 lines) | stat: -rw-r--r-- 6,333 bytes parent folder | download
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * if_linux.c
 *
 * GKrellM LongRun Plugin
 * Copyright (c) 2001-2003 Masaharu FUJITA
 *
 * Initial work by: (c) 2001 Nozomi Sato (nozomi@palette.plala.or.jp)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 * You may contact the author by:
 *	e-mail: m@fjts.org
 *	url: http://fjts.org/~m/Soft/GKrelLongRun/ (in Japanese)
 *	     http://fjts.org/~m/Soft/GKrelLongRun/index_e.html (in English)
 *
 */
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>

#define MSR_TMx86_LONGRUN		0x80868010
#define MSR_TMx86_LONGRUN_FLAGS		0x80868011

#define CPUID_TMx86_VENDOR_ID           0x80860000
#define CPUID_TMx86_PROCESSOR_INFO      0x80860001
#define CPUID_TMx86_LONGRUN_STATUS      0x80860007
#define CPUID_TMx86_FEATURE_LONGRUN(x) ((x) & 0x02)

#define LONGRUN_MASK(x) ((x) & 0x0000007f)
#define LONGRUN_RESERVED(x) ((x) & 0xffffff80)
#define LONGRUN_WRITE(x, y) (LONGRUN_RESERVED(x) | LONGRUN_MASK(y))

typedef enum {
	Economy,
	Performance,
} ModeType;

typedef enum {
	Minimum,
	Variableness,
	Maxmum,
} FrequencyType;

static int cpuid_fd = -1;
static int msr_fd = -1; 

static const char *msr_device = "/dev/cpu/0/msr";
static const char *cpuid_device = "/dev/cpu/0/cpuid";
gchar *longrun_mode_label[] = { "Eco.", "Per." };

static int max_frequency = 0;
static int min_frequency = 0;

static int
open_devices()
{
	if ((cpuid_fd = open(cpuid_device, O_RDONLY)) < 0) {
		fprintf(stderr, "gkrellongrun: %s : %s\n",
			strerror(errno), cpuid_device);
		return -1;
	}
	if ((msr_fd = open(msr_device, O_RDWR)) < 0) {
		fprintf(stderr, "gkrellongrun: %s : %s\n",
			strerror(errno), msr_device);
		return -1;
	}
	return 0;
}

static void
close_devices()
{
	if (cpuid_fd > 0)
		close(cpuid_fd); 
	if (msr_fd > 0)
		close(msr_fd); 
	cpuid_fd = msr_fd = -1;
}

static void
read_cpuid( off_t address, int *eax, int *ebx, int *ecx, int *edx )
{
	uint32_t data[4];

	if ( pread( cpuid_fd, &data, 16, address ) != 16 )
		data[0] = data[1] = data[2] = data[3] = 0;

	if ( eax )
		*eax = data[0];
	if ( ebx )
		*ebx = data[1];
	if ( ecx )
		*ecx = data[2]; 
	if ( edx )
		*edx = data[3]; 
}

static void
read_msr( off_t address, int *lower, int *upper )
{
	uint32_t data[2];

	if (pread(msr_fd, &data, 8, address) != 8) {
		return;
	}

	if (lower)
		*lower = data[0];
	if (upper)
		*upper = data[1];
}

static void
write_msr(off_t address, int lower, int upper)
{
	uint32_t data[2];

	data[0] = (uint32_t)lower;
	data[1] = (uint32_t)upper;

	pwrite(msr_fd, &data, 8, address);
}


static void
set_longrun_mode(GtkWidget *widget, gpointer data)
{
	int lower, upper;

	read_msr(MSR_TMx86_LONGRUN_FLAGS, &lower, &upper);
	switch (GPOINTER_TO_INT(data)) {
		case Economy:
			write_msr( MSR_TMx86_LONGRUN_FLAGS,
					lower & 0xfffffffe, upper);
			break;
		case Performance:
			write_msr( MSR_TMx86_LONGRUN_FLAGS,
					(lower & 0xffffffff) | 0x1, upper);
			break;
	}
}

static void
set_longrun_frequency(GtkWidget *widget, gpointer data)
{
	int lower, upper, low, high;

	switch (GPOINTER_TO_INT(data)) {
		case Minimum:
			low = min_frequency;
			high = min_frequency;
			break;
		case Variableness:
			low = min_frequency;
			high = max_frequency;
			break;
		case Maxmum:
			low = max_frequency;
			high = max_frequency;
			break;
		default:
			return;
	}
	low = ((low - min_frequency) * 100)/(max_frequency - min_frequency);
	high = ((high - min_frequency) * 100)/(max_frequency - min_frequency);

	read_msr(MSR_TMx86_LONGRUN, &lower, &upper);
	write_msr(MSR_TMx86_LONGRUN, LONGRUN_WRITE(lower, low),
			LONGRUN_WRITE(upper, high));
}

/*
 * read_longrun_data
 */
void
read_longrun_data()
{
	int lower, upper;

	read_cpuid( CPUID_TMx86_LONGRUN_STATUS,
			&longrun.data[FrequencyData],
			&longrun.data[VoltageData],
			&longrun.data[PercentageData], NULL );

	read_msr(MSR_TMx86_LONGRUN_FLAGS, &lower, &upper );
	longrun.data[LongRunData] = (lower & 1) ? Performance : Economy;

	set_longrun_label();
}

/*
 * check_cpu
 */
int
check_cpu()
{
	int eax, ebx, ecx, edx;
	int lower, upper;

	if (open_devices()) {
		close_devices();
		return -1;
	}

	/* test for "TransmetaCPU" */
	read_cpuid(CPUID_TMx86_VENDOR_ID, &eax, &ebx, &ecx, &edx);
	if (ebx != 0x6e617254 || ecx != 0x55504361 || edx != 0x74656d73) {
		close_devices();
		return -1;
	}
	/* test for LongRun feature flag */
	read_cpuid(CPUID_TMx86_PROCESSOR_INFO, &eax, &ebx, &ecx, &edx);
	if (!CPUID_TMx86_FEATURE_LONGRUN(edx)) {
		close_devices();
		return -1;
	}

	/* get maximum & minimum frequency */
	read_msr(MSR_TMx86_LONGRUN, &lower, &upper);
	read_cpuid(CPUID_TMx86_PROCESSOR_INFO, 0, 0, &max_frequency, 0);
	write_msr(MSR_TMx86_LONGRUN, LONGRUN_WRITE(lower, 0),
			LONGRUN_WRITE(upper, 0));
	read_cpuid(CPUID_TMx86_LONGRUN_STATUS, &min_frequency, 0, 0, 0);
	write_msr(MSR_TMx86_LONGRUN, lower, upper);

	return 0;
}

GtkItemFactoryEntry gkrellongrun_factory_entry[] =
{
	{"/-",		NULL, NULL, 0,"<Separator>"},
	{"/Mode",	NULL, NULL, 0, "<Item>"},
	{"/-",		NULL, NULL, 0,"<Separator>"},
	{"/Economy",	NULL, set_longrun_mode, Economy, "<Item>"},
	{"/Performance", NULL, set_longrun_mode, Performance, "<Item>"},
	{"/-",		NULL, NULL, 0,"<Separator>"},
	{"/Frequency",	NULL, NULL, 0, "<Item>"},
	{"/-",		NULL, NULL, 0,"<Separator>"},
	{"/Minimum",	NULL, set_longrun_frequency, Minimum, "<Item>"},
	{"/Variableness", NULL, set_longrun_frequency, Variableness, "<Item>"},
	{"/Maxmum",	NULL, set_longrun_frequency, Maxmum, "<Item>"},
	{"/-",		NULL, NULL, 0,"<Separator>"},
};


/*
 * get_gkrellongrun_itemfactory_size
 */
int
get_gkrellongrun_itemfactory_size()
{
	return sizeof(gkrellongrun_factory_entry)/sizeof(GtkItemFactoryEntry);
}