SlideShare a Scribd company logo
Whenwe’redone, it’sdone!
DMA Survival Guide
Property of Tandem Group
Whenwe’redone, it’sdone!
Ramon Fried
 Developer and hacker since 1983
 B.sc in computer science
 Expertise in Embedded systems and Linux
system and kernel development.
 Currently Embedded Linux Team Leader in
TandemG
Property of Tandem Group
Whenwe’redone, it’sdone!
TandemG
 TandemG is Israel’s leading Software, Hardware and Systems R&D
center, acting as a one-stop-shop for our range of partners, from
prominent start-ups through to market leaders
 In the embedded domain, TandemG tailors solutions spanning
across RTOS, Embedded Linux, low-level Android and DSP.
 For the second year in a row, TandemG has been selected to
Delloite’s Israel Technology Fast50 list in the 23rd place
 Visit us at www.tandemG.com, for additional details.
Property of Tandem Group
Whenwe’redone, it’sdone!
Agenda
 What is DMA
 DMA Buffer allocation
— Coherent
— Streaming
 Scatter Gather mapping
 DMA pools
 DMA Triggering
— PCI
— dmaengine
Property of Tandem Group
Whenwe’redone, it’sdone!
What is DMA
 Direct memory access
 Feature of computer systems that allows
certain hardware subsystems to access main
system memory (RAM), independent of the
central processing unit (CPU).
 CPU can be notified on the end of operation by
IRQ.
Property of Tandem Group
Whenwe’redone, it’sdone!
What is DMA
Photo from: http://encyclopedia2.thefreedictionary.com/DMA
Property of Tandem Group
Whenwe’redone, it’sdone!
DMA Buffer allocation
 DMA controller works on physical addresses
 Physical memory needs to be accessible by
DMA controller
 Memory must be continuous
Property of Tandem Group
Whenwe’redone, it’sdone!
DMA Buffer allocation
 Coherent DMA mapping
— Usually long lasting.
— Can be accessed by both ends.
— No-caching *
— At least page sized.
 Streaming DMA mapping
— Usually singly used and freed.
— Architecture/Platform optimized.
— direction must be defined explicitly.
Property of Tandem Group
Whenwe’redone, it’sdone!
DMA access mask
#include <linux/dma-mapping.h>
int dma_set_mask_and_coherent(struct device
*dev, u64 mask);
int dma_set_mask (struct device *dev, u64
mask);
int dma_set_coherent_mask(struct device *dev,
u64 mask);
Property of Tandem Group
Whenwe’redone, it’sdone!
Coherent DMA mapping
#include <linux/dma-mapping.h>
void *dma_alloc_coherent(struct device *dev,
size_t size, dma_addr_t
*dma_handle, gfp_t flag);
void dma_free_coherent(struct device *dev,
size_t size, void *cpu_addr,
dma_addr_t dma_handle);
Property of Tandem Group
Whenwe’redone, it’sdone!
Streaming DMA mapping
#include <linux/dma-mapping.h>
dma_addr_t dma_map_single(struct device
*dev, void *ptr,size_t size,
enum dma_data_direction dir);
void dma_unmap_single(struct device
*dev, dma_addr_t addr,
size_t size,
enum dma_data_direction dir);
Property of Tandem Group
Whenwe’redone, it’sdone!
Streaming DMA mapping (cont’d)
 enum dma_data_direction
— DMA_TO_DEVICE
— DMA_FROM_DEVICE
— DMA_BIDIRECTIONAL
Property of Tandem Group
Whenwe’redone, it’sdone!
Streaming DMA usage
 Buffer Ownership
— Buffer is owned by the device.
— Altering the buffer can be done only after acquiring
ownership
 dma_sync_single_for_cpu()
— After altering the buffer, the ownership needs to be
returned to the device.
 dma_sync_single_for_device()
