Просмотр исходного кода

Add Arrandale DTS and ThinkPad EC sensors

The X201's i5 M 520 (Arrandale, model 0x25) was missing from the DTS
allowlist, so the kernel logged "no Intel DTS".  Add it (plus the other
Nehalem/Westmere variants) and read TjMax from IA32_TEMPERATURE_TARGET
for the models that implement it; the X201 reports 105 C, while Bonnell
keeps its table-driven values.  The boot log now says where TjMax came
from.

Add tpec.c for the board sensors: a checksum-validated SMBIOS type 1
lookup gates EC access to Lenovo/IBM machines, then the standard ACPI EC
read handshake reads temperatures at 0x78..0x7F / 0xC0..0xC7 (skipping
the second block on functional revision 3+), the fan tachometer at
0x84/0x85 and the 0x2F fan mode.  Pending output bytes (hotkey events,
since there is no SCI handler) are drained before every command and
every wait is bounded; nothing is written to the EC.

SYS_TPEC_SENSORS exposes the snapshot and the ring-3 sensors command
prints the EC registers, fan RPM and decoded fan mode.

Tested: QEMU boots cleanly with the default, Lenovo and Nehalem/Westmere
SMBIOS/CPU variants, and sensors runs end-to-end; a host simulation with
a fake EC exercises the SMBIOS parsing, the read handshake, stale-event
draining and the revision rule.  On the X201 the DTS reading is visible
in the top bar; the EC block still needs a hardware check.
Johannes Zellner 2 недель назад
Родитель
Сommit
8b3ac72c99
12 измененных файлов с 675 добавлено и 42 удалено
  1. 25 3
      AGENTS.md
  2. 6 3
      Makefile
  3. 4 0
      kernel.c
  4. 23 0
      sys_info.c
  5. 1 0
      sys_info.h
  6. 5 0
      syscall.c
  7. 17 0
      syscall.h
  8. 68 5
      thermal.c
  9. 368 0
      tpec.c
  10. 29 0
      tpec.h
  11. 5 0
      ucmd_lib.h
  12. 124 31
      ucmd_sensors.c

+ 25 - 3
AGENTS.md

@@ -452,9 +452,13 @@ run area), so adding code no longer moves any address.
 - **CPU temperature**: `thermal.c/h` — Intel DTS through `IA32_THERM_STATUS`
   (MSR `0x19C`).  Each CPU samples itself once a second from its scheduler
   tick (`struct cpu.temp_c`), and `SYS_CPU_TEMP` (`sys_info.c`) exposes the
-  hottest core plus TjMax to ring 3.  TjMax is table-driven (Atom 330 =
-  125 °C, 230 = 100, other Bonnell = 90, otherwise 100) because Bonnell has
-  no `IA32_TEMPERATURE_TARGET`.  The 330's 125 °C is Linux coretemp's
+  hottest core plus TjMax to ring 3.  The model allowlist follows coretemp
+  (Core/Core 2, Nehalem/Westmere including the X201's Arrandale `0x25`, and
+  Bonnell) and is what keeps `rdmsr` away from CPUs that lack the MSR.
+  Nehalem and later read TjMax from `IA32_TEMPERATURE_TARGET` (MSR `0x1A2`,
+  bits 23:16; the X201's i5 M 520 reports 105 °C); Bonnell has no such MSR,
+  so its TjMax stays table-driven (Atom 330 = 125 °C, 230 = 100, other
+  Bonnell = 90, otherwise 100).  The 330's 125 °C is Linux coretemp's
   empirical value; Intel only specifies TJ = 90 °C and Tcase-max = 85.2 °C,
   so `sensors` prints the raw delta and the 90/100/125 °C scales as well as
   the top bar's `CPU 43C` before the clock and `cpuinfo`'s value plus EIST
@@ -463,6 +467,16 @@ run area), so adding code no longer moves any address.
   (the AT3IONT-I exposes three temperatures and three fan tachometers), used
   by `SYS_SENSORS` and the ring-3 `sensors` command to cross-check the DTS
   and show whether the fans turn.  Nothing is written to the fan control.
+  `tpec.c/h` adds the ThinkPad embedded controller of the X201
+  (`SYS_TPEC_SENSORS`): a `_SM_` SMBIOS type 1 check gates the probe to
+  Lenovo/IBM machines, then EC reads through ports 0x62/0x66 return the
+  temperatures at EC 0x78..0x7F / 0xC0..0xC7 (0x80 = absent, 0x00 =
+  unimplemented; revision 3+ at EC 0xEF only uses the first block), the fan
+  tachometer at 0x84 (LSB, always read first) / 0x85 and the 0x2F fan mode.
+  A pending EC output byte (a hotkey event, since there is no SCI handler) is
+  drained before every command, and every wait is bounded; nothing is
+  written to the EC.  `sensors` labels the EC registers and decodes the fan
+  mode (auto / full speed / level).
 - **User programs**: `run.c`, `run_api.h`, `prog.c`, `space.c` — raw binaries at
   `0x80000`, API function-pointer struct at `0x7FF00`.  `prog`/`space` are
   **embedded in the kernel image** (`prog_embed.o`/`space_embed.o`), so
@@ -1200,3 +1214,11 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     mask-all step after ACPI init.  UEFI runtime services are not used, so
     `reboot`/`poweroff` still use the legacy ports.  The image is 17 MB: the
     9 MB kernel + rootfs disk and the 8 MB ESP.
