RTEMS 7.0-rc1
Loading...
Searching...
No Matches
alt_cache.h
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/*
4 * Copyright (C) 2024 Kevin Kirspel
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28/******************************************************************************
29* *
30* License Agreement *
31* *
32* Copyright (c) 2003, 2007 Altera Corporation, San Jose, California, USA. *
33* All rights reserved. *
34* *
35* Permission is hereby granted, free of charge, to any person obtaining a *
36* copy of this software and associated documentation files (the "Software"), *
37* to deal in the Software without restriction, including without limitation *
38* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
39* and/or sell copies of the Software, and to permit persons to whom the *
40* Software is furnished to do so, subject to the following conditions: *
41* *
42* The above copyright notice and this permission notice shall be included in *
43* all copies or substantial portions of the Software. *
44* *
45* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
46* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
47* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
48* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
49* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
50* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
51* DEALINGS IN THE SOFTWARE. *
52* *
53* This agreement shall be governed in all respects by the laws of the State *
54* of California and by the laws of the United States of America. *
55* *
56******************************************************************************/
57
58#ifndef __ALT_CACHE_H__
59#define __ALT_CACHE_H__
60
61/*
62 * Cache maintenance macros
63 * CLEAN - Writeback to memory;
64 * FLUSH - Writeback to memory and invalidate.
65 */
66
67#if ALT_CPU_DCACHE_SIZE > 0
68#define DCACHE_CLEAN_BY_INDEX_VAL(i) \
69__asm__ volatile(".insn i 0x0F, 0x2, zero, %[i_reg], 0x081" :: [i_reg] "r"(i));
70
71#define DCACHE_FLUSH_BY_INDEX_VAL(i) \
72__asm__ volatile(".insn i 0x0F, 0x2, zero, %[i_reg], 0x082" :: [i_reg] "r"(i));
73
74#define DCACHE_INVALIDATE_BY_INDEX_VAL(i) \
75__asm__ volatile(".insn i 0x0F, 0x2, zero, %[i_reg], 0x080" :: [i_reg] "r"(i));
76
77#define ALT_FLUSH_DATA(i) \
78__asm__ volatile(".option arch, +zicbom\n" "cbo.flush 0(%[addr])" :: [addr] "r"(i))
79
80#define ALT_INVALIDATE_DATA(i) \
81__asm__ volatile(".option arch, +zicbom\n" "cbo.inval 0(%[addr])" :: [addr] "r"(i))
82#endif
83
84/*
85 * alt_cache.h defines the processor specific functions for manipulating the
86 * cache.
87 */
88
89#ifdef __cplusplus
90extern "C"
91{
92#endif /* __cplusplus */
93
94/*
95 * alt_icache_flush() is called to flush the instruction cache for a memory
96 * region of length "len" bytes, starting at address "start".
97 */
98
99static inline void alt_icache_flush (const void* start, uint32_t len)
100{
101#if ALT_CPU_ICACHE_SIZE > 0
102 __asm__ volatile(".option arch, +zifencei\n" "fence.i" ::: "memory");
103#else
104 (void) start;
105 (void) len;
106#endif
107}
108
109/*
110 * alt_dcache_flush() is called to flush the data cache for a memory
111 * region of length "len" bytes, starting at address "start".
112 * Any dirty lines in the data cache are written back to memory.
113 */
114
115static inline void alt_dcache_flush (const void* start, uint32_t len)
116{
117#if ALT_CPU_DCACHE_SIZE > 0
118 const char* i;
119 const char* end = ((char*)start) + len;
120
121 for (i = start; i < end; i+= ALT_CPU_DCACHE_LINE_SIZE) {
122 ALT_FLUSH_DATA(i);
123 }
124
125 /*
126 * For an unaligned flush request, we've got one more line left.
127 * Note that this is dependent on ALT_CPU_DCACHE_LINE_SIZE to be a
128 * multiple of 2 (which it always is).
129 */
130 if (((uint32_t)start) & (ALT_CPU_DCACHE_LINE_SIZE - 1)) {
131 ALT_FLUSH_DATA(i);
132 }
133#else
134 (void) start;
135 (void) len;
136#endif
137}
138
139/*
140 * alt_dcache_flush() is called to flush the data cache for a memory
141 * region of length "len" bytes, starting at address "start".
142 * Any dirty lines in the data cache are NOT written back to memory.
143 */
144
145static inline void alt_dcache_flush_no_writeback (
146 const void* start,
147 uint32_t len
148)
149{
150#if ALT_CPU_DCACHE_SIZE > 0
151 const char* i;
152 const char* end = ((char*)start) + len;
153
154 for (i = start; i < end; i+= ALT_CPU_DCACHE_LINE_SIZE) {
155 ALT_INVALIDATE_DATA(i);
156 }
157
158 /*
159 * For an unaligned invalidate request, we've got one more line left.
160 * Note that this is dependent on ALT_CPU_DCACHE_LINE_SIZE to be a
161 * multiple of 2 (which it always is).
162 */
163 if (((uint32_t)start) & (ALT_CPU_DCACHE_LINE_SIZE - 1)) {
164 ALT_INVALIDATE_DATA(i);
165 }
166#else
167 (void) start;
168 (void) len;
169#endif
170}
171
172/*
173 * Flush the entire instruction cache.
174 */
175
176static inline void alt_icache_flush_all (void)
177{
178#if ALT_CPU_ICACHE_SIZE > 0
179 __asm__ volatile(".option arch, +zifencei\n" "fence.i" ::: "memory");
180#endif
181}
182
183/*
184 * Flush the entire data cache.
185 */
186
187static inline void alt_dcache_flush_all (void)
188{
189#if ALT_CPU_DCACHE_SIZE > 0
190 char* i;
191 for (
192 i = (char*)0;
193 i < (char*) ALT_CPU_DCACHE_SIZE;
194 i+= ALT_CPU_DCACHE_LINE_SIZE
195 ) {
196 DCACHE_CLEAN_BY_INDEX_VAL(i);
197 }
198#endif
199}
200
201#ifdef __cplusplus
202}
203#endif
204
205#endif /* __ALT_CACHE_H__ */