Received: by 10.213.65.68 with SMTP id h4csp2126286imn; Thu, 5 Apr 2018 09:23:03 -0700 (PDT) X-Google-Smtp-Source: AIpwx48OQ/dnWel+VMeANNPnu+CDfQ6JYezk1IIrCxksoP4mcOQQ+Xqf2/vTeODkvWd9BFZolJ8t X-Received: by 10.98.85.197 with SMTP id j188mr17582659pfb.86.1522945383286; Thu, 05 Apr 2018 09:23:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1522945383; cv=none; d=google.com; s=arc-20160816; b=c7ZBxc/cBPaza9cJKfGqAWmlGMJvabFwxZGpxZSdtzvUXE/CK7C4ltPW3im7Oyccjs kcU3fuphaM+QxSLHpS0oZCfAN2u5QRdXIFuNX8H5pSM/fZEDQK0bYQYzpDnIltYo53Cf POCNuLsZFT/9I+V+hGHTFueX2eNfMl+JeBvtgaMabNNGbPFRgt2dBsN3TIxSBFGkabAA HA+Q3Mdo3J3dVY/EDnw6kn9HURytbvEGai6hY+srltJjeBJwKLuhGkMy60cA85eoTYw2 jWq9/vyGevO42cjNBzTCUj5aEs1Bd8dLuIMIRb18/uqRnJ58YjHiDVs0q4i+4T+BQxju OhWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dmarc-filter:dkim-signature:dkim-signature :arc-authentication-results; bh=40vjeYD6wTEgrjf4tL7RhkpxEjBCbYMyST9pQxe5fZo=; b=WIMflQETNrTzTWMCA/YNy6qEPgnQ2t7w8gMLBJU714oqURBVdfNXGMOJHL+xgnshyC BW4i+1dVJfigFm4bUwHX7VPLia3h1vBxRa5GbTumxj3c6HVf9ImRO9BbEh7stO/S5mw5 l1pwXspb3PBSjeZaWv8V+9cGjJjRKAJ+1CcC+O6KLhRDo60fLUD3nhaq0MNAVBzfhoAN kmFBuTZ6BTse0zUCSItC3fJR/6S1E4p+CXY26vjj5o5GnkFxJaI+sxo9MN8zt7Wb61rV 3ATxhajc/vPQKvY2c+F1e/3LAMJdTAi1/9v1HUHXGeM9ZtKuJp0GJVCxS2m4mmZDxEag CUQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@codeaurora.org header.s=default header.b=b3VfI3rS; dkim=pass header.i=@codeaurora.org header.s=default header.b=HFLZRpyf; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u189si6368266pfb.102.2018.04.05.09.22.49; Thu, 05 Apr 2018 09:23:03 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@codeaurora.org header.s=default header.b=b3VfI3rS; dkim=pass header.i=@codeaurora.org header.s=default header.b=HFLZRpyf; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751678AbeDEQVF (ORCPT + 99 others); Thu, 5 Apr 2018 12:21:05 -0400 Received: from smtp.codeaurora.org ([198.145.29.96]:41704 "EHLO smtp.codeaurora.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752080AbeDEQSs (ORCPT ); Thu, 5 Apr 2018 12:18:48 -0400 Received: by smtp.codeaurora.org (Postfix, from userid 1000) id 4158160F90; Thu, 5 Apr 2018 16:18:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1522945127; bh=ooUxSBxwww8bEm12smBqHwbVpm2raZivEZ2uxWk4IUw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=b3VfI3rSMj8dqYuIPmv6BqkjT1huTVEcQwEMHA9NiuHeAOZvgpR0zOaftgsqgI+A2 9FRGrAmCh+gg9Z4ruuBAbZLz9PQ/F2c7ASeISNa1P11joDBmddVg5wWFTxJk9NXaT3 KDw1M3t6/DIDnIk2zXqJ8wzghXS49tR0s5eEQN2Y= X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on pdx-caf-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.8 required=2.0 tests=ALL_TRUSTED,BAYES_00, DKIM_SIGNED,T_DKIM_INVALID autolearn=no autolearn_force=no version=3.4.0 Received: from codeaurora.org (i-global254.qualcomm.com [199.106.103.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: ilina@smtp.codeaurora.org) by smtp.codeaurora.org (Postfix) with ESMTPSA id CA156602B6; Thu, 5 Apr 2018 16:18:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1522945124; bh=ooUxSBxwww8bEm12smBqHwbVpm2raZivEZ2uxWk4IUw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HFLZRpyf3RwrGRWkThJIVz7iazrd7bo1YZxQtBDD8E/GRVQoTeZabrO/eJKCFpYIJ 0QCjDRucpy3P5y2IBkaqF7nd+uwoHyfjMxEiHDnmIl+dC2k4iIkiqA9vtW6wRDXE8K 1iJmxYAmM2lYG4/cshZAghHWWAdmeR0EA8NrFWdU= DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org CA156602B6 Authentication-Results: pdx-caf-mail.web.codeaurora.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: pdx-caf-mail.web.codeaurora.org; spf=none smtp.mailfrom=ilina@codeaurora.org From: Lina Iyer To: andy.gross@linaro.org, david.brown@linaro.org, linux-arm-msm@vger.kernel.org, linux-soc@vger.kernel.org Cc: rnayak@codeaurora.org, bjorn.andersson@linaro.org, linux-kernel@vger.kernel.org, sboyd@kernel.org, evgreen@chromium.org, dianders@chromium.org, Lina Iyer Subject: [PATCH v5 04/10] drivers: qcom: rpmh: add RPMH helper functions Date: Thu, 5 Apr 2018 10:18:28 -0600 Message-Id: <20180405161834.3850-5-ilina@codeaurora.org> X-Mailer: git-send-email 2.16.3 In-Reply-To: <20180405161834.3850-1-ilina@codeaurora.org> References: <20180405161834.3850-1-ilina@codeaurora.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Sending RPMH requests and waiting for response from the controller through a callback is common functionality across all platform drivers. To simplify drivers, add a library functions to create RPMH client and send resource state requests. rpmh_write() is a synchronous blocking call that can be used to send active state requests. Signed-off-by: Lina Iyer --- Changes in v4: - use const struct tcs_cmd in API - remove wait count from this patch - changed -EFAULT to -EINVAL --- drivers/soc/qcom/Makefile | 4 +- drivers/soc/qcom/rpmh-internal.h | 2 + drivers/soc/qcom/rpmh-rsc.c | 7 ++ drivers/soc/qcom/rpmh.c | 253 +++++++++++++++++++++++++++++++++++++++ include/soc/qcom/rpmh.h | 34 ++++++ 5 files changed, 299 insertions(+), 1 deletion(-) create mode 100644 drivers/soc/qcom/rpmh.c create mode 100644 include/soc/qcom/rpmh.h diff --git a/drivers/soc/qcom/Makefile b/drivers/soc/qcom/Makefile index cb6300f6a8e9..bb395c3202ca 100644 --- a/drivers/soc/qcom/Makefile +++ b/drivers/soc/qcom/Makefile @@ -7,7 +7,9 @@ obj-$(CONFIG_QCOM_PM) += spm.o obj-$(CONFIG_QCOM_QMI_HELPERS) += qmi_helpers.o qmi_helpers-y += qmi_encdec.o qmi_interface.o obj-$(CONFIG_QCOM_RMTFS_MEM) += rmtfs_mem.o -obj-$(CONFIG_QCOM_RPMH) += rpmh-rsc.o +obj-$(CONFIG_QCOM_RPMH) += qcom_rpmh.o +qcom_rpmh-y += rpmh-rsc.o +qcom_rpmh-y += rpmh.o obj-$(CONFIG_QCOM_SMD_RPM) += smd-rpm.o obj-$(CONFIG_QCOM_SMEM) += smem.o obj-$(CONFIG_QCOM_SMEM_STATE) += smem_state.o diff --git a/drivers/soc/qcom/rpmh-internal.h b/drivers/soc/qcom/rpmh-internal.h index aa73ec4b3e42..125f9faec536 100644 --- a/drivers/soc/qcom/rpmh-internal.h +++ b/drivers/soc/qcom/rpmh-internal.h @@ -86,4 +86,6 @@ struct rsc_drv { int rpmh_rsc_send_data(struct rsc_drv *drv, const struct tcs_request *msg); +void rpmh_tx_done(const struct tcs_request *msg, int r); + #endif /* __RPM_INTERNAL_H__ */ diff --git a/drivers/soc/qcom/rpmh-rsc.c b/drivers/soc/qcom/rpmh-rsc.c index f604101a4fc2..c5cde917dba6 100644 --- a/drivers/soc/qcom/rpmh-rsc.c +++ b/drivers/soc/qcom/rpmh-rsc.c @@ -252,6 +252,8 @@ static void tcs_notify_tx_done(unsigned long data) struct rsc_drv *drv = (struct rsc_drv *)data; struct tcs_response *resp; unsigned long flags; + const struct tcs_request *msg; + int err; for (;;) { spin_lock_irqsave(&drv->drv_lock, flags); @@ -264,7 +266,10 @@ static void tcs_notify_tx_done(unsigned long data) list_del(&resp->list); spin_unlock_irqrestore(&drv->drv_lock, flags); trace_rpmh_notify_tx_done(drv, resp); + msg = resp->msg; + err = resp->err; free_response(resp); + rpmh_tx_done(msg, err); } } @@ -554,6 +559,8 @@ static int rpmh_rsc_probe(struct platform_device *pdev) /* Enable the active TCS to send requests immediately */ write_tcs_reg(drv, RSC_DRV_IRQ_ENABLE, 0, 0, drv->tcs[ACTIVE_TCS].mask); + dev_set_drvdata(&pdev->dev, drv); + return devm_of_platform_populate(&pdev->dev); } diff --git a/drivers/soc/qcom/rpmh.c b/drivers/soc/qcom/rpmh.c new file mode 100644 index 000000000000..e3c7491e7baf --- /dev/null +++ b/drivers/soc/qcom/rpmh.c @@ -0,0 +1,253 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "rpmh-internal.h" + +#define RPMH_MAX_MBOXES 2 +#define RPMH_TIMEOUT_MS 10000 + +#define DEFINE_RPMH_MSG_ONSTACK(rc, s, q, name) \ + struct rpmh_request name = { \ + .msg = { \ + .state = s, \ + .cmds = name.cmd, \ + .num_cmds = 0, \ + .wait_for_compl = true, \ + }, \ + .cmd = { { 0 } }, \ + .completion = q, \ + .rc = rc, \ + } + +/** + * struct rpmh_request: the message to be sent to rpmh-rsc + * + * @msg: the request + * @cmd: the payload that will be part of the @msg + * @completion: triggered when request is done + * @err: err return from the controller + */ +struct rpmh_request { + struct tcs_request msg; + struct tcs_cmd cmd[MAX_RPMH_PAYLOAD]; + struct completion *completion; + struct rpmh_client *rc; + int err; +}; + +/** + * struct rpmh_ctrlr: our representation of the controller + * + * @drv: the controller instance + */ +struct rpmh_ctrlr { + struct rsc_drv *drv; +}; + +/** + * struct rpmh_client: the client object + * + * @dev: the platform device that is the owner + * @ctrlr: the controller associated with this client. + */ +struct rpmh_client { + struct device *dev; + struct rpmh_ctrlr *ctrlr; +}; + +static struct rpmh_ctrlr rpmh_rsc[RPMH_MAX_MBOXES]; +static DEFINE_MUTEX(rpmh_ctrlr_mutex); + +void rpmh_tx_done(const struct tcs_request *msg, int r) +{ + struct rpmh_request *rpm_msg = container_of(msg, struct rpmh_request, + msg); + struct completion *compl = rpm_msg->completion; + + rpm_msg->err = r; + + if (r) + dev_err(rpm_msg->rc->dev, + "RPMH TX fail in msg addr=%#x, err=%d\n", + rpm_msg->msg.cmds[0].addr, r); + + /* Signal the blocking thread we are done */ + if (compl) + complete(compl); +} +EXPORT_SYMBOL(rpmh_tx_done); + +/** + * wait_for_tx_done: Wait until the response is received. + * + * @rc: The RPMH client + * @compl: The completion object + * @addr: An addr that we sent in that request + * @data: The data for the address in that request + */ +static int wait_for_tx_done(struct rpmh_client *rc, + struct completion *compl, u32 addr, u32 data) +{ + int ret; + + might_sleep(); + + ret = wait_for_completion_timeout(compl, + msecs_to_jiffies(RPMH_TIMEOUT_MS)); + if (ret) + dev_dbg(rc->dev, + "RPMH response received addr=%#x data=%#x\n", + addr, data); + else + dev_err(rc->dev, + "RPMH response timeout addr=%#x data=%#x\n", + addr, data); + + return (ret > 0) ? 0 : -ETIMEDOUT; +} + +/** + * __rpmh_write: send the RPMH request + * + * @rc: The RPMH client + * @state: Active/Sleep request type + * @rpm_msg: The data that needs to be sent (cmds). + */ +static int __rpmh_write(struct rpmh_client *rc, enum rpmh_state state, + struct rpmh_request *rpm_msg) +{ + int ret = -EINVAL; + + rpm_msg->msg.state = state; + + if (state == RPMH_ACTIVE_ONLY_STATE) { + WARN_ON(irqs_disabled()); + ret = rpmh_rsc_send_data(rc->ctrlr->drv, &rpm_msg->msg); + if (!ret) + dev_dbg(rc->dev, + "RPMH request sent addr=%#x, data=%i#x\n", + rpm_msg->msg.cmds[0].addr, + rpm_msg->msg.cmds[0].data); + else + dev_warn(rc->dev, + "Error in RPMH request addr=%#x, data=%#x\n", + rpm_msg->msg.cmds[0].addr, + rpm_msg->msg.cmds[0].data); + } + + return ret; +} + +/** + * rpmh_write: Write a set of RPMH commands and block until response + * + * @rc: The RPMh handle got from rpmh_get_client + * @state: Active/sleep set + * @cmd: The payload data + * @n: The number of elements in @cmd + * + * May sleep. Do not call from atomic contexts. + */ +int rpmh_write(struct rpmh_client *rc, enum rpmh_state state, + const struct tcs_cmd *cmd, u32 n) +{ + DECLARE_COMPLETION_ONSTACK(compl); + DEFINE_RPMH_MSG_ONSTACK(rc, state, &compl, rpm_msg); + int ret; + + if (IS_ERR_OR_NULL(rc) || !cmd || !n || n > MAX_RPMH_PAYLOAD) + return -EINVAL; + + memcpy(rpm_msg.cmd, cmd, n * sizeof(*cmd)); + rpm_msg.msg.num_cmds = n; + + ret = __rpmh_write(rc, state, &rpm_msg); + if (ret) + return ret; + + return wait_for_tx_done(rc, &compl, cmd[0].addr, cmd[0].data); +} +EXPORT_SYMBOL(rpmh_write); + +static struct rpmh_ctrlr *get_rpmh_ctrlr(struct platform_device *pdev) +{ + int i; + struct rsc_drv *drv = dev_get_drvdata(pdev->dev.parent); + struct rpmh_ctrlr *ctrlr = ERR_PTR(-EINVAL); + + if (!drv) + return ctrlr; + + mutex_lock(&rpmh_ctrlr_mutex); + for (i = 0; i < RPMH_MAX_MBOXES; i++) { + if (rpmh_rsc[i].drv == drv) { + ctrlr = &rpmh_rsc[i]; + goto unlock; + } + } + + for (i = 0; i < RPMH_MAX_MBOXES; i++) { + if (rpmh_rsc[i].drv == NULL) { + ctrlr = &rpmh_rsc[i]; + ctrlr->drv = drv; + break; + } + } + WARN_ON(i == RPMH_MAX_MBOXES); +unlock: + mutex_unlock(&rpmh_ctrlr_mutex); + return ctrlr; +} + +/** + * rpmh_get_client: Get the RPMh handle + * + * @pdev: the platform device which needs to communicate with RPM + * accelerators + * May sleep. + */ +struct rpmh_client *rpmh_get_client(struct platform_device *pdev) +{ + struct rpmh_client *rc; + + rc = kzalloc(sizeof(*rc), GFP_KERNEL); + if (!rc) + return ERR_PTR(-ENOMEM); + + rc->dev = &pdev->dev; + rc->ctrlr = get_rpmh_ctrlr(pdev); + if (IS_ERR(rc->ctrlr)) { + kfree(rc); + return ERR_PTR(-EINVAL); + } + + return rc; +} +EXPORT_SYMBOL(rpmh_get_client); + +/** + * rpmh_release: Release the RPMH client + * + * @rc: The RPMh handle to be freed. + */ +void rpmh_release(struct rpmh_client *rc) +{ + kfree(rc); +} +EXPORT_SYMBOL(rpmh_release); diff --git a/include/soc/qcom/rpmh.h b/include/soc/qcom/rpmh.h new file mode 100644 index 000000000000..95334d4c1ede --- /dev/null +++ b/include/soc/qcom/rpmh.h @@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (c) 2016-2018, The Linux Foundation. All rights reserved. + */ + +#ifndef __SOC_QCOM_RPMH_H__ +#define __SOC_QCOM_RPMH_H__ + +#include +#include + +struct rpmh_client; + +#if IS_ENABLED(CONFIG_QCOM_RPMH) +int rpmh_write(struct rpmh_client *rc, enum rpmh_state state, + const struct tcs_cmd *cmd, u32 n); + +struct rpmh_client *rpmh_get_client(struct platform_device *pdev); + +void rpmh_release(struct rpmh_client *rc); + +#else + +static inline int rpmh_write(struct rpmh_client *rc, enum rpmh_state state, + const struct tcs_cmd *cmd, u32 n) +{ return -ENODEV; } + +static inline struct rpmh_client *rpmh_get_client(struct platform_device *pdev) +{ return ERR_PTR(-ENODEV); } + +static inline void rpmh_release(struct rpmh_client *rc) { } +#endif /* CONFIG_QCOM_RPMH */ + +#endif /* __SOC_QCOM_RPMH_H__ */ -- The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum, a Linux Foundation Collaborative Project