+
+25. ThinkPad EC sensor access (`tpec.c`) runs in legacy/SMM mode because
+    there is no ACPI interpreter: an SMI that touches the EC between our port
+    accesses can produce an occasional bad sample, and EC hotkey events are
+    drained/discarded instead of handled.  The PCH thermal sensor
+    (`8086:3B32`, the `intel_ips` MMIO block) is not read because it needs
+    the ME/BIOS to have enabled thermal reporting; the EC and DTS readings
+    cover the package temperature.

+ 6 - 3
Makefile

@@ -116,6 +116,9 @@ cpu.o: cpu.c cpu.h
 thermal.o: thermal.c thermal.h cpu.h sched.h serial.h
 	$(CC) $(CFLAGS) -o $@ $<
 
+tpec.o: tpec.c tpec.h paging.h pci.h serial.h
+	$(CC) $(CFLAGS) -o $@ $<
+
 it87.o: it87.c it87.h pci.h
 	$(CC) $(CFLAGS) -o $@ $<
 
@@ -326,7 +329,7 @@ sys_mem.o: sys_mem.c sys_mem.h mm.h paging.h process.h
 inflate.o: inflate.c inflate.h
 	$(CC) $(CFLAGS) -o $@ $<
 
-sys_info.o: sys_info.c sys_info.h syscall.h ahci.h blkdev.h it87.h mm.h mouse.h thermal.h usbhid.h vfs.h
+sys_info.o: sys_info.c sys_info.h syscall.h ahci.h blkdev.h it87.h mm.h mouse.h thermal.h tpec.h usbhid.h vfs.h
 	$(CC) $(CFLAGS) -o $@ $<
 
 sys_net.o: sys_net.c sys_net.h syscall.h dhcp.h dns.h net.h netdev.h netlog.h process.h sched.h
@@ -335,7 +338,7 @@ sys_net.o: sys_net.c sys_net.h syscall.h dhcp.h dns.h net.h netdev.h netlog.h pr
 syscall_stub.o: syscall_stub.S irqret.inc
 	$(AS) $(ASFLAGS) -o $@ $<
 
-kernel.elf: kernel.ld vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o blit.o cpu.o thermal.o it87.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o gfx.o sys_gfx.o display.o mouse.o uhci.o ohci.o ehci.o xhci.o asix.o usb.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o sys_net.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o acpi.o lapic.o smp.o ap_tramp_embed.o prog_embed.o space_embed.o inflate.o ramfs.o share.o payload.o
+kernel.elf: kernel.ld vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o reboot.o poweroff.o disk.o fat.o run.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o blit.o cpu.o thermal.o it87.o tpec.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o fbtext.o gfx.o sys_gfx.o display.o mouse.o uhci.o ohci.o ehci.o xhci.o asix.o usb.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o sys_net.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o acpi.o lapic.o smp.o ap_tramp_embed.o prog_embed.o space_embed.o inflate.o ramfs.o share.o payload.o
 	$(LD) $(KERNEL_LDFLAGS) -o $@ $(filter-out kernel.ld,$^)
 
 MBR_LDFLAGS = -T mbr.ld -nostdlib -m elf_i386
@@ -1032,4 +1035,4 @@ kernel-net-dev: kernel.bin netkexec_send
 	./netkexec_send $(BOARD_IP) $(NETKEXEC_PORT) 0x$$entry kernel.bin
 
 clean:
-	rm -f boot.o vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o blit.o cpu.o thermal.o it87.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o font_8x16.o fbtext.o gfx.o sys_gfx.o display.o mouse.o uhci.o ohci.o ehci.o xhci.o asix.o usb.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o inflate.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o acpi.o lapic.o smp.o ap_tramp.o ap_tramp.elf ap_tramp.bin ap_tramp_embed.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.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 hello.elf hello_embed.o prog_embed.o space_embed.o cmds.pack cmds_pack_gz.bin cmds_pack.o rootfs.bin cmds.img mkrootfs ramfs.o share.o payload.o mkpack uwm_lib.o ngui.o img.o img_mem.o img_blit.o img_png.o img_bmp.o img_gif.o img_jpeg.o img_tga.o img_pnm.o img_ico.o img_pam.o img_gif_anim.o img_file.o browser.o browser_html.o uhttp.o browser.elf img_test img_test_asan *.boot.tmp $(addsuffix .o,$(UCMDS)) $(addsuffix .elf,$(UCMDS)) gallery.o gallery.elf $(TLSLIB_OBJS) tls_test bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send netkexec_send netmonitor BOOTX64.EFI esp.img usb-efi.img efi/main.o efi/payload.o efi/tramp.o efi/tramp.elf efi/tramp.bin efi/efi_entry.h
+	rm -f boot.o vesa_stage.o entry.o kernel.o timer.o switch.o sched.o process.o mm.o kmalloc.o paging.o elf.o tss.o syscall.o sys_wm.o syscall_stub.o screen.o io.o tty.o rtc.o terminal.o wm.o hello.o reboot.o poweroff.o fat.o run.o disk.o serial.o serial_ring_buf.o serial_loop.o serial_recv.o bin_recv.o vga.o blit.o cpu.o thermal.o it87.o tpec.o vga_copy.o gfxbench.o gpu.o ati.o cirrus.o nvidia.o nv50.o radeon.o mtrr.o font_8x8.o font_8x16.o fbtext.o gfx.o sys_gfx.o display.o mouse.o uhci.o ohci.o ehci.o xhci.o asix.o usb.o usbhid.o lsusb.o panic.o bootcon.o blkdev.o ata.o ahci.o fatdetect.o fatformat.o format.o vfs.o sys_fs.o sys_info.o sys_mem.o inflate.o idle.o kexec.o kexec_tramp.o dbg.o lspci.o peek.o ne2000.o r8168.o e1000e.o netdev.o net.o netkexec.o netlog.o dhcp.o dns.o tcp.o acpi.o lapic.o smp.o ap_tramp.o ap_tramp.elf ap_tramp.bin ap_tramp_embed.o mbr.o boot.elf kernel.elf mbr.elf boot.bin mbr.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 hello.elf hello_embed.o prog_embed.o space_embed.o cmds.pack cmds_pack_gz.bin cmds_pack.o rootfs.bin cmds.img mkrootfs ramfs.o share.o payload.o mkpack uwm_lib.o ngui.o img.o img_mem.o img_blit.o img_png.o img_bmp.o img_gif.o img_jpeg.o img_tga.o img_pnm.o img_ico.o img_pam.o img_gif_anim.o img_file.o browser.o browser_html.o uhttp.o browser.elf img_test img_test_asan *.boot.tmp $(addsuffix .o,$(UCMDS)) $(addsuffix .elf,$(UCMDS)) gallery.o gallery.elf $(TLSLIB_OBJS) tls_test bootstrap.o bootstrap.elf bootstrap.bin send_serial kexec_send netkexec_send netmonitor BOOTX64.EFI esp.img usb-efi.img efi/main.o efi/payload.o efi/tramp.o efi/tramp.elf efi/tramp.bin efi/efi_entry.h

