-1

I have to flush 4MB cache memory in ARM assembly language, what would we the efficient way to do it?

I thought of allocating 4MB memory,writing some random data and reading back

I'm implementing a tool to test Main memory. To make sure my tool tests Main memory only, i have to flush the cache memory

I'm working on Cortex A8 processor, i want to flush all L1,L2,L3 caches

artless noise
  • 21,212
  • 6
  • 68
  • 105
Nikhilendra
  • 51
  • 3
  • 11
  • I want the method to do it. What i thought was allocating 4MB memory writing some random data and reading back – Nikhilendra Jun 30 '14 at 14:10
  • I have no idea what you're trying to achieve. Are you implementing a cache in your program or are you talking about the CPU cache? What memory are you 'allocating'? – tangrs Jun 30 '14 at 14:14
  • Which cache? Which CPU model? Which mode is your code going to run in? Have you look at [the available](http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0360f/I1014942.html) CP15 [cache operations](http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0201d/ch03s03s05.html)? – Michael Jun 30 '14 at 14:17
  • @tangrs I'm implementing a tool to test Main memory. To make sure my tool tests Main memory only, i have to flush the cache memory. – Nikhilendra Jun 30 '14 at 14:18
  • @Michael I'm working on Cortex A8 processor, i want to flush all L1,L2,L3 caches – Nikhilendra Jun 30 '14 at 14:21
  • 1
    Yes if you can get at the physical address space and you can cover, linearly, starting at an aligned address based on the size of the largest cache, and write all locations, it will push anything in the cache out. Problem of course you are running code to do this, also if you are on an operating system it is likely not to work, you should never be doing a memory test while something is running on it anyway and in that case simply turn off the cache and do the memory test. assembly has absolutely nothing to do with this task, C or asm the answers are all the same – old_timer Jun 30 '14 at 15:29
  • Allocating a big bunch, even bigger than 4MB, of memory writing some random data and reading it back won't give you the guarantee that all the content of your cache will be written back to memory. You need to use the cache maintenance operations. – lucvoo Apr 11 '16 at 23:57

1 Answers1

3

Taking the question literally, the most efficient method of flushing the CPU caches is to use the cache maintenance operations to clean+invalidate by set/way - see v7_flush_dcache_all for an example, but note that for maximum performance the order of the loops can make a noticable difference, particularly if the cached addresses are non-random.

For external caches (i.e. L3) you're on your own and will need to consult the TRM for whatever implementation you have.

However, since you're writing a memory tester rather than an OS, the far more sensible approach is to just leave the MMU and data caches turned off in the first place, and avoid the issue altogether.

Notlikethat
  • 20,095
  • 3
  • 40
  • 77