Răsfoiți Sursa

Add some options for power saving

Johannes Zellner 7 luni în urmă
părinte
comite
25a9ef928d
6 a modificat fișierele cu 641 adăugiri și 15 ștergeri
  1. 8 5
      Makefile
  2. 499 9
      cpuinfo.c
  3. 85 0
      idle.c
  4. 11 0
      idle.h
  5. 2 0
      kernel.c
  6. 36 1
      terminal.c

+ 8 - 5
Makefile

@@ -20,7 +20,7 @@ all: floppy.img usb.img final.bin root.img
 boot.o: boot.S
 	$(AS) $(ASFLAGS) -o $@ $<
 
-kernel.o: kernel.c io.h process.h terminal.h timer.h serial.h vga.h
+kernel.o: kernel.c idle.h io.h process.h terminal.h timer.h serial.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 io.o: io.c io.h fbtext.h
@@ -32,7 +32,7 @@ font_8x8.o: font_8x8.c font_8x8.h
 fbtext.o: fbtext.c fbtext.h font_8x8.h vga.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-terminal.o: terminal.c terminal.h io.h fbtext.h process.h rtc.h timer.h disk.h
+terminal.o: terminal.c terminal.h io.h fbtext.h idle.h process.h rtc.h timer.h disk.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 hello.o: hello.c io.h
@@ -83,7 +83,10 @@ colors.o: colors.c fbtext.h io.h timer.h vga.h
 display.o: display.c io.h vga.h gpu.h ati.h fbtext.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-cpuinfo.o: cpuinfo.c io.h timer.h
+cpuinfo.o: cpuinfo.c io.h timer.h idle.h
+	$(CC) $(CFLAGS) -o $@ $<
+
+idle.o: idle.c idle.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 boot.elf: boot.o
@@ -101,7 +104,7 @@ switch.o: switch.S
 process.o: process.c process.h io.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o disk.o fat.o ls.o cat.o run.o serial.o vga.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o
+kernel.elf: vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o disk.o fat.o ls.o cat.o run.o serial.o vga.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o idle.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $^
 
 boot.bin: boot.elf
@@ -182,4 +185,4 @@ run-usb: usb.img root.img
 	qemu-system-x86_64 -M pc -boot c -drive file=usb.img,format=raw,if=ide -drive file=root.img,format=raw,if=floppy
 
 clean:
-	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o fat.o ls.o cat.o run.o disk.o serial.o vga.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin
+	rm -f boot.o vesa_stage.o kernel.o timer.o switch.o process.o io.o rtc.o terminal.o hello.o reboot.o fat.o ls.o cat.o run.o disk.o serial.o vga.o gpu.o ati.o nvidia.o font_8x8.o fbtext.o clock.o colors.o display.o cpuinfo.o idle.o boot.elf kernel.elf boot.bin kernel.bin boot.bin.tmp boot.bin.patched image.bin floppy.img usb.img final.bin root.img prog.o prog.elf prog.bin space.o space.elf space.bin

+ 499 - 9
cpuinfo.c

@@ -1,8 +1,19 @@
 /* cpuinfo command: print CPU vendor/model and estimated current clock speed. */
 
+#include "idle.h"
 #include "io.h"
 #include "timer.h"
 
+#define MSR_IA32_PERF_STATUS     0x198u
+#define MSR_IA32_PERF_CTL        0x199u
+#define MSR_IA32_PLATFORM_INFO   0xCEu
+#define MSR_IA32_PM_ENABLE       0x770u
+#define MSR_IA32_HWP_CAPABILITIES 0x771u
+#define MSR_IA32_HWP_REQUEST     0x774u
+#define MSR_IA32_ENERGY_PERF_BIAS 0x1B0u
+#define MSR_AMD_PSTATE_CTL       0xC0010062u
+#define MSR_AMD_PSTATE_STATUS    0xC0010063u
+
 static void put_uint(unsigned int n)
 {
 	char buf[12];
@@ -20,6 +31,43 @@ static void put_uint(unsigned int n)
 		io_putchar(*p++);
 }
 