+ 4 - 0
kernel.c

@@ -22,6 +22,7 @@
 #include "sys_gfx.h"
 #include "thermal.h"
 #include "timer.h"
+#include "tpec.h"
 #include "tss.h"
 #include "usbhid.h"
 #include "wm.h"
@@ -378,6 +379,9 @@ void kernel_main(void)
 	boot_trace("io");
 	idle_init();
 	thermal_init();
+	/* Board sensors: the ThinkPad EC probe is gated on SMBIOS identifying a
+	 * Lenovo/IBM machine, so it is a no-op everywhere else. */
+	tpec_init();
 	boot_trace("idle");
 
 	/* process_init must run before timer_init: once the PIT is started (sti

+ 23 - 0
sys_info.c

@@ -11,10 +11,14 @@
 #include "sched.h"
 #include "sys_fs.h"
 #include "thermal.h"
+#include "tpec.h"
 #include "usb.h"
 #include "usbhid.h"
 #include "vfs.h"
 
+_Static_assert(TPEC_TEMP_COUNT == SYS_TPEC_TEMP_MAX,
+	       "sys_tpec_sensors.temp_c and tpec_info.temp_c must match");
+
 static void copy_str(char *dst, const char *src, unsigned int size)
 {
 	unsigned int i = 0;
@@ -147,3 +151,22 @@ int sys_sensors(struct sys_sensors *out)
 	}
 	return 0;
 }
+
+int sys_tpec_sensors(struct sys_tpec_sensors *out)
+{
+	struct tpec_info info;
+	unsigned int i;
+
+	if (!out)
+		return -1;
+	tpec_read(&info);
+	out->present = info.present ? 1u : 0u;
+	copy_str(out->vendor, info.vendor, sizeof(out->vendor));
+	copy_str(out->product, info.product, sizeof(out->product));
+	copy_str(out->version, info.version, sizeof(out->version));
+	for (i = 0; i < SYS_TPEC_TEMP_MAX; i++)
+		out->temp_c[i] = info.temp_c[i];
+	out->fan_rpm = info.fan_rpm;
+	out->fan_level = info.fan_level;
+	return 0;
+}

+ 1 - 0
sys_info.h

@@ -11,5 +11,6 @@ int sys_storage_info(struct sys_storage_info *out);
 int sys_blkdev(unsigned int index, struct sys_blkdev *out);
 int sys_cputemp(struct sys_cputemp *out);
 int sys_sensors(struct sys_sensors *out);
+int sys_tpec_sensors(struct sys_tpec_sensors *out);
 
 #endif

+ 5 - 0
syscall.c

@@ -280,6 +280,11 @@ static int syscall_dispatch_locked(int num, int a1, int a2, int a3)
 					  sizeof(struct sys_sensors), 1))
 			return -1;
 		return sys_sensors((struct sys_sensors *)a1);
+	case SYS_TPEC_SENSORS:
+		if (!paging_user_range_ok(current_proc()->cr3, (unsigned int)a1,
+					  sizeof(struct sys_tpec_sensors), 1))
+			return -1;
+		return sys_tpec_sensors((struct sys_tpec_sensors *)a1);
 	case SYS_WIN_KEY_WAIT:
 		return sys_win_key_wait((unsigned int)a1);
 	case SYS_SCHEDINFO:

+ 17 - 0
syscall.h

@@ -135,6 +135,9 @@
 #define SYS_NET_SPEED     100  /* (mbps) -> 0; 0 = auto, 100 = force 100M */
 #define SYS_NET_MAC_SET   101  /* (const mac[6]) -> 0/-1/-2 */
 
+/* ThinkPad embedded-controller sensors (see tpec.c); read-only. */
+#define SYS_TPEC_SENSORS  102  /* (struct sys_tpec_sensors *out) -> 0/-1 */
+
 /* Stat one path (the file service has no open handle). */
 #define SYS_FS_STAT        98  /* (path, struct sys_stat *out) -> 0/<0 */
 
@@ -263,6 +266,20 @@ struct sys_sensors {
 	unsigned int fan_rpm[3];
 };
 
