From patchwork Thu Dec 10 10:02:51 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Loic Poulain X-Patchwork-Id: 340975 Delivered-To: patch@linaro.org Received: by 2002:a02:85a7:0:0:0:0:0 with SMTP id d36csp5381720jai; Thu, 10 Dec 2020 01:57:02 -0800 (PST) X-Google-Smtp-Source: ABdhPJw7fyyFxJPGnhK7Q5HwqsydcSWv5+1xXpKP/IZg0K12FLnOt9KsgUYjkhk5R1XLeS2xlK5t X-Received: by 2002:a17:906:369b:: with SMTP id a27mr5826088ejc.183.1607594222443; Thu, 10 Dec 2020 01:57:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607594222; cv=none; d=google.com; s=arc-20160816; b=F3WKL567iEONCW/vWYmqVL7OIPyw5uD1NX2RccvxY3a365dcxFf+NKNJYeypbqKqjm r518k9otg/KVQWPTZujC0dscG8XN+sTTK8UO4t1eHISt7CA0vxHf1o2gNJ28t2S28jIM uJn03cLx3kjjr++/DJBXuxQWaNbhGyyZn8fvGSk2RcZTFiCtZj8t3A+xZpr1FkWba9xt n01NlbkJuZRhwDbdqXysqr62TyynhL7LQ9uQ8UdM84Lt9pV3saEuuDcJIeMqprveB7xX 7WzuZJG+A9I4MD3lD4hpIymiOL/B77k4ZbP+VSFAOIw2zOCmBmcx5Z9te9MOyJu9MiMT WqzA== 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=YZ51yJhwC/8ztxtdW3nAMxNQRU2ROlrQxWx95f0lCfc=; b=jfTuVEX2I44yl+AQ3rXmemPmtJ9nwhHx8YDQEq9iSzLVspokuDzFBGbPuiP32F1Mim 7EAQCjuLxuB0Zc+9EOrfJr1TxbNRx4rnsoheRRKK/gQN46TwIlR/GQaBJ5z/O1SRzipn sq3sJk7OM10NOkviW+zJDvCpEMmzQd8+hZ2ZDGCbIU7AulqDKi39rWAV6PXkEgIRmWyU kKgA5pLFEtW1UWLBxwiJdZ5ibcRqqvBS/N18TKQ+y+ntAKrizLiXXUmSulfMyhtaO0PK fKPFSOLIoiksn5qf8FAbUg48ebnVkhG3TdrdCQCdO+byF2u65u3eCsjYuQaLMGfUuz7D 74qQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=JyIdNHZK; 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 11si2499658edv.53.2020.12.10.01.57.02; Thu, 10 Dec 2020 01:57:02 -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=JyIdNHZK; 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 S1728773AbgLJJ5B (ORCPT + 15 others); Thu, 10 Dec 2020 04:57:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729892AbgLJJ4u (ORCPT ); Thu, 10 Dec 2020 04:56:50 -0500 Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B2054C06179C for ; Thu, 10 Dec 2020 01:56:01 -0800 (PST) Received: by mail-wr1-x441.google.com with SMTP id t4so4770832wrr.12 for ; Thu, 10 Dec 2020 01:56:01 -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=YZ51yJhwC/8ztxtdW3nAMxNQRU2ROlrQxWx95f0lCfc=; b=JyIdNHZKTZmx1AvZY97gv1LHCcJvHeOggXeryBZLV6uTHhe6y6Pjr/bpwzZ92UxH9m uY/FWf+MLOTO/WMAtC5m08pLFOqRctzHpjPOF7bVdCJlxyi19b3Kq46WZl4L8zwG+jDI pAwTg1xCMHiSm/G6uSK4t8GRKQRDB8OkaFb1ZIpcy19enTluDggJjqfVW2uaLR5CrDjS ZmU06H449cSJoc+J3bNSu+lt0M+gcQ3oFWSvHgAzmb3CBjI2MhBc0hg8CB8fxW4XvpC6 iyvTV/Agl0N8KPhsfVdi8CFHVlH/bBtL0KEnzyt2tr/nGE4J8m0sifo5OYahz1isyiJs 6EQg== 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=YZ51yJhwC/8ztxtdW3nAMxNQRU2ROlrQxWx95f0lCfc=; b=JF/nLoDLma0Gc78emnvkAjkZ8zMLSvpxCa6IaKXhSvKn1lLBZSWq4gRn2qIGSmj56O PIR2klOf3aTM/4YEafQDzF3xnCHgtAlrhTiiUyuSoM1s9COlPlmdCbtgkWUyfPT6cHZp ESPgMKSfoqcFAGrJXUqkFARxkwCyUFWkroFr3KFDFkSKgJiMnC+yhnRlM2yNO/FpfP/w BkBYz9uO2OeIbRBKWWJNKb4UVc9j4RmiIOoCzbYKZgjHYjQRxcl5nghrA5vLuhWYu6Pt Sz1xNHOKgW3YXSGOdlYSKRGdzuUmlr7+xdKJ4HTVAuSaFOszDRBY8O9vxvJqybiUq4nr COXQ== X-Gm-Message-State: AOAM532bxisup48ruxTe8AaFAuVE5X/0P/pzJsh+mH7iuLmYGja3IyTv d0SUw4djp5jX3czvm4zHHuaoEA== X-Received: by 2002:adf:9b98:: with SMTP id d24mr7254286wrc.240.1607594160467; Thu, 10 Dec 2020 01:56:00 -0800 (PST) Received: from localhost.localdomain ([2a01:e0a:490:8730:4468:1cc2:be0c:233f]) by smtp.gmail.com with ESMTPSA id l16sm9043721wrx.5.2020.12.10.01.55.59 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 10 Dec 2020 01:56:00 -0800 (PST) From: Loic Poulain To: manivannan.sadhasivam@linaro.org, hemantk@codeaurora.org Cc: linux-arm-msm@vger.kernel.org, jhugo@codeaurora.org, bbhatt@codeaurora.org, Loic Poulain Subject: [PATCH v4 06/10] mhi: pci_generic: Add suspend/resume/recovery procedure Date: Thu, 10 Dec 2020 11:02:51 +0100 Message-Id: <1607594575-31590-7-git-send-email-loic.poulain@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1607594575-31590-1-git-send-email-loic.poulain@linaro.org> References: <1607594575-31590-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 Reviewed-by: Hemant Kumar diff --git a/drivers/bus/mhi/pci_generic.c b/drivers/bus/mhi/pci_generic.c index d70d3ea..0c16f36 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 @@ -185,6 +186,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; }; @@ -312,6 +314,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; @@ -326,6 +370,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; @@ -390,6 +436,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); @@ -455,12 +503,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);