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