1*6766e8ceSRuslan Bukin /*-
2*6766e8ceSRuslan Bukin * SPDX-License-Identifier: BSD-2-Clause
3*6766e8ceSRuslan Bukin *
4*6766e8ceSRuslan Bukin * Copyright (c) 2024 Ruslan Bukin <br@bsdpad.com>
5*6766e8ceSRuslan Bukin *
6*6766e8ceSRuslan Bukin * This software was developed by the University of Cambridge Computer
7*6766e8ceSRuslan Bukin * Laboratory (Department of Computer Science and Technology) under Innovate
8*6766e8ceSRuslan Bukin * UK project 105694, "Digital Security by Design (DSbD) Technology Platform
9*6766e8ceSRuslan Bukin * Prototype".
10*6766e8ceSRuslan Bukin *
11*6766e8ceSRuslan Bukin * Redistribution and use in source and binary forms, with or without
12*6766e8ceSRuslan Bukin * modification, are permitted provided that the following conditions
13*6766e8ceSRuslan Bukin * are met:
14*6766e8ceSRuslan Bukin * 1. Redistributions of source code must retain the above copyright
15*6766e8ceSRuslan Bukin * notice, this list of conditions and the following disclaimer.
16*6766e8ceSRuslan Bukin * 2. Redistributions in binary form must reproduce the above copyright
17*6766e8ceSRuslan Bukin * notice, this list of conditions and the following disclaimer in the
18*6766e8ceSRuslan Bukin * documentation and/or other materials provided with the distribution.
19*6766e8ceSRuslan Bukin *
20*6766e8ceSRuslan Bukin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21*6766e8ceSRuslan Bukin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22*6766e8ceSRuslan Bukin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23*6766e8ceSRuslan Bukin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24*6766e8ceSRuslan Bukin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25*6766e8ceSRuslan Bukin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26*6766e8ceSRuslan Bukin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*6766e8ceSRuslan Bukin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28*6766e8ceSRuslan Bukin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29*6766e8ceSRuslan Bukin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30*6766e8ceSRuslan Bukin * SUCH DAMAGE.
31*6766e8ceSRuslan Bukin */
32*6766e8ceSRuslan Bukin
33*6766e8ceSRuslan Bukin #include <sys/param.h>
34*6766e8ceSRuslan Bukin #include <sys/systm.h>
35*6766e8ceSRuslan Bukin #include <sys/bus.h>
36*6766e8ceSRuslan Bukin #include <sys/rman.h>
37*6766e8ceSRuslan Bukin #include <sys/kernel.h>
38*6766e8ceSRuslan Bukin #include <sys/module.h>
39*6766e8ceSRuslan Bukin #include <machine/bus.h>
40*6766e8ceSRuslan Bukin
41*6766e8ceSRuslan Bukin #include <dev/ofw/ofw_bus.h>
42*6766e8ceSRuslan Bukin #include <dev/ofw/ofw_bus_subr.h>
43*6766e8ceSRuslan Bukin #include <dev/fdt/fdt_common.h>
44*6766e8ceSRuslan Bukin
45*6766e8ceSRuslan Bukin #include <vm/vm.h>
46*6766e8ceSRuslan Bukin #include <vm/vm_extern.h>
47*6766e8ceSRuslan Bukin #include <vm/pmap.h>
48*6766e8ceSRuslan Bukin
49*6766e8ceSRuslan Bukin #define SIFIVE_CCACHE_CONFIG 0x000
50*6766e8ceSRuslan Bukin #define CCACHE_CONFIG_WAYS_S 8
51*6766e8ceSRuslan Bukin #define CCACHE_CONFIG_WAYS_M (0xff << CCACHE_CONFIG_WAYS_S)
52*6766e8ceSRuslan Bukin #define SIFIVE_CCACHE_WAYENABLE 0x008
53*6766e8ceSRuslan Bukin #define SIFIVE_CCACHE_FLUSH64 0x200
54*6766e8ceSRuslan Bukin
55*6766e8ceSRuslan Bukin #define SIFIVE_CCACHE_LINE_SIZE 64
56*6766e8ceSRuslan Bukin
57*6766e8ceSRuslan Bukin #define RD8(sc, off) (bus_read_8((sc)->res, (off)))
58*6766e8ceSRuslan Bukin #define WR8(sc, off, val) (bus_write_8((sc)->res, (off), (val)))
59*6766e8ceSRuslan Bukin #define CC_WR8(offset, value) \
60*6766e8ceSRuslan Bukin *(volatile uint64_t *)((uintptr_t)ccache_va + (offset)) = (value)
61*6766e8ceSRuslan Bukin
62*6766e8ceSRuslan Bukin static struct ofw_compat_data compat_data[] = {
63*6766e8ceSRuslan Bukin { "sifive,eic7700", 1 },
64*6766e8ceSRuslan Bukin { NULL, 0 }
65*6766e8ceSRuslan Bukin };
66*6766e8ceSRuslan Bukin
67*6766e8ceSRuslan Bukin struct ccache_softc {
68*6766e8ceSRuslan Bukin struct resource *res;
69*6766e8ceSRuslan Bukin };
70*6766e8ceSRuslan Bukin
71*6766e8ceSRuslan Bukin static void *ccache_va = NULL;
72*6766e8ceSRuslan Bukin
73*6766e8ceSRuslan Bukin static struct resource_spec ccache_spec[] = {
74*6766e8ceSRuslan Bukin { SYS_RES_MEMORY, 0, RF_ACTIVE },
75*6766e8ceSRuslan Bukin { -1, 0 }
76*6766e8ceSRuslan Bukin };
77*6766e8ceSRuslan Bukin
78*6766e8ceSRuslan Bukin /*
79*6766e8ceSRuslan Bukin * Non-standard EIC7700 cache-flushing routine.
80*6766e8ceSRuslan Bukin */
81*6766e8ceSRuslan Bukin static void
ccache_flush_range(vm_offset_t start,size_t len)82*6766e8ceSRuslan Bukin ccache_flush_range(vm_offset_t start, size_t len)
83*6766e8ceSRuslan Bukin {
84*6766e8ceSRuslan Bukin vm_offset_t paddr;
85*6766e8ceSRuslan Bukin vm_offset_t sva;
86*6766e8ceSRuslan Bukin vm_offset_t step;
87*6766e8ceSRuslan Bukin uint64_t line;
88*6766e8ceSRuslan Bukin
89*6766e8ceSRuslan Bukin if (ccache_va == NULL || len == 0)
90*6766e8ceSRuslan Bukin return;
91*6766e8ceSRuslan Bukin
92*6766e8ceSRuslan Bukin mb();
93*6766e8ceSRuslan Bukin
94*6766e8ceSRuslan Bukin for (sva = start; len > 0;) {
95*6766e8ceSRuslan Bukin paddr = pmap_kextract(sva);
96*6766e8ceSRuslan Bukin step = min(PAGE_SIZE - (paddr & PAGE_MASK), len);
97*6766e8ceSRuslan Bukin for (line = rounddown2(paddr, SIFIVE_CCACHE_LINE_SIZE);
98*6766e8ceSRuslan Bukin line < paddr + step;
99*6766e8ceSRuslan Bukin line += SIFIVE_CCACHE_LINE_SIZE)
100*6766e8ceSRuslan Bukin CC_WR8(SIFIVE_CCACHE_FLUSH64, line);
101*6766e8ceSRuslan Bukin sva += step;
102*6766e8ceSRuslan Bukin len -= step;
103*6766e8ceSRuslan Bukin }
104*6766e8ceSRuslan Bukin
105*6766e8ceSRuslan Bukin mb();
106*6766e8ceSRuslan Bukin }
107*6766e8ceSRuslan Bukin
108*6766e8ceSRuslan Bukin static void
ccache_install_hooks(void)109*6766e8ceSRuslan Bukin ccache_install_hooks(void)
110*6766e8ceSRuslan Bukin {
111*6766e8ceSRuslan Bukin struct riscv_cache_ops eswin_ops;
112*6766e8ceSRuslan Bukin
113*6766e8ceSRuslan Bukin eswin_ops.dcache_wbinv_range = ccache_flush_range;
114*6766e8ceSRuslan Bukin eswin_ops.dcache_inv_range = ccache_flush_range;
115*6766e8ceSRuslan Bukin eswin_ops.dcache_wb_range = ccache_flush_range;
116*6766e8ceSRuslan Bukin
117*6766e8ceSRuslan Bukin riscv_cache_install_hooks(&eswin_ops, SIFIVE_CCACHE_LINE_SIZE);
118*6766e8ceSRuslan Bukin }
119*6766e8ceSRuslan Bukin
120*6766e8ceSRuslan Bukin static int
ccache_probe(device_t dev)121*6766e8ceSRuslan Bukin ccache_probe(device_t dev)
122*6766e8ceSRuslan Bukin {
123*6766e8ceSRuslan Bukin
124*6766e8ceSRuslan Bukin if (!ofw_bus_status_okay(dev))
125*6766e8ceSRuslan Bukin return (ENXIO);
126*6766e8ceSRuslan Bukin
127*6766e8ceSRuslan Bukin if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
128*6766e8ceSRuslan Bukin return (ENXIO);
129*6766e8ceSRuslan Bukin
130*6766e8ceSRuslan Bukin if (device_get_unit(dev) != 0)
131*6766e8ceSRuslan Bukin return (ENXIO);
132*6766e8ceSRuslan Bukin
133*6766e8ceSRuslan Bukin device_set_desc(dev, "SiFive Cache Controller");
134*6766e8ceSRuslan Bukin
135*6766e8ceSRuslan Bukin return (BUS_PROBE_DEFAULT);
136*6766e8ceSRuslan Bukin }
137*6766e8ceSRuslan Bukin
138*6766e8ceSRuslan Bukin static int
ccache_attach(device_t dev)139*6766e8ceSRuslan Bukin ccache_attach(device_t dev)
140*6766e8ceSRuslan Bukin {
141*6766e8ceSRuslan Bukin struct ccache_softc *sc;
142*6766e8ceSRuslan Bukin size_t config, ways;
143*6766e8ceSRuslan Bukin
144*6766e8ceSRuslan Bukin sc = device_get_softc(dev);
145*6766e8ceSRuslan Bukin
146*6766e8ceSRuslan Bukin if (bus_alloc_resources(dev, ccache_spec, &sc->res) != 0) {
147*6766e8ceSRuslan Bukin device_printf(dev, "cannot allocate resources for device\n");
148*6766e8ceSRuslan Bukin return (ENXIO);
149*6766e8ceSRuslan Bukin }
150*6766e8ceSRuslan Bukin
151*6766e8ceSRuslan Bukin /* Non-standard EIC7700 cache unit configuration. */
152*6766e8ceSRuslan Bukin config = RD8(sc, SIFIVE_CCACHE_CONFIG);
153*6766e8ceSRuslan Bukin ways = (config & CCACHE_CONFIG_WAYS_M) >> CCACHE_CONFIG_WAYS_S;
154*6766e8ceSRuslan Bukin WR8(sc, SIFIVE_CCACHE_WAYENABLE, (ways - 1));
155*6766e8ceSRuslan Bukin
156*6766e8ceSRuslan Bukin ccache_va = rman_get_virtual(sc->res);
157*6766e8ceSRuslan Bukin ccache_install_hooks();
158*6766e8ceSRuslan Bukin
159*6766e8ceSRuslan Bukin return (0);
160*6766e8ceSRuslan Bukin }
161*6766e8ceSRuslan Bukin
162*6766e8ceSRuslan Bukin static device_method_t ccache_methods[] = {
163*6766e8ceSRuslan Bukin /* Device interface */
164*6766e8ceSRuslan Bukin DEVMETHOD(device_probe, ccache_probe),
165*6766e8ceSRuslan Bukin DEVMETHOD(device_attach, ccache_attach),
166*6766e8ceSRuslan Bukin DEVMETHOD_END
167*6766e8ceSRuslan Bukin };
168*6766e8ceSRuslan Bukin
169*6766e8ceSRuslan Bukin static driver_t ccache_driver = {
170*6766e8ceSRuslan Bukin "ccache",
171*6766e8ceSRuslan Bukin ccache_methods,
172*6766e8ceSRuslan Bukin sizeof(struct ccache_softc),
173*6766e8ceSRuslan Bukin };
174*6766e8ceSRuslan Bukin
175*6766e8ceSRuslan Bukin EARLY_DRIVER_MODULE(ccache, simplebus, ccache_driver, 0, 0,
176*6766e8ceSRuslan Bukin BUS_PASS_BUS + BUS_PASS_ORDER_FIRST);
177*6766e8ceSRuslan Bukin MODULE_VERSION(ccache, 1);
178