From patchwork Mon Dec 16 14:06:20 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lukasz Luba X-Patchwork-Id: 11294295 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D15F0930 for ; Mon, 16 Dec 2019 14:06:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B9988206B7 for ; Mon, 16 Dec 2019 14:06:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728040AbfLPOGs (ORCPT ); Mon, 16 Dec 2019 09:06:48 -0500 Received: from foss.arm.com ([217.140.110.172]:56568 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727952AbfLPOGr (ORCPT ); Mon, 16 Dec 2019 09:06:47 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id CA8001045; Mon, 16 Dec 2019 06:06:46 -0800 (PST) Received: from e123648.arm.com (unknown [10.37.12.145]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 894E93F718; Mon, 16 Dec 2019 06:06:44 -0800 (PST) From: lukasz.luba@arm.com To: linux-kernel@vger.kernel.org, rui.zhang@intel.com, daniel.lezcano@linaro.org, linux-pm@vger.kernel.org, linux-doc@vger.kernel.org Cc: amit.kucheria@verdurent.com, corbet@lwn.net, lukasz.luba@arm.com, dietmar.eggemann@arm.com Subject: [PATCH 1/3] docs: thermal: Add bind, unbind information together with trip point Date: Mon, 16 Dec 2019 14:06:20 +0000 Message-Id: <20191216140622.25467-2-lukasz.luba@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191216140622.25467-1-lukasz.luba@arm.com> References: <20191216140622.25467-1-lukasz.luba@arm.com> Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org From: Lukasz Luba Update sysfs interface documentation about new attributes in the cooling device folder: bind and unbind. It also updates the changed cooling device associated trip point interface extending it by information about writable values. Signed-off-by: Lukasz Luba --- .../driver-api/thermal/sysfs-api.rst | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/Documentation/driver-api/thermal/sysfs-api.rst b/Documentation/driver-api/thermal/sysfs-api.rst index b40b1f839148..605f0d9ffe71 100644 --- a/Documentation/driver-api/thermal/sysfs-api.rst +++ b/Documentation/driver-api/thermal/sysfs-api.rst @@ -407,6 +407,8 @@ Thermal cooling device sys I/F, created once it's registered:: |---stats/time_in_state_ms: Time (msec) spent in various cooling states |---stats/total_trans: Total number of times cooling state is changed |---stats/trans_table: Cooing state transition table + |---bind_tz: Interface for binding cooling device to thermal zone + |---unbind_tz: Interface for unbinding cooling device from thermal zone Then next two dynamic attributes are created/removed in pairs. They represent @@ -507,9 +509,12 @@ available_policies `cdev[0-*]_trip_point` The trip point in this thermal zone which `cdev[0-*]` is associated with; -1 means the cooling device is not associated with any trip - point. + point. Writing a value (a proper integer) will set new trip point + to this cooling device. The value -1 will cause the cooling device + is not associated to any trip point and can be unbind from the + thermal zone. - RO, Optional + RW, Optional `cdev[0-*]_weight` The influence of `cdev[0-*]` in this thermal zone. This value @@ -637,6 +642,27 @@ max_state RO, Required +bind_tz + Writing the thermal zone name binds this cooling device with the + specified thermal zone. A new dynamic attributes 'cdev*' will be + created inside the thermal zone directory. + The proper thermal zone name can be from read the attribute: + /sys/class/thermal/thermal_zone*/type + The default (-1) trip point will be set into the cooling instance, + which should be updated later using 'cdev*_trip_point' interface. + + WO, Required + +unbind_tz + Writing the thermal zone name unbinds this cooling device from + the specified thermal zone. The cooling instance needs to set -1 + into the associated cooling device instance 'cdev*_trip_point' + first. + The proper thermal zone name can be read from the attribute: + /sys/class/thermal/thermal_zone*/type + + WO, Required + cur_state The current cooling state of this cooling device. The value can any integer numbers between 0 and max_state: From patchwork Mon Dec 16 14:06:21 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lukasz Luba X-Patchwork-Id: 11294293 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B2DAA13B6 for ; Mon, 16 Dec 2019 14:06:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9B1BB2072B for ; Mon, 16 Dec 2019 14:06:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728077AbfLPOGv (ORCPT ); Mon, 16 Dec 2019 09:06:51 -0500 Received: from foss.arm.com ([217.140.110.172]:56580 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727952AbfLPOGu (ORCPT ); Mon, 16 Dec 2019 09:06:50 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 77A271FB; Mon, 16 Dec 2019 06:06:49 -0800 (PST) Received: from e123648.arm.com (unknown [10.37.12.145]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 36CB23F718; Mon, 16 Dec 2019 06:06:47 -0800 (PST) From: lukasz.luba@arm.com To: linux-kernel@vger.kernel.org, rui.zhang@intel.com, daniel.lezcano@linaro.org, linux-pm@vger.kernel.org, linux-doc@vger.kernel.org Cc: amit.kucheria@verdurent.com, corbet@lwn.net, lukasz.luba@arm.com, dietmar.eggemann@arm.com Subject: [PATCH 2/3] thermal: Make cooling device trip point writable from sysfs Date: Mon, 16 Dec 2019 14:06:21 +0000 Message-Id: <20191216140622.25467-3-lukasz.luba@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191216140622.25467-1-lukasz.luba@arm.com> References: <20191216140622.25467-1-lukasz.luba@arm.com> Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org From: Lukasz Luba Make it possible to change trip point for the cooling device instance in the thermal zone. It would be helpful in case when cooling devices can by bind to thermal zones using sysfs interface. A proper trip point can be chosen for a cooling device by: echo 2 > /sys/class/thermal/thermal_zoneX/cdev_Z_trip_point It is also possible to unpin cooling device from trip point: echo -1 > /sys/class/thermal/thermal_zoneX/cdev_Z_trip_point Signed-off-by: Lukasz Luba --- drivers/thermal/thermal_core.c | 3 ++- drivers/thermal/thermal_core.h | 2 ++ drivers/thermal/thermal_sysfs.c | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c index 9a321dc548c8..aa66a73e9fa5 100644 --- a/drivers/thermal/thermal_core.c +++ b/drivers/thermal/thermal_core.c @@ -740,8 +740,9 @@ int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz, sprintf(dev->attr_name, "cdev%d_trip_point", dev->id); sysfs_attr_init(&dev->attr.attr); dev->attr.attr.name = dev->attr_name; - dev->attr.attr.mode = 0444; + dev->attr.attr.mode = 0644; dev->attr.show = trip_point_show; + dev->attr.store = trip_point_store; result = device_create_file(&tz->device, &dev->attr); if (result) goto remove_symbol_link; diff --git a/drivers/thermal/thermal_core.h b/drivers/thermal/thermal_core.h index 207b0cda70da..203cc7a63706 100644 --- a/drivers/thermal/thermal_core.h +++ b/drivers/thermal/thermal_core.h @@ -76,6 +76,8 @@ void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device *); void thermal_cooling_device_destroy_sysfs(struct thermal_cooling_device *cdev); /* used only at binding time */ ssize_t trip_point_show(struct device *, struct device_attribute *, char *); +ssize_t trip_point_store(struct device *, struct device_attribute *, + const char *, size_t); ssize_t weight_show(struct device *, struct device_attribute *, char *); ssize_t weight_store(struct device *, struct device_attribute *, const char *, size_t); diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c index aa99edb4dff7..80c8bae6dd1c 100644 --- a/drivers/thermal/thermal_sysfs.c +++ b/drivers/thermal/thermal_sysfs.c @@ -977,6 +977,26 @@ trip_point_show(struct device *dev, struct device_attribute *attr, char *buf) return sprintf(buf, "%d\n", instance->trip); } +ssize_t trip_point_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct thermal_instance *instance; + int ret, trip; + + ret = kstrtoint(buf, 0, &trip); + if (ret) + return ret; + + instance = container_of(attr, struct thermal_instance, attr); + + if (trip >= instance->tz->trips || trip < THERMAL_TRIPS_NONE) + return -EINVAL; + + instance->trip = trip; + + return count; +} + ssize_t weight_show(struct device *dev, struct device_attribute *attr, char *buf) { From patchwork Mon Dec 16 14:06:22 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lukasz Luba X-Patchwork-Id: 11294291 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 38B23930 for ; Mon, 16 Dec 2019 14:06:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 203AA20726 for ; Mon, 16 Dec 2019 14:06:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728106AbfLPOGx (ORCPT ); Mon, 16 Dec 2019 09:06:53 -0500 Received: from foss.arm.com ([217.140.110.172]:56596 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728103AbfLPOGw (ORCPT ); Mon, 16 Dec 2019 09:06:52 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 251EF1FB; Mon, 16 Dec 2019 06:06:52 -0800 (PST) Received: from e123648.arm.com (unknown [10.37.12.145]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id D86363F718; Mon, 16 Dec 2019 06:06:49 -0800 (PST) From: lukasz.luba@arm.com To: linux-kernel@vger.kernel.org, rui.zhang@intel.com, daniel.lezcano@linaro.org, linux-pm@vger.kernel.org, linux-doc@vger.kernel.org Cc: amit.kucheria@verdurent.com, corbet@lwn.net, lukasz.luba@arm.com, dietmar.eggemann@arm.com Subject: [PATCH 3/3] thermal: Add sysfs binding for cooling device and thermal zone Date: Mon, 16 Dec 2019 14:06:22 +0000 Message-Id: <20191216140622.25467-4-lukasz.luba@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20191216140622.25467-1-lukasz.luba@arm.com> References: <20191216140622.25467-1-lukasz.luba@arm.com> Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org From: Lukasz Luba Make it possible to bind from userspace a cooling device to an existing thermal zone. It adds more flexibility in addition to static device tree definitions. There is also a code for changing trip point connected to cooling device instance in the thermal zone. In order to bind a device to a zone, first the proper thermal zone name must be checked from file: cat /sys/class/thermal/thermal_zoneX/type Then that name must be set into cooling device 'bind_tz' file: echo 'gpu-thermal' > /sys/class/thermal/cooling_deviceY/bind_tz Next a proper trip point must be chosen for this cooling device: echo 2 > /sys/class/thermal/thermal_zoneX/cdev_Z_trip_point To unbind, first set -1 to connected trip point: echo -1 > /sys/class/thermal/thermal_zoneX/cdev_Z_trip_point Then unbind the thermal zone from the cooling device: echo 'gpu-thermal' > /sys/class/thermal/cooling_deviceY/unbind_tz Signed-off-by: Lukasz Luba --- drivers/thermal/thermal_sysfs.c | 57 +++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/drivers/thermal/thermal_sysfs.c b/drivers/thermal/thermal_sysfs.c index 80c8bae6dd1c..473449b41d55 100644 --- a/drivers/thermal/thermal_sysfs.c +++ b/drivers/thermal/thermal_sysfs.c @@ -722,15 +722,72 @@ cur_state_store(struct device *dev, struct device_attribute *attr, return result ? result : count; } +static ssize_t +bind_tz_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct thermal_cooling_device *cdev = to_cooling_device(dev); + struct thermal_zone_device *tz; + char *orig, *name; + int res = 0; + + orig = kstrndup(buf, count, GFP_KERNEL); + if (!orig) + return -ENOMEM; + + name = strstrip(orig); + + tz = thermal_zone_get_zone_by_name(name); + if (IS_ERR_OR_NULL(tz)) + return -EINVAL; + + res = thermal_zone_bind_cooling_device(tz, THERMAL_TRIPS_NONE, cdev, + THERMAL_NO_LIMIT, + THERMAL_NO_LIMIT, + THERMAL_WEIGHT_DEFAULT); + + kfree(orig); + return res ? res : count; +} + +static ssize_t +unbind_tz_store(struct device *dev, struct device_attribute *attr, + const char *buf, size_t count) +{ + struct thermal_cooling_device *cdev = to_cooling_device(dev); + struct thermal_zone_device *tz; + char *name, *orig; + int res = 0; + + orig = kstrndup(buf, count, GFP_KERNEL); + if (!orig) + return -ENOMEM; + + name = strstrip(orig); + + tz = thermal_zone_get_zone_by_name(name); + if (IS_ERR_OR_NULL(tz)) + return -EINVAL; + + res = thermal_zone_unbind_cooling_device(tz, THERMAL_TRIPS_NONE, cdev); + + kfree(orig); + return res ? res : count; +} + static struct device_attribute dev_attr_cdev_type = __ATTR(type, 0444, cdev_type_show, NULL); static DEVICE_ATTR_RO(max_state); static DEVICE_ATTR_RW(cur_state); +static DEVICE_ATTR_WO(bind_tz); +static DEVICE_ATTR_WO(unbind_tz); static struct attribute *cooling_device_attrs[] = { &dev_attr_cdev_type.attr, &dev_attr_max_state.attr, &dev_attr_cur_state.attr, + &dev_attr_bind_tz.attr, + &dev_attr_unbind_tz.attr, NULL, };