+/* ThinkPad embedded-controller snapshot (see tpec.c).  temp_c[] follows
+ * thinkpad_acpi's temp1..temp16 (EC 0x78..0x7F, then 0xC0..0xC7); -128 means
+ * the sensor is not present.  fan_level is the raw EC 0x2F value. */
+#define SYS_TPEC_TEMP_MAX 16
+struct sys_tpec_sensors {
+	unsigned int present;
+	char vendor[16];
+	char product[32];
+	char version[32];
+	int temp_c[SYS_TPEC_TEMP_MAX];
+	unsigned int fan_rpm;
+	unsigned int fan_level;
+};
+
 /* Window canvas handed to a ring-3 graphics command (see sys_gfx.c). */
 struct sys_gfx_info {
 	unsigned int width;    /* canvas (= window content) size in pixels */

+ 68 - 5
thermal.c

@@ -4,9 +4,12 @@
  * below TjMax in bits 22:16.  Bonnell (Atom 230/330) has no
  * IA32_TEMPERATURE_TARGET MSR, so TjMax has to come from a table; these are
  * the same values Linux's coretemp uses (the 330's DTS is calibrated against
- * 125 C, other Bonnell parts against 90 C).  The DTS "valid" bit is ignored
- * on purpose: coretemp does the same because the delta stays meaningful (the
- * bit can be clear at temperatures below the sensor's range).
+ * 125 C, other Bonnell parts against 90 C).  Nehalem and later (including the
+ * X201's Arrandale) publish TjMax in bits 23:16 of MSR 0x1A2, which is read
+ * for the models that implement it; the table remains the fallback.  The DTS
+ * "valid" bit is ignored on purpose: coretemp does the same because the delta
+ * stays meaningful (the bit can be clear at temperatures below the sensor's
+ * range).
  */
 #include "thermal.h"
 #include "cpu.h"
@@ -14,10 +17,13 @@
 #include "serial.h"
 #include "timer.h"
 
-#define MSR_IA32_THERM_STATUS 0x19C
+#define MSR_IA32_THERM_STATUS        0x19C
+#define MSR_IA32_TEMPERATURE_TARGET  0x1A2
 
 static int thermal_state = -1;  /* -1 unprobed, 0 unsupported, 1 DTS */
 static int thermal_tjmax_c = 100;
+static unsigned int thermal_tjmax_raw;   /* last MSR 0x1A2 sample */
+static int thermal_tjmax_msr;            /* TjMax came from the MSR */
 
 static void cpuid_leaf(unsigned int leaf, unsigned int sub,
 		       unsigned int *a, unsigned int *b, unsigned int *c,
@@ -65,12 +71,47 @@ static int model_has_dts(unsigned int model)
 	case 0x1D:   /* Atom Pineview */
 	case 0x1E:   /* Lynnfield */
 	case 0x1F:   /* Nehalem */
+	case 0x25:   /* Arrandale/Clarkdale (Core i5/i7 M, X201) */
+	case 0x2C:   /* Westmere-EP */
+	case 0x2E:   /* Nehalem-EX */
+	case 0x2F:   /* Westmere-EX */
 		return 1;
 	default:
 		return 0;
 	}
 }
 
+/* Models whose TjMax must be read from IA32_TEMPERATURE_TARGET (0x1A2)
+ * instead of the Bonnell table.  Nehalem and later implement the MSR; older
+ * coretemp-listed parts do not, so a read there could raise #GP.  Linux's
+ * coretemp reads the same MSR and falls back to 100 C when it is missing or
+ * implausible. */
+static int model_has_msr_tjmax(unsigned int model)
+{
+	switch (model) {
+	case 0x1A:
+	case 0x1E:
+	case 0x1F:
+	case 0x25:
+	case 0x2C:
+	case 0x2E:
+	case 0x2F:
+		return 1;
+	default:
+		return 0;
+	}
+}
+
+static unsigned int read_tjmax_msr(void)
+{
+	unsigned int lo, hi;
+
+	__asm__ volatile ("rdmsr" : "=a"(lo), "=d"(hi)
+			  : "c"(MSR_IA32_TEMPERATURE_TARGET));
+	(void)hi;
+	return (lo >> 16) & 0xFF;
+}
+
 static void read_brand(char out[49])
 {
 	unsigned int max_ext, a, b, c, d, i;
@@ -114,6 +155,7 @@ void thermal_init(void)
 	unsigned int family, model = 0, stepping = 0;
 	char vendor[13];
 	char brand[49];
+	int msr_probed = 0;
 
 	thermal_state = 0;
 	thermal_tjmax_c = 100;
@@ -148,6 +190,18 @@ void thermal_init(void)
 
 	read_brand(brand);
 	thermal_tjmax_c = pick_tjmax(model, stepping, brand);
+	thermal_tjmax_msr = 0;
+	thermal_tjmax_raw = 0;
+	if (model_has_msr_tjmax(model)) {
+		unsigned int target = read_tjmax_msr();
+
+		thermal_tjmax_raw = target;
+		msr_probed = 1;
+		if (target >= 80 && target <= 125) {
+			thermal_tjmax_c = (int)target;
+			thermal_tjmax_msr = 1;
+		}
+	}
 	thermal_state = 1;
 
 out:
@@ -158,7 +212,16 @@ out:
 		serial_putdec(SERIAL_COM1, stepping);
 		serial_puts(SERIAL_COM1, ", TjMax ");
 		serial_putdec(SERIAL_COM1, (unsigned int)thermal_tjmax_c);
-		serial_puts(SERIAL_COM1, " C\n");
+		serial_puts(SERIAL_COM1, " C");
+		if (msr_probed) {
+			serial_puts(SERIAL_COM1, " (msr 0x1A2=0x");
+			serial_puthex(SERIAL_COM1, thermal_tjmax_raw);
+			serial_puts(SERIAL_COM1,
+				    thermal_tjmax_msr ? ")" : ", out of range)");
+		} else {
+			serial_puts(SERIAL_COM1, " (table)");
+		}
+		serial_puts(SERIAL_COM1, "\n");
 	} else {
 		serial_puts(SERIAL_COM1, "thermal: no Intel DTS, disabled\n");
 	}

