From patchwork Mon Nov 23 21:42:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 330684 Delivered-To: patch@linaro.org Received: by 2002:a17:907:2110:0:0:0:0 with SMTP id qn16csp3859598ejb; Mon, 23 Nov 2020 13:44:09 -0800 (PST) X-Google-Smtp-Source: ABdhPJzb/QSsRGBIHQ8JSC0J6n8IOX+DSvP4AX4pBJQvlHaP/kthWj+06afcIty2Z02WLGoB5PuU X-Received: by 2002:a17:906:145b:: with SMTP id q27mr1460604ejc.86.1606167849264; Mon, 23 Nov 2020 13:44:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606167849; cv=none; d=google.com; s=arc-20160816; b=u3N12Sm5xlFH+lYfQKbcM6M4QCGQr1kEN792s5QrLOZ2njXhunAo/aiAxqXF1hqEY0 CRUzQLfYBvHtg5Aid7RH95VKe4ku2uDk+yVb35gpBg/lHoAgk0Ob4Ef1Me3sUoIIbQPb t0y0K7LamckNBNUL3OlMZTRYAWX/2pCBn2tC0Z55TOfIdJ3IglK+XDg8earClpB9zR1N EVOySMxEC4ofnvJyzOvAnmYaHbl4CiinB/HS3iD/mNiXd9GqOBQzb94SL3tSwEcrw5Wi y7R4mChjnDcApewDpBBj1IqczI3Qlh3G7U7PZNkdShdI0c5X8XatR+pwUgLOlGunwGNP rvjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=AN+diq6krUutxY4s1BjD5o/2cLxtj1QMMKMF7O1rYVo=; b=niyk4hu+QSsfRCKZsgqSsmPlyZ+OW3GJvGuE5oY/tUemdJBHAnhofqjoeFATuRqgaH sfYlkuX1XKjqorgwAy6VcZV//XqZ2sXBg1EfDDrRI/SjuJ3YM7nd9f22qazpCH8059JA GRwull8DS0PuUsLcSFw2wnhqwtat/pztoQq1a/RUCJKZKq6viY4L/zN6JdLZIBy0gJfe O3DT29EpeB3CtENjkY9Dxt060C1/Mu0PFO2HqQ6G2SmJjYDZJGqidmxvtSPMr2hh5qdE khR5J9yaQLR6uAuqpZUIaOfeSiAw8Unv2iaciKGjgn7kUa7XGAo9cI4SSX1Y81zKaLTz 5DOA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=TMunjAZv; spf=pass (google.com: domain of linux-pm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-pm-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id be11si7076134edb.164.2020.11.23.13.44.09; Mon, 23 Nov 2020 13:44:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-pm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=TMunjAZv; spf=pass (google.com: domain of linux-pm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-pm-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731487AbgKWVmX (ORCPT + 8 others); Mon, 23 Nov 2020 16:42:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56826 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731430AbgKWVmW (ORCPT ); Mon, 23 Nov 2020 16:42:22 -0500 Received: from mail-wr1-x442.google.com (mail-wr1-x442.google.com [IPv6:2a00:1450:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1127CC061A4D for ; Mon, 23 Nov 2020 13:42:22 -0800 (PST) Received: by mail-wr1-x442.google.com with SMTP id r17so20333684wrw.1 for ; Mon, 23 Nov 2020 13:42:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=AN+diq6krUutxY4s1BjD5o/2cLxtj1QMMKMF7O1rYVo=; b=TMunjAZvezqNRjxxbJ02gu00nqa+UL1Sr0P24qsBWwWQit+/eyOfS4Uk25FGQsV5O7 RI6hgMN7bRBIDD8bb38R0R8jj61Se6x6QmJSJnpCFMlhY04T9vwpNS+z8mp5noXKYdOh br1EMUUeTwBaR9UM1yiG59VyraUTIp5CHtXPa0k4LjZYoqtNG4wNm/bC1PJmQ+PXU/43 2EvGCjpSWkmr+v6sTAvnAUS2xE1ece7nCW6mW6v7o7oK+vggLzyf9qk9eh233vn41Jl+ QDZoT+hs4PcW8iqXR2qip0LQpWjQpVBFw1TY8nI0E7Z0r6rqU5jwazy+HQ7EX9DmSF+G +W1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=AN+diq6krUutxY4s1BjD5o/2cLxtj1QMMKMF7O1rYVo=; b=DJU/xcTX8GIuLUqgYzU3nxuu2xBNyZmkMtpjQW4Qn03nPEoBWhsjqbZgNmHpFnBjX5 p9tgjcIAz5JbN8qGGIG0Iqs9qTViN+ryDCjgz39tdWUnBk1lFcbeiPDsaD2s3qrH92p0 oaEQDwP9YvukGXAh1n8TxcciSjU3l0tVIYJg7RoOqCB43Ry5DfGiLUqWsHb21xgMA2AD z2WSQYUYyvZjdkVkCkaQmOuwpMxcFfpKAItBGdNK041ml+qMR0t0ViC4cPDZo9pUlykY UXabT3maVU2/xorlUTAm6zokybm9INEMNpuZdjiTAsgE3XYLoIfnYezONTMeM+sOvs1t fjGw== X-Gm-Message-State: AOAM530VMMk6QOBjz87SMMY0mH7DJTfqh9yi5KAwAPJhcNc2Rr2JZCDj iOmVuPLHNqLhZPXd7gKN50FOFQ== X-Received: by 2002:a5d:630b:: with SMTP id i11mr1776131wru.404.1606167740649; Mon, 23 Nov 2020 13:42:20 -0800 (PST) Received: from localhost.localdomain (lns-bzn-59-82-252-157-164.adsl.proxad.net. [82.252.157.164]) by smtp.gmail.com with ESMTPSA id 35sm22400598wro.71.2020.11.23.13.42.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Nov 2020 13:42:20 -0800 (PST) From: Daniel Lezcano To: rjw@rjwysocki.net Cc: daniel.lezcano@linaro.org, corbet@lwn.net, ulf.hansson@linaro.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, ilina@codeaurora.org, lukasz.luba@arm.com, rkumbako@codeaurora.org, rui.zhang@intel.com, linux-arm-kernel@lists.infradead.org Subject: [PATCH v3 1/4] units: Add Watt units Date: Mon, 23 Nov 2020 22:42:05 +0100 Message-Id: <20201123214208.11007-2-daniel.lezcano@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20201123214208.11007-1-daniel.lezcano@linaro.org> References: <20201123214208.11007-1-daniel.lezcano@linaro.org> Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org As there are the temperature units, let's add the Watt macros definition. Signed-off-by: Daniel Lezcano Reviewed-by: Lukasz Luba --- include/linux/units.h | 4 ++++ 1 file changed, 4 insertions(+) -- 2.17.1 diff --git a/include/linux/units.h b/include/linux/units.h index aaf716364ec3..92c234e71cab 100644 --- a/include/linux/units.h +++ b/include/linux/units.h @@ -4,6 +4,10 @@ #include +#define MILLIWATT_PER_WATT 1000L +#define MICROWATT_PER_MILLIWATT 1000L +#define MICROWATT_PER_WATT 1000000L + #define ABSOLUTE_ZERO_MILLICELSIUS -273150 static inline long milli_kelvin_to_millicelsius(long t) From patchwork Mon Nov 23 21:42:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 330685 Delivered-To: patch@linaro.org Received: by 2002:a17:907:2110:0:0:0:0 with SMTP id qn16csp3859604ejb; Mon, 23 Nov 2020 13:44:09 -0800 (PST) X-Google-Smtp-Source: ABdhPJxC5PqeFSCttuOGl7G+ZT/bq/3GZtNiTVMzS/BCKZ51Ee+dxVYj9/ktFxBPSesTAZncrx4b X-Received: by 2002:aa7:dccd:: with SMTP id w13mr1160189edu.385.1606167849652; Mon, 23 Nov 2020 13:44:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606167849; cv=none; d=google.com; s=arc-20160816; b=NnSVwrmM1zsKo67b2OvRJDHQc9Y6V+wVK4GZIXWqUmsvXIcFqM0gkJQPG9mOyrj50j yjmslJCI0PpuBYGypS+mzezq5kWt6ndugzObWYCUIk4wHDOtBrLC45LaedHCtYXTLMBa lh6jIDOpa0fBWaHFQ4xI3yKDBRrTyyy6RA/BsG0o6Cy3AcuV/tVGbLWk5HIjBM4teWdY ikGBZctIixYa6uhqIA8Q8l3KBej2Jb7fNpu4UsBTPj/N20MGf+ppbebszrB4d3ZvbO3k hVX/f53vGyVrqKYXnLO9QdtWXAIoxbhWD7yi+y/IB3GIIieZcAkQf/I9xSBBE11f67Nk JYCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=e1RaVZeNacgl+YOwi5hVlfHJeCDFt69wWogqmulz7sA=; b=L+xjwRfRD4p/tYRHkAfMx0o1xV9mVZ8NDctOojoJnRNn7g323XGTBPByWODjbZrj10 dO6+opS7Ig37WQr07GVFt0Fnr6Iqfo+bLhfIl+k9i705CpnOOOvLgi1w0WUclFJiZbJ+ 0UKt8KFdk8yM89xpcJFiwrS3n5AR2AVvLYofR1HnaqqZxt/4Axo4vZe40oLXs6/Qy5ij f5gcsUAHC9cPp+xOghXges1UMwb5RS5ZdA5Pkrq3EyG8WZ0Xx7naNPpJSGKCWxd0hNhh 2Bcugv3a/wifg1sFCDu1HRMlha/671BQkupzKwxGLvBWF4Mh2ZK6Ei69mvvbpzloCWdi 8rBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=QgGS3DNr; spf=pass (google.com: domain of linux-pm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-pm-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id be11si7076134edb.164.2020.11.23.13.44.09; Mon, 23 Nov 2020 13:44:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-pm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=QgGS3DNr; spf=pass (google.com: domain of linux-pm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-pm-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731496AbgKWVm0 (ORCPT + 8 others); Mon, 23 Nov 2020 16:42:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731434AbgKWVmZ (ORCPT ); Mon, 23 Nov 2020 16:42:25 -0500 Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F9E5C0613CF for ; Mon, 23 Nov 2020 13:42:24 -0800 (PST) Received: by mail-wm1-x334.google.com with SMTP id p22so740647wmg.3 for ; Mon, 23 Nov 2020 13:42:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=e1RaVZeNacgl+YOwi5hVlfHJeCDFt69wWogqmulz7sA=; b=QgGS3DNrIKu612n7fdCmp/F/GtVU4/c+UcnbsoXzmOKjfOAHzOag2mUavH+PxGIdq8 jm5JoLQscoMVu8c6DpMf8bSwfbPhjtDRYJIbHlHoRcbEXyZU8vRoX/T572jDa2/mEN/8 PYfXKeu05u2dc83eVjUrpWhm+zPH8MKW05MpFo14s/XvoIr6LrBg9fOYGjKbfXbk4dYm +tX5IQyd7Q3wFJumVh2tUrSaU+/xhHbDUWobFZiAR77t/EPq967HQ1U8r3ahmcpbfTTn Z+BduNP+yaYudAJDvtJLtTPvBw4VkLf9FPRZAlrQYCabJ4JaqQou2mN2AF2GeaIASFBN Cs/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=e1RaVZeNacgl+YOwi5hVlfHJeCDFt69wWogqmulz7sA=; b=p4M/wHGxguTKIGBmU6+dYHGMLdclYGbIlPBfJltIoaBtf8h4HzJHmoj+70Xxw78ygs ymS2kJ0Jqmgwc+080D3OmvDls3EAshuSBHWsk1oZ68HniMdzOCn8fhTrFX9w9Jwtr0Sv Pd6vM9/cViJ1Eds7PrM83GytvcD1Ssjf8hOLgkLlUvdJuz/AJY3qVmbqRVixvxz501IL Hu66nEbRHW+tgSxUFYhL8pe5K/Iain30+KYVTC16HJImqtIBw0SwBQYghsVc1RVi8ox4 xQnhmvJYuvvZzfnOr/ysHQRyBOHn1FAShC0Imxm4ehcYnC4O6HMxYu6/r90hRenvaX2H ljeg== X-Gm-Message-State: AOAM532IZQAuTu2ahjOo8pS7vQMI2HRm2vnnyz/GMybjaLjofiLasWf6 /qUV7V6llt8ySInb0p7+dm0MPA== X-Received: by 2002:a1c:9cc9:: with SMTP id f192mr907402wme.143.1606167743209; Mon, 23 Nov 2020 13:42:23 -0800 (PST) Received: from localhost.localdomain (lns-bzn-59-82-252-157-164.adsl.proxad.net. [82.252.157.164]) by smtp.gmail.com with ESMTPSA id 35sm22400598wro.71.2020.11.23.13.42.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Nov 2020 13:42:22 -0800 (PST) From: Daniel Lezcano To: rjw@rjwysocki.net Cc: daniel.lezcano@linaro.org, corbet@lwn.net, ulf.hansson@linaro.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, ilina@codeaurora.org, lukasz.luba@arm.com, rkumbako@codeaurora.org, rui.zhang@intel.com, linux-arm-kernel@lists.infradead.org Subject: [PATCH v3 2/4] Documentation/powercap/dtpm: Add documentation for dtpm Date: Mon, 23 Nov 2020 22:42:06 +0100 Message-Id: <20201123214208.11007-3-daniel.lezcano@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20201123214208.11007-1-daniel.lezcano@linaro.org> References: <20201123214208.11007-1-daniel.lezcano@linaro.org> Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org The dynamic thermal and power management is a technique to dynamically adjust the power consumption of different devices in order to ensure a global thermal constraint. An userspace daemon is usually monitoring the temperature and the power to take immediate action on the device. The DTPM framework provides an unified API to userspace to act on the power. Document this framework. Signed-off-by: Daniel Lezcano --- Documentation/power/index.rst | 1 + Documentation/power/powercap/dtpm.rst | 210 ++++++++++++++++++++++++++ 2 files changed, 211 insertions(+) create mode 100644 Documentation/power/powercap/dtpm.rst -- 2.17.1 diff --git a/Documentation/power/index.rst b/Documentation/power/index.rst index ced8a8007434..a0f5244fb427 100644 --- a/Documentation/power/index.rst +++ b/Documentation/power/index.rst @@ -30,6 +30,7 @@ Power Management userland-swsusp powercap/powercap + powercap/dtpm regulator/consumer regulator/design diff --git a/Documentation/power/powercap/dtpm.rst b/Documentation/power/powercap/dtpm.rst new file mode 100644 index 000000000000..ca095ef4b887 --- /dev/null +++ b/Documentation/power/powercap/dtpm.rst @@ -0,0 +1,210 @@ +========================================== +Dynamic Thermal Power Management framework +========================================== + +On the embedded world, the complexity of the SoC leads to an +increasing number of hotspots which need to be monitored and mitigated +as a whole in order to prevent the temperature to go above the +normative and legally stated 'skin temperature'. + +Another aspect is to sustain the performance for a given power budget, +for example virtual reality where the user can feel dizziness if the +performance is capped while a big CPU is processing something else. Or +reduce the battery charging because the dissipated power is too high +compared with the power consumed by other devices. + +The userspace is the most adequate place to dynamically act on the +different devices by limiting their power given an application +profile: it has the knowledge of the platform. + +The Dynamic Thermal Power Management (DTPM) is a technique acting on +the device power by limiting and/or balancing a power budget among +different devices. + +The DTPM framework provides an unified interface to act on the +device power. + +Overview +======== + +The DTPM framework relies on the powercap framework to create the +powercap entries in the sysfs directory and implement the backend +driver to do the connection with the power manageable device. + +The DTPM is a tree representation describing the power constraints +shared between devices, not their physical positions. + +The nodes of the tree are a virtual description aggregating the power +characteristics of the children nodes and their power limitations. + +The leaves of the tree are the real power manageable devices. + +For instance:: + + SoC + | + `-- pkg + | + |-- pd0 (cpu0-3) + | + `-- pd1 (cpu4-5) + +The pkg power will be the sum of pd0 and pd1 power numbers:: + + SoC (400mW - 3100mW) + | + `-- pkg (400mW - 3100mW) + | + |-- pd0 (100mW - 700mW) + | + `-- pd1 (300mW - 2400mW) + +When the nodes are inserted in the tree, their power characteristics are propagated to the parents:: + + SoC (600mW - 5900mW) + | + |-- pkg (400mW - 3100mW) + | | + | |-- pd0 (100mW - 700mW) + | | + | `-- pd1 (300mW - 2400mW) + | + `-- pd2 (200mW - 2800mW) + +Each node have a weight on a 2^10 basis reflecting the percentage of power consumption along the siblings:: + + SoC (w=1024) + | + |-- pkg (w=538) + | | + | |-- pd0 (w=231) + | | + | `-- pd1 (w=794) + | + `-- pd2 (w=486) + + Note the sum of weights at the same level are equal to 1024. + +When a power limitation is applied to a node, then it is distributed along the children given their weights. For example, if we set a power limitation of 3200mW at the 'SoC' root node, the resulting tree will be:: + + SoC (w=1024) <--- power_limit = 3200mW + | + |-- pkg (w=538) --> power_limit = 1681mW + | | + | |-- pd0 (w=231) --> power_limit = 378mW + | | + | `-- pd1 (w=794) --> power_limit = 1303mW + | + `-- pd2 (w=486) --> power_limit = 1519mW + + +Flat description +---------------- + +A root node is created and it is the parent of all the nodes. This +description is the simplest one and it is supposed to give to +userspace a flat representation of all the devices supporting the +power limitation without any power limitation distribution. + +Hierarchical description +------------------------ + +The different devices supporting the power limitation are represented +hierarchically. There is one root node, all intermediate nodes are +grouping the child nodes which can be intermediate nodes also or real +devices. + +The intermediate nodes aggregate the power information and allows to +set the power limit given the weight of the nodes. + +Userspace API +============= + +As stated in the overview, the DTPM framework is built on top of the +powercap framework. Thus the sysfs interface is the same, please refer +to the powercap documentation for further details. + + * power_uw: Instantaneous power consumption. If the node is an + intermediate node, then the power consumption will be the sum of all + children power consumption. + + * max_power_range_uw: The power range resulting of the maximum power + minus the minimum power. + + * name: The name of the node. This is implementation dependant. Even + if it is not recommended for the userspace, several nodes can have + the same name. + + * constraint_X_name: The name of the constraint. + + * constraint_X_max_power_uw: The maximum power limit to be applicable + to the node. + + * constraint_X_power_limit_uw: The power limit to be applied to the + node. If the value contained in constraint_X_max_power_uw is set, + the constraint will be removed. + + * constraint_X_time_window_us: The meaning of this file will depend + on the constraint number. + +Constraints +----------- + + * Constraint 0: The power limitation is immediately applied, without + limitation in time. + +Kernel API +========== + +Overview +-------- + +The DTPM framework has no power limiting backend support. It is +generic and provides a set of API to let the different drivers to +implement the backend part for the power limitation and create a the +power constraints tree. + +It is up to the platform to provide the initialization function to +allocate and link the different nodes of the tree. + +A special macro has the role of declaring a node and the corresponding +initialization function via a description structure. This one contains +an optional parent field allowing to hook different devices to an +already existing tree at boot time. + +For instance:: + + struct dtpm_descr my_descr = { + .name = "my_name", + .init = my_init_func, + }; + + DTPM_DECLARE(my_descr); + +The nodes of the DTPM tree are described with dtpm structure. The +steps to add a new power limitable device is done in three steps: + + * Allocate the dtpm node + * Set the power number of the dtpm node + * Register the dtpm node + +The registration of the dtpm node is done with the powercap +ops. Basically, it must implements the callbacks to get and set the +power and the limit. + +Alternatively, if the node to be inserted is an intermediate one, then +a simple function to insert it as a future parent is available. + +If a device has its power characteristics changing, then the tree must +be updated with the new power numbers and weights. + +Nomenclature +------------ + + * dtpm_alloc() : Allocate and initialize a dtpm structure + + * dtpm_register() : Add the dtpm node to the tree + + * dtpm_unregister() : Remove the dtpm node from the tree + + * dtpm_update_power() : Update the power characteristics of the dtpm node From patchwork Mon Nov 23 21:42:07 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 330686 Delivered-To: patch@linaro.org Received: by 2002:a17:907:2110:0:0:0:0 with SMTP id qn16csp3859616ejb; Mon, 23 Nov 2020 13:44:10 -0800 (PST) X-Google-Smtp-Source: ABdhPJypbPtNoc2lCi1LPNHnKDogNTDtaCS+6KYjZf7e1MWRIhoCgJ68yDTWXPATv9FRV41ONWEz X-Received: by 2002:a50:d78f:: with SMTP id w15mr1175798edi.227.1606167850720; Mon, 23 Nov 2020 13:44:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606167850; cv=none; d=google.com; s=arc-20160816; b=LTnsc6zNnBz95Kg+HhREZf8knD6Gf+eZu++ypOvmxMoZ3i+YR90nF7QfoGGJEFsmkT pH22LovskYMvTfOPYwc54cjREBW0HXXRgytKhW7ZwzE7b4EcmPF74UNFI2S0Tj9RQYro Aped/X/XLOUS9aIk13mCmFkH+R5lKTRgTDLFh4fJL2z0yz0TEgmsFxq0CylibIPuP3tw r/wPZ8gUNpZ2TEvVhQQA93zJUf96l3iiHtEFNdnjLCKx3FmEqpr4JxTxPkB7ViJdIxec yfnlIpkXEzM2Jw95/PSRTlyymhRdM9y79GbWfpkHpw4yA5NOpLpeluGUpEYx+lLXV5DI pevQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=0Cf8W+2YWMEjHSLQt5pI2/yaE1k/htVur+7R2n7V6q4=; b=q5F5VNMS+vYzagIyhkkV+U8DHdNw8jsz7snCe7QFV1QqoOi5Mx/+jvYy6IxQjKalAO oRW4l7X7WERxZhtd9dNjMVcCfRrZsDul/TD4+YtuhSLNhQ9UUSJEtwpYu+m/krsGMUHs dcpG/FR7C9PsEauK7CHTxcSmurAL0Gn4sZ4fITlgQshY3RUaLCmfTJO3PNRVcAhn0T+k YD0NHuS10sLv7hiGpLtxGVkod28NCr8ypZQi+xHg2lzB0GJ+/5+yw3e0jcM37atAEqbw Uzj2hoD3BjmbwUqQjlVeH1G76qg8P+MoDSpLU4FssU0IIfPo1eKmgaq9GyuX2Fi6reO+ 6qPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ldppN0aR; spf=pass (google.com: domain of linux-pm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-pm-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id be11si7076134edb.164.2020.11.23.13.44.10; Mon, 23 Nov 2020 13:44:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-pm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ldppN0aR; spf=pass (google.com: domain of linux-pm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-pm-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731434AbgKWVm2 (ORCPT + 8 others); Mon, 23 Nov 2020 16:42:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731545AbgKWVm1 (ORCPT ); Mon, 23 Nov 2020 16:42:27 -0500 Received: from mail-wr1-x442.google.com (mail-wr1-x442.google.com [IPv6:2a00:1450:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A0DFDC061A4E for ; Mon, 23 Nov 2020 13:42:26 -0800 (PST) Received: by mail-wr1-x442.google.com with SMTP id i2so1242999wrs.4 for ; Mon, 23 Nov 2020 13:42:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=0Cf8W+2YWMEjHSLQt5pI2/yaE1k/htVur+7R2n7V6q4=; b=ldppN0aR/Mzq64r2Mc+G+9NgGHCzN677WZJ5dpPeVdO+L2OG7XKJS2y0NHlVsR9xAT 0lp7VY8ZqlV5tTNHRyk2PpQ/C4qA3nKI6rqqNXp7SpYGu/FiKDvOtyEYdEnDQFAzRRKI w7kHt8MNay3Jg+bUub4eOnl7e+1l5RUl/dTFvi4mythupVdRU2TgMa/Y6VzWFeCSDNBN 9tq3DJ0g367fTXx4TYQ23vRei29APJElB2uLHxnBd6TDben4xlJbcIWLDNtZNPxPUBjd boyoazAIa/HArmXuCSUXySu1dHRbezpqv94/lmpUnVqcAD4/5RfsequxMRRZlYVok3a/ 7HDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=0Cf8W+2YWMEjHSLQt5pI2/yaE1k/htVur+7R2n7V6q4=; b=Pt0KgBgiD42is53IzGE/u+zilEDl695+7JfQp0krbr4pGWf0fYXZEYCGxauDb8UiRc lE5vivTKzpwJ0HkaD+lElGTC1epuBE6MVCNUlugs5M8cVYq/6yjEi7QuBhENG0QTeNzE +aCZYhY+HC7ZLj+wfUCxJy1bVVqwWHj4DedLkuDtVgmbMNIcg+cWY2ybwZ56dVtKQmtw UdGBBFcBYy1duNKzj6CVh12/lcpuof1DJJlo10PmpRzBER55/lMMvS25VPFAUd9xdGhQ 8MEMh9ZR5SGb7YmmF7YsDpNy3h4yAy31GAUjzf6WSa5ggGYN0cxwspSRTumZQ8IuNnx7 UGuA== X-Gm-Message-State: AOAM532rlhcf6hjs+6zZipW/KmMviMIouSpo8yE32rmYebAxO1ZaXS5x kvkC8p4fDCDjKGNFasujmVQDsw== X-Received: by 2002:adf:fc8c:: with SMTP id g12mr1687811wrr.355.1606167745106; Mon, 23 Nov 2020 13:42:25 -0800 (PST) Received: from localhost.localdomain (lns-bzn-59-82-252-157-164.adsl.proxad.net. [82.252.157.164]) by smtp.gmail.com with ESMTPSA id 35sm22400598wro.71.2020.11.23.13.42.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Nov 2020 13:42:24 -0800 (PST) From: Daniel Lezcano To: rjw@rjwysocki.net Cc: daniel.lezcano@linaro.org, corbet@lwn.net, ulf.hansson@linaro.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, ilina@codeaurora.org, lukasz.luba@arm.com, rkumbako@codeaurora.org, rui.zhang@intel.com, linux-arm-kernel@lists.infradead.org Subject: [PATCH v3 3/4] powercap/drivers/dtpm: Add API for dynamic thermal power management Date: Mon, 23 Nov 2020 22:42:07 +0100 Message-Id: <20201123214208.11007-4-daniel.lezcano@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20201123214208.11007-1-daniel.lezcano@linaro.org> References: <20201123214208.11007-1-daniel.lezcano@linaro.org> Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org On the embedded world, the complexity of the SoC leads to an increasing number of hotspots which need to be monitored and mitigated as a whole in order to prevent the temperature to go above the normative and legally stated 'skin temperature'. Another aspect is to sustain the performance for a given power budget, for example virtual reality where the user can feel dizziness if the GPU performance is capped while a big CPU is processing something else. Or reduce the battery charging because the dissipated power is too high compared with the power consumed by other devices. The userspace is the most adequate place to dynamically act on the different devices by limiting their power given an application profile: it has the knowledge of the platform. These userspace daemons are in charge of the Dynamic Thermal Power Management (DTPM). Nowadays, the dtpm daemons are abusing the thermal framework as they act on the cooling device state to force a specific and arbitrary state without taking care of the governor decisions. Given the closed loop of some governors that can confuse the logic or directly enter in a decision conflict. As the number of cooling device support is limited today to the CPU and the GPU, the dtpm daemons have little control on the power dissipation of the system. The out of tree solutions are hacking around here and there in the drivers, in the frameworks to have control on the devices. The common solution is to declare them as cooling devices. There is no unification of the power limitation unit, opaque states are used. This patch provides a way to create a hierarchy of constraints using the powercap framework. The devices which are registered as power limit-able devices are represented in this hierarchy as a tree. They are linked together with intermediate nodes which are just there to propagate the constraint to the children. The leaves of the tree are the real devices, the intermediate nodes are virtual, aggregating the children constraints and power characteristics. Each node have a weight on a 2^10 basis, in order to reflect the percentage of power distribution of the children's node. This percentage is used to dispatch the power limit to the children. The weight is computed against the max power of the siblings. This simple approach allows to do a fair distribution of the power limit. Signed-off-by: Daniel Lezcano Reviewed-by: Lukasz Luba Tested-by: Lukasz Luba --- drivers/powercap/Kconfig | 6 + drivers/powercap/Makefile | 1 + drivers/powercap/dtpm.c | 441 ++++++++++++++++++++++++++++++ include/asm-generic/vmlinux.lds.h | 11 + include/linux/dtpm.h | 76 +++++ 5 files changed, 535 insertions(+) create mode 100644 drivers/powercap/dtpm.c create mode 100644 include/linux/dtpm.h -- 2.17.1 diff --git a/drivers/powercap/Kconfig b/drivers/powercap/Kconfig index bc228725346b..cc1953bd8bed 100644 --- a/drivers/powercap/Kconfig +++ b/drivers/powercap/Kconfig @@ -43,4 +43,10 @@ config IDLE_INJECT CPUs for power capping. Idle period can be injected synchronously on a set of specified CPUs or alternatively on a per CPU basis. + +config DTPM + bool "Power capping for Dynamic Thermal Power Management" + help + This enables support for the power capping for the dynamic + thermal power management userspace engine. endif diff --git a/drivers/powercap/Makefile b/drivers/powercap/Makefile index 7255c94ec61c..6482ac52054d 100644 --- a/drivers/powercap/Makefile +++ b/drivers/powercap/Makefile @@ -1,4 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only +obj-$(CONFIG_DTPM) += dtpm.o obj-$(CONFIG_POWERCAP) += powercap_sys.o obj-$(CONFIG_INTEL_RAPL_CORE) += intel_rapl_common.o obj-$(CONFIG_INTEL_RAPL) += intel_rapl_msr.o diff --git a/drivers/powercap/dtpm.c b/drivers/powercap/dtpm.c new file mode 100644 index 000000000000..a4d1f6fd6c0a --- /dev/null +++ b/drivers/powercap/dtpm.c @@ -0,0 +1,441 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright 2020 Linaro Limited + * + * Author: Daniel Lezcano + * + * The powercap based Dynamic Thermal Power Management framework + * provides to the userspace a consistent API to set the power limit + * on some devices. + * + * DTPM defines the functions to create a tree of constraints. Each + * parent node is a virtual description of the aggregation of the + * children. It propagates the constraints set at its level to its + * children and collect the children power information. The leaves of + * the tree are the real devices which have the ability to get their + * current power consumption and set their power limit. + */ +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include +#include +#include + +#define DTPM_POWER_LIMIT_FLAG BIT(0) + +static const char *constraint_name[] = { + "Instantaneous", +}; + +static struct powercap_control_type *pct; +static struct dtpm *root; + +static int get_time_window_us(struct powercap_zone *pcz, int cid, u64 *window) +{ + return -ENOSYS; +} + +static int set_time_window_us(struct powercap_zone *pcz, int cid, u64 window) +{ + return -ENOSYS; +} + +static int get_max_power_range_uw(struct powercap_zone *pcz, u64 *max_power_uw) +{ + struct dtpm *dtpm = to_dtpm(pcz); + + spin_lock(&dtpm->lock); + *max_power_uw = dtpm->power_max - dtpm->power_min; + spin_unlock(&dtpm->lock); + + return 0; +} + +static int get_power_uw(struct powercap_zone *pcz, u64 *power_uw) +{ + struct dtpm *dtpm = to_dtpm(pcz); + struct dtpm *child; + u64 power; + int ret = 0; + + if (dtpm->ops) { + *power_uw = dtpm->ops->get_power_uw(dtpm); + return 0; + } + + *power_uw = 0; + + spin_lock(&dtpm->lock); + list_for_each_entry(child, &dtpm->children, sibling) { + ret = child->zone.ops->get_power_uw(&child->zone, &power); + if (ret) + break; + *power_uw += power; + } + spin_unlock(&dtpm->lock); + + return ret; +} + +static void __dtpm_rebalance_weight(struct dtpm *dtpm) +{ + struct dtpm *child; + + spin_lock(&dtpm->lock); + list_for_each_entry(child, &dtpm->children, sibling) { + + pr_debug("Setting weight '%d' for '%s'\n", + child->weight, child->zone.name); + + child->weight = DIV_ROUND_CLOSEST(child->power_max * 1024, + dtpm->power_max); + + __dtpm_rebalance_weight(child); + } + spin_unlock(&dtpm->lock); +} + +static void dtpm_rebalance_weight(void) +{ + __dtpm_rebalance_weight(root); +} + +static void dtpm_sub_power(struct dtpm *dtpm) +{ + struct dtpm *parent = dtpm->parent; + + while (parent) { + spin_lock(&parent->lock); + parent->power_min -= dtpm->power_min; + parent->power_max -= dtpm->power_max; + parent->power_limit -= dtpm->power_limit; + spin_unlock(&parent->lock); + parent = parent->parent; + } + + dtpm_rebalance_weight(); +} + +static void dtpm_add_power(struct dtpm *dtpm) +{ + struct dtpm *parent = dtpm->parent; + + while (parent) { + spin_lock(&parent->lock); + parent->power_min += dtpm->power_min; + parent->power_max += dtpm->power_max; + parent->power_limit += dtpm->power_limit; + spin_unlock(&parent->lock); + parent = parent->parent; + } + + dtpm_rebalance_weight(); +} + +/** + * dtpm_update_power - Update the power on the dtpm + * @dtpm: a pointer to a dtpm structure to update + * @power_min: a u64 representing the new power_min value + * @power_max: a u64 representing the new power_max value + * + * Function to update the power values of the dtpm node specified in + * parameter. These new values will be propagated to the tree. + * + * Return: zero on success, -EINVAL if the values are inconsistent + */ +int dtpm_update_power(struct dtpm *dtpm, u64 power_min, u64 power_max) +{ + if (power_min == dtpm->power_min && power_max == dtpm->power_max) + return 0; + + if (power_max < power_min) + return -EINVAL; + + dtpm_sub_power(dtpm); + spin_lock(&dtpm->lock); + dtpm->power_min = power_min; + dtpm->power_max = power_max; + if (!test_bit(DTPM_POWER_LIMIT_FLAG, &dtpm->flags)) + dtpm->power_limit = power_max; + spin_unlock(&dtpm->lock); + dtpm_add_power(dtpm); + + return 0; +} + +/** + * dtpm_release_zone - Cleanup when the node is released + * @pcz: a pointer to a powercap_zone structure + * + * Do some housecleaning and update the weight on the tree. The + * release will be denied if the node has children. This function must + * be called by the specific release callback of the different + * backends. + * + * Return: 0 on success, -EBUSY if there are children + */ +int dtpm_release_zone(struct powercap_zone *pcz) +{ + struct dtpm *dtpm = to_dtpm(pcz); + struct dtpm *parent = dtpm->parent; + + if (!list_empty(&dtpm->children)) + return -EBUSY; + + if (parent) { + spin_lock(&parent->lock); + list_del(&dtpm->sibling); + spin_unlock(&parent->lock); + } + + dtpm_sub_power(dtpm); + + if (dtpm->ops) + dtpm->ops->release(dtpm); + + kfree(dtpm); + + return 0; +} + +/* + * Set the power limit on the nodes, the power limit is distributed + * given the weight of the children. + */ +static int set_power_limit_uw(struct powercap_zone *pcz, + int cid, u64 power_limit) +{ + struct dtpm *dtpm = to_dtpm(pcz); + struct dtpm *child; + int ret = 0; + u64 power; + + spin_lock(&dtpm->lock); + + /* + * Don't allow values outside of the power range previously + * set when initializing the power numbers. + */ + power_limit = clamp_val(power_limit, dtpm->power_min, dtpm->power_max); + + /* + * A max power limitation means we remove the power limit, + * otherwise we set a constraint and flag the dtpm node. + */ + if (power_limit == dtpm->power_max) { + clear_bit(DTPM_POWER_LIMIT_FLAG, &dtpm->flags); + } else { + set_bit(DTPM_POWER_LIMIT_FLAG, &dtpm->flags); + } + + /* + * Only leaves of the dtpm tree has ops to get/set the power + */ + if (dtpm->ops) { + dtpm->power_limit = dtpm->ops->set_power_uw(dtpm, power_limit); + } else { + dtpm->power_limit = 0; + + list_for_each_entry(child, &dtpm->children, sibling) { + + /* + * Integer division rounding will inevitably lead to a + * different max value when set several times. In + * order to restore the initial value, we force the + * child's max power every time if the constraint is + * removed by setting a value greater or equal to the + * max power. + */ + if (power_limit == dtpm->power_max) + power = child->power_max; + else + power = DIV_ROUND_CLOSEST( + power_limit * child->weight, 1024); + + pr_debug("Setting power limit for '%s': %llu uW\n", + child->zone.name, power); + + ret = child->zone.constraints->ops->set_power_limit_uw( + &child->zone, cid, power); + if (ret) + break; + + ret = child->zone.constraints->ops->get_power_limit_uw( + &child->zone, cid, &power); + if (ret) + break; + + dtpm->power_limit += power; + } + } + + spin_unlock(&dtpm->lock); + + return ret; +} + +static int get_power_limit_uw(struct powercap_zone *pcz, + int cid, u64 *power_limit) +{ + struct dtpm *dtpm = to_dtpm(pcz); + + spin_lock(&dtpm->lock); + *power_limit = dtpm->power_limit; + spin_unlock(&dtpm->lock); + + return 0; +} + +static const char *get_constraint_name(struct powercap_zone *pcz, int cid) +{ + return constraint_name[cid]; +} + +static int get_max_power_uw(struct powercap_zone *pcz, int id, u64 *max_power) +{ + struct dtpm *dtpm = to_dtpm(pcz); + + spin_lock(&dtpm->lock); + *max_power = dtpm->power_max; + spin_unlock(&dtpm->lock); + + return 0; +} + +static struct powercap_zone_constraint_ops constraint_ops = { + .set_power_limit_uw = set_power_limit_uw, + .get_power_limit_uw = get_power_limit_uw, + .set_time_window_us = set_time_window_us, + .get_time_window_us = get_time_window_us, + .get_max_power_uw = get_max_power_uw, + .get_name = get_constraint_name, +}; + +static struct powercap_zone_ops zone_ops = { + .get_max_power_range_uw = get_max_power_range_uw, + .get_power_uw = get_power_uw, + .release = dtpm_release_zone, +}; + +/** + * dtpm_alloc - Allocate and initialize a dtpm struct + * @name: a string specifying the name of the node + * + * Return: a struct dtpm pointer, NULL in case of error + */ +struct dtpm *dtpm_alloc(struct dtpm_ops *ops) +{ + struct dtpm *dtpm; + + dtpm = kzalloc(sizeof(*dtpm), GFP_KERNEL); + if (dtpm) { + INIT_LIST_HEAD(&dtpm->children); + INIT_LIST_HEAD(&dtpm->sibling); + spin_lock_init(&dtpm->lock); + dtpm->weight = 1024; + dtpm->ops = ops; + } + + return dtpm; +} + +/** + * dtpm_unregister - Unregister a dtpm node from the hierarchy tree + * @dtpm: a pointer to a dtpm structure corresponding to the node to be removed + * + * Call the underlying powercap unregister function. That will call + * the release callback of the powercap zone. + */ +void dtpm_unregister(struct dtpm *dtpm) +{ + powercap_unregister_zone(pct, &dtpm->zone); + + pr_info("Unregistered dtpm node '%s'\n", dtpm->zone.name); +} + +/** + * dtpm_register - Register a dtpm node in the hierarchy tree + * @name: a string specifying the name of the node + * @dtpm: a pointer to a dtpm structure corresponding to the new node + * @parent: a pointer to a dtpm structure corresponding to the parent node + * + * Create a dtpm node in the tree. If no parent is specified, the node + * is the root node of the hierarchy. If the root node already exists, + * then the registration will fail. The powercap controller must be + * initialized before calling this function. + * + * The dtpm structure must be initialized with the power numbers + * before calling this function. + * + * Return: zero on success, a negative value in case of error: + * -EAGAIN: the function is called before the framework is initialized. + * -EBUSY: the root node is already inserted + * -EINVAL: * there is no root node yet and @parent is specified + * * no all ops are defined + * * parent have ops which are reserved for leaves + * Other negative values are reported back from the powercap framework + */ +int dtpm_register(const char *name, struct dtpm *dtpm, struct dtpm *parent) +{ + struct powercap_zone *pcz; + + if (!pct) + return -EAGAIN; + + if (root && !parent) + return -EBUSY; + + if (!root && parent) + return -EINVAL; + + if (parent && parent->ops) + return -EINVAL; + + if (dtpm->ops && !(dtpm->ops->set_power_uw && + dtpm->ops->get_power_uw && + dtpm->ops->release)) + return -EINVAL; + + pcz = powercap_register_zone(&dtpm->zone, pct, name, + parent ? &parent->zone : NULL, + &zone_ops, MAX_DTPM_CONSTRAINTS, + &constraint_ops); + if (IS_ERR(pcz)) + return PTR_ERR(pcz); + + if (parent) { + spin_lock(&parent->lock); + list_add_tail(&dtpm->sibling, &parent->children); + dtpm->parent = parent; + spin_unlock(&parent->lock); + } else { + root = dtpm; + } + + dtpm_add_power(dtpm); + + pr_info("Registered dtpm node '%s' / %llu-%llu uW, \n", + dtpm->zone.name, dtpm->power_min, dtpm->power_max); + + return 0; +} + +static int __init dtpm_init(void) +{ + struct dtpm_descr **dtpm_descr; + + pct = powercap_register_control_type(NULL, "dtpm", NULL); + if (!pct) { + pr_err("Failed to register control type\n"); + return -EINVAL; + } + + for_each_dtpm_table(dtpm_descr) + (*dtpm_descr)->init(*dtpm_descr); + + return 0; +} +late_initcall(dtpm_init); diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h index b2b3d81b1535..b3e4e0740089 100644 --- a/include/asm-generic/vmlinux.lds.h +++ b/include/asm-generic/vmlinux.lds.h @@ -316,6 +316,16 @@ #define THERMAL_TABLE(name) #endif +#ifdef CONFIG_DTPM +#define DTPM_TABLE() \ + . = ALIGN(8); \ + __dtpm_table = .; \ + KEEP(*(__dtpm_table)) \ + __dtpm_table_end = .; +#else +#define DTPM_TABLE() +#endif + #define KERNEL_DTB() \ STRUCT_ALIGN(); \ __dtb_start = .; \ @@ -733,6 +743,7 @@ ACPI_PROBE_TABLE(irqchip) \ ACPI_PROBE_TABLE(timer) \ THERMAL_TABLE(governor) \ + DTPM_TABLE() \ EARLYCON_TABLE() \ LSM_TABLE() \ EARLY_LSM_TABLE() \ diff --git a/include/linux/dtpm.h b/include/linux/dtpm.h new file mode 100644 index 000000000000..84b087b4ac01 --- /dev/null +++ b/include/linux/dtpm.h @@ -0,0 +1,76 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2020 Linaro Ltd + * + * Author: Daniel Lezcano + */ +#ifndef ___DTPM_H__ +#define ___DTPM_H__ + +#include + +#define MAX_DTPM_DESCR 8 +#define MAX_DTPM_CONSTRAINTS 1 + +struct dtpm { + struct powercap_zone zone; + struct dtpm *parent; + struct list_head sibling; + struct list_head children; + struct dtpm_ops *ops; + unsigned long flags; + spinlock_t lock; + u64 power_limit; + u64 power_max; + u64 power_min; + int weight; + void *private; +}; + +struct dtpm_ops { + u64 (*set_power_uw)(struct dtpm *, u64); + u64 (*get_power_uw)(struct dtpm *); + void (*release)(struct dtpm *); +}; + +struct dtpm_descr; + +typedef int (*dtpm_init_t)(struct dtpm_descr *); + +struct dtpm_descr { + struct dtpm *parent; + const char *name; + dtpm_init_t init; +}; + +/* Init section thermal table */ +extern struct dtpm_descr *__dtpm_table[]; +extern struct dtpm_descr *__dtpm_table_end[]; + +#define DTPM_TABLE_ENTRY(name) \ + static typeof(name) *__dtpm_table_entry_##name \ + __used __section("__dtpm_table") = &name + +#define DTPM_DECLARE(name) DTPM_TABLE_ENTRY(name) + +#define for_each_dtpm_table(__dtpm) \ + for (__dtpm = __dtpm_table; \ + __dtpm < __dtpm_table_end; \ + __dtpm++) + +static inline struct dtpm *to_dtpm(struct powercap_zone *zone) +{ + return container_of(zone, struct dtpm, zone); +} + +int dtpm_update_power(struct dtpm *dtpm, u64 power_min, u64 power_max); + +int dtpm_release_zone(struct powercap_zone *pcz); + +struct dtpm *dtpm_alloc(struct dtpm_ops *ops); + +void dtpm_unregister(struct dtpm *dtpm); + +int dtpm_register(const char *name, struct dtpm *dtpm, struct dtpm *parent); + +#endif From patchwork Mon Nov 23 21:42:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Lezcano X-Patchwork-Id: 330687 Delivered-To: patch@linaro.org Received: by 2002:a17:907:2110:0:0:0:0 with SMTP id qn16csp3859632ejb; Mon, 23 Nov 2020 13:44:12 -0800 (PST) X-Google-Smtp-Source: ABdhPJz8nIepvycFO/nxGPVaREY44DsvU3hNoJLJwZg5jFZCMeX/qywV1cxV3LMsmIbRR9cz7HTx X-Received: by 2002:a17:906:6713:: with SMTP id a19mr1552490ejp.468.1606167852243; Mon, 23 Nov 2020 13:44:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606167852; cv=none; d=google.com; s=arc-20160816; b=KpyiOz5/28yuc64Xlzv/g9GVYks4BoXh51XWSZWTWy5QZEAsJbSIoCP4XZ5Rt0UQZR JxbjUZ9OGxmyrq9TaqQq21qOADD5+Y9hIySr7MvGeRpaHTb1WR16QIbdsramzeoOzt3K 7B75Q5L4tSkKrzGJTEfFB3VskV5nCUI13BUK00Z6qiBmLOnmaA8X2QiKoqTdUpYmTLbs ymWQZxISHgiSvHSPxrAEYVWlky8jvbVkwMbeoICYuqqexEyhcS0Vp2EAPTFQXRH8fvWO YBP7V0i6Xr3gqcNn13ZUmxMolZr4e4CKjEG1gZGd3sjuIzxaGZLKQkAUX9LMrRhc6QPX dvwA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=BY6V6LfPzm2TS6iX4iHYAl5lbJAiGRYPM4silY/pXUU=; b=E/z9QO0uRqCkXS4sZ7boRYZ/C55/gC0Y9I00bXp9sIHPZWm/c6O9WeM9k/bpfYdt5l Psfig0NxKqCo77Iu+8UNgDsoHQ9OTr0z+oFDVj3ed9iaTb2U8MvDJuN2RvYCVSJMkd6M pbWFSKIbjXDj/dny7cVceGNx9qVsiXxT/N3q/ngM15VmVnKRyxU5fz/eeYQfcCbLL8wu xiBXOefC8VomDsaazdxJU+iFqMegy0CzJixaQxIyAYawIyd97BiWEAi0P7KpjM2KTg4s qQo1JDjkZgXu0BhgD60vM4jr6eSUuIP0SHJLCl+4loIaIMGAm6XLA901A0lE8oJxzSJ9 j0Sg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=cYcxYg2E; spf=pass (google.com: domain of linux-pm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-pm-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id be11si7076134edb.164.2020.11.23.13.44.12; Mon, 23 Nov 2020 13:44:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-pm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=cYcxYg2E; spf=pass (google.com: domain of linux-pm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-pm-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731609AbgKWVmb (ORCPT + 8 others); Mon, 23 Nov 2020 16:42:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56852 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731545AbgKWVm3 (ORCPT ); Mon, 23 Nov 2020 16:42:29 -0500 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F47CC061A4D for ; Mon, 23 Nov 2020 13:42:28 -0800 (PST) Received: by mail-wm1-x32b.google.com with SMTP id p22so740741wmg.3 for ; Mon, 23 Nov 2020 13:42:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=BY6V6LfPzm2TS6iX4iHYAl5lbJAiGRYPM4silY/pXUU=; b=cYcxYg2EvHJvWPFpV9ifYndNPY5XKKrtY8Jo6nNz4evKNYpNM2EVlVPBPqrwwkXpUr T5rptMfLkvEATBcMH2sHEgguTuVaFLuO4N62/ihwffLuekWequYW3PmXZHOrZupe+HgI bFqPP5fvNHQmc0NFy9EPjwx/aS24Vli1EVqbyGhNp4SSd7Hw5v/H3dhdtcNNVuROkvKi 1nSZuJg0zf/41u4J+MrcdSZQOdIFYtYJ9sqQdM9Xh8PxyQtQZ6BnjuwyQfkjTCgqwauX riX1zizRnOfL6Y9Fe1pyeSLxtA6V5qNrfmEPI3knt6R20ZF0axxwu7TBcSA3Uq8WIT5q arwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=BY6V6LfPzm2TS6iX4iHYAl5lbJAiGRYPM4silY/pXUU=; b=flL8jSnV5I92gOzG+iT2FSX/hCdjHPp3B5pUxNuu+w60iLWJgkGTCE2ox21BoWolEr YfWGJsoHzE8acLstitHJfnDSEpqw+jOJ89wqoFhWR28xcbj7Pb5ypNK3D3b7vq4J6t9X Vs+Pl4WZavd2mhwLiZb25oe4jWNC5Q6Me3sQ5xXfAlGgo9w3GMgjpH8t9dTolzHq6pks AQCfw4pG7olPKiefGNPUGDWwny2pKAD0IyvdEAmZZ38oMjhZ67sxg6BS+bFq6vCn8Wk6 muayMPYCxO0WitIcd9CEEGwHC6HBZ6Klz6MUbLenMEAiQqWdV3j6vcrzZkM0oJlbivjt i6Pg== X-Gm-Message-State: AOAM530WG7RXkekMpQ+RkBhPaLZpqZlA1uscPa4quurC/Z8cKE6HTT3e 7tnlAmvaugcIXUZD47tBzqgkZg== X-Received: by 2002:a1c:a706:: with SMTP id q6mr946586wme.2.1606167746704; Mon, 23 Nov 2020 13:42:26 -0800 (PST) Received: from localhost.localdomain (lns-bzn-59-82-252-157-164.adsl.proxad.net. [82.252.157.164]) by smtp.gmail.com with ESMTPSA id 35sm22400598wro.71.2020.11.23.13.42.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Nov 2020 13:42:26 -0800 (PST) From: Daniel Lezcano To: rjw@rjwysocki.net Cc: daniel.lezcano@linaro.org, corbet@lwn.net, ulf.hansson@linaro.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, ilina@codeaurora.org, lukasz.luba@arm.com, rkumbako@codeaurora.org, rui.zhang@intel.com, linux-arm-kernel@lists.infradead.org Subject: [PATCH v3 4/4] powercap/drivers/dtpm: Add CPU energy model based support Date: Mon, 23 Nov 2020 22:42:08 +0100 Message-Id: <20201123214208.11007-5-daniel.lezcano@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20201123214208.11007-1-daniel.lezcano@linaro.org> References: <20201123214208.11007-1-daniel.lezcano@linaro.org> Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org With the powercap dtpm controller, we are able to plug devices with power limitation features in the tree. The following patch introduces the CPU power limitation based on the energy model and the performance states. The power limitation is done at the performance domain level. If some CPUs are unplugged, the corresponding power will be subtracted from the performance domain total power. It is up to the platform to initialize the dtpm tree and add the CPU. Here is an example to create a simple tree with one root node called "pkg" and the CPU's performance domains. static int dtpm_register_pkg(struct dtpm_descr *descr) { struct dtpm *pkg; int ret; pkg = dtpm_alloc(NULL); if (!pkg) return -ENOMEM; ret = dtpm_register(descr->name, pkg, descr->parent); if (ret) return ret; return dtpm_register_cpu(pkg); } static struct dtpm_descr descr = { .name = "pkg", .init = dtpm_register_pkg, }; DTPM_DECLARE(descr); Signed-off-by: Daniel Lezcano --- drivers/powercap/Kconfig | 7 + drivers/powercap/Makefile | 1 + drivers/powercap/dtpm_cpu.c | 252 ++++++++++++++++++++++++++++++++++++ include/linux/cpuhotplug.h | 1 + include/linux/dtpm.h | 2 + 5 files changed, 263 insertions(+) create mode 100644 drivers/powercap/dtpm_cpu.c -- 2.17.1 diff --git a/drivers/powercap/Kconfig b/drivers/powercap/Kconfig index cc1953bd8bed..20b4325c6161 100644 --- a/drivers/powercap/Kconfig +++ b/drivers/powercap/Kconfig @@ -49,4 +49,11 @@ config DTPM help This enables support for the power capping for the dynamic thermal power management userspace engine. + +config DTPM_CPU + bool "Add CPU power capping based on the energy model" + depends on DTPM && ENERGY_MODEL + help + This enables support for CPU power limitation based on + energy model. endif diff --git a/drivers/powercap/Makefile b/drivers/powercap/Makefile index 6482ac52054d..fabcf388a8d3 100644 --- a/drivers/powercap/Makefile +++ b/drivers/powercap/Makefile @@ -1,5 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only obj-$(CONFIG_DTPM) += dtpm.o +obj-$(CONFIG_DTPM_CPU) += dtpm_cpu.o obj-$(CONFIG_POWERCAP) += powercap_sys.o obj-$(CONFIG_INTEL_RAPL_CORE) += intel_rapl_common.o obj-$(CONFIG_INTEL_RAPL) += intel_rapl_msr.o diff --git a/drivers/powercap/dtpm_cpu.c b/drivers/powercap/dtpm_cpu.c new file mode 100644 index 000000000000..81f0996dd8ad --- /dev/null +++ b/drivers/powercap/dtpm_cpu.c @@ -0,0 +1,252 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright 2020 Linaro Limited + * + * Author: Daniel Lezcano + * + * The DTPM CPU is based on the energy model. It hooks the CPU in the + * DTPM tree which in turns update the power number by propagating the + * power number from the CPU energy model information to the parents. + * + * The association between the power and the performance state, allows + * to set the power of the CPU at the OPP granularity. + * + * The CPU hotplug is supported and the power numbers will be updated + * if a CPU is hot plugged / unplugged. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +static struct dtpm *__parent; + +static DEFINE_PER_CPU(struct dtpm *, dtpm_per_cpu); + +struct dtpm_cpu { + struct freq_qos_request qos_req; + int cpu; +}; + +/* + * When a new CPU is inserted at hotplug or boot time, add the power + * contribution and update the dtpm tree. + */ +static int power_add(struct dtpm *dtpm, struct em_perf_domain *em) +{ + u64 power_min, power_max; + + power_min = em->table[0].power; + power_min *= MICROWATT_PER_MILLIWATT; + power_min += dtpm->power_min; + + power_max = em->table[em->nr_perf_states - 1].power; + power_max *= MICROWATT_PER_MILLIWATT; + power_max += dtpm->power_max; + + return dtpm_update_power(dtpm, power_min, power_max); +} + +/* + * When a CPU is unplugged, remove its power contribution from the + * dtpm tree. + */ +static int power_sub(struct dtpm *dtpm, struct em_perf_domain *em) +{ + u64 power_min, power_max; + + power_min = em->table[0].power; + power_min *= MICROWATT_PER_MILLIWATT; + power_min = dtpm->power_min - power_min; + + power_max = em->table[em->nr_perf_states - 1].power; + power_max *= MICROWATT_PER_MILLIWATT; + power_max = dtpm->power_max - power_max; + + return dtpm_update_power(dtpm, power_min, power_max); +} + +static u64 set_pd_power_limit(struct dtpm *dtpm, u64 power_limit) +{ + struct dtpm_cpu *dtpm_cpu = dtpm->private; + struct em_perf_domain *pd; + struct cpumask cpus; + unsigned long freq; + u64 power; + int i, nr_cpus; + + pd = em_cpu_get(dtpm_cpu->cpu); + + cpumask_and(&cpus, cpu_online_mask, to_cpumask(pd->cpus)); + + nr_cpus = cpumask_weight(&cpus); + + for (i = 0; i < pd->nr_perf_states; i++) { + + power = pd->table[i].power * MICROWATT_PER_MILLIWATT * nr_cpus; + + if (power > power_limit) + break; + } + + freq = pd->table[i - 1].frequency; + + freq_qos_update_request(&dtpm_cpu->qos_req, freq); + + power_limit = pd->table[i - 1].power * + MICROWATT_PER_MILLIWATT * nr_cpus; + + return power_limit; +} + +static u64 get_pd_power_uw(struct dtpm *dtpm) +{ + struct dtpm_cpu *dtpm_cpu = dtpm->private; + struct em_perf_domain *pd; + struct cpumask cpus; + unsigned long freq; + int i, nr_cpus; + + pd = em_cpu_get(dtpm_cpu->cpu); + freq = cpufreq_quick_get(dtpm_cpu->cpu); + cpumask_and(&cpus, cpu_online_mask, to_cpumask(pd->cpus)); + nr_cpus = cpumask_weight(&cpus); + + for (i = 0; i < pd->nr_perf_states; i++) { + + if (pd->table[i].frequency < freq) + continue; + + return pd->table[i].power * + MICROWATT_PER_MILLIWATT * nr_cpus; + } + + return 0; +} + +static void pd_release(struct dtpm *dtpm) +{ + struct dtpm_cpu *dtpm_cpu = dtpm->private; + + freq_qos_remove_request(&dtpm_cpu->qos_req); + kfree(dtpm_cpu); +} + +static struct dtpm_ops dtpm_ops = { + .set_power_uw = set_pd_power_limit, + .get_power_uw = get_pd_power_uw, + .release = pd_release, +}; + +static int cpuhp_dtpm_cpu_offline(unsigned int cpu) +{ + struct cpufreq_policy *policy; + struct em_perf_domain *pd; + struct dtpm *dtpm; + + policy = cpufreq_cpu_get(cpu); + + if (!policy) + return 0; + + pd = em_cpu_get(cpu); + if (!pd) + return -EINVAL; + + dtpm = per_cpu(dtpm_per_cpu, cpu); + + power_sub(dtpm, pd); + + if (cpumask_weight(policy->cpus) != 1) + return 0; + + for_each_cpu(cpu, policy->related_cpus) + per_cpu(dtpm_per_cpu, cpu) = NULL; + + dtpm_unregister(dtpm); + + return 0; +} + +static int cpuhp_dtpm_cpu_online(unsigned int cpu) +{ + struct dtpm *dtpm; + struct dtpm_cpu *dtpm_cpu; + struct cpufreq_policy *policy; + struct em_perf_domain *pd; + char name[CPUFREQ_NAME_LEN]; + int ret; + + policy = cpufreq_cpu_get(cpu); + + if (!policy) + return 0; + + pd = em_cpu_get(cpu); + if (!pd) + return -EINVAL; + + dtpm = per_cpu(dtpm_per_cpu, cpu); + if (dtpm) + return power_add(dtpm, pd); + + dtpm = dtpm_alloc(&dtpm_ops); + if (!dtpm) + return -EINVAL; + + dtpm_cpu = kzalloc(sizeof(dtpm_cpu), GFP_KERNEL); + if (!dtpm_cpu) { + kfree(dtpm); + return -ENOMEM; + } + + dtpm->private = dtpm_cpu; + dtpm_cpu->cpu = cpu; + + for_each_cpu(cpu, policy->related_cpus) + per_cpu(dtpm_per_cpu, cpu) = dtpm; + + sprintf(name, "cpu%d", dtpm_cpu->cpu); + + ret = dtpm_register(name, dtpm, __parent); + if (ret) + goto out_kfree_dtpm_cpu; + + ret = power_add(dtpm, pd); + if (ret) + goto out_power_sub; + + ret = freq_qos_add_request(&policy->constraints, + &dtpm_cpu->qos_req, FREQ_QOS_MAX, + pd->table[pd->nr_perf_states - 1].frequency); + if (ret) + goto out_dtpm_unregister; + + return 0; + +out_dtpm_unregister: + dtpm_unregister(dtpm); + dtpm_cpu = NULL; /* Already freed by the release ops */ +out_power_sub: + power_sub(dtpm, pd); +out_kfree_dtpm_cpu: + for_each_cpu(cpu, policy->related_cpus) + per_cpu(dtpm_per_cpu, cpu) = NULL; + kfree(dtpm_cpu); + + return ret; +} + +int dtpm_register_cpu(struct dtpm *parent) +{ + __parent = parent; + + return cpuhp_setup_state(CPUHP_AP_DTPM_CPU_ONLINE, + "dtpm_cpu:online", + cpuhp_dtpm_cpu_online, + cpuhp_dtpm_cpu_offline); +} diff --git a/include/linux/cpuhotplug.h b/include/linux/cpuhotplug.h index bc56287a1ed1..72fd8db62342 100644 --- a/include/linux/cpuhotplug.h +++ b/include/linux/cpuhotplug.h @@ -192,6 +192,7 @@ enum cpuhp_state { CPUHP_AP_ONLINE_DYN_END = CPUHP_AP_ONLINE_DYN + 30, CPUHP_AP_X86_HPET_ONLINE, CPUHP_AP_X86_KVM_CLK_ONLINE, + CPUHP_AP_DTPM_CPU_ONLINE, CPUHP_AP_ACTIVE, CPUHP_ONLINE, }; diff --git a/include/linux/dtpm.h b/include/linux/dtpm.h index 84b087b4ac01..723fe51f1628 100644 --- a/include/linux/dtpm.h +++ b/include/linux/dtpm.h @@ -73,4 +73,6 @@ void dtpm_unregister(struct dtpm *dtpm); int dtpm_register(const char *name, struct dtpm *dtpm, struct dtpm *parent); +int dtpm_register_cpu(struct dtpm *parent); + #endif