linuxcachingembedded-linuxarm64cortex-a

How to put data in L2 cache with A72 Core?


I have an array of data that looks like this :

uint32_t data[128]; //Could be more than L1D Cache size

In order to do computation on it, I want to put the data as close as possible to my computing unit so in the L2 Cache.

My target runs with a linux kernel and some additionnal apps

I know that I can get an access to a certain area of the memory with mmap and I have succesfully done it in some part of my available memory shared between cores.

How to do the same thing but in L2 Cache area ?

I've read part of gcc documentation and AArch64 assembly instruction set but cannot figure out the way to achieve this.


Solution

  • How to do the same thing but in L2 Cache area ?

    Your hardware doesn't support that.

    In general, the ARMv8 architecture doesn't make any guarantees about the contents of caches and does not provide any means to explicitly manipulate or query them - it only makes guarantees and provides tools for dealing with coherency.

    Specifically, from section D4.4.1 "General behavior of the caches" of the spec:

    [...] the architecture cannot guarantee whether:
    
    • A memory location present in the cache remains in the cache.
    • A memory location not present in the cache is brought into the cache.
    
    Instead, the following principles apply to the behavior of caches:
    
    • The architecture has a concept of an entry locked down in the cache.
      How lockdown is achieved is IMPLEMENTATION DEFINED, and lockdown might
      not be supported by:
    
      — A particular implementation.
      — Some memory attributes.
    
    • An unlocked entry in a cache might not remain in that cache. The
      architecture does not guarantee that an unlocked cache entry remains in
      the cache or remains incoherent with the rest of memory. Software must
      not assume that an unlocked item that remains in the cache remains dirty.
    
    • A locked entry in a cache is guaranteed to remain in that cache. The
      architecture does not guarantee that a locked cache entry remains
      incoherent with the rest of memory, that is, it might not remain dirty.
    
    [...]
    
    • Any memory location is not guaranteed to remain incoherent with the rest of memory.
    

    So basically you want cache lockdown. Consulting the manual of your CPU though:

    • The Cortex-A72 processor does not support TLB or cache lockdown.
    

    So you can't put something in cache on purpose. Now, you might be able to tell whether something has been cached by trying to observe side effects. The two common side effects of caches are latency and coherency. So you could try and time access times or modify the contents of DRAM and check whether you see that change in your cached mapping... but that's still a terrible idea.
    For one, both of these are destructive operations, meaning they will change the property you're measuring, by measuring it. And for another, just because you observe them once does not mean you can rely on that happening.

    Bottom line: you cannot guarantee that something is held in any particular cache by the time you use it.