From patchwork Tue Dec 29 08:43:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Loic Poulain X-Patchwork-Id: 353972 Delivered-To: patch@linaro.org Received: by 2002:a02:85a7:0:0:0:0:0 with SMTP id d36csp10371750jai; Tue, 29 Dec 2020 00:39:06 -0800 (PST) X-Google-Smtp-Source: ABdhPJy/J6d8LLr7O789Lf1HCIr2yS3LICZZmH78sCBavMpq+SY8ihy2OhjVOWVt2M/lrgi+DZFo X-Received: by 2002:a17:906:d152:: with SMTP id br18mr41210661ejb.297.1609231146253; Tue, 29 Dec 2020 00:39:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609231146; cv=none; d=google.com; s=arc-20160816; b=fRjbJABbbfiJSr91+Oiu/6jZf919NezP5/ARpEbgLh9FDwmokO6/uy4P8vVnOhoTAH iT02aE6x7wvgaK7QBZKVVCjcr4L9wzEfPZmOT4RFLIv+62ZROHDSShLPXUVzvySi6mZs YM+Ktpta9XyBBqFAyNCI5Q/wGa0i3lR1kMVuxTylCMLnKZCpvJF+6jYYd/QbkFFUqQa0 Jtu0pc5Nm3gJ+Bnj+ng5PZNAr46vWi5lWo6nCAQEmYAr5hKLq5T90kkkTsdsgTABDhw8 pqeL3ei+gRs9GJ2LRlybW/PjuFh3f220aYoP+x75ue7OPMuROydtva6mMfKqxFQ+9wCI Whbw== 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=LFcOLpi81oSUdisHEF+x7OxdMm5UlH9qgt1oY8gs1Nc=; b=b54kU8EcCf0AOuxFkvgxANX5+E/tNVn/vTF7aC7SSpmduY47prfplzXln51dOMG6CJ PkZ2b+uGq0rSk3Bc6xLqwgnQ4jxPyK1ZHyv4HmQOAwaVovmnb5vsk7SMIlyuvRux38Kh 7eosD1vrof9NmFhM9oKPUV/IiXGkIQTReOrpj6fMWqJz2/Qs5MhzG0bwZVTmGGeEUME0 jVyVvz/f93PXkoonLEHcSvgfFusTN+Kk69StO3MFUF1c/LWCNW0SLKhR3BTf4yLpVhzU XS0QZjKPwMP/lqNNRZmsp3yJ1QzQo8EYm64SeJmrB0H3igFDD5ILTWNHZ4xJOLDPbKRZ T+Hg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=nag8Wqhp; 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 y7si21816192edp.7.2020.12.29.00.39.06; Tue, 29 Dec 2020 00:39:06 -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=nag8Wqhp; 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 S1726148AbgL2Iht (ORCPT + 15 others); Tue, 29 Dec 2020 03:37:49 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60734 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726144AbgL2Ihs (ORCPT ); Tue, 29 Dec 2020 03:37:48 -0500 Received: from mail-ej1-x629.google.com (mail-ej1-x629.google.com [IPv6:2a00:1450:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 00E74C06179C for ; Tue, 29 Dec 2020 00:36:32 -0800 (PST) Received: by mail-ej1-x629.google.com with SMTP id q22so17269759eja.2 for ; Tue, 29 Dec 2020 00:36:31 -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=LFcOLpi81oSUdisHEF+x7OxdMm5UlH9qgt1oY8gs1Nc=; b=nag8WqhpdK2Kh32JnmQ5Qb1fAL3yKWQa+N0BlBFnB9vZYX017gUpuLlQTEJfywVVOr dBYkoKRN1RhaFSl70KRJB1IyE28GgmNj1LRgeZQAYFirfQpQjWe8gEwFtHsXfh+kBO1G jNd5TF7hIheEfjM7RGyd0m2hFsEhawdKsC4zgnTmAqylG1Y/W7CYkgIXoaLrIxn0A8og OmmvQLTaZOxEuUv02jSg3SrJ5VJ3LEEgkgUEBq1BIeDgFExW9p/uyzBKsYGauVpCXG2l Ns6mYGTMwg5DUkE1xYvoRAOJx78Wb55Zji7JtWaLqSsj5EwLlIxg+my2CAJOJscAmruw M2EA== 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=LFcOLpi81oSUdisHEF+x7OxdMm5UlH9qgt1oY8gs1Nc=; b=ffENfIHbuVPZ7OA4T86wTvMBXG1mz+QVvQhQJcmbS43OFDWHe4L6Hj01rEuKJ/8sIp zpoXqp9sFNpt0jZq7GAQTSYz/wbe4d/gqbqVbqzS2FoqQNMc7RQY3GaImo9EESshnqg7 MbegrLN3Khlz9z8vrjRnYNvz4ghmcyeAjXHtfCkDOtnPjOs5SqEwIv2XjbNXt85ZTgcs LxejUWW1D5EOFiW+n6hM+wx/IR5J8gxlRY0n6ZxUHcIrnFyjAoVRYSB05ypFqV+bLgHH JjrqYptuX6suMjP3xy9huC1MVS1Y4f3WXC+IaCTfGWMN9BUvQ2xkCYxJlZIrbXmHFFTK AMgQ== X-Gm-Message-State: AOAM531RS9y3SR+LY0TebT603YIVLnnpKBd4xAoPok1hYDuuvJ+ktO7Q KFdUkAwmVRyf40E8B5OKEsfkLg== X-Received: by 2002:a17:906:f9d7:: with SMTP id lj23mr45839530ejb.266.1609230990739; Tue, 29 Dec 2020 00:36:30 -0800 (PST) Received: from localhost.localdomain ([2a01:e0a:490:8730:6f69:290a:2b46:b9]) by smtp.gmail.com with ESMTPSA id c23sm37265143eds.88.2020.12.29.00.36.30 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 29 Dec 2020 00:36:30 -0800 (PST) From: Loic Poulain To: manivannan.sadhasivam@linaro.org Cc: linux-arm-msm@vger.kernel.org, bbhatt@codeaurora.org, hemantk@codeaurora.org, Loic Poulain Subject: [PATCH v6 06/10] mhi: pci_generic: Add suspend/resume/recovery procedure Date: Tue, 29 Dec 2020 09:43:47 +0100 Message-Id: <1609231431-10048-7-git-send-email-loic.poulain@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1609231431-10048-1-git-send-email-loic.poulain@linaro.org> References: <1609231431-10048-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 Reviewed-by Hemant Kumar --- drivers/bus/mhi/pci_generic.c | 105 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) -- 2.7.4 Reviewed-by: Manivannan Sadhasivam diff --git a/drivers/bus/mhi/pci_generic.c b/drivers/bus/mhi/pci_generic.c index 2521cd4..3297d95 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 @@ -186,6 +187,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; }; @@ -313,6 +315,50 @@ 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; + + dev_dbg(&pdev->dev, "Recovery completed\n"); + + 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; @@ -327,6 +373,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; @@ -391,6 +439,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); @@ -456,12 +506,67 @@ static const struct pci_error_handlers mhi_pci_err_handler = { .reset_done = mhi_pci_reset_done, }; +static 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 err; +} + +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);