Landin library reference discuss source

platform/cortex_m/l1/l1.ldn

1--  The Cortex-M7's level 1 caches as `core/cache`'s maintenance provider
2--  (D972): on a cached profile, one whose description declares
3--  `compiler.cache("armv7-m-l1", 32)`, whose reset enabled them and whose
4--  entry's return disables them (D973).  Elsewhere on an M-profile target
5--  it declares nothing, and a call says it is a cached profile's; as a
6--  submodule of `platform/cortex_m` it is imported on an M-profile target
7--  alone (D267).  It is a module apart from `platform/cortex_m`, so that a
8--  program that maintains no cache reaches none of `core/cache`.
9
10import core/cache
11
12fixed if compiler.feature.cache then
13    --- The level 1 caches' handle for `core/cache`'s maintenance, made by
14    --- `new`. Its line is the description's, 32 bytes.
15    public handle: type = struct
16        line: usize
17    end handle
18
19    --- The maintenance provider of a cached profile's caches, which reset
20    --- enabled. It borrows nothing: the caches are the core's.
21    public new: () -> (caches: handle) =
22        caches = (line: line_bytes)
23    end new
24
25    --- CCSIDR for the level 1 data cache, which reset selected in CSSELR
26    --- and nothing else changes. Bits 2:0 are LineSize, the log of the
27    --- line's words less 2, bits 12:3 the associativity less one and bits
28    --- 27:13 the sets less one, as CMSIS's core_cm7.h defines its fields.
29    public geometry: () -> (ccsidr: u32) =
30        at: ptr u32 = ptr(ccsidr_address)
31        ccsidr = compiler.volatile_load(at)
32    end geometry
33
34    line_bytes: usize = compiler.cache_line
35    ccsidr_address: usize = 0xe000_ed80
36    -- The SCB's maintenance by address, at the addresses CMSIS's
37    -- core_cm7.h gives the SCB's registers: DCIMVAC invalidates, DCCMVAC
38    -- cleans, and DCCIMVAC cleans and invalidates, the data cache line
39    -- holding the address written to it.
40    dcimvac: usize = 0xe000_ef5c
41    dccmvac: usize = 0xe000_ef68
42    dccimvac: usize = 0xe000_ef70
43
44    -- Each line's address written to OPERATION, between completion
45    -- barriers: the first orders the CPU's earlier accesses to the range
46    -- before its maintenance, the second completes the maintenance before
47    -- anything after it, a device's enable among them, and ISB then
48    -- refetches what follows, as CMSIS's SCB_CleanDCache_by_Addr orders
49    -- them.
50    maintain: (operation: usize, line: usize, range: []u8)
51              -> none ! cache.misaligned =
52        count := try cache.lines(line, range)
53        compiler.completion_barrier()
54        if count > 0 then
55            at: ptr mut u32 = ptr(operation)
56            first := usize(addr range[0])
57            for index in 0..<count do
58                compiler.volatile_store(at, u32(first + index * line))
59            end for
60        end if
61        compiler.completion_barrier()
62        assembler.block("isb sy")
63    end maintain
64
65    handle_line: (self: handle) -> (bytes: usize) = bytes = self.line end handle_line
66
67    handle_clean: (self: handle, range: []u8) -> none ! cache.misaligned =
68        try maintain(dccmvac, self.line, range)
69    end handle_clean
70
71    handle_invalidate: (self: handle, range: []u8) -> none ! cache.misaligned =
72        try maintain(dcimvac, self.line, range)
73    end handle_invalidate
74
75    handle_clean_invalidate: (self: handle, range: []u8)
76                             -> none ! cache.misaligned =
77        try maintain(dccimvac, self.line, range)
78    end handle_clean_invalidate
79
80    handle is cache.maintenance (line: handle_line, clean: handle_clean,
81                                 invalidate: handle_invalidate,
82                                 clean_invalidate: handle_clean_invalidate)
83end if