Skip to contentSkip to conversation

ACPI / fan: Add fine grain control

PATCH Superseded Srinivas Pandruvada 2 participants 9 messages Feb 11, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v4 cover ACPI / fan: Add fine grain control

This series of changes adds fine grain control for fans. First 3 patches are clean up and preparation patches.

v4
Addressed review comments:
- Creation of attributes to separate file
- Handle error in get/set callbacks
- Fix commit description for current state
- Remove casting to int by changing fif struct
- Change fan_get_fps() to acpi_fan_get_fst()
- Fallback to old method for invalid control value
- Remove logic to take reminder to adjust to 100
- Add else if for step size sanity checks

v3
Added fine_grain_control attribute.
v2-update
Change log is missed for v2.
v2
Fix for build issue as reported by Reported-by: kernel test robot <[email protected]>

Srinivas Pandruvada (6):
ACPI / fan: Fix error reporting to user space
ACPI / fan: Separate file for attributes creation ACPI / fan: Optimize struct acpi_fan_fif
ACPI / fan: Properly handle fine grain control
ACPI / fan: Add additional attributes for fine grain control Documentation/admin-guide/acpi: Add documentation for fine grain
control

 .../acpi/fan_performance_states.rst           |  28 +++
 drivers/acpi/Makefile                         |   3 +
 drivers/acpi/fan.h                            |  44 ++++
 drivers/acpi/fan_attr.c                       | 137 ++++++++++++
 drivers/acpi/{fan.c => fan_core.c}            | 204 ++++++++----------
 5 files changed, 298 insertions(+), 118 deletions(-)
 create mode 100644 drivers/acpi/fan_attr.c
 rename drivers/acpi/{fan.c => fan_core.c} (75%)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v4 1/6 ACPI / fan: Fix error reporting to user space

When user get/set cur_state fails, it should be some negative error value instead of whatever returned by acpi_evaluate_object() or from acpi_execute_simple_method(). The return value from these apis is some positive values greater than 0. For example if AE_NOT_FOUND is returned it will be "5".

In other ACPI drivers, -ENODEV is returned when ACPI_FAILURE(status) is true. Do the same thing here for thermal sysfs callbacks for get and set for failures.

---
 drivers/acpi/fan.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/acpi/fan.c +2 -2
index 5cd0ceb50bc8..098d64568d6d 100644
@@ -107,7 +107,7 @@ static int fan_get_state_acpi4(struct acpi_device *device, unsigned long *state)
 	status = acpi_evaluate_object(device->handle, "_FST", NULL, &buffer);
 	if (ACPI_FAILURE(status)) {
 		dev_err(&device->dev, "Get fan state failed\n");
-		return status;
+		return -ENODEV;
 	}
 
 	obj = buffer.pointer;
@@ -195,7 +195,7 @@ static int fan_set_state_acpi4(struct acpi_device *device, unsigned long state)
 					    fan->fps[state].control);
 	if (ACPI_FAILURE(status)) {
 		dev_dbg(&device->dev, "Failed to set state by _FSL\n");
-		return status;
+		return -ENODEV;
 	}
 
 	return 0;
Signature · 1 line
2.34.1
#2 lore raw

PATCH v4 2/6 ACPI / fan: Separate file for attributes creation

In reply to Srinivas Pandruvada

Move the functionality of creation of sysfs attributes under acpi device to a new file fan_attr.c. This cleans up the core fan code, which just use thermal sysfs interface. The original fan.c is renamed to fan_core.c.

No functional changes are expected.

---
 drivers/acpi/Makefile              |  3 +
 drivers/acpi/fan.h                 | 35 +++++++++++
 drivers/acpi/fan_attr.c            | 86 ++++++++++++++++++++++++++
 drivers/acpi/{fan.c => fan_core.c} | 98 +++---------------------------
 4 files changed, 133 insertions(+), 89 deletions(-)
 create mode 100644 drivers/acpi/fan_attr.c
 rename drivers/acpi/{fan.c => fan_core.c} (80%)
drivers/acpi/Makefile +3 -0
index bb757148e7ba..b5a8d3e00a52 100644
@@ -81,6 +81,9 @@ obj-$(CONFIG_ACPI_AC) 		+= ac.o
 obj-$(CONFIG_ACPI_BUTTON)	+= button.o
 obj-$(CONFIG_ACPI_TINY_POWER_BUTTON)	+= tiny-power-button.o
 obj-$(CONFIG_ACPI_FAN)		+= fan.o
+fan-objs			:= fan_core.o
+fan-objs			+= fan_attr.o
+
 obj-$(CONFIG_ACPI_VIDEO)	+= video.o
 obj-$(CONFIG_ACPI_TAD)		+= acpi_tad.o
 obj-$(CONFIG_ACPI_PCI_SLOT)	+= pci_slot.o
