From patchwork Wed Apr 24 07:58:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joshua Yeong X-Patchwork-Id: 13641476 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 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 4FD3EC10F16 for ; Wed, 24 Apr 2024 07:59:41 +0000 (UTC) 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=DUOcJzA/WjE8rJxIepBDIXAK2JBkf8p0ar3FPHYkeHQ=; b=bEm9pGuu/k02hu 5cRC8E37izRSRYX9pRd7IC8ly1gHwYaX9qbbc/sbm1JdkINUSBam1qsJTpbKEple+E7PkSaYcRsgr BgB/TwMtzzrSnXRwu3BdMafohtJcp6FTQVSxXZ4RpIQtWARF6rAmg0KgKAP3u7F7NM1cUu+F3PqyF XQIdhxVkL3LvChbpvXJt0NGdS9HocaK7Wb+AtDXm2Nah0kYpLUyOil/qrNecJWUraHAkx8k6vYzD9 kqUinyISRjjWWozJVVVTt+oUryJTShVMcVD6M4V3BTfc5+NTOlOlAmwz2AkWqNaeuSgw/OqaUXnOC DalqEXCDsN/naRGRYnNg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzXXS-00000003Baz-1axv; Wed, 24 Apr 2024 07:59:34 +0000 Received: from mail-sh0chn02on20700.outbound.protection.partner.outlook.cn ([2406:e500:4420:2::700] helo=CHN02-SH0-obe.outbound.protection.partner.outlook.cn) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rzXXN-00000003BVH-1a5t for linux-riscv@lists.infradead.org; Wed, 24 Apr 2024 07:59:31 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YGpAzmAxaVzMlcbMqcf6O53ZbXME7AL6jrC3J3MRBTsQ11U35bN/n7Zbysxegv+X+SbztpePFyDWcBEUF2OezlYLvKwDpe+rmCWaKH0ElFar7YU1pNCdL9lcPtZ09DBVIAYvBuYCeaoLu04ZUvqc7wGV1rJWFc1Zf+MiZaVUN1RRHdqhj9sgxO8UIenJ/tB0SErYzdGQ79DBnRE2yl4h3+iwn1xHE76Gd8mHhuKD9DJcYPCvj5jUvFEIiI10Z3JBg7eUFN6Jg2lr797uTOAwLw3IUz7LFYOY6iyF7fUIMK7+j6NUZTZKQ5lFK6g37S4x34BZAXIUfbJP/Pu8fakr+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=sNtDg60wNrrjdhIVYCSsKiN5ruLjPoYQcVIa7XLQcc0=; b=ZQStBNWOv7c74ABS4KH/UQLA4TZzqA53iXy97rxsL1LiTCJx6fvxUZbrTjWbKZhp/9pzeFvEE6Tui8T8SlH33upyX8HiGydLq7A3mdLqozJGUal0Di2qP4L9bYB9CXAXX/gcsZLRBbQ9SahNsHp6tnDm8kkrvV3TzoJXTjsODCFWC2tu/z9TEXm/v1V+C7vNeQYSntZFU26IdzycAFWyYPS9OLRUEdjfWwbgNrIsHXZNlxRm0liXZJIpzp00L6BMl/kdK6yNtb1ebsfbORv67R5qar/JJtfrUWbnpaOItKPV7bT6rmP8A39E4Y5lsdaN9bQO+Lg7IB39lRLkbbh9ng== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=starfivetech.com; dmarc=pass action=none header.from=starfivetech.com; dkim=pass header.d=starfivetech.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=starfivetech.com; Received: from SH0PR01MB0841.CHNPR01.prod.partner.outlook.cn (2406:e500:c311:20::14) by SH0PR01MB0460.CHNPR01.prod.partner.outlook.cn (2406:e500:c311:8::17) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.33; Wed, 24 Apr 2024 07:59:19 +0000 Received: from SH0PR01MB0841.CHNPR01.prod.partner.outlook.cn ([fe80::e0a:f88a:cad1:dc1c]) by SH0PR01MB0841.CHNPR01.prod.partner.outlook.cn ([fe80::e0a:f88a:cad1:dc1c%7]) with mapi id 15.20.7472.044; Wed, 24 Apr 2024 07:59:19 +0000 From: Joshua Yeong To: robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, conor@kernel.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, joshua.yeong@starfivetech.com, leyfoon.tan@starfivetech.com, jeeheng.sia@starfivetech.com Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org Subject: [PATCH v3 1/2] cache: Add StarFive StarLink cache management for StarFive JH8100 Date: Wed, 24 Apr 2024 15:58:55 +0800 Message-Id: <20240424075856.145850-2-joshua.yeong@starfivetech.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240424075856.145850-1-joshua.yeong@starfivetech.com> References: <20240424075856.145850-1-joshua.yeong@starfivetech.com> X-ClientProxiedBy: BJXPR01CA0048.CHNPR01.prod.partner.outlook.cn (2406:e500:c211:12::15) To SH0PR01MB0841.CHNPR01.prod.partner.outlook.cn (2406:e500:c311:20::14) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SH0PR01MB0841:EE_|SH0PR01MB0460:EE_ X-MS-Office365-Filtering-Correlation-Id: a7443985-e42e-4d9c-167a-08dc643471fe X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: lEYMvYyZsbKSH215i1rdV5bBVzrqiQqL1AApvkWZuITcxoTmQJdBR0/9F0F8wqDfz/7gdNScrLEA13rYf4p/avXmedAX7LP81rf9rE/lWQfuiIejPjP3fl5vv5nFGiMhf9V+7e3g4nAi8StHb9j9qpSMPUAr8fzCemgE4VPwnEmr1ppm6GHil+C+BrLDaLDAD3In02xHNdXMPqQfzlnB9/L6JvIotSH8RvqWmA3A2QRFjCF4VWOW8BRY1uC2G2PwTCjfD9Dq42+8bhAcRUsPMwwGULpu6OixGMPO8/oE+Ed6CXq0G0utWLqH+LoIXeEv8++fC7i6wuinb02kI4wrqe95m5s4jFFW7slfeO7RcGImtIR1eVaX9Nx0ke4CzCbQbKYaVeTJfHYZiY/3NmN59P6hcXyu/pkhdo66uDUiqSHP6Q0pD1M3p9SeY6l15CGNIoJ3SUuA6f5DHeyUXnomgM2zP10ZVibL55+d3yS1vPp7K16NF7cGLuSo3u4vzmD9+lo35fGg5FVjq6MPmCvxtXSO6cQrGL9dmMevM1GiDNpMkm6cnwOXMCctmr75w+cpKzKcrVf2cldagzQ4V26ULls9E9f/CzQzqRyzRYsI8DaBp06eMRjMHLEiDhm5YwLuB43EW1wXipp4+Hpp7P3fAw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SH0PR01MB0841.CHNPR01.prod.partner.outlook.cn;PTR:;CAT:NONE;SFS:(13230031)(366007)(52116005)(7416005)(41320700004)(1800799015)(921011)(38350700005);DIR:OUT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9tfHAm+/Ds688JJWnIMTe10+j85qPx7Hmatt80xlsPbbd6K4o8LQew77f8keBZOsuCVq9s91j5FI7/+0QPF9aBMYhRN3y43TtYF+PB3zxmpl6/IhKDwvc+lI/KfkrqmXELyiRQMkOsY458rjr8rEjY2+VBRBRyAqYsUhWr/5NrJZnK7PyFhddHJrI6eGzhGI/F9V8uAGvACKUB2kJUzzpDOGp/5KbPw63J+02v8Po/HTf6ZX5gh8Usij5KerY1MQOUF/QuKltnYqENO2akZAl6Fg/INejvb9DFpIucA6ZUw6C/vGb5Oyf9o/GRaHirt6qqhjRj0OOml1++5Ez73t4/T+DKqvwLd52UejsZoS1TjaZEIzkxxVk4SrrR9R0uB/YtU/954ea86DwV4MUWyAd18ThyshduspocV/hTLCNYj0Bii7a1smpQJOqwTmbb4bKXMAC4b/K2o1CNaJeUIO6nj+uUwsaov9edd/QO9hyUG1qJSN1BJXyn3fQLKwI2pIXlWWJlD62QjuKeMakzHYk6OPeCs/R3BTKJkc0SaY1Yy//grMRy6FO4z/OG/ibAlQUr+atdc9xhFXZgC7J43rOjhvo5KjdCyAg7yjJoZMuUQGI= X-OriginatorOrg: starfivetech.com X-MS-Exchange-CrossTenant-Network-Message-Id: a7443985-e42e-4d9c-167a-08dc643471fe X-MS-Exchange-CrossTenant-AuthSource: SH0PR01MB0841.CHNPR01.prod.partner.outlook.cn X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Apr 2024 07:59:19.4331 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 06fe3fa3-1221-43d3-861b-5a4ee687a85c X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: Y2vFHAnfuZhQ0V9pnQ7jJ1ju28cKjiaMX0x21p4yGhdgj49iv2qjRhPU+VmX/6v5roUPtynsAvvtAhT9Gw0RqqhIYFMazV97jHeuECUj21Y= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SH0PR01MB0460 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240424_005929_488748_2A363CE3 X-CRM114-Status: GOOD ( 17.86 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org Add StarFive Starlink cache management driver for JH8100 SoC. This driver enables RISC-V non-standard cache operation on JH8100 that does not support Zicbom extension instructions. Signed-off-by: Joshua Yeong --- drivers/cache/Kconfig | 9 ++ drivers/cache/Makefile | 5 +- drivers/cache/starfive_starlink_cache.c | 135 ++++++++++++++++++++++++ 3 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 drivers/cache/starfive_starlink_cache.c diff --git a/drivers/cache/Kconfig b/drivers/cache/Kconfig index 9345ce4976d7..9181cd391f53 100644 --- a/drivers/cache/Kconfig +++ b/drivers/cache/Kconfig @@ -14,4 +14,13 @@ config SIFIVE_CCACHE help Support for the composable cache controller on SiFive platforms. +config STARFIVE_STARLINK_CACHE + bool "StarFive StarLink Cache controller" + depends on RISCV + depends on ARCH_STARFIVE + select RISCV_DMA_NONCOHERENT + select RISCV_NONSTANDARD_CACHE_OPS + help + Support for the StarLink cache controller on StarFive platforms. + endmenu diff --git a/drivers/cache/Makefile b/drivers/cache/Makefile index 7657cff3bd6c..55c5e851034d 100644 --- a/drivers/cache/Makefile +++ b/drivers/cache/Makefile @@ -1,4 +1,5 @@ # SPDX-License-Identifier: GPL-2.0 -obj-$(CONFIG_AX45MP_L2_CACHE) += ax45mp_cache.o -obj-$(CONFIG_SIFIVE_CCACHE) += sifive_ccache.o +obj-$(CONFIG_AX45MP_L2_CACHE) += ax45mp_cache.o +obj-$(CONFIG_SIFIVE_CCACHE) += sifive_ccache.o +obj-$(CONFIG_STARFIVE_STARLINK_CACHE) += starfive_starlink_cache.o diff --git a/drivers/cache/starfive_starlink_cache.c b/drivers/cache/starfive_starlink_cache.c new file mode 100644 index 000000000000..2f1fa6a923ee --- /dev/null +++ b/drivers/cache/starfive_starlink_cache.c @@ -0,0 +1,135 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Cache Management Operations for StarFive's Starlink cache controller + * + * Copyright (C) 2024 Shanghai StarFive Technology Co., Ltd. + * + * Author: Joshua Yeong + */ + +#include +#include +#include +#include + +#include + +#define STARLINK_CACHE_FLUSH_START_ADDR 0x0 +#define STARLINK_CACHE_FLUSH_END_ADDR 0x8 +#define STARLINK_CACHE_FLUSH_CTL 0x10 +#define STARLINK_CACHE_ALIGN 0x40 + +#define STARLINK_CACHE_ADDRESS_RANGE_MASK GENMASK(39, 0) +#define STARLINK_CACHE_FLUSH_CTL_MODE_MASK GENMASK(2, 1) +#define STARLINK_CACHE_FLUSH_CTL_ENABLE_MASK BIT(0) + +#define STARLINK_CACHE_FLUSH_CTL_CLEAN_INVALIDATE 0 +#define STARLINK_CACHE_FLUSH_CTL_MAKE_INVALIDATE 1 +#define STARLINK_CACHE_FLUSH_CTL_CLEAN_SHARED 2 +#define STARLINK_CACHE_FLUSH_POLL_DELAY_US 1 +#define STARLINK_CACHE_FLUSH_TIMEOUT_US 5000000 + +struct starlink_cache_priv { + void __iomem *base_addr; +}; + +static struct starlink_cache_priv starlink_cache_priv; + +static void starlink_cache_flush_complete(void) +{ + volatile void __iomem *ctl = starlink_cache_priv.base_addr + + STARLINK_CACHE_FLUSH_CTL; + u64 v; + int ret; + + ret = readq_poll_timeout_atomic(ctl, v, !(v & STARLINK_CACHE_FLUSH_CTL_ENABLE_MASK), + STARLINK_CACHE_FLUSH_POLL_DELAY_US, + STARLINK_CACHE_FLUSH_TIMEOUT_US); + if (ret) + WARN(1, "StarFive Starlink cache flush operation timeout\n"); +} + +static void starlink_cache_dma_cache_wback(phys_addr_t paddr, unsigned long size) +{ + writeq(FIELD_PREP(STARLINK_CACHE_ADDRESS_RANGE_MASK, paddr), + starlink_cache_priv.base_addr + STARLINK_CACHE_FLUSH_START_ADDR); + writeq(FIELD_PREP(STARLINK_CACHE_ADDRESS_RANGE_MASK, paddr + size), + starlink_cache_priv.base_addr + STARLINK_CACHE_FLUSH_END_ADDR); + + mb(); + writeq(FIELD_PREP(STARLINK_CACHE_FLUSH_CTL_MODE_MASK, + STARLINK_CACHE_FLUSH_CTL_CLEAN_SHARED), + starlink_cache_priv.base_addr + STARLINK_CACHE_FLUSH_CTL); + + starlink_cache_flush_complete(); +} + +static void starlink_cache_dma_cache_invalidate(phys_addr_t paddr, unsigned long size) +{ + writeq(FIELD_PREP(STARLINK_CACHE_ADDRESS_RANGE_MASK, paddr), + starlink_cache_priv.base_addr + STARLINK_CACHE_FLUSH_START_ADDR); + writeq(FIELD_PREP(STARLINK_CACHE_ADDRESS_RANGE_MASK, paddr + size), + starlink_cache_priv.base_addr + STARLINK_CACHE_FLUSH_END_ADDR); + + mb(); + writeq(FIELD_PREP(STARLINK_CACHE_FLUSH_CTL_MODE_MASK, + STARLINK_CACHE_FLUSH_CTL_MAKE_INVALIDATE), + starlink_cache_priv.base_addr + STARLINK_CACHE_FLUSH_CTL); + + starlink_cache_flush_complete(); +} + +static void starlink_cache_dma_cache_wback_inv(phys_addr_t paddr, unsigned long size) +{ + writeq(FIELD_PREP(STARLINK_CACHE_ADDRESS_RANGE_MASK, paddr), + starlink_cache_priv.base_addr + STARLINK_CACHE_FLUSH_START_ADDR); + writeq(FIELD_PREP(STARLINK_CACHE_ADDRESS_RANGE_MASK, paddr + size), + starlink_cache_priv.base_addr + STARLINK_CACHE_FLUSH_END_ADDR); + + mb(); + writeq(FIELD_PREP(STARLINK_CACHE_FLUSH_CTL_MODE_MASK, + STARLINK_CACHE_FLUSH_CTL_CLEAN_INVALIDATE), + starlink_cache_priv.base_addr + STARLINK_CACHE_FLUSH_CTL); + + starlink_cache_flush_complete(); +} + +static const struct riscv_nonstd_cache_ops starlink_cache_ops = { + .wback = &starlink_cache_dma_cache_wback, + .inv = &starlink_cache_dma_cache_invalidate, + .wback_inv = &starlink_cache_dma_cache_wback_inv, +}; + +static const struct of_device_id starlink_cache_ids[] = { + { .compatible = "starfive,jh8100-starlink-cache" }, + { /* sentinel */ } +}; + +static int __init starlink_cache_init(void) +{ + struct device_node *np; + u32 block_size = 0; + int ret; + + np = of_find_matching_node(NULL, starlink_cache_ids); + if (!of_device_is_available(np)) + return -ENODEV; + + ret = of_property_read_u32(np, "cache-block-size", &block_size); + if (ret) + return ret; + + if (block_size % STARLINK_CACHE_ALIGN) + return -EINVAL; + + starlink_cache_priv.base_addr = of_iomap(np, 0); + if (!starlink_cache_priv.base_addr) + return -ENOMEM; + + riscv_cbom_block_size = block_size; + riscv_noncoherent_supported(); + riscv_noncoherent_register_cache_ops(&starlink_cache_ops); + + return 0; +} +early_initcall(starlink_cache_init);