+ 368 - 0
tpec.c

@@ -0,0 +1,368 @@
+/* Read-only ThinkPad embedded-controller sensors (see tpec.h).
+ *
+ * The X201 (i5 M 520, Arrandale) has no IT87xx Super I/O like the
+ * AT3IONT-I.  Its board temperatures and the fan tachometer live behind the
+ * ACPI embedded controller, which Linux's thinkpad_acpi reads through the
+ * same ports: command/status 0x66, data 0x62.  The EC protocol is simple
+ * enough to drive directly:
+ *
+ *   write 0x80 (read) to 0x66, then the register index to 0x62,
+ *   wait for OBF (0x01) and read the byte back from 0x62.
+ *
+ * The SMBIOS type 1 entry gates the probe to Lenovo/IBM machines, so the
+ * EC ports are never touched on the ASUS board, QEMU or other hardware.
+ *
+ * Two details matter on real hardware:
+ *  - There is no SCI handler, so hotkey events queue up in the output buffer.
+ *    A pending byte is drained before every command; otherwise the EC would
+ *    never replace it with the register we asked for.
+ *  - Firmware keeps running in legacy/SMM mode (we have no ACPI interpreter),
+ *    so an SMI can access the EC between our port accesses.  The status
+ *    handshake plus bounded polling keeps that to an occasional bad sample;
+ *    nothing is written, so the EC state cannot be corrupted.
+ *
+ * EC register map (thinkpad_acpi): 0x78..0x7F and 0xC0..0xC7 temperatures
+ * (0x80 = sensor not present, 0x00 = register not implemented), 0x84/0x85
+ * fan tachometer LSB/MSB and 0x2F fan control (bit 7 automatic, bit 6
+ * disengaged/full speed, bits 2:0 level).  EC 0xEF is the functional
+ * revision: revision 3 and later only implement the 0x78..0x7F sensors.
+ */
+#include "tpec.h"
+#include "paging.h"
+#include "pci.h"      /* port_inb/port_outb */
+#include "serial.h"
+
+#define EC_DATA 0x62
+#define EC_STS  0x66
+
+#define EC_OBF  0x01   /* output buffer full */
+#define EC_IBF  0x02   /* input buffer full */
+#define EC_READ 0x80
+
+#define TP_FAN_LEVEL  0x2F
+#define TP_FAN_LSB    0x84
+#define TP_FAN_MSB    0x85
+#define TP_TEMP_LO    0x78   /* temp_c[0..7] */
+#define TP_TEMP_HI    0xC0   /* temp_c[8..15] */
+#define TP_EC_REV     0xEF   /* EC functional revision */
+
+/* Enough for a slow EC command (tens of milliseconds of port polls) but
+ * short enough that a dead controller cannot stall the boot or a syscall. */
+#define EC_POLL_MAX 20000
+
+static int tpec_state = -1;   /* -1 unprobed, 0 absent, 1 present */
+static unsigned int tpec_ec_rev;
+static char tpec_vendor[16];
+static char tpec_product[32];
+static char tpec_version[32];
+
+static unsigned int rd32(const unsigned char *p)
+{
+	return (unsigned int)p[0] | ((unsigned int)p[1] << 8) |
+	       ((unsigned int)p[2] << 16) | ((unsigned int)p[3] << 24);
+}
+
+static void copy_trim(char *dst, unsigned int size, const unsigned char *src)
+{
+	unsigned int i = 0;
+
+	while (i + 1 < size && src && src[i]) {
+		dst[i] = (char)src[i];
+		i++;
+	}
+	while (i && dst[i - 1] == ' ')
+		i--;
+	dst[i] = '\0';
+}
+
+/* Map every 4 MB page touched by [addr, addr+len); SMBIOS tables can sit
+ * above the 128 MB identity map on larger machines (same approach as
+ * acpi.c). */
+static void map_range(unsigned int addr, unsigned int len)
+{
+	unsigned int p = addr & 0xFFC00000u;
+	unsigned int last = (addr + len - 1u) & 0xFFC00000u;
+
+	for (;;) {
+		paging_map_ram(p, 0x400000u);
+		if (p >= last || p > 0xFFC00000u - 0x400000u)
+			break;
+		p += 0x400000u;
+	}
+}
+
+/* The n-th (1-based) string that follows a formatted SMBIOS structure. */
+static const unsigned char *smbios_string(const unsigned char *p,
+					  const unsigned char *end,
+					  unsigned int index)
+{
+	unsigned int n = 1;
+
+	if (!index)
+		return 0;
+	while (p < end && *p) {
+		const unsigned char *s = p;
+
+		if (n == index)
+			return s;
+		while (p < end && *p)
+			p++;
+		p++;
+		n++;
+	}
+	return 0;
+}
+
+static int smbios_type1(char vendor[16], char product[32], char version[32])
+{
+	const unsigned char *ep = 0;
+	const unsigned char *table, *end, *p;
+	unsigned int addr, table_addr, table_len, i;
+
+	for (addr = 0xF0000; addr < 0x100000; addr += 16) {
+		const unsigned char *q = (const unsigned char *)addr;
+		unsigned char sum = 0;
+		unsigned int len;
+
+		if (q[0] != '_' || q[1] != 'S' || q[2] != 'M' || q[3] != '_')
+			continue;
+		len = q[5];
+		if (len < 0x1F || len > 0x40)
+			continue;
+		for (i = 0; i < len; i++)
+			sum = (unsigned char)(sum + q[i]);
+		if (sum)
+			continue;
+		if (q[0x10] != '_' || q[0x11] != 'D' || q[0x12] != 'M' ||
+		    q[0x13] != 'I' || q[0x14] != '_')
+			continue;
+		ep = q;
+		break;
+	}
+	if (!ep)
+		return -1;
+
+	table_len = (unsigned int)ep[0x16] | ((unsigned int)ep[0x17] << 8);
+	table_addr = rd32(ep + 0x18);
+	if (table_addr < 0x1000 || table_addr >= 0xFF000000u ||
+	    table_len < 4 || table_len > 0x10000u ||
+	    table_addr + table_len < table_addr)
+		return -1;
+
+	map_range(table_addr, table_len);
+	table = (const unsigned char *)table_addr;
+	end = table + table_len;
+
+	for (p = table; p + 4 <= end; ) {
+		unsigned char type = p[0];
+		unsigned int len = p[1];
+		const unsigned char *strs, *next;
+
+		if (len < 4)
+			break;
+		if (type == 127)          /* end-of-table */
+			break;
+		strs = p + len;
+		if (strs > end)
+			break;
+		if (type == 1) {
+			if (len >= 8) {
+				copy_trim(vendor, 16,
+					  smbios_string(strs, end, p[4]));
+				copy_trim(product, 32,
+					  smbios_string(strs, end, p[5]));
+				copy_trim(version, 32,
+					  smbios_string(strs, end, p[6]));
+				return 0;
+			}
+			break;
+		}
+		next = strs;
+		while (next + 1 < end && !(next[0] == 0 && next[1] == 0))
+			next++;
+		next += 2;
+		if (next > end)
+			break;
+		p = next;
+	}
+	return -1;
+}
+
+static int ec_wait(unsigned char mask, int set)
+{
+	unsigned int i;
+
+	for (i = 0; i < EC_POLL_MAX; i++) {
+		unsigned char st = port_inb(EC_STS);
+
+		if (st == 0xFF)           /* no controller on these ports */
+			return -1;
+		if (set ? (st & mask) != 0 : (st & mask) == 0)
+			return 0;
+	}
+	return -1;
+}
+
+/* Keep a stale output byte (a hotkey event) from shadowing the register we
+ * are about to read. */
+static int ec_drain(void)
+{
+	unsigned int i;
+
+	for (i = 0; i < 64; i++) {
+		unsigned char st = port_inb(EC_STS);
+
+		if (st == 0xFF)
+			return -1;
+		if (!(st & EC_OBF))
+			return 0;
+		(void)port_inb(EC_DATA);
+	}
+	return -1;
+}
+
+static int ec_read(unsigned char reg, unsigned char *out)
+{
+	if (ec_drain())
+		return -1;
+	if (ec_wait(EC_IBF, 0))
+		return -1;
+	port_outb(EC_STS, EC_READ);
+	if (ec_wait(EC_IBF, 0))
+		return -1;
+	port_outb(EC_DATA, reg);
+	if (ec_wait(EC_OBF, 1))
+		return -1;
+	*out = port_inb(EC_DATA);
+	return 0;
+}
+
+static int str_eq(const char *a, const char *b)
+{
+	while (*a && *a == *b) {
+		a++;
+		b++;
+	}
+	return *a == *b;
+}
+
+static void log_info(void)
+{
+	serial_puts(SERIAL_COM1, "tpec: ");
+	if (tpec_vendor[0]) {
+		serial_puts(SERIAL_COM1, tpec_vendor);
+		serial_puts(SERIAL_COM1, " ");
+	}
+	if (tpec_product[0]) {
+		serial_puts(SERIAL_COM1, tpec_product);
+		serial_puts(SERIAL_COM1, " ");
+	}
+	if (tpec_version[0])
+		serial_puts(SERIAL_COM1, tpec_version);
+}
+
+void tpec_init(void)
+{
+	char vendor[16], product[32], version[32];
+	unsigned char probe, rev;
+
+	tpec_state = 0;
+	tpec_vendor[0] = '\0';
+	tpec_product[0] = '\0';
+	tpec_version[0] = '\0';
+
+	if (smbios_type1(vendor, product, version) != 0) {
+		serial_puts(SERIAL_COM1, "tpec: no SMBIOS type 1, disabled\n");
+		return;
+	}
+	copy_trim(tpec_vendor, sizeof(tpec_vendor), (const unsigned char *)vendor);
+	copy_trim(tpec_product, sizeof(tpec_product),
+		  (const unsigned char *)product);
+	copy_trim(tpec_version, sizeof(tpec_version),
+		  (const unsigned char *)version);
+
+	if (!str_eq(vendor, "LENOVO") && !str_eq(vendor, "IBM")) {
+		log_info();
+		serial_puts(SERIAL_COM1, ": not a ThinkPad vendor, no EC probe\n");
+		return;
+	}
+
+	if (ec_read(TP_FAN_LEVEL, &probe) != 0) {
+		log_info();
+		serial_puts(SERIAL_COM1, ": EC not responding\n");
+		return;
+	}
+	if (ec_read(TP_EC_REV, &rev) == 0)
+		tpec_ec_rev = rev;
+	tpec_state = 1;
+
+	log_info();
+	serial_puts(SERIAL_COM1, ": EC ok, fw rev ");
+	serial_putdec(SERIAL_COM1, tpec_ec_rev);
+	serial_puts(SERIAL_COM1, ", fan level 0x");
+	serial_puthex(SERIAL_COM1, probe);
+	serial_puts(SERIAL_COM1, "\n");
+}
+
+void tpec_read(struct tpec_info *out)
+{
+	unsigned char b;
+	unsigned int i;
+
+	out->present = 0;
+	out->vendor[0] = '\0';
+	out->product[0] = '\0';
+	out->version[0] = '\0';
+	for (i = 0; i < TPEC_TEMP_COUNT; i++)
+		out->temp_c[i] = -128;
+	out->fan_rpm = 0;
+	out->fan_level = 0;
+
+	if (tpec_state < 0)
+		tpec_init();
+	if (!tpec_state)
+		return;
+
+	out->present = 1;
+	for (i = 0; i < sizeof(out->vendor) - 1 && tpec_vendor[i]; i++)
+		out->vendor[i] = tpec_vendor[i];
+	out->vendor[i] = '\0';
+	for (i = 0; i < sizeof(out->product) - 1 && tpec_product[i]; i++)
+		out->product[i] = tpec_product[i];
+	out->product[i] = '\0';
+	for (i = 0; i < sizeof(out->version) - 1 && tpec_version[i]; i++)
+		out->version[i] = tpec_version[i];
+	out->version[i] = '\0';
+
+	for (i = 0; i < 8; i++) {
+		if (ec_read((unsigned char)(TP_TEMP_LO + i), &b) != 0)
+			continue;
+		/* 0x80 = sensor present but unavailable, 0x00 = register not
+		 * implemented (thinkpad_acpi treats a block of zeros as an EC
+		 * that does not expose the register map at all). */
+		if (b != 0x80 && b != 0x00)
+			out->temp_c[i] = (signed char)b;
+	}
+	/* EC functional revision 3 and later only uses 0x78..0x7F; the 0xC0
+	 * block may hold unrelated values (e.g. a power supply ID) there. */
+	if (tpec_ec_rev < 3) {
+		for (i = 0; i < 8; i++) {
+			if (ec_read((unsigned char)(TP_TEMP_HI + i), &b) != 0)
+				continue;
+			if (b != 0x80 && b != 0x00)
+				out->temp_c[8 + i] = (signed char)b;
+		}
+	}
+
+	{
+		unsigned char lo = 0, hi = 0;
+
+		if (ec_read(TP_FAN_LSB, &lo) == 0 &&
+		    ec_read(TP_FAN_MSB, &hi) == 0) {
+			if (!(lo == 0xFF && hi == 0xFF))
+				out->fan_rpm = ((unsigned int)hi << 8) | lo;
+		}
+	}
+	if (ec_read(TP_FAN_LEVEL, &b) == 0)
+		out->fan_level = b;
+}

