linux kernel code tool power x86 x86_energy_perf_policy.c 2
void cmdline(int argc, char **argv)
{
int opt;
progname = argv[0];
while ((opt = getopt(argc, argv, “+rvc:”)) != -1) {
switch (opt) {
case ‘c’:
cpu = atoi(optarg);
break;
case ‘r’:
read_only = 1;
break;
case ‘v’:
verbose++;
break;
default:
usage();
}
}
/* if -r, then should be no additional optind */
if (read_only && (argc > optind))
usage();
/*
* if no -r , then must be one additional optind
*/
if (!read_only) {
if (argc != optind + 1) {
printf(“must supply -r or policy param\n”);
usage();
}
if (!strcmp(“performance”, argv[optind])) {
new_bias = BIAS_PERFORMANCE;
} else if (!strcmp(“normal”, argv[optind])) {
new_bias = BIAS_BALANCE;
} else if (!strcmp(“powersave”, argv[optind])) {
new_bias = BIAS_POWERSAVE;
} else {
char *endptr;
new_bias = strtoull(argv[optind], &endptr, 0);
if (endptr == argv[optind] ||
new_bias > BIAS_POWERSAVE) {
fprintf(stderr, “invalid value: %s\n”,
argv[optind]);
usage();
}
}
}
}
/*
* validate_cpuid()
* returns on success, quietly exits on failure (make verbose with -v)
*/
void validate_cpuid(void)
{
unsigned int eax, ebx, ecx, edx, max_level;
char brand[16];
unsigned int fms, family, model, stepping;
eax = ebx = ecx = edx = 0;
asm(“cpuid” : “=a” (max_level), “=b” (ebx), “=c” (ecx),
“=d” (edx) : “a” (0));
if (ebx != 0x756e6547 || edx != 0x49656e69 || ecx != 0x6c65746e) {
if (verbose)
fprintf(stderr, “%.4s%.4s%.4s != GenuineIntel”,
(char *)&ebx, (char *)&edx, (char *)&ecx);
exit(1);
}
asm(“cpuid” : “=a” (fms), “=c” (ecx), “=d” (edx) : “a” (1) : “ebx”);
family = (fms >>
& 0xf;
model = (fms >> 4) & 0xf;
stepping = fms & 0xf;
if (family == 6 || family == 0xf)
model += ((fms >> 16) & 0xf) << 4;
if (verbose > 1)
printf(“CPUID %s %d levels family:model:stepping ”
“0x%x:%x:%x (%d:%d:%d)\n”, brand, max_level,
family, model, stepping, family, model, stepping);
if (!(edx & (1 << 5))) {
if (verbose)
printf(“CPUID: no MSR\n”);
exit(1);
}
/*
* Support for MSR_IA32_ENERGY_PERF_BIAS
* is indicated by CPUID.06H.ECX.bit3
*/
asm(“cpuid” : “=a” (eax), “=b” (ebx), “=c” (ecx), “=d” (edx) : “a” (6));
if (verbose)
printf(“CPUID.06H.ECX: 0x%x\n”, ecx);
if (!(ecx & (1 << 3))) {
if (verbose)
printf(“CPUID: No MSR_IA32_ENERGY_PERF_BIAS\n”);
exit(1);
}
return; /* success */
}