Property of Tandem Group
Whenwe’redone, it’sdone!
Scatter gather buffers
 Special type of streaming DMA
 writev, readv, clustered buffers (YUV plannar,
non continuous memory, etc.)
Property of Tandem Group
Whenwe’redone, it’sdone!
Scatter/Gather API
#include linux/dma-mapping.h
int dma_map_sg( struct device *dev,
struct scatterlist *sg,
int nents,
enum dma_data_direction dir);
void dma_unmap_sg(struct device *dev,
struct scatterlist *list,
int nents,
enum dma_data_direction dir);
Property of Tandem Group
Whenwe’redone, it’sdone!
Don’t forget to sync
#include linux/dma-mapping.h
void dma_sync_sg_for_cpu(struct device *dev,
struct scatterlist *sg,
int nelems,
enum dma_data_direction dir);
void dma_sync_sg_for_device(struct device *dev,
struct scatterlist *sg,
int nelems,
enum dma_data_direction dir);
Property of Tandem Group
Whenwe’redone, it’sdone!
DMA Pools
 DMA Pools
— Coherent
— Used to allocate buffers smaller than a page.
— dma_pool_create()
— dma_pool_destroy()
— dma_pool_alloc()
— dma_pool_free()
Property of Tandem Group
Whenwe’redone, it’sdone!
Start the DMA operation
Property of Tandem Group
Whenwe’redone, it’sdone!
Triggering the DMA operation
 PCI
 dmaengine
Property of Tandem Group
Whenwe’redone, it’sdone!
PCI Wrappers
 PCI wrappers
— pci_alloc_consistent()
— pci_free_consistent()
— pci_set_dma_mask()
— pci_pool_create()
— …
Property of Tandem Group
Whenwe’redone, it’sdone!
PCI DMA transaction example
dma_addr_t bus_addr;
bus_addr = dma_map_single(&dev->pci_dev->dev, buffer, count, DMA_TO_DEVICE);
writeb(dev->registers.command, DAD_CMD_DISABLEDMA);
writeb(dev->registers.command, DAD_CMD_WR);
writel(dev->registers.addr, cpu_to_le32(bus_addr));
writel(dev->registers.len, cpu_to_le32(count));
writeb(dev->registers.command, DAD_CMD_ENABLEDMA);
Property of Tandem Group
Whenwe’redone, it’sdone!
DMA Engine
 Subsystem to handle memory-to-device
