Linux下Cache的使用

来源:互联网 发布:租房软件那个好 编辑:程序博客网 时间:2024/06/16 20:44

1.首先我们看一下ARM中Cache的几种设置:

 

 

 

00:表示Cache和Write Buffer都被禁止

01:表示nonCache,Write Buffer使能

10:Write-Through模式

11:Write-Back模式

如果是Write-Through 模式,每次写操作都通过Cache+Write Buffer把数据直接写到主存中去;如果是Write-back模式,数据最初只是写到Cache上,必要的时候再将CACHE上的数据通过Write Buffer实际回写到主存中去。

 

2.Linux下如何设置内存的Cache模式

/*
 * Mark the prot value as uncacheable and unbufferable.
 */
#define pgprot_noncached(prot) /

/* 模式0,nonCahed & nonWriteBuffer */
 __pgprot_modify(prot, L_PTE_MT_MASK, L_PTE_MT_UNCACHED)
#define pgprot_writecombine(prot) /
 __pgprot_modify(prot, L_PTE_MT_MASK, L_PTE_MT_BUFFERABLE)

/* 模式1,NonCache,WriteBuffer被使能 */
#ifdef CONFIG_ARM_DMA_MEM_BUFFERABLE
#define pgprot_dmacoherent(prot) /
 __pgprot_modify(prot, L_PTE_MT_MASK|L_PTE_EXEC, L_PTE_MT_BUFFERABLE)
#else
#define pgprot_dmacoherent(prot) /
 __pgprot_modify(prot, L_PTE_MT_MASK|L_PTE_EXEC, L_PTE_MT_UNCACHED)

#endif

3.Linux下如何处理DMA与Cache的问题

当一块内存同时开始DMA和Cache时,会出现内存一致性问题:

1).在DMA操作前,假如CPU对内存进行了操作但是结果只是保持在Cache中,没有被更新到内存,DMA操作的内存数据就会是错误的。

2).DMA操作后,内存数据已经更新,假如Cache中仍然保持的旧数据,CPU操作会出错。

对于情况(1)调用Cache的Flush操作:

dma_sync_single_for_device(port->dev,
        pdc->dma_addr,
        pdc->dma_size,
        DMA_TO_DEVICE);

对于情况(2)调用Cache的Invalidate操作:

dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
     pdc->dma_size, DMA_FROM_DEVICE);

2010/09/09