tpec.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /* Read-only ThinkPad embedded-controller sensors (see tpec.h).
  2. *
  3. * The X201 (i5 M 520, Arrandale) has no IT87xx Super I/O like the
  4. * AT3IONT-I. Its board temperatures and the fan tachometer live behind the
  5. * ACPI embedded controller, which Linux's thinkpad_acpi reads through the
  6. * same ports: command/status 0x66, data 0x62. The EC protocol is simple
  7. * enough to drive directly:
  8. *
  9. * write 0x80 (read) to 0x66, then the register index to 0x62,
  10. * wait for OBF (0x01) and read the byte back from 0x62.
  11. *
  12. * The SMBIOS type 1 entry gates the probe to Lenovo/IBM machines, so the
  13. * EC ports are never touched on the ASUS board, QEMU or other hardware.
  14. *
  15. * Two details matter on real hardware:
  16. * - There is no SCI handler, so hotkey events queue up in the output buffer.
  17. * A pending byte is drained before every command; otherwise the EC would
  18. * never replace it with the register we asked for.
  19. * - Firmware keeps running in legacy/SMM mode (we have no ACPI interpreter),
  20. * so an SMI can access the EC between our port accesses. The status
  21. * handshake plus bounded polling keeps that to an occasional bad sample;
  22. * nothing is written, so the EC state cannot be corrupted.
  23. *
  24. * EC register map (thinkpad_acpi): 0x78..0x7F and 0xC0..0xC7 temperatures
  25. * (0x80 = sensor not present, 0x00 = register not implemented), 0x84/0x85
  26. * fan tachometer LSB/MSB and 0x2F fan control (bit 7 automatic, bit 6
  27. * disengaged/full speed, bits 2:0 level). EC 0xEF is the functional
  28. * revision: revision 3 and later only implement the 0x78..0x7F sensors.
  29. */
  30. #include "tpec.h"
  31. #include "paging.h"
  32. #include "pci.h" /* port_inb/port_outb */
  33. #include "serial.h"
  34. #define EC_DATA 0x62
  35. #define EC_STS 0x66
  36. #define EC_OBF 0x01 /* output buffer full */
  37. #define EC_IBF 0x02 /* input buffer full */
  38. #define EC_READ 0x80
  39. #define TP_FAN_LEVEL 0x2F
  40. #define TP_FAN_LSB 0x84
  41. #define TP_FAN_MSB 0x85
  42. #define TP_TEMP_LO 0x78 /* temp_c[0..7] */
  43. #define TP_TEMP_HI 0xC0 /* temp_c[8..15] */
  44. #define TP_EC_REV 0xEF /* EC functional revision */
  45. /* Enough for a slow EC command (tens of milliseconds of port polls) but
  46. * short enough that a dead controller cannot stall the boot or a syscall. */
  47. #define EC_POLL_MAX 20000
  48. static int tpec_state = -1; /* -1 unprobed, 0 absent, 1 present */
  49. static unsigned int tpec_ec_rev;
  50. static char tpec_vendor[16];
  51. static char tpec_product[32];
  52. static char tpec_version[32];
  53. static unsigned int rd32(const unsigned char *p)
  54. {
  55. return (unsigned int)p[0] | ((unsigned int)p[1] << 8) |
  56. ((unsigned int)p[2] << 16) | ((unsigned int)p[3] << 24);
  57. }
  58. static void copy_trim(char *dst, unsigned int size, const unsigned char *src)
  59. {
  60. unsigned int i = 0;
  61. while (i + 1 < size && src && src[i]) {
  62. dst[i] = (char)src[i];
  63. i++;
  64. }
  65. while (i && dst[i - 1] == ' ')
  66. i--;
  67. dst[i] = '\0';
  68. }
  69. /* Map every 4 MB page touched by [addr, addr+len); SMBIOS tables can sit
  70. * above the 128 MB identity map on larger machines (same approach as
  71. * acpi.c). */
  72. static void map_range(unsigned int addr, unsigned int len)
  73. {
  74. unsigned int p = addr & 0xFFC00000u;
  75. unsigned int last = (addr + len - 1u) & 0xFFC00000u;
  76. for (;;) {
  77. paging_map_ram(p, 0x400000u);
  78. if (p >= last || p > 0xFFC00000u - 0x400000u)
  79. break;
  80. p += 0x400000u;
  81. }
  82. }
  83. /* The n-th (1-based) string that follows a formatted SMBIOS structure. */
  84. static const unsigned char *smbios_string(const unsigned char *p,
  85. const unsigned char *end,
  86. unsigned int index)
  87. {
  88. unsigned int n = 1;
  89. if (!index)
  90. return 0;
  91. while (p < end && *p) {
  92. const unsigned char *s = p;
  93. if (n == index)
  94. return s;
  95. while (p < end && *p)
  96. p++;
  97. p++;
  98. n++;
  99. }
  100. return 0;
  101. }
  102. static int smbios_type1(char vendor[16], char product[32], char version[32])
  103. {
  104. const unsigned char *ep = 0;
  105. const unsigned char *table, *end, *p;
  106. unsigned int addr, table_addr, table_len, i;
  107. for (addr = 0xF0000; addr < 0x100000; addr += 16) {
  108. const unsigned char *q = (const unsigned char *)addr;
  109. unsigned char sum = 0;
  110. unsigned int len;
  111. if (q[0] != '_' || q[1] != 'S' || q[2] != 'M' || q[3] != '_')
  112. continue;
  113. len = q[5];
  114. if (len < 0x1F || len > 0x40)
  115. continue;
  116. for (i = 0; i < len; i++)
  117. sum = (unsigned char)(sum + q[i]);
  118. if (sum)
  119. continue;
  120. if (q[0x10] != '_' || q[0x11] != 'D' || q[0x12] != 'M' ||
  121. q[0x13] != 'I' || q[0x14] != '_')
  122. continue;
  123. ep = q;
  124. break;
  125. }
  126. if (!ep)
  127. return -1;
  128. table_len = (unsigned int)ep[0x16] | ((unsigned int)ep[0x17] << 8);
  129. table_addr = rd32(ep + 0x18);
  130. if (table_addr < 0x1000 || table_addr >= 0xFF000000u ||
  131. table_len < 4 || table_len > 0x10000u ||
  132. table_addr + table_len < table_addr)
  133. return -1;
  134. map_range(table_addr, table_len);
  135. table = (const unsigned char *)table_addr;
  136. end = table + table_len;
  137. for (p = table; p + 4 <= end; ) {
  138. unsigned char type = p[0];
  139. unsigned int len = p[1];
  140. const unsigned char *strs, *next;
  141. if (len < 4)
  142. break;
  143. if (type == 127) /* end-of-table */
  144. break;
  145. strs = p + len;
  146. if (strs > end)
  147. break;
  148. if (type == 1) {
  149. if (len >= 8) {
  150. copy_trim(vendor, 16,
  151. smbios_string(strs, end, p[4]));
  152. copy_trim(product, 32,
  153. smbios_string(strs, end, p[5]));
  154. copy_trim(version, 32,
  155. smbios_string(strs, end, p[6]));
  156. return 0;
  157. }
  158. break;
  159. }
  160. next = strs;
  161. while (next + 1 < end && !(next[0] == 0 && next[1] == 0))
  162. next++;
  163. next += 2;
  164. if (next > end)
  165. break;
  166. p = next;
  167. }
  168. return -1;
  169. }
  170. static int ec_wait(unsigned char mask, int set)
  171. {
  172. unsigned int i;
  173. for (i = 0; i < EC_POLL_MAX; i++) {
  174. unsigned char st = port_inb(EC_STS);
  175. if (st == 0xFF) /* no controller on these ports */
  176. return -1;
  177. if (set ? (st & mask) != 0 : (st & mask) == 0)
  178. return 0;
  179. }
  180. return -1;
  181. }
  182. /* Keep a stale output byte (a hotkey event) from shadowing the register we
  183. * are about to read. */
  184. static int ec_drain(void)
  185. {
  186. unsigned int i;
  187. for (i = 0; i < 64; i++) {
  188. unsigned char st = port_inb(EC_STS);
  189. if (st == 0xFF)
  190. return -1;
  191. if (!(st & EC_OBF))
  192. return 0;
  193. (void)port_inb(EC_DATA);
  194. }
  195. return -1;
  196. }
  197. static int ec_read(unsigned char reg, unsigned char *out)
  198. {
  199. if (ec_drain())
  200. return -1;
  201. if (ec_wait(EC_IBF, 0))
  202. return -1;
  203. port_outb(EC_STS, EC_READ);
  204. if (ec_wait(EC_IBF, 0))
  205. return -1;
  206. port_outb(EC_DATA, reg);
  207. if (ec_wait(EC_OBF, 1))
  208. return -1;
  209. *out = port_inb(EC_DATA);
  210. return 0;
  211. }
  212. static int str_eq(const char *a, const char *b)
  213. {
  214. while (*a && *a == *b) {
  215. a++;
  216. b++;
  217. }
  218. return *a == *b;
  219. }
  220. static void log_info(void)
  221. {
  222. serial_puts(SERIAL_COM1, "tpec: ");
  223. if (tpec_vendor[0]) {
  224. serial_puts(SERIAL_COM1, tpec_vendor);
  225. serial_puts(SERIAL_COM1, " ");
  226. }
  227. if (tpec_product[0]) {
  228. serial_puts(SERIAL_COM1, tpec_product);
  229. serial_puts(SERIAL_COM1, " ");
  230. }
  231. if (tpec_version[0])
  232. serial_puts(SERIAL_COM1, tpec_version);
  233. }
  234. void tpec_init(void)
  235. {
  236. char vendor[16], product[32], version[32];
  237. unsigned char probe, rev;
  238. tpec_state = 0;
  239. tpec_vendor[0] = '\0';
  240. tpec_product[0] = '\0';
  241. tpec_version[0] = '\0';
  242. if (smbios_type1(vendor, product, version) != 0) {
  243. serial_puts(SERIAL_COM1, "tpec: no SMBIOS type 1, disabled\n");
  244. return;
  245. }
  246. copy_trim(tpec_vendor, sizeof(tpec_vendor), (const unsigned char *)vendor);
  247. copy_trim(tpec_product, sizeof(tpec_product),
  248. (const unsigned char *)product);
  249. copy_trim(tpec_version, sizeof(tpec_version),
  250. (const unsigned char *)version);
  251. if (!str_eq(vendor, "LENOVO") && !str_eq(vendor, "IBM")) {
  252. log_info();
  253. serial_puts(SERIAL_COM1, ": not a ThinkPad vendor, no EC probe\n");
  254. return;
  255. }
  256. if (ec_read(TP_FAN_LEVEL, &probe) != 0) {
  257. log_info();
  258. serial_puts(SERIAL_COM1, ": EC not responding\n");
  259. return;
  260. }
  261. if (ec_read(TP_EC_REV, &rev) == 0)
  262. tpec_ec_rev = rev;
  263. tpec_state = 1;
  264. log_info();
  265. serial_puts(SERIAL_COM1, ": EC ok, fw rev ");
  266. serial_putdec(SERIAL_COM1, tpec_ec_rev);
  267. serial_puts(SERIAL_COM1, ", fan level 0x");
  268. serial_puthex(SERIAL_COM1, probe);
  269. serial_puts(SERIAL_COM1, "\n");
  270. }
  271. void tpec_read(struct tpec_info *out)
  272. {
  273. unsigned char b;
  274. unsigned int i;
  275. out->present = 0;
  276. out->vendor[0] = '\0';
  277. out->product[0] = '\0';
  278. out->version[0] = '\0';
  279. for (i = 0; i < TPEC_TEMP_COUNT; i++)
  280. out->temp_c[i] = -128;
  281. out->fan_rpm = 0;
  282. out->fan_level = 0;
  283. if (tpec_state < 0)
  284. tpec_init();
  285. if (!tpec_state)
  286. return;
  287. out->present = 1;
  288. for (i = 0; i < sizeof(out->vendor) - 1 && tpec_vendor[i]; i++)
  289. out->vendor[i] = tpec_vendor[i];
  290. out->vendor[i] = '\0';
  291. for (i = 0; i < sizeof(out->product) - 1 && tpec_product[i]; i++)
  292. out->product[i] = tpec_product[i];
  293. out->product[i] = '\0';
  294. for (i = 0; i < sizeof(out->version) - 1 && tpec_version[i]; i++)
  295. out->version[i] = tpec_version[i];
  296. out->version[i] = '\0';
  297. for (i = 0; i < 8; i++) {
  298. if (ec_read((unsigned char)(TP_TEMP_LO + i), &b) != 0)
  299. continue;
  300. /* 0x80 = sensor present but unavailable, 0x00 = register not
  301. * implemented (thinkpad_acpi treats a block of zeros as an EC
  302. * that does not expose the register map at all). */
  303. if (b != 0x80 && b != 0x00)
  304. out->temp_c[i] = (signed char)b;
  305. }
  306. /* EC functional revision 3 and later only uses 0x78..0x7F; the 0xC0
  307. * block may hold unrelated values (e.g. a power supply ID) there. */
  308. if (tpec_ec_rev < 3) {
  309. for (i = 0; i < 8; i++) {
  310. if (ec_read((unsigned char)(TP_TEMP_HI + i), &b) != 0)
  311. continue;
  312. if (b != 0x80 && b != 0x00)
  313. out->temp_c[8 + i] = (signed char)b;
  314. }
  315. }
  316. {
  317. unsigned char lo = 0, hi = 0;
  318. if (ec_read(TP_FAN_LSB, &lo) == 0 &&
  319. ec_read(TP_FAN_MSB, &hi) == 0) {
  320. if (!(lo == 0xFF && hi == 0xFF))
  321. out->fan_rpm = ((unsigned int)hi << 8) | lo;
  322. }
  323. }
  324. if (ec_read(TP_FAN_LEVEL, &b) == 0)
  325. out->fan_level = b;
  326. }