+ 29 - 0
tpec.h

@@ -0,0 +1,29 @@
+/* Read-only ThinkPad embedded-controller sensors (X201/Arrandale class). */
+#ifndef TPEC_H
+#define TPEC_H
+
+#define TPEC_TEMP_COUNT 16
+
+/* Temperatures are indexed like thinkpad_acpi's temp1..temp16:
+ * temp_c[0..7] come from EC 0x78..0x7F, temp_c[8..15] from EC 0xC0..0xC7.
+ * -128 means the EC reports the sensor as not present. */
+struct tpec_info {
+	int present;
+	char vendor[16];
+	char product[32];
+	char version[32];
+	int temp_c[TPEC_TEMP_COUNT];
+	unsigned int fan_rpm;
+	unsigned int fan_level;   /* raw EC 0x2F value */
+};
+
+/* Probe SMBIOS + the EC once on the BSP; logs the result to COM1.  Does
+ * nothing (present = 0) unless SMBIOS identifies a Lenovo/IBM machine and the
+ * EC answers, so boards without one are never touched. */
+void tpec_init(void);
+
+/* Read the current temperatures, fan speed and fan level.  present is 0 when
+ * no ThinkPad EC was found. */
+void tpec_read(struct tpec_info *out);
+
+#endif

+ 5 - 0
ucmd_lib.h

