Landin library reference discuss source

Shared: available on every enabled target.

Cache maintenance for memory a device reads or writes behind the CPU's caches: handing a DMA buffer between CPU and device as one concept, maintenance, over ranges of whole lines (D227).

Call What it does
cache.line The bytes in one of the provider's lines.
cache.clean Write the range's dirty lines to memory and keep them: before a device reads what the CPU wrote, as a transmit does.
cache.invalidate Discard the range's lines, so the next read is memory's: once a device has written it, as a receive completes.
cache.clean_invalidate Both: before a device writes the range, as a receive is armed.
cache.lines How many lines a range is, refusing one that is not whole lines.
cache.cacheless The provider for memory no cache stands in front of: the barriers alone, on every target.

A range must begin on a line and be a whole number of lines. One that is not is refused with cache.misaligned, and nothing is maintained.

A range is never rounded out, because a rounded line can hold bytes the caller does not own: an invalidate would lose their dirty copy, and a clean would write their stale copy over what a device wrote. Align a DMA buffer to the line and size it in lines, link(align: 32) on the STM32H753, and no line it occupies holds anything else.

Every operation ends with the completion barrier, so it has reached the point a device observes when it returns.

The order is the driver's, as D227 states it:

  • clean and invalidate the receive buffer before the stream is armed;
  • invalidate it once a count or completion says the bytes are written and before reading them;
  • clean a transmit buffer before the stream is enabled.

The device barrier before each enable stays the driver's own.

cache.cacheless(line) takes the line a cached provider would have and refuses the same ranges. So a driver tested on a cacheless core, or a hosted one, takes the refusals it would take on a cached profile. A line that is not a nonzero power of two is cache.bad_line.

The Cortex-M7's provider is platform/cortex_m/l1, on a cached profile alone.

Items

maintenance

concept core/cache/cache.ldn:28 ↑ index
public maintenance: type = concept (provider: type)

The bytes in one line, which every range is a whole number of.

    line: (self: provider) -> (bytes: usize)

Write the range's dirty lines to memory, keeping them cached: before a device reads what the CPU wrote, as a transmit does.

    clean: (self: provider, range: []u8) -> none ! misaligned

Discard the range's cached lines, dirty or not, so the next read is memory's: after a device wrote it, as a receive completes.

    invalidate: (self: provider, range: []u8) -> none ! misaligned

Write the range's dirty lines to memory and discard them: before a device writes it, as a receive is armed.

    clean_invalidate: (self: provider, range: []u8) -> none ! misaligned
end maintenance

Keeping memory and a device that reaches it behind the caches in step. Each operation takes a range of whole lines, as lines checks, and returns once it is complete at the point the device observes.

public barriers: type = struct
    line: usize
end barriers

The cacheless provider's handle: memory a device observes as the CPU does, ranges held to lines of line bytes.

public bad_line: atom

A line size that is not a nonzero power of two.

public misaligned: atom

A range that does not begin on a cache line or is not a whole number of lines: nothing was maintained.

cacheless

function core/cache/cache.ldn:87 ↑ index
public cacheless: (line: usize) -> (caches: barriers) ! bad_line

Make the cacheless provider, which refuses ranges that are not whole lines of line bytes. Its operations check the range and issue the completion barrier: all that memory with no cache in front needs.

public clean: (provider: type is maintenance, caches: provider,
               range: []u8) -> none ! misaligned

Clean a range of whole lines, through maintenance evidence.

clean_invalidate

function core/cache/cache.ldn:73 ↑ index
public clean_invalidate: (provider: type is maintenance, caches: provider,
                          range: []u8) -> none ! misaligned

Clean and invalidate a range of whole lines, through maintenance evidence.

invalidate

function core/cache/cache.ldn:66 ↑ index
public invalidate: (provider: type is maintenance, caches: provider,
                    range: []u8) -> none ! misaligned

Invalidate a range of whole lines, through maintenance evidence.

public line: (provider: type is maintenance, caches: provider)
             -> (bytes: usize)

The bytes in one line of the provider's caches.

public lines: (line: usize, range: []u8) -> (count: usize) ! misaligned

Count the lines of line bytes in range, refusing a range that does not begin on a line or end on one. An empty range is no lines.

Example program

This complete program is one of the compiler's runtime tests. View source.

The program, 84 lines
compiler/tests/fixtures/runtime/library-shared-cache/main.ldn84 lines
import core/cache

-- Two lines' room and a line's slack, so a range of whole 32-byte lines
-- can be taken wherever the buffer lies.
mut storage: [96]u8 = zeroed

-- A receive's handoff through any provider: clean and invalidate before
-- the device writes, invalidate once it has, as D227 orders them.
receive: (provider: type is cache.maintenance, caches: provider,
          range: []u8) -> none ! cache.misaligned =
    try cache.clean_invalidate(caches, range)
    try cache.invalidate(caches, range)
end receive

-- Whether the cacheless provider refuses LINE as no power of two.
refuses: (line: usize) -> (refused: bool) =
    refused = false
    caches := cache.cacheless(line) else (problem)
        refused = problem == cache.bad_line
        return
    end
    _ = caches
end refuses

-- The cacheless provider takes only a power-of-two line, keeps every
-- byte through its operations, maintains ranges of whole lines, and
-- refuses a range that begins or ends inside a line, as a cached
-- provider of the same line would.
exercise: () -> (status: i32) =
    status = 2
    return when not refuses(0) or not refuses(24) or refuses(1)
    status = 6
    caches := cache.cacheless(32) else (problem)
        _ = problem
        return
    end
    return when cache.line(caches) <> 32
    status = 7
    slack := (32 - (usize(addr storage[0]) & 31)) & 31
    whole := storage[slack..<slack + 64]
    for index in 0..<lenof whole do storage[slack + index] = u8(index) end for
    receive(caches, whole) else (problem)
        _ = problem
        return
    end
    count := cache.lines(32, whole) else (problem)
        _ = problem
        return
    end
    return when count <> 2
    status = 8
    cache.clean(caches, whole) else (problem)
        _ = problem
        return
    end
    cache.clean(caches, storage[slack..<slack]) else (problem)
        _ = problem
        return
    end
    for index in 0..<lenof whole do
        return when storage[slack + index] <> u8(index)
    end for
    status = 9
    mut refused: u32 = 0
    cache.invalidate(caches, storage[slack + 1..<slack + 33]) else (problem)
        if problem == cache.misaligned then refused += 1 end if
    end
    cache.clean(caches, storage[slack..<slack + 33]) else (problem)
        if problem == cache.misaligned then refused += 1 end if
    end
    cache.clean_invalidate(caches, storage[slack + 16..<slack + 48]) else (problem)
        if problem == cache.misaligned then refused += 1 end if
    end
    receive(caches, storage[slack..<slack + 40]) else (problem)
        if problem == cache.misaligned then refused += 1 end if
    end
    return when refused <> 4
    status = 0
end exercise

public main: () -> (code: i32) =
    code = exercise()
    if code == 0 then code = 42 end if
end main