Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp227099pxb; Wed, 4 Nov 2020 20:47:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJzes6zyRJBaT/yNWgU9j2pZdlNpy19x4sA3x5bBwxWeU8udan+LtB0IJ8ULDBgpXvLK/7az X-Received: by 2002:aa7:d404:: with SMTP id z4mr766919edq.334.1604551635514; Wed, 04 Nov 2020 20:47:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604551635; cv=none; d=google.com; s=arc-20160816; b=Q5mXq9gcOiuR8bk/KAEPbPwtKcSJOHRIhcC3TfeYAgTn7NT0+TDavwdjHNy2gRTjvv mN3OqsiKb7Mn/I9xY//q3ByU7qMtgLzGA02TsYsi5GNV9m+mqPhR2nbbNmYsOvYTeoZt YJqDvyXtEyzTsSALjWtLMxbix2O8y4tEYbJHkUDBgMwvJ0Pu3+8U5DLkJkSXX9GkH2kG xxpKR+ljvgLl5McDAJa4KnUG6hAUg43i+oWi3aQJ9i5VUiHOmQlOSOexSvTIdf+LxXht zfaEDakUYU5WIG7ij2FWYzD7KzXheEEgbfuR8qh76sjzYeXDZWIrh3JCQVYQ+EzUn7R2 9LVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:dlp-filter:cms-type :content-transfer-encoding:content-language:in-reply-to:mime-version :user-agent:date:message-id:organization:from:cc:to:subject :dkim-signature:dkim-filter; bh=jNvW1QYRp55tS9iML16rKRdyc1jSws940X+qvg5l9sU=; b=zZfmt/3reLH+K7xNSTE9+mXCuCkw+0s3hQAEQqIc13HdhTkAPodwDg7fRSlJX0FI9n FrMDeSfq77O4rmuwoyEvhpkzHYi2SMY2iEjFJxTHYiudGr4mM3wGml/NrSSy+zxxEUBT ZpYnIvlWngrRLdR8PkByIOf1U0VQvYuvkl2bWYjnIrgwvgvsPLW8Yy95PLm3Da8ED8IX vJaJr7XgCkxwJ3jPOG2wzf8OI85CoEsaGw+aPTHST/gUbN81URKaoxftxqJfYaXfe2r5 2At+PzW0feNVUY2sMCVLukqMECFANn6LXaz+SMKuEun5byMjWNccnpYv0fSCrOWUSvhU xYig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b=imiHgJQc; 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=samsung.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a10si300121eju.313.2020.11.04.20.46.51; Wed, 04 Nov 2020 20:47:15 -0800 (PST) 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=@samsung.com header.s=mail20170921 header.b=imiHgJQc; 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=samsung.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732154AbgKECQo (ORCPT + 99 others); Wed, 4 Nov 2020 21:16:44 -0500 Received: from mailout2.samsung.com ([203.254.224.25]:37208 "EHLO mailout2.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730781AbgKECQn (ORCPT ); Wed, 4 Nov 2020 21:16:43 -0500 Received: from epcas1p3.samsung.com (unknown [182.195.41.47]) by mailout2.samsung.com (KnoxPortal) with ESMTP id 20201105021641epoutp026b6a71ae1f6d5d9929caa7ab43ce3b3e~Eev6dGs4-1503215032epoutp02J for ; Thu, 5 Nov 2020 02:16:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout2.samsung.com 20201105021641epoutp026b6a71ae1f6d5d9929caa7ab43ce3b3e~Eev6dGs4-1503215032epoutp02J DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1604542601; bh=jNvW1QYRp55tS9iML16rKRdyc1jSws940X+qvg5l9sU=; h=Subject:To:Cc:From:Date:In-Reply-To:References:From; b=imiHgJQcSbLBhwreovbY4886e3vZr5lWRc94I3QHEd03V6ZFKGnat3Oc1jrEO1PlT 6L9lPkyW8UZv0zRLMJJmxbRqjx3PqKAMlAj9lFqvuidLykNzFFXs8pr8qQGt26ObbC MBTHpOzLGjIa3WXSoVN43ihoGtODw3NsthLVKSFE= Received: from epsnrtp1.localdomain (unknown [182.195.42.162]) by epcas1p4.samsung.com (KnoxPortal) with ESMTP id 20201105021640epcas1p48c5712936d8c8b5338d6357ddce4e8b6~Eev56-kB90090400904epcas1p4W; Thu, 5 Nov 2020 02:16:40 +0000 (GMT) Received: from epsmges1p3.samsung.com (unknown [182.195.40.154]) by epsnrtp1.localdomain (Postfix) with ESMTP id 4CRRwP742nzMqYm0; Thu, 5 Nov 2020 02:16:37 +0000 (GMT) Received: from epcas1p1.samsung.com ( [182.195.41.45]) by epsmges1p3.samsung.com (Symantec Messaging Gateway) with SMTP id 00.AF.09582.48063AF5; Thu, 5 Nov 2020 11:16:36 +0900 (KST) Received: from epsmtrp1.samsung.com (unknown [182.195.40.13]) by epcas1p4.samsung.com (KnoxPortal) with ESMTPA id 20201105021636epcas1p4ac5ca3b02c51ef248a64c1c659484362~Eev142zY03066930669epcas1p4B; Thu, 5 Nov 2020 02:16:36 +0000 (GMT) Received: from epsmgms1p2.samsung.com (unknown [182.195.42.42]) by epsmtrp1.samsung.com (KnoxPortal) with ESMTP id 20201105021636epsmtrp158daa64df16c9d5766645cf7677d3e0a~Eev135Wog0600806008epsmtrp1M; Thu, 5 Nov 2020 02:16:36 +0000 (GMT) X-AuditID: b6c32a37-899ff7000000256e-ed-5fa36084dc06 Received: from epsmtip2.samsung.com ( [182.195.34.31]) by epsmgms1p2.samsung.com (Symantec Messaging Gateway) with SMTP id C6.90.08745.48063AF5; Thu, 5 Nov 2020 11:16:36 +0900 (KST) Received: from [10.113.221.102] (unknown [10.113.221.102]) by epsmtip2.samsung.com (KnoxPortal) with ESMTPA id 20201105021635epsmtip2f5833d1c0346068df99822f88e5ea2f6~Eev1gCREa1768217682epsmtip2K; Thu, 5 Nov 2020 02:16:35 +0000 (GMT) Subject: Re: [PATCH v7 36/47] memory: tegra20-emc: Add devfreq support To: Dmitry Osipenko , Thierry Reding , Jonathan Hunter , Georgi Djakov , Rob Herring , Michael Turquette , Stephen Boyd , Peter De Schrijver , MyungJoo Ham , Kyungmin Park , Mikko Perttunen , Viresh Kumar , Peter Geis , Nicolas Chauvet , Krzysztof Kozlowski Cc: linux-tegra@vger.kernel.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, dri-devel@lists.freedesktop.org, devicetree@vger.kernel.org From: Chanwoo Choi Organization: Samsung Electronics Message-ID: <95e289fc-210c-1f8d-1b24-36e33ce96deb@samsung.com> Date: Thu, 5 Nov 2020 11:30:32 +0900 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:59.0) Gecko/20100101 Thunderbird/59.0 MIME-Version: 1.0 In-Reply-To: <20201104164923.21238-37-digetx@gmail.com> Content-Language: en-US Content-Transfer-Encoding: 7bit X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrLJsWRmVeSWpSXmKPExsWy7bCmrm5LwuJ4g+WT1S3efXrKajH/yDlW i9UfHzNaXPn6ns1i+t5NbBYtsxaxWJw/v4HdYuvTNUwWZ5vesFtc3jWHzeJz7xFGi84vs9gs Lp5ytbjduILNYtLaqYwWZ595W7TuPcJu8e/aRhaLn7vmsVhsfnCMzUHE4/2NVnaPnbPusntc OveH2WPTqk42jzvX9rB53O8+zuTR2/yOzaNvyypGj8+b5AI4o7JtMlITU1KLFFLzkvNTMvPS bZW8g+Od403NDAx1DS0tzJUU8hJzU22VXHwCdN0yc4A+U1IoS8wpBQoF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AAA== X-CMS-MailID: 20201105021636epcas1p4ac5ca3b02c51ef248a64c1c659484362 X-Msg-Generator: CA Content-Type: text/plain; charset="utf-8" X-Sendblock-Type: SVC_REQ_APPROVE CMS-TYPE: 101P DLP-Filter: Pass X-CFilter-Loop: Reflected X-CMS-RootMailID: 20201104165200epcas1p1c8f692fa2d5a01c01e4f8aca55462fb2 References: <20201104164923.21238-1-digetx@gmail.com> <20201104164923.21238-37-digetx@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 11/5/20 1:49 AM, Dmitry Osipenko wrote: > Add devfreq support to the Tegra20 EMC driver. Memory utilization > statistics will be periodically polled from the memory controller and > appropriate minimum clock rate will be selected by the devfreq governor. > > Signed-off-by: Dmitry Osipenko > --- > drivers/memory/tegra/Kconfig | 2 + > drivers/memory/tegra/tegra20-emc.c | 92 ++++++++++++++++++++++++++++++ > 2 files changed, 94 insertions(+) > > diff --git a/drivers/memory/tegra/Kconfig b/drivers/memory/tegra/Kconfig > index ac3dfe155505..76e9a3b10839 100644 > --- a/drivers/memory/tegra/Kconfig > +++ b/drivers/memory/tegra/Kconfig > @@ -12,6 +12,8 @@ config TEGRA20_EMC > tristate "NVIDIA Tegra20 External Memory Controller driver" > default y > depends on TEGRA_MC && ARCH_TEGRA_2x_SOC > + select DEVFREQ_GOV_SIMPLE_ONDEMAND > + select PM_DEVFREQ > select PM_OPP nitpick. If you select PM_DEVFREQ, don't need to select 'PM_OPP' bacause PM_DEVFREQ use OPP as mandatory with 'select PM_OPP' in Kconfig. > help > This driver is for the External Memory Controller (EMC) found on > diff --git a/drivers/memory/tegra/tegra20-emc.c b/drivers/memory/tegra/tegra20-emc.c > index 5e10aa97809f..9946b957bb01 100644 > --- a/drivers/memory/tegra/tegra20-emc.c > +++ b/drivers/memory/tegra/tegra20-emc.c > @@ -8,6 +8,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -102,6 +103,10 @@ > > #define EMC_FBIO_CFG5_DRAM_WIDTH_X16 BIT(4) > > +#define EMC_PWR_GATHER_CLEAR (1 << 8) > +#define EMC_PWR_GATHER_DISABLE (2 << 8) > +#define EMC_PWR_GATHER_ENABLE (3 << 8) > + > static const u16 emc_timing_registers[] = { > EMC_RC, > EMC_RFC, > @@ -157,6 +162,7 @@ struct emc_timing { > }; > > enum emc_rate_request_type { > + EMC_RATE_DEVFREQ, > EMC_RATE_DEBUG, > EMC_RATE_ICC, > EMC_RATE_TYPE_MAX, > @@ -193,6 +199,9 @@ struct tegra_emc { > > /* protect shared rate-change code path */ > struct mutex rate_lock; > + > + struct devfreq_simple_ondemand_data ondemand_data; > + struct devfreq *devfreq; > }; > > static irqreturn_t tegra_emc_isr(int irq, void *data) > @@ -952,6 +961,88 @@ static int tegra_emc_opp_table_init(struct tegra_emc *emc) > return err; > } > > +static int tegra_emc_devfreq_target(struct device *dev, unsigned long *freq, > + u32 flags) > +{ > + struct tegra_emc *emc = dev_get_drvdata(dev); > + struct dev_pm_opp *opp; > + unsigned long rate; > + > + opp = devfreq_recommended_opp(dev, freq, flags); > + if (IS_ERR(opp)) { > + dev_err(dev, "failed to find opp for %lu Hz\n", *freq); > + return PTR_ERR(opp); > + } > + > + rate = dev_pm_opp_get_freq(opp); > + dev_pm_opp_put(opp); > + > + return emc_set_min_rate(emc, rate, EMC_RATE_DEVFREQ); > +} > + > +static int tegra_emc_devfreq_get_dev_status(struct device *dev, > + struct devfreq_dev_status *stat) > +{ > + struct tegra_emc *emc = dev_get_drvdata(dev); > + > + /* freeze counters */ > + writel_relaxed(EMC_PWR_GATHER_DISABLE, emc->regs + EMC_STAT_CONTROL); > + > + /* > + * busy_time: number of clocks EMC request was accepted > + * total_time: number of clocks PWR_GATHER control was set to ENABLE > + */ > + stat->busy_time = readl_relaxed(emc->regs + EMC_STAT_PWR_COUNT); > + stat->total_time = readl_relaxed(emc->regs + EMC_STAT_PWR_CLOCKS); > + stat->current_frequency = clk_get_rate(emc->clk); > + > + /* clear counters and restart */ > + writel_relaxed(EMC_PWR_GATHER_CLEAR, emc->regs + EMC_STAT_CONTROL); > + writel_relaxed(EMC_PWR_GATHER_ENABLE, emc->regs + EMC_STAT_CONTROL); > + > + return 0; > +} > + > +static struct devfreq_dev_profile tegra_emc_devfreq_profile = { > + .polling_ms = 30, > + .target = tegra_emc_devfreq_target, > + .get_dev_status = tegra_emc_devfreq_get_dev_status, > +}; > + > +static int tegra_emc_devfreq_init(struct tegra_emc *emc) > +{ > + int err; > + > + /* > + * PWR_COUNT is 1/2 of PWR_CLOCKS at max, and thus, the up-threshold > + * should be less than 50. Secondly, multiple active memory clients > + * may cause over 20% of lost clock cycles due to stalls caused by > + * competing memory accesses. This means that threshold should be > + * set to a less than 30 in order to have a properly working governor. > + */ > + emc->ondemand_data.upthreshold = 20; > + > + /* > + * Reset statistic gathers state, select global bandwidth for the > + * statistics collection mode and set clocks counter saturation > + * limit to maximum. > + */ > + writel_relaxed(0x00000000, emc->regs + EMC_STAT_CONTROL); > + writel_relaxed(0x00000000, emc->regs + EMC_STAT_LLMC_CONTROL); > + writel_relaxed(0xffffffff, emc->regs + EMC_STAT_PWR_CLOCK_LIMIT); > + > + emc->devfreq = devfreq_add_device(emc->dev, &tegra_emc_devfreq_profile, > + DEVFREQ_GOV_SIMPLE_ONDEMAND, > + &emc->ondemand_data); Do you want to use 'devfreq_add_device' instead of 'devm_devfreq_add_device()'? If you have to use 'devfreq_add_device' due to some reason, you need to call 'devfreq_remove_device' on exit. > + if (IS_ERR(emc->devfreq)) { > + err = PTR_ERR(emc->devfreq); > + dev_err(emc->dev, "failed to initialize devfreq: %d", err); > + return err; > + } > + > + return 0; > +} > + > static int tegra_emc_probe(struct platform_device *pdev) > { > struct device_node *np; > @@ -1019,6 +1110,7 @@ static int tegra_emc_probe(struct platform_device *pdev) > tegra_emc_rate_requests_init(emc); > tegra_emc_debugfs_init(emc); > tegra_emc_interconnect_init(emc); > + tegra_emc_devfreq_init(emc); > > /* > * Don't allow the kernel module to be unloaded. Unloading adds some > -- Best Regards, Chanwoo Choi Samsung Electronics