acpi_power_meter.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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 <[email protected]>
  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 int can_cap_in_hardware(void)
  43. {
  44. return force_cap_on || cap_in_hardware;
  45. }
  46. static const struct acpi_device_id power_meter_ids[] = {
  47. {"ACPI000D", 0},
  48. {"", 0},
  49. };
  50. MODULE_DEVICE_TABLE(acpi, power_meter_ids);
  51. struct acpi_power_meter_capabilities {
  52. u64 flags;
  53. u64 units;
  54. u64 type;
  55. u64 accuracy;
  56. u64 sampling_time;
  57. u64 min_avg_interval;
  58. u64 max_avg_interval;
  59. u64 hysteresis;
  60. u64 configurable_cap;
  61. u64 min_cap;
  62. u64 max_cap;
  63. };
  64. struct acpi_power_meter_resource {
  65. struct acpi_device *acpi_dev;
  66. acpi_bus_id name;
  67. struct mutex lock;
  68. struct device *hwmon_dev;
  69. struct acpi_power_meter_capabilities caps;
  70. acpi_string model_number;
  71. acpi_string serial_number;
  72. acpi_string oem_info;
  73. u64 power;
  74. u64 cap;
  75. u64 avg_interval;
  76. int sensors_valid;
  77. unsigned long sensors_last_updated;
  78. struct sensor_device_attribute sensors[NUM_SENSORS];
  79. int num_sensors;
  80. s64 trip[2];
  81. int num_domain_devices;
  82. struct acpi_device **domain_devices;
  83. struct kobject *holders_dir;
  84. };
  85. struct sensor_template {
  86. char *label;
  87. ssize_t (*show)(struct device *dev,
  88. struct device_attribute *devattr,
  89. char *buf);
  90. ssize_t (*set)(struct device *dev,
  91. struct device_attribute *devattr,
  92. const char *buf, size_t count);
  93. int index;
  94. };
  95. /* Averaging interval */
  96. static int update_avg_interval(struct acpi_power_meter_resource *resource)
  97. {
  98. unsigned long long data;
  99. acpi_status status;
  100. status = acpi_evaluate_integer(resource->acpi_dev->handle, "_GAI",
  101. NULL, &data);
  102. if (ACPI_FAILURE(status)) {
  103. acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_GAI",
  104. status);
  105. return -ENODEV;
  106. }
  107. resource->avg_interval = data;
  108. return 0;
  109. }
  110. static ssize_t show_avg_interval(struct device *dev,
  111. struct device_attribute *devattr,
  112. char *buf)
  113. {
  114. struct acpi_device *acpi_dev = to_acpi_device(dev);
  115. struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
  116. mutex_lock(&resource->lock);
  117. update_avg_interval(resource);
  118. mutex_unlock(&resource->lock);
  119. return sprintf(buf, "%llu\n", resource->avg_interval);
  120. }
  121. static ssize_t set_avg_interval(struct device *dev,
  122. struct device_attribute *devattr,
  123. const char *buf, size_t count)
  124. {
  125. struct acpi_device *acpi_dev = to_acpi_device(dev);
  126. struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
  127. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  128. struct acpi_object_list args = { 1, &arg0 };
  129. int res;
  130. unsigned long temp;
  131. unsigned long long data;
  132. acpi_status status;
  133. res = kstrtoul(buf, 10, &temp);
  134. if (res)
  135. return res;
  136. if (temp > resource->caps.max_avg_interval ||
  137. temp < resource->caps.min_avg_interval)
  138. return -EINVAL;
  139. arg0.integer.value = temp;
  140. mutex_lock(&resource->lock);
  141. status = acpi_evaluate_integer(resource->acpi_dev->handle, "_PAI",
  142. &args, &data);
  143. if (ACPI_SUCCESS(status))
  144. resource->avg_interval = temp;
  145. mutex_unlock(&resource->lock);
  146. if (ACPI_FAILURE(status)) {
  147. acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_PAI",
  148. status);
  149. return -EINVAL;
  150. }
  151. /* _PAI returns 0 on success, nonzero otherwise */
  152. if (data)
  153. return -EINVAL;
  154. return count;
  155. }
  156. /* Cap functions */
  157. static int update_cap(struct acpi_power_meter_resource *resource)
  158. {
  159. unsigned long long data;
  160. acpi_status status;
  161. status = acpi_evaluate_integer(resource->acpi_dev->handle, "_GHL",
  162. NULL, &data);
  163. if (ACPI_FAILURE(status)) {
  164. acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_GHL",
  165. status);
  166. return -ENODEV;
  167. }
  168. resource->cap = data;
  169. return 0;
  170. }
  171. static ssize_t show_cap(struct device *dev,
  172. struct device_attribute *devattr,
  173. char *buf)
  174. {
  175. struct acpi_device *acpi_dev = to_acpi_device(dev);
  176. struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
  177. mutex_lock(&resource->lock);
  178. update_cap(resource);
  179. mutex_unlock(&resource->lock);
  180. return sprintf(buf, "%llu\n", resource->cap * 1000);
  181. }
  182. static ssize_t set_cap(struct device *dev, struct device_attribute *devattr,
  183. const char *buf, size_t count)
  184. {
  185. struct acpi_device *acpi_dev = to_acpi_device(dev);
  186. struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
  187. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  188. struct acpi_object_list args = { 1, &arg0 };
  189. int res;
  190. unsigned long temp;
  191. unsigned long long data;
  192. acpi_status status;
  193. res = kstrtoul(buf, 10, &temp);
  194. if (res)
  195. return res;
  196. temp = DIV_ROUND_CLOSEST(temp, 1000);
  197. if (temp > resource->caps.max_cap || temp < resource->caps.min_cap)
  198. return -EINVAL;
  199. arg0.integer.value = temp;
  200. mutex_lock(&resource->lock);
  201. status = acpi_evaluate_integer(resource->acpi_dev->handle, "_SHL",
  202. &args, &data);
  203. if (ACPI_SUCCESS(status))
  204. resource->cap = temp;
  205. mutex_unlock(&resource->lock);
  206. if (ACPI_FAILURE(status)) {
  207. acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_SHL",
  208. status);
  209. return -EINVAL;
  210. }
  211. /* _SHL returns 0 on success, nonzero otherwise */
  212. if (data)
  213. return -EINVAL;
  214. return count;
  215. }
  216. /* Power meter trip points */
  217. static int set_acpi_trip(struct acpi_power_meter_resource *resource)
  218. {
  219. union acpi_object arg_objs[] = {
  220. {ACPI_TYPE_INTEGER},
  221. {ACPI_TYPE_INTEGER}
  222. };
  223. struct acpi_object_list args = { 2, arg_objs };
  224. unsigned long long data;
  225. acpi_status status;
  226. /* Both trip levels must be set */
  227. if (resource->trip[0] < 0 || resource->trip[1] < 0)
  228. return 0;
  229. /* This driver stores min, max; ACPI wants max, min. */
  230. arg_objs[0].integer.value = resource->trip[1];
  231. arg_objs[1].integer.value = resource->trip[0];
  232. status = acpi_evaluate_integer(resource->acpi_dev->handle, "_PTP",
  233. &args, &data);
  234. if (ACPI_FAILURE(status)) {
  235. acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_PTP",
  236. status);
  237. return -EINVAL;
  238. }
  239. /* _PTP returns 0 on success, nonzero otherwise */
  240. if (data)
  241. return -EINVAL;
  242. return 0;
  243. }
  244. static ssize_t set_trip(struct device *dev, struct device_attribute *devattr,
  245. const char *buf, size_t count)
  246. {
  247. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  248. struct acpi_device *acpi_dev = to_acpi_device(dev);
  249. struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
  250. int res;
  251. unsigned long temp;
  252. res = kstrtoul(buf, 10, &temp);
  253. if (res)
  254. return res;
  255. temp = DIV_ROUND_CLOSEST(temp, 1000);
  256. mutex_lock(&resource->lock);
  257. resource->trip[attr->index - 7] = temp;
  258. res = set_acpi_trip(resource);
  259. mutex_unlock(&resource->lock);
  260. if (res)
  261. return res;
  262. return count;
  263. }
  264. /* Power meter */
  265. static int update_meter(struct acpi_power_meter_resource *resource)
  266. {
  267. unsigned long long data;
  268. acpi_status status;
  269. unsigned long local_jiffies = jiffies;
  270. if (time_before(local_jiffies, resource->sensors_last_updated +
  271. msecs_to_jiffies(resource->caps.sampling_time)) &&
  272. resource->sensors_valid)
  273. return 0;
  274. status = acpi_evaluate_integer(resource->acpi_dev->handle, "_PMM",
  275. NULL, &data);
  276. if (ACPI_FAILURE(status)) {
  277. acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_PMM",
  278. status);
  279. return -ENODEV;
  280. }
  281. resource->power = data;
  282. resource->sensors_valid = 1;
  283. resource->sensors_last_updated = jiffies;
  284. return 0;
  285. }
  286. static ssize_t show_power(struct device *dev,
  287. struct device_attribute *devattr,
  288. char *buf)
  289. {
  290. struct acpi_device *acpi_dev = to_acpi_device(dev);
  291. struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
  292. mutex_lock(&resource->lock);
  293. update_meter(resource);
  294. mutex_unlock(&resource->lock);
  295. if (resource->power == UNKNOWN_POWER)
  296. return -ENODATA;
  297. return sprintf(buf, "%llu\n", resource->power * 1000);
  298. }
  299. /* Miscellaneous */
  300. static ssize_t show_str(struct device *dev,
  301. struct device_attribute *devattr,
  302. char *buf)
  303. {
  304. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  305. struct acpi_device *acpi_dev = to_acpi_device(dev);
  306. struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
  307. acpi_string val;
  308. int ret;
  309. mutex_lock(&resource->lock);
  310. switch (attr->index) {
  311. case 0:
  312. val = resource->model_number;
  313. break;
  314. case 1:
  315. val = resource->serial_number;
  316. break;
  317. case 2:
  318. val = resource->oem_info;
  319. break;
  320. default:
  321. WARN(1, "Implementation error: unexpected attribute index %d\n",
  322. attr->index);
  323. val = "";
  324. break;
  325. }
  326. ret = sprintf(buf, "%s\n", val);
  327. mutex_unlock(&resource->lock);
  328. return ret;
  329. }
  330. static ssize_t show_val(struct device *dev,
  331. struct device_attribute *devattr,
  332. char *buf)
  333. {
  334. struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
  335. struct acpi_device *acpi_dev = to_acpi_device(dev);
  336. struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
  337. u64 val = 0;
  338. switch (attr->index) {
  339. case 0:
  340. val = resource->caps.min_avg_interval;
  341. break;
  342. case 1:
  343. val = resource->caps.max_avg_interval;
  344. break;
  345. case 2:
  346. val = resource->caps.min_cap * 1000;
  347. break;
  348. case 3:
  349. val = resource->caps.max_cap * 1000;
  350. break;
  351. case 4:
  352. if (resource->caps.hysteresis == UNKNOWN_HYSTERESIS)
  353. return sprintf(buf, "unknown\n");
  354. val = resource->caps.hysteresis * 1000;
  355. break;
  356. case 5:
  357. if (resource->caps.flags & POWER_METER_IS_BATTERY)
  358. val = 1;
  359. else
  360. val = 0;
  361. break;
  362. case 6:
  363. if (resource->power > resource->cap)
  364. val = 1;
  365. else
  366. val = 0;
  367. break;
  368. case 7:
  369. case 8:
  370. if (resource->trip[attr->index - 7] < 0)
  371. return sprintf(buf, "unknown\n");
  372. val = resource->trip[attr->index - 7] * 1000;
  373. break;
  374. default:
  375. WARN(1, "Implementation error: unexpected attribute index %d\n",
  376. attr->index);
  377. break;
  378. }
  379. return sprintf(buf, "%llu\n", val);
  380. }
  381. static ssize_t show_accuracy(struct device *dev,
  382. struct device_attribute *devattr,
  383. char *buf)
  384. {
  385. struct acpi_device *acpi_dev = to_acpi_device(dev);
  386. struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
  387. unsigned int acc = resource->caps.accuracy;
  388. return sprintf(buf, "%u.%u%%\n", acc / 1000, acc % 1000);
  389. }
  390. static ssize_t show_name(struct device *dev,
  391. struct device_attribute *devattr,
  392. char *buf)
  393. {
  394. return sprintf(buf, "%s\n", ACPI_POWER_METER_NAME);
  395. }
  396. #define RO_SENSOR_TEMPLATE(_label, _show, _index) \
  397. { \
  398. .label = _label, \
  399. .show = _show, \
  400. .index = _index, \
  401. }
  402. #define RW_SENSOR_TEMPLATE(_label, _show, _set, _index) \
  403. { \
  404. .label = _label, \
  405. .show = _show, \
  406. .set = _set, \
  407. .index = _index, \
  408. }
  409. /* Sensor descriptions. If you add a sensor, update NUM_SENSORS above! */
  410. static struct sensor_template meter_attrs[] = {
  411. RO_SENSOR_TEMPLATE(POWER_AVERAGE_NAME, show_power, 0),
  412. RO_SENSOR_TEMPLATE("power1_accuracy", show_accuracy, 0),
  413. RO_SENSOR_TEMPLATE("power1_average_interval_min", show_val, 0),
  414. RO_SENSOR_TEMPLATE("power1_average_interval_max", show_val, 1),
  415. RO_SENSOR_TEMPLATE("power1_is_battery", show_val, 5),
  416. RW_SENSOR_TEMPLATE(POWER_AVG_INTERVAL_NAME, show_avg_interval,
  417. set_avg_interval, 0),
  418. {},
  419. };
  420. static struct sensor_template misc_cap_attrs[] = {
  421. RO_SENSOR_TEMPLATE("power1_cap_min", show_val, 2),
  422. RO_SENSOR_TEMPLATE("power1_cap_max", show_val, 3),
  423. RO_SENSOR_TEMPLATE("power1_cap_hyst", show_val, 4),
  424. RO_SENSOR_TEMPLATE(POWER_ALARM_NAME, show_val, 6),
  425. {},
  426. };
  427. static struct sensor_template ro_cap_attrs[] = {
  428. RO_SENSOR_TEMPLATE(POWER_CAP_NAME, show_cap, 0),
  429. {},
  430. };
  431. static struct sensor_template rw_cap_attrs[] = {
  432. RW_SENSOR_TEMPLATE(POWER_CAP_NAME, show_cap, set_cap, 0),
  433. {},
  434. };
  435. static struct sensor_template trip_attrs[] = {
  436. RW_SENSOR_TEMPLATE("power1_average_min", show_val, set_trip, 7),
  437. RW_SENSOR_TEMPLATE("power1_average_max", show_val, set_trip, 8),
  438. {},
  439. };
  440. static struct sensor_template misc_attrs[] = {
  441. RO_SENSOR_TEMPLATE("name", show_name, 0),
  442. RO_SENSOR_TEMPLATE("power1_model_number", show_str, 0),
  443. RO_SENSOR_TEMPLATE("power1_oem_info", show_str, 2),
  444. RO_SENSOR_TEMPLATE("power1_serial_number", show_str, 1),
  445. {},
  446. };
  447. #undef RO_SENSOR_TEMPLATE
  448. #undef RW_SENSOR_TEMPLATE
  449. /* Read power domain data */
  450. static void remove_domain_devices(struct acpi_power_meter_resource *resource)
  451. {
  452. int i;
  453. if (!resource->num_domain_devices)
  454. return;
  455. for (i = 0; i < resource->num_domain_devices; i++) {
  456. struct acpi_device *obj = resource->domain_devices[i];
  457. if (!obj)
  458. continue;
  459. sysfs_remove_link(resource->holders_dir,
  460. kobject_name(&obj->dev.kobj));
  461. acpi_dev_put(obj);
  462. }
  463. kfree(resource->domain_devices);
  464. kobject_put(resource->holders_dir);
  465. resource->num_domain_devices = 0;
  466. }
  467. static int read_domain_devices(struct acpi_power_meter_resource *resource)
  468. {
  469. int res = 0;
  470. int i;
  471. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  472. union acpi_object *pss;
  473. acpi_status status;
  474. status = acpi_evaluate_object(resource->acpi_dev->handle, "_PMD", NULL,
  475. &buffer);
  476. if (ACPI_FAILURE(status)) {
  477. acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_PMD",
  478. status);
  479. return -ENODEV;
  480. }
  481. pss = buffer.pointer;
  482. if (!pss ||
  483. pss->type != ACPI_TYPE_PACKAGE) {
  484. dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME
  485. "Invalid _PMD data\n");
  486. res = -EFAULT;
  487. goto end;
  488. }
  489. if (!pss->package.count)
  490. goto end;
  491. resource->domain_devices = kcalloc(pss->package.count,
  492. sizeof(struct acpi_device *),
  493. GFP_KERNEL);
  494. if (!resource->domain_devices) {
  495. res = -ENOMEM;
  496. goto end;
  497. }
  498. resource->holders_dir = kobject_create_and_add("measures",
  499. &resource->acpi_dev->dev.kobj);
  500. if (!resource->holders_dir) {
  501. res = -ENOMEM;
  502. goto exit_free;
  503. }
  504. resource->num_domain_devices = pss->package.count;
  505. for (i = 0; i < pss->package.count; i++) {
  506. struct acpi_device *obj;
  507. union acpi_object *element = &pss->package.elements[i];
  508. /* Refuse non-references */
  509. if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
  510. continue;
  511. /* Create a symlink to domain objects */
  512. obj = acpi_get_acpi_dev(element->reference.handle);
  513. resource->domain_devices[i] = obj;
  514. if (!obj)
  515. continue;
  516. res = sysfs_create_link(resource->holders_dir, &obj->dev.kobj,
  517. kobject_name(&obj->dev.kobj));
  518. if (res) {
  519. acpi_dev_put(obj);
  520. resource->domain_devices[i] = NULL;
  521. }
  522. }
  523. res = 0;
  524. goto end;
  525. exit_free:
  526. kfree(resource->domain_devices);
  527. end:
  528. kfree(buffer.pointer);
  529. return res;
  530. }
  531. /* Registration and deregistration */
  532. static int register_attrs(struct acpi_power_meter_resource *resource,
  533. struct sensor_template *attrs)
  534. {
  535. struct device *dev = &resource->acpi_dev->dev;
  536. struct sensor_device_attribute *sensors =
  537. &resource->sensors[resource->num_sensors];
  538. int res = 0;
  539. while (attrs->label) {
  540. sensors->dev_attr.attr.name = attrs->label;
  541. sensors->dev_attr.attr.mode = 0444;
  542. sensors->dev_attr.show = attrs->show;
  543. sensors->index = attrs->index;
  544. if (attrs->set) {
  545. sensors->dev_attr.attr.mode |= 0200;
  546. sensors->dev_attr.store = attrs->set;
  547. }
  548. sysfs_attr_init(&sensors->dev_attr.attr);
  549. res = device_create_file(dev, &sensors->dev_attr);
  550. if (res) {
  551. sensors->dev_attr.attr.name = NULL;
  552. goto error;
  553. }
  554. sensors++;
  555. resource->num_sensors++;
  556. attrs++;
  557. }
  558. error:
  559. return res;
  560. }
  561. static void remove_attrs(struct acpi_power_meter_resource *resource)
  562. {
  563. int i;
  564. for (i = 0; i < resource->num_sensors; i++) {
  565. if (!resource->sensors[i].dev_attr.attr.name)
  566. continue;
  567. device_remove_file(&resource->acpi_dev->dev,
  568. &resource->sensors[i].dev_attr);
  569. }
  570. remove_domain_devices(resource);
  571. resource->num_sensors = 0;
  572. }
  573. static int setup_attrs(struct acpi_power_meter_resource *resource)
  574. {
  575. int res = 0;
  576. res = read_domain_devices(resource);
  577. if (res)
  578. return res;
  579. if (resource->caps.flags & POWER_METER_CAN_MEASURE) {
  580. res = register_attrs(resource, meter_attrs);
  581. if (res)
  582. goto error;
  583. }
  584. if (resource->caps.flags & POWER_METER_CAN_CAP) {
  585. if (!can_cap_in_hardware()) {
  586. dev_warn(&resource->acpi_dev->dev,
  587. "Ignoring unsafe software power cap!\n");
  588. goto skip_unsafe_cap;
  589. }
  590. if (resource->caps.configurable_cap)
  591. res = register_attrs(resource, rw_cap_attrs);
  592. else
  593. res = register_attrs(resource, ro_cap_attrs);
  594. if (res)
  595. goto error;
  596. res = register_attrs(resource, misc_cap_attrs);
  597. if (res)
  598. goto error;
  599. }
  600. skip_unsafe_cap:
  601. if (resource->caps.flags & POWER_METER_CAN_TRIP) {
  602. res = register_attrs(resource, trip_attrs);
  603. if (res)
  604. goto error;
  605. }
  606. res = register_attrs(resource, misc_attrs);
  607. if (res)
  608. goto error;
  609. return res;
  610. error:
  611. remove_attrs(resource);
  612. return res;
  613. }
  614. static void free_capabilities(struct acpi_power_meter_resource *resource)
  615. {
  616. acpi_string *str;
  617. int i;
  618. str = &resource->model_number;
  619. for (i = 0; i < 3; i++, str++) {
  620. kfree(*str);
  621. *str = NULL;
  622. }
  623. }
  624. static int read_capabilities(struct acpi_power_meter_resource *resource)
  625. {
  626. int res = 0;
  627. int i;
  628. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  629. struct acpi_buffer state = { 0, NULL };
  630. struct acpi_buffer format = { sizeof("NNNNNNNNNNN"), "NNNNNNNNNNN" };
  631. union acpi_object *pss;
  632. acpi_string *str;
  633. acpi_status status;
  634. status = acpi_evaluate_object(resource->acpi_dev->handle, "_PMC", NULL,
  635. &buffer);
  636. if (ACPI_FAILURE(status)) {
  637. acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_PMC",
  638. status);
  639. return -ENODEV;
  640. }
  641. pss = buffer.pointer;
  642. if (!pss ||
  643. pss->type != ACPI_TYPE_PACKAGE ||
  644. pss->package.count != 14) {
  645. dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME
  646. "Invalid _PMC data\n");
  647. res = -EFAULT;
  648. goto end;
  649. }
  650. /* Grab all the integer data at once */
  651. state.length = sizeof(struct acpi_power_meter_capabilities);
  652. state.pointer = &resource->caps;
  653. status = acpi_extract_package(pss, &format, &state);
  654. if (ACPI_FAILURE(status)) {
  655. dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME
  656. "_PMC package parsing failed: %s\n",
  657. acpi_format_exception(status));
  658. res = -EFAULT;
  659. goto end;
  660. }
  661. if (resource->caps.units) {
  662. dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME
  663. "Unknown units %llu.\n",
  664. resource->caps.units);
  665. res = -EINVAL;
  666. goto end;
  667. }
  668. /* Grab the string data */
  669. str = &resource->model_number;
  670. for (i = 11; i < 14; i++) {
  671. union acpi_object *element = &pss->package.elements[i];
  672. if (element->type != ACPI_TYPE_STRING) {
  673. res = -EINVAL;
  674. goto error;
  675. }
  676. *str = kcalloc(element->string.length + 1, sizeof(u8),
  677. GFP_KERNEL);
  678. if (!*str) {
  679. res = -ENOMEM;
  680. goto error;
  681. }
  682. strncpy(*str, element->string.pointer, element->string.length);
  683. str++;
  684. }
  685. dev_info(&resource->acpi_dev->dev, "Found ACPI power meter.\n");
  686. goto end;
  687. error:
  688. free_capabilities(resource);
  689. end:
  690. kfree(buffer.pointer);
  691. return res;
  692. }
  693. /* Handle ACPI event notifications */
  694. static void acpi_power_meter_notify(struct acpi_device *device, u32 event)
  695. {
  696. struct acpi_power_meter_resource *resource;
  697. int res;
  698. if (!device || !acpi_driver_data(device))
  699. return;
  700. resource = acpi_driver_data(device);
  701. switch (event) {
  702. case METER_NOTIFY_CONFIG:
  703. mutex_lock(&resource->lock);
  704. free_capabilities(resource);
  705. res = read_capabilities(resource);
  706. mutex_unlock(&resource->lock);
  707. if (res)
  708. break;
  709. remove_attrs(resource);
  710. setup_attrs(resource);
  711. break;
  712. case METER_NOTIFY_TRIP:
  713. sysfs_notify(&device->dev.kobj, NULL, POWER_AVERAGE_NAME);
  714. break;
  715. case METER_NOTIFY_CAP:
  716. sysfs_notify(&device->dev.kobj, NULL, POWER_CAP_NAME);
  717. break;
  718. case METER_NOTIFY_INTERVAL:
  719. sysfs_notify(&device->dev.kobj, NULL, POWER_AVG_INTERVAL_NAME);
  720. break;
  721. case METER_NOTIFY_CAPPING:
  722. sysfs_notify(&device->dev.kobj, NULL, POWER_ALARM_NAME);
  723. dev_info(&device->dev, "Capping in progress.\n");
  724. break;
  725. default:
  726. WARN(1, "Unexpected event %d\n", event);
  727. break;
  728. }
  729. acpi_bus_generate_netlink_event(ACPI_POWER_METER_CLASS,
  730. dev_name(&device->dev), event, 0);
  731. }
  732. static int acpi_power_meter_add(struct acpi_device *device)
  733. {
  734. int res;
  735. struct acpi_power_meter_resource *resource;
  736. if (!device)
  737. return -EINVAL;
  738. resource = kzalloc(sizeof(*resource), GFP_KERNEL);
  739. if (!resource)
  740. return -ENOMEM;
  741. resource->sensors_valid = 0;
  742. resource->acpi_dev = device;
  743. mutex_init(&resource->lock);
  744. strcpy(acpi_device_name(device), ACPI_POWER_METER_DEVICE_NAME);
  745. strcpy(acpi_device_class(device), ACPI_POWER_METER_CLASS);
  746. device->driver_data = resource;
  747. res = read_capabilities(resource);
  748. if (res)
  749. goto exit_free;
  750. resource->trip[0] = -1;
  751. resource->trip[1] = -1;
  752. res = setup_attrs(resource);
  753. if (res)
  754. goto exit_free_capability;
  755. resource->hwmon_dev = hwmon_device_register(&device->dev);
  756. if (IS_ERR(resource->hwmon_dev)) {
  757. res = PTR_ERR(resource->hwmon_dev);
  758. goto exit_remove;
  759. }
  760. res = 0;
  761. goto exit;
  762. exit_remove:
  763. remove_attrs(resource);
  764. exit_free_capability:
  765. free_capabilities(resource);
  766. exit_free:
  767. kfree(resource);
  768. exit:
  769. return res;
  770. }
  771. static int acpi_power_meter_remove(struct acpi_device *device)
  772. {
  773. struct acpi_power_meter_resource *resource;
  774. if (!device || !acpi_driver_data(device))
  775. return -EINVAL;
  776. resource = acpi_driver_data(device);
  777. hwmon_device_unregister(resource->hwmon_dev);
  778. remove_attrs(resource);
  779. free_capabilities(resource);
  780. kfree(resource);
  781. return 0;
  782. }
  783. static int acpi_power_meter_resume(struct device *dev)
  784. {
  785. struct acpi_power_meter_resource *resource;
  786. if (!dev)
  787. return -EINVAL;
  788. resource = acpi_driver_data(to_acpi_device(dev));
  789. if (!resource)
  790. return -EINVAL;
  791. free_capabilities(resource);
  792. read_capabilities(resource);
  793. return 0;
  794. }
  795. static DEFINE_SIMPLE_DEV_PM_OPS(acpi_power_meter_pm, NULL,
  796. acpi_power_meter_resume);
  797. static struct acpi_driver acpi_power_meter_driver = {
  798. .name = "power_meter",
  799. .class = ACPI_POWER_METER_CLASS,
  800. .ids = power_meter_ids,
  801. .ops = {
  802. .add = acpi_power_meter_add,
  803. .remove = acpi_power_meter_remove,
  804. .notify = acpi_power_meter_notify,
  805. },
  806. .drv.pm = pm_sleep_ptr(&acpi_power_meter_pm),
  807. };
  808. /* Module init/exit routines */
  809. static int __init enable_cap_knobs(const struct dmi_system_id *d)
  810. {
  811. cap_in_hardware = 1;
  812. return 0;
  813. }
  814. static const struct dmi_system_id pm_dmi_table[] __initconst = {
  815. {
  816. enable_cap_knobs, "IBM Active Energy Manager",
  817. {
  818. DMI_MATCH(DMI_SYS_VENDOR, "IBM")
  819. },
  820. },
  821. {}
  822. };
  823. static int __init acpi_power_meter_init(void)
  824. {
  825. int result;
  826. if (acpi_disabled)
  827. return -ENODEV;
  828. dmi_check_system(pm_dmi_table);
  829. result = acpi_bus_register_driver(&acpi_power_meter_driver);
  830. if (result < 0)
  831. return result;
  832. return 0;
  833. }
  834. static void __exit acpi_power_meter_exit(void)
  835. {
  836. acpi_bus_unregister_driver(&acpi_power_meter_driver);
  837. }
  838. MODULE_AUTHOR("Darrick J. Wong <[email protected]>");
  839. MODULE_DESCRIPTION("ACPI 4.0 power meter driver");
  840. MODULE_LICENSE("GPL");
  841. module_param(force_cap_on, bool, 0644);
  842. MODULE_PARM_DESC(force_cap_on, "Enable power cap even it is unsafe to do so.");
  843. module_init(acpi_power_meter_init);
  844. module_exit(acpi_power_meter_exit);