@@ -397,6 +397,11 @@ static inline int u_sensors(struct sys_sensors *out)
 	return u_syscall1(SYS_SENSORS, (int)out);
 }
 
+static inline int u_tpec_sensors(struct sys_tpec_sensors *out)
+{
+	return u_syscall1(SYS_TPEC_SENSORS, (int)out);
+}
+
 /* Per-process anonymous heap: one growable arena, freed at exit. */
 static inline void *u_mem_alloc(unsigned int bytes)
 {

+ 124 - 31
ucmd_sensors.c

@@ -1,10 +1,13 @@
-/* sensors: CPU DTS + IT87xx Super I/O temperatures and fan speeds.
+/* sensors: CPU DTS plus the board's hardware monitor.
+ *
+ * Two board sources exist: the IT87xx Super I/O (AT3IONT-I, three
+ * temperatures and fan tachometers) and the ThinkPad embedded controller
+ * (X201, temperatures at EC 0x78..0x7F / 0xC0..0xC7 plus the fan tachometer).
  *
  * The DTS TjMax of the Atom 330 is not published; Intel only specifies
  * TJ = 90 C and Tcase-max = 85.2 C, while Linux coretemp uses 125 C for this
  * model.  This command prints the raw DTS delta and the resulting absolute
- * temperature for each common TjMax, plus the Super I/O's independent diode
- * readings and the fan tachometers.
+ * temperature for each common TjMax, plus the board's independent readings.
  */
 #include "ucmd_lib.h"
 
@@ -44,25 +47,33 @@ static void put_scale(int tjmax, int delta)
 	}
 }
 
