1-- Cache maintenance for memory a device reads or writes behind the CPU's
2-- caches, D227's handoff as a concept with a provider for each kind of
3-- memory system (D970). A range is maintained in whole lines or not at
4-- all: a range that does not begin and end on a line is refused, never
5-- rounded out, because a rounded line may hold bytes the caller does
6-- not own, whose dirty copy an invalidate loses or whose stale copy a
7-- clean writes over a device's data. Every operation ends with the
8-- completion barrier, so it has reached the point a device observes
9-- when it returns; the device barrier before enabling a transfer stays
10-- the driver's, as D227 orders it.
11--
12-- `cacheless` is the provider for memory no cache stands in front of,
13-- on every target: its operations are the barriers alone, and it
14-- refuses the ranges a cached provider of the same line would, so that a
15-- driver checked on a cacheless core takes the same refusals on a cached
16-- one. `platform/cortex_m/l1` provides the Cortex-M7's level 1 caches on
17-- a cached profile (D972).
18
19--- A range that does not begin on a cache line or is not a whole number
20--- of lines: nothing was maintained.
21public misaligned: atom
22--- A line size that is not a nonzero power of two.
23public bad_line: atom
24
25--- Keeping memory and a device that reaches it behind the caches in
26--- step. Each operation takes a range of whole lines, as `lines` checks,
27--- and returns once it is complete at the point the device observes.
28public maintenance: type = concept (provider: type)
29 --- The bytes in one line, which every range is a whole number of.
30 line: (self: provider) -> (bytes: usize)
31 --- Write the range's dirty lines to memory, keeping them cached:
32 --- before a device reads what the CPU wrote, as a transmit does.
33 clean: (self: provider, range: []u8) -> none ! misaligned
34 --- Discard the range's cached lines, dirty or not, so the next read
35 --- is memory's: after a device wrote it, as a receive completes.
36 invalidate: (self: provider, range: []u8) -> none ! misaligned
37 --- Write the range's dirty lines to memory and discard them: before
38 --- a device writes it, as a receive is armed.
39 clean_invalidate: (self: provider, range: []u8) -> none ! misaligned
40end maintenance
41
42--- Count the lines of `line` bytes in `range`, refusing a range that
43--- does not begin on a line or end on one. An empty range is no lines.
44public lines: (line: usize, range: []u8) -> (count: usize) ! misaligned =
45 size := lenof range
46 count = 0
47 return when size == 0
48 fail misaligned when (size & (line - 1)) <> 0
49 or (usize(addr range[0]) & (line - 1)) <> 0
50 count = size / line
51end lines
52
53--- The bytes in one line of the provider's caches.
54public line: (provider: type is maintenance, caches: provider)
55 -> (bytes: usize) =
56 bytes = provider.line(caches)
57end line
58
59--- Clean a range of whole lines, through `maintenance` evidence.
60public clean: (provider: type is maintenance, caches: provider,
61 range: []u8) -> none ! misaligned =
62 try provider.clean(caches, range)
63end clean
64
65--- Invalidate a range of whole lines, through `maintenance` evidence.
66public invalidate: (provider: type is maintenance, caches: provider,
67 range: []u8) -> none ! misaligned =
68 try provider.invalidate(caches, range)
69end invalidate
70
71--- Clean and invalidate a range of whole lines, through `maintenance`
72--- evidence.
73public clean_invalidate: (provider: type is maintenance, caches: provider,
74 range: []u8) -> none ! misaligned =
75 try provider.clean_invalidate(caches, range)
76end clean_invalidate
77
78--- The cacheless provider's handle: memory a device observes as the CPU
79--- does, ranges held to lines of `line` bytes.
80public barriers: type = struct
81 line: usize
82end barriers
83
84--- Make the cacheless provider, which refuses ranges that are not whole
85--- lines of `line` bytes. Its operations check the range and issue the
86--- completion barrier: all that memory with no cache in front needs.
87public cacheless: (line: usize) -> (caches: barriers) ! bad_line =
88 fail bad_line when line == 0 or (line & (line - 1)) <> 0
89 caches = (line: line)
90end cacheless
91
92barriers_line: (self: barriers) -> (bytes: usize) = bytes = self.line end barriers_line
93
94barriers_complete: (self: barriers, range: []u8) -> none ! misaligned =
95 _ = try lines(self.line, range)
96 compiler.completion_barrier()
97end barriers_complete
98
99barriers is maintenance (line: barriers_line, clean: barriers_complete,
100 invalidate: barriers_complete,
101 clean_invalidate: barriers_complete)