Files
rt-thread/documentation/6.components/device-driver/dma/pool.md
T
GuEe-GUI cfda3b3d1a
doc_doxygen / doxygen_doc generate (push) Has been cancelled
doc_doxygen / deploy (push) Has been cancelled
[docs] device-driver: add DM subsystem docs and expand INDEX
Add Doxygen pages for RT-Thread device-model drivers: platform/OFW,
DM core (bus, dm, power), clk/regulator/reset/power-domain, PCI/PIC,
Phye, DMA/hwcache, block/SCSI/ATA/NVMe/UFS/SDIO, SCMI, RPMsg,
mailbox, NVMem, NUMA, syscon, thermal, input/LED/IIO/graphic, and
related power/charger/supply docs.
Expand device-driver INDEX.md with ~90 @subpage entries so all new
pages appear in the driver chapter navigation.
Update existing UART/SPI/RTC/pin/framework/dtc pages: UART docs moved
from serial/ to uart/ (uart + uart_dm + uart_earlycon); SPI and RTC
gain DM companion pages; pin links to pin_dm; framework points at DM
topics.
Focus on registration/probe flow and in-tree APIs; no standalone
VirtIO subsystem page (transport code still incomplete).
Test plan: build Doxygen for documentation/ and verify device-driver
INDEX links resolve without missing @ref/@subpage warnings.

Signed-off-by: GuEe-GUI <2991707448@qq.com>
2026-05-28 16:09:55 +08:00

4.0 KiB
Executable File

@page page_device_dma_pool DMA memory pools

DMA coherent / CMA pools

Header: struct rt_dma_pool and rt_dma_pool_* in components/drivers/include/drivers/dma.h. Implementation: components/drivers/dma/dma_pool.c.

rt_dma_alloc / rt_dma_free (and default rt_dma_map_ops) allocate from installed pools instead of the generic heap when RT_USING_DMA and memblock reservations are configured. Controllers and slaves still use documentation/6.components/device-driver/dma/dma.md for channel programming; pools only back buffer memory.

Driver / BSP quick notes

Topic Guidance
Boot reservation Call rt_dma_pool_extract early (memblock dma-pool region) before drivers rt_dma_alloc.
Coherent vs CMA coherent_pool is carved from the low end of the reserved region; CMA uses the remainder—cma_size must be ≥ coherent_pool_size.
32-bit DMA Pools below 4 GiB set RT_DMA_F_32BITS on the pool—devices with limited addressing need buffers from these pools.
dma-coherent DT rt_dma_device_is_coherent(dev) reads the property; map ops may skip flush on coherent devices.

struct rt_dma_pool

Field Meaning
region Physical start / end, optional name.
map / bits Page bitmap (ARCH_PAGE_SIZE granule).
flags e.g. RT_DMA_F_32BITS, RT_DMA_F_NOCACHE.
list Global pool list (internal).

Public API

API Role
rt_dma_pool_install(region) Register a physical memory region as a pool; returns struct rt_dma_pool * or NULL on error.
rt_dma_pool_extract(cma_size, coherent_pool_size) Find memblock region named dma-pool, split into coherent-pool + cma sub-regions, install both pools, mark memblock as consumed. Returns -RT_EEMPTY if no suitable region, -RT_EINVAL if sizes are inconsistent.

rt_dma_pool_install is also used directly when the BSP hands a fixed rt_region_t without going through memblock extract.

How allocation uses pools

  1. rt_dma_alloc(dev, size, &handle, flags) walks installed pools (under dma_pools_lock), picks a pool matching flags (coherent, 32-bit, device, etc.).
  2. Default rt_dma_map_ops:
    • Coherent: flush on sync_out_data, invalidate on sync_in_data.
    • Non-coherent: physical address via rt_kmem_v2p without cache maintenance on sync-in.
  3. With RT_USING_OFW, rt_dma_device_set_ops may install ofw_dma_map_* that translates CPU ↔ DMA bus addresses via rt_ofw_translate_cpu2dma.

Slaves should use rt_dma_alloc_coherent for ring buffers the CPU and DMA both touch.

Boot flow (typical)

  1. Bootloader or memblock code reserves dma-pool in device tree / memblock.
  2. Early init calls rt_dma_pool_extract(SIZE_MB(8), SIZE_MB(2)) (example sizes).
  3. Drivers probe and rt_dma_alloc for descriptor rings and payload buffers.

Debugging

With RT_USING_CONSOLE and RT_USING_MSH, list_dma_pool prints installed regions (name, size, base).

Pitfalls

  • Pool exhausted: rt_dma_alloc returns NULL—size requests must fit contiguous free pages in a matching pool.
  • CMA smaller than coherent: rt_dma_pool_extract rejects cma_size < coherent_pool_size.
  • High memory only: if no sub-4G dma-pool chunk is large enough, extract may fall back with a warning—DMA to peripherals that cannot reach high memory will fail.
  • Double extract: second call may find memblock already consumed—guard in BSP init.

See also

  • DMA engine and channels: documentation/6.components/device-driver/dma/dma.md
  • OFW DMA range: documentation/6.components/device-driver/ofw/ofw.md
  • NUMA / affinity: documentation/6.components/device-driver/numa/numa.md
  • Source: components/drivers/dma/dma_pool.c
  • Header: components/drivers/include/drivers/dma.h