cache - flush and invalidate operation

kumar picture kumar · Feb 22, 2010 · Viewed 34.3k times · Source

I have some questions on cache synchronization operations.

Invalidate: Before cpu tries to read a portion of memory updated by a device, the corresponding memory needs to be invalidated.

Flush: Before the device read a portion of memory updated by CPU, CPU must flush (write back is also correct?) the contents from cache to memory, so that device reads the contents from memory with updated contents.

If flush is not carried out it may read junk data present in memory as the memory is not still updated with contents written to cache.

Please confirm whether my above understanding is correct?

When do you want to combine both flush and invalidate? I heard that while playing with device control descriptors we need to synchronize by combining flush and invalidate. Why so?

Do we need to follow a sequence like flush followed by invalidate?

Is there a scenario in which invalidate followed by flush will be useful?

Answer

BCran picture BCran · Feb 27, 2010

Flush does write back the contents of cache to main memory, and invalidate does mark cache lines as invalid so that future reads go to main memory.

I think you would combine flush and invalidate if the device was updating a block of memory: the flush would ensure that the device had the latest contents, and the invalidate would then ensure that when the device had finished that the CPU would read the new contents from memory.