본문으로 건너뛰기
Embedded Performance Engineering · 21/57

DMA 성능 최적화 — Burst·Scatter-Gather·Chain·Cache 일관성

· Hawk · 7분 읽기

#한 줄 요약

“DMA는 CPU를 우회한 데이터 이동입니다.” burst size와 alignment, cache가 핵심을 결정합니다.

이 글의 STM32 코드는 STM32CubeH7 HAL 드라이버 소스(STMicroelectronics/stm32h7xx-hal-driver, 커밋 65e5c7e)에서 함수·필드 이름을 확인했고, 보드에서 실행하지는 않았습니다. 이 시리즈의 Debian 게스트에는 DMA 컨트롤러가 없어(/sys/class/dma가 비어 있고 dmatest 모듈도 없음) Linux dmaengine도 실행하지 못했습니다.

#DMA Throughput 결정 요소

요소영향
Burst 길이transaction 하나에 담는 beat 수. AXI에서는 Length = AxLEN + 1 (3-01)
Beat 크기한 번에 옮기는 byte. 버스 폭을 넘을 수 없음(AXI A3.1.1)
정렬transaction은 4 KB 경계를 넘을 수 없어(AXI A3.1.2) 경계 근처에서는 나뉨
OutstandingDMA controller가 동시에 열어 둘 수 있는 transaction 수
Bus contention같은 Subordinate를 쓰는 다른 master의 영향(3-02)

#Burst 설정 — STM32H7 DMA

STM32H7 HAL의 DMA stream 설정에서 burst는 Init.MemBurst/Init.PeriphBurst, 한 beat의 크기는 Init.MemDataAlignment/Init.PeriphDataAlignment입니다(Inc/stm32h7xx_hal_dma.h, 예: DMA_MBURST_INC4, DMA_PDATAALIGN_WORD).

/* Instance, Request, Direction, Inc 같은 나머지 필드는 대상 칩과 요청에 맞춰 채운다 */
hdma.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD; /* 4 byte beat */
hdma.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
hdma.Init.FIFOMode = DMA_FIFOMODE_ENABLE; /* burst는 FIFO 모드에서만 쓰임 */
hdma.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma.Init.MemBurst = DMA_MBURST_INC4; /* 4 beat burst */
if (HAL_DMA_Init(&hdma) != HAL_OK) {
Error_Handler();
}

STM32H7 HAL의 HAL_DMA_Init()은 FIFO가 꺼져 있으면 burst 설정을 레지스터에 넣지 않습니다(stm32h7xx_hal_dma.c 292~296행 “the Memory burst and peripheral burst are not used when the FIFO is disabled”). beat 4개 × 4 byte = 16 byte가 burst 하나입니다. burst를 키우면 transaction마다 붙는 요청·응답 단계가 더 많은 데이터에 나뉘어 붙습니다. 실제 배수는 버스 구조와 상대 장치에 달려 있고, 이 글에서는 재지 않았습니다.

#STM32H7 MDMA - Master DMA

MDMA_HandleTypeDef hmdma;
hmdma.Init.Request = MDMA_REQUEST_SW;
hmdma.Init.TransferTriggerMode = MDMA_BUFFER_TRANSFER;
hmdma.Init.Priority = MDMA_PRIORITY_HIGH;
hmdma.Init.Endianness = MDMA_LITTLE_ENDIANNESS_PRESERVE;
hmdma.Init.SourceInc = MDMA_SRC_INC_WORD;
hmdma.Init.DestinationInc = MDMA_DEST_INC_WORD;
hmdma.Init.SourceDataSize = MDMA_SRC_DATASIZE_WORD;
hmdma.Init.DestDataSize = MDMA_DEST_DATASIZE_WORD;
hmdma.Init.DataAlignment = MDMA_DATAALIGN_PACKENABLE;
hmdma.Init.BufferTransferLength = 128; /* buffer transfer 하나의 byte 수 */
/* Instance, SourceBurst·DestBurst 등 나머지 필드와 HAL_MDMA_Init(&hmdma) 생략 */
HAL_MDMA_Start(&hmdma, (uint32_t)src, (uint32_t)dst, len, 1);