drivers/acpi/fan.h +35 -0
index dd9bb8ca2244..36c5e1a57094 100644
@@ -6,9 +6,44 @@
  *
  * Add new device IDs before the generic ACPI fan one.
  */
+
+#ifndef _ACPI_FAN_H_
+#define _ACPI_FAN_H_
+
 #define ACPI_FAN_DEVICE_IDS	\
 	{"INT3404", }, /* Fan */ \
 	{"INTC1044", }, /* Fan for Tiger Lake generation */ \
 	{"INTC1048", }, /* Fan for Alder Lake generation */ \
 	{"INTC10A2", }, /* Fan for Raptor Lake generation */ \
 	{"PNP0C0B", } /* Generic ACPI fan */
+
+#define ACPI_FPS_NAME_LEN	20
+
+struct acpi_fan_fps {
+	u64 control;
+	u64 trip_point;
+	u64 speed;
+	u64 noise_level;
+	u64 power;
+	char name[ACPI_FPS_NAME_LEN];
+	struct device_attribute dev_attr;
+};
+
+struct acpi_fan_fif {
+	u64 revision;
+	u64 fine_grain_ctrl;
+	u64 step_size;
+	u64 low_speed_notification;
+};
+
+struct acpi_fan {
+	bool acpi4;
+	struct acpi_fan_fif fif;
+	struct acpi_fan_fps *fps;
+	int fps_count;
+	struct thermal_cooling_device *cdev;
+};
+
+int acpi_fan_create_attributes(struct acpi_device *device);
+void acpi_fan_delete_attributes(struct acpi_device *device);
+#endif
drivers/acpi/fan_attr.c new file +86 -0

86 lines not on this page · raw message

drivers/acpi/fan_core.c renamed from drivers/acpi/fan.c +9 -89

143 lines not on this page · raw message

Signature · 1 line
2.34.1
#3 lore raw

PATCH v4 3/6 ACPI / fan: Optimize struct acpi_fan_fif

In reply to Srinivas Pandruvada

We don't need u64 to store the information about _FIF. There are two booleans (fine_grain_ctrl and low_speed_notification) and one field step_size which can take value from 1-9. There are no internal users of revision field. So convert all fields to u8, by not directly extracting the _FIF info the struct. Use an intermediate buffer to extract and assign.

This will help to do u32 math using these fields. No functional changes are expected.

---
 drivers/acpi/fan.h      | 8 ++++----
 drivers/acpi/fan_core.c | 8 +++++++-
 2 files changed, 11 insertions(+), 5 deletions(-)
drivers/acpi/fan.h +4 -4
index 36c5e1a57094..6cbb4b028da0 100644
@@ -30,10 +30,10 @@ struct acpi_fan_fps {
 };
 
 struct acpi_fan_fif {
-	u64 revision;
-	u64 fine_grain_ctrl;
-	u64 step_size;
-	u64 low_speed_notification;
+	u8 revision;
+	u8 fine_grain_ctrl;
+	u8 step_size;
+	u8 low_speed_notification;
 };
 
 struct acpi_fan {
drivers/acpi/fan_core.c +7 -1
index 9f8e68403fad..484cee0fb13e 100644
@@ -211,7 +211,8 @@ static int acpi_fan_get_fif(struct acpi_device *device)
 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
 	struct acpi_fan *fan = acpi_driver_data(device);
 	struct acpi_buffer format = { sizeof("NNNN"), "NNNN" };
-	struct acpi_buffer fif = { sizeof(fan->fif), &fan->fif };
+	u64 fields[4];
+	struct acpi_buffer fif = { sizeof(fields), fields };
 	union acpi_object *obj;
 	acpi_status status;
 
@@ -232,6 +233,11 @@ static int acpi_fan_get_fif(struct acpi_device *device)
 		status = -EINVAL;
 	}
 
+	fan->fif.revision = fields[0];
+	fan->fif.fine_grain_ctrl = fields[1];
+	fan->fif.step_size = fields[2];
+	fan->fif.low_speed_notification = fields[3];
+
 err:
 	kfree(obj);
 	return status;
Signature · 1 line
2.34.1
#4 lore raw

PATCH v4 4/6 ACPI / fan: Properly handle fine grain control

In reply to Srinivas Pandruvada

When _FIF object specifies support for fine grain control, then fan speed can be set from 0 to 100% with the recommended minimum "step size" via _FSL object. Here the control value doesn't need to match any value from _FPS object.

Currently we have a simple solution implemented which just pick maximum control value from _FPS to display the actual state, but this is not optimal when there is a big window between two control values in _FPS. Also there is no way to set to any speed which doesn't match control values in _FPS. The system firmware can start the fan at speed which doesn't match any control value.

