From patchwork Mon Oct 11 16:57:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hector Martin X-Patchwork-Id: 12550621 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 33B29C433F5 for ; Mon, 11 Oct 2021 17:01:15 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 00F2C60F3A for ; Mon, 11 Oct 2021 17:01:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 00F2C60F3A Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=marcan.st Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Nfi/FUoAo/Dfk2vJDHA9fqxUhD5iu4Z2pyMDHWcjCZ4=; b=rCKT839uFLWyqX 6IywkP6ev+oSD+2kKQNqWlw7OGWOBfkX9TZ4dyJF/+HhpR7SGPFWTANhK8x1ctNUS2nwo5MoDJ6te bLX0M8X7yGSaq0dsvwNK/1fEyfvLNu/0YZJIlJzhs4HHv6XwXjVcVGC7LsY29uDrPBF4ulVfpXPXj 74lZr3yEfG9y3BkjhYqZma5jlus8RCo3wZXi/XTU23xMcLdjlgIUcH1igVGTYRSlTn6FckqJAPizK GWub0oj+xvqna6/CV02ykcGVFB4/WlTY8lXhu9kO9bvlDUs7XFZn5FAtNie9/n6gOij/ew5uP+RqX Q1FsnY4NJ5ndlWEsJAgg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mZydZ-00A8f3-3B; Mon, 11 Oct 2021 16:58:53 +0000 Received: from marcansoft.com ([212.63.210.85] helo=mail.marcansoft.com) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mZycm-00A8Lm-Kz for linux-arm-kernel@lists.infradead.org; Mon, 11 Oct 2021 16:58:06 +0000 Received: from [127.0.0.1] (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: hector@marcansoft.com) by mail.marcansoft.com (Postfix) with ESMTPSA id 0B91D424EC; Mon, 11 Oct 2021 16:57:57 +0000 (UTC) From: Hector Martin To: linux-arm-kernel@lists.infradead.org Cc: Hector Martin , Alyssa Rosenzweig , Sven Peter , Marc Zyngier , Mark Kettenis , Michael Turquette , Stephen Boyd , Rob Herring , Krzysztof Kozlowski , Viresh Kumar , Nishanth Menon , Catalin Marinas , "Rafael J. Wysocki" , Kevin Hilman , Ulf Hansson , linux-clk@vger.kernel.org, devicetree@vger.kernel.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 6/9] memory: apple: Add apple-mcc driver to manage MCC perf in Apple SoCs Date: Tue, 12 Oct 2021 01:57:04 +0900 Message-Id: <20211011165707.138157-7-marcan@marcan.st> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211011165707.138157-1-marcan@marcan.st> References: <20211011165707.138157-1-marcan@marcan.st> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211011_095804_905471_2959C669 X-CRM114-Status: GOOD ( 25.20 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org This driver binds to the memory controller hardware in Apple SoCs such as the Apple M1, and provides a power domain that downstream devices can use to change the performance state of the memory controller. Signed-off-by: Hector Martin --- drivers/memory/Kconfig | 9 +++ drivers/memory/Makefile | 1 + drivers/memory/apple-mcc.c | 130 +++++++++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 drivers/memory/apple-mcc.c diff --git a/drivers/memory/Kconfig b/drivers/memory/Kconfig index 72c0df129d5c..48ef3d563a1c 100644 --- a/drivers/memory/Kconfig +++ b/drivers/memory/Kconfig @@ -30,6 +30,15 @@ config ARM_PL172_MPMC If you have an embedded system with an AMBA bus and a PL172 controller, say Y or M here. +config APPLE_MCC + tristate "Apple SoC MCC driver" + default y if ARCH_APPLE + select PM_GENERIC_DOMAINS + depends on ARCH_APPLE || COMPILE_TEST + help + This driver manages performance tuning for the memory controller in + Apple SoCs, such as the Apple M1. + config ATMEL_SDRAMC bool "Atmel (Multi-port DDR-)SDRAM Controller" default y if ARCH_AT91 diff --git a/drivers/memory/Makefile b/drivers/memory/Makefile index bc7663ed1c25..947840cbd2d4 100644 --- a/drivers/memory/Makefile +++ b/drivers/memory/Makefile @@ -8,6 +8,7 @@ ifeq ($(CONFIG_DDR),y) obj-$(CONFIG_OF) += of_memory.o endif obj-$(CONFIG_ARM_PL172_MPMC) += pl172.o +obj-$(CONFIG_APPLE_MCC) += apple-mcc.o obj-$(CONFIG_ATMEL_SDRAMC) += atmel-sdramc.o obj-$(CONFIG_ATMEL_EBI) += atmel-ebi.o obj-$(CONFIG_BRCMSTB_DPFE) += brcmstb_dpfe.o diff --git a/drivers/memory/apple-mcc.c b/drivers/memory/apple-mcc.c new file mode 100644 index 000000000000..55959f034b9a --- /dev/null +++ b/drivers/memory/apple-mcc.c @@ -0,0 +1,130 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* + * Apple SoC MCC memory controller performance control driver + * + * Copyright The Asahi Linux Contributors + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define APPLE_MCC_PERF_CONFIG1 0xdc4 +#define APPLE_MCC_PERF_CONFIG2 0xdbc +#define APPLE_MCC_CHANNEL(x) ((x) * 0x40000) + +struct apple_mcc { + struct device *dev; + struct generic_pm_domain genpd; + void __iomem *reg_base; + u32 num_channels; +}; + +#define to_apple_mcc(_genpd) container_of(_genpd, struct apple_mcc, genpd) + +static int apple_mcc_set_performance_state(struct generic_pm_domain *genpd, unsigned int state) +{ + struct apple_mcc *mcc = to_apple_mcc(genpd); + struct dev_pm_opp *opp; + struct device_node *np; + u32 perf_config[2]; + unsigned int i; + + dev_dbg(mcc->dev, "switching to perf state %d\n", state); + + opp = dev_pm_opp_find_level_exact(&mcc->genpd.dev, state); + if (IS_ERR(opp)) + return PTR_ERR(opp); + + np = dev_pm_opp_get_of_node(opp); + if (of_property_read_u32_array(np, "apple,memory-perf-config", + perf_config, ARRAY_SIZE(perf_config))) { + dev_err(mcc->dev, "missing apple,memory-perf-config property"); + of_node_put(np); + return -EINVAL; + } + of_node_put(np); + + for (i = 0; i < mcc->num_channels; i++) { + writel_relaxed(perf_config[0], + mcc->reg_base + APPLE_MCC_CHANNEL(i) + APPLE_MCC_PERF_CONFIG1); + writel_relaxed(perf_config[1], + mcc->reg_base + APPLE_MCC_CHANNEL(i) + APPLE_MCC_PERF_CONFIG2); + } + + return 0; +} + +static unsigned int apple_mcc_opp_to_performance_state(struct generic_pm_domain *genpd, + struct dev_pm_opp *opp) +{ + return dev_pm_opp_get_level(opp); +} + +static int apple_mcc_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct device_node *node = dev->of_node; + struct apple_mcc *mcc; + int ret; + + mcc = devm_kzalloc(dev, sizeof(*mcc), GFP_KERNEL); + if (!mcc) + return -ENOMEM; + + mcc->reg_base = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(mcc->reg_base)) + return PTR_ERR(mcc->reg_base); + + if (of_property_read_u32(node, "apple,num-channels", &mcc->num_channels)) { + dev_err(dev, "missing apple,num-channels property\n"); + return -ENOENT; + } + + mcc->dev = dev; + mcc->genpd.name = "apple-mcc-perf"; + mcc->genpd.opp_to_performance_state = apple_mcc_opp_to_performance_state; + mcc->genpd.set_performance_state = apple_mcc_set_performance_state; + + ret = pm_genpd_init(&mcc->genpd, NULL, false); + if (ret < 0) { + dev_err(dev, "pm_genpd_init failed\n"); + return ret; + } + + ret = of_genpd_add_provider_simple_noclk(node, &mcc->genpd); + if (ret < 0) { + dev_err(dev, "of_genpd_add_provider_simple failed\n"); + return ret; + } + + dev_info(dev, "Apple MCC performance driver initialized\n"); + + return 0; +} + +static const struct of_device_id apple_mcc_of_match[] = { + { .compatible = "apple,mcc" }, + {} +}; + +MODULE_DEVICE_TABLE(of, apple_mcc_of_match); + +static struct platform_driver apple_mcc_driver = { + .probe = apple_mcc_probe, + .driver = { + .name = "apple-mcc", + .of_match_table = apple_mcc_of_match, + }, +}; + +MODULE_AUTHOR("Hector Martin "); +MODULE_DESCRIPTION("MCC memory controller performance tuning driver for Apple SoCs"); +MODULE_LICENSE("GPL v2"); + +module_platform_driver(apple_mcc_driver);