xref: /netbsd-src/sys/arch/riscv/sifive/fu540_ccache.c (revision 688f2af96516f26a1e6da01ba4150103371a54d8)
1*688f2af9Sskrll /*	$NetBSD: fu540_ccache.c,v 1.2 2024/01/14 07:13:15 skrll Exp $	*/
273ce3dc0Sskrll 
373ce3dc0Sskrll /*-
473ce3dc0Sskrll  * Copyright (c) 2023 The NetBSD Foundation, Inc.
573ce3dc0Sskrll  * All rights reserved.
673ce3dc0Sskrll  *
773ce3dc0Sskrll  * This code is derived from software contributed to The NetBSD Foundation
873ce3dc0Sskrll  * by Nick Hudson
973ce3dc0Sskrll  *
1073ce3dc0Sskrll  * Redistribution and use in source and binary forms, with or without
1173ce3dc0Sskrll  * modification, are permitted provided that the following conditions
1273ce3dc0Sskrll  * are met:
1373ce3dc0Sskrll  * 1. Redistributions of source code must retain the above copyright
1473ce3dc0Sskrll  *    notice, this list of conditions and the following disclaimer.
1573ce3dc0Sskrll  * 2. Redistributions in binary form must reproduce the above copyright
1673ce3dc0Sskrll  *    notice, this list of conditions and the following disclaimer in the
1773ce3dc0Sskrll  *    documentation and/or other materials provided with the distribution.
1873ce3dc0Sskrll  *
1973ce3dc0Sskrll  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2073ce3dc0Sskrll  * ``AS IS'' AND ANY EXPRESS OR IMPLinIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2173ce3dc0Sskrll  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2273ce3dc0Sskrll  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2373ce3dc0Sskrll  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2473ce3dc0Sskrll  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2573ce3dc0Sskrll  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2673ce3dc0Sskrll  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2773ce3dc0Sskrll  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2873ce3dc0Sskrll  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2973ce3dc0Sskrll  * POSSIBILITY OF SUCH DAMAGE.
3073ce3dc0Sskrll  */
3173ce3dc0Sskrll 
3273ce3dc0Sskrll #include <sys/cdefs.h>
33*688f2af9Sskrll __KERNEL_RCSID(0, "$NetBSD: fu540_ccache.c,v 1.2 2024/01/14 07:13:15 skrll Exp $");
3473ce3dc0Sskrll 
3573ce3dc0Sskrll #include <sys/param.h>
3673ce3dc0Sskrll 
3773ce3dc0Sskrll #include <sys/bus.h>
3873ce3dc0Sskrll 
3973ce3dc0Sskrll #include <dev/fdt/fdtvar.h>
4073ce3dc0Sskrll 
4173ce3dc0Sskrll #include <machine/cpufunc.h>
4273ce3dc0Sskrll 
4373ce3dc0Sskrll #define CCACHE_CONFIG			0x0000
4473ce3dc0Sskrll #define  CCACHE_CONFIG_BANKS_MASK		__BITS( 7,  0)
4573ce3dc0Sskrll #define  CCACHE_CONFIG_WAYS_MASK		__BITS(15,  8)
4673ce3dc0Sskrll #define  CCACHE_CONFIG_LGSETS_MASK		__BITS(23, 16)
4773ce3dc0Sskrll #define  CCACHE_CONFIG_LGBLOCKBYTES_MASK	__BITS(31, 24)
4873ce3dc0Sskrll #define CCACHE_WAYENABLE		0x0008
4973ce3dc0Sskrll 
5073ce3dc0Sskrll #define CCACHE_ECCINJECTERROR		0x0040
5173ce3dc0Sskrll 
5273ce3dc0Sskrll #define CCACHE_DIRECCFIX_LOW		0x0100
5373ce3dc0Sskrll #define CCACHE_DIRECCFIX_HIGH		0x0104
5473ce3dc0Sskrll #define CCACHE_DIRECCFIX_COUNT 		0x0108
5573ce3dc0Sskrll 
5673ce3dc0Sskrll #define CCACHE_DIRECCFAIL_LOW		0x0120
5773ce3dc0Sskrll #define CCACHE_DIRECCFAIL_HIGH	 	0x0124
5873ce3dc0Sskrll #define CCACHE_DIRECCFAIL_COUNT 	0x0128
5973ce3dc0Sskrll 
6073ce3dc0Sskrll #define CCACHE_DATECCFIX_LOW		0x0140
6173ce3dc0Sskrll #define CCACHE_DATECCFIX_HIGH		0x0144
6273ce3dc0Sskrll #define CCACHE_DATECCFIX_COUNT		0x0148
6373ce3dc0Sskrll 
6473ce3dc0Sskrll #define CCACHE_DATECCFAIL_LOW		0x0160
6573ce3dc0Sskrll #define CCACHE_DATECCFAIL_HIGH		0x0164
6673ce3dc0Sskrll #define CCACHE_DATECCFAIL_COUNT		0x0168
6773ce3dc0Sskrll 
6873ce3dc0Sskrll #define CCACHE_FLUSH64			0x0200
6973ce3dc0Sskrll #define CCACHE_FLUSH32			0x0250
7073ce3dc0Sskrll 
7173ce3dc0Sskrll #define CCACHE_MAX_ECCINTR		4
7273ce3dc0Sskrll 
7373ce3dc0Sskrll #define CCACHE_FLUSH64_LINE_LEN		64
7473ce3dc0Sskrll 
7573ce3dc0Sskrll static const struct device_compatible_entry compat_data[] = {
7673ce3dc0Sskrll 	{ .compat = "sifive,fu540-c000-ccache" },
7773ce3dc0Sskrll 	{ .compat = "sifive,fu740-c000-ccache" },
78*688f2af9Sskrll 	{ .compat = "starfive,jh7100-ccache" },
79*688f2af9Sskrll 	{ .compat = "starfive,jh7110-ccache" },
80*688f2af9Sskrll 	{ .compat = "starfive,ccache0" },
8173ce3dc0Sskrll 	DEVICE_COMPAT_EOL
8273ce3dc0Sskrll };
8373ce3dc0Sskrll 
8473ce3dc0Sskrll struct fu540_ccache_softc {
8573ce3dc0Sskrll 	device_t		 sc_dev;
8673ce3dc0Sskrll 	bus_space_tag_t		 sc_bst;
8773ce3dc0Sskrll 	bus_space_handle_t	 sc_bsh;
8873ce3dc0Sskrll 
8973ce3dc0Sskrll 	uint32_t		 sc_line_size;
9073ce3dc0Sskrll 	uint32_t		 sc_size;
9173ce3dc0Sskrll 	uint32_t		 sc_sets;
9273ce3dc0Sskrll 	uint32_t		 sc_level;
9373ce3dc0Sskrll };
9473ce3dc0Sskrll 
9573ce3dc0Sskrll 
9673ce3dc0Sskrll static struct fu540_ccache_softc *fu540_ccache_sc;
9773ce3dc0Sskrll 
9873ce3dc0Sskrll #define RD4(sc, reg) \
9973ce3dc0Sskrll 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
10073ce3dc0Sskrll #define	WR4(sc, reg, val) \
10173ce3dc0Sskrll 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
10273ce3dc0Sskrll #define	WR8(sc, reg, val)						\
10373ce3dc0Sskrll 	bus_space_write_8((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
10473ce3dc0Sskrll 
10573ce3dc0Sskrll 
10673ce3dc0Sskrll static void
fu540_ccache_cache_wbinv_range(vaddr_t va,paddr_t pa,psize_t len)10773ce3dc0Sskrll fu540_ccache_cache_wbinv_range(vaddr_t va, paddr_t pa, psize_t len)
10873ce3dc0Sskrll {
10973ce3dc0Sskrll 	struct fu540_ccache_softc * const sc = fu540_ccache_sc;
11073ce3dc0Sskrll 
11173ce3dc0Sskrll 	KASSERT(powerof2(sc->sc_line_size));
11273ce3dc0Sskrll 	KASSERT(len != 0);
11373ce3dc0Sskrll 
11473ce3dc0Sskrll 	const paddr_t spa = rounddown2(pa, sc->sc_line_size);
11573ce3dc0Sskrll 	const paddr_t epa = roundup2(pa + len, sc->sc_line_size);
11673ce3dc0Sskrll 
11773ce3dc0Sskrll 	asm volatile ("fence iorw,iorw" ::: "memory");
11873ce3dc0Sskrll 
11973ce3dc0Sskrll 	for (paddr_t fpa = spa; fpa < epa; fpa += sc->sc_line_size) {
12073ce3dc0Sskrll #ifdef _LP64
12173ce3dc0Sskrll 		WR8(sc, CCACHE_FLUSH64, fpa);
12273ce3dc0Sskrll #else
12373ce3dc0Sskrll 		WR4(sc, CCACHE_FLUSH32, fpa >> 4);
12473ce3dc0Sskrll #endif
12573ce3dc0Sskrll 		asm volatile ("fence iorw,iorw" ::: "memory");
12673ce3dc0Sskrll 	}
12773ce3dc0Sskrll }
12873ce3dc0Sskrll 
12973ce3dc0Sskrll 
13073ce3dc0Sskrll static int
fu540_ccache_match(device_t parent,cfdata_t cf,void * aux)13173ce3dc0Sskrll fu540_ccache_match(device_t parent, cfdata_t cf, void *aux)
13273ce3dc0Sskrll {
13373ce3dc0Sskrll 	struct fdt_attach_args * const faa = aux;
13473ce3dc0Sskrll 
13573ce3dc0Sskrll 	return of_compatible_match(faa->faa_phandle, compat_data);
13673ce3dc0Sskrll }
13773ce3dc0Sskrll 
13873ce3dc0Sskrll static void
fu540_ccache_attach(device_t parent,device_t self,void * aux)13973ce3dc0Sskrll fu540_ccache_attach(device_t parent, device_t self, void *aux)
14073ce3dc0Sskrll {
14173ce3dc0Sskrll 	struct fu540_ccache_softc * const sc = device_private(self);
14273ce3dc0Sskrll 	const struct fdt_attach_args * const faa = aux;
14373ce3dc0Sskrll 	const int phandle = faa->faa_phandle;
14473ce3dc0Sskrll 	bus_addr_t addr;
14573ce3dc0Sskrll 	bus_size_t size;
14673ce3dc0Sskrll 
14773ce3dc0Sskrll 	int error;
14873ce3dc0Sskrll 	error = fdtbus_get_reg(phandle, 0, &addr, &size);
14973ce3dc0Sskrll 	if (error) {
15073ce3dc0Sskrll 		aprint_error(": couldn't get registers\n");
15173ce3dc0Sskrll 		return;
15273ce3dc0Sskrll 	}
15373ce3dc0Sskrll 
15473ce3dc0Sskrll 	sc->sc_dev = self;
15573ce3dc0Sskrll 	sc->sc_bst = faa->faa_bst;
15673ce3dc0Sskrll 
15773ce3dc0Sskrll 	error = bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh);
15873ce3dc0Sskrll 	if (error) {
15973ce3dc0Sskrll 		aprint_error(": couldn't map registers\n");
16073ce3dc0Sskrll 		return;
16173ce3dc0Sskrll 	}
16273ce3dc0Sskrll 
16373ce3dc0Sskrll 	int ret;
16473ce3dc0Sskrll 	ret = of_getprop_uint32(phandle, "cache-block-size",
16573ce3dc0Sskrll 	    &sc->sc_line_size);
16673ce3dc0Sskrll 	if (ret < 0) {
16773ce3dc0Sskrll 		aprint_error(": can't get cache-block-size\n");
16873ce3dc0Sskrll 		return;
16973ce3dc0Sskrll 	}
17073ce3dc0Sskrll 
17173ce3dc0Sskrll 	ret = of_getprop_uint32(phandle, "cache-level",
17273ce3dc0Sskrll 	    &sc->sc_level);
17373ce3dc0Sskrll 	if (ret < 0) {
17473ce3dc0Sskrll 		aprint_error(": can't get cache-level\n");
17573ce3dc0Sskrll 		return;
17673ce3dc0Sskrll 	}
17773ce3dc0Sskrll 
17873ce3dc0Sskrll 	ret = of_getprop_uint32(phandle, "cache-sets",
17973ce3dc0Sskrll 	    &sc->sc_sets);
18073ce3dc0Sskrll 	if (ret < 0) {
18173ce3dc0Sskrll 		aprint_error(": can't get cache-sets\n");
18273ce3dc0Sskrll 		return;
18373ce3dc0Sskrll 	}
18473ce3dc0Sskrll 	ret = of_getprop_uint32(phandle, "cache-size",
18573ce3dc0Sskrll 	    &sc->sc_size);
18673ce3dc0Sskrll 	if (ret < 0) {
18773ce3dc0Sskrll 		aprint_error(": can't get cache-size\n");
18873ce3dc0Sskrll 		return;
18973ce3dc0Sskrll 	}
19073ce3dc0Sskrll 
19173ce3dc0Sskrll 	if (!of_hasprop(phandle, "cache-unified")) {
19273ce3dc0Sskrll 		aprint_error(": can't get cache-unified\n");
19373ce3dc0Sskrll 		return;
19473ce3dc0Sskrll 	}
19573ce3dc0Sskrll 
19673ce3dc0Sskrll 	uint32_t ways = sc->sc_size / (sc->sc_sets * sc->sc_line_size);
19773ce3dc0Sskrll 
19873ce3dc0Sskrll 	aprint_naive("\n");
19973ce3dc0Sskrll 	aprint_normal(": L%u cache controller. %u KiB/%uB %u-way (%u set).\n",
20073ce3dc0Sskrll 	    sc->sc_level, sc->sc_size / 1024, sc->sc_line_size, ways,
20173ce3dc0Sskrll 	    sc->sc_sets);
20273ce3dc0Sskrll 
20373ce3dc0Sskrll 	uint32_t l2config = RD4(sc, CCACHE_CONFIG);
20473ce3dc0Sskrll 
20573ce3dc0Sskrll 	aprint_debug_dev(self,   "l2config        %#10" PRIx32 "\n",
20673ce3dc0Sskrll 	    l2config);
20773ce3dc0Sskrll 	aprint_verbose_dev(self, "No. of banks          %4" __PRIuBIT "\n",
20873ce3dc0Sskrll 	    __SHIFTOUT(l2config, CCACHE_CONFIG_BANKS_MASK));
20973ce3dc0Sskrll 	aprint_verbose_dev(self, "No. of ways per bank  %4" __PRIuBIT "\n",
21073ce3dc0Sskrll 	    __SHIFTOUT(l2config, CCACHE_CONFIG_WAYS_MASK));
21173ce3dc0Sskrll 	aprint_verbose_dev(self, "Sets per bank         %4lu\n",
21273ce3dc0Sskrll 	    1UL << __SHIFTOUT(l2config, CCACHE_CONFIG_LGSETS_MASK));
21373ce3dc0Sskrll 	aprint_verbose_dev(self, "Bytes per cache block %4lu\n",
21473ce3dc0Sskrll 	    1UL << __SHIFTOUT(l2config, CCACHE_CONFIG_LGBLOCKBYTES_MASK));
21573ce3dc0Sskrll 
21673ce3dc0Sskrll 	uint32_t l2wayenable = RD4(sc, CCACHE_WAYENABLE);
21773ce3dc0Sskrll 
21873ce3dc0Sskrll 	aprint_verbose_dev(self, "Largest way enabled   %4" PRIu32 "\n",
21973ce3dc0Sskrll 	    l2wayenable);
22073ce3dc0Sskrll 
22173ce3dc0Sskrll 	fu540_ccache_sc = sc;
22273ce3dc0Sskrll 
22373ce3dc0Sskrll 	cpu_sdcache_wbinv_range = fu540_ccache_cache_wbinv_range;
22473ce3dc0Sskrll 	cpu_sdcache_inv_range = fu540_ccache_cache_wbinv_range;
22573ce3dc0Sskrll 	cpu_sdcache_wb_range = fu540_ccache_cache_wbinv_range;
22673ce3dc0Sskrll }
22773ce3dc0Sskrll 
22873ce3dc0Sskrll CFATTACH_DECL_NEW(fu540_ccache, sizeof(struct fu540_ccache_softc),
22973ce3dc0Sskrll 	fu540_ccache_match, fu540_ccache_attach, NULL, NULL);
230