To support fine grain control (when supported) via thermal sysfs:
- cooling device max state is not _FPS state count but it will be 100 / _FIF.step_size
Step size can be from 1 to 9.
- cooling device current state is _FST.control / _FIF.step_size
- cooling device set state will set the control value cdev.curr_state * _FIF.step_size plus any adjustment for 100%. By the spec, when control value do not sum to 100% because of _FIF.step_size, OSPM may select an appropriate ending Level increment to reach 100%.

There is no rounding during calculation. For example if step size is 6:
thermal sysfs cooling device max_state = 100/6 = 16
So user can set any value from 0-16.

If the system boots with a _FST.control which is not multiples of step_size, the thermal sysfs cur_state will be based on the range. For example for step size = 6:
_FST.control thermal sysfs cur_state
------------------------------------------------
0-5 0
6-11 1
..
..
90-95 15
96-100 16

While setting the _FST.control, the compensation will be at the last step for cur_state = 16, which will set the _FST.control to 100.

---
 drivers/acpi/fan.h      |  6 +++
 drivers/acpi/fan_core.c | 94 +++++++++++++++++++++++++++++------------
 2 files changed, 74 insertions(+), 26 deletions(-)
drivers/acpi/fan.h +6 -0
index 6cbb4b028da0..4c01be2e3b77 100644
@@ -36,6 +36,12 @@ struct acpi_fan_fif {
 	u8 low_speed_notification;
 };
 
+struct acpi_fan_fst {
+	u64 revision;
+	u64 control;
+	u64 speed;
+};
+
 struct acpi_fan {
 	bool acpi4;
 	struct acpi_fan_fif fif;
drivers/acpi/fan_core.c +68 -26

141 lines not on this page · raw message

Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH v4 4/6] ACPI / fan: Properly handle fine grain control

I love your patch! Yet something to improve:

Hi Srinivas,

I love your patch! Yet something to improve:

[auto build test ERROR on rafael-pm/linux-next]
[also build test ERROR on linus/master v5.17-rc3]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Srinivas-Pandruvada/ACPI-fan-Add-fine-grain-control/20220212-001118
base:   https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git linux-next
config: i386-defconfig (https://download.01.org/0day-ci/archive/20220212/202202120604.Qxt5VNqx-lkp(a)intel.com/config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0

reproduce (this is a W=1 build):
# https://github.com/0day-ci/linux/commit/367e082b5f2d1c640b906682cffcb7195e722f26
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Srinivas-Pandruvada/ACPI-fan-Add-fine-grain-control/20220212-001118 git checkout 367e082b5f2d1c640b906682cffcb7195e722f26
# save the config file to linux build tree
mkdir build_dir
make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

ld: drivers/acpi/fan_core.o: in function `fan_get_cur_state':

>> fan_core.c:(.text+0x2f9): undefined reference to `__udivdi3'

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#6 lore raw

Re: [PATCH v4 4/6] ACPI / fan: Properly handle fine grain control

In reply to Srinivas Pandruvada
I love your patch! Yet something to improve:

Hi Srinivas,

I love your patch! Yet something to improve:

[auto build test ERROR on rafael-pm/linux-next]
[also build test ERROR on linus/master v5.17-rc3 next-20220211] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Srinivas-Pandruvada/ACPI-fan-Add-fine-grain-control/20220212-001118
base:   https://git.kernel.org/pub/scm/linux/kernel/git/rafael/linux-pm.git linux-next
config: i386-randconfig-a003 (https://download.01.org/0day-ci/archive/20220212/202202120622.lPBqcxi6-lkp(a)intel.com/config)
compiler: gcc-9 (Debian 9.3.0-22) 9.3.0

reproduce (this is a W=1 build):
# https://github.com/0day-ci/linux/commit/367e082b5f2d1c640b906682cffcb7195e722f26
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Srinivas-Pandruvada/ACPI-fan-Add-fine-grain-control/20220212-001118 git checkout 367e082b5f2d1c640b906682cffcb7195e722f26
# save the config file to linux build tree
mkdir build_dir
make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

ld: drivers/acpi/fan_core.o: in function `fan_get_state_acpi4':

>> drivers/acpi/fan_core.c:126: undefined reference to `__udivdi3'

vim +126 drivers/acpi/fan_core.c

108
109 static int fan_get_state_acpi4(struct acpi_device *device, unsigned long *state)
110 {
111 struct acpi_fan *fan = acpi_driver_data(device);
112 struct acpi_fan_fst fst;
113 int status, i;
114
115 status = acpi_fan_get_fst(device, &fst);
116 if (status)
117 return status;
118
119 if (fan->fif.fine_grain_ctrl) {
120 /* This control should be same what we set using _FSL by spec */
121 if (fst.control > 100) {
122 dev_dbg(&device->dev, "Invalid control value returned\n");
123 goto match_fps;
124 }
125

 > 126			*state = fst.control / fan->fif.step_size;

127 return 0;
128 }
129
130 match_fps:
131 for (i = 0; i < fan->fps_count; i++) {
132 if (fst.control == fan->fps[i].control)
133 break;
134 }
135 if (i == fan->fps_count) {
136 dev_dbg(&device->dev, "Invalid control value returned\n");
137 return -EINVAL;
138 }
139
140 *state = i;
141
142 return status;
143 }
144

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#7 lore raw

PATCH v4 5/6 ACPI / fan: Add additional attributes for fine grain control

In reply to Srinivas Pandruvada

Add additional attributes, which helps in implementing algorithm in the user space to optimize fan control. These attributes are presented in the same directory as the existing performance state attributes.

Additional attributes:

1. Support of fine grain control
Publish support of presence of fine grain control so that fan speed can be tuned correctly. This attribute is called "fine_grain_control".

2. fan speed
Publish the actual fan rpm in sysfs. Knowing fan rpm is helpful to reduce noise level and use passive control instead. Also fan performance may not be same over time, so the same control value may not be enough to run the fan at a speed. So a feedback value of speed is helpful. This sysfs attribute is called "fan_speed_rpm".

---
 drivers/acpi/fan.h      |  3 +++
 drivers/acpi/fan_attr.c | 55 +++++++++++++++++++++++++++++++++++++++--
 drivers/acpi/fan_core.c |  2 +-
 3 files changed, 57 insertions(+), 3 deletions(-)
drivers/acpi/fan.h +3 -0
index 4c01be2e3b77..44728529a5b6 100644
@@ -48,8 +48,11 @@ struct acpi_fan {
 	struct acpi_fan_fps *fps;
 	int fps_count;
 	struct thermal_cooling_device *cdev;
+	struct device_attribute fst_speed;
+	struct device_attribute fine_grain_control;
 };
 
+int acpi_fan_get_fst(struct acpi_device *device, struct acpi_fan_fst *fst);
 int acpi_fan_create_attributes(struct acpi_device *device);
 void acpi_fan_delete_attributes(struct acpi_device *device);
 #endif
drivers/acpi/fan_attr.c +53 -2

77 lines not on this page · raw message

drivers/acpi/fan_core.c +1 -1
index 495f59222881..f89e43b89511 100644
@@ -75,7 +75,7 @@ static int fan_get_max_state(struct thermal_cooling_device *cdev, unsigned long
 	return 0;
 }
 
-static int acpi_fan_get_fst(struct acpi_device *device, struct acpi_fan_fst *fst)
+int acpi_fan_get_fst(struct acpi_device *device, struct acpi_fan_fst *fst)
 {
 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
 	union acpi_object *obj;
Signature · 1 line
2.34.1
#8 lore raw

PATCH v4 6/6 Documentation/admin-guide/acpi: Add documentation for fine grain control

In reply to Srinivas Pandruvada

Add documentation for the newly added attributes:
fine_grain_control
fan_speed_rpm

---
 .../acpi/fan_performance_states.rst           | 28 +++++++++++++++++++
 1 file changed, 28 insertions(+)
Documentation/admin-guide/acpi/fan_performance_states.rst +28 -0
index 98fe5c333121..b9e4b4d146c1 100644
@@ -60,3 +60,31 @@ For example::
 
 When a given field is not populated or its value provided by the platform
 firmware is invalid, the "not-defined" string is shown instead of the value.
+
+ACPI Fan Fine Grain Control
+=============================
+
+When _FIF object specifies support for fine grain control, then fan speed
+can be set from 0 to 100% with the recommended minimum "step size" via
+_FSL object. User can adjust fan speed using thermal sysfs cooling device.
+
+Here use can look at fan performance states for a reference speed (speed_rpm)
+and set it by changing cooling device cur_state. If the fine grain control
+is supported then user can also adjust to some other speeds which are
+not defined in the performance states.
+
+The support of fine grain control is presented via sysfs attribute
+"fine_grain_control". If fine grain control is present, this attribute
+will show "1" otherwise "0".
+
+This sysfs attribute is presented in the same directory as performance states.
+
+ACPI Fan Performance Feedback
+=============================
+
+The optional _FST object provides status information for the fan device.
+This includes field to provide current fan speed in revolutions per minute
+at which the fan is rotating.
+
+This speed is presented in the sysfs using the attribute "fan_speed_rpm",
+in the same directory as performance states.
Signature · 1 line
2.34.1