+static int streq(const char *a, const char *b)
+{
+	unsigned int i = 0;
+	while (a[i] && b[i]) {
+		if (a[i] != b[i])
+			return 0;
+		i++;
+	}
+	return a[i] == '\0' && b[i] == '\0';
+}
+
+static int str_starts_with(const char *s, const char *prefix)
+{
+	unsigned int i = 0;
+	while (prefix[i]) {
+		if (s[i] != prefix[i])
+			return 0;
+		i++;
+	}
+	return 1;
+}
+
+static void put_hex8(unsigned int x)
+{
+	static const char hex[] = "0123456789ABCDEF";
+	io_putchar(hex[(x >> 4) & 0xF]);
+	io_putchar(hex[x & 0xF]);
+}
+
+static void put_hex32(unsigned int x)
+{
+	put_hex8((x >> 24) & 0xFF);
+	put_hex8((x >> 16) & 0xFF);
+	put_hex8((x >> 8) & 0xFF);
+	put_hex8(x & 0xFF);
+}
+
 static void cpuid(unsigned int leaf, unsigned int subleaf,
 	unsigned int *eax, unsigned int *ebx,
 	unsigned int *ecx, unsigned int *edx)
@@ -37,6 +85,16 @@ static unsigned int read_tsc_low(void)
 	return lo;
 }
 
+static void rdmsr_u32(unsigned int msr, unsigned int *lo, unsigned int *hi)
+{
+	__asm__ volatile ("rdmsr" : "=a"(*lo), "=d"(*hi) : "c"(msr));
+}
+
+static void wrmsr_u32(unsigned int msr, unsigned int lo, unsigned int hi)
+{
+	__asm__ volatile ("wrmsr" :: "c"(msr), "a"(lo), "d"(hi));
+}
+
 static void trim_right_spaces(char *s)
 {
 	unsigned int len = 0;
@@ -144,6 +202,56 @@ static void print_features_ext(unsigned int max_ext_leaf)
 	io_putchar('\n');
 }
 
