/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2024 The FreeBSD Foundation4*5* This software was developed by Mitchell Horne <[email protected]> under6* sponsorship from the FreeBSD Foundation.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND18* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE21* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT25* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY26* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF27* SUCH DAMAGE.28*/29#include <sys/param.h>30#include <sys/systm.h>3132#include <machine/thead.h>3334bool has_errata_thead_pbmt = false;3536/* ----------------- dcache ops --------------------- */373839/* th.dcache.civa: clean & invalidate at VA stored in t0. */40#define THEAD_DCACHE_CIVA ".long 0x0272800b\n"4142/* th.dcache.iva: invalidate at VA stored in t0. */43#define THEAD_DCACHE_IVA ".long 0x0262800b\n"4445/* th.dcache.cva: clean at VA stored in t0. */46#define THEAD_DCACHE_CVA ".long 0x0252800b\n"4748/* th.sync.s: two-way instruction barrier */49#define THEAD_SYNC_S ".long 0x0190000b\n"5051/* MHTODO: we could parse this information from the device tree. */52#define THEAD_DCACHE_SIZE 645354static void55thead_cpu_dcache_wbinv_range(vm_offset_t va, vm_size_t len)56{57register vm_offset_t t0 __asm("t0") = rounddown(va, dcache_line_size);5859for (; t0 < va + len; t0 += dcache_line_size) {60__asm __volatile(THEAD_DCACHE_CIVA61:: "r" (t0) : "memory");62}63__asm __volatile(THEAD_SYNC_S ::: "memory");64}6566static void67thead_cpu_dcache_inv_range(vm_offset_t va, vm_size_t len)68{69register vm_offset_t t0 __asm("t0") = rounddown(va, dcache_line_size);7071for (; t0 < va + len; t0 += dcache_line_size) {72__asm __volatile(THEAD_DCACHE_IVA73:: "r" (t0) : "memory");74}75__asm __volatile(THEAD_SYNC_S ::: "memory");76}7778static void79thead_cpu_dcache_wb_range(vm_offset_t va, vm_size_t len)80{81register vm_offset_t t0 __asm("t0") = rounddown(va, dcache_line_size);8283for (; t0 < va + len; t0 += dcache_line_size) {84__asm __volatile(THEAD_DCACHE_CVA85:: "r" (t0) : "memory");86}87__asm __volatile(THEAD_SYNC_S ::: "memory");88}8990void91thead_setup_cache(void)92{93struct riscv_cache_ops thead_ops;9495thead_ops.dcache_wbinv_range = thead_cpu_dcache_wbinv_range;96thead_ops.dcache_inv_range = thead_cpu_dcache_inv_range;97thead_ops.dcache_wb_range = thead_cpu_dcache_wb_range;9899riscv_cache_install_hooks(&thead_ops, THEAD_DCACHE_SIZE);100}101102103