From patchwork Sat Apr 20 16:43:48 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Florian Fainelli X-Patchwork-Id: 1088418 Return-Path: X-Original-To: incoming-imx@patchwork.ozlabs.org Delivered-To: patchwork-incoming-imx@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-arm-kernel-bounces+incoming-imx=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="jMYhKPDi"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="mj1ldbgd"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 44mdw472HGz9s4Y for ; Sun, 21 Apr 2019 02:44:01 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Date:Message-ID:Subject: From:To:Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=cUyd4LHBJgn9G1OtNFASPLPR83+af0go2otWJvpApiI=; b=jMYhKPDihHLVdR PVnkes0zNQfP2zxZB53hSCNiaFP95rQ9ukEYGBovzSEehT4dcQHtkGFesHFY9+M9PfSMAC15QQ2Z2 r94RAOa3wlG8HLd/UP9xWLZ+oyD8lpCVvtFIvd+unmGle3xO0AtZaeOC8mQWkhg9drW5YUVq7tGzg MPFmKXoz+hC9N8UoIJTMBobcf42oI0oD48zCD5KsfBsgXifUZQD0anQnVOK/chQA51jUfkBnjFixf Qp2VyC0JtVG29S9x4Vcw/cJiKx3NgR8zu6AR3g7UIJS5OXCdKPgh0bzYGHRmF6kVWG/XXdXY4p1zT T2Y6dwxZYanrqT1GiCUg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1hHt5p-0001Gb-2p; Sat, 20 Apr 2019 16:43:57 +0000 Received: from mail-ed1-x543.google.com ([2a00:1450:4864:20::543]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1hHt5l-0001GE-8r for linux-arm-kernel@lists.infradead.org; Sat, 20 Apr 2019 16:43:54 +0000 Received: by mail-ed1-x543.google.com with SMTP id d1so6590058edd.13 for ; Sat, 20 Apr 2019 09:43:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:openpgp:message-id:date:user-agent:mime-version :content-language:content-transfer-encoding; bh=brUF2b+wAOM6SXOaF3N8J1yeIQAMPiB8AzAHPKqzNlg=; b=mj1ldbgdARfOfpNDlOdwimp7CrV4TPU1bAKEGt/s3Ta+0CinnpdTSgVUbrWC73FA4b GBfgOuyXwEJcUtuVBSrWofi5WLLkD6t5uK1CIS304hKuKnS7l/MgRXT8LXYb6TTC8pAn yif62ej47xUV7x4SSe1v5dopxHQlvi3Ww98X7HuKqkQ/dAuOEOFtTHfe2IY6/JQ2UUxL opFoJsJeGXctH/QIIeFnTbga1DKheb5yvbvuXxNv8u7jiY/0hxQXwdW/I7uyaQDqquiX NS2blmvJYm2K7/1V1YmRqD3a0HBjdCvQXOjwyy8DHbxwae4XxoORZrVfmvq8Id1HURxp Q/Zg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:openpgp:message-id:date :user-agent:mime-version:content-language:content-transfer-encoding; bh=brUF2b+wAOM6SXOaF3N8J1yeIQAMPiB8AzAHPKqzNlg=; b=Wxu+XX6VpwQVylS2doXWgUxz367ctsg0pIhMsYjdb20YRA0G+JtAFkeHRoVWWKRftD rKGuD+hwWitrJ0VHDuTnmgVYcq7qH2F8h+Btteod/Ia6gMfPFjn04i7xAzsM1054Tcx2 fdRsWXg53AHAC7zU/xzhlaaXnEzuCxssY2M8/3iF/RJEYZ1M0LwKwPQmVP6FnbzjI5lE 1HoXeZZFe9Ib25lOBlu6rv142nk6KlNEBe/X0nhZkTn0Bf9WP6g/jXbRzWbAe6EmWGUF 7Cl0PAr3fwhIcQ3OoOArCJMRuXwm4lisyq/9At5rvsri+ApxAaSoTZiNgrwS5J+SwinW TzQw== X-Gm-Message-State: APjAAAUtK7b6LCwXQgYX/m/GwylyQVA2gjk3xRblQIsLTsg1+x4VsapG RoX/nq/DeMxGfJ8MAHmcJIQ+9x1g X-Google-Smtp-Source: APXvYqw4qpmGsw66lYfExFMGs8YUe8zJE9VWNgQPPpCcsETp6NF/65rKDX5wf8fyxlRjAAdps63wOQ== X-Received: by 2002:a05:6402:781:: with SMTP id d1mr6094521edy.286.1555778630956; Sat, 20 Apr 2019 09:43:50 -0700 (PDT) Received: from [10.230.1.150] ([192.19.228.250]) by smtp.gmail.com with ESMTPSA id w6sm1393186ejq.73.2019.04.20.09.43.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 20 Apr 2019 09:43:50 -0700 (PDT) To: Sudeep Holla , Guenter Roeck , linux-hwmon@vger.kernel.org, "moderated list:ARM/FREESCALE IMX / MXC ARM ARCHITECTURE" From: Florian Fainelli Subject: SCMI sensor reads unit scaling Openpgp: preference=signencrypt Message-ID: <9207bc01-e0c6-ef73-6a24-6deda7b789c5@gmail.com> Date: Sat, 20 Apr 2019 09:43:48 -0700 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 Content-Language: en-US X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190420_094353_340780_AF5AAF57 X-CRM114-Status: GOOD ( 20.24 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.2 on bombadil.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider (f.fainelli[at]gmail.com) -0.0 SPF_PASS SPF: sender matches SPF record -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:543 listed in] [list.dnswl.org] -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+incoming-imx=patchwork.ozlabs.org@lists.infradead.org List-Id: linux-imx-kernel.lists.patchwork.ozlabs.org Hi Sudeep, Guenter, The current SCMI hwmon support does not seem to make use of the sensor scale/unit as defined in the sensor replies (or the update scale for that matter). I came up with the patch below which gets the job done, but I am worried about possibly breaking people's SCMI deployments and sensors reading because they may have intentionally or not already decided to return a value which is scaled the way Linux's hwmon expect it, and may, or may not have populated a valid unit number in the sensor reply. Ideally we should probably do two conversions: - from within scmi_sensor_reading_get(), scale the value as indicated by the reply - from within scmi_hwmon_read(), take that scaled value and apply the necessary conversion expected by Linux's HWMON conventions (e.g.: reporting voltage as mV values) What do you think? --- From: Florian Fainelli Subject: firmware: arm_scmi: Support sensor scaling The SCMI sensor management protocol includes the following provision: The power-of-10 multiplier in two’s-complement format that is applied to the sensor unit specified by the SensorType field. Add support for scaling the value returned based on what is provided by the firmware. This requires us to be able to look up a sensor identifier to its backing scmi_sensor_info structure and apply the necessary scale. Signed-off-by: Florian Fainelli --- drivers/firmware/arm_scmi/sensors.c | 37 ++++++++++++++++++++++++++++++++++++- include/linux/scmi_protocol.h | 1 + 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/drivers/firmware/arm_scmi/sensors.c b/drivers/firmware/arm_scmi/sensors.c index bbb469f..2eccc6a 100644 --- a/drivers/firmware/arm_scmi/sensors.c +++ b/drivers/firmware/arm_scmi/sensors.c @@ -33,7 +33,8 @@ struct scmi_msg_resp_sensor_description { #define NUM_TRIP_POINTS(x) (((x) >> 4) & 0xff) __le32 attributes_high; #define SENSOR_TYPE(x) ((x) & 0xff) -#define SENSOR_SCALE(x) (((x) >> 11) & 0x3f) +#define SENSOR_SCALE_MASK 0x3f +#define SENSOR_SCALE(x) (((x) >> 11) & SENSOR_SCALE_MASK) #define SENSOR_UPDATE_SCALE(x) (((x) >> 22) & 0x1f) #define SENSOR_UPDATE_BASE(x) (((x) >> 27) & 0x1f) u8 name[SCMI_MAX_STR_SIZE]; @@ -72,6 +73,35 @@ struct sensors_info { struct scmi_sensor_info *sensors; }; +static +struct scmi_sensor_info *scmi_sensor_info_from_id(struct sensors_info *si, + u32 sensor_id) +{ + struct scmi_sensor_info *s = NULL; + int i; + + for (i = 0; i < si->num_sensors; i++) { + s = &si->sensors[i]; + if (s->id == sensor_id) + return s; + } + + return s; +} + +static void inline scmi_scale_value(u8 scale, u64 *value) +{ + unsigned int i; + + if (scale & ((SENSOR_SCALE_MASK + 1) >> 1)) { + for (i = 0; i < (~scale) + 1; i++) + *value /= 10; + } else { + for (i = 0; i < scale; i++) + *value *= 10; + } +} + static int scmi_sensor_attributes_get(const struct scmi_handle *handle, struct sensors_info *si) { @@ -140,6 +170,7 @@ static int scmi_sensor_description_get(const struct scmi_handle *handle, s = &si->sensors[desc_index + cnt]; s->id = le32_to_cpu(buf->desc[cnt].id); s->type = SENSOR_TYPE(attrh); + s->scale = SENSOR_SCALE(attrh); memcpy(s->name, buf->desc[cnt].name, SCMI_MAX_STR_SIZE); } @@ -205,6 +236,8 @@ static int scmi_sensor_trip_point_set(const struct scmi_handle *handle, static int scmi_sensor_reading_get(const struct scmi_handle *handle, u32 sensor_id, bool async, u64 *value) { + struct sensors_info *si = handle->sensor_priv; + struct scmi_sensor_info *s = scmi_sensor_info_from_id(si, sensor_id); int ret; struct scmi_xfer *t; struct scmi_msg_sensor_reading_get *sensor; @@ -225,6 +258,8 @@ static int scmi_sensor_reading_get(const struct scmi_handle *handle, *value = le32_to_cpu(*pval); *value |= (u64)le32_to_cpu(*(pval + 1)) << 32; + if (s && s->scale) + scmi_scale_value(s->scale, value); } scmi_one_xfer_put(handle, t); diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h index b458c87..782b53e 100644 --- a/include/linux/scmi_protocol.h +++ b/include/linux/scmi_protocol.h @@ -140,6 +140,7 @@ struct scmi_power_ops { struct scmi_sensor_info { u32 id; u8 type; + u8 scale; char name[SCMI_MAX_STR_SIZE]; };