22 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/kdev_t.h>
30 #include <linux/sched.h>
31 #include <linux/time.h>
36 #define ACPI_POWER_METER_NAME "power_meter"
38 #define ACPI_POWER_METER_DEVICE_NAME "Power Meter"
39 #define ACPI_POWER_METER_CLASS "pwr_meter_resource"
41 #define NUM_SENSORS 17
43 #define POWER_METER_CAN_MEASURE (1 << 0)
44 #define POWER_METER_CAN_TRIP (1 << 1)
45 #define POWER_METER_CAN_CAP (1 << 2)
46 #define POWER_METER_CAN_NOTIFY (1 << 3)
47 #define POWER_METER_IS_BATTERY (1 << 8)
48 #define UNKNOWN_HYSTERESIS 0xFFFFFFFF
50 #define METER_NOTIFY_CONFIG 0x80
51 #define METER_NOTIFY_TRIP 0x81
52 #define METER_NOTIFY_CAP 0x82
53 #define METER_NOTIFY_CAPPING 0x83
54 #define METER_NOTIFY_INTERVAL 0x84
56 #define POWER_AVERAGE_NAME "power1_average"
57 #define POWER_CAP_NAME "power1_cap"
58 #define POWER_AVG_INTERVAL_NAME "power1_average_interval"
59 #define POWER_ALARM_NAME "power1_alarm"
61 static int cap_in_hardware;
62 static bool force_cap_on;
64 static int can_cap_in_hardware(
void)
66 return force_cap_on || cap_in_hardware;
125 unsigned long long data;
143 struct acpi_device *acpi_dev = to_acpi_device(dev);
147 update_avg_interval(resource);
157 struct acpi_device *acpi_dev = to_acpi_device(dev);
163 unsigned long long data;
166 res = kstrtoul(buf, 10, &temp);
170 if (temp > resource->
caps.max_avg_interval ||
171 temp < resource->
caps.min_avg_interval)
197 unsigned long long data;
215 struct acpi_device *acpi_dev = to_acpi_device(dev);
219 update_cap(resource);
222 return sprintf(buf,
"%llu\n", resource->
cap * 1000);
226 const char *buf,
size_t count)
228 struct acpi_device *acpi_dev = to_acpi_device(dev);
234 unsigned long long data;
237 res = kstrtoul(buf, 10, &temp);
242 if (temp > resource->
caps.max_cap || temp < resource->
caps.min_cap)
273 unsigned long long data;
277 if (resource->
trip[0] < 0 || resource->
trip[1] < 0)
299 const char *buf,
size_t count)
302 struct acpi_device *acpi_dev = to_acpi_device(dev);
307 res = kstrtoul(buf, 10, &temp);
315 res = set_acpi_trip(resource);
327 unsigned long long data;
329 unsigned long local_jiffies =
jiffies;
353 struct acpi_device *acpi_dev = to_acpi_device(dev);
357 update_meter(resource);
369 struct acpi_device *acpi_dev = to_acpi_device(dev);
373 switch (attr->
index) {
388 return sprintf(buf,
"%s\n", val);
396 struct acpi_device *acpi_dev = to_acpi_device(dev);
400 switch (attr->
index) {
402 val = resource->
caps.min_avg_interval;
405 val = resource->
caps.max_avg_interval;
408 val = resource->
caps.min_cap * 1000;
411 val = resource->
caps.max_cap * 1000;
415 return sprintf(buf,
"unknown\n");
417 val = resource->
caps.hysteresis * 1000;
426 if (resource->
power > resource->
cap)
434 return sprintf(buf,
"unknown\n");
436 val = resource->
trip[attr->
index - 7] * 1000;
442 return sprintf(buf,
"%llu\n", val);
449 struct acpi_device *acpi_dev = to_acpi_device(dev);
451 unsigned int acc = resource->
caps.accuracy;
453 return sprintf(buf,
"%u.%u%%\n", acc / 1000, acc % 1000);
463 #define RO_SENSOR_TEMPLATE(_label, _show, _index) \
470 #define RW_SENSOR_TEMPLATE(_label, _show, _set, _index) \
486 set_avg_interval, 0),
522 #undef RO_SENSOR_TEMPLATE
523 #undef RW_SENSOR_TEMPLATE
539 kobject_name(&obj->dev.kobj));
567 "Invalid _PMD data\n");
591 for (i = 0; i < pss->
package.count; i++) {
592 struct acpi_device *obj;
610 kobject_name(&obj->dev.kobj));
636 while (attrs->
label) {
667 if (!resource->
sensors[i].dev_attr.attr.name)
670 &resource->
sensors[i].dev_attr);
673 remove_domain_devices(resource);
682 res = read_domain_devices(resource);
687 res = register_attrs(resource, meter_attrs);
693 if (!can_cap_in_hardware()) {
695 "Ignoring unsafe software power cap!\n");
696 goto skip_unsafe_cap;
699 if (resource->
caps.configurable_cap)
700 res = register_attrs(resource, rw_cap_attrs);
702 res = register_attrs(resource, ro_cap_attrs);
707 res = register_attrs(resource, misc_cap_attrs);
714 res = register_attrs(resource, trip_attrs);
719 res = register_attrs(resource, misc_attrs);
725 remove_attrs(resource);
735 for (i = 0; i < 3; i++, str++)
762 "Invalid _PMC data\n");
778 if (resource->
caps.units) {
780 "Unknown units %llu.\n",
781 resource->
caps.units);
789 for (i = 11; i < 14; i++) {
797 *str = kzalloc(
sizeof(
u8) * (element->
string.length + 1),
812 for (i = 0; i < 3; i++, str++)
820 static void acpi_power_meter_notify(
struct acpi_device *
device,
u32 event)
825 if (!device || !acpi_driver_data(device))
828 resource = acpi_driver_data(device);
833 free_capabilities(resource);
834 res = read_capabilities(resource);
838 remove_attrs(resource);
839 setup_attrs(resource);
843 update_meter(resource);
847 update_cap(resource);
851 update_avg_interval(resource);
855 dev_info(&device->dev,
"Capping in progress.\n");
863 dev_name(&device->dev), event, 0);
866 static int acpi_power_meter_add(
struct acpi_device *device)
886 free_capabilities(resource);
887 res = read_capabilities(resource);
891 resource->
trip[0] = resource->
trip[1] = -1;
893 res = setup_attrs(resource);
907 remove_attrs(resource);
914 static int acpi_power_meter_remove(
struct acpi_device *device,
int type)
918 if (!device || !acpi_driver_data(device))
921 resource = acpi_driver_data(device);
924 free_capabilities(resource);
925 remove_attrs(resource);
931 #ifdef CONFIG_PM_SLEEP
933 static int acpi_power_meter_resume(
struct device *dev)
940 resource = acpi_driver_data(to_acpi_device(dev));
944 free_capabilities(resource);
945 read_capabilities(resource);
954 static struct acpi_driver acpi_power_meter_driver = {
955 .name =
"power_meter",
957 .ids = power_meter_ids,
959 .add = acpi_power_meter_add,
960 .remove = acpi_power_meter_remove,
961 .notify = acpi_power_meter_notify,
963 .drv.pm = &acpi_power_meter_pm,
975 enable_cap_knobs,
"IBM Active Energy Manager",
983 static int __init acpi_power_meter_init(
void)
999 static void __exit acpi_power_meter_exit(
void)
1009 MODULE_PARM_DESC(force_cap_on,
"Enable power cap even it is unsafe to do so.");