forked from gloomyandy/CoreN2G
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCache.cpp
404 lines (354 loc) · 12.9 KB
/
Cache.cpp
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* Cache.cpp
*
* Created on: 22 Nov 2019
* Author: David
*/
#include "Cache.h"
#ifdef RTOS
# include <RTOSIface/RTOSIface.h>
#endif
#if SAM4E || SAME70 || SAME5x
#if SAME70
# include <core_cm7.h>
#define USE_MPU 1
extern uint32_t _nocache_ram_start;
extern uint32_t _nocache_ram_end;
# if USE_MPU
# include <mpu_armv7.h>
// Macro ARM_MPU_RASR_EX is incorrectly defined in CMSIS 5.4.0, see https://github.com/ARM-software/CMSIS_5/releases. Redefine it here.
# undef ARM_MPU_RASR_EX
/**
* MPU Region Attribute and Size Register Value
*
* \param DisableExec Instruction access disable bit, 1= disable instruction fetches.
* \param AccessPermission Data access permissions, allows you to configure read/write access for User and Privileged mode.
* \param AccessAttributes Memory access attribution, see \ref ARM_MPU_ACCESS_.
* \param SubRegionDisable Sub-region disable field.
* \param Size Region size of the region to be configured, for example 4K, 8K.
*/
# define ARM_MPU_RASR_EX(DisableExec, AccessPermission, AccessAttributes, SubRegionDisable, Size) \
((((DisableExec) << MPU_RASR_XN_Pos) & MPU_RASR_XN_Msk) | \
(((AccessPermission) << MPU_RASR_AP_Pos) & MPU_RASR_AP_Msk) | \
(((AccessAttributes) & (MPU_RASR_TEX_Msk | MPU_RASR_S_Msk | MPU_RASR_C_Msk | MPU_RASR_B_Msk))) | \
(((SubRegionDisable) << MPU_RASR_SRD_Pos) & MPU_RASR_SRD_Msk) | \
(((Size) << MPU_RASR_SIZE_Pos) & MPU_RASR_SIZE_Msk) | \
(((MPU_RASR_ENABLE_Msk))))
# endif
#else
// SAM4E and SAME5x use fairly similar cache controllers
const unsigned int CacheWays = 4;
const uint32_t CacheLineSize = 16; // each cache line is 16 bytes long on both SAME5x and SAM4E
const unsigned int CacheLineShift = 4; // how many bits we shift an address right to get the cache line number
static_assert(CacheLineSize == 1u << CacheLineShift);
# if SAME5x
const uint32_t CacheLinesPerWay = 64;
static_assert(CacheWays * CacheLinesPerWay * CacheLineSize == 4096); // cache size is 4K
# elif SAM4E
const uint32_t CacheLinesPerWay = 32;
static_assert(CacheWays * CacheLinesPerWay * CacheLineSize == 2048); // cache size is 2K
# endif
// Check whether the cache is enabled
static inline bool is_cache_enabled() noexcept
{
#if SAME5x
return CMCC->SR.bit.CSTS;
#elif SAM4E
return (CMCC->CMCC_SR & CMCC_SR_CSTS) != 0;
#endif
}
// Disable the cache
static inline void cache_disable() noexcept
{
while (is_cache_enabled())
{
#if SAME5x
CMCC->CTRL.reg = 0;
#elif SAM4E
CMCC->CMCC_CTRL = 0;
#endif
__ISB();
#if !(SAME5x && CACHE_INSTRUCTIONS_ONLY)
__DSB();
#endif
}
}
// Enable the cache. If this is called other than from an invalidate operation, the cache must be invalidated first.
static inline void cache_enable() noexcept
{
#if SAME5x
# if CACHE_INSTRUCTIONS_ONLY
CMCC->CFG.bit.DCDIS = 1;
# endif
CMCC->CTRL.reg = CMCC_CTRL_CEN;
#elif SAM4E
CMCC->CMCC_CTRL = CMCC_CTRL_CEN;
#endif
__ISB();
#if !(SAME5x && CACHE_INSTRUCTIONS_ONLY)
__DSB();
#endif
}
// Invalidate the whole cache. Cache must be disabled first.
static inline void cache_invalidate_all() noexcept
{
#if SAME5x
CMCC->MAINT0.reg = CMCC_MAINT0_INVALL;
#elif SAM4E
CMCC->CMCC_MAINT0 = CMCC_MAINT0_INVALL;
#endif
__ISB();
#if !(SAME5x && CACHE_INSTRUCTIONS_ONLY)
__DSB();
#endif
}
#if !(SAME5x && CACHE_INSTRUCTIONS_ONLY)
// Invalidate a region
// Due to the time this can take, above a certain size it is quicker to invalidate the whole cache.
// DMA receive buffer sizes we use commonly:
// TMC2660 driver: 4
// TMC2209 driver: 20
// ADC on SAME5x: 32
// SBC header: 16
// SBC reply: 4
// SBC data: up to 8Kb
// WiFi header: 12
// WiFi data: up to 2Kb
// GMAC Tx or Rx descriptor on SAME5x: 8
// GMAC data: up to about 1500
// CAN buffers on SAME5x: 72 (8-byte aligned, so always fits in 5 cache lines)
// All of the short buffers fit within 5 cache lines
constexpr uint32_t MaxSelectiveInvalidateCacheLines = 6;
static inline void cache_invalidate_region(const volatile void *start, size_t length) noexcept
{
if (length != 0 && is_cache_enabled()) // the following won't work if the length is zero
{
// In non-RTOS builds, just invalidate the whole cache always
#ifdef RTOS
const uint32_t startAddr = reinterpret_cast<uint32_t>(start); // convert start address to integer
length += startAddr & (CacheLineSize - 1); // round up size to start of the first cache line
uint32_t numLines = (length + (CacheLineSize - 1)) >> CacheLineShift; // get the number of cache lines to be invalidated
if (numLines > MaxSelectiveInvalidateCacheLines) // if more than a small part the cache needs to be invalidated, invalidate the whole cache
#endif
{
const irqflags_t flags = IrqSave();
cache_disable();
cache_invalidate_all();
cache_enable();
IrqRestore(flags);
}
#ifdef RTOS
else
{
// Unfortunately we have to disable the cache to invalidate cache lines.
// We must do no data writes while the cache is disabled, or we might subsequently read stale data. Therefore interrupts must be disabled.
uint32_t startLine = startAddr >> CacheLineShift; // this will have extraneous bits set, but CMCC_MAINT1_INDEX will mask those off
while (numLines != 0)
{
// Invalidate this cache line in all 4 ways
const irqflags_t flags = IrqSave();
cache_disable();
# if SAME5x
CMCC->MAINT1.reg = CMCC_MAINT1_WAY(0) | CMCC_MAINT1_INDEX(startLine);
CMCC->MAINT1.reg = CMCC_MAINT1_WAY(1) | CMCC_MAINT1_INDEX(startLine);
CMCC->MAINT1.reg = CMCC_MAINT1_WAY(2) | CMCC_MAINT1_INDEX(startLine);
CMCC->MAINT1.reg = CMCC_MAINT1_WAY(3) | CMCC_MAINT1_INDEX(startLine);
# elif SAM4E
CMCC->CMCC_MAINT1 = CMCC_MAINT1_WAY_WAY0 | CMCC_MAINT1_INDEX(startLine);
CMCC->CMCC_MAINT1 = CMCC_MAINT1_WAY_WAY1 | CMCC_MAINT1_INDEX(startLine);
CMCC->CMCC_MAINT1 = CMCC_MAINT1_WAY_WAY2 | CMCC_MAINT1_INDEX(startLine);
CMCC->CMCC_MAINT1 = CMCC_MAINT1_WAY_WAY3 | CMCC_MAINT1_INDEX(startLine);
# endif
cache_enable();
IrqRestore(flags);
++startLine;
--numLines;
}
}
#endif
}
}
#endif
#endif
void Cache::Init() noexcept
{
#if SAME70 && USE_MPU
# if 0
// For debugging
# define CACHE_MODE ARM_MPU_CACHEP_WT_NWA
# else
// Normal operation
# define CACHE_MODE ARM_MPU_CACHEP_WB_WRA
# endif
// Set up the MPU so that we can have a non-cacheable RAM region, and so that we can trap accesses to non-existent memory
// Where regions overlap, the region with the highest region number takes priority
constexpr ARM_MPU_Region_t regionTable[] =
{
// Flash memory: read-only, execute allowed, cacheable
{
ARM_MPU_RBAR(0, IFLASH_ADDR),
ARM_MPU_RASR_EX(0u, ARM_MPU_AP_RO, ARM_MPU_ACCESS_NORMAL(ARM_MPU_CACHEP_WB_WRA, ARM_MPU_CACHEP_WB_WRA, 0u), 0u, ARM_MPU_REGION_SIZE_1MB)
},
// First 512b of the flash memory is also the flash write page buffer, which we need to write to when writing the user page
{
ARM_MPU_RBAR(1, IFLASH_ADDR),
ARM_MPU_RASR_EX(0u, ARM_MPU_AP_FULL, ARM_MPU_ACCESS_NORMAL(ARM_MPU_CACHEP_WT_NWA, ARM_MPU_CACHEP_WT_NWA, 0u), 0u, ARM_MPU_REGION_SIZE_512B)
},
// First 256kb RAM, read-write, cacheable, execute disabled (parts of this are overridden later)
{
ARM_MPU_RBAR(2, IRAM_ADDR),
ARM_MPU_RASR_EX(1u, ARM_MPU_AP_FULL, ARM_MPU_ACCESS_NORMAL(CACHE_MODE, CACHE_MODE, 0u), 0u, ARM_MPU_REGION_SIZE_256KB)
},
// First 64kb RAM, read-write, shared, execute disabled
{
ARM_MPU_RBAR(3, IRAM_ADDR),
ARM_MPU_RASR_EX(1u, ARM_MPU_AP_FULL, ARM_MPU_ACCESS_NORMAL(ARM_MPU_CACHEP_NOCACHE, ARM_MPU_CACHEP_NOCACHE, 1u), 0u, ARM_MPU_REGION_SIZE_64KB)
},
// Next 32kb RAM, read-write, shared, execute disabled
{
ARM_MPU_RBAR(4, IRAM_ADDR + 0x00010000),
ARM_MPU_RASR_EX(1u, ARM_MPU_AP_FULL, ARM_MPU_ACCESS_NORMAL(ARM_MPU_CACHEP_NOCACHE, ARM_MPU_CACHEP_NOCACHE, 1u), 0u, ARM_MPU_REGION_SIZE_32KB)
},
// RAMFUNC memory. Read-only (the code has already been written to it), execution allowed. The initialised data memory follows, so it must be RW.
// 256 bytes is enough at present (check the linker memory map if adding more RAMFUNCs).
{
ARM_MPU_RBAR(5, IRAM_ADDR + 0x00018000),
ARM_MPU_RASR_EX(0u, ARM_MPU_AP_FULL, ARM_MPU_ACCESS_NORMAL(CACHE_MODE, CACHE_MODE, 0u), 0u, ARM_MPU_REGION_SIZE_256B)
},
// Final 128kb RAM, read-write, cacheable, execute disabled
{
ARM_MPU_RBAR(6, IRAM_ADDR + 0x00040000),
ARM_MPU_RASR_EX(1u, ARM_MPU_AP_FULL, ARM_MPU_ACCESS_NORMAL(CACHE_MODE, CACHE_MODE, 0u), 0u, ARM_MPU_REGION_SIZE_128KB)
},
// Peripherals
{
ARM_MPU_RBAR(7, 0x40000000),
ARM_MPU_RASR_EX(1u, ARM_MPU_AP_FULL, ARM_MPU_ACCESS_DEVICE(1u), 0u, ARM_MPU_REGION_SIZE_16MB)
},
// USBHS
{
ARM_MPU_RBAR(8, 0xA0100000),
ARM_MPU_RASR_EX(1u, ARM_MPU_AP_FULL, ARM_MPU_ACCESS_DEVICE(1u), 0u, ARM_MPU_REGION_SIZE_1MB)
},
// ROM
{
ARM_MPU_RBAR(9, IROM_ADDR),
ARM_MPU_RASR_EX(0u, ARM_MPU_AP_RO, ARM_MPU_ACCESS_NORMAL(ARM_MPU_CACHEP_WT_NWA, ARM_MPU_CACHEP_WT_NWA, 0u), 0u, ARM_MPU_REGION_SIZE_4MB)
},
// ARM Private Peripheral Bus
{
ARM_MPU_RBAR(10, 0xE0000000),
ARM_MPU_RASR_EX(1u, ARM_MPU_AP_FULL, ARM_MPU_ACCESS_ORDERED, 0u, ARM_MPU_REGION_SIZE_1MB)
}
};
static_assert(ARRAY_SIZE(regionTable) <= 16); // SAME70 supports 16 regions
// Ensure MPU is disabled
ARM_MPU_Disable();
// Clear all regions
const uint32_t numRegions = (MPU->TYPE & MPU_TYPE_DREGION_Msk) >> MPU_TYPE_DREGION_Pos;
for (unsigned int region = 0; region < numRegions; ++region)
{
ARM_MPU_ClrRegion(region);
}
// Load regions from our table
ARM_MPU_Load(regionTable, ARRAY_SIZE(regionTable));
// Enable the MPU, disabling the default map but allowing exception handlers to use it
ARM_MPU_Enable(0x01);
#elif SAME5x
CMCC->MCFG.reg = CMCC_MCFG_MODE_DHIT_COUNT; // data hit mode
CMCC->MEN.bit.MENABLE = 1;
#elif SAM4E
CMCC->CMCC_MCFG = 1; // instruction hit mode
CMCC->CMCC_MEN |= CMCC_MEN_MENABLE;
#endif
}
void Cache::Enable() noexcept
{
#if SAME70
if ((SCB->CCR & SCB_CCR_IC_Msk) == 0) // if instruction cache is not enabled
{
SCB_EnableICache();
}
if ((SCB->CCR & SCB_CCR_DC_Msk) == 0) // if data cache is not enabled
{
SCB_EnableDCache();
}
#else
const irqflags_t flags = IrqSave();
if (!is_cache_enabled())
{
cache_invalidate_all();
cache_enable();
}
IrqRestore(flags);
#endif
}
// Disable the cache, returning true if it was enabled
bool Cache::Disable() noexcept
{
#if SAME70
if ((SCB->CCR & SCB_CCR_IC_Msk) != 0) // if instruction cache is enabled
{
SCB_DisableICache();
}
const bool wasEnabled = (SCB->CCR & SCB_CCR_DC_Msk) != 0;
if (wasEnabled) // if data cache is enabled
{
// Warning: this code is fragile! There must be no memory writes while flushing the data cache, hence we must disable interrupts.
const irqflags_t flags = IrqSave();
SCB_DisableDCache(); // this cleans it as well as disabling it
IrqRestore(flags);
}
#else
const bool wasEnabled = is_cache_enabled();
cache_disable();
#endif
return wasEnabled;
}
#if SAME70
extern "C" [[noreturn]] void vAssertCalled(uint32_t line, const char *file) noexcept;
void Cache::Flush(const volatile void *start, size_t length) noexcept
{
if ((SCB->CCR & SCB_CCR_DC_Msk) != 0) // if data cache is enabled
{
// The DMA buffer should be entirely inside the non-cached RAM area
if ((const char *)start < (const char *)&_nocache_ram_start || (const char *)start + length >= (const char *)&_nocache_ram_end)
{
vAssertCalled(__LINE__, __FILE__);
}
}
}
#endif
#if !(SAME5x && CACHE_INSTRUCTIONS_ONLY)
void Cache::Invalidate(const volatile void *start, size_t length) noexcept
{
# if SAME70
if ((SCB->CCR & SCB_CCR_DC_Msk) != 0) // if data cache is enabled
{
// The DMA buffer should be entirely inside the non-cached RAM area
if ((const char *)start < (const char *)&_nocache_ram_start || (const char *)start + length >= (const char *)&_nocache_ram_end)
{
vAssertCalled(__LINE__, __FILE__);
}
}
# else
cache_invalidate_region(start, length);
# endif
}
#endif
#if SAM4E || SAME5x
uint32_t Cache::GetHitCount() noexcept
{
#if SAM4E
return CMCC->CMCC_MSR;
#elif SAME5x
return CMCC->MSR.reg;
#endif
}
#endif
#endif // SAM4E || SAME70 || SAME5x
// Entry points that can be called from ASF C code
void CacheFlushBeforeDMAReceive(const volatile void *start, size_t length) noexcept { Cache::FlushBeforeDMAReceive(start, length); }
void CacheInvalidateAfterDMAReceive(const volatile void *start, size_t length) noexcept { Cache::InvalidateAfterDMAReceive(start, length); }
void CacheFlushBeforeDMASend(const volatile void *start, size_t length) noexcept { Cache::FlushBeforeDMASend(start, length); }
// End