Skip to content

Commit

Permalink
core.memory: Add 'align' argument to dma_alloc()
Browse files Browse the repository at this point in the history
Now it is possible to request specific alignment for DMA memory.

This is practical. For example, Mellanox ConnectX-4 requires specific
alignments (e.g. 4KB).
  • Loading branch information
lukego committed Jun 8, 2016
1 parent 6a426bd commit f349c41
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/README.md
Expand Up @@ -339,10 +339,13 @@ can be accessed directly by network cards. The important
characteristic of DMA memory is being located in contiguous physical
memory at a stable address.

— Function **memory.dma_alloc** *bytes*
— Function **memory.dma_alloc** *bytes*, *[alignment]*

Returns a pointer to *bytes* of new DMA memory.

Optionally a specific *alignment* requirement can be provided (in
bytes). The default alignment is 128.

— Function **memory.virtual_to_physical** *pointer*

Returns the physical address (`uint64_t`) the DMA memory at *pointer*.
Expand Down
15 changes: 11 additions & 4 deletions src/core/memory.lua
Expand Up @@ -23,13 +23,20 @@ chunks = {}

-- Allocate DMA-friendly memory.
-- Return virtual memory pointer, physical address, and actual size.
function dma_alloc (bytes)
function dma_alloc (bytes, align)
align = align or 128
assert(bytes <= huge_page_size)
bytes = lib.align(bytes, 128)
if #chunks == 0 or bytes + chunks[#chunks].used > chunks[#chunks].size then
-- Get current chunk of memory to allocate from
if #chunks == 0 then allocate_next_chunk() end
local chunk = chunks[#chunks]
-- Skip allocation forward pointer to suit alignment
chunk.used = lib.align(chunk.used, align)
-- Need a new chunk to service this allocation?
if chunk.used + bytes > chunk.size then
allocate_next_chunk()
chunk = chunks[#chunks]
end
local chunk = chunks[#chunks]
-- Slice out the memory we need
local where = chunk.used
chunk.used = chunk.used + bytes
return chunk.pointer + where, chunk.physical + where, bytes
Expand Down

0 comments on commit f349c41

Please sign in to comment.