transfers
 Exists since 2.6.18 (2006)
 Code in “drivers/dma”
 Documentation in “dmaengine/*”
 Poorly documented
THANK YOU!

More Related Content

What's hot

High-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uringHigh-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uring
ScyllaDB
 
The linux networking architecture
The linux networking architectureThe linux networking architecture
The linux networking architecture
hugo lu
 

What's hot (20)

Detecting Silent Data Corruptions using Linux DMA Debug API
Detecting Silent Data Corruptions using Linux DMA Debug APIDetecting Silent Data Corruptions using Linux DMA Debug API
Detecting Silent Data Corruptions using Linux DMA Debug API
 
Linux kernel debugging
Linux kernel debuggingLinux kernel debugging
Linux kernel debugging
 
What are latest new features that DPDK brings into 2018?
What are latest new features that DPDK brings into 2018?What are latest new features that DPDK brings into 2018?
What are latest new features that DPDK brings into 2018?
 
High-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uringHigh-Performance Networking Using eBPF, XDP, and io_uring
High-Performance Networking Using eBPF, XDP, and io_uring
 
The linux networking architecture
The linux networking architectureThe linux networking architecture
The linux networking architecture
 
Performance Wins with eBPF: Getting Started (2021)
Performance Wins with eBPF: Getting Started (2021)Performance Wins with eBPF: Getting Started (2021)
Performance Wins with eBPF: Getting Started (2021)
 
Introduction to DPDK
Introduction to DPDKIntroduction to DPDK
Introduction to DPDK
 
Spi drivers
Spi driversSpi drivers
Spi drivers
 
DPDK: Multi Architecture High Performance Packet Processing
DPDK: Multi Architecture High Performance Packet ProcessingDPDK: Multi Architecture High Performance Packet Processing
DPDK: Multi Architecture High Performance Packet Processing
 
BPF Internals (eBPF)
BPF Internals (eBPF)BPF Internals (eBPF)
BPF Internals (eBPF)
 
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
Vmlinux: anatomy of bzimage and how x86 64 processor is bootedVmlinux: anatomy of bzimage and how x86 64 processor is booted
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
 
Linux device drivers
Linux device drivers Linux device drivers
Linux device drivers
 
Understanding eBPF in a Hurry!
Understanding eBPF in a Hurry!Understanding eBPF in a Hurry!
Understanding eBPF in a Hurry!
 
Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...
 
Linux Kernel Crashdump
Linux Kernel CrashdumpLinux Kernel Crashdump
Linux Kernel Crashdump
 
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
qemu + gdb: The efficient way to understand/debug Linux kernel code/data stru...
 
Linux MMAP & Ioremap introduction
Linux MMAP & Ioremap introductionLinux MMAP & Ioremap introduction
Linux MMAP & Ioremap introduction
 
Introduction to Linux Kernel by Quontra Solutions
Introduction to Linux Kernel by Quontra SolutionsIntroduction to Linux Kernel by Quontra Solutions
Introduction to Linux Kernel by Quontra Solutions
 
malloc & vmalloc in Linux
malloc & vmalloc in Linuxmalloc & vmalloc in Linux
malloc & vmalloc in Linux
 
Linux Initialization Process (2)
Linux Initialization Process (2)Linux Initialization Process (2)
Linux Initialization Process (2)
 

Similar to DMA Survival Guide

the productive programer: mechanics
the productive programer: mechanicsthe productive programer: mechanics
the productive programer: mechanics
elliando dias
 
UGIF 12 2010 - features11.70
UGIF 12 2010 - features11.70UGIF 12 2010 - features11.70
UGIF 12 2010 - features11.70
UGIF
 
Informix User Group France - 30/11/2010 - Fonctionalités IDS 11.7
Informix User Group France - 30/11/2010 - Fonctionalités IDS 11.7Informix User Group France - 30/11/2010 - Fonctionalités IDS 11.7
Informix User Group France - 30/11/2010 - Fonctionalités IDS 11.7
Nicolas Desachy
 
Ugif 04 2011 déployer informix
Ugif 04 2011   déployer informixUgif 04 2011   déployer informix
Ugif 04 2011 déployer informix
UGIF
 
DYNAmore Ecosystem, News on DYNAmore's LS-DYNA-Tools_S_Mattern.pdf
DYNAmore Ecosystem, News on DYNAmore's LS-DYNA-Tools_S_Mattern.pdfDYNAmore Ecosystem, News on DYNAmore's LS-DYNA-Tools_S_Mattern.pdf
DYNAmore Ecosystem, News on DYNAmore's LS-DYNA-Tools_S_Mattern.pdf
BharathChannappa1
 

Similar to DMA Survival Guide (20)

Best practices for long-term support and security of the device-tree
Best practices for long-term support and security of the device-treeBest practices for long-term support and security of the device-tree
Best practices for long-term support and security of the device-tree
 
KIRANKUMAR_MV
KIRANKUMAR_MVKIRANKUMAR_MV
KIRANKUMAR_MV
 
OpenNebulaConf 2016 - The DRBD SDS for OpenNebula by Philipp Reisner, LINBIT
OpenNebulaConf 2016 - The DRBD SDS for OpenNebula by Philipp Reisner, LINBITOpenNebulaConf 2016 - The DRBD SDS for OpenNebula by Philipp Reisner, LINBIT
OpenNebulaConf 2016 - The DRBD SDS for OpenNebula by Philipp Reisner, LINBIT
 
Droidcon London 2021 - Full Stack Dart
Droidcon London 2021   - Full Stack DartDroidcon London 2021   - Full Stack Dart
Droidcon London 2021 - Full Stack Dart
 
Data compression in Modern Application
Data compression in Modern ApplicationData compression in Modern Application
Data compression in Modern Application
 
The rainbow treasure map: Advanced color management on Linux with AMD/Steam D...
The rainbow treasure map: Advanced color management on Linux with AMD/Steam D...The rainbow treasure map: Advanced color management on Linux with AMD/Steam D...
The rainbow treasure map: Advanced color management on Linux with AMD/Steam D...
 
Deft
DeftDeft
Deft
 
Overview of Spark for HPC
Overview of Spark for HPCOverview of Spark for HPC
Overview of Spark for HPC
 
DinakaraPandian_9+
DinakaraPandian_9+DinakaraPandian_9+
DinakaraPandian_9+
 
Managed DirectX
Managed DirectXManaged DirectX
Managed DirectX
 
Monitoring MySQL with DTrace/SystemTap
Monitoring MySQL with DTrace/SystemTapMonitoring MySQL with DTrace/SystemTap
Monitoring MySQL with DTrace/SystemTap
 
FOSDEM'17: Disaster Recovery Management with ReaR (relax-and-recover) & DRLM ...
FOSDEM'17: Disaster Recovery Management with ReaR (relax-and-recover) & DRLM ...FOSDEM'17: Disaster Recovery Management with ReaR (relax-and-recover) & DRLM ...
FOSDEM'17: Disaster Recovery Management with ReaR (relax-and-recover) & DRLM ...
 
HDX 3D
HDX 3DHDX 3D
HDX 3D
 
Fall of a domain | From local admin to Domain user hashes
Fall of a domain | From local admin to Domain user hashesFall of a domain | From local admin to Domain user hashes
Fall of a domain | From local admin to Domain user hashes
 
DevOpSec_DockerNPodMan-20230220.pdf
DevOpSec_DockerNPodMan-20230220.pdfDevOpSec_DockerNPodMan-20230220.pdf
DevOpSec_DockerNPodMan-20230220.pdf
 
the productive programer: mechanics
the productive programer: mechanicsthe productive programer: mechanics
the productive programer: mechanics
 
UGIF 12 2010 - features11.70
UGIF 12 2010 - features11.70UGIF 12 2010 - features11.70
UGIF 12 2010 - features11.70
 
Informix User Group France - 30/11/2010 - Fonctionalités IDS 11.7
Informix User Group France - 30/11/2010 - Fonctionalités IDS 11.7Informix User Group France - 30/11/2010 - Fonctionalités IDS 11.7
Informix User Group France - 30/11/2010 - Fonctionalités IDS 11.7
 
Ugif 04 2011 déployer informix
Ugif 04 2011   déployer informixUgif 04 2011   déployer informix
Ugif 04 2011 déployer informix
 
DYNAmore Ecosystem, News on DYNAmore's LS-DYNA-Tools_S_Mattern.pdf
DYNAmore Ecosystem, News on DYNAmore's LS-DYNA-Tools_S_Mattern.pdfDYNAmore Ecosystem, News on DYNAmore's LS-DYNA-Tools_S_Mattern.pdf
DYNAmore Ecosystem, News on DYNAmore's LS-DYNA-Tools_S_Mattern.pdf
 

More from Kernel TLV

Building Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCCBuilding Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCC
Kernel TLV
 
netfilter and iptables
netfilter and iptablesnetfilter and iptables
netfilter and iptables
Kernel TLV
 

More from Kernel TLV (20)

DPDK In Depth
DPDK In DepthDPDK In Depth
DPDK In Depth
 
Building Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCCBuilding Network Functions with eBPF & BCC
Building Network Functions with eBPF & BCC
 
SGX Trusted Execution Environment
SGX Trusted Execution EnvironmentSGX Trusted Execution Environment
SGX Trusted Execution Environment
 
Fun with FUSE
Fun with FUSEFun with FUSE
Fun with FUSE
 
Kernel Proc Connector and Containers
Kernel Proc Connector and ContainersKernel Proc Connector and Containers
Kernel Proc Connector and Containers
 
Bypassing ASLR Exploiting CVE 2015-7545
Bypassing ASLR Exploiting CVE 2015-7545Bypassing ASLR Exploiting CVE 2015-7545
Bypassing ASLR Exploiting CVE 2015-7545
 
Present Absence of Linux Filesystem Security
Present Absence of Linux Filesystem SecurityPresent Absence of Linux Filesystem Security
Present Absence of Linux Filesystem Security
 
OpenWrt From Top to Bottom
OpenWrt From Top to BottomOpenWrt From Top to Bottom
OpenWrt From Top to Bottom
 
Make Your Containers Faster: Linux Container Performance Tools
Make Your Containers Faster: Linux Container Performance ToolsMake Your Containers Faster: Linux Container Performance Tools
Make Your Containers Faster: Linux Container Performance Tools
 
Emerging Persistent Memory Hardware and ZUFS - PM-based File Systems in User ...
Emerging Persistent Memory Hardware and ZUFS - PM-based File Systems in User ...Emerging Persistent Memory Hardware and ZUFS - PM-based File Systems in User ...
Emerging Persistent Memory Hardware and ZUFS - PM-based File Systems in User ...
 
File Systems: Why, How and Where
File Systems: Why, How and WhereFile Systems: Why, How and Where
File Systems: Why, How and Where
 
netfilter and iptables
netfilter and iptablesnetfilter and iptables
netfilter and iptables
 
KernelTLV Speaker Guidelines
KernelTLV Speaker GuidelinesKernelTLV Speaker Guidelines
KernelTLV Speaker Guidelines
 
Userfaultfd: Current Features, Limitations and Future Development
Userfaultfd: Current Features, Limitations and Future DevelopmentUserfaultfd: Current Features, Limitations and Future Development
Userfaultfd: Current Features, Limitations and Future Development
 
The Linux Block Layer - Built for Fast Storage
The Linux Block Layer - Built for Fast StorageThe Linux Block Layer - Built for Fast Storage
The Linux Block Layer - Built for Fast Storage
 
Linux Kernel Cryptographic API and Use Cases
Linux Kernel Cryptographic API and Use CasesLinux Kernel Cryptographic API and Use Cases
Linux Kernel Cryptographic API and Use Cases
 
FD.IO Vector Packet Processing
FD.IO Vector Packet ProcessingFD.IO Vector Packet Processing
FD.IO Vector Packet Processing
 
WiFi and the Beast
WiFi and the BeastWiFi and the Beast
WiFi and the Beast
 
FreeBSD and Drivers
FreeBSD and DriversFreeBSD and Drivers
FreeBSD and Drivers
 
Specializing the Data Path - Hooking into the Linux Network Stack
Specializing the Data Path - Hooking into the Linux Network StackSpecializing the Data Path - Hooking into the Linux Network Stack
Specializing the Data Path - Hooking into the Linux Network Stack
 

Recently uploaded

JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)
Max Lee
 
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfMastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
mbmh111980
 

Recently uploaded (20)

top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
INGKA DIGITAL: Linked Metadata by Design
INGKA DIGITAL: Linked Metadata by DesignINGKA DIGITAL: Linked Metadata by Design
INGKA DIGITAL: Linked Metadata by Design
 
JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 
10 Essential Software Testing Tools You Need to Know About.pdf
10 Essential Software Testing Tools You Need to Know About.pdf10 Essential Software Testing Tools You Need to Know About.pdf
10 Essential Software Testing Tools You Need to Know About.pdf
 
CompTIA Security+ (Study Notes) for cs.pdf
CompTIA Security+ (Study Notes) for cs.pdfCompTIA Security+ (Study Notes) for cs.pdf
CompTIA Security+ (Study Notes) for cs.pdf
 
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning Framework
 
Workforce Efficiency with Employee Time Tracking Software.pdf
Workforce Efficiency with Employee Time Tracking Software.pdfWorkforce Efficiency with Employee Time Tracking Software.pdf
Workforce Efficiency with Employee Time Tracking Software.pdf
 
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with StrimziStrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi
 
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfMastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
 
How to install and activate eGrabber JobGrabber
How to install and activate eGrabber JobGrabberHow to install and activate eGrabber JobGrabber
How to install and activate eGrabber JobGrabber
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
A Guideline to Gorgias to to Re:amaze Data Migration
A Guideline to Gorgias to to Re:amaze Data MigrationA Guideline to Gorgias to to Re:amaze Data Migration
A Guideline to Gorgias to to Re:amaze Data Migration
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
5 Reasons Driving Warehouse Management Systems Demand
5 Reasons Driving Warehouse Management Systems Demand5 Reasons Driving Warehouse Management Systems Demand
5 Reasons Driving Warehouse Management Systems Demand
 
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
Tree in the Forest - Managing Details in BDD Scenarios (live2test 2024)
 
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdf
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdfImplementing KPIs and Right Metrics for Agile Delivery Teams.pdf
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdf
 
Benefits of Employee Monitoring Software
Benefits of  Employee Monitoring SoftwareBenefits of  Employee Monitoring Software
Benefits of Employee Monitoring Software
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 

DMA Survival Guide

  • 2. Property of Tandem Group Whenwe’redone, it’sdone! Ramon Fried  Developer and hacker since 1983  B.sc in computer science  Expertise in Embedded systems and Linux system and kernel development.  Currently Embedded Linux Team Leader in TandemG
  • 3. Property of Tandem Group Whenwe’redone, it’sdone! TandemG  TandemG is Israel’s leading Software, Hardware and Systems R&D center, acting as a one-stop-shop for our range of partners, from prominent start-ups through to market leaders  In the embedded domain, TandemG tailors solutions spanning across RTOS, Embedded Linux, low-level Android and DSP.  For the second year in a row, TandemG has been selected to Delloite’s Israel Technology Fast50 list in the 23rd place  Visit us at www.tandemG.com, for additional details.
  • 4. Property of Tandem Group Whenwe’redone, it’sdone! Agenda  What is DMA  DMA Buffer allocation — Coherent — Streaming  Scatter Gather mapping  DMA pools  DMA Triggering — PCI — dmaengine
  • 5. Property of Tandem Group Whenwe’redone, it’sdone! What is DMA  Direct memory access  Feature of computer systems that allows certain hardware subsystems to access main system memory (RAM), independent of the central processing unit (CPU).  CPU can be notified on the end of operation by IRQ.
  • 6. Property of Tandem Group Whenwe’redone, it’sdone! What is DMA Photo from: http://encyclopedia2.thefreedictionary.com/DMA
  • 7. Property of Tandem Group Whenwe’redone, it’sdone! DMA Buffer allocation  DMA controller works on physical addresses  Physical memory needs to be accessible by DMA controller  Memory must be continuous
  • 8. Property of Tandem Group Whenwe’redone, it’sdone! DMA Buffer allocation  Coherent DMA mapping — Usually long lasting. — Can be accessed by both ends. — No-caching * — At least page sized.  Streaming DMA mapping — Usually singly used and freed. — Architecture/Platform optimized. — direction must be defined explicitly.
  • 9. Property of Tandem Group Whenwe’redone, it’sdone! DMA access mask #include <linux/dma-mapping.h> int dma_set_mask_and_coherent(struct device *dev, u64 mask); int dma_set_mask (struct device *dev, u64 mask); int dma_set_coherent_mask(struct device *dev, u64 mask);
  • 10. Property of Tandem Group Whenwe’redone, it’sdone! Coherent DMA mapping #include <linux/dma-mapping.h> void *dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle, gfp_t flag); void dma_free_coherent(struct device *dev, size_t size, void *cpu_addr, dma_addr_t dma_handle);
  • 11. Property of Tandem Group Whenwe’redone, it’sdone! Streaming DMA mapping #include <linux/dma-mapping.h> dma_addr_t dma_map_single(struct device *dev, void *ptr,size_t size, enum dma_data_direction dir); void dma_unmap_single(struct device *dev, dma_addr_t addr, size_t size, enum dma_data_direction dir);
  • 12. Property of Tandem Group Whenwe’redone, it’sdone! Streaming DMA mapping (cont’d)  enum dma_data_direction — DMA_TO_DEVICE — DMA_FROM_DEVICE — DMA_BIDIRECTIONAL
  • 13. Property of Tandem Group Whenwe’redone, it’sdone! Streaming DMA usage  Buffer Ownership — Buffer is owned by the device. — Altering the buffer can be done only after acquiring ownership  dma_sync_single_for_cpu() — After altering the buffer, the ownership needs to be returned to the device.  dma_sync_single_for_device()
  • 14. Property of Tandem Group Whenwe’redone, it’sdone! Scatter gather buffers  Special type of streaming DMA  writev, readv, clustered buffers (YUV plannar, non continuous memory, etc.)
  • 15. Property of Tandem Group Whenwe’redone, it’sdone! Scatter/Gather API #include linux/dma-mapping.h int dma_map_sg( struct device *dev, struct scatterlist *sg, int nents, enum dma_data_direction dir); void dma_unmap_sg(struct device *dev, struct scatterlist *list, int nents, enum dma_data_direction dir);
  • 16. Property of Tandem Group Whenwe’redone, it’sdone! Don’t forget to sync #include linux/dma-mapping.h void dma_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, int nelems, enum dma_data_direction dir); void dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg, int nelems, enum dma_data_direction dir);
  • 17. Property of Tandem Group Whenwe’redone, it’sdone! DMA Pools  DMA Pools — Coherent — Used to allocate buffers smaller than a page. — dma_pool_create() — dma_pool_destroy() — dma_pool_alloc() — dma_pool_free()
  • 18. Property of Tandem Group Whenwe’redone, it’sdone! Start the DMA operation
  • 19. Property of Tandem Group Whenwe’redone, it’sdone! Triggering the DMA operation  PCI  dmaengine
  • 20. Property of Tandem Group Whenwe’redone, it’sdone! PCI Wrappers  PCI wrappers — pci_alloc_consistent() — pci_free_consistent() — pci_set_dma_mask() — pci_pool_create() — …
  • 21. Property of Tandem Group Whenwe’redone, it’sdone! PCI DMA transaction example dma_addr_t bus_addr; bus_addr = dma_map_single(&dev->pci_dev->dev, buffer, count, DMA_TO_DEVICE); writeb(dev->registers.command, DAD_CMD_DISABLEDMA); writeb(dev->registers.command, DAD_CMD_WR); writel(dev->registers.addr, cpu_to_le32(bus_addr)); writel(dev->registers.len, cpu_to_le32(count)); writeb(dev->registers.command, DAD_CMD_ENABLEDMA);
  • 22. Property of Tandem Group Whenwe’redone, it’sdone! DMA Engine  Subsystem to handle memory-to-device transfers  Exists since 2.6.18 (2006)  Code in “drivers/dma”  Documentation in “dmaengine/*”  Poorly documented

Editor's Notes

  1. DMA Controller work with physical memory. It doesn’t understands CPU’s virtual. It can access only the physical memory that is actually connected to it in the interconnect,. This memory is called bus addresses and usually is the same as CPU’s physical memory. ----- Virtual memory is not always continuous. The underling physical memory must be continuous. Basically kmalloc returns continuous, vmalloc not.
  2. Coherent examples: ring buffers, messages queues, mailboxes, etc. However, Reordering can occur. * We’ll see soon a way of allocating smaller chunks. Streaming examples: file systems buffers, network buffers
  3. Returns 0 on fail. Dma_handle is output param
  4. The buffer is given in ptr. Mapping can fail of course. Check return value using: dma_mapping_error()