Skip to content

Commit effe55a

Browse files
committed
Merge remote-tracking branch 'linux-pm/acpi-platform' into review-hans
2 parents cfa75cc + 041142d commit effe55a

File tree

7 files changed

+303
-0
lines changed

7 files changed

+303
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
What: /sys/firmware/acpi/platform_profile_choices
2+
Date: October 2020
3+
Contact: Hans de Goede <hdegoede@redhat.com>
4+
Description: This file contains a space-separated list of profiles supported for this device.
5+
6+
Drivers must use the following standard profile-names:
7+
8+
============ ============================================
9+
low-power Low power consumption
10+
cool Cooler operation
11+
quiet Quieter operation
12+
balanced Balance between low power consumption and performance
13+
performance High performance operation
14+
============ ============================================
15+
16+
Userspace may expect drivers to offer more than one of these
17+
standard profile names.
18+
19+
What: /sys/firmware/acpi/platform_profile
20+
Date: October 2020
21+
Contact: Hans de Goede <hdegoede@redhat.com>
22+
Description: Reading this file gives the current selected profile for this
23+
device. Writing this file with one of the strings from
24+
platform_profile_choices changes the profile to the new value.

Documentation/userspace-api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ place where this information is gathered.
2424
ioctl/index
2525
iommu
2626
media/index
27+
sysfs-platform_profile
2728

2829
.. only:: subproject and html
2930

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
=====================================================================
2+
Platform Profile Selection (e.g. /sys/firmware/acpi/platform_profile)
3+
=====================================================================
4+
5+
On modern systems the platform performance, temperature, fan and other
6+
hardware related characteristics are often dynamically configurable. The
7+
platform configuration is often automatically adjusted to the current
8+
conditions by some automatic mechanism (which may very well live outside
9+
the kernel).
10+
11+
These auto platform adjustment mechanisms often can be configured with
12+
one of several platform profiles, with either a bias towards low power
13+
operation or towards performance.
14+
15+
The purpose of the platform_profile attribute is to offer a generic sysfs
16+
API for selecting the platform profile of these automatic mechanisms.
17+
18+
Note that this API is only for selecting the platform profile, it is
19+
NOT a goal of this API to allow monitoring the resulting performance
20+
characteristics. Monitoring performance is best done with device/vendor
21+
specific tools such as e.g. turbostat.
22+
23+
Specifically when selecting a high performance profile the actual achieved
24+
performance may be limited by various factors such as: the heat generated
25+
by other components, room temperature, free air flow at the bottom of a
26+
laptop, etc. It is explicitly NOT a goal of this API to let userspace know
27+
about any sub-optimal conditions which are impeding reaching the requested
28+
performance level.
29+
30+
Since numbers on their own cannot represent the multiple variables that a
31+
profile will adjust (power consumption, heat generation, etc) this API
32+
uses strings to describe the various profiles. To make sure that userspace
33+
gets a consistent experience the sysfs-platform_profile ABI document defines
34+
a fixed set of profile names. Drivers *must* map their internal profile
35+
representation onto this fixed set.
36+
37+
If there is no good match when mapping then a new profile name may be
38+
added. Drivers which wish to introduce new profile names must:
39+
40+
1. Explain why the existing profile names canot be used.
41+
2. Add the new profile name, along with a clear description of the
42+
expected behaviour, to the sysfs-platform_profile ABI documentation.

drivers/acpi/Kconfig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,23 @@ config ACPI_THERMAL
326326
To compile this driver as a module, choose M here:
327327
the module will be called thermal.
328328

329+
config ACPI_PLATFORM_PROFILE
330+
tristate "ACPI Platform Profile Driver"
331+
default m
332+
help
333+
This driver adds support for platform-profiles on platforms that
334+
support it.
335+
336+
Platform-profiles can be used to control the platform behaviour. For
337+
example whether to operate in a lower power mode, in a higher
338+
power performance mode or between the two.
339+
340+
This driver provides the sysfs interface and is used as the registration
341+
point for platform specific drivers.
342+
343+
Which profiles are supported is determined on a per-platform basis and
344+
should be obtained from the platform specific driver.
345+
329346
config ACPI_CUSTOM_DSDT_FILE
330347
string "Custom DSDT Table file to include"
331348
default ""

