2010年2月16日 星期二

ARM cache coherency issue (with speculatively prefetch)

Re: USB mass storage and ARM cache coherency
http://lkml.org/lkml/2010/2/16/80

On map, buffers are cleaned if they're being used for DMA_TO_DEVICE and DMA_BIDIRECTIONAL, or invalidated in the case of DMA_FROM_DEVICE.

(On unmap, nothing else is done)

However, because ARM CPUs (ARMv7, or some of that higher than ARM11 MPCore) can now speculatively prefetch, just leaving it at that (do nothing on unmap) results in corruption of buffers used for DMA. So we have to invalidate DMA_FROM_DEVICE and DMA_BIDIRECTIONAL buffers on unmap to ensure coherency with DMA operations.

If the CPU writes to a DMA_FROM_DEVICE buffer between map and unmap, the writes can sit in the cache, and on unmap, they will be discarded.

(Mixing PIO and DMA buffers wasn't a big issue with previous ARM CPUs, whoes unmap didn't do cache operation; while with ARM CPUs with speculatively perfetch, invalidating on unmap would invalidate the valid data that cached by PIO.)

Cleaning the cache on unmap is not an option; that too can lead to DMA buffer corruption in the DMA case.

USB and associated host driver must abide by the DMA API buffer ownership rules otherwise the result will be data corruption; either that or USB/host driver people need to have a discussion with the DMA API authors to remove this sensible "restriction".

沒有留言: