summaryrefslogtreecommitdiff
path: root/libcpu/risc-v/t-head/cache.c
blob: 5fabe80ddf3910af83b1ab8c4cee2d7104056167 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Copyright (c) 2006-2025, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-01-29     lizhirui     first version
 * 2021-11-05     JasonHu      add c906/c908 cache inst
 * 2022-11-09     WangXiaoyao  Support cache coherence operations;
 *                             improve portability and make
 *                             no assumption on undefined behavior
 * 2025-04-26     GuEe-GUI     Follow the specific and vendor
 */

#include <rthw.h>
#include <rtcompiler.h>

#include <cache.h>
#include <opcode.h>
#ifdef RT_USING_SMP
#include <smp_call.h>
#endif

#define THEAD_CMD_OP(_op, _sync_op, _start, _size, _cachesize)      \
    rt_ubase_t _block = (rt_ubase_t)_start & ~(_cachesize - 1UL);   \
    rt_ubase_t _end = (rt_ubase_t)_start + _size;                   \
    for (; _block < _end; _block += _cachesize)                     \
    {                                                               \
        __asm__ volatile (_op(%0)::"r"(_block):"memory");           \
    }                                                               \
    __asm__ volatile (_sync_op:::"memory");

static void thead_icache_invalidate_local(void *data)
{
    rt_ubase_t *start_size = data;
    rt_uint32_t cachesize = riscv_cache_get_cbom_block_size();

    THEAD_CMD_OP(OPC_ICACHE_IVA, OPC_SYNC_IS, start_size[0], start_size[1], cachesize);
}

static void thead_icache_invalidate(void *addr, int size)
{
    rt_ubase_t start_size[2] = { (rt_ubase_t)addr, size };

    thead_icache_invalidate_local(start_size);

#ifdef RT_USING_SMP
    rt_smp_call_each_cpu(thead_icache_invalidate_local, start_size, SMP_CALL_WAIT_ALL);
#endif
}

static void thead_icache_invalidate_all_local(void *data)
{
    RT_UNUSED(data);

    __asm__ volatile(OPC_ICACHE_IALL "\n\t" OPC_SYNC_I:::"memory");
}

static void thead_icache_invalidate_all(void)
{
    thead_icache_invalidate_all_local(RT_NULL);

#ifdef RT_USING_SMP
    rt_smp_call_each_cpu(thead_icache_invalidate_all_local, RT_NULL, SMP_CALL_WAIT_ALL);
#endif
}

static void thead_dcache_invalidate(void *addr, int size)
{
    rt_uint32_t cachesize = riscv_cache_get_cbom_block_size();

    THEAD_CMD_OP(OPC_DCACHE_IVA, OPC_SYNC_S, addr, size, cachesize);
}

static void thead_dcache_clean(void *addr, int size)
{
    rt_uint32_t cachesize = riscv_cache_get_cbom_block_size();

    THEAD_CMD_OP(OPC_DCACHE_CVA, OPC_SYNC_S, addr, size, cachesize);
}

static void thead_dcache_flush(void *addr, int size)
{
    rt_uint32_t cachesize = riscv_cache_get_cbom_block_size();

    THEAD_CMD_OP(OPC_DCACHE_CIVA, OPC_SYNC_S, addr, size, cachesize);
}

/**
 * =====================================================
 * Architecture Independent API
 * =====================================================
 */

void rt_hw_cpu_icache_ops(int ops, void *addr, int size)
{
    if (ops == RT_HW_CACHE_INVALIDATE)
    {
        rt_hw_cpu_icache_invalidate(addr, size);
    }
}

void rt_hw_cpu_dcache_ops(int ops, void *addr, int size)
{
    if (ops == RT_HW_CACHE_FLUSH)
    {
        thead_dcache_clean(addr, size);
    }
    else
    {
        thead_dcache_invalidate(addr, size);
    }
}

void rt_hw_cpu_icache_invalidate_all(void)
{
    thead_icache_invalidate_all();
}

void rt_hw_cpu_icache_invalidate(void *addr, rt_size_t size)
{
    thead_icache_invalidate(addr, size);
}

void rt_hw_cpu_dcache_clean_and_invalidate(void *addr, rt_size_t size)
{
    thead_dcache_flush(addr, size);
}

void rt_hw_icache_invalidate_all(void)
{
    thead_icache_invalidate_all();
}

/**
 * @brief Synchronize cache to Point of Unification
 */
void rt_hw_sync_cache_local(void *addr, int size)
{
    __asm__ volatile(OPC_DCACHE_CVAL1(a0)::: "memory");

    thead_icache_invalidate(addr, size);
}