Documentation
¶
Overview ¶
Package dma provides primitives for direct memory allocation and alignment, it is primarily used in bare metal device driver operation to avoid passing Go pointers for DMA purposes.
This package is only meant to be used with `GOOS=tamago` as supported by the TamaGo framework for bare metal Go, see https://github.com/usbarmory/tamago.
Index ¶
- Constants
- func Alloc(buf []byte, align int) (addr uint)
- func Free(addr uint)
- func Init(start uint, size int) (err error)
- func Read(addr uint, off int, buf []byte)
- func Release(addr uint)
- func Reserve(size int, align int) (addr uint, buf []byte)
- func Reserved(buf []byte) (res bool, addr uint)
- func Write(addr uint, off int, buf []byte)
- type Region
- func (r *Region) Alloc(buf []byte, align int) (addr uint)
- func (r *Region) End() uint
- func (r *Region) Free(addr uint)
- func (r *Region) FreeBlocks() map[uint]uint
- func (r *Region) Init(start uint, size uint)
- func (r *Region) Read(addr uint, off int, buf []byte)
- func (r *Region) Release(addr uint)
- func (r *Region) Reserve(size int, align int) (addr uint, buf []byte)
- func (r *Region) Reserved(buf []byte) (res bool, addr uint)
- func (r *Region) Size() uint
- func (r *Region) Start() uint
- func (r *Region) UsedBlocks() map[uint]uint
- func (r *Region) Write(addr uint, off int, buf []byte)
Constants ¶
const DefaultAlignment = (32 << (^uint(0) >> 63)) / 8
Variables ¶
This section is empty.
Functions ¶
func Init ¶
Init initializes the global memory region for DMA buffer allocation, used throughout the tamago package for all DMA allocations.
Additional DMA regions for application use can be allocated through NewRegion().
func Release ¶
func Release(addr uint)
Release is the equivalent of Region.Release() on the global DMA region.
Types ¶
type Region ¶
Region represents a memory region allocated for DMA purposes.
func NewRegion ¶
NewRegion initializes a memory region for DMA buffer allocation.
To avoid unforseen consequences the caller must ensure that allocated regions do not overlap among themselves or with the global one (see Init()).
To allow allocation of DMA buffers within Go runtime memory the unsafe flag must be set.
func (*Region) Alloc ¶
Alloc reserves a memory region for DMA purposes, copying over a buffer and returning its allocation address, with optional alignment. The region can be freed up with Free().
If the argument is a buffer previously created with Reserve(), then its address is return without any re-allocation.
The optional alignment must be a power of 2 and word alignment is always enforced, 0 means 4 on 32-bit platforms and 8 on 64-bit ones.
func (*Region) Free ¶
Free frees the memory region stored at the passed address, the region must have been previously allocated with Alloc().
func (*Region) FreeBlocks ¶
FreeBlocks returns the DMA region free blocks addresses and size.
func (*Region) Read ¶
Read reads exactly len(buf) bytes from a memory region address into a buffer, the region must have been previously allocated with Alloc().
The offset and buffer size are used to retrieve a slice of the memory region, a panic occurs if these parameters are not compatible with the initial allocation for the address.
If the argument is a buffer previously created with Reserve(), then the function returns without modifying it, as it is assumed for the buffer to be already updated.
func (*Region) Release ¶
Release frees the memory region stored at the passed address, the region must have been previously allocated with Reserve().
func (*Region) Reserve ¶
Reserve allocates a Slice of bytes for DMA purposes, by placing its data within the DMA region, with optional alignment. It returns the slice along with its data allocation address. The buffer can be freed up with Release().
Reserving buffers with Reserve() allows applications to pre-allocate DMA regions, avoiding unnecessary memory copy operations when performance is a concern. Reserved buffers cause Alloc() and Read() to return without any allocation or memory copy.
Great care must be taken on reserved buffer as:
- buf contents are uninitialized (unlike when using Alloc())
- buf slices remain in reserved space but only the original buf can be subject of Release()
The optional alignment must be a power of 2 and word alignment is always enforced, 0 means 4 on 32-bit platforms and 8 on 64-bit ones.
func (*Region) Reserved ¶
Reserved returns whether a slice of bytes data is allocated within the DMA buffer region, it is used to determine whether the passed buffer has been previously allocated by this package with Reserve().
func (*Region) UsedBlocks ¶
UsedBlocks returns the DMA region allocated blocks addresses and size.