1 // SPDX-License-Identifier: GPL-2.0-or-later 2 3 /* Platform profile sysfs interface */ 4 5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 6 7 #include <linux/acpi.h> 8 #include <linux/bits.h> 9 #include <linux/cleanup.h> 10 #include <linux/init.h> 11 #include <linux/list.h> 12 #include <linux/mutex.h> 13 #include <linux/platform_profile.h> 14 #include <linux/sysfs.h> 15 16 #define to_pprof_handler(d) (container_of(d, struct platform_profile_handler, dev)) 17 18 /* 19 * Protects profile_handlers membership and serializes profile operations 20 * against each other and handler removal. Never held across sysfs teardown, 21 * which can wait for callbacks that acquire this lock. 22 */ 23 static DEFINE_MUTEX(profile_lock); 24 /* Serialize registration and sysfs updates without blocking sysfs callbacks. */ 25 static DEFINE_MUTEX(profile_registration_lock); 26 static LIST_HEAD(profile_handlers); 27 28 struct platform_profile_handler { 29 struct list_head node; 30 const char *name; 31 struct device dev; 32 int minor; 33 unsigned long choices[BITS_TO_LONGS(PLATFORM_PROFILE_LAST)]; 34 unsigned long hidden_choices[BITS_TO_LONGS(PLATFORM_PROFILE_LAST)]; 35 const struct platform_profile_ops *ops; 36 }; 37 38 struct aggregate_choices_data { 39 unsigned long aggregate[BITS_TO_LONGS(PLATFORM_PROFILE_LAST)]; 40 int count; 41 }; 42 43 static const char * const profile_names[] = { 44 [PLATFORM_PROFILE_LOW_POWER] = "low-power", 45 [PLATFORM_PROFILE_COOL] = "cool", 46 [PLATFORM_PROFILE_QUIET] = "quiet", 47 [PLATFORM_PROFILE_BALANCED] = "balanced", 48 [PLATFORM_PROFILE_BALANCED_PERFORMANCE] = "balanced-performance", 49 [PLATFORM_PROFILE_PERFORMANCE] = "performance", 50 [PLATFORM_PROFILE_MAX_POWER] = "max-power", 51 [PLATFORM_PROFILE_CUSTOM] = "custom", 52 }; 53 static_assert(ARRAY_SIZE(profile_names) == PLATFORM_PROFILE_LAST); 54 55 static DEFINE_IDA(platform_profile_ida); 56 57 static int profile_for_each_handler(void *data, 58 int (*fn)(struct device *dev, void *data)) 59 { 60 struct platform_profile_handler *handler; 61 int ret; 62 63 lockdep_assert_held(&profile_lock); 64 65 list_for_each_entry(handler, &profile_handlers, node) { 66 ret = fn(&handler->dev, data); 67 if (ret) 68 return ret; 69 } 70 71 return 0; 72 } 73 74 /** 75 * _commmon_choices_show - Show the available profile choices 76 * @choices: The available profile choices 77 * @buf: The buffer to write to 78 * 79 * Return: The number of bytes written 80 */ 81 static ssize_t _commmon_choices_show(unsigned long *choices, char *buf) 82 { 83 int i, len = 0; 84 85 for_each_set_bit(i, choices, PLATFORM_PROFILE_LAST) { 86 if (len == 0) 87 len += sysfs_emit_at(buf, len, "%s", profile_names[i]); 88 else 89 len += sysfs_emit_at(buf, len, " %s", profile_names[i]); 90 } 91 len += sysfs_emit_at(buf, len, "\n"); 92 93 return len; 94 } 95 96 /** 97 * _store_class_profile - Set the profile for a class device 98 * @dev: The class device 99 * @data: The profile to set 100 * 101 * Return: 0 on success, -errno on failure 102 */ 103 static int _store_class_profile(struct device *dev, void *data) 104 { 105 struct platform_profile_handler *handler; 106 int *bit = (int *)data; 107 108 lockdep_assert_held(&profile_lock); 109 handler = to_pprof_handler(dev); 110 if (!test_bit(*bit, handler->choices) && !test_bit(*bit, handler->hidden_choices)) 111 return -EOPNOTSUPP; 112 113 return handler->ops->profile_set(dev, *bit); 114 } 115 116 /** 117 * _notify_class_profile - Notify the class device of a profile change 118 * @dev: The class device 119 * @data: Unused 120 * 121 * Return: 0 on success, -errno on failure 122 */ 123 static int _notify_class_profile(struct device *dev, void *data) 124 { 125 struct platform_profile_handler *handler = to_pprof_handler(dev); 126 127 lockdep_assert_held(&profile_lock); 128 sysfs_notify(&handler->dev.kobj, NULL, "profile"); 129 kobject_uevent(&handler->dev.kobj, KOBJ_CHANGE); 130 131 return 0; 132 } 133 134 /** 135 * get_class_profile - Show the current profile for a class device 136 * @dev: The class device 137 * @profile: The profile to return 138 * 139 * Return: 0 on success, -errno on failure 140 */ 141 static int get_class_profile(struct device *dev, 142 enum platform_profile_option *profile) 143 { 144 struct platform_profile_handler *handler; 145 enum platform_profile_option val; 146 int err; 147 148 lockdep_assert_held(&profile_lock); 149 handler = to_pprof_handler(dev); 150 err = handler->ops->profile_get(dev, &val); 151 if (err) { 152 pr_err("Failed to get profile for handler %s\n", handler->name); 153 return err; 154 } 155 156 if (WARN_ON(val >= PLATFORM_PROFILE_LAST)) 157 return -EINVAL; 158 *profile = val; 159 160 return 0; 161 } 162 163 /** 164 * name_show - Show the name of the profile handler 165 * @dev: The device 166 * @attr: The attribute 167 * @buf: The buffer to write to 168 * 169 * Return: The number of bytes written 170 */ 171 static ssize_t name_show(struct device *dev, struct device_attribute *attr, char *buf) 172 { 173 struct platform_profile_handler *handler = to_pprof_handler(dev); 174 175 return sysfs_emit(buf, "%s\n", handler->name); 176 } 177 static DEVICE_ATTR_RO(name); 178 179 /** 180 * choices_show - Show the available profile choices 181 * @dev: The device 182 * @attr: The attribute 183 * @buf: The buffer to write to 184 * 185 * Return: The number of bytes written 186 */ 187 static ssize_t choices_show(struct device *dev, 188 struct device_attribute *attr, 189 char *buf) 190 { 191 struct platform_profile_handler *handler = to_pprof_handler(dev); 192 193 return _commmon_choices_show(handler->choices, buf); 194 } 195 static DEVICE_ATTR_RO(choices); 196 197 /** 198 * profile_show - Show the current profile for a class device 199 * @dev: The device 200 * @attr: The attribute 201 * @buf: The buffer to write to 202 * 203 * Return: The number of bytes written 204 */ 205 static ssize_t profile_show(struct device *dev, 206 struct device_attribute *attr, 207 char *buf) 208 { 209 enum platform_profile_option profile = PLATFORM_PROFILE_LAST; 210 int err; 211 212 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) { 213 err = get_class_profile(dev, &profile); 214 if (err) 215 return err; 216 } 217 218 return sysfs_emit(buf, "%s\n", profile_names[profile]); 219 } 220 221 /** 222 * profile_store - Set the profile for a class device 223 * @dev: The device 224 * @attr: The attribute 225 * @buf: The buffer to read from 226 * @count: The number of bytes to read 227 * 228 * Return: The number of bytes read 229 */ 230 static ssize_t profile_store(struct device *dev, 231 struct device_attribute *attr, 232 const char *buf, size_t count) 233 { 234 int index, ret; 235 236 index = sysfs_match_string(profile_names, buf); 237 if (index < 0) 238 return -EINVAL; 239 240 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) { 241 ret = _store_class_profile(dev, &index); 242 if (ret) 243 return ret; 244 } 245 246 sysfs_notify(acpi_kobj, NULL, "platform_profile"); 247 248 return count; 249 } 250 static DEVICE_ATTR_RW(profile); 251 252 static struct attribute *profile_attrs[] = { 253 &dev_attr_name.attr, 254 &dev_attr_choices.attr, 255 &dev_attr_profile.attr, 256 NULL 257 }; 258 ATTRIBUTE_GROUPS(profile); 259 260 static void pprof_device_release(struct device *dev) 261 { 262 struct platform_profile_handler *pprof = to_pprof_handler(dev); 263 264 kfree(pprof); 265 } 266 267 static const struct class platform_profile_class = { 268 .name = "platform-profile", 269 .dev_groups = profile_groups, 270 .dev_release = pprof_device_release, 271 }; 272 273 /** 274 * _aggregate_choices - Aggregate the available profile choices 275 * @dev: The device 276 * @arg: struct aggregate_choices_data, with it's aggregate member bitmap 277 * initially filled with ones 278 * 279 * Return: 0 on success, -errno on failure 280 */ 281 static int _aggregate_choices(struct device *dev, void *arg) 282 { 283 unsigned long tmp[BITS_TO_LONGS(PLATFORM_PROFILE_LAST)]; 284 struct aggregate_choices_data *data = arg; 285 struct platform_profile_handler *handler; 286 287 lockdep_assert_held(&profile_lock); 288 289 handler = to_pprof_handler(dev); 290 bitmap_or(tmp, handler->choices, handler->hidden_choices, PLATFORM_PROFILE_LAST); 291 bitmap_and(data->aggregate, tmp, data->aggregate, PLATFORM_PROFILE_LAST); 292 data->count++; 293 294 return 0; 295 } 296 297 /** 298 * _remove_hidden_choices - Remove hidden choices from aggregate data 299 * @dev: The device 300 * @arg: struct aggregate_choices_data 301 * 302 * Return: 0 on success, -errno on failure 303 */ 304 static int _remove_hidden_choices(struct device *dev, void *arg) 305 { 306 struct aggregate_choices_data *data = arg; 307 struct platform_profile_handler *handler; 308 309 lockdep_assert_held(&profile_lock); 310 handler = to_pprof_handler(dev); 311 bitmap_andnot(data->aggregate, handler->choices, 312 handler->hidden_choices, PLATFORM_PROFILE_LAST); 313 314 return 0; 315 } 316 317 /** 318 * platform_profile_choices_show - Show the available profile choices for legacy sysfs interface 319 * @kobj: The kobject 320 * @attr: The attribute 321 * @buf: The buffer to write to 322 * 323 * Return: The number of bytes written 324 */ 325 static ssize_t platform_profile_choices_show(struct kobject *kobj, 326 struct kobj_attribute *attr, 327 char *buf) 328 { 329 struct aggregate_choices_data data = { 330 .aggregate = { [0 ... BITS_TO_LONGS(PLATFORM_PROFILE_LAST) - 1] = ~0UL }, 331 .count = 0, 332 }; 333 int err; 334 335 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) { 336 err = profile_for_each_handler(&data, _aggregate_choices); 337 if (err) 338 return err; 339 if (data.count == 1) { 340 err = profile_for_each_handler(&data, _remove_hidden_choices); 341 if (err) 342 return err; 343 } 344 } 345 346 /* no profile handler registered any more */ 347 if (!data.count || bitmap_empty(data.aggregate, PLATFORM_PROFILE_LAST)) 348 return -EINVAL; 349 350 return _commmon_choices_show(data.aggregate, buf); 351 } 352 353 /** 354 * _aggregate_profiles - Aggregate the profiles for legacy sysfs interface 355 * @dev: The device 356 * @data: The profile to return 357 * 358 * Return: 0 on success, -errno on failure 359 */ 360 static int _aggregate_profiles(struct device *dev, void *data) 361 { 362 enum platform_profile_option *profile = data; 363 enum platform_profile_option val; 364 int err; 365 366 err = get_class_profile(dev, &val); 367 if (err) 368 return err; 369 370 if (*profile != PLATFORM_PROFILE_LAST && *profile != val) 371 *profile = PLATFORM_PROFILE_CUSTOM; 372 else 373 *profile = val; 374 375 return 0; 376 } 377 378 /** 379 * _store_and_notify - Store and notify a class from legacy sysfs interface 380 * @dev: The device 381 * @data: The profile to return 382 * 383 * Return: 0 on success, -errno on failure 384 */ 385 static int _store_and_notify(struct device *dev, void *data) 386 { 387 enum platform_profile_option *profile = data; 388 int err; 389 390 err = _store_class_profile(dev, profile); 391 if (err) 392 return err; 393 return _notify_class_profile(dev, NULL); 394 } 395 396 /** 397 * platform_profile_show - Show the current profile for legacy sysfs interface 398 * @kobj: The kobject 399 * @attr: The attribute 400 * @buf: The buffer to write to 401 * 402 * Return: The number of bytes written 403 */ 404 static ssize_t platform_profile_show(struct kobject *kobj, 405 struct kobj_attribute *attr, 406 char *buf) 407 { 408 enum platform_profile_option profile = PLATFORM_PROFILE_LAST; 409 int err; 410 411 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) { 412 err = profile_for_each_handler(&profile, _aggregate_profiles); 413 if (err) 414 return err; 415 } 416 417 /* no profile handler registered any more */ 418 if (profile == PLATFORM_PROFILE_LAST) 419 return -EINVAL; 420 421 return sysfs_emit(buf, "%s\n", profile_names[profile]); 422 } 423 424 /** 425 * platform_profile_store - Set the profile for legacy sysfs interface 426 * @kobj: The kobject 427 * @attr: The attribute 428 * @buf: The buffer to read from 429 * @count: The number of bytes to read 430 * 431 * Return: The number of bytes read 432 */ 433 static ssize_t platform_profile_store(struct kobject *kobj, 434 struct kobj_attribute *attr, 435 const char *buf, size_t count) 436 { 437 struct aggregate_choices_data data = { 438 .aggregate = { [0 ... BITS_TO_LONGS(PLATFORM_PROFILE_LAST) - 1] = ~0UL }, 439 .count = 0, 440 }; 441 int ret; 442 int i; 443 444 /* Scan for a matching profile */ 445 i = sysfs_match_string(profile_names, buf); 446 if (i < 0 || i == PLATFORM_PROFILE_CUSTOM) 447 return -EINVAL; 448 449 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) { 450 ret = profile_for_each_handler(&data, _aggregate_choices); 451 if (ret) 452 return ret; 453 if (!data.count || !test_bit(i, data.aggregate)) 454 return -EOPNOTSUPP; 455 456 ret = profile_for_each_handler(&i, _store_and_notify); 457 if (ret) 458 return ret; 459 } 460 461 sysfs_notify(acpi_kobj, NULL, "platform_profile"); 462 463 return count; 464 } 465 466 static struct kobj_attribute attr_platform_profile_choices = __ATTR_RO(platform_profile_choices); 467 static struct kobj_attribute attr_platform_profile = __ATTR_RW(platform_profile); 468 469 static struct attribute *platform_profile_attrs[] = { 470 &attr_platform_profile_choices.attr, 471 &attr_platform_profile.attr, 472 NULL 473 }; 474 475 static umode_t profile_class_is_visible(struct kobject *kobj, struct attribute *attr, int idx) 476 { 477 guard(mutex)(&profile_lock); 478 479 return list_empty(&profile_handlers) ? 0 : attr->mode; 480 } 481 482 static const struct attribute_group platform_profile_group = { 483 .attrs = platform_profile_attrs, 484 .is_visible = profile_class_is_visible, 485 }; 486 487 /** 488 * platform_profile_notify - Notify class device and legacy sysfs interface 489 * @dev: The class device 490 */ 491 void platform_profile_notify(struct device *dev) 492 { 493 scoped_cond_guard(mutex_intr, return, &profile_lock) { 494 _notify_class_profile(dev, NULL); 495 } 496 sysfs_notify(acpi_kobj, NULL, "platform_profile"); 497 } 498 EXPORT_SYMBOL_GPL(platform_profile_notify); 499 500 /** 501 * platform_profile_cycle - Cycles profiles available on all registered class devices 502 * 503 * Return: 0 on success, -errno on failure 504 */ 505 int platform_profile_cycle(void) 506 { 507 struct aggregate_choices_data data = { 508 .aggregate = { [0 ... BITS_TO_LONGS(PLATFORM_PROFILE_LAST) - 1] = ~0UL }, 509 .count = 0, 510 }; 511 enum platform_profile_option next = PLATFORM_PROFILE_LAST; 512 enum platform_profile_option profile = PLATFORM_PROFILE_LAST; 513 int err; 514 515 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &profile_lock) { 516 err = profile_for_each_handler(&profile, _aggregate_profiles); 517 if (err) 518 return err; 519 520 if (profile == PLATFORM_PROFILE_MAX_POWER || 521 profile == PLATFORM_PROFILE_CUSTOM || 522 profile == PLATFORM_PROFILE_LAST) 523 return -EINVAL; 524 525 err = profile_for_each_handler(&data, _aggregate_choices); 526 if (err) 527 return err; 528 529 /* never iterate into a custom or max power if all drivers supported it */ 530 clear_bit(PLATFORM_PROFILE_MAX_POWER, data.aggregate); 531 clear_bit(PLATFORM_PROFILE_CUSTOM, data.aggregate); 532 533 next = find_next_bit_wrap(data.aggregate, 534 PLATFORM_PROFILE_LAST, 535 profile + 1); 536 537 err = profile_for_each_handler(&next, _store_and_notify); 538 539 if (err) 540 return err; 541 } 542 543 sysfs_notify(acpi_kobj, NULL, "platform_profile"); 544 545 return 0; 546 } 547 EXPORT_SYMBOL_GPL(platform_profile_cycle); 548 549 /** 550 * platform_profile_register - Creates and registers a platform profile class device 551 * @dev: Parent device 552 * @name: Name of the class device 553 * @drvdata: Driver data that will be attached to the class device 554 * @ops: Platform profile's mandatory operations 555 * 556 * Return: pointer to the new class device on success, ERR_PTR on failure 557 */ 558 struct device *platform_profile_register(struct device *dev, const char *name, 559 void *drvdata, 560 const struct platform_profile_ops *ops) 561 { 562 struct device *ppdev; 563 int minor; 564 int err; 565 566 /* Sanity check */ 567 if (WARN_ON_ONCE(!dev || !name || !ops || !ops->profile_get || 568 !ops->profile_set || !ops->probe)) 569 return ERR_PTR(-EINVAL); 570 571 struct platform_profile_handler *pprof __free(kfree) = kzalloc_obj(*pprof); 572 if (!pprof) 573 return ERR_PTR(-ENOMEM); 574 575 err = ops->probe(drvdata, pprof->choices); 576 if (err) { 577 dev_err(dev, "platform_profile probe failed\n"); 578 return ERR_PTR(err); 579 } 580 581 if (bitmap_empty(pprof->choices, PLATFORM_PROFILE_LAST)) { 582 dev_err(dev, "Failed to register platform_profile class device with empty choices\n"); 583 return ERR_PTR(-EINVAL); 584 } 585 586 if (ops->hidden_choices) { 587 err = ops->hidden_choices(drvdata, pprof->hidden_choices); 588 if (err) { 589 dev_err(dev, "platform_profile hidden_choices failed\n"); 590 return ERR_PTR(err); 591 } 592 } 593 594 guard(mutex)(&profile_registration_lock); 595 596 /* create class interface for individual handler */ 597 minor = ida_alloc(&platform_profile_ida, GFP_KERNEL); 598 if (minor < 0) 599 return ERR_PTR(minor); 600 601 pprof->name = name; 602 pprof->ops = ops; 603 pprof->minor = minor; 604 pprof->dev.class = &platform_profile_class; 605 pprof->dev.parent = dev; 606 dev_set_drvdata(&pprof->dev, drvdata); 607 dev_set_name(&pprof->dev, "platform-profile-%d", pprof->minor); 608 /* device_register() takes ownership of pprof/ppdev */ 609 ppdev = &no_free_ptr(pprof)->dev; 610 err = device_register(ppdev); 611 if (err) { 612 put_device(ppdev); 613 goto cleanup_ida; 614 } 615 scoped_guard(mutex, &profile_lock) 616 list_add_tail(&to_pprof_handler(ppdev)->node, &profile_handlers); 617 618 sysfs_notify(acpi_kobj, NULL, "platform_profile"); 619 620 err = sysfs_update_group(acpi_kobj, &platform_profile_group); 621 if (err) 622 goto cleanup_cur; 623 624 return ppdev; 625 626 cleanup_cur: 627 scoped_guard(mutex, &profile_lock) 628 list_del(&to_pprof_handler(ppdev)->node); 629 630 device_unregister(ppdev); 631 632 cleanup_ida: 633 ida_free(&platform_profile_ida, minor); 634 635 return ERR_PTR(err); 636 } 637 EXPORT_SYMBOL_GPL(platform_profile_register); 638 639 /** 640 * platform_profile_remove - Unregisters a platform profile class device 641 * @dev: Class device 642 */ 643 void platform_profile_remove(struct device *dev) 644 { 645 struct platform_profile_handler *pprof; 646 647 if (IS_ERR_OR_NULL(dev)) 648 return; 649 650 pprof = to_pprof_handler(dev); 651 652 guard(mutex)(&profile_registration_lock); 653 654 scoped_guard(mutex, &profile_lock) 655 list_del(&pprof->node); 656 657 ida_free(&platform_profile_ida, pprof->minor); 658 device_unregister(&pprof->dev); 659 660 sysfs_notify(acpi_kobj, NULL, "platform_profile"); 661 sysfs_update_group(acpi_kobj, &platform_profile_group); 662 } 663 EXPORT_SYMBOL_GPL(platform_profile_remove); 664 665 static void devm_platform_profile_release(struct device *dev, void *res) 666 { 667 struct device **ppdev = res; 668 669 platform_profile_remove(*ppdev); 670 } 671 672 /** 673 * devm_platform_profile_register - Device managed version of platform_profile_register 674 * @dev: Parent device 675 * @name: Name of the class device 676 * @drvdata: Driver data that will be attached to the class device 677 * @ops: Platform profile's mandatory operations 678 * 679 * Return: pointer to the new class device on success, ERR_PTR on failure 680 */ 681 struct device *devm_platform_profile_register(struct device *dev, const char *name, 682 void *drvdata, 683 const struct platform_profile_ops *ops) 684 { 685 struct device *ppdev; 686 struct device **dr; 687 688 dr = devres_alloc(devm_platform_profile_release, sizeof(*dr), GFP_KERNEL); 689 if (!dr) 690 return ERR_PTR(-ENOMEM); 691 692 ppdev = platform_profile_register(dev, name, drvdata, ops); 693 if (IS_ERR(ppdev)) { 694 devres_free(dr); 695 return ppdev; 696 } 697 698 *dr = ppdev; 699 devres_add(dev, dr); 700 701 return ppdev; 702 } 703 EXPORT_SYMBOL_GPL(devm_platform_profile_register); 704 705 static int __init platform_profile_init(void) 706 { 707 int err; 708 709 if (acpi_disabled) 710 return -EOPNOTSUPP; 711 712 err = class_register(&platform_profile_class); 713 if (err) 714 return err; 715 716 err = sysfs_create_group(acpi_kobj, &platform_profile_group); 717 if (err) 718 class_unregister(&platform_profile_class); 719 720 return err; 721 } 722 723 static void __exit platform_profile_exit(void) 724 { 725 sysfs_remove_group(acpi_kobj, &platform_profile_group); 726 class_unregister(&platform_profile_class); 727 } 728 module_init(platform_profile_init); 729 module_exit(platform_profile_exit); 730 731 MODULE_AUTHOR("Mark Pearson <markpearson@lenovo.com>"); 732 MODULE_DESCRIPTION("ACPI platform profile sysfs interface"); 733 MODULE_LICENSE("GPL"); 734