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