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
9 #include <linux/module.h>
10 #include <linux/hwmon.h>
11 #include <linux/hwmon-sysfs.h>
12 #include <linux/jiffies.h>
13 #include <linux/mutex.h>
14 #include <linux/dmi.h>
15 #include <linux/slab.h>
16 #include <linux/kdev_t.h>
17 #include <linux/sched.h>
18 #include <linux/time.h>
19 #include <linux/err.h>
20 #include <linux/acpi.h>
21
22 #define ACPI_POWER_METER_NAME "power_meter"
23 #define ACPI_POWER_METER_DEVICE_NAME "Power Meter"
24 #define ACPI_POWER_METER_CLASS "pwr_meter_resource"
25
26 #define NUM_SENSORS 17
27
28 #define POWER_METER_CAN_MEASURE (1 << 0)
29 #define POWER_METER_CAN_TRIP (1 << 1)
30 #define POWER_METER_CAN_CAP (1 << 2)
31 #define POWER_METER_CAN_NOTIFY (1 << 3)
32 #define POWER_METER_IS_BATTERY (1 << 8)
33 #define UNKNOWN_HYSTERESIS 0xFFFFFFFF
34 #define UNKNOWN_POWER 0xFFFFFFFF
35
36 #define METER_NOTIFY_CONFIG 0x80
37 #define METER_NOTIFY_TRIP 0x81
38 #define METER_NOTIFY_CAP 0x82
39 #define METER_NOTIFY_CAPPING 0x83
40 #define METER_NOTIFY_INTERVAL 0x84
41
42 #define POWER_AVERAGE_NAME "power1_average"
43 #define POWER_CAP_NAME "power1_cap"
44 #define POWER_AVG_INTERVAL_NAME "power1_average_interval"
45 #define POWER_ALARM_NAME "power1_alarm"
46
47 static int cap_in_hardware;
48 static bool force_cap_on;
49
can_cap_in_hardware(void)50 static int can_cap_in_hardware(void)
51 {
52 return force_cap_on || cap_in_hardware;
53 }
54
55 static const struct acpi_device_id power_meter_ids[] = {
56 {"ACPI000D", 0},
57 {"", 0},
58 };
59 MODULE_DEVICE_TABLE(acpi, power_meter_ids);
60
61 struct acpi_power_meter_capabilities {
62 u64 flags;
63 u64 units;
64 u64 type;
65 u64 accuracy;
66 u64 sampling_time;
67 u64 min_avg_interval;
68 u64 max_avg_interval;
69 u64 hysteresis;
70 u64 configurable_cap;
71 u64 min_cap;
72 u64 max_cap;
73 };
74
75 struct acpi_power_meter_resource {
76 struct acpi_device *acpi_dev;
77 acpi_bus_id name;
78 struct mutex lock;
79 struct device *hwmon_dev;
80 struct acpi_power_meter_capabilities caps;
81 acpi_string model_number;
82 acpi_string serial_number;
83 acpi_string oem_info;
84 u64 power;
85 u64 cap;
86 u64 avg_interval;
87 bool power_alarm;
88 int sensors_valid;
89 unsigned long sensors_last_updated;
90 struct sensor_device_attribute sensors[NUM_SENSORS];
91 int num_sensors;
92 s64 trip[2];
93 int num_domain_devices;
94 struct acpi_device **domain_devices;
95 struct kobject *holders_dir;
96 };
97
98 struct sensor_template {
99 char *label;
100 ssize_t (*show)(struct device *dev,
101 struct device_attribute *devattr,
102 char *buf);
103 ssize_t (*set)(struct device *dev,
104 struct device_attribute *devattr,
105 const char *buf, size_t count);
106 int index;
107 };
108
109 /* Averaging interval */
update_avg_interval(struct acpi_power_meter_resource * resource)110 static int update_avg_interval(struct acpi_power_meter_resource *resource)
111 {
112 unsigned long long data;
113 acpi_status status;
114
115 status = acpi_evaluate_integer(resource->acpi_dev->handle, "_GAI",
116 NULL, &data);
117 if (ACPI_FAILURE(status)) {
118 acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_GAI",
119 status);
120 return -ENODEV;
121 }
122
123 resource->avg_interval = data;
124 return 0;
125 }
126
show_avg_interval(struct device * dev,struct device_attribute * devattr,char * buf)127 static ssize_t show_avg_interval(struct device *dev,
128 struct device_attribute *devattr,
129 char *buf)
130 {
131 struct acpi_device *acpi_dev = to_acpi_device(dev);
132 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
133
134 mutex_lock(&resource->lock);
135 update_avg_interval(resource);
136 mutex_unlock(&resource->lock);
137
138 return sprintf(buf, "%llu\n", resource->avg_interval);
139 }
140
set_avg_interval(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)141 static ssize_t set_avg_interval(struct device *dev,
142 struct device_attribute *devattr,
143 const char *buf, size_t count)
144 {
145 struct acpi_device *acpi_dev = to_acpi_device(dev);
146 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
147 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
148 struct acpi_object_list args = { 1, &arg0 };
149 int res;
150 unsigned long temp;
151 unsigned long long data;
152 acpi_status status;
153
154 res = kstrtoul(buf, 10, &temp);
155 if (res)
156 return res;
157
158 if (temp > resource->caps.max_avg_interval ||
159 temp < resource->caps.min_avg_interval)
160 return -EINVAL;
161 arg0.integer.value = temp;
162
163 mutex_lock(&resource->lock);
164 status = acpi_evaluate_integer(resource->acpi_dev->handle, "_PAI",
165 &args, &data);
166 if (ACPI_SUCCESS(status))
167 resource->avg_interval = temp;
168 mutex_unlock(&resource->lock);
169
170 if (ACPI_FAILURE(status)) {
171 acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_PAI",
172 status);
173 return -EINVAL;
174 }
175
176 /* _PAI returns 0 on success, nonzero otherwise */
177 if (data)
178 return -EINVAL;
179
180 return count;
181 }
182
183 /* Cap functions */
update_cap(struct acpi_power_meter_resource * resource)184 static int update_cap(struct acpi_power_meter_resource *resource)
185 {
186 unsigned long long data;
187 acpi_status status;
188
189 status = acpi_evaluate_integer(resource->acpi_dev->handle, "_GHL",
190 NULL, &data);
191 if (ACPI_FAILURE(status)) {
192 acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_GHL",
193 status);
194 return -ENODEV;
195 }
196
197 resource->cap = data;
198 return 0;
199 }
200
show_cap(struct device * dev,struct device_attribute * devattr,char * buf)201 static ssize_t show_cap(struct device *dev,
202 struct device_attribute *devattr,
203 char *buf)
204 {
205 struct acpi_device *acpi_dev = to_acpi_device(dev);
206 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
207
208 mutex_lock(&resource->lock);
209 update_cap(resource);
210 mutex_unlock(&resource->lock);
211
212 return sprintf(buf, "%llu\n", resource->cap * 1000);
213 }
214
set_cap(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)215 static ssize_t set_cap(struct device *dev, struct device_attribute *devattr,
216 const char *buf, size_t count)
217 {
218 struct acpi_device *acpi_dev = to_acpi_device(dev);
219 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
220 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
221 struct acpi_object_list args = { 1, &arg0 };
222 int res;
223 unsigned long temp;
224 unsigned long long data;
225 acpi_status status;
226
227 res = kstrtoul(buf, 10, &temp);
228 if (res)
229 return res;
230
231 temp = DIV_ROUND_CLOSEST(temp, 1000);
232 if (temp > resource->caps.max_cap || temp < resource->caps.min_cap)
233 return -EINVAL;
234 arg0.integer.value = temp;
235
236 mutex_lock(&resource->lock);
237 status = acpi_evaluate_integer(resource->acpi_dev->handle, "_SHL",
238 &args, &data);
239 if (ACPI_SUCCESS(status))
240 resource->cap = temp;
241 mutex_unlock(&resource->lock);
242
243 if (ACPI_FAILURE(status)) {
244 acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_SHL",
245 status);
246 return -EINVAL;
247 }
248
249 /* _SHL returns 0 on success, nonzero otherwise */
250 if (data)
251 return -EINVAL;
252
253 return count;
254 }
255
256 /* Power meter trip points */
set_acpi_trip(struct acpi_power_meter_resource * resource)257 static int set_acpi_trip(struct acpi_power_meter_resource *resource)
258 {
259 union acpi_object arg_objs[] = {
260 {ACPI_TYPE_INTEGER},
261 {ACPI_TYPE_INTEGER}
262 };
263 struct acpi_object_list args = { 2, arg_objs };
264 unsigned long long data;
265 acpi_status status;
266
267 /* Both trip levels must be set */
268 if (resource->trip[0] < 0 || resource->trip[1] < 0)
269 return 0;
270
271 /* This driver stores min, max; ACPI wants max, min. */
272 arg_objs[0].integer.value = resource->trip[1];
273 arg_objs[1].integer.value = resource->trip[0];
274
275 status = acpi_evaluate_integer(resource->acpi_dev->handle, "_PTP",
276 &args, &data);
277 if (ACPI_FAILURE(status)) {
278 acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_PTP",
279 status);
280 return -EINVAL;
281 }
282
283 /* _PTP returns 0 on success, nonzero otherwise */
284 if (data)
285 return -EINVAL;
286
287 return 0;
288 }
289
set_trip(struct device * dev,struct device_attribute * devattr,const char * buf,size_t count)290 static ssize_t set_trip(struct device *dev, struct device_attribute *devattr,
291 const char *buf, size_t count)
292 {
293 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
294 struct acpi_device *acpi_dev = to_acpi_device(dev);
295 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
296 unsigned long temp, trip_bk;
297 int res;
298
299 res = kstrtoul(buf, 10, &temp);
300 if (res)
301 return res;
302
303 temp = DIV_ROUND_CLOSEST(temp, 1000);
304
305 guard(mutex)(&resource->lock);
306
307 trip_bk = resource->trip[attr->index - 7];
308 resource->trip[attr->index - 7] = temp;
309 res = set_acpi_trip(resource);
310 if (res) {
311 resource->trip[attr->index - 7] = trip_bk;
312 return res;
313 }
314
315 return count;
316 }
317
318 /* Power meter */
update_meter(struct acpi_power_meter_resource * resource)319 static int update_meter(struct acpi_power_meter_resource *resource)
320 {
321 unsigned long long data;
322 acpi_status status;
323 unsigned long local_jiffies = jiffies;
324
325 if (time_before(local_jiffies, resource->sensors_last_updated +
326 msecs_to_jiffies(resource->caps.sampling_time)) &&
327 resource->sensors_valid)
328 return 0;
329
330 status = acpi_evaluate_integer(resource->acpi_dev->handle, "_PMM",
331 NULL, &data);
332 if (ACPI_FAILURE(status)) {
333 acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_PMM",
334 status);
335 return -ENODEV;
336 }
337
338 resource->power = data;
339 resource->sensors_valid = 1;
340 resource->sensors_last_updated = jiffies;
341 return 0;
342 }
343
show_power(struct device * dev,struct device_attribute * devattr,char * buf)344 static ssize_t show_power(struct device *dev,
345 struct device_attribute *devattr,
346 char *buf)
347 {
348 struct acpi_device *acpi_dev = to_acpi_device(dev);
349 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
350
351 mutex_lock(&resource->lock);
352 update_meter(resource);
353 mutex_unlock(&resource->lock);
354
355 if (resource->power == UNKNOWN_POWER)
356 return -ENODATA;
357
358 return sprintf(buf, "%llu\n", resource->power * 1000);
359 }
360
361 /* Miscellaneous */
show_str(struct device * dev,struct device_attribute * devattr,char * buf)362 static ssize_t show_str(struct device *dev,
363 struct device_attribute *devattr,
364 char *buf)
365 {
366 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
367 struct acpi_device *acpi_dev = to_acpi_device(dev);
368 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
369 acpi_string val;
370 int ret;
371
372 mutex_lock(&resource->lock);
373 switch (attr->index) {
374 case 0:
375 val = resource->model_number;
376 break;
377 case 1:
378 val = resource->serial_number;
379 break;
380 case 2:
381 val = resource->oem_info;
382 break;
383 default:
384 WARN(1, "Implementation error: unexpected attribute index %d\n",
385 attr->index);
386 val = "";
387 break;
388 }
389 ret = sprintf(buf, "%s\n", val);
390 mutex_unlock(&resource->lock);
391 return ret;
392 }
393
show_val(struct device * dev,struct device_attribute * devattr,char * buf)394 static ssize_t show_val(struct device *dev,
395 struct device_attribute *devattr,
396 char *buf)
397 {
398 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
399 struct acpi_device *acpi_dev = to_acpi_device(dev);
400 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
401 u64 val = 0;
402 int ret;
403
404 guard(mutex)(&resource->lock);
405
406 switch (attr->index) {
407 case 0:
408 val = resource->caps.min_avg_interval;
409 break;
410 case 1:
411 val = resource->caps.max_avg_interval;
412 break;
413 case 2:
414 val = resource->caps.min_cap * 1000;
415 break;
416 case 3:
417 val = resource->caps.max_cap * 1000;
418 break;
419 case 4:
420 if (resource->caps.hysteresis == UNKNOWN_HYSTERESIS)
421 return sprintf(buf, "unknown\n");
422
423 val = resource->caps.hysteresis * 1000;
424 break;
425 case 5:
426 if (resource->caps.flags & POWER_METER_IS_BATTERY)
427 val = 1;
428 else
429 val = 0;
430 break;
431 case 6:
432 ret = update_meter(resource);
433 if (ret)
434 return ret;
435 /* need to update cap if not to support the notification. */
436 if (!(resource->caps.flags & POWER_METER_CAN_NOTIFY)) {
437 ret = update_cap(resource);
438 if (ret)
439 return ret;
440 }
441 val = resource->power_alarm || resource->power > resource->cap;
442 resource->power_alarm = resource->power > resource->cap;
443 break;
444 case 7:
445 case 8:
446 if (resource->trip[attr->index - 7] < 0)
447 return sprintf(buf, "unknown\n");
448
449 val = resource->trip[attr->index - 7] * 1000;
450 break;
451 default:
452 WARN(1, "Implementation error: unexpected attribute index %d\n",
453 attr->index);
454 break;
455 }
456
457 return sprintf(buf, "%llu\n", val);
458 }
459
show_accuracy(struct device * dev,struct device_attribute * devattr,char * buf)460 static ssize_t show_accuracy(struct device *dev,
461 struct device_attribute *devattr,
462 char *buf)
463 {
464 struct acpi_device *acpi_dev = to_acpi_device(dev);
465 struct acpi_power_meter_resource *resource = acpi_dev->driver_data;
466 unsigned int acc = resource->caps.accuracy;
467
468 return sprintf(buf, "%u.%u%%\n", acc / 1000, acc % 1000);
469 }
470
show_name(struct device * dev,struct device_attribute * devattr,char * buf)471 static ssize_t show_name(struct device *dev,
472 struct device_attribute *devattr,
473 char *buf)
474 {
475 return sprintf(buf, "%s\n", ACPI_POWER_METER_NAME);
476 }
477
478 #define RO_SENSOR_TEMPLATE(_label, _show, _index) \
479 { \
480 .label = _label, \
481 .show = _show, \
482 .index = _index, \
483 }
484
485 #define RW_SENSOR_TEMPLATE(_label, _show, _set, _index) \
486 { \
487 .label = _label, \
488 .show = _show, \
489 .set = _set, \
490 .index = _index, \
491 }
492
493 /* Sensor descriptions. If you add a sensor, update NUM_SENSORS above! */
494 static struct sensor_template meter_attrs[] = {
495 RO_SENSOR_TEMPLATE(POWER_AVERAGE_NAME, show_power, 0),
496 RO_SENSOR_TEMPLATE("power1_accuracy", show_accuracy, 0),
497 RO_SENSOR_TEMPLATE("power1_average_interval_min", show_val, 0),
498 RO_SENSOR_TEMPLATE("power1_average_interval_max", show_val, 1),
499 RO_SENSOR_TEMPLATE("power1_is_battery", show_val, 5),
500 RW_SENSOR_TEMPLATE(POWER_AVG_INTERVAL_NAME, show_avg_interval,
501 set_avg_interval, 0),
502 {},
503 };
504
505 static struct sensor_template misc_cap_attrs[] = {
506 RO_SENSOR_TEMPLATE("power1_cap_min", show_val, 2),
507 RO_SENSOR_TEMPLATE("power1_cap_max", show_val, 3),
508 RO_SENSOR_TEMPLATE("power1_cap_hyst", show_val, 4),
509 RO_SENSOR_TEMPLATE(POWER_ALARM_NAME, show_val, 6),
510 {},
511 };
512
513 static struct sensor_template ro_cap_attrs[] = {
514 RO_SENSOR_TEMPLATE(POWER_CAP_NAME, show_cap, 0),
515 {},
516 };
517
518 static struct sensor_template rw_cap_attrs[] = {
519 RW_SENSOR_TEMPLATE(POWER_CAP_NAME, show_cap, set_cap, 0),
520 {},
521 };
522
523 static struct sensor_template trip_attrs[] = {
524 RW_SENSOR_TEMPLATE("power1_average_min", show_val, set_trip, 7),
525 RW_SENSOR_TEMPLATE("power1_average_max", show_val, set_trip, 8),
526 {},
527 };
528
529 static struct sensor_template misc_attrs[] = {
530 RO_SENSOR_TEMPLATE("name", show_name, 0),
531 RO_SENSOR_TEMPLATE("power1_model_number", show_str, 0),
532 RO_SENSOR_TEMPLATE("power1_oem_info", show_str, 2),
533 RO_SENSOR_TEMPLATE("power1_serial_number", show_str, 1),
534 {},
535 };
536
537 #undef RO_SENSOR_TEMPLATE
538 #undef RW_SENSOR_TEMPLATE
539
540 /* Read power domain data */
remove_domain_devices(struct acpi_power_meter_resource * resource)541 static void remove_domain_devices(struct acpi_power_meter_resource *resource)
542 {
543 int i;
544
545 if (!resource->num_domain_devices)
546 return;
547
548 for (i = 0; i < resource->num_domain_devices; i++) {
549 struct acpi_device *obj = resource->domain_devices[i];
550
551 if (!obj)
552 continue;
553
554 sysfs_remove_link(resource->holders_dir,
555 kobject_name(&obj->dev.kobj));
556 acpi_dev_put(obj);
557 }
558
559 kfree(resource->domain_devices);
560 kobject_put(resource->holders_dir);
561 resource->num_domain_devices = 0;
562 }
563
read_domain_devices(struct acpi_power_meter_resource * resource)564 static int read_domain_devices(struct acpi_power_meter_resource *resource)
565 {
566 int res = 0;
567 int i;
568 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
569 union acpi_object *pss;
570 acpi_status status;
571
572 status = acpi_evaluate_object(resource->acpi_dev->handle, "_PMD", NULL,
573 &buffer);
574 if (ACPI_FAILURE(status)) {
575 acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_PMD",
576 status);
577 return -ENODEV;
578 }
579
580 pss = buffer.pointer;
581 if (!pss ||
582 pss->type != ACPI_TYPE_PACKAGE) {
583 dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME
584 "Invalid _PMD data\n");
585 res = -EFAULT;
586 goto end;
587 }
588
589 if (!pss->package.count)
590 goto end;
591
592 resource->domain_devices = kcalloc(pss->package.count,
593 sizeof(struct acpi_device *),
594 GFP_KERNEL);
595 if (!resource->domain_devices) {
596 res = -ENOMEM;
597 goto end;
598 }
599
600 resource->holders_dir = kobject_create_and_add("measures",
601 &resource->acpi_dev->dev.kobj);
602 if (!resource->holders_dir) {
603 res = -ENOMEM;
604 goto exit_free;
605 }
606
607 resource->num_domain_devices = pss->package.count;
608
609 for (i = 0; i < pss->package.count; i++) {
610 struct acpi_device *obj;
611 union acpi_object *element = &pss->package.elements[i];
612
613 /* Refuse non-references */
614 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
615 continue;
616
617 /* Create a symlink to domain objects */
618 obj = acpi_get_acpi_dev(element->reference.handle);
619 resource->domain_devices[i] = obj;
620 if (!obj)
621 continue;
622
623 res = sysfs_create_link(resource->holders_dir, &obj->dev.kobj,
624 kobject_name(&obj->dev.kobj));
625 if (res) {
626 acpi_dev_put(obj);
627 resource->domain_devices[i] = NULL;
628 }
629 }
630
631 res = 0;
632 goto end;
633
634 exit_free:
635 kfree(resource->domain_devices);
636 end:
637 kfree(buffer.pointer);
638 return res;
639 }
640
641 /* Registration and deregistration */
register_attrs(struct acpi_power_meter_resource * resource,struct sensor_template * attrs)642 static int register_attrs(struct acpi_power_meter_resource *resource,
643 struct sensor_template *attrs)
644 {
645 struct device *dev = &resource->acpi_dev->dev;
646 struct sensor_device_attribute *sensors =
647 &resource->sensors[resource->num_sensors];
648 int res = 0;
649
650 while (attrs->label) {
651 sensors->dev_attr.attr.name = attrs->label;
652 sensors->dev_attr.attr.mode = 0444;
653 sensors->dev_attr.show = attrs->show;
654 sensors->index = attrs->index;
655
656 if (attrs->set) {
657 sensors->dev_attr.attr.mode |= 0200;
658 sensors->dev_attr.store = attrs->set;
659 }
660
661 sysfs_attr_init(&sensors->dev_attr.attr);
662 res = device_create_file(dev, &sensors->dev_attr);
663 if (res) {
664 sensors->dev_attr.attr.name = NULL;
665 goto error;
666 }
667 sensors++;
668 resource->num_sensors++;
669 attrs++;
670 }
671
672 error:
673 return res;
674 }
675
remove_attrs(struct acpi_power_meter_resource * resource)676 static void remove_attrs(struct acpi_power_meter_resource *resource)
677 {
678 int i;
679
680 for (i = 0; i < resource->num_sensors; i++) {
681 if (!resource->sensors[i].dev_attr.attr.name)
682 continue;
683 device_remove_file(&resource->acpi_dev->dev,
684 &resource->sensors[i].dev_attr);
685 }
686
687 remove_domain_devices(resource);
688
689 resource->num_sensors = 0;
690 }
691
setup_attrs(struct acpi_power_meter_resource * resource)692 static int setup_attrs(struct acpi_power_meter_resource *resource)
693 {
694 int res = 0;
695
696 /* _PMD method is optional. */
697 res = read_domain_devices(resource);
698 if (res && res != -ENODEV)
699 return res;
700
701 if (resource->caps.flags & POWER_METER_CAN_MEASURE) {
702 res = register_attrs(resource, meter_attrs);
703 if (res)
704 goto error;
705 }
706
707 if (resource->caps.flags & POWER_METER_CAN_CAP) {
708 if (!can_cap_in_hardware()) {
709 dev_warn(&resource->acpi_dev->dev,
710 "Ignoring unsafe software power cap!\n");
711 goto skip_unsafe_cap;
712 }
713
714 if (resource->caps.configurable_cap)
715 res = register_attrs(resource, rw_cap_attrs);
716 else
717 res = register_attrs(resource, ro_cap_attrs);
718
719 if (res)
720 goto error;
721
722 res = register_attrs(resource, misc_cap_attrs);
723 if (res)
724 goto error;
725 }
726
727 skip_unsafe_cap:
728 if (resource->caps.flags & POWER_METER_CAN_TRIP) {
729 res = register_attrs(resource, trip_attrs);
730 if (res)
731 goto error;
732 }
733
734 res = register_attrs(resource, misc_attrs);
735 if (res)
736 goto error;
737
738 return res;
739 error:
740 remove_attrs(resource);
741 return res;
742 }
743
free_capabilities(struct acpi_power_meter_resource * resource)744 static void free_capabilities(struct acpi_power_meter_resource *resource)
745 {
746 acpi_string *str;
747 int i;
748
749 str = &resource->model_number;
750 for (i = 0; i < 3; i++, str++) {
751 kfree(*str);
752 *str = NULL;
753 }
754 }
755
read_capabilities(struct acpi_power_meter_resource * resource)756 static int read_capabilities(struct acpi_power_meter_resource *resource)
757 {
758 int res = 0;
759 int i;
760 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
761 struct acpi_buffer state = { 0, NULL };
762 struct acpi_buffer format = { sizeof("NNNNNNNNNNN"), "NNNNNNNNNNN" };
763 union acpi_object *pss;
764 acpi_string *str;
765 acpi_status status;
766
767 status = acpi_evaluate_object(resource->acpi_dev->handle, "_PMC", NULL,
768 &buffer);
769 if (ACPI_FAILURE(status)) {
770 acpi_evaluation_failure_warn(resource->acpi_dev->handle, "_PMC",
771 status);
772 return -ENODEV;
773 }
774
775 pss = buffer.pointer;
776 if (!pss ||
777 pss->type != ACPI_TYPE_PACKAGE ||
778 pss->package.count != 14) {
779 dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME
780 "Invalid _PMC data\n");
781 res = -EFAULT;
782 goto end;
783 }
784
785 /* Grab all the integer data at once */
786 state.length = sizeof(struct acpi_power_meter_capabilities);
787 state.pointer = &resource->caps;
788
789 status = acpi_extract_package(pss, &format, &state);
790 if (ACPI_FAILURE(status)) {
791 dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME
792 "_PMC package parsing failed: %s\n",
793 acpi_format_exception(status));
794 res = -EFAULT;
795 goto end;
796 }
797
798 if (resource->caps.units) {
799 dev_err(&resource->acpi_dev->dev, ACPI_POWER_METER_NAME
800 "Unknown units %llu.\n",
801 resource->caps.units);
802 res = -EINVAL;
803 goto end;
804 }
805
806 /* Grab the string data */
807 str = &resource->model_number;
808
809 for (i = 11; i < 14; i++) {
810 union acpi_object *element = &pss->package.elements[i];
811
812 if (element->type != ACPI_TYPE_STRING) {
813 res = -EINVAL;
814 goto error;
815 }
816
817 *str = kmemdup_nul(element->string.pointer, element->string.length,
818 GFP_KERNEL);
819 if (!*str) {
820 res = -ENOMEM;
821 goto error;
822 }
823
824 str++;
825 }
826
827 dev_info(&resource->acpi_dev->dev, "Found ACPI power meter.\n");
828 goto end;
829 error:
830 free_capabilities(resource);
831 end:
832 kfree(buffer.pointer);
833 return res;
834 }
835
836 /* Handle ACPI event notifications */
acpi_power_meter_notify(struct acpi_device * device,u32 event)837 static void acpi_power_meter_notify(struct acpi_device *device, u32 event)
838 {
839 struct acpi_power_meter_resource *resource;
840 int res;
841
842 if (!device || !acpi_driver_data(device))
843 return;
844
845 resource = acpi_driver_data(device);
846
847 switch (event) {
848 case METER_NOTIFY_CONFIG:
849 mutex_lock(&resource->lock);
850 free_capabilities(resource);
851 res = read_capabilities(resource);
852 mutex_unlock(&resource->lock);
853 if (res)
854 break;
855
856 remove_attrs(resource);
857 setup_attrs(resource);
858 break;
859 case METER_NOTIFY_TRIP:
860 sysfs_notify(&device->dev.kobj, NULL, POWER_AVERAGE_NAME);
861 break;
862 case METER_NOTIFY_CAP:
863 mutex_lock(&resource->lock);
864 res = update_cap(resource);
865 if (res)
866 dev_err_once(&device->dev, "update cap failed when capping value is changed.\n");
867 mutex_unlock(&resource->lock);
868 sysfs_notify(&device->dev.kobj, NULL, POWER_CAP_NAME);
869 break;
870 case METER_NOTIFY_INTERVAL:
871 sysfs_notify(&device->dev.kobj, NULL, POWER_AVG_INTERVAL_NAME);
872 break;
873 case METER_NOTIFY_CAPPING:
874 mutex_lock(&resource->lock);
875 resource->power_alarm = true;
876 mutex_unlock(&resource->lock);
877 sysfs_notify(&device->dev.kobj, NULL, POWER_ALARM_NAME);
878 dev_info(&device->dev, "Capping in progress.\n");
879 break;
880 default:
881 WARN(1, "Unexpected event %d\n", event);
882 break;
883 }
884
885 acpi_bus_generate_netlink_event(ACPI_POWER_METER_CLASS,
886 dev_name(&device->dev), event, 0);
887 }
888
acpi_power_meter_add(struct acpi_device * device)889 static int acpi_power_meter_add(struct acpi_device *device)
890 {
891 int res;
892 struct acpi_power_meter_resource *resource;
893
894 if (!device)
895 return -EINVAL;
896
897 resource = kzalloc(sizeof(*resource), GFP_KERNEL);
898 if (!resource)
899 return -ENOMEM;
900
901 resource->sensors_valid = 0;
902 resource->acpi_dev = device;
903 mutex_init(&resource->lock);
904 strcpy(acpi_device_name(device), ACPI_POWER_METER_DEVICE_NAME);
905 strcpy(acpi_device_class(device), ACPI_POWER_METER_CLASS);
906 device->driver_data = resource;
907
908 #if IS_REACHABLE(CONFIG_ACPI_IPMI)
909 /*
910 * On Dell systems several methods of acpi_power_meter access
911 * variables in IPMI region, so wait until IPMI space handler is
912 * installed by acpi_ipmi and also wait until SMI is selected to make
913 * the space handler fully functional.
914 */
915 if (dmi_match(DMI_SYS_VENDOR, "Dell Inc.")) {
916 struct acpi_device *ipi_device = acpi_dev_get_first_match_dev("IPI0001", NULL, -1);
917
918 if (ipi_device && acpi_wait_for_acpi_ipmi())
919 dev_warn(&device->dev, "Waiting for ACPI IPMI timeout");
920 acpi_dev_put(ipi_device);
921 }
922 #endif
923
924 res = read_capabilities(resource);
925 if (res)
926 goto exit_free;
927
928 resource->trip[0] = -1;
929 resource->trip[1] = -1;
930
931 res = setup_attrs(resource);
932 if (res)
933 goto exit_free_capability;
934
935 resource->hwmon_dev = hwmon_device_register(&device->dev);
936 if (IS_ERR(resource->hwmon_dev)) {
937 res = PTR_ERR(resource->hwmon_dev);
938 goto exit_remove;
939 }
940
941 res = 0;
942 goto exit;
943
944 exit_remove:
945 remove_attrs(resource);
946 exit_free_capability:
947 free_capabilities(resource);
948 exit_free:
949 kfree(resource);
950 exit:
951 return res;
952 }
953
acpi_power_meter_remove(struct acpi_device * device)954 static void acpi_power_meter_remove(struct acpi_device *device)
955 {
956 struct acpi_power_meter_resource *resource;
957
958 if (!device || !acpi_driver_data(device))
959 return;
960
961 resource = acpi_driver_data(device);
962 hwmon_device_unregister(resource->hwmon_dev);
963
964 remove_attrs(resource);
965 free_capabilities(resource);
966
967 kfree(resource);
968 }
969
acpi_power_meter_resume(struct device * dev)970 static int acpi_power_meter_resume(struct device *dev)
971 {
972 struct acpi_power_meter_resource *resource;
973
974 if (!dev)
975 return -EINVAL;
976
977 resource = acpi_driver_data(to_acpi_device(dev));
978 if (!resource)
979 return -EINVAL;
980
981 free_capabilities(resource);
982 read_capabilities(resource);
983
984 return 0;
985 }
986
987 static DEFINE_SIMPLE_DEV_PM_OPS(acpi_power_meter_pm, NULL,
988 acpi_power_meter_resume);
989
990 static struct acpi_driver acpi_power_meter_driver = {
991 .name = "power_meter",
992 .class = ACPI_POWER_METER_CLASS,
993 .ids = power_meter_ids,
994 .ops = {
995 .add = acpi_power_meter_add,
996 .remove = acpi_power_meter_remove,
997 .notify = acpi_power_meter_notify,
998 },
999 .drv.pm = pm_sleep_ptr(&acpi_power_meter_pm),
1000 };
1001
1002 /* Module init/exit routines */
enable_cap_knobs(const struct dmi_system_id * d)1003 static int __init enable_cap_knobs(const struct dmi_system_id *d)
1004 {
1005 cap_in_hardware = 1;
1006 return 0;
1007 }
1008
1009 static const struct dmi_system_id pm_dmi_table[] __initconst = {
1010 {
1011 enable_cap_knobs, "IBM Active Energy Manager",
1012 {
1013 DMI_MATCH(DMI_SYS_VENDOR, "IBM")
1014 },
1015 },
1016 {}
1017 };
1018
acpi_power_meter_init(void)1019 static int __init acpi_power_meter_init(void)
1020 {
1021 int result;
1022
1023 if (acpi_disabled)
1024 return -ENODEV;
1025
1026 dmi_check_system(pm_dmi_table);
1027
1028 result = acpi_bus_register_driver(&acpi_power_meter_driver);
1029 if (result < 0)
1030 return result;
1031
1032 return 0;
1033 }
1034
acpi_power_meter_exit(void)1035 static void __exit acpi_power_meter_exit(void)
1036 {
1037 acpi_bus_unregister_driver(&acpi_power_meter_driver);
1038 }
1039
1040 MODULE_AUTHOR("Darrick J. Wong <darrick.wong@oracle.com>");
1041 MODULE_DESCRIPTION("ACPI 4.0 power meter driver");
1042 MODULE_LICENSE("GPL");
1043
1044 module_param(force_cap_on, bool, 0644);
1045 MODULE_PARM_DESC(force_cap_on, "Enable power cap even it is unsafe to do so.");
1046
1047 module_init(acpi_power_meter_init);
1048 module_exit(acpi_power_meter_exit);
1049