HAL 헤더 stm32h7xx_hal_mdma.h 8081행은 BufferTransferLength를 “한 번의 transfer로 옮길 byte 수(1128 byte)“로 설명합니다. burst 길이가 아니고, burst는 별도 필드 SourceBurst·DestBurst가 정합니다. HAL_MDMA_Start()의 마지막 두 인자는 BlockDataLength와 BlockCount입니다(673행).

#Scatter-Gather

흩어진 조각을 하나의 연속 영역으로 모으는 전송입니다. 조각마다 CPU가 다시 설정하는 대신, 조각 정보를 descriptor로 메모리에 두고 DMA controller가 다음 descriptor를 스스로 읽어 이어 갑니다.

typedef struct dma_desc {
uint32_t src;
uint32_t dst;
uint32_t len;
struct dma_desc *next; /* NULL = 종료 */
} dma_desc_t;
dma_desc_t descriptors[4] = {
{ .src = 0x1000, .dst = BASE + 0, .len = 256, .next = &descriptors[1] },
{ .src = 0x4500, .dst = BASE + 256, .len = 512, .next = &descriptors[2] },
{ .src = 0x9100, .dst = BASE + 768, .len = 128, .next = &descriptors[3] },
{ .src = 0xC200, .dst = BASE + 896, .len = 256, .next = NULL },
};

이 구조체는 개념을 보이는 예이고, 실제 descriptor 형식은 controller마다 reference manual이 정합니다. STM32H7 MDMA는 HAL에서 HAL_MDMA_LinkedList_CreateNode()와 HAL_MDMA_LinkedList_AddNode()로 linked list를 만듭니다(stm32h7xx_hal_mdma.h). descriptor 자체도 DMA가 메모리에서 읽어 오므로, Cortex-M7에서 descriptor를 cacheable 영역에 두었다면 다 쓴 뒤 clean해야 DMA가 최신 내용을 봅니다(아래 Cache 일관성).

#Double Buffer

/* STM32H7 DMA — Double Buffer mode (interrupt) */
hdma.XferCpltCallback = rx_half0_done; /* memory 0 완료 */
hdma.XferM1CpltCallback = rx_half1_done; /* memory 1 완료 */
HAL_DMAEx_MultiBufferStart_IT(&hdma, src, dst_a, dst_b, len);

HAL_DMA_M0CpltCallback()·HAL_DMA_M1CpltCallback() 같은 weak 함수는 STM32H7 HAL에 없습니다. DMA_HandleTypeDef의 함수 포인터 XferCpltCallback(148행)과 XferM1CpltCallback(152행)을 채우고, 인터럽트 모드는 HAL_DMAEx_MultiBufferStart_IT()로 시작합니다(stm32h7xx_hal_dma_ex.h 237~238행). 같은 드라이버의 HAL_DMAEx_MultiBufferStart()는 첫머리에서 “Memory-to-memory transfer not supported in double buffering mode”라며 memory-to-memory 방향을 거부합니다(stm32h7xx_hal_dma_ex.c 129행). double buffer는 주변장치 ↔ 메모리 스트림용입니다.

이득은 단순한 식으로 볼 수 있습니다. buffer 하나를 DMA가 채우는 시간을 T_dma, CPU가 처리하는 시간을 T_cpu라 하면, buffer 하나로는 둘이 번갈아 해야 하므로 buffer당 T_dma + T_cpu가 걸리고, 둘로 겹치면 max(T_dma, T_cpu)가 걸립니다. 둘이 같을 때 최대 2배이고, 한쪽이 훨씬 길면 이득이 거의 없습니다.

#Cache 일관성 - Cortex-M7

#CPU가 buffer write → DMA가 send

fill_data(tx_buf, 256); /* CPU가 씀: write-back이면 cache에만 있을 수 있음 */
SCB_CleanDCache_by_Addr(tx_buf, 256); /* memory로 내보냄 */
HAL_UART_Transmit_DMA(&huart1, tx_buf, 256);

#DMA가 buffer fill → CPU가 read

HAL_UART_Receive_DMA(&huart1, rx_buf, 256);
/* DMA complete */
SCB_InvalidateDCache_by_Addr(rx_buf, 256); /* cache의 옛 line을 버림 */
process(rx_buf);

#Misalignment 함정

