acpi_power_meter.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * A hwmon driver for ACPI 4.0 power meters
  4. * Copyright (C) 2009 IBM
  5. *
  6. * Author: Darrick J. Wong <darrick.wong@oracle.com>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/hwmon.h>
  10. #include <linux/hwmon-sysfs.h>
  11. #include <linux/jiffies.h>
  12. #include <linux/mutex.h>
  13. #include <linux/dmi.h>
  14. #include <linux/slab.h>
  15. #include <linux/kdev_t.h>
  16. #include <linux/sched.h>
  17. #include <linux/time.h>
  18. #include <linux/err.h>
  19. #include <linux/acpi.h>
  20. #define ACPI_POWER_METER_NAME "power_meter"
  21. #define ACPI_POWER_METER_DEVICE_NAME "Power Meter"
  22. #define ACPI_POWER_METER_CLASS "pwr_meter_resource"
  23. #define NUM_SENSORS 17
  24. #define POWER_METER_CAN_MEASURE (1 << 0)
  25. #define POWER_METER_CAN_TRIP (1 << 1)
  26. #define POWER_METER_CAN_CAP (1 << 2)
  27. #define POWER_METER_CAN_NOTIFY (1 << 3)
  28. #define POWER_METER_IS_BATTERY (1 << 8)
  29. #define UNKNOWN_HYSTERESIS 0xFFFFFFFF
  30. #define UNKNOWN_POWER 0xFFFFFFFF
  31. #define METER_NOTIFY_CONFIG 0x80
  32. #define METER_NOTIFY_TRIP 0x81
  33. #define METER_NOTIFY_CAP 0x82
  34. #define METER_NOTIFY_CAPPING 0x83
  35. #define METER_NOTIFY_INTERVAL 0x84
  36. #define POWER_AVERAGE_NAME "power1_average"
  37. #define POWER_CAP_NAME "power1_cap"
  38. #define POWER_AVG_INTERVAL_NAME "power1_average_interval"
  39. #define POWER_ALARM_NAME "power1_alarm"
  40. static int cap_in_hardware;
  41. static bool force_cap_on;
  42. static DEFINE_MUTEX(acpi_notify_lock);
  43. static int can_cap_in_hardware(void)
  44. {
  45. return force_cap_on || cap_in_hardware;
  46. }
  47. static const struct acpi_device_id power_meter_ids[] = {
  48. {"ACPI000D", 0},
  49. {"", 0},
  50. };
  51. MODULE_DEVICE_TABLE(acpi, power_meter_ids);
  52. struct acpi_power_meter_capabilities {
  53. u64 flags;
  54. u64 units;
  55. u64 type;
  56. u64 accuracy;
  57. u64 sampling_time;
  58. u64 min_avg_interval;
  59. u64 max_avg_interval;
  60. u64 hysteresis;
  61. u64 configurable_cap;
  62. u64 min_cap;
  63. u64 max_cap;
  64. };
  65. struct acpi_power_meter_resource {
  66. struct acpi_device *acpi_dev;
  67. acpi_bus_id name;
  68. struct mutex lock;
  69. struct device *hwmon_dev;
  70. struct acpi_power_meter_capabilities caps;
  71. acpi_string model_number;
  72. acpi_string serial_number;
  73. acpi_string oem_info;
  74. u64 power;
  75. u64 cap;
  76. u64 avg_interval;
  77. bool power_alarm;
  78. int sensors_valid;
  79. unsigned long sensors_last_updated;
  80. #define POWER_METER_TRIP_AVERAGE_MIN_IDX 0
  81. #define POWER_METER_TRIP_AVERAGE_MAX_IDX 1
  82. s64 trip[2];
  83. int num_domain_devices;
  84. struct acpi_device **domain_devices;
  85. struct kobject *holders_dir;
  86. };
  87. /* Averaging interval */
  88. static int update_avg_interval(struct acpi_power_meter_resource *resource)
  89. {
  90. unsigned long long data;
  91. acpi_status status;
  92. status = acpi_evaluate_integer(resource->acpi_dev->handle, "_GAI",
  93. NULL, &data);
  94. if (ACPI_FAILURE(status)) {
  95. acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_GAI",
  96. status);
  97. return -ENODEV;
  98. }
  99. resource->avg_interval = data;
  100. return 0;
  101. }
  102. /* Cap functions */
  103. static int update_cap(struct acpi_power_meter_resource *resource)
  104. {
  105. unsigned long long data;
  106. acpi_status status;
  107. status = acpi_evaluate_integer(resource->acpi_dev->handle, "_GHL",
  108. NULL, &data);
  109. if (ACPI_FAILURE(status)) {
  110. acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_GHL",
  111. status);
  112. return -ENODEV;
  113. }
  114. resource->cap = data;
  115. return 0;
  116. }
  117. /* Power meter trip points */
  118. static int set_acpi_trip(struct acpi_power_meter_resource *resource)
  119. {
  120. union acpi_object arg_objs[] = {
  121. {ACPI_TYPE_INTEGER},
  122. {ACPI_TYPE_INTEGER}
  123. };
  124. struct acpi_object_list args = { 2, arg_objs };
  125. unsigned long long data;
  126. acpi_status status;
  127. /* Both trip levels must be set */
  128. if (resource->trip[0] < 0 || resource->trip[1] < 0)
  129. return 0;
  130. /* This driver stores min, max; ACPI wants max, min. */
  131. arg_objs[0].integer.value = resource->trip[1];
  132. arg_objs[1].integer.value = resource->trip[0];
  133. status = acpi_evaluate_integer(resource->acpi_dev->handle, "_PTP",
  134. &args, &data);
  135. if (ACPI_FAILURE(status)) {
  136. acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_PTP",
  137. status);
  138. return -EINVAL;
  139. }
  140. /* _PTP returns 0 on success, nonzero otherwise */
  141. if (data)
  142. return -EINVAL;
  143. return 0;
  144. }
  145. /* Power meter */
  146. static int update_meter(struct acpi_power_meter_resource *resource)
  147. {
  148. unsigned long long data;
  149. acpi_status status;
  150. unsigned long local_jiffies = jiffies;
  151. if (time_before(local_jiffies, resource->sensors_last_updated +
  152. msecs_to_jiffies(resource->caps.sampling_time)) &&
  153. resource->sensors_valid)
  154. return 0;
  155. status = acpi_evaluate_integer(resource->acpi_dev->handle, "_PMM",
  156. NULL, &data);
  157. if (ACPI_FAILURE(status)) {
  158. acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_PMM",
  159. status);
  160. return -ENODEV;
  161. }
  162. resource->power = data;
  163. resource->sensors_valid = 1;
  164. resource->sensors_last_updated = jiffies;
  165. return 0;
  166. }
  167. /* Read power domain data */
  168. static void remove_domain_devices(struct acpi_power_meter_resource *resource)
  169. {
  170. int i;
  171. if (!resource->num_domain_devices)
  172. return;
  173. for (i = 0; i < resource->num_domain_devices; i++) {
  174. struct acpi_device *obj = resource->domain_devices[i];
  175. if (!obj)
  176. continue;
  177. sysfs_remove_link(resource->holders_dir,
  178. kobject_name(&obj->dev.kobj));
  179. acpi_dev_put(obj);
  180. }
  181. kfree(resource->domain_devices);
  182. kobject_put(resource->holders_dir);
  183. resource->num_domain_devices = 0;
  184. }
  185. static int read_domain_devices(struct acpi_power_meter_resource *resource)
  186. {
  187. int res = 0;
  188. int i;
  189. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  190. union acpi_object *pss;
  191. acpi_status status;
  192. status = acpi_evaluate_object(resource->acpi_dev->handle, "_PMD", NULL,
  193. &buffer);
  194. if (ACPI_FAILURE(status)) {
  195. acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_PMD",
  196. status);
  197. return -ENODEV;
  198. }
  199. pss = buffer.pointer;
  200. if (!pss ||
  201. pss->type != ACPI_TYPE_PACKAGE) {
  202. dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME
  203. "Invalid _PMD data\n");
  204. res = -EFAULT;
  205. goto end;
  206. }
  207. if (!pss->package.count)
  208. goto end;
  209. resource->domain_devices = kzalloc_objs(struct acpi_device *,
  210. pss->package.count);
  211. if (!resource->domain_devices) {
  212. res = -ENOMEM;
  213. goto end;
  214. }
  215. resource->holders_dir = kobject_create_and_add("measures",
  216. &resource->acpi_dev->dev.kobj);
  217. if (!resource->holders_dir) {
  218. res = -ENOMEM;
  219. goto exit_free;
  220. }
  221. resource->num_domain_devices = pss->package.count;
  222. for (i = 0; i < pss->package.count; i++) {
  223. struct acpi_device *obj;
  224. union acpi_object *element = &pss->package.elements[i];
  225. /* Refuse non-references */
  226. if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
  227. continue;
  228. /* Create a symlink to domain objects */
  229. obj = acpi_get_acpi_dev(element->reference.handle);
  230. resource->domain_devices[i] = obj;
  231. if (!obj)
  232. continue;
  233. res = sysfs_create_link(resource->holders_dir, &obj->dev.kobj,
  234. kobject_name(&obj->dev.kobj));
  235. if (res) {
  236. acpi_dev_put(obj);
  237. resource->domain_devices[i] = NULL;
  238. }
  239. }
  240. res = 0;
  241. goto end;
  242. exit_free:
  243. kfree(resource->domain_devices);
  244. end:
  245. kfree(buffer.pointer);
  246. return res;
  247. }
  248. static int set_trip(struct acpi_power_meter_resource *resource, u16 trip_idx,
  249. unsigned long trip)
  250. {
  251. unsigned long trip_bk;
  252. int ret;
  253. trip = DIV_ROUND_CLOSEST(trip, 1000);
  254. trip_bk = resource->trip[trip_idx];
  255. resource->trip[trip_idx] = trip;
  256. ret = set_acpi_trip(resource);
  257. if (ret) {
  258. dev_err(&resource->acpi_dev->dev, "set %s failed.\n",
  259. (trip_idx == POWER_METER_TRIP_AVERAGE_MIN_IDX) ?
  260. "power1_average_min" : "power1_average_max");
  261. resource->trip[trip_idx] = trip_bk;
  262. }
  263. return ret;
  264. }
  265. static int set_cap(struct acpi_power_meter_resource *resource,
  266. unsigned long cap)
  267. {
  268. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  269. struct acpi_object_list args = { 1, &arg0 };
  270. unsigned long long data;
  271. acpi_status status;
  272. cap = DIV_ROUND_CLOSEST(cap, 1000);
  273. if (cap > resource->caps.max_cap || cap < resource->caps.min_cap)
  274. return -EINVAL;
  275. arg0.integer.value = cap;
  276. status = acpi_evaluate_integer(resource->acpi_dev->handle, "_SHL",
  277. &args, &data);
  278. if (ACPI_FAILURE(status)) {
  279. acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_SHL",
  280. status);
  281. return -EINVAL;
  282. }
  283. resource->cap = cap;
  284. /* _SHL returns 0 on success, nonzero otherwise */
  285. if (data)
  286. return -EINVAL;
  287. return 0;
  288. }
  289. static int set_avg_interval(struct acpi_power_meter_resource *resource,
  290. unsigned long val)
  291. {
  292. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  293. struct acpi_object_list args = { 1, &arg0 };
  294. unsigned long long data;
  295. acpi_status status;
  296. if (val > resource->caps.max_avg_interval ||
  297. val < resource->caps.min_avg_interval)
  298. return -EINVAL;
  299. arg0.integer.value = val;
  300. status = acpi_evaluate_integer(resource->acpi_dev->handle, "_PAI",
  301. &args, &data);
  302. if (ACPI_FAILURE(status)) {
  303. acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_PAI",
  304. status);
  305. return -EINVAL;
  306. }
  307. resource->avg_interval = val;
  308. /* _PAI returns 0 on success, nonzero otherwise */
  309. if (data)
  310. return -EINVAL;
  311. return 0;
  312. }
  313. static int get_power_alarm_state(struct acpi_power_meter_resource *resource,
  314. long *val)
  315. {
  316. int ret;
  317. ret = update_meter(resource);
  318. if (ret)
  319. return ret;
  320. /* need to update cap if not to support the notification. */
  321. if (!(resource->caps.flags & POWER_METER_CAN_NOTIFY)) {
  322. ret = update_cap(resource);
  323. if (ret)
  324. return ret;
  325. resource->power_alarm = resource->power > resource->cap;
  326. *val = resource->power_alarm;
  327. } else {
  328. *val = resource->power_alarm || resource->power > resource->cap;
  329. resource->power_alarm = resource->power > resource->cap;
  330. }
  331. return 0;
  332. }
  333. static umode_t power_meter_is_visible(const void *data,
  334. enum hwmon_sensor_types type,
  335. u32 attr, int channel)
  336. {
  337. const struct acpi_power_meter_resource *res = data;
  338. if (type != hwmon_power)
  339. return 0;
  340. switch (attr) {
  341. case hwmon_power_average:
  342. case hwmon_power_average_interval_min:
  343. case hwmon_power_average_interval_max:
  344. if (res->caps.flags & POWER_METER_CAN_MEASURE)
  345. return 0444;
  346. break;
  347. case hwmon_power_average_interval:
  348. if (res->caps.flags & POWER_METER_CAN_MEASURE)
  349. return 0644;
  350. break;
  351. case hwmon_power_cap_min:
  352. case hwmon_power_cap_max:
  353. case hwmon_power_alarm:
  354. if (res->caps.flags & POWER_METER_CAN_CAP && can_cap_in_hardware())
  355. return 0444;
  356. break;
  357. case hwmon_power_cap:
  358. if (res->caps.flags & POWER_METER_CAN_CAP && can_cap_in_hardware()) {
  359. if (res->caps.configurable_cap)
  360. return 0644;
  361. else
  362. return 0444;
  363. }
  364. break;
  365. default:
  366. break;
  367. }
  368. return 0;
  369. }
  370. static int power_meter_read(struct device *dev, enum hwmon_sensor_types type,
  371. u32 attr, int channel, long *val)
  372. {
  373. struct acpi_power_meter_resource *res = dev_get_drvdata(dev);
  374. int ret = 0;
  375. if (type != hwmon_power)
  376. return -EINVAL;
  377. guard(mutex)(&res->lock);
  378. switch (attr) {
  379. case hwmon_power_average:
  380. ret = update_meter(res);
  381. if (ret)
  382. return ret;
  383. if (res->power == UNKNOWN_POWER)
  384. return -ENODATA;
  385. *val = res->power * 1000;
  386. break;
  387. case hwmon_power_average_interval_min:
  388. *val = res->caps.min_avg_interval;
  389. break;
  390. case hwmon_power_average_interval_max:
  391. *val = res->caps.max_avg_interval;
  392. break;
  393. case hwmon_power_average_interval:
  394. ret = update_avg_interval(res);
  395. if (ret)
  396. return ret;
  397. *val = (res)->avg_interval;
  398. break;
  399. case hwmon_power_cap_min:
  400. *val = res->caps.min_cap * 1000;
  401. break;
  402. case hwmon_power_cap_max:
  403. *val = res->caps.max_cap * 1000;
  404. break;
  405. case hwmon_power_alarm:
  406. ret = get_power_alarm_state(res, val);
  407. if (ret)
  408. return ret;
  409. break;
  410. case hwmon_power_cap:
  411. ret = update_cap(res);
  412. if (ret)
  413. return ret;
  414. *val = res->cap * 1000;
  415. break;
  416. default:
  417. break;
  418. }
  419. return 0;
  420. }
  421. static int power_meter_write(struct device *dev, enum hwmon_sensor_types type,
  422. u32 attr, int channel, long val)
  423. {
  424. struct acpi_power_meter_resource *res = dev_get_drvdata(dev);
  425. int ret;
  426. if (type != hwmon_power)
  427. return -EINVAL;
  428. guard(mutex)(&res->lock);
  429. switch (attr) {
  430. case hwmon_power_cap:
  431. ret = set_cap(res, val);
  432. break;
  433. case hwmon_power_average_interval:
  434. ret = set_avg_interval(res, val);
  435. break;
  436. default:
  437. ret = -EOPNOTSUPP;
  438. }
  439. return ret;
  440. }
  441. static const struct hwmon_channel_info * const power_meter_info[] = {
  442. HWMON_CHANNEL_INFO(power, HWMON_P_AVERAGE |
  443. HWMON_P_AVERAGE_INTERVAL | HWMON_P_AVERAGE_INTERVAL_MIN |
  444. HWMON_P_AVERAGE_INTERVAL_MAX | HWMON_P_CAP | HWMON_P_CAP_MIN |
  445. HWMON_P_CAP_MAX | HWMON_P_ALARM),
  446. NULL
  447. };
  448. static const struct hwmon_ops power_meter_ops = {
  449. .is_visible = power_meter_is_visible,
  450. .read = power_meter_read,
  451. .write = power_meter_write,
  452. };
  453. static const struct hwmon_chip_info power_meter_chip_info = {
  454. .ops = &power_meter_ops,
  455. .info = power_meter_info,
  456. };
  457. static ssize_t power1_average_max_store(struct device *dev,
  458. struct device_attribute *attr,
  459. const char *buf, size_t count)
  460. {
  461. struct acpi_power_meter_resource *res = dev_get_drvdata(dev);
  462. unsigned long trip;
  463. int ret;
  464. ret = kstrtoul(buf, 10, &trip);
  465. if (ret)
  466. return ret;
  467. mutex_lock(&res->lock);
  468. ret = set_trip(res, POWER_METER_TRIP_AVERAGE_MAX_IDX, trip);
  469. mutex_unlock(&res->lock);
  470. return ret == 0 ? count : ret;
  471. }
  472. static ssize_t power1_average_min_store(struct device *dev,
  473. struct device_attribute *attr,
  474. const char *buf, size_t count)
  475. {
  476. struct acpi_power_meter_resource *res = dev_get_drvdata(dev);
  477. unsigned long trip;
  478. int ret;
  479. ret = kstrtoul(buf, 10, &trip);
  480. if (ret)
  481. return ret;
  482. mutex_lock(&res->lock);
  483. ret = set_trip(res, POWER_METER_TRIP_AVERAGE_MIN_IDX, trip);
  484. mutex_unlock(&res->lock);
  485. return ret == 0 ? count : ret;
  486. }
  487. static ssize_t power1_average_min_show(struct device *dev,
  488. struct device_attribute *attr, char *buf)
  489. {
  490. struct acpi_power_meter_resource *res = dev_get_drvdata(dev);
  491. if (res->trip[POWER_METER_TRIP_AVERAGE_MIN_IDX] < 0)
  492. return sysfs_emit(buf, "unknown\n");
  493. return sysfs_emit(buf, "%lld\n",
  494. res->trip[POWER_METER_TRIP_AVERAGE_MIN_IDX] * 1000);
  495. }
  496. static ssize_t power1_average_max_show(struct device *dev,
  497. struct device_attribute *attr, char *buf)
  498. {
  499. struct acpi_power_meter_resource *res = dev_get_drvdata(dev);
  500. if (res->trip[POWER_METER_TRIP_AVERAGE_MAX_IDX] < 0)
  501. return sysfs_emit(buf, "unknown\n");
  502. return sysfs_emit(buf, "%lld\n",
  503. res->trip[POWER_METER_TRIP_AVERAGE_MAX_IDX] * 1000);
  504. }
  505. static ssize_t power1_cap_hyst_show(struct device *dev,
  506. struct device_attribute *attr, char *buf)
  507. {
  508. struct acpi_power_meter_resource *res = dev_get_drvdata(dev);
  509. if (res->caps.hysteresis == UNKNOWN_HYSTERESIS)
  510. return sysfs_emit(buf, "unknown\n");
  511. return sysfs_emit(buf, "%llu\n", res->caps.hysteresis * 1000);
  512. }
  513. static ssize_t power1_accuracy_show(struct device *dev,
  514. struct device_attribute *attr,
  515. char *buf)
  516. {
  517. struct acpi_power_meter_resource *res = dev_get_drvdata(dev);
  518. unsigned int acc = res->caps.accuracy;
  519. return sysfs_emit(buf, "%u.%u%%\n", acc / 1000, acc % 1000);
  520. }
  521. static ssize_t power1_is_battery_show(struct device *dev,
  522. struct device_attribute *attr,
  523. char *buf)
  524. {
  525. struct acpi_power_meter_resource *res = dev_get_drvdata(dev);
  526. return sysfs_emit(buf, "%u\n",
  527. res->caps.flags & POWER_METER_IS_BATTERY ? 1 : 0);
  528. }
  529. static ssize_t power1_model_number_show(struct device *dev,
  530. struct device_attribute *attr,
  531. char *buf)
  532. {
  533. struct acpi_power_meter_resource *res = dev_get_drvdata(dev);
  534. return sysfs_emit(buf, "%s\n", res->model_number);
  535. }
  536. static ssize_t power1_oem_info_show(struct device *dev,
  537. struct device_attribute *attr,
  538. char *buf)
  539. {
  540. struct acpi_power_meter_resource *res = dev_get_drvdata(dev);
  541. return sysfs_emit(buf, "%s\n", res->oem_info);
  542. }
  543. static ssize_t power1_serial_number_show(struct device *dev,
  544. struct device_attribute *attr,
  545. char *buf)
  546. {
  547. struct acpi_power_meter_resource *res = dev_get_drvdata(dev);
  548. return sysfs_emit(buf, "%s\n", res->serial_number);
  549. }
  550. /* depend on POWER_METER_CAN_TRIP */
  551. static DEVICE_ATTR_RW(power1_average_max);
  552. static DEVICE_ATTR_RW(power1_average_min);
  553. /* depend on POWER_METER_CAN_CAP */
  554. static DEVICE_ATTR_RO(power1_cap_hyst);
  555. /* depend on POWER_METER_CAN_MEASURE */
  556. static DEVICE_ATTR_RO(power1_accuracy);
  557. static DEVICE_ATTR_RO(power1_is_battery);
  558. static DEVICE_ATTR_RO(power1_model_number);
  559. static DEVICE_ATTR_RO(power1_oem_info);
  560. static DEVICE_ATTR_RO(power1_serial_number);
  561. static umode_t power_extra_is_visible(struct kobject *kobj,
  562. struct attribute *attr, int idx)
  563. {
  564. struct device *dev = kobj_to_dev(kobj);
  565. struct acpi_power_meter_resource *res = dev_get_drvdata(dev);
  566. if (attr == &dev_attr_power1_is_battery.attr ||
  567. attr == &dev_attr_power1_accuracy.attr) {
  568. if ((res->caps.flags & POWER_METER_CAN_MEASURE) == 0)
  569. return 0;
  570. }
  571. if (attr == &dev_attr_power1_cap_hyst.attr) {
  572. if ((res->caps.flags & POWER_METER_CAN_CAP) == 0) {
  573. return 0;
  574. } else if (!can_cap_in_hardware()) {
  575. dev_warn(&res->acpi_dev->dev,
  576. "Ignoring unsafe software power cap!\n");
  577. return 0;
  578. }
  579. }
  580. if (attr == &dev_attr_power1_average_max.attr ||
  581. attr == &dev_attr_power1_average_min.attr) {
  582. if ((res->caps.flags & POWER_METER_CAN_TRIP) == 0)
  583. return 0;
  584. }
  585. return attr->mode;
  586. }
  587. static struct attribute *power_extra_attrs[] = {
  588. &dev_attr_power1_average_max.attr,
  589. &dev_attr_power1_average_min.attr,
  590. &dev_attr_power1_cap_hyst.attr,
  591. &dev_attr_power1_accuracy.attr,
  592. &dev_attr_power1_is_battery.attr,
  593. &dev_attr_power1_model_number.attr,
  594. &dev_attr_power1_oem_info.attr,
  595. &dev_attr_power1_serial_number.attr,
  596. NULL
  597. };
  598. static const struct attribute_group power_extra_group = {
  599. .attrs = power_extra_attrs,
  600. .is_visible = power_extra_is_visible,
  601. };
  602. __ATTRIBUTE_GROUPS(power_extra);
  603. static void free_capabilities(struct acpi_power_meter_resource *resource)
  604. {
  605. acpi_string *str;
  606. int i;
  607. str = &resource->model_number;
  608. for (i = 0; i < 3; i++, str++) {
  609. kfree(*str);
  610. *str = NULL;
  611. }
  612. }
  613. static int read_capabilities(struct acpi_power_meter_resource *resource)
  614. {
  615. int res = 0;
  616. int i;
  617. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  618. struct acpi_buffer state = { 0, NULL };
  619. struct acpi_buffer format = { sizeof("NNNNNNNNNNN"), "NNNNNNNNNNN" };
  620. union acpi_object *pss;
  621. acpi_string *str;
  622. acpi_status status;
  623. status = acpi_evaluate_object(resource->acpi_dev->handle, "_PMC", NULL,
  624. &buffer);
  625. if (ACPI_FAILURE(status)) {
  626. acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_PMC",
  627. status);
  628. return -ENODEV;
  629. }
  630. pss = buffer.pointer;
  631. if (!pss ||
  632. pss->type != ACPI_TYPE_PACKAGE ||
  633. pss->package.count != 14) {
  634. dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME
  635. "Invalid _PMC data\n");
  636. res = -EFAULT;
  637. goto end;
  638. }
  639. /* Grab all the integer data at once */
  640. state.length = sizeof(struct acpi_power_meter_capabilities);
  641. state.pointer = &resource->caps;
  642. status = acpi_extract_package(pss, &format, &state);
  643. if (ACPI_FAILURE(status)) {
  644. dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME
  645. "_PMC package parsing failed: %s\n",
  646. acpi_format_exception(status));
  647. res = -EFAULT;
  648. goto end;
  649. }
  650. if (resource->caps.units) {
  651. dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME
  652. "Unknown units %llu.\n",
  653. resource->caps.units);
  654. res = -EINVAL;
  655. goto end;
  656. }
  657. /* Grab the string data */
  658. str = &resource->model_number;
  659. for (i = 11; i < 14; i++) {
  660. union acpi_object *element = &pss->package.elements[i];
  661. if (element->type != ACPI_TYPE_STRING) {
  662. res = -EINVAL;
  663. goto error;
  664. }
  665. *str = kmemdup_nul(element->string.pointer, element->string.length,
  666. GFP_KERNEL);
  667. if (!*str) {
  668. res = -ENOMEM;
  669. goto error;
  670. }
  671. str++;
  672. }
  673. dev_info(&resource->acpi_dev->dev, "Found ACPI power meter.\n");
  674. goto end;
  675. error:
  676. free_capabilities(resource);
  677. end:
  678. kfree(buffer.pointer);
  679. return res;
  680. }
  681. /* Handle ACPI event notifications */
  682. static void acpi_power_meter_notify(struct acpi_device *device, u32 event)
  683. {
  684. struct acpi_power_meter_resource *resource;
  685. int res;
  686. if (!device || !acpi_driver_data(device))
  687. return;
  688. resource = acpi_driver_data(device);
  689. guard(mutex)(&acpi_notify_lock);
  690. switch (event) {
  691. case METER_NOTIFY_CONFIG:
  692. if (!IS_ERR(resource->hwmon_dev))
  693. hwmon_device_unregister(resource->hwmon_dev);
  694. mutex_lock(&resource->lock);
  695. free_capabilities(resource);
  696. remove_domain_devices(resource);
  697. res = read_capabilities(resource);
  698. if (res)
  699. dev_err_once(&device->dev, "read capabilities failed.\n");
  700. res = read_domain_devices(resource);
  701. if (res && res != -ENODEV)
  702. dev_err_once(&device->dev, "read domain devices failed.\n");
  703. mutex_unlock(&resource->lock);
  704. resource->hwmon_dev =
  705. hwmon_device_register_with_info(&device->dev,
  706. ACPI_POWER_METER_NAME,
  707. resource,
  708. &power_meter_chip_info,
  709. power_extra_groups);
  710. if (IS_ERR(resource->hwmon_dev))
  711. dev_err_once(&device->dev, "register hwmon device failed.\n");
  712. break;
  713. case METER_NOTIFY_TRIP:
  714. sysfs_notify(&device->dev.kobj, NULL, POWER_AVERAGE_NAME);
  715. break;
  716. case METER_NOTIFY_CAP:
  717. mutex_lock(&resource->lock);
  718. res = update_cap(resource);
  719. if (res)
  720. dev_err_once(&device->dev, "update cap failed when capping value is changed.\n");
  721. mutex_unlock(&resource->lock);
  722. sysfs_notify(&device->dev.kobj, NULL, POWER_CAP_NAME);
  723. break;
  724. case METER_NOTIFY_INTERVAL:
  725. sysfs_notify(&device->dev.kobj, NULL, POWER_AVG_INTERVAL_NAME);
  726. break;
  727. case METER_NOTIFY_CAPPING:
  728. mutex_lock(&resource->lock);
  729. resource->power_alarm = true;
  730. mutex_unlock(&resource->lock);
  731. sysfs_notify(&device->dev.kobj, NULL, POWER_ALARM_NAME);
  732. dev_info(&device->dev, "Capping in progress.\n");
  733. break;
  734. default:
  735. WARN(1, "Unexpected event %d\n", event);
  736. break;
  737. }
  738. acpi_bus_generate_netlink_event(ACPI_POWER_METER_CLASS,
  739. dev_name(&device->dev), event, 0);
  740. }
  741. static int acpi_power_meter_add(struct acpi_device *device)
  742. {
  743. int res;
  744. struct acpi_power_meter_resource *resource;
  745. if (!device)
  746. return -EINVAL;
  747. resource = kzalloc_obj(*resource);
  748. if (!resource)
  749. return -ENOMEM;
  750. resource->sensors_valid = 0;
  751. resource->acpi_dev = device;
  752. mutex_init(&resource->lock);
  753. strscpy(acpi_device_name(device), ACPI_POWER_METER_DEVICE_NAME);
  754. strscpy(acpi_device_class(device), ACPI_POWER_METER_CLASS);
  755. device->driver_data = resource;
  756. #if IS_REACHABLE(CONFIG_ACPI_IPMI)
  757. /*
  758. * On Dell systems several methods of acpi_power_meter access
  759. * variables in IPMI region, so wait until IPMI space handler is
  760. * installed by acpi_ipmi and also wait until SMI is selected to make
  761. * the space handler fully functional.
  762. */
  763. if (dmi_match(DMI_SYS_VENDOR, "Dell Inc.")) {
  764. struct acpi_device *ipi_device = acpi_dev_get_first_match_dev("IPI0001", NULL, -1);
  765. if (ipi_device && acpi_wait_for_acpi_ipmi())
  766. dev_warn(&device->dev, "Waiting for ACPI IPMI timeout");
  767. acpi_dev_put(ipi_device);
  768. }
  769. #endif
  770. res = read_capabilities(resource);
  771. if (res)
  772. goto exit_free;
  773. resource->trip[0] = -1;
  774. resource->trip[1] = -1;
  775. /* _PMD method is optional. */
  776. res = read_domain_devices(resource);
  777. if (res && res != -ENODEV)
  778. goto exit_free_capability;
  779. resource->hwmon_dev =
  780. hwmon_device_register_with_info(&device->dev,
  781. ACPI_POWER_METER_NAME, resource,
  782. &power_meter_chip_info,
  783. power_extra_groups);
  784. if (IS_ERR(resource->hwmon_dev)) {
  785. res = PTR_ERR(resource->hwmon_dev);
  786. goto exit_remove;
  787. }
  788. res = 0;
  789. goto exit;
  790. exit_remove:
  791. remove_domain_devices(resource);
  792. exit_free_capability:
  793. free_capabilities(resource);
  794. exit_free:
  795. kfree(resource);
  796. exit:
  797. return res;
  798. }
  799. static void acpi_power_meter_remove(struct acpi_device *device)
  800. {
  801. struct acpi_power_meter_resource *resource;
  802. if (!device || !acpi_driver_data(device))
  803. return;
  804. resource = acpi_driver_data(device);
  805. if (!IS_ERR(resource->hwmon_dev))
  806. hwmon_device_unregister(resource->hwmon_dev);
  807. remove_domain_devices(resource);
  808. free_capabilities(resource);
  809. kfree(resource);
  810. }
  811. static int acpi_power_meter_resume(struct device *dev)
  812. {
  813. struct acpi_power_meter_resource *resource;
  814. if (!dev)
  815. return -EINVAL;
  816. resource = acpi_driver_data(to_acpi_device(dev));
  817. if (!resource)
  818. return -EINVAL;
  819. free_capabilities(resource);
  820. read_capabilities(resource);
  821. return 0;
  822. }
  823. static DEFINE_SIMPLE_DEV_PM_OPS(acpi_power_meter_pm, NULL,
  824. acpi_power_meter_resume);
  825. static struct acpi_driver acpi_power_meter_driver = {
  826. .name = "power_meter",
  827. .class = ACPI_POWER_METER_CLASS,
  828. .ids = power_meter_ids,
  829. .ops = {
  830. .add = acpi_power_meter_add,
  831. .remove = acpi_power_meter_remove,
  832. .notify = acpi_power_meter_notify,
  833. },
  834. .drv.pm = pm_sleep_ptr(&acpi_power_meter_pm),
  835. };
  836. /* Module init/exit routines */
  837. static int __init enable_cap_knobs(const struct dmi_system_id *d)
  838. {
  839. cap_in_hardware = 1;
  840. return 0;
  841. }
  842. static const struct dmi_system_id pm_dmi_table[] __initconst = {
  843. {
  844. enable_cap_knobs, "IBM Active Energy Manager",
  845. {
  846. DMI_MATCH(DMI_SYS_VENDOR, "IBM")
  847. },
  848. },
  849. {}
  850. };
  851. static int __init acpi_power_meter_init(void)
  852. {
  853. int result;
  854. if (acpi_disabled)
  855. return -ENODEV;
  856. dmi_check_system(pm_dmi_table);
  857. result = acpi_bus_register_driver(&acpi_power_meter_driver);
  858. if (result < 0)
  859. return result;
  860. return 0;
  861. }
  862. static void __exit acpi_power_meter_exit(void)
  863. {
  864. acpi_bus_unregister_driver(&acpi_power_meter_driver);
  865. }
  866. MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
  867. MODULE_DESCRIPTION("ACPI 4.0 power meter driver");
  868. MODULE_LICENSE("GPL");
  869. module_param(force_cap_on, bool, 0644);
  870. MODULE_PARM_DESC(force_cap_on, "Enable power cap even it is unsafe to do so.");
  871. module_init(acpi_power_meter_init);
  872. module_exit(acpi_power_meter_exit);