-void _start(void)
+static void show_dts(void)
 {
 	struct sys_cputemp ct;
-	struct sys_sensors s;
 	unsigned int i;
 
 	u_puts("dts ");
-	if (u_cpu_temp(&ct) == 0 && ct.temp_c >= 0) {
-		u_puts("max ");
-		put_temp(ct.temp_c);
-		u_puts(" C (tjmax ");
-		u_putdec((unsigned int)ct.tjmax_c);
-		u_puts(" C)\n");
-		if (ct.delta_c >= 0) {
-			u_puts("this core delta ");
-			u_putdec((unsigned int)ct.delta_c);
-			u_puts(", status 0x");
-			u_puthex(ct.status);
-			u_puts("\nscale 90: ");
+	if (u_cpu_temp(&ct) != 0 || ct.temp_c < 0) {
+		u_puts("unavailable (no Intel DTS)\n");
+		return;
+	}
+	u_puts("max ");
+	put_temp(ct.temp_c);
+	u_puts(" C (tjmax ");
+	u_putdec((unsigned int)ct.tjmax_c);
+	u_puts(" C)\n");
+	if (ct.delta_c >= 0) {
+		u_puts("this core delta ");
+		u_putdec((unsigned int)ct.delta_c);
+		u_puts(", status 0x");
+		u_puthex(ct.status);
+		u_putc('\n');
+		/* The scales are for CPUs whose TjMax is not published (the Atom
+		 * 330); when the MSR reports a real value such as 105 C for
+		 * Arrandale there is nothing to guess. */
+		if (ct.tjmax_c == 90 || ct.tjmax_c == 100 ||
+		    ct.tjmax_c == 125) {
+			u_puts("scale 90: ");
 			put_scale(90, ct.delta_c);
 			u_puts(", 100: ");
 			put_scale(100, ct.delta_c);
@@ -70,22 +81,24 @@ void _start(void)
 			put_scale(125, ct.delta_c);
 			u_putc('\n');
 		}
-		u_puts("cores ");
-		for (i = 0; i < ct.ncpus && i < SYS_CPU_MAX; i++) {
-			put_temp(ct.cpu_temp_c[i]);
-			u_puts(" C");
-			if (i + 1 < ct.ncpus && i + 1 < SYS_CPU_MAX)
-				u_puts(", ");
-		}
-		u_putc('\n');
-	} else {
-		u_puts("unavailable (no Intel DTS)\n");
 	}
-
-	if (u_sensors(&s) != 0 || !s.present) {
-		u_puts("it87 not found\n");
-		u_exit(0);
+	u_puts("cores ");
+	for (i = 0; i < ct.ncpus && i < SYS_CPU_MAX; i++) {
+		put_temp(ct.cpu_temp_c[i]);
+		u_puts(" C");
+		if (i + 1 < ct.ncpus && i + 1 < SYS_CPU_MAX)
+			u_puts(", ");
 	}
+	u_putc('\n');
+}
+
+static int show_it87(void)
+{
+	struct sys_sensors s;
+	unsigned int i;
+
+	if (u_sensors(&s) != 0 || !s.present)
+		return 0;
 	u_puts("it87 chip 0x");
 	put_hex16(s.chip_id);
 	u_puts(" base 0x");
@@ -122,5 +135,85 @@ void _start(void)
 			u_puts(", ");
 	}
 	u_putc('\n');
+	return 1;
+}
+
+/* EC 0x2F: bit 7 = automatic, bit 6 = disengaged/full speed (takes
+ * precedence), bits 2:0 = manual level (0 stops the fan, 7 is the usual max).
+ */
+static void put_fan_level(unsigned int level)
+{
+	u_puts(" (0x");
+	u_put_hex2(level);
+	if (level & 0x40)
+		u_puts(", full speed");
+	else if (level & 0x80)
+		u_puts(", auto");
+	else if ((level & 0x3F) == 0)
+		u_puts(", stopped");
+	else {
+		u_puts(", level ");
+		u_putdec(level & 0x3F);
+	}
+	u_puts(")");
+}
+
+static int show_tpec(void)
+{
+	struct sys_tpec_sensors s;
+	unsigned int i, shown = 0;
+
+	if (u_tpec_sensors(&s) != 0 || !s.present)
+		return 0;
+	u_puts("thinkpad");
+	if (s.vendor[0]) {
+		u_puts(" ");
+		u_puts(s.vendor);
+	}
+	if (s.product[0]) {
+		u_puts(" ");
+		u_puts(s.product);
+	}
+	if (s.version[0]) {
+		u_puts(" ");
+		u_puts(s.version);
+	}
+	u_putc('\n');
+	for (i = 0; i < SYS_TPEC_TEMP_MAX; i++) {
+		unsigned int reg = i < 8 ? 0x78 + i : 0xC0 + i - 8;
+
+		if (s.temp_c[i] <= -50)
+			continue;
+		if (shown)
+			u_puts(", ");
+		u_puts("temp");
+		u_putdec(i + 1);
+		u_puts(" (0x");
+		u_put_hex2(reg);
+		u_puts(") ");
+		put_temp(s.temp_c[i]);
+		u_puts(" C");
+		shown++;
+	}
+	if (!shown)
+		u_puts("no EC temperature sensors");
+	u_putc('\n');
+	u_puts("fan ");
+	u_putdec(s.fan_rpm);
+	u_puts(" RPM");
+	put_fan_level(s.fan_level);
+	u_putc('\n');
+	return 1;
+}
+
+void _start(void)
+{
+	int any;
+
+	show_dts();
+	any = show_it87();
+	any |= show_tpec();
+	if (!any)
+		u_puts("no board sensors found\n");
 	u_exit(0);
 }