CMSIS cachel1_armv7.h의 주석에 따르면 SCB_InvalidateDCache_by_Addr()는 32 byte로 정렬된 주소부터 32 byte 단위로 동작합니다. Cortex-M7의 line도 32 byte입니다(2-05).

SCB_InvalidateDCache_by_Addr(buf, 100); /* buf가 32 byte 정렬이 아니면 */

buf가 정렬되지 않았거나 길이가 32의 배수가 아니면, 처음과 마지막 line에 함께 들어 있는 다른 변수까지 버려집니다. CPU가 그 변수에 쓴 값이 아직 memory에 없었다면 그 값이 사라집니다.

__attribute__((aligned(32))) uint8_t buf[128]; /* line 정렬, 길이 32의 배수 */
SCB_InvalidateDCache_by_Addr(buf, 128); /* 정확히 4 line */

#Non-Cacheable Memory 영역

/* MPU로 non-cacheable region 설정 (STM32H7 HAL) */
MPU_Region_InitTypeDef region = {0};
region.Enable = MPU_REGION_ENABLE;
region.Number = MPU_REGION_NUMBER0;
region.BaseAddress = (uint32_t)&__dma_region_start; /* 링커가 정한 시작 주소 */
region.Size = MPU_REGION_SIZE_512KB;
region.AccessPermission = MPU_REGION_FULL_ACCESS;
region.TypeExtField = MPU_TEX_LEVEL1; /* TEX=001, C=0, B=0: Normal non-cacheable */
region.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
region.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
HAL_MPU_ConfigRegion(&region);

MPU_Region_InitTypeDef의 첫 두 필드는 Enable과 Number입니다(stm32h7xx_hal_cortex.h 47~52행). {0}으로 초기화한 채 두면 region이 꺼진 상태로 남습니다. 또 ARMv7-M ARM 표 B3-13에 따르면 TEX·C·B가 모두 0이면 Strongly-ordered이고, Normal non-cacheable은 TEX = 001, C = 0, B = 0이므로 TypeExtField를 MPU_TEX_LEVEL1(stm32h7xx_hal_cortex.h 174행)로 둡니다. ARMv7-M ARM B3.5.3에 따르면 region 크기는 2^N(5 ≤ N ≤ 32) byte이고 시작 주소는 크기에 맞춰 정렬되어야 하며(RegionBaseAddress[(N−1)

] = 0), region이 겹치면 번호가 큰 region이 이깁니다. 이 영역의 buffer는 cache maintenance가 필요 없는 대신 CPU가 읽고 쓸 때도 cache를 쓰지 못합니다.

#DMA Latency

DMA 시작까지의 비용(설정 레지스터 쓰기, 요청 처리)과 완료 통지 비용(인터럽트 진입 등)은 controller와 드라이버에 따라 다르고, 이 글에서는 재지 않았습니다. Cortex-M4의 인터럽트 진입만 해도 wait state 없는 메모리에서 최대 12 cycle입니다(M4 TRM 3.9.1, 3-05). 작은 전송일수록 이 고정 비용의 비중이 커집니다. 손익분기점은 3-04에서 다룹니다.

#Linux dmaengine API

dma_cap_mask_t mask;
dma_cap_zero(mask);
dma_cap_set(DMA_MEMCPY, mask);
struct dma_chan *chan = dma_request_chan_by_mask(&mask); /* memcpy를 지원하는 channel */
if (IS_ERR(chan))
return PTR_ERR(chan);
struct dma_async_tx_descriptor *tx;
tx = dmaengine_prep_dma_memcpy(chan, dst, src, len, DMA_PREP_INTERRUPT);
if (!tx)
return -EIO; /* channel이 memcpy를 지원하지 않으면 NULL */
tx->callback = dma_done_cb;
dma_cookie_t cookie = dmaengine_submit(tx);
dma_async_issue_pending(chan);
dma_sync_wait(chan, cookie);

함수 이름은 Linux 6.12 include/linux/dmaengine.h에 있습니다(dmaengine_prep_dma_memcpy 1079행, dmaengine_submit 1244행, dma_async_issue_pending 1450행, dma_sync_wait 1515행, dma_request_chan 1522행). 주변장치 드라이버가 쓰는 slave DMA 절차는 Documentation/driver-api/dmaengine/client.rst에 단계별로 있습니다.

#전송 방식 비교

