sys_info.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* Kernel-state info for the info commands (meminfo/mouseinfo/storage).
  2. * Small, read-only snapshots; storage additionally refreshes the mount cache,
  3. * which reads disks and therefore runs under the fs-busy gate (see sys_fs.c). */
  4. #include "syscall.h"
  5. #include "ahci.h"
  6. #include "blkdev.h"
  7. #include "cros_ec.h"
  8. #include "it87.h"
  9. #include "mm.h"
  10. #include "mouse.h"
  11. #include "power_src.h"
  12. #include "sched.h"
  13. #include "sys_fs.h"
  14. #include "thermal.h"
  15. #include "touchpad.h"
  16. #include "tpec.h"
  17. #include "usb.h"
  18. #include "usbhid.h"
  19. #include "vfs.h"
  20. _Static_assert(TPEC_TEMP_COUNT == SYS_TPEC_TEMP_MAX,
  21. "sys_tpec_sensors.temp_c and tpec_info.temp_c must match");
  22. _Static_assert(POWER_BATT_MAX == SYS_POWER_BATT_MAX,
  23. "sys_powerinfo.battery and power_info.battery must match");
  24. _Static_assert(CROS_EC_TEMP_MAX == SYS_CROS_TEMP_MAX,
  25. "sys_cros_sensors.temp_c and cros_ec_sensors.temp_c must match");
  26. _Static_assert(CROS_EC_FAN_MAX == SYS_CROS_FAN_MAX,
  27. "sys_cros_sensors.fan_rpm and cros_ec_sensors.fan_rpm must match");
  28. static void copy_str(char *dst, const char *src, unsigned int size)
  29. {
  30. unsigned int i = 0;
  31. while (i + 1 < size && src[i]) {
  32. dst[i] = src[i];
  33. i++;
  34. }
  35. dst[i] = '\0';
  36. }
  37. int sys_meminfo(struct sys_meminfo *out)
  38. {
  39. out->total_kb = mm_total_ram_kb();
  40. out->free_pages = mm_free_pages();
  41. out->page_size = PAGE_SIZE;
  42. out->e820_count = mm_e820_count();
  43. return 0;
  44. }
  45. int sys_e820(unsigned int index, struct sys_e820 *out)
  46. {
  47. const struct e820_entry *e = mm_e820_entry(index);
  48. if (!e)
  49. return 0;
  50. out->base = (unsigned int)e->base;
  51. out->length = (unsigned int)e->length;
  52. out->type = e->type;
  53. return 1;
  54. }
  55. int sys_mouseinfo(struct sys_mouseinfo *out)
  56. {
  57. unsigned int i;
  58. for (i = 0; i < sizeof(*out); i++)
  59. ((unsigned char *)out)[i] = 0;
  60. out->present = mouse_is_present() ? 1 : 0;
  61. out->cursor_visible = mouse_cursor_visible() ? 1 : 0;
  62. copy_str(out->backend, mouse_cursor_backend_name(), sizeof(out->backend));
  63. if (!out->present)
  64. return 0;
  65. out->ps2 = mouse_ps2_present() ? 1 : 0;
  66. if (out->ps2) {
  67. out->ps2_id = mouse_device_id() & 0xFFu;
  68. out->ps2_packet_size = mouse_packet_size();
  69. out->ps2_streaming = mouse_streaming_enabled() ? 1 : 0;
  70. }
  71. out->usb = usbhid_mouse_attached() ? 1 : 0;
  72. if (out->usb) {
  73. unsigned int ep, mps;
  74. int low;
  75. usbhid_get_info(&out->usb_ctrl, &out->usb_addr, &ep, &mps, &low);
  76. out->usb_ep = ep;
  77. out->usb_mps = mps;
  78. out->usb_low = (unsigned int)low;
  79. copy_str(out->hcd, usb_hcd_name_ctrl(out->usb_ctrl),
  80. sizeof(out->hcd));
  81. }
  82. out->usb_keyboard = usbhid_keyboard_attached() ? 1 : 0;
  83. out->touchpad = touchpad_present() ? 1 : 0;
  84. if (out->touchpad) {
  85. out->tp_vendor = touchpad_vendor();
  86. out->tp_product = touchpad_product();
  87. }
  88. return 1;
  89. }
  90. int sys_storage_info(struct sys_storage_info *out)
  91. {
  92. sys_fs_scan(); /* rescan; sleeps in the probes */
  93. out->ahci_present = ahci_controller_present() ? 1 : 0;
  94. out->blkdev_count = blk_dev_count;
  95. return 0;
  96. }
  97. int sys_blkdev(unsigned int index, struct sys_blkdev *out)
  98. {
  99. const blk_dev_t *d;
  100. if (index >= blk_dev_count)
  101. return 0;
  102. d = &blk_devs[index];
  103. copy_str(out->name, d->name, sizeof(out->name));
  104. copy_str(out->model, d->model, sizeof(out->model));
  105. out->kind = (unsigned int)d->kind;
  106. out->present = d->present ? 1 : 0;
  107. out->total_sectors = d->total_sectors;
  108. return 1;
  109. }
  110. int sys_cputemp(struct sys_cputemp *out)
  111. {
  112. unsigned int i;
  113. _Static_assert(MAX_CPUS <= SYS_CPU_MAX,
  114. "sys_cputemp.cpu_temp_c is too small");
  115. if (!out)
  116. return -1;
  117. out->temp_c = thermal_max_c();
  118. out->tjmax_c = thermal_tjmax();
  119. out->delta_c = -1;
  120. out->status = 0;
  121. if (thermal_status_this_cpu(&out->status) == 0)
  122. out->delta_c = (int)((out->status >> 16) & 0x7F);
  123. out->ncpus = cpu_count;
  124. for (i = 0; i < SYS_CPU_MAX; i++)
  125. out->cpu_temp_c[i] = cpus[i].temp_c;
  126. return 0;
  127. }
  128. int sys_sensors(struct sys_sensors *out)
  129. {
  130. struct it87_info info;
  131. unsigned int i;
  132. if (!out)
  133. return -1;
  134. it87_read(&info);
  135. out->present = info.present ? 1u : 0u;
  136. out->chip_id = info.chip_id;
  137. out->base = info.base;
  138. out->temp_enable = info.temp_enable;
  139. out->temp_extra = info.temp_extra;
  140. for (i = 0; i < 3; i++) {
  141. out->temp_c[i] = info.temp_c[i];
  142. out->fan_rpm[i] = info.fan_rpm[i];
  143. }
  144. return 0;
  145. }
  146. int sys_tpec_sensors(struct sys_tpec_sensors *out)
  147. {
  148. struct tpec_info info;
  149. unsigned int i;
  150. if (!out)
  151. return -1;
  152. tpec_read(&info);
  153. out->present = info.present ? 1u : 0u;
  154. copy_str(out->vendor, info.vendor, sizeof(out->vendor));
  155. copy_str(out->product, info.product, sizeof(out->product));
  156. copy_str(out->version, info.version, sizeof(out->version));
  157. for (i = 0; i < SYS_TPEC_TEMP_MAX; i++)
  158. out->temp_c[i] = info.temp_c[i];
  159. out->fan_rpm = info.fan_rpm;
  160. out->fan_level = info.fan_level;
  161. return 0;
  162. }
  163. int sys_powerinfo(struct sys_powerinfo *out, unsigned int flags)
  164. {
  165. struct power_info p;
  166. unsigned int i;
  167. if (!out)
  168. return -1;
  169. power_source_read(&p, (flags & SYS_POWER_F_DETAIL) != 0);
  170. for (i = 0; i < sizeof(*out); i++)
  171. ((unsigned char *)out)[i] = 0;
  172. out->present = p.present ? 1u : 0u;
  173. out->source = (unsigned int)p.source;
  174. out->ac_online = p.ac_online;
  175. out->ec_rev = p.ec_rev;
  176. copy_str(out->vendor, p.vendor, sizeof(out->vendor));
  177. copy_str(out->product, p.product, sizeof(out->product));
  178. copy_str(out->version, p.version, sizeof(out->version));
  179. out->battery_count = p.battery_count;
  180. for (i = 0; i < SYS_POWER_BATT_MAX; i++) {
  181. struct sys_battery *d = &out->battery[i];
  182. const struct power_battery *s = &p.battery[i];
  183. d->present = s->present ? 1u : 0u;
  184. d->error = s->error ? 1u : 0u;
  185. d->status = s->status;
  186. d->charging = s->charging ? 1u : 0u;
  187. d->discharging = s->discharging ? 1u : 0u;
  188. d->percent = s->percent > 0 ? (unsigned int)s->percent : 0u;
  189. d->percent_valid = s->percent_valid ? 1u : 0u;
  190. d->remaining = s->remaining;
  191. d->full = s->full;
  192. d->design = s->design;
  193. d->voltage_mv = s->voltage_mv;
  194. d->rate = s->rate;
  195. d->unit_known = s->unit_known ? 1u : 0u;
  196. d->unit_mwh = s->unit_mwh ? 1u : 0u;
  197. d->cycle_count = s->cycle_count;
  198. copy_str(d->manufacturer, s->manufacturer,
  199. sizeof(d->manufacturer));
  200. copy_str(d->model, s->model, sizeof(d->model));
  201. copy_str(d->serial, s->serial, sizeof(d->serial));
  202. }
  203. return 0;
  204. }
  205. int sys_cros_sensors(struct sys_cros_sensors *out)
  206. {
  207. struct cros_ec_sensors info;
  208. unsigned int i;
  209. if (!out)
  210. return -1;
  211. cros_ec_sensors_read(&info);
  212. out->present = info.present ? 1u : 0u;
  213. out->acpi_version = info.acpi_version;
  214. out->battery_version = info.battery_version;
  215. out->thermal_version = info.thermal_version;
  216. for (i = 0; i < SYS_CROS_TEMP_MAX; i++)
  217. out->temp_c[i] = info.temp_c[i];
  218. for (i = 0; i < SYS_CROS_FAN_MAX; i++)
  219. out->fan_rpm[i] = info.fan_rpm[i];
  220. out->fan_present = info.fan_present;
  221. out->fan_duty = info.fan_duty;
  222. out->switches = info.switches;
  223. return 0;
  224. }