sys_info.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. out->high_free_pages = mm_free_high_pages();
  44. out->uncached_high_mb = mm_uncached_high_mb;
  45. out->uncached_low_mb = mm_uncached_low_mb;
  46. return 0;
  47. }
  48. int sys_e820(unsigned int index, struct sys_e820 *out)
  49. {
  50. const struct e820_entry *e = mm_e820_entry(index);
  51. if (!e)
  52. return 0;
  53. out->base = (unsigned int)e->base;
  54. out->length = (unsigned int)e->length;
  55. out->type = e->type;
  56. return 1;
  57. }
  58. int sys_mouseinfo(struct sys_mouseinfo *out)
  59. {
  60. unsigned int i;
  61. for (i = 0; i < sizeof(*out); i++)
  62. ((unsigned char *)out)[i] = 0;
  63. out->present = mouse_is_present() ? 1 : 0;
  64. out->cursor_visible = mouse_cursor_visible() ? 1 : 0;
  65. copy_str(out->backend, mouse_cursor_backend_name(), sizeof(out->backend));
  66. if (!out->present)
  67. return 0;
  68. out->ps2 = mouse_ps2_present() ? 1 : 0;
  69. if (out->ps2) {
  70. out->ps2_id = mouse_device_id() & 0xFFu;
  71. out->ps2_packet_size = mouse_packet_size();
  72. out->ps2_streaming = mouse_streaming_enabled() ? 1 : 0;
  73. }
  74. out->usb = usbhid_mouse_attached() ? 1 : 0;
  75. if (out->usb) {
  76. unsigned int ep, mps;
  77. int low;
  78. usbhid_get_info(&out->usb_ctrl, &out->usb_addr, &ep, &mps, &low);
  79. out->usb_ep = ep;
  80. out->usb_mps = mps;
  81. out->usb_low = (unsigned int)low;
  82. copy_str(out->hcd, usb_hcd_name_ctrl(out->usb_ctrl),
  83. sizeof(out->hcd));
  84. }
  85. out->usb_keyboard = usbhid_keyboard_attached() ? 1 : 0;
  86. out->touchpad = touchpad_present() ? 1 : 0;
  87. if (out->touchpad) {
  88. out->tp_vendor = touchpad_vendor();
  89. out->tp_product = touchpad_product();
  90. }
  91. return 1;
  92. }
  93. int sys_storage_info(struct sys_storage_info *out)
  94. {
  95. sys_fs_scan(); /* rescan; sleeps in the probes */
  96. out->ahci_present = ahci_controller_present() ? 1 : 0;
  97. out->blkdev_count = blk_dev_count;
  98. return 0;
  99. }
  100. int sys_blkdev(unsigned int index, struct sys_blkdev *out)
  101. {
  102. const blk_dev_t *d;
  103. if (index >= blk_dev_count)
  104. return 0;
  105. d = &blk_devs[index];
  106. copy_str(out->name, d->name, sizeof(out->name));
  107. copy_str(out->model, d->model, sizeof(out->model));
  108. out->kind = (unsigned int)d->kind;
  109. out->present = d->present ? 1 : 0;
  110. out->total_sectors = d->total_sectors;
  111. return 1;
  112. }
  113. int sys_cputemp(struct sys_cputemp *out)
  114. {
  115. unsigned int i;
  116. _Static_assert(MAX_CPUS <= SYS_CPU_MAX,
  117. "sys_cputemp.cpu_temp_c is too small");
  118. if (!out)
  119. return -1;
  120. out->temp_c = thermal_max_c();
  121. out->tjmax_c = thermal_tjmax();
  122. out->delta_c = -1;
  123. out->status = 0;
  124. if (thermal_status_this_cpu(&out->status) == 0)
  125. out->delta_c = (int)((out->status >> 16) & 0x7F);
  126. out->ncpus = cpu_count;
  127. for (i = 0; i < SYS_CPU_MAX; i++)
  128. out->cpu_temp_c[i] = cpus[i].temp_c;
  129. return 0;
  130. }
  131. int sys_sensors(struct sys_sensors *out)
  132. {
  133. struct it87_info info;
  134. unsigned int i;
  135. if (!out)
  136. return -1;
  137. it87_read(&info);
  138. out->present = info.present ? 1u : 0u;
  139. out->chip_id = info.chip_id;
  140. out->base = info.base;
  141. out->temp_enable = info.temp_enable;
  142. out->temp_extra = info.temp_extra;
  143. for (i = 0; i < 3; i++) {
  144. out->temp_c[i] = info.temp_c[i];
  145. out->fan_rpm[i] = info.fan_rpm[i];
  146. }
  147. return 0;
  148. }
  149. int sys_tpec_sensors(struct sys_tpec_sensors *out)
  150. {
  151. struct tpec_info info;
  152. unsigned int i;
  153. if (!out)
  154. return -1;
  155. tpec_read(&info);
  156. out->present = info.present ? 1u : 0u;
  157. copy_str(out->vendor, info.vendor, sizeof(out->vendor));
  158. copy_str(out->product, info.product, sizeof(out->product));
  159. copy_str(out->version, info.version, sizeof(out->version));
  160. for (i = 0; i < SYS_TPEC_TEMP_MAX; i++)
  161. out->temp_c[i] = info.temp_c[i];
  162. out->fan_rpm = info.fan_rpm;
  163. out->fan_level = info.fan_level;
  164. return 0;
  165. }
  166. int sys_powerinfo(struct sys_powerinfo *out, unsigned int flags)
  167. {
  168. struct power_info p;
  169. unsigned int i;
  170. if (!out)
  171. return -1;
  172. power_source_read(&p, (flags & SYS_POWER_F_DETAIL) != 0);
  173. for (i = 0; i < sizeof(*out); i++)
  174. ((unsigned char *)out)[i] = 0;
  175. out->present = p.present ? 1u : 0u;
  176. out->source = (unsigned int)p.source;
  177. out->ac_online = p.ac_online;
  178. out->ec_rev = p.ec_rev;
  179. copy_str(out->vendor, p.vendor, sizeof(out->vendor));
  180. copy_str(out->product, p.product, sizeof(out->product));
  181. copy_str(out->version, p.version, sizeof(out->version));
  182. out->battery_count = p.battery_count;
  183. for (i = 0; i < SYS_POWER_BATT_MAX; i++) {
  184. struct sys_battery *d = &out->battery[i];
  185. const struct power_battery *s = &p.battery[i];
  186. d->present = s->present ? 1u : 0u;
  187. d->error = s->error ? 1u : 0u;
  188. d->status = s->status;
  189. d->charging = s->charging ? 1u : 0u;
  190. d->discharging = s->discharging ? 1u : 0u;
  191. d->percent = s->percent > 0 ? (unsigned int)s->percent : 0u;
  192. d->percent_valid = s->percent_valid ? 1u : 0u;
  193. d->remaining = s->remaining;
  194. d->full = s->full;
  195. d->design = s->design;
  196. d->voltage_mv = s->voltage_mv;
  197. d->rate = s->rate;
  198. d->unit_known = s->unit_known ? 1u : 0u;
  199. d->unit_mwh = s->unit_mwh ? 1u : 0u;
  200. d->cycle_count = s->cycle_count;
  201. copy_str(d->manufacturer, s->manufacturer,
  202. sizeof(d->manufacturer));
  203. copy_str(d->model, s->model, sizeof(d->model));
  204. copy_str(d->serial, s->serial, sizeof(d->serial));
  205. }
  206. return 0;
  207. }
  208. int sys_cros_sensors(struct sys_cros_sensors *out)
  209. {
  210. struct cros_ec_sensors info;
  211. unsigned int i;
  212. if (!out)
  213. return -1;
  214. cros_ec_sensors_read(&info);
  215. out->present = info.present ? 1u : 0u;
  216. out->acpi_version = info.acpi_version;
  217. out->battery_version = info.battery_version;
  218. out->thermal_version = info.thermal_version;
  219. for (i = 0; i < SYS_CROS_TEMP_MAX; i++)
  220. out->temp_c[i] = info.temp_c[i];
  221. for (i = 0; i < SYS_CROS_FAN_MAX; i++)
  222. out->fan_rpm[i] = info.fan_rpm[i];
  223. out->fan_present = info.fan_present;
  224. out->fan_duty = info.fan_duty;
  225. out->switches = info.switches;
  226. return 0;
  227. }