drivers/acpi/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ obj-$(CONFIG_ACPI_PCI_SLOT) += pci_slot.o
7979
obj-$(CONFIG_ACPI_PROCESSOR) += processor.o
8080
obj-$(CONFIG_ACPI) += container.o
8181
obj-$(CONFIG_ACPI_THERMAL) += thermal.o
82+
obj-$(CONFIG_ACPI_PLATFORM_PROFILE) += platform_profile.o
8283
obj-$(CONFIG_ACPI_NFIT) += nfit/
8384
obj-$(CONFIG_ACPI_NUMA) += numa/
8485
obj-$(CONFIG_ACPI) += acpi_memhotplug.o

drivers/acpi/platform_profile.c

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
3+
/* Platform profile sysfs interface */
4+
5+
#include <linux/acpi.h>
6+
#include <linux/bits.h>
7+
#include <linux/init.h>
8+
#include <linux/mutex.h>
9+
#include <linux/platform_profile.h>
10+
#include <linux/sysfs.h>
11+
12+
static struct platform_profile_handler *cur_profile;
13+
static DEFINE_MUTEX(profile_lock);
14+
15+
static const char * const profile_names[] = {
16+
[PLATFORM_PROFILE_LOW_POWER] = "low-power",
17+
[PLATFORM_PROFILE_COOL] = "cool",
18+
[PLATFORM_PROFILE_QUIET] = "quiet",
19+
[PLATFORM_PROFILE_BALANCED] = "balanced",
20+
[PLATFORM_PROFILE_PERFORMANCE] = "performance",
21+
};
22+
static_assert(ARRAY_SIZE(profile_names) == PLATFORM_PROFILE_LAST);
23+
24+
static ssize_t platform_profile_choices_show(struct device *dev,
25+
struct device_attribute *attr,
26+
char *buf)
27+
{
28+
int len = 0;
29+
int err, i;
30+
31+
err = mutex_lock_interruptible(&profile_lock);
32+
if (err)
33+
return err;
34+
35+
if (!cur_profile) {
36+
mutex_unlock(&profile_lock);
37+
return -ENODEV;
38+
}
39+
40+
for_each_set_bit(i, cur_profile->choices, PLATFORM_PROFILE_LAST) {
41+
if (len == 0)
42+
len += sysfs_emit_at(buf, len, "%s", profile_names[i]);
43+
else
44+
len += sysfs_emit_at(buf, len, " %s", profile_names[i]);
45+
}
46+
len += sysfs_emit_at(buf, len, "\n");
47+
mutex_unlock(&profile_lock);
48+
return len;
49+
}
50+
51+
static ssize_t platform_profile_show(struct device *dev,
52+
struct device_attribute *attr,
53+
char *buf)
54+
{
55+
enum platform_profile_option profile = PLATFORM_PROFILE_BALANCED;
56+
int err;
57+
58+
err = mutex_lock_interruptible(&profile_lock);
59+
if (err)
60+
return err;
61+
62+
if (!cur_profile) {
63+
mutex_unlock(&profile_lock);
64+
return -ENODEV;
65+
}
66+
67+
err = cur_profile->profile_get(cur_profile, &profile);
68+
mutex_unlock(&profile_lock);
69+
if (err)
70+
return err;
71+
72+
/* Check that profile is valid index */
73+
if (WARN_ON((profile < 0) || (profile >= ARRAY_SIZE(profile_names))))
74+
return -EIO;
75+
76+
return sysfs_emit(buf, "%s\n", profile_names[profile]);
77+
}
78+
79+
static ssize_t platform_profile_store(struct device *dev,
80+
struct device_attribute *attr,
81+
const char *buf, size_t count)
82+
{
83+
int err, i;
84+
85+
err = mutex_lock_interruptible(&profile_lock);
86+
if (err)
87+
return err;
88+
89+
if (!cur_profile) {
90+
mutex_unlock(&profile_lock);
91+
return -ENODEV;
92+
}
93+
94+
/* Scan for a matching profile */
95+
i = sysfs_match_string(profile_names, buf);
96+
if (i < 0) {
97+
mutex_unlock(&profile_lock);
98+
return -EINVAL;
99+
}
100+
101+
/* Check that platform supports this profile choice */
102+
if (!test_bit(i, cur_profile->choices)) {
103+
mutex_unlock(&profile_lock);
104+
return -EOPNOTSUPP;
105+
}
106+
107+
err = cur_profile->profile_set(cur_profile, i);
108+
mutex_unlock(&profile_lock);
109+
if (err)
110+
return err;
111+
return count;
112+
}
113+
114+
static DEVICE_ATTR_RO(platform_profile_choices);
115+
static DEVICE_ATTR_RW(platform_profile);
116+
117+
static struct attribute *platform_profile_attrs[] = {
118+
&dev_attr_platform_profile_choices.attr,
119+
&dev_attr_platform_profile.attr,
120+
NULL
121+
};
122+
123+
static const struct attribute_group platform_profile_group = {
124+
.attrs = platform_profile_attrs
125+
};
126+
127+
void platform_profile_notify(void)
128+
{
129+
if (!cur_profile)
130+
return;
131+
sysfs_notify(acpi_kobj, NULL, "platform_profile");
132+
}
133+
EXPORT_SYMBOL_GPL(platform_profile_notify);
134+
135+
int platform_profile_register(struct platform_profile_handler *pprof)
136+
{
137+
int err;
138+
139+
mutex_lock(&profile_lock);
140+
/* We can only have one active profile */
141+
if (cur_profile) {
142+
mutex_unlock(&profile_lock);
143+
return -EEXIST;
144+
}
145+
146+
/* Sanity check the profile handler field are set */
147+
if (!pprof || bitmap_empty(pprof->choices, PLATFORM_PROFILE_LAST) ||
148+
!pprof->profile_set || !pprof->profile_get) {
149+
mutex_unlock(&profile_lock);
150+
return -EINVAL;
151+
}
152+
153+
err = sysfs_create_group(acpi_kobj, &platform_profile_group);
154+
if (err) {
155+
mutex_unlock(&profile_lock);
156+
return err;
157+
}
158+
159+
cur_profile = pprof;
160+
mutex_unlock(&profile_lock);
161+
return 0;
162+
}
163+
EXPORT_SYMBOL_GPL(platform_profile_register);
164+
165+
int platform_profile_remove(void)
166+
{
167+
sysfs_remove_group(acpi_kobj, &platform_profile_group);
168+
169+
mutex_lock(&profile_lock);
170+
cur_profile = NULL;
171+
mutex_unlock(&profile_lock);
172+
return 0;
173+
}
174+
EXPORT_SYMBOL_GPL(platform_profile_remove);
175+
176+
MODULE_AUTHOR("Mark Pearson <markpearson@lenovo.com>");
177+
MODULE_LICENSE("GPL");