방식CPU가 하는 일완료를 아는 단위
Polling데이터마다 상태 레지스터를 읽고 옮김데이터 하나
IRQ per byte데이터마다 인터럽트 진입·복귀데이터 하나
DMA + IRQ설정 한 번, 완료 인터럽트 한 번buffer 하나
DMA chain / double bufferbuffer가 바뀔 때만 처리buffer 하나

#자주 하는 실수

⚠️ DMA buffer를 stack에

void send_data(void) {
uint8_t buf[256]; /* stack */
fill(buf);
HAL_UART_Transmit_DMA(&huart, buf, 256);
return; /* DMA가 아직 읽는 중인데 buf의 수명이 끝남 */
}

함수가 돌아오면 그 stack 영역은 다음 호출이 덮어씁니다. DMA buffer는 전송이 끝날 때까지 살아 있는 static·전역 영역에 둡니다.

⚠️ Cache maintenance 누락

CPU가 쓴 뒤 clean하지 않으면 DMA가 memory의 옛 값을 보내고, DMA가 쓴 뒤 invalidate하지 않으면 CPU가 cache의 옛 값을 읽습니다.

⚠️ MPU region 정렬

B3.5.3의 규칙대로 region의 시작 주소는 크기의 배수여야 합니다. 512 KB region을 정렬되지 않은 주소에 두면 의도한 영역과 다른 범위가 덮이므로, 링커 스크립트에서 DMA 영역을 크기에 맞춰 정렬합니다.

#정리

  • AXI burst는 AxLEN + 1 beat이고 4 KB 경계를 넘을 수 없습니다.
  • STM32H7 MDMA의 BufferTransferLength는 buffer transfer 하나의 byte 수(1~128)이지 burst 길이가 아닙니다.
  • STM32H7 double buffer는 XferCpltCallback·XferM1CpltCallback과 HAL_DMAEx_MultiBufferStart_IT()로 쓰고, memory-to-memory는 지원하지 않습니다.
  • double buffer의 이득은 T_dma + T_cpu에서 max(T_dma, T_cpu)로 줄어드는 만큼이고, 최대 2배입니다.
  • Cortex-M7 DMA buffer는 32 byte 정렬과 clean·invalidate가 필요하고, MPU non-cacheable region은 Enable·Number를 채우고 크기에 맞춰 정렬해야 합니다.

다음 편은 DMA vs CPU 손익 분석을 다룹니다.

#관련 항목

#참고 자료

  • STM32CubeH7 HAL driver (stm32h7xx_hal_dma.h, _dma_ex.h/.c, _mdma.h, _cortex.h)
  • ARMv7-M Architecture Reference Manual (DDI 0403E) B3.5.3
  • AMBA AXI Protocol Specification (ARM IHI 0022, Issue L) A3.1.1, A3.1.2
  • CMSIS_5 cachel1_armv7.h
  • Linux v6.12 include/linux/dmaengine.h, Documentation/driver-api/dmaengine/client.rst
