DMA_ZONE、dma_mask、coherent_dma_mask和dma bounce相关问题的澄清

ARM 165浏览

转自http://21cnbao.blog.51cto.com/109393/521160针对arm平台而讲,但对各平台都有参考意义。

 

本文针对ARM体系结构,参考的内核版本是2.6.32。

1.       为什么存在DMA ZONE?

原因是某些硬件的DMA引擎不能访问到所有的内存区域,因此,加上一个DMA ZONE,当使用GFP_DMA方式申请内存时,获得的内存限制在DMA_ZONE的范围内,这些特定的硬件需要使用GFP_DMA方式获得可以做DMA的内存;

如果系统中所有的设备都可选址所有的内存,那么DMA ZONE覆盖所有内存。

2.       device结构体的dma_mask和coherent_dma_mask的作用分别是什么?

dma_mask是设备DMA能访问的内存范围,coherent_dma_mask则作用于申请一致性DMA缓冲区。

3.       dma_alloc_coherent分配的内存一定在DMA ZONE内吗?

答案是否定的,应该说绝对多数情况下都不在DMA ZONE内。dma_alloc_coherent会调用__dma_alloc,__dma_alloc判断是否从DMA ZONE申请内存的依据是:

        u64 mask = get_coherent_dma_mask(dev);

        …

        if (mask < 0xffffffffULL)

                gfp |= GFP_DMA;

 

        page = alloc_pages(gfp, order);

        if (!page)

                goto no_page;

由此可见,只有当设备的dma_coherent_mask小于全部地址访问(0xFFFFFFFF)的时候,也就是针对少数特殊设备,才会去DMA ZONE申请内存,其他时候直接从NORMAL ZONE获得内存。而对于绝大多数设备而言,DMA寻址范围是0xFFFFFFFF。

4.       什么时候需要DMA bounce?

最常见的case是,当底层驱动做DMA操作时所使用的内存不是透过dma_alloc_coherent等一致性DMA缓冲区API获得,而是来自于驱动的上层,譬如文件系统,那么驱动在执行DMA操作时,会调用流式DMA API,如map_single等。而如果上层传下来的内存超过了设备DMA可寻址的内存范围,即:

needs_bounce = (dma_addr | (dma_addr + size - 1)) & ~mask;

得到满足的时候,这片区域不能执行DMA,map_single底层会自动执行DMA。

从以上分析可知,目前部分ARM新片 BSP关于此部分的实现不能体现内核关于dma_mask和coherent_dma_mask的设计思想,实现了冗余的dma_needs_bounce()函数。因此也见到一些改进,譬如Eric Miao的如下patch:

 

[PATCH 1/4] [ARM] Remove now unnecessary dma_needs_bounce()
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

--------------------------------------------------------------------------------

Subject: [PATCH 1/4] [ARM] Remove now unnecessary dma_needs_bounce()
From: Eric Miao <eric.y.miao@xxxxxxxxx>
Date: Sat, 5 Jun 2010 15:55:35 +0800
Cc: Eric Miao <eric.y.miao@xxxxxxxxx>
In-reply-to: <1275724538-5075-1-git-send-email-eric.y.miao@xxxxxxxxx>

--------------------------------------------------------------------------------
With a correct dev->dma_mask before calling dmabounce_register_dev(),
dma_needs_bounce() is not necessary.

The above is true for it8152.c and ixp4xx/common-pci.c.

The sa1111, though, is a bit complicated. Until it's fully understood
and fixed, dma_needs_bounce() for sa1111 is kept if CONFIG_SA1111 is
enabled with no side effect (with the condition of machine_is_*)

Signed-off-by: Eric Miao <eric.y.miao@xxxxxxxxx>
---
 arch/arm/common/it8152.c           |    8 --------
 arch/arm/include/asm/dma-mapping.h |    7 +++++++
 arch/arm/mach-ixp4xx/common-pci.c  |    5 -----
 3 files changed, 7 insertions(+), 13 deletions(-)

diff --git a/arch/arm/common/it8152.c b/arch/arm/common/it8152.c
index 7974baa..6c09135 100644
--- a/arch/arm/common/it8152.c
+++ b/arch/arm/common/it8152.c
@@ -263,14 +263,6 @@ static int it8152_pci_platform_notify_remove(struct device *dev)
  return 0;
 }
 
-int dma_needs_bounce(struct device *dev, dma_addr_t dma_addr, size_t size)
-{
- dev_dbg(dev, "%s: dma_addr %08x, size %08xn",
-  __func__, dma_addr, size);
- return (dev->bus == &pci_bus_type) &&
-  ((dma_addr + size - PHYS_OFFSET) >= SZ_64M);
-}
-
 int __init it8152_pci_setup(int nr, struct pci_sys_data *sys)
 {
  it8152_io.start = IT8152_IO_BASE + 0x12000;
diff --git a/arch/arm/include/asm/dma-mapping.h b/arch/arm/include/asm/dma-mapping.h
index 69ce072..c80d4d2 100644
--- a/arch/arm/include/asm/dma-mapping.h
+++ b/arch/arm/include/asm/dma-mapping.h
@@ -298,7 +298,14 @@ extern void dmabounce_unregister_dev(struct device *);
  * DMA access and 1 if the buffer needs to be bounced.
  *
  */
+#ifdef CONFIG_SA1111
 extern int dma_needs_bounce(struct device*, dma_addr_t, size_t);
+#else
+static inline int dma_needs_bounce(struct device*, dma_addr_t, size_t)
+{
+ return 0;
+}
+#endif
 
 /*
  * The DMA API, implemented by dmabounce.c.  See below for descriptions.
diff --git a/arch/arm/mach-ixp4xx/common-pci.c b/arch/arm/mach-ixp4xx/common-pci.c
index e318153..c7aa0ac 100644
--- a/arch/arm/mach-ixp4xx/common-pci.c
+++ b/arch/arm/mach-ixp4xx/common-pci.c
@@ -337,11 +337,6 @@ static int ixp4xx_pci_platform_notify_remove(struct device *dev)
  return 0;
 }
 
-int dma_needs_bounce(struct device *dev, dma_addr_t dma_addr, size_t size)
-{
- return (dev->bus == &pci_bus_type ) && ((dma_addr + size) >= SZ_64M);
-}
-
 /*
  * Only first 64MB of memory can be accessed via PCI.
  * We use GFP_DMA to allocate safe buffers to do map/unmap.
--
1.7.0.4