include/linux/platform_profile.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
/*
3+
* Platform profile sysfs interface
4+
*
5+
* See Documentation/ABI/testing/sysfs-platform_profile.rst for more
6+
* information.
7+
*/
8+
9+
#ifndef _PLATFORM_PROFILE_H_
10+
#define _PLATFORM_PROFILE_H_
11+
12+
#include <linux/bitops.h>
13+
14+
/*
15+
* If more options are added please update profile_names
16+
* array in platform-profile.c and sysfs-platform-profile.rst
17+
* documentation.
18+
*/
19+
20+
enum platform_profile_option {
21+
PLATFORM_PROFILE_LOW_POWER,
22+
PLATFORM_PROFILE_COOL,
23+
PLATFORM_PROFILE_QUIET,
24+
PLATFORM_PROFILE_BALANCED,
25+
PLATFORM_PROFILE_PERFORMANCE,
26+
PLATFORM_PROFILE_LAST, /*must always be last */
27+
};
28+
29+
struct platform_profile_handler {
30+
unsigned long choices[BITS_TO_LONGS(PLATFORM_PROFILE_LAST)];
31+
int (*profile_get)(struct platform_profile_handler *pprof,
32+
enum platform_profile_option *profile);
33+
int (*profile_set)(struct platform_profile_handler *pprof,
34+
enum platform_profile_option profile);
35+
};
36+
37+
int platform_profile_register(struct platform_profile_handler *pprof);
38+
int platform_profile_remove(void);
39+
void platform_profile_notify(void);
40+
41+
#endif /*_PLATFORM_PROFILE_H_*/

0 commit comments

Comments
 (0)