+static void print_cpuid_frequency(unsigned int max_leaf)
+{
+	unsigned int eax, ebx, ecx, edx;
+
+	if (max_leaf >= 0x15u) {
+		cpuid(0x15u, 0, &eax, &ebx, &ecx, &edx);
+		if (eax != 0 && ebx != 0) {
+			io_putstr("cpuid15 ratio ");
+			put_uint(ebx);
+			io_putchar('/');
+			put_uint(eax);
+			if (ecx != 0) {
+				io_putstr(" crystal ");
+				put_uint(ecx / 1000000u);
+				io_putstr(" MHz");
+			}
+			io_putchar('\n');
+		}
+	}
+	if (max_leaf >= 0x16u) {
+		cpuid(0x16u, 0, &eax, &ebx, &ecx, &edx);
+		if (eax != 0 || ebx != 0 || ecx != 0) {
+			io_putstr("cpuid16 base ");
+			put_uint(eax);
+			io_putstr(" max ");
+			put_uint(ebx);
+			io_putstr(" bus ");
+			put_uint(ecx);
+			io_putstr(" MHz\n");
+		}
+	}
+}
+
+static void parse_family_model(unsigned int eax1,
+			       unsigned int *family_out,
+			       unsigned int *model_out,
+			       unsigned int *stepping_out)
+{
+	unsigned int family = (eax1 >> 8) & 0xF;
+	unsigned int model = (eax1 >> 4) & 0xF;
+	unsigned int stepping = eax1 & 0xF;
+	if (family == 0xF)
+		family += (eax1 >> 20) & 0xFF;
+	if (family == 0x6 || family == 0xF)
+		model += ((eax1 >> 16) & 0xF) << 4;
+	*family_out = family;
+	*model_out = model;
+	*stepping_out = stepping;
+}
+
 void program_cpuinfo_main(void)
 {
 	unsigned int eax, ebx, ecx, edx;
@@ -182,13 +290,7 @@ void program_cpuinfo_main(void)
 	ebx1 = ebx;
 	ecx1 = ecx;
 	edx1 = edx;
-	stepping = eax & 0xF;
-	model = (eax >> 4) & 0xF;
-	family = (eax >> 8) & 0xF;
-	if (family == 0xF)
-		family += (eax >> 20) & 0xFF;
-	if (family == 0x6 || family == 0xF)
-		model += ((eax >> 16) & 0xF) << 4;
+	parse_family_model(eax, &family, &model, &stepping);
 
 	io_putstr("family ");
 	put_uint(family);
@@ -250,6 +352,8 @@ void program_cpuinfo_main(void)
 	put_uint(threads_per_core);
 	io_putstr(" threads/core\n");
 
+	print_cpuid_frequency(max_leaf);
+
 	start_ms = get_ticks_ms();
 	t0 = read_tsc_low();
 	do {
@@ -264,13 +368,399 @@ void program_cpuinfo_main(void)
 
 	/* MHz with one decimal: cycles / (ms * 100). */
 	mhz_x10 = delta_cycles / (delta_ms * 100);
-	io_putstr("clock ");
+	io_putstr("clock tsc ");
 	put_uint(mhz_x10 / 10);
 	io_putchar('.');
 	put_uint(mhz_x10 % 10);
-	io_putstr(" MHz (estimated)\n");
+	io_putstr(" MHz (sampled)\n");
 
 	print_features_leaf1(ecx1, edx1);
 	print_features_leaf7(max_leaf);
 	print_features_ext(max_ext_leaf);
 }
+
+static void print_power_usage(void)
+{
+	io_putstr("usage:\n");
+	io_putstr("  power info\n");
+	io_putstr("  power set low      (deep C-state, lowest power)\n");
+	io_putstr("  power set balanced (moderate C-state)\n");
+	io_putstr("  power set max      (C1 only, max performance)\n");
+}
+
+static void detect_cpu(char vendor[13], unsigned int *max_leaf,
+		       unsigned int *eax1, unsigned int *ecx1, unsigned int *edx1,
+		       unsigned int *family, unsigned int *model, unsigned int *stepping)
+{
+	unsigned int ebx, ecx, edx;
+	cpuid(0, 0, max_leaf, &ebx, &ecx, &edx);
+	((unsigned int *)vendor)[0] = ebx;
+	((unsigned int *)vendor)[1] = edx;
+	((unsigned int *)vendor)[2] = ecx;
+	vendor[12] = '\0';
+	cpuid(1, 0, eax1, &ebx, ecx1, edx1);
+	parse_family_model(*eax1, family, model, stepping);
+}
+
+static int intel_has_platform_info(unsigned int family, unsigned int model)
+{
+	if (family != 6)
+		return 0;
+	if (model >= 0x1A)
+		return 1;
+	return 0;
+}
+
+static int intel_has_epb(unsigned int eax6)
+{
+	return (eax6 & (1u << 3)) ? 1 : 0;
+}
+
+static void power_info_intel(unsigned int family, unsigned int model, unsigned int ecx1, unsigned int edx1)
+{
+	unsigned int lo = 0, hi = 0;
+	unsigned int target_ratio = 0, current_ratio = 0;
+	unsigned int min_ratio = 0, max_ratio = 0;
+	unsigned int eax6, ebx6, ecx6, edx6;
+	int has_hwp = 0;
+	int hwp_enabled = 0;
+	int has_platinfo = 0;
+	int has_epb = 0;
+
+	if ((edx1 & (1u << 5)) == 0) {
+		io_putstr("intel: no MSR feature bit\n");
+		return;
+	}
+	if ((ecx1 & (1u << 7)) == 0)
+		io_putstr("intel: EIST not advertised\n");
+	if (family != 6 && family != 15) {
+		io_putstr("intel: family not supported for perf ctl\n");
+		return;
+	}
+
+	cpuid(6, 0, &eax6, &ebx6, &ecx6, &edx6);
+	(void)ebx6;
+	(void)ecx6;
+	(void)edx6;
+	has_hwp = (eax6 & (1u << 7)) ? 1 : 0;
+	has_epb = intel_has_epb(eax6);
+	has_platinfo = intel_has_platform_info(family, model);
+
+	if (has_platinfo) {
+		rdmsr_u32(MSR_IA32_PLATFORM_INFO, &lo, &hi);
+		min_ratio = (hi >> 8) & 0xFFu;
+		max_ratio = (lo >> 8) & 0xFFu;
+	}
+
+	rdmsr_u32(MSR_IA32_PERF_STATUS, &lo, &hi);
+	current_ratio = (lo >> 8) & 0xFF;
+	rdmsr_u32(MSR_IA32_PERF_CTL, &lo, &hi);
+	target_ratio = (lo >> 8) & 0xFF;
+
+	if (has_hwp) {
+		rdmsr_u32(MSR_IA32_PM_ENABLE, &lo, &hi);
+		hwp_enabled = (lo & 1u) ? 1 : 0;
+	}
+
+	io_putstr("intel ratio current ");
+	put_uint(current_ratio);
+	io_putstr(" target ");
+	put_uint(target_ratio);
+	io_putchar('\n');
+	if (has_platinfo) {
+		io_putstr("intel ratio range ");
+		put_uint(min_ratio);
+		io_putstr("..");
+		put_uint(max_ratio);
+		io_putchar('\n');
+	}
+	if (has_hwp) {
+		io_putstr("intel hwp ");
+		io_putstr(hwp_enabled ? "enabled\n" : "disabled\n");
+		if (hwp_enabled) {
+			rdmsr_u32(MSR_IA32_HWP_REQUEST, &lo, &hi);
+			io_putstr("intel hwp req min ");
+			put_uint(lo & 0xFFu);
+			io_putstr(" max ");
+			put_uint((lo >> 8) & 0xFFu);
+			io_putstr(" des ");
+			put_uint((lo >> 16) & 0xFFu);
+			io_putstr(" epp ");
+			put_uint((lo >> 24) & 0xFFu);
+			io_putchar('\n');
+			rdmsr_u32(MSR_IA32_HWP_CAPABILITIES, &lo, &hi);
+			io_putstr("intel hwp cap low ");
+			put_uint(lo & 0xFFu);
+			io_putstr(" high ");
+			put_uint((lo >> 8) & 0xFFu);
+			io_putstr(" guar ");
+			put_uint((lo >> 16) & 0xFFu);
+			io_putstr(" eff ");
+			put_uint((lo >> 24) & 0xFFu);
+			io_putchar('\n');
+		}
+	}
+	if (has_epb) {
+		rdmsr_u32(MSR_IA32_ENERGY_PERF_BIAS, &lo, &hi);
+		io_putstr("intel epb ");
+		put_uint(lo & 0xFu);
+		io_putchar('\n');
+	}
+	io_putstr("intel family ");
+	put_uint(family);
+	io_putstr(" model ");
+	put_uint(model);
+	io_putchar('\n');
+}
+
+static int power_set_intel(const char *profile, unsigned int family, unsigned int model, unsigned int ecx1, unsigned int edx1)
+{
+	unsigned int lo = 0, hi = 0;
+	unsigned int current_ratio, target_ratio;
+	unsigned int min_ratio = 8, max_ratio = 63;
+	unsigned int eax6, ebx6, ecx6, edx6;
+	int has_hwp = 0;
+	int hwp_enabled = 0;
+	int has_platinfo = 0;
+
+	if ((edx1 & (1u << 5)) == 0 || (ecx1 & (1u << 7)) == 0) {
+		io_putstr("intel: missing MSR/EIST support\n");
+		return -1;
+	}
+	if (family != 6 && family != 15) {
+		io_putstr("intel: unsupported family for write path\n");
+		return -1;
+	}
+
+	cpuid(6, 0, &eax6, &ebx6, &ecx6, &edx6);
+	(void)ebx6;
+	(void)ecx6;
+	(void)edx6;
+	has_hwp = (eax6 & (1u << 7)) ? 1 : 0;
+	has_platinfo = intel_has_platform_info(family, model);
+
+	if (has_platinfo) {
+		rdmsr_u32(MSR_IA32_PLATFORM_INFO, &lo, &hi);
+		min_ratio = (hi >> 8) & 0xFFu;
+		max_ratio = (lo >> 8) & 0xFFu;
+		if (min_ratio == 0 || max_ratio == 0 || min_ratio > max_ratio) {
+			min_ratio = 8;
+			max_ratio = 63;
+		}
+	}
+
+	if (has_hwp) {
+		rdmsr_u32(MSR_IA32_PM_ENABLE, &lo, &hi);
+		hwp_enabled = (lo & 1u) ? 1 : 0;
+	}
+
+	rdmsr_u32(MSR_IA32_PERF_STATUS, &lo, &hi);
+	current_ratio = (lo >> 8) & 0xFF;
+	target_ratio = current_ratio < min_ratio ? min_ratio : current_ratio;
+	if (target_ratio > max_ratio)
+		target_ratio = max_ratio;
+
+	if (streq(profile, "low")) {
+		target_ratio = min_ratio;
+	} else if (streq(profile, "balanced")) {
+		target_ratio = (min_ratio + max_ratio) / 2;
+	} else if (streq(profile, "max")) {
+		target_ratio = max_ratio;
+	} else {
+		io_putstr("power: invalid profile\n");
+		return -1;
+	}
+
+	if (has_hwp && hwp_enabled) {
+		unsigned int minp = min_ratio;
+		unsigned int maxp = max_ratio;
+		unsigned int des = target_ratio;
+		unsigned int epp = 0x80u;
+		if (streq(profile, "low"))
+			epp = 0xC0u;
+		else if (streq(profile, "balanced"))
+			epp = 0x80u;
+		else if (streq(profile, "max"))
+			epp = 0x00u;
+		lo = (minp & 0xFFu) |
+		     ((maxp & 0xFFu) << 8) |
+		     ((des & 0xFFu) << 16) |
+		     ((epp & 0xFFu) << 24);
+		wrmsr_u32(MSR_IA32_HWP_REQUEST, lo, 0);
+		io_putstr("intel hwp profile ");
+		io_putstr(profile);
+		io_putstr(" set (min ");
+		put_uint(minp);
+		io_putstr(" max ");
+		put_uint(maxp);
+		io_putstr(" des ");
+		put_uint(des);
+		io_putstr(" epp ");
+		put_uint(epp);
+		io_putstr(")\n");
+		return 0;
+	}
+
+	rdmsr_u32(MSR_IA32_PERF_CTL, &lo, &hi);
+	lo &= ~0xFF00u;
+	lo |= (target_ratio & 0xFFu) << 8;
+	wrmsr_u32(MSR_IA32_PERF_CTL, lo, hi);
+	io_putstr("intel target ratio set ");
+	put_uint(target_ratio);
+	io_putchar('\n');
+	return 0;
+}
+
+static void power_info_amd(unsigned int family, unsigned int model, unsigned int edx1)
+{
+	unsigned int lo = 0, hi = 0;
+	if ((edx1 & (1u << 5)) == 0) {
+		io_putstr("amd: no MSR feature bit\n");
+		return;
+	}
+	if (family < 0x10) {
+		io_putstr("amd: family too old for p-state ctl\n");
+		return;
+	}
+	rdmsr_u32(MSR_AMD_PSTATE_STATUS, &lo, &hi);
+	io_putstr("amd pstate current ");
+	put_uint(lo & 0x7u);
+	io_putstr(" raw 0x");
+	put_hex32(lo);
+	io_putchar('\n');
+	io_putstr("amd family ");
+	put_uint(family);
+	io_putstr(" model ");
+	put_uint(model);
+	io_putchar('\n');
+}
+
+static int power_set_amd(const char *profile, unsigned int family, unsigned int edx1)
+{
+	unsigned int lo = 0, hi = 0;
+	unsigned int pstate = 0;
+	if ((edx1 & (1u << 5)) == 0) {
+		io_putstr("amd: no MSR support\n");
+		return -1;
+	}
+	if (family < 0x10) {
+		io_putstr("amd: unsupported family for write path\n");
+		return -1;
+	}
+	if (streq(profile, "max")) {
+		pstate = 0;
+	} else if (streq(profile, "balanced")) {
+		pstate = 1;
+	} else if (streq(profile, "low")) {
+		pstate = 2;
+	} else {
+		io_putstr("power: invalid profile\n");
+		return -1;
+	}
+	rdmsr_u32(MSR_AMD_PSTATE_CTL, &lo, &hi);
+	lo &= ~0x7u;
+	lo |= pstate;
+	wrmsr_u32(MSR_AMD_PSTATE_CTL, lo, hi);
+	io_putstr("amd target pstate set ");
+	put_uint(pstate);
+	io_putchar('\n');
+	return 0;
+}
+
+void program_power_main(const char *args)
+{
+	char vendor[13];
+	char local[40];
+	char command[8];
+	char profile[16];
+	unsigned int max_leaf, eax1, ecx1, edx1, family, model, stepping;
+	unsigned int i = 0, j = 0;
+	int is_set = 0;
+	unsigned int cstate_level;
+
+	detect_cpu(vendor, &max_leaf, &eax1, &ecx1, &edx1, &family, &model, &stepping);
+	(void)eax1;
+	(void)max_leaf;
+
+	while (args && args[i] && i + 1 < sizeof(local)) {
+		local[i] = args[i];
+		i++;
+	}
+	local[i] = '\0';
+	i = 0;
+	while (local[i] == ' ')
+		i++;
+	j = 0;
+	while (local[i] && local[i] != ' ' && j + 1 < sizeof(command))
+		command[j++] = local[i++];
+	command[j] = '\0';
+	while (local[i] == ' ')
+		i++;
+	j = 0;
+	while (local[i] && local[i] != ' ' && j + 1 < sizeof(profile))
+		profile[j++] = local[i++];
+	profile[j] = '\0';
+
+	if (command[0] == '\0' || streq(command, "info")) {
+		io_putstr("power vendor ");
+		io_putstr(vendor);
+		io_putstr(" family ");
+		put_uint(family);
+		io_putstr(" model ");
+		put_uint(model);
+		io_putstr(" stepping ");
+		put_uint(stepping);
+		io_putchar('\n');
+
+		io_putstr("idle mode ");
+		if (idle_has_mwait())
+			io_putstr("mwait");
+		else
+			io_putstr("hlt");
+		io_putstr(" cstate C");
+		put_uint(idle_get_cstate());
+		io_putchar('\n');
+
+		if (streq(vendor, "GenuineIntel"))
+			power_info_intel(family, model, ecx1, edx1);
+		else if (streq(vendor, "AuthenticAMD"))
+			power_info_amd(family, model, edx1);
+		else
+			io_putstr("power: vendor backend unavailable\n");
+		print_power_usage();
+		return;
+	}
+
+	if (streq(command, "set"))
+		is_set = 1;
+	if (!is_set || profile[0] == '\0') {
+		print_power_usage();
+		return;
+	}
+	if (str_starts_with(profile, "low") == 0 &&
+	    str_starts_with(profile, "balanced") == 0 &&
+	    str_starts_with(profile, "max") == 0) {
+		io_putstr("power: expected low|balanced|max\n");
+		return;
+	}
+
+	if (streq(profile, "low")) {
+		cstate_level = 3;
+	} else if (streq(profile, "balanced")) {
+		cstate_level = 2;
+	} else {
+		cstate_level = 0;
+	}
+	idle_set_cstate(cstate_level);
+	io_putstr("idle cstate set C");
+	put_uint(cstate_level);
+	io_putchar('\n');
+
+	if (streq(vendor, "GenuineIntel")) {
+		if (power_set_intel(profile, family, model, ecx1, edx1) < 0)
+			io_putstr("power: intel p-state control unavailable\n");
+	} else if (streq(vendor, "AuthenticAMD")) {
+		if (power_set_amd(profile, family, edx1) < 0)
+			io_putstr("power: amd p-state control unavailable\n");
+	}
+}

+ 85 - 0
idle.c

@@ -0,0 +1,85 @@
+/* CPU idle: use mwait for deep C-states when available, hlt fallback. */
+
+#include "idle.h"
+
+static int has_mwait = 0;
+static int has_mwait_ext = 0;
+static unsigned int mwait_cstate_hint = 0x00;
+
+static void cpuid_idle(unsigned int leaf, unsigned int *eax, unsigned int *ebx,
+		       unsigned int *ecx, unsigned int *edx)
+{
+	__asm__ volatile ("cpuid"
+		: "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx)
+		: "a"(leaf), "c"(0));
+}
+
+void idle_init(void)
+{
+	unsigned int eax, ebx, ecx, edx;
+	unsigned int max_leaf;
+
+	cpuid_idle(0, &max_leaf, &ebx, &ecx, &edx);
+	if (max_leaf < 1)
+		return;
+
+	cpuid_idle(1, &eax, &ebx, &ecx, &edx);
+	has_mwait = (ecx & (1u << 3)) ? 1 : 0;
+
+	if (has_mwait && max_leaf >= 5) {
+		cpuid_idle(5, &eax, &ebx, &ecx, &edx);
+		has_mwait_ext = (ecx & 1u) ? 1 : 0;
+	}
+
+	mwait_cstate_hint = 0x10;
+}
+
+void idle_set_cstate(unsigned int level)
+{
+	switch (level) {
+	case 0:
+		mwait_cstate_hint = 0x00;
+		break;
+	case 1:
+		mwait_cstate_hint = 0x10;
+		break;
+	case 2:
+		mwait_cstate_hint = 0x20;
+		break;
+	case 3:
+		mwait_cstate_hint = 0x30;
+		break;
+	default:
+		mwait_cstate_hint = 0x10;
+		break;
+	}
+}
+
+unsigned int idle_get_cstate(void)
+{
+	return (mwait_cstate_hint >> 4) & 0xF;
+}
+
+int idle_has_mwait(void)
+{
+	return has_mwait;
+}
+
+void cpu_idle(void)
+{
+	if (has_mwait && has_mwait_ext) {
+		volatile unsigned int dummy = 0;
+		__asm__ volatile (
+			"monitor"
+			:
+			: "a"(&dummy), "c"(0), "d"(0)
+		);
+		__asm__ volatile (
+			"mwait"
+			:
+			: "a"(mwait_cstate_hint), "c"(1)
+		);
+	} else {
+		__asm__ volatile ("hlt");
+	}
+}

+ 11 - 0
idle.h

@@ -0,0 +1,11 @@
+/* CPU idle: deep C-state support via mwait when available. */
+#ifndef IDLE_H
+#define IDLE_H
+
+void idle_init(void);
+void idle_set_cstate(unsigned int level);
+unsigned int idle_get_cstate(void);
+int idle_has_mwait(void);
+void cpu_idle(void);
+
+#endif

+ 2 - 0
kernel.c

@@ -1,5 +1,6 @@
 /* Kernel: entry point at 0x7E00, timer (PIT 1 kHz), process init, terminal. */
 
+#include "idle.h"
 #include "io.h"
 #include "process.h"
 #include "terminal.h"
@@ -128,6 +129,7 @@ void kernel_main(void)
 
 	serial_init();
 	io_init();
+	idle_init();
 	timer_init();
 	process_init();
 	terminal_run();

+ 36 - 1
terminal.c

@@ -3,6 +3,7 @@
 
 #include "io.h"
 #include "fbtext.h"
+#include "idle.h"
 #include "process.h"
 #include "rtc.h"
 #include "terminal.h"
@@ -17,6 +18,7 @@ extern void program_clock_main(void);
 extern void program_colors_main(void);
 extern void program_display_main(void);
 extern void program_cpuinfo_main(void);
+extern void program_power_main(const char *args);
 extern void disk_set_drive(unsigned int drive);
 extern unsigned int disk_get_drive(void);
 
@@ -345,6 +347,34 @@ static int is_cmd_kill(const char *line, unsigned int line_len, unsigned int *pi
 	return 1;
 }
 
+#define POWER_ARGS_MAX 32
+static int is_cmd_power(const char *line, unsigned int line_len, char *args_buf, unsigned int buf_size)
+{
+	unsigned int i = 0, j = 0;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i + 5 > line_len)
+		return 0;
+	if (line[i] != 'p' || line[i+1] != 'o' || line[i+2] != 'w' || line[i+3] != 'e' || line[i+4] != 'r')
+		return 0;
+	i += 5;
+	while (i < line_len && line[i] == ' ')
+		i++;
+	if (i == line_len) {
+		if (buf_size > 0)
+			args_buf[0] = '\0';
+		return 1;
+	}
+	while (i < line_len && j + 1 < buf_size)
+		args_buf[j++] = line[i++];
+	while (j > 0 && args_buf[j - 1] == ' ')
+		j--;
+	args_buf[j] = '\0';
+	while (i < line_len && line[i] == ' ')
+		i++;
+	return (i == line_len);
+}
+
 /* Redraw line from index `from` to end, clear one cell after, set cursor to line_pos. */
 static void redraw_line_from(const char *line, unsigned int line_len,
 	unsigned int line_pos, unsigned int line_start, unsigned int from)
