From patchwork Thu Nov 26 15:29:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Loic Poulain X-Patchwork-Id: 333029 Delivered-To: patch@linaro.org Received: by 2002:a92:5e16:0:0:0:0:0 with SMTP id s22csp1402520ilb; Thu, 26 Nov 2020 07:22:55 -0800 (PST) X-Google-Smtp-Source: ABdhPJyh4sjDGxGFIe3eXfC87mosmNVMF3bq6APJA33H6yPf/+KuCmVcG397DCPRAsCW8m1bNnvN X-Received: by 2002:a17:906:3bd6:: with SMTP id v22mr3226536ejf.160.1606404175678; Thu, 26 Nov 2020 07:22:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606404175; cv=none; d=google.com; s=arc-20160816; b=RvU88iq7oOMen0ogPJ4WrC1DEMnLvSHxiGjnOExePaQ0uHCCVElfJ78DH1nWerUUdl Chj6rBVV8QRD0xTHCtxvf+nL/28TGGE6NZW1LsYk4sACyKMNE2BqdZ3Q5L8mXCmO9tTv /EuTn6qfDsgqH8sqaY4KLP4Y9eyAoBSLYpF58a/xC22c+LRzTIjYi4ZdfJgueVGaHuY2 5MlP279zcMjPWdG+lt1gNtbAjLx3vptOaG0om2fse79RNRdGoT38bwv4ll3rzoI/8DOv nrpLWrX70iZCyCBbwTq8TCUgnrFsJx1EoJIRe70EybW+IwQWFsydu1jBKGDWXaCGNOm+ QkdA== 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=S2YIODlUjYnbR8vGoOQUbNfWgpKp3X534Xh5o6umMnc=; b=HOVwHBJdRepw7+ePQ37DwAhgyIXRokaQoTTpqinQYB3NtUISdDaXwqbivuYRn4gUUX 1QIU2KXnl1ZREqF8AeLvMYB/m/Z2XVSfisUyhKRQjSXGJMG74Q6BpjNIcm1praslyAvA STEvPPXjEcIT21lQ5Z8moYrRx3Rl7IyPJnmPEBViGUhUutHbVVTwHSMVQNjAjkS1x5+0 Ytu5uadoctJ0NgNPqLUbSvuE08R7OTbi9B7f6A0RztYQHESQ10IO8jENlvkEwZ0ECqZY FLBmZbe6ZW7MPmAzd5ljXZEdXYn62oAhfGTqc3zcKor8VMA1wlWQSLRyDCgDskkS+uX3 DzoQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=CCzGvHau; spf=pass (google.com: domain of linux-arm-msm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-arm-msm-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 w10si3453177edx.350.2020.11.26.07.22.55; Thu, 26 Nov 2020 07:22:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-arm-msm-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=CCzGvHau; spf=pass (google.com: domain of linux-arm-msm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-arm-msm-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 S2391056AbgKZPWX (ORCPT + 15 others); Thu, 26 Nov 2020 10:22:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46132 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391296AbgKZPWX (ORCPT ); Thu, 26 Nov 2020 10:22:23 -0500 Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B92C2C0613D4 for ; Thu, 26 Nov 2020 07:22:21 -0800 (PST) Received: by mail-wr1-x443.google.com with SMTP id i2so2528946wrs.4 for ; Thu, 26 Nov 2020 07:22: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=S2YIODlUjYnbR8vGoOQUbNfWgpKp3X534Xh5o6umMnc=; b=CCzGvHauZhf4/QksJfvsqlG9zeCJRrIjf1w8Pc/wtTGAItJnr788oMD3jPFf8CkV2B 60qUbQK7Alq7CE8yolcjB3MXJnQuKyDbZrkW2DMUFzDbpvGHKGL6n1f5gOwRiyFQFrI5 CVO/j+iFHCo89B1dnZCZXdcwpajNhnxA+X/fcd+bNr2k//mpX0Li1kCNIrBsH+m+WyKZ o+0aJ2DMdgOay5lvsqLSGASsV9FavWSj05vKMGvoAQRwdYWacZSMhcs+nkJCSLoaCP6c OjR94Hlb4/TaIRL8+zSBZhOpnL2DzTBPV2440I3csQC0o3WqMguN0riQPPQaKVWt6Zny 2y0A== 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=S2YIODlUjYnbR8vGoOQUbNfWgpKp3X534Xh5o6umMnc=; b=GExoCMaghUkydkj05Q7WWHuVFyqOUM9oZdtX7hvPQC3SiyzGHliOOGdR8YnU8ZYa/V MPau1u9/yII6Y2x+WG+n/KsbFQnyNjdjgf0QuTn8kib7TW8ZjSt+2rkhUG9sCpQ26Ieo rRPhBrCyrATnquIYOc6qLh+Jf1GAUTRw1KWMQgoSjJbpydIkDgWCdybYAFxTy2bnrZep xc2Atc4kIeoigjUKPxkNPwP7HjeEP0qS67PorcYAGHdflsrllodpYQBBvhJSOrHm2p2b mjc4FztGr6siQSb1oGwfFH3zljaTAObygAtQh42yRmu+sVO9mfGaVgybRSJ/aqO5RoSI bKvQ== X-Gm-Message-State: AOAM532pKGsS5HXAXzgvE3FcD9LZAF49NvGLYRSoZLWBgCDLhGTSaPhb 0SMvlaj8AokqU2Qz6HcdG/TAgQ== X-Received: by 2002:a5d:6046:: with SMTP id j6mr4467104wrt.317.1606404140414; Thu, 26 Nov 2020 07:22:20 -0800 (PST) Received: from localhost.localdomain ([88.122.66.28]) by smtp.gmail.com with ESMTPSA id l10sm2756144wme.42.2020.11.26.07.22.19 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 26 Nov 2020 07:22:19 -0800 (PST) From: Loic Poulain To: manivannan.sadhasivam@linaro.org, hemantk@codeaurora.org Cc: linux-arm-msm@vger.kernel.org, bbhatt@codeaurora.org, Loic Poulain Subject: [PATCH v3 6/9] mhi: pci_generic: Add suspend/resume/recovery procedure Date: Thu, 26 Nov 2020 16:29:04 +0100 Message-Id: <1606404547-10737-7-git-send-email-loic.poulain@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1606404547-10737-1-git-send-email-loic.poulain@linaro.org> References: <1606404547-10737-1-git-send-email-loic.poulain@linaro.org> Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org Add support for system wide suspend/resume. During suspend, MHI device controller must be put in M3 state and PCI bus in D3 state. Add a recovery procedure allowing to reinitialize the device in case of error during resume steps, which can happen if device loses power (and so its context) while system suspend. Signed-off-by: Loic Poulain --- drivers/bus/mhi/pci_generic.c | 102 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) -- 2.7.4 diff --git a/drivers/bus/mhi/pci_generic.c b/drivers/bus/mhi/pci_generic.c index 67fcbcf..9919ad6 100644 --- a/drivers/bus/mhi/pci_generic.c +++ b/drivers/bus/mhi/pci_generic.c @@ -13,6 +13,7 @@ #include #include #include +#include #define MHI_PCI_DEFAULT_BAR_NUM 0 @@ -187,6 +188,7 @@ enum mhi_pci_device_status { struct mhi_pci_device { struct mhi_controller mhi_cntrl; struct pci_saved_state *pci_state; + struct work_struct recovery_work; unsigned long status; }; @@ -319,6 +321,48 @@ static void mhi_pci_runtime_put(struct mhi_controller *mhi_cntrl) /* no PM for now */ } +static void mhi_pci_recovery_work(struct work_struct *work) +{ + struct mhi_pci_device *mhi_pdev = container_of(work, struct mhi_pci_device, + recovery_work); + struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; + struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev); + int err; + + dev_warn(&pdev->dev, "device recovery started\n"); + + /* Clean up MHI state */ + if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) { + mhi_power_down(mhi_cntrl, false); + mhi_unprepare_after_power_down(mhi_cntrl); + } + + /* Check if we can recover without full reset */ + pci_set_power_state(pdev, PCI_D0); + pci_load_saved_state(pdev, mhi_pdev->pci_state); + pci_restore_state(pdev); + + if (!mhi_pci_is_alive(mhi_cntrl)) + goto err_try_reset; + + err = mhi_prepare_for_power_up(mhi_cntrl); + if (err) + goto err_try_reset; + + err = mhi_sync_power_up(mhi_cntrl); + if (err) + goto err_unprepare; + + set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status); + return; + +err_unprepare: + mhi_unprepare_after_power_down(mhi_cntrl); +err_try_reset: + if (pci_reset_function(pdev)) + dev_err(&pdev->dev, "Recovery failed\n"); +} + static int mhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) { const struct mhi_pci_dev_info *info = (struct mhi_pci_dev_info *) id->driver_data; @@ -333,6 +377,8 @@ static int mhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) if (!mhi_pdev) return -ENOMEM; + INIT_WORK(&mhi_pdev->recovery_work, mhi_pci_recovery_work); + mhi_cntrl_config = info->config; mhi_cntrl = &mhi_pdev->mhi_cntrl; @@ -397,6 +443,8 @@ static void mhi_pci_remove(struct pci_dev *pdev) struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev); struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; + cancel_work_sync(&mhi_pdev->recovery_work); + if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) { mhi_power_down(mhi_cntrl, true); mhi_unprepare_after_power_down(mhi_cntrl); @@ -465,12 +513,66 @@ static const struct pci_error_handlers mhi_pci_err_handler = { .reset_done = mhi_pci_reset_done, }; +int __maybe_unused mhi_pci_suspend(struct device *dev) +{ + struct pci_dev *pdev = to_pci_dev(dev); + struct mhi_pci_device *mhi_pdev = dev_get_drvdata(dev); + struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; + + cancel_work_sync(&mhi_pdev->recovery_work); + + /* Transition to M3 state */ + mhi_pm_suspend(mhi_cntrl); + + pci_save_state(pdev); + pci_disable_device(pdev); + pci_wake_from_d3(pdev, true); + pci_set_power_state(pdev, PCI_D3hot); + + return 0; +} + +static int __maybe_unused mhi_pci_resume(struct device *dev) +{ + struct pci_dev *pdev = to_pci_dev(dev); + struct mhi_pci_device *mhi_pdev = dev_get_drvdata(dev); + struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; + int err; + + pci_set_power_state(pdev, PCI_D0); + pci_restore_state(pdev); + pci_set_master(pdev); + + err = pci_enable_device(pdev); + if (err) + goto err_recovery; + + /* Exit M3, transition to M0 state */ + err = mhi_pm_resume(mhi_cntrl); + if (err) { + dev_err(&pdev->dev, "failed to resume device: %d\n", err); + goto err_recovery; + } + + return 0; + +err_recovery: + /* The device may have loose power or crashed, try recovering it */ + queue_work(system_long_wq, &mhi_pdev->recovery_work); + return 0; +} + +static const struct dev_pm_ops mhi_pci_pm_ops = { + SET_SYSTEM_SLEEP_PM_OPS(mhi_pci_suspend, mhi_pci_resume) +}; + static struct pci_driver mhi_pci_driver = { .name = "mhi-pci-generic", .id_table = mhi_pci_id_table, .probe = mhi_pci_probe, .remove = mhi_pci_remove, .err_handler = &mhi_pci_err_handler, + .driver.pm = &mhi_pci_pm_ops }; module_pci_driver(mhi_pci_driver);