Embedded Performance Engineering · 22 of 57 전체 목차
  1. 1 Embedded Performance Engineering — 임베디드 성능 엔지니어링 시리즈 소개
  2. 2 임베디드 성능 분석 방법론 — Measure → Analyze → Optimize 사이클
  3. 3 성능 지표 정의 — Latency·Throughput·Utilization 분석
  4. 4 성능 측정의 기본 — Wall-Clock·CPU Cycle·Instruction Count
  5. 5 성능 데이터 통계적 분석 — Percentile·Histogram·평균의 함정
  6. 6 실시간 성능 분석 — WCET·Jitter·Deadline Miss 측정
  7. 7 임베디드 벤치마킹 기초 — 재현성·Warmup·노이즈 제거
  8. 8 성능 모델링 — Amdahl·Gustafson·Roofline Model 적용
  9. 9 프로파일링 기법 개요 — Sampling vs Instrumentation·PGO·LTO
  10. 10 CPU 파이프라인 분석 — 5-stage·Cortex-M·Cortex-A 비교
  11. 11 Pipeline Stall 분석 — Data·Structural·Control Hazard·Forwarding
  12. 12 Branch Prediction 분석 — Static·2-bit·BTB·BHT·Mispredict 비용
  13. 13 Speculative Execution 분석 — OoO·Reorder Buffer·Register Renaming
  14. 14 CPU Cache 기초 — L1·L2·L3·Set Associative·Replacement Policy
  15. 15 Cache Miss 3C Model 분석 — Compulsory·Capacity·Conflict
  16. 16 Cache Line 최적화 — Alignment·Prefetch·False Sharing 처리
  17. 17 메모리 대역폭 분석 — STREAM·Roofline·Bus Saturation 측정
  18. 18 SIMD·NEON 활용 — 128-bit Vector·Auto-Vectorization·SVE/SVE2
  19. 19 PMU·HPM 하드웨어 카운터 분석 — 정밀 성능 진단
  20. 20 임베디드 Bus Architecture — AHB·AXI·CHI 진화와 5-Channel
  21. 21 Bus Contention 진단 — Arbitration·QoS·Starvation 측정
  22. 22 DMA 성능 최적화 — Burst·Scatter-Gather·Chain·Cache 일관성
  23. 23 DMA vs CPU Copy 성능 비교 — Break-even·Setup Overhead 측정법
  24. 24 Interrupt Latency 분석 — 진입·종료·Tail-Chaining·Late Arrival
  25. 25 Interrupt Storm 처리 — NAPI·Rate-Limit·Polling 전환
  26. 26 MMIO 접근 성능 — Cache Policy·Write-Combining·Volatile·Barrier
  27. 27 Peripheral Clock 분석 — PLL·Divider·Gating·DVFS
  28. 28 Power vs Performance 트레이드오프 — DVFS·Race-to-Idle·Big.LITTLE
  29. 29 Thermal Throttling 분석 — Junction Temp·Trip Point·냉각
  30. 30 CXL Interconnect 분석 — AI 시대 메모리 대역폭 확장
  31. 31 Concurrency 기초 — Concurrency vs Parallelism·Race·Memory Model
  32. 32 False Sharing 진단 — Cache Line Ping-Pong·Padding·측정
  33. 33 Lock Contention 분석 — Wait·Hold·Convoy·측정 기법
  34. 34 Spinlock 성능 분석 — Spin-Wait vs Context Switch·Ticket·MCS
  35. 35 Mutex 성능 분석 — Futex·Adaptive·Priority Inheritance
  36. 36 Reader-Writer Lock 성능 — Reader/Writer Priority·RCU·Seqlock
  37. 37 Lock-Free 자료구조 성능 — CAS·ABA·Hazard Pointer·Epoch Reclamation
  38. 38 Memory Ordering 분석 — Acquire·Release·Seq-Cst·ARM Relaxed Model
  39. 39 Cache Coherency 프로토콜 — MESI·MOESI·Snoop·Directory
  40. 40 SMP 성능 분석 — Per-Core·Affinity·Load Balance·Scalability
  41. 41 Linux perf 기초 — stat·record·report 활용
  42. 42 Linux perf 고급 — Raw Event·Tracepoint·perf script
  43. 43 ftrace 활용 — function·function_graph·latency tracer
  44. 44 eBPF·bpftrace 동적 트레이싱 — 커널 무수정 관측
  45. 45 Flamegraph 분석 — On-CPU·Off-CPU·Differential
  46. 46 ARM DS·Lauterbach 분석 — Hardware Trace 전문 도구
  47. 47 Bare-metal 프로파일링 — GPIO·DWT·SysTick·ITM 활용
  48. 48 NVIDIA Nsight Systems — GPU·NPU 포함 시스템 분석
  49. 49 모던 프로파일러 비교 — Tracy·Hotspot·uftrace·Coz
  50. 50 연속 프로파일링 — Parca·Pixie·Pyroscope·Tetragon
  51. 51 사례 분석 — ISR latency deadline miss 진단 절차
  52. 52 사례 분석 — 행렬 곱 성능과 메모리 접근 패턴
  53. 53 사례 분석 — 멀티코어 처리량 저하에서 lock 병목 조사
  54. 54 사례 분석 — 카메라 payload와 DMA 전송 경로
  55. 55 CXL.mem 지연·대역폭 실측 — Direct·Switch·Pooled 토폴로지 비교
  56. 56 CXL 성능 프로파일링 도구 — cxl-cli·DAMON·perf-mem 활용
  57. 57 설계 시나리오 — CXL.mem과 LLM inference KV cache