Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp1504123pxb; Sun, 11 Apr 2021 22:33:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzO8e/Mf3UzGN6ZDNjE3Dd5n5ey/q1TTdaQFFCwDLz1m80U1TQxqSkh2Fz2TqPDvhUXB2d9 X-Received: by 2002:a17:90a:3f08:: with SMTP id l8mr5848563pjc.136.1618205593916; Sun, 11 Apr 2021 22:33:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618205593; cv=none; d=google.com; s=arc-20160816; b=EetTHbrInyp/qXZ5eADGyqnsYZRg04L2ZuW/FwoT1yLDpE4lPCcrpF4JNqaFlGaEJb erjwW2i07EZHfD/RhBndSnsUioZwnyUT45TGr7lCDe7AT3JFi0FMeOAtg8lDkpKIlETe 6h5bC3ak+79bbwXW2DzH+4YSQ5qLrcAByq6cv5cKaHReYbmjQyU5UnhzAXSS8Py2Gl+B vopn/ZDiSdyQyFRGo7hPvFRmikRI1M7b6WQBbwJM2KQlb6jrRfa965/zZuugHteOOj1e 3gB9bAxsAUncc5NJeF9szhUTlJv6dAgjWkcgq1OeGBNi7On6vZKTDKpmd6zE5dbsUwhi GRqw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=QShRKzsEMp6U8GpuK5A8DEeE3/Uf7j3/vVAxV0LlqQc=; b=S87K+5uPBqukY/3mjDFfImdfYMb85MHYl/yT8HEhRdWraOTux4SX/U/ZKbxzp1tfPV eOqAQubJhCQmUbBV65mH0QSDA2drRfr+lEt26cHhXTSvUbbf0SnPtMzrY6uArnZ1yyhL KXvl7ySUYJtHoP2CuSw+SEvBAFB99GU1CMVLNfRKkWucn+f3sNKjABXtPFWx1hDuiY48 CtTZyyjzDAEsxqZOXb5VLeSv+/jXvuRgxf9vnkM1RgWzAxfrw64de0fCFGod0C/VJJoD eGk55WL+YLZigDJFbt35uFlLKEj2YRCNWB+kneFLura+mPxWMyjV9/Fh8j1iuzB0omFO t0bQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=AIiw6hUM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s5si12220606pgn.303.2021.04.11.22.33.00; Sun, 11 Apr 2021 22:33:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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=@chromium.org header.s=google header.b=AIiw6hUM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229493AbhDLFOi (ORCPT + 99 others); Mon, 12 Apr 2021 01:14:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45752 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229617AbhDLFOh (ORCPT ); Mon, 12 Apr 2021 01:14:37 -0400 Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EDCEBC06138D for ; Sun, 11 Apr 2021 22:14:19 -0700 (PDT) Received: by mail-pl1-x636.google.com with SMTP id d8so5718275plh.11 for ; Sun, 11 Apr 2021 22:14:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=QShRKzsEMp6U8GpuK5A8DEeE3/Uf7j3/vVAxV0LlqQc=; b=AIiw6hUM7qMzpIYYYhYaG+qhav+fbxWTnxxgdFbFOjCbtoFg1oVT8kC6jRXF5sTGG8 OPedReYxorhiKILob9A256qYhQSr0Eonok0fGQTB1nvGMc/dzLnSXALXQ/XN7tH6n4vY 3KKyoqnRbv1Av+paR2BV5f8gTACO88Qt5VhiA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=QShRKzsEMp6U8GpuK5A8DEeE3/Uf7j3/vVAxV0LlqQc=; b=JllBhvdKmxR/shYAlxPV8IPS7SFDkz2Gd1PXrX7MazNHvaR3cxBVGng251Sf7LyYNr oi91Gq8oEQ12jrmwRlSG0KZRJrrKg+Bil533WSFKYZkuQP190/87RVXR9Jy2Ie4lMIqD QZgHL8yhIJt+HGsRGwzJNyq9J1cnFykTr8+iH/0tfaDMALgDTI3sGnAFGyf2UBtbRJVf ocOcLX1M17YtLdpjomJMA0CSFm+jDLB8FIbx5B1hB841/emEobQrbqN0hqp4PpY/+isW X3s3Robj+hN+TpHakyqKX+IazHspXMN5o87ieR/5STC4KWIkIVP+U//q0wJEBsnHh9cj 3fxg== X-Gm-Message-State: AOAM530arU5DInrEkFSl+Z0bius2VnNTNWOcY87/mJ/RoSG8E3i+BKgY YmlB5ucHW7Brxo6aaB5r2Ool1UM1xUOu6su7oW6OWw== X-Received: by 2002:a17:90b:349:: with SMTP id fh9mr4936936pjb.126.1618204459324; Sun, 11 Apr 2021 22:14:19 -0700 (PDT) MIME-Version: 1.0 References: <1617874514-12282-1-git-send-email-chunfeng.yun@mediatek.com> <1617874514-12282-4-git-send-email-chunfeng.yun@mediatek.com> <1617958441.12105.39.camel@mhfsdcap03> In-Reply-To: <1617958441.12105.39.camel@mhfsdcap03> From: Ikjoon Jang Date: Mon, 12 Apr 2021 13:14:09 +0800 Message-ID: Subject: Re: [PATCH 4/6] usb: xhci-mtk: add support runtime PM To: Chunfeng Yun Cc: Rob Herring , Mathias Nyman , Greg Kroah-Hartman , Matthias Brugger , "Rafael J. Wysocki" , Len Brown , Pavel Machek , linux-usb@vger.kernel.org, "moderated list:ARM/Mediatek SoC support" , "moderated list:ARM/Mediatek SoC support" , "open list:OPEN FIRMWARE AND FLATTENED DEVICE TREE BINDINGS" , open list , linux-pm@vger.kernel.org, Tony Lindgren , Tianping Fang , Eddie Hung , Nicolas Boichat Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Apr 9, 2021 at 4:54 PM Chunfeng Yun wrote: > > On Fri, 2021-04-09 at 13:45 +0800, Ikjoon Jang wrote: > > On Thu, Apr 8, 2021 at 5:35 PM Chunfeng Yun wrote: > > > > > > A dedicated wakeup irq will be used to handle runtime suspend/resume, > > > we use dev_pm_set_dedicated_wake_irq API to take care of requesting > > > and attaching wakeup irq, then the suspend/resume framework will help > > > to enable/disable wakeup irq. > > > > > > The runtime PM is default off since some platforms may not support it. > > > users can enable it via power/control (set "auto") in sysfs. > > > > > > Signed-off-by: Chunfeng Yun > > > --- > > > drivers/usb/host/xhci-mtk.c | 140 +++++++++++++++++++++++++++++++----- > > > 1 file changed, 124 insertions(+), 16 deletions(-) > > > > > > diff --git a/drivers/usb/host/xhci-mtk.c b/drivers/usb/host/xhci-mtk.c > > > index a74764ab914a..30927f4064d4 100644 > > > --- a/drivers/usb/host/xhci-mtk.c > > > +++ b/drivers/usb/host/xhci-mtk.c > > > @@ -16,6 +16,7 @@ > > > #include > > > #include > > > #include > > > +#include > > > #include > > > #include > > > > > > @@ -358,7 +359,6 @@ static int usb_wakeup_of_property_parse(struct xhci_hcd_mtk *mtk, > > > mtk->uwk_reg_base, mtk->uwk_vers); > > > > > > return PTR_ERR_OR_ZERO(mtk->uwk); > > > - > > > } > > > > > > static void usb_wakeup_set(struct xhci_hcd_mtk *mtk, bool enable) > > > @@ -458,6 +458,7 @@ static int xhci_mtk_probe(struct platform_device *pdev) > > > struct resource *res; > > > struct usb_hcd *hcd; > > > int ret = -ENODEV; > > > + int wakeup_irq; > > > int irq; > > > > > > if (usb_disabled()) > > > @@ -485,6 +486,21 @@ static int xhci_mtk_probe(struct platform_device *pdev) > > > if (ret) > > > return ret; > > > > > > + irq = platform_get_irq_byname_optional(pdev, "host"); > > > + if (irq < 0) { > > > + if (irq == -EPROBE_DEFER) > > > + return irq; > > > + > > > + /* for backward compatibility */ > > > + irq = platform_get_irq(pdev, 0); > > > + if (irq < 0) > > > + return irq; > > > + } > > > + > > > + wakeup_irq = platform_get_irq_byname_optional(pdev, "wakeup"); > > > + if (wakeup_irq == -EPROBE_DEFER) > > > + return wakeup_irq; > > > + > > > mtk->lpm_support = of_property_read_bool(node, "usb3-lpm-capable"); > > > /* optional property, ignore the error if it does not exist */ > > > of_property_read_u32(node, "mediatek,u3p-dis-msk", > > > @@ -496,9 +512,11 @@ static int xhci_mtk_probe(struct platform_device *pdev) > > > return ret; > > > } > > > > > > + pm_runtime_set_active(dev); > > > + pm_runtime_use_autosuspend(dev); > > > + pm_runtime_set_autosuspend_delay(dev, 4000); > > > pm_runtime_enable(dev); > > > pm_runtime_get_sync(dev); > > > - device_enable_async_suspend(dev); > > > > > > ret = xhci_mtk_ldos_enable(mtk); > > > if (ret) > > > @@ -508,12 +526,6 @@ static int xhci_mtk_probe(struct platform_device *pdev) > > > if (ret) > > > goto disable_ldos; > > > > > > - irq = platform_get_irq(pdev, 0); > > > - if (irq < 0) { > > > - ret = irq; > > > - goto disable_clk; > > > - } > > > - > > > hcd = usb_create_hcd(driver, dev, dev_name(dev)); > > > if (!hcd) { > > > ret = -ENOMEM; > > > @@ -579,8 +591,26 @@ static int xhci_mtk_probe(struct platform_device *pdev) > > > if (ret) > > > goto dealloc_usb2_hcd; > > > > > > + if (wakeup_irq > 0) { > > > + ret = dev_pm_set_dedicated_wake_irq(dev, wakeup_irq); > > > + if (ret) { > > > + dev_err(dev, "set wakeup irq %d failed\n", wakeup_irq); > > > + goto dealloc_usb3_hcd; > > > + } > > > + dev_info(dev, "wakeup irq %d\n", wakeup_irq); > > > + } > > > + > > > + device_enable_async_suspend(dev); > > > + pm_runtime_mark_last_busy(dev); > > > + pm_runtime_put_autosuspend(dev); > > > + pm_runtime_forbid(dev); > > > + > > > return 0; > > > > > > +dealloc_usb3_hcd: > > > + usb_remove_hcd(xhci->shared_hcd); > > > + xhci->shared_hcd = NULL; > > > + > > > dealloc_usb2_hcd: > > > usb_remove_hcd(hcd); > > > > > > @@ -601,25 +631,26 @@ static int xhci_mtk_probe(struct platform_device *pdev) > > > xhci_mtk_ldos_disable(mtk); > > > > > > disable_pm: > > > - pm_runtime_put_sync(dev); > > > + pm_runtime_put_sync_autosuspend(dev); > > > pm_runtime_disable(dev); > > > return ret; > > > } > > > > > > -static int xhci_mtk_remove(struct platform_device *dev) > > > +static int xhci_mtk_remove(struct platform_device *pdev) > > > { > > > - struct xhci_hcd_mtk *mtk = platform_get_drvdata(dev); > > > + struct xhci_hcd_mtk *mtk = platform_get_drvdata(pdev); > > > struct usb_hcd *hcd = mtk->hcd; > > > struct xhci_hcd *xhci = hcd_to_xhci(hcd); > > > struct usb_hcd *shared_hcd = xhci->shared_hcd; > > > + struct device *dev = &pdev->dev; > > > > > > - pm_runtime_put_noidle(&dev->dev); > > > - pm_runtime_disable(&dev->dev); > > > + pm_runtime_get_sync(dev); > > > + xhci->xhc_state |= XHCI_STATE_REMOVING; > > > + dev_pm_clear_wake_irq(dev); > > > + device_init_wakeup(dev, false); > > > > > > usb_remove_hcd(shared_hcd); > > > xhci->shared_hcd = NULL; > > > - device_init_wakeup(&dev->dev, false); > > > - > > > usb_remove_hcd(hcd); > > > usb_put_hcd(shared_hcd); > > > usb_put_hcd(hcd); > > > @@ -627,6 +658,10 @@ static int xhci_mtk_remove(struct platform_device *dev) > > > xhci_mtk_clks_disable(mtk); > > > xhci_mtk_ldos_disable(mtk); > > > > > > + pm_runtime_disable(dev); > > > + pm_runtime_put_noidle(dev); > > > + pm_runtime_set_suspended(dev); > > > + > > > return 0; > > > } > > > > > > @@ -690,10 +725,83 @@ static int __maybe_unused xhci_mtk_resume(struct device *dev) > > > return ret; > > > } > > > > > > +static int check_rhub_status(struct xhci_hcd *xhci, struct xhci_hub *rhub) > > > +{ > > > + u32 suspended_ports; > > > + u32 status; > > > + int num_ports; > > > + int i; > > > + > > > + num_ports = rhub->num_ports; > > > + suspended_ports = rhub->bus_state.suspended_ports; > > > + for (i = 0; i < num_ports; i++) { > > > + if (!(suspended_ports & BIT(i))) { > > > + status = readl(rhub->ports[i]->addr); > > > + if (status & PORT_CONNECT) > > > > So this pm_runtime support is activated only when there's no devices > > connected at all? > No, if the connected devices also support runtime suspend, it will enter > suspend mode when no data transfer, then the controller can enter > suspend too > > I think this will always return -EBUSY with my board having an on-board hub > > connected to both rhubs. > the on-board hub supports runtime suspend by default, so if no devices > connected, it will enter suspend Sorry, you're correct. I was confused that the condition was (suspended && connect) My on-board hub connected to rhub is always in a suspended state whenever it's called. However, I don't think this could return -EBUSY rpm_suspend() only be called when all the descendants are in sleep already. Did you see any cases of this function returning -EBUSY or any concerns on here? > > > > > > + return -EBUSY; > > > + } > > > + } > > > + > > > + return 0; > > > +} > > > + > > > +/* > > > + * check the bus whether it could suspend or not > > > + * the bus will suspend if the downstream ports are already suspended, > > > + * or no devices connected. > > > + */ > > > +static int check_bus_status(struct xhci_hcd *xhci) > > > +{ > > > + int ret; > > > + > > > + ret = check_rhub_status(xhci, &xhci->usb3_rhub); > > > + if (ret) > > > + return ret; > > > + > > > + return check_rhub_status(xhci, &xhci->usb2_rhub); > > > +} > > > + > > > +static int __maybe_unused xhci_mtk_runtime_suspend(struct device *dev) > > > +{ > > > + struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev); > > > + struct xhci_hcd *xhci = hcd_to_xhci(mtk->hcd); > > > + int ret = 0; > > > + > > > + if (xhci->xhc_state) > > > + return -ESHUTDOWN; > > > + > > > + if (device_may_wakeup(dev)) { > > > + ret = check_bus_status(xhci); > > > + if (!ret) > > > + ret = xhci_mtk_suspend(dev); > > > + } > > > + > > > + /* -EBUSY: let PM automatically reschedule another autosuspend */ > > > + return ret ? -EBUSY : 0; > > > +} > > > + > > > +static int __maybe_unused xhci_mtk_runtime_resume(struct device *dev) > > > +{ > > > + struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev); > > > + struct xhci_hcd *xhci = hcd_to_xhci(mtk->hcd); > > > + int ret = 0; > > > + > > > + if (xhci->xhc_state) > > > + return -ESHUTDOWN; > > > + > > > + if (device_may_wakeup(dev)) > > > + ret = xhci_mtk_resume(dev); > > > + > > > + return ret; > > > +} > > > + > > > static const struct dev_pm_ops xhci_mtk_pm_ops = { > > > SET_SYSTEM_SLEEP_PM_OPS(xhci_mtk_suspend, xhci_mtk_resume) > > > + SET_RUNTIME_PM_OPS(xhci_mtk_runtime_suspend, > > > + xhci_mtk_runtime_resume, NULL) > > > }; > > > -#define DEV_PM_OPS IS_ENABLED(CONFIG_PM) ? &xhci_mtk_pm_ops : NULL > > > + > > > +#define DEV_PM_OPS (IS_ENABLED(CONFIG_PM) ? &xhci_mtk_pm_ops : NULL) > > > > > > static const struct of_device_id mtk_xhci_of_match[] = { > > > { .compatible = "mediatek,mt8173-xhci"}, > > > -- > > > 2.18.0 > > > >