thermal/core/power_allocator: enable user to change the power budget

Message ID 20230801083025.13912-1-di.shen@unisoc.com
State New
Headers
Series thermal/core/power_allocator: enable user to change the power budget |

Commit Message

Di Shen Aug. 1, 2023, 8:30 a.m. UTC
  For IPA, the total power budget of all cooling actors can be
calculated with the input of thermal zone temperature in PID
controller. However, in some scenarios, if user wants more
restrictive temperature control, which means lower power allocated
for all cooling devices, the power budget should be changed to be
a lower one.

In order to allow users to change the power budget value, this
patch adds a variable of power_budget. Users can write a power
budget value through the sysfs node, and then the governor will
choose the lower value (the more strict one) compared with the
power budget calculated by PID controller to be allocated for
cooling deivces.

Signed-off-by: Di Shen <di.shen@unisoc.com>
---
 drivers/thermal/gov_power_allocator.c |  3 +++
 drivers/thermal/thermal_sysfs.c       | 33 +++++++++++++++++++++++++++
 include/linux/thermal.h               |  6 +++++
 3 files changed, 42 insertions(+)
  

Patch

diff --git a/drivers/thermal/gov_power_allocator.c b/drivers/thermal/gov_power_allocator.c
index 8642f1096b91..c839e8277eab 100644
--- a/drivers/thermal/gov_power_allocator.c
+++ b/drivers/thermal/gov_power_allocator.c
@@ -460,6 +460,8 @@  static int allocate_power(struct thermal_zone_device *tz,
 	}
 
 	power_range = pid_controller(tz, control_temp, max_allocatable_power);
+	if (tz->tzp->power_budget && tz->tzp->power_budget < power_range)
+		power_range = tz->tzp->power_budget;
 
 	divvy_up_power(weighted_req_power, max_power, num_actors,
 		       total_weighted_req_power, power_range, granted_power,
@@ -665,6 +667,7 @@  static int power_allocator_bind(struct thermal_zone_device *tz)
 					       trip.temperature);
 	}
 
+	tz->tzp->power_budget = 0;
 	reset_pid_controller(params);
 
 	tz->governor_data = params;
diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c
index 6c20c9f90a05..85dd4194b5c7 100644
--- a/drivers/thermal/thermal_sysfs.c
+++ b/drivers/thermal/thermal_sysfs.c
@@ -326,6 +326,37 @@  sustainable_power_store(struct device *dev, struct device_attribute *devattr,
 	return count;
 }
 
+static ssize_t
+power_budget_show(struct device *dev, struct device_attribute *devattr, char *buf)
+{
+	struct thermal_zone_device *tz = to_thermal_zone(dev);
+
+	if (tz->tzp)
+		return sprintf(buf, "%u\n", tz->tzp->power_budget);
+	else
+		return -EIO;
+}
+
+static ssize_t
+power_budget_store(struct device *dev, struct device_attribute *devattr,
+		       const char *buf, size_t count)
+{
+	struct thermal_zone_device *tz = to_thermal_zone(dev);
+	u32 power_budget;
+
+	if (!tz->tzp)
+		return -EIO;
+
+	if (kstrtou32(buf, 10, &power_budget))
+		return -EINVAL;
+
+	tz->tzp->power_budget = power_budget;
+
+	__thermal_zone_device_update(tz, THERMAL_EVENT_UNSPECIFIED);
+
+	return count;
+}
+
 #define create_s32_tzp_attr(name)					\
 	static ssize_t							\
 	name##_show(struct device *dev, struct device_attribute *devattr, \
@@ -377,6 +408,7 @@  static DEVICE_ATTR_RO(temp);
 static DEVICE_ATTR_RW(policy);
 static DEVICE_ATTR_RO(available_policies);
 static DEVICE_ATTR_RW(sustainable_power);
+static DEVICE_ATTR_RW(power_budget);
 
 /* These thermal zone device attributes are created based on conditions */
 static DEVICE_ATTR_RW(mode);
@@ -391,6 +423,7 @@  static struct attribute *thermal_zone_dev_attrs[] = {
 	&dev_attr_policy.attr,
 	&dev_attr_available_policies.attr,
 	&dev_attr_sustainable_power.attr,
+	&dev_attr_power_budget.attr,
 	&dev_attr_k_po.attr,
 	&dev_attr_k_pu.attr,
 	&dev_attr_k_i.attr,
diff --git a/include/linux/thermal.h b/include/linux/thermal.h
index 87837094d549..580a1e786037 100644
--- a/include/linux/thermal.h
+++ b/include/linux/thermal.h
@@ -224,6 +224,12 @@  struct thermal_zone_params {
 	 */
 	u32 sustainable_power;
 
+	/*
+	 * A power budget needed to compare with power_range of power allocator
+	 * in mW
+	 */
+	u32 power_budget;
+
 	/*
 	 * Proportional parameter of the PID controller when
 	 * overshooting (i.e., when temperature is below the target)