@@ -492,6 +522,7 @@ void terminal_run(void)
 					}
 				} else if (k == '\n') {
 					static char cat_fname[CAT_FNAME_MAX];
+					static char power_args[POWER_ARGS_MAX];
 					unsigned int drive_sel, kill_pid;
 					int skip_prompt = 0;
 					io_putchar('\n');
@@ -526,6 +557,9 @@ void terminal_run(void)
 					} else if (is_cmd_cpuinfo(line, line_len)) {
 						if (process_create("cpuinfo", program_cpuinfo_main) < 0)
 							io_putstr("error: too many processes\n");
+					} else if (is_cmd_power(line, line_len, power_args, POWER_ARGS_MAX)) {
+						if (process_create_arg("power", program_power_main, power_args) < 0)
+							io_putstr("error: too many processes\n");
 					} else if (is_cmd_drive(line, line_len, &drive_sel)) {
 						disk_set_drive(drive_sel);
 						io_putstr("drive ");
@@ -544,6 +578,7 @@ void terminal_run(void)
 						io_putstr("run <file>  space\n");
 						io_putstr("clock     colors\n");
 						io_putstr("display   cpuinfo\n");
+						io_putstr("power [info|set <low|balanced|max>]\n");
 						io_putstr("drive <0|1>\n");
 						io_putstr("ps        kill <pid>\n");
 						io_putstr("help      clear\n");
@@ -587,7 +622,7 @@ void terminal_run(void)
 					}
 				}
 			} else {
-				__asm__ volatile ("hlt");
+				cpu_idle();
 			}
 		}
 	}