This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Clean Whole Cache on Cortex-A9

I am doing some benchmarking and I need to clear the cache before each test. I have this example here:

Caches and Self-Modifying Code

However, I just want to clean the whole cache. Is there an easy way to do that? I do not need to know the start and end address of the functions.  I just want some simple code

Thanks.

  • The ARMv7-A/R Architecture gives an example sequence for cleaning the entire data and unified cache, using set/way operations.  It's right at the end of chapter B2.2.7 (Cache and branch predictor maintenance operations), under a sub-heading of "Performing cache maintenance operations".  My recommendation would to use that code.

    If you have DS-5 installed, the "startup_Cortex-A9" example in the Bare-metal_examples.zip includes a version of this code.  The difference is the code in the example is doing an invalidate rather than a clean operation.  But that is easy enough to change.

  • I am doing this in user side code.  I thought that this was only possible via the clean_cache function.  So, running your suggested code will work there too?

    Thanks.

  • Ahh, sorry - I had misunderstood your question.  No, you can't directly clean/invalidate the caches from User mode.  If you're are running under an OS, you will have to make a call to the OS to have it do it for you.

    Which OS are you using? 

  • I am using the Linux Kernel 3.7 on a Altera Cyclone V board.

  • > The ARMv7-A/R Architecture gives an example sequence for cleaning the entire data and unified cache, using set/way operations


    Hmm - this isn't guaranteed to work portably. On a single-core system you'll get away with it, but the set/way maintenance operations are not guaranteed to be SMP-safe. The only "architecturally sound" means to clean/clean-invalidate the cache in an SMP system once it is up and running is use the "by VA" operations.


    *EDIT* Also note that by default the data memory allocations returned to an application in Linux is marked "XN" in the MMU (eXecute Never) - the data pages are not executable. IIRC you'll need to make some special memory allocation calls with the kernel to get some executable memory.


    HTH,

    Pete

  • So, essentially, I have to use that clean_cache function as in the link in my OP?  You wouldn't know of any documentation for that function?  Also, I guess I cannot just pass the result of malloc?  As discussed here:

    How clear and invalidate ARM v7 processor cache from User Mode on Linux 2.6.35 - Stack Overflow

    Why is that?

    Thanks.

  • Are you able to say if this is enough to clean the whole cache?  Using the example in the OP

       uint32_t * code = mmap(

      NULL,

      32768,  // 32kb -- Whole Cache.

    PROT_READ | PROT_WRITE | PROT_EXEC,

    MAP_PRIVATE | MAP_ANONYMOUS,

    -1,

      0);

     


     

  • Hi there,

    I know that this is an old post, but I have a related question.

    I should clean and invalidate the L2 cache on Cortex A9. Can I use your function? Instead of 32768 I will use 512000, the size of L2 cache.

    It works?