C FCode Memory Allocation





For OpenBoot 2

To get general-purpose memory, use buffer: or alloc-mem. Use free-mem to deallocate memory obtained with alloc-mem.

To map in portions of your device for ordinary access, use " map-in" $call-parent ( adr space size -- virt ), as in:

------------------------------------------------------------------------
my-address offset + my-space size " map-in" $call-parent ( virt ) ------------------------------------------------------------------------

To later map out those portions of your device, use " map-out" $call- parent ( virt size -- ), as in:

-----------------------------------------
( virt ) size " map-out" $call-parent -----------------------------------------

To use a region of system memory for DMA (for example, for both direct CPU access and DMA access from a device), first define the following mapping and allocation routines, then follow the steps below to ensure data coherency.

---------------------------------------------------------------------------
: dma-alloc ( n -- virt ) " dma-alloc" $call-parent ; : dma-free ( virt n -- ) " dma-free" $call-parent ; : dma-map-in ( virt n cache? -- devaddr ) " dma-map-in" $call-parent ; : dma-map-out ( virt devaddr n -- ) " dma-map-out" $call-parent ; : dma-sync ( virt devaddr size -- ) \ Correct even if "dma-sync" missing " dma-sync" ['] $call-parent catch if 2drop 3drop then ; ---------------------------------------------------------------------------
    1. Allocate the DMA region with:
    2. CPU accesses the region using virt from dma-alloc, then perform:
    3. Start DMA operation, using devaddr from dma-map-in.
    4. Repeat steps 2 and 3 as needed
    5. 5) Deallocate the region when completed, with:

For OpenBoot 1

To obtain general-purpose memory, use buffer: or alloc-mem for small amounts (less than several hundred bytes). Use dma-alloc for larger amounts.

Use free-mem to deallocate memory allocated with alloc-mem. Use free- virtual to deallocate memory allocated with dma-alloc.

To map in portions of your device for ordinary access, use map-sbus.

To map out portions of your device, use free-virtual.

To use a region of system memory for DMA (for example, both direct CPU access and DMA access from a device), map it in with dma-alloc. CPU accesses and DMA accesses may be performed interchangeably.

When the memory is no longer needed, unmap it with free-virtual.

When unmapping multiple regions using free-virtual, you must perform the unmapping in the reverse order that the memory was originally mapped in.