I encounter some problem which I think is about coherency between DMA and CPU. Here is the simplified use case.
Cortex A5 CPU writes to the non-secure memory under non-secure state. MMU is enabled and the memory attribute is normal, shareable and cacheable. So the data may be in so-called non-secure cache according to the following references:
[1] "Data Cache Tag data format" in DDI0434B_cortex_a5_mpcore_r0p1_trm
[2] Answer from artless noise in Secure mode can access secure / non secure memory how?
[3] 6th page in http://atmel.force.com/support/servlet/fileField?id=0BEG000000002Ur
DMA issues a secure coherent read to that memory area. DMA is connected to ACP.
SCU will only try to check cache tagged as secure. So the data is returned from main memory according to "ACP requests" in DDI0434B_cortex_a5_mpcore_r0p1_trm.
DMA may get stale data.
If my description is correct, here is my second question. Which action can solve this issue?
1) Clean the cache explicitly before DMA reads
2) Change the attribute of the memory to normal, shareable and non-cacheable.
Also the TRM says there is a write buffer that is used to hold data from cache evictions or non-cacheable write bursts before they are written out on the SCU interface in "Bus Interface Unit and SCU interface". However no more information about the write buffer can be found. May I assume the write buffer has some magic to make sure coherency? I would appreciate a lot if someone can explain the magic.
Considering more combinations, I get this table myself.
CPU state | Memory | DMA access | Result
-----------+------------+------------+-----------+
non-secure | non-secure | non-secure | OK
non-secure | non-secure | secure | NG
non-secure | secure | - | NA
secure | non-secure | non-secure | OK
secure | non-secure | secure | NG (Same reason as the second case)
secure | secure | non-secure | NA
secure | secure | secure | OK
The first column shows under which state CPU writes to the memory.
The second column shows the memory is secure or not. Here "secure" means access will be denied by AXI if the AxPROT[1] is high. Memory attribute is normal, shareable and cacheable.
The third column shows what kinds of access DMA issues. The access is coherent.
The fourth column tells what will happen.
NG means DMA may get stale data.
NA means the access is impossible.
OK means DMA is coherent with CPU.
Is is right?
I add this part according to Notlikethat's answer. It may be a nightmare in practice. However I want to know what happens theoretically.
- Suppose that the physical address 0x10 is mapped to two virtual addresses, NS:0x110 and S:0x210.
- CPU under secure state writes value 0x110 to NS:0x110, and then writes value 0x210 to S:0x210.
So both may be in L1 cache at the same time, right? - Then DMA1 makes a secure read to PA 0x10 and may get 0x210. DMA2 makes a non-secure read to PA 0x10 and may get 0x110.
- After that it is unpredictable which one will be in the main memory eventually.
Please confirm or correct my understandings. Thank a lot.