title: DMA cache coherence management
body: <p>My question is this: how can I determine when it is safe to disable cache snooping when I am correctly using <code>[pci_]dma_sync_single_for_{cpu,device}</code> in my device driver?</p>
  
  <p>I'm working on a device driver for a device which writes directly to RAM over PCI Express (DMA), and am concerned about managing cache coherence.  There is a control bit I can set when initiating DMA to enable or disable cache snooping during DMA, clearly for performance I would like to leave cache snooping disabled if at all possible.</p>
  
  <p>In the interrupt routine I call <code>pci_dma_sync_single_for_cpu()</code> and <code>..._for_device()</code> as appropriate, when switching DMA buffers, but on 32-bit Linux 2.6.18 (RHEL 5) it turns out that these commands are macros which expand to nothing ... which explains why my device returns garbage when cache snooping is disabled on this kernel!</p>
  
  <p>I've trawled through the history of the kernel sources, and it seems that up until 2.6.25 only 64-bit x86 had hooks for DMA synchronisation.  From 2.6.26 there seems to be a generic unified indirection mechanism for DMA synchronisation (currently in <code>include/asm-generic/dma-mapping-common.h</code>) via fields <code>sync_single_for_{cpu,device}</code> of <code>dma_map_ops</code>, but so far I've failed to find any definitions of these operations.</p>