sys_info.c 6.9 KB

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