arm: libpayload: Add cache coherent DMA memory definition and management

This patch adds a mechanism to set aside a region of cache-coherent
(i.e. usually uncached) virtual memory, which can be used to communicate
with DMA devices without automatic cache snooping (common on ARM)
without the need of explicit flush/invalidation instructions in the
driver code.

This works by setting aside said region in the (board-specific) page
table setup, as exemplary done in this patch for the Snow and Pit
boards. It uses a new mechanism for adding board-specific Coreboot table
entries to describe this region in an entry with the LB_DMA tag.

Libpayload's memory allocator is enhanced to be able to operate on
distinct types/regions of memory. It provides dma_malloc() and
dma_memalign() functions for use in drivers, which by default just
operate on the same heap as their traditional counterparts. However, if
the Coreboot table parsing code finds a CB_DMA section, further requests
through the dma_xxx() functions will return memory from the region
described therein instead.

Change-Id: Ia9c249249e936bbc3eb76e7b4822af2230ffb186
Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/167155
(cherry picked from commit d142ccdcd902a9d6ab4d495fbe6cbe85c61a5f01)
Signed-off-by: Isaac Christensen <isaac.christensen@se-eng.com>
Reviewed-on: http://review.coreboot.org/6622
Tested-by: build bot (Jenkins)
Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
This commit is contained in:
Julius Werner 2013-08-27 15:48:32 -07:00 committed by Isaac Christensen
parent 4498f6a6e5
commit b8fad3d029
8 changed files with 142 additions and 42 deletions

View File

@ -108,6 +108,12 @@ static void cb_parse_vdat(unsigned char *ptr, struct sysinfo_t *info)
} }
#endif #endif
static void cb_parse_dma(unsigned char *ptr)
{
struct cb_range *dma = (struct cb_range *)ptr;
init_dma_memory(phys_to_virt(dma->range_start), dma->range_size);
}
static void cb_parse_tstamp(unsigned char *ptr, struct sysinfo_t *info) static void cb_parse_tstamp(unsigned char *ptr, struct sysinfo_t *info)
{ {
struct cb_cbmem_tab *const cbmem = (struct cb_cbmem_tab *)ptr; struct cb_cbmem_tab *const cbmem = (struct cb_cbmem_tab *)ptr;
@ -262,6 +268,9 @@ static int cb_parse_header(void *addr, int len, struct sysinfo_t *info)
cb_parse_vbnv(ptr, info); cb_parse_vbnv(ptr, info);
break; break;
#endif #endif
case CB_TAG_DMA:
cb_parse_dma(ptr);
break;
case CB_TAG_TIMESTAMPS: case CB_TAG_TIMESTAMPS:
cb_parse_tstamp(ptr, info); cb_parse_tstamp(ptr, info);
break; break;

View File

@ -187,6 +187,7 @@ struct cb_gpios {
#define CB_TAG_VDAT 0x0015 #define CB_TAG_VDAT 0x0015
#define CB_TAG_VBNV 0x0019 #define CB_TAG_VBNV 0x0019
#define CB_TAG_VBOOT_HANDOFF 0x0020 #define CB_TAG_VBOOT_HANDOFF 0x0020
#define CB_TAG_DMA 0x0022
struct lb_range { struct lb_range {
uint32_t tag; uint32_t tag;
uint32_t size; uint32_t size;

View File

@ -112,6 +112,9 @@ void *calloc(size_t nmemb, size_t size);
void *realloc(void *ptr, size_t size); void *realloc(void *ptr, size_t size);
void *memalign(size_t align, size_t size); void *memalign(size_t align, size_t size);
#endif #endif
void init_dma_memory(void *start, u32 size);
void *dma_malloc(size_t size);
void *dma_memalign(size_t align, size_t size);
/** @} */ /** @} */
/** /**

View File

@ -42,10 +42,17 @@
#define IN_MALLOC_C #define IN_MALLOC_C
#include <libpayload.h> #include <libpayload.h>
struct memory_type {
void *start;
void *end;
struct align_region_t* align_regions;
};
extern char _heap, _eheap; /* Defined in the ldscript. */ extern char _heap, _eheap; /* Defined in the ldscript. */
static void *hstart = (void *)&_heap; static struct memory_type default_type = { (void *)&_heap, (void *)&_eheap, NULL };
static void *hend = (void *)&_eheap; static struct memory_type *const heap = &default_type;
static struct memory_type *dma = &default_type;
typedef u64 hdrtype_t; typedef u64 hdrtype_t;
#define HDRSIZE (sizeof(hdrtype_t)) #define HDRSIZE (sizeof(hdrtype_t))
@ -65,7 +72,7 @@ typedef u64 hdrtype_t;
#define IS_FREE(_h) (((_h) & (MAGIC | FLAG_FREE)) == (MAGIC | FLAG_FREE)) #define IS_FREE(_h) (((_h) & (MAGIC | FLAG_FREE)) == (MAGIC | FLAG_FREE))
#define HAS_MAGIC(_h) (((_h) & MAGIC) == MAGIC) #define HAS_MAGIC(_h) (((_h) & MAGIC) == MAGIC)
static int free_aligned(void* addr); static int free_aligned(void* addr, struct memory_type *type);
void print_malloc_map(void); void print_malloc_map(void);
#ifdef CONFIG_LP_DEBUG_MALLOC #ifdef CONFIG_LP_DEBUG_MALLOC
@ -73,6 +80,23 @@ static int heap_initialized = 0;
static int minimal_free = 0; static int minimal_free = 0;
#endif #endif
void init_dma_memory(void *start, u32 size)
{
#ifdef CONFIG_LP_DEBUG_MALLOC
if (dma != heap) {
printf("WARNING: %s called twice!\n");
return;
}
printf("Initializing cache-coherent DMA memory at [%p:%p]\n", start, start + size);
#endif
dma = malloc(sizeof(*dma));
dma->start = start;
dma->end = start + size;
dma->align_regions = NULL;
}
static void setup(hdrtype_t volatile *start, int size) static void setup(hdrtype_t volatile *start, int size)
{ {
*start = FREE_BLOCK(size); *start = FREE_BLOCK(size);
@ -83,10 +107,10 @@ static void setup(hdrtype_t volatile *start, int size)
#endif #endif
} }
static void *alloc(int len) static void *alloc(int len, struct memory_type *type)
{ {
hdrtype_t header; hdrtype_t header;
hdrtype_t volatile *ptr = (hdrtype_t volatile *) hstart; hdrtype_t volatile *ptr = (hdrtype_t volatile *)type->start;
/* Align the size. */ /* Align the size. */
len = (len + HDRSIZE - 1) & ~(HDRSIZE - 1); len = (len + HDRSIZE - 1) & ~(HDRSIZE - 1);
@ -137,17 +161,17 @@ static void *alloc(int len)
ptr = (hdrtype_t volatile *)((int)ptr + HDRSIZE + size); ptr = (hdrtype_t volatile *)((int)ptr + HDRSIZE + size);
} while (ptr < (hdrtype_t *) hend); } while (ptr < (hdrtype_t *) type->end);
/* Nothing available. */ /* Nothing available. */
return (void *)NULL; return (void *)NULL;
} }
static void _consolidate(void) static void _consolidate(struct memory_type *type)
{ {
void *ptr = hstart; void *ptr = type->start;
while (ptr < hend) { while (ptr < type->end) {
void *nptr; void *nptr;
hdrtype_t hdr = *((hdrtype_t *) ptr); hdrtype_t hdr = *((hdrtype_t *) ptr);
unsigned int size = 0; unsigned int size = 0;
@ -160,7 +184,7 @@ static void _consolidate(void)
size = SIZE(hdr); size = SIZE(hdr);
nptr = ptr + HDRSIZE + SIZE(hdr); nptr = ptr + HDRSIZE + SIZE(hdr);
while (nptr < hend) { while (nptr < type->end) {
hdrtype_t nhdr = *((hdrtype_t *) nptr); hdrtype_t nhdr = *((hdrtype_t *) nptr);
if (!(IS_FREE(nhdr))) if (!(IS_FREE(nhdr)))
@ -181,15 +205,18 @@ static void _consolidate(void)
void free(void *ptr) void free(void *ptr)
{ {
hdrtype_t hdr; hdrtype_t hdr;
struct memory_type *type = heap;
if (free_aligned(ptr)) return;
ptr -= HDRSIZE;
/* Sanity check. */ /* Sanity check. */
if (ptr < hstart || ptr >= hend) if (ptr < type->start || ptr >= type->end) {
type = dma;
if (ptr < type->start || ptr >= type->end)
return; return;
}
if (free_aligned(ptr, type)) return;
ptr -= HDRSIZE;
hdr = *((hdrtype_t *) ptr); hdr = *((hdrtype_t *) ptr);
/* Not our header (we're probably poisoned). */ /* Not our header (we're probably poisoned). */
@ -201,18 +228,23 @@ void free(void *ptr)
return; return;
*((hdrtype_t *) ptr) = FREE_BLOCK(SIZE(hdr)); *((hdrtype_t *) ptr) = FREE_BLOCK(SIZE(hdr));
_consolidate(); _consolidate(type);
} }
void *malloc(size_t size) void *malloc(size_t size)
{ {
return alloc(size); return alloc(size, heap);
}
void *dma_malloc(size_t size)
{
return alloc(size, dma);
} }
void *calloc(size_t nmemb, size_t size) void *calloc(size_t nmemb, size_t size)
{ {
size_t total = nmemb * size; size_t total = nmemb * size;
void *ptr = alloc(total); void *ptr = alloc(total, heap);
if (ptr) if (ptr)
memset(ptr, 0, total); memset(ptr, 0, total);
@ -224,15 +256,19 @@ void *realloc(void *ptr, size_t size)
{ {
void *ret, *pptr; void *ret, *pptr;
unsigned int osize; unsigned int osize;
struct memory_type *type = heap;
if (ptr == NULL) if (ptr == NULL)
return alloc(size); return alloc(size, type);
pptr = ptr - HDRSIZE; pptr = ptr - HDRSIZE;
if (!HAS_MAGIC(*((hdrtype_t *) pptr))) if (!HAS_MAGIC(*((hdrtype_t *) pptr)))
return NULL; return NULL;
if (ptr < type->start || ptr >= type->end)
type = dma;
/* Get the original size of the block. */ /* Get the original size of the block. */
osize = SIZE(*((hdrtype_t *) pptr)); osize = SIZE(*((hdrtype_t *) pptr));
@ -242,7 +278,7 @@ void *realloc(void *ptr, size_t size)
* reallocated the new space. * reallocated the new space.
*/ */
free(ptr); free(ptr);
ret = alloc(size); ret = alloc(size, type);
/* /*
* if ret == NULL, then doh - failure. * if ret == NULL, then doh - failure.
@ -277,14 +313,12 @@ struct align_region_t
struct align_region_t *next; struct align_region_t *next;
}; };
static struct align_region_t* align_regions = 0; static struct align_region_t *allocate_region(int alignment, int num_elements, struct memory_type *type)
static struct align_region_t *allocate_region(int alignment, int num_elements)
{ {
struct align_region_t *new_region; struct align_region_t *new_region;
#ifdef CONFIG_LP_DEBUG_MALLOC #ifdef CONFIG_LP_DEBUG_MALLOC
printf("%s(old align_regions=%p, alignment=%u, num_elements=%u)\n", printf("%s(old align_regions=%p, alignment=%u, num_elements=%u)\n",
__func__, align_regions, alignment, num_elements); __func__, type->align_regions, alignment, num_elements);
#endif #endif
new_region = malloc(sizeof(struct align_region_t)); new_region = malloc(sizeof(struct align_region_t));
@ -292,7 +326,7 @@ static struct align_region_t *allocate_region(int alignment, int num_elements)
if (!new_region) if (!new_region)
return NULL; return NULL;
new_region->alignment = alignment; new_region->alignment = alignment;
new_region->start = malloc((num_elements+1) * alignment + num_elements); new_region->start = alloc((num_elements+1) * alignment + num_elements, type);
if (!new_region->start) { if (!new_region->start) {
free(new_region); free(new_region);
return NULL; return NULL;
@ -300,16 +334,16 @@ static struct align_region_t *allocate_region(int alignment, int num_elements)
new_region->start_data = (void*)((u32)(new_region->start + num_elements + alignment - 1) & (~(alignment-1))); new_region->start_data = (void*)((u32)(new_region->start + num_elements + alignment - 1) & (~(alignment-1)));
new_region->size = num_elements * alignment; new_region->size = num_elements * alignment;
new_region->free = num_elements; new_region->free = num_elements;
new_region->next = align_regions; new_region->next = type->align_regions;
memset(new_region->start, 0, num_elements); memset(new_region->start, 0, num_elements);
align_regions = new_region; type->align_regions = new_region;
return new_region; return new_region;
} }
static int free_aligned(void* addr) static int free_aligned(void* addr, struct memory_type *type)
{ {
struct align_region_t *reg = align_regions; struct align_region_t *reg = type->align_regions;
while (reg != 0) while (reg != 0)
{ {
if ((addr >= reg->start_data) && (addr < reg->start_data + reg->size)) if ((addr >= reg->start_data) && (addr < reg->start_data + reg->size))
@ -329,16 +363,16 @@ static int free_aligned(void* addr)
return 0; return 0;
} }
void *memalign(size_t align, size_t size) static void *alloc_aligned(size_t align, size_t size, struct memory_type *type)
{ {
if (size == 0) return 0; if (size == 0) return 0;
if (align_regions == 0) { if (type->align_regions == 0) {
align_regions = malloc(sizeof(struct align_region_t)); type->align_regions = malloc(sizeof(struct align_region_t));
if (align_regions == NULL) if (type->align_regions == NULL)
return NULL; return NULL;
memset(align_regions, 0, sizeof(struct align_region_t)); memset(type->align_regions, 0, sizeof(struct align_region_t));
} }
struct align_region_t *reg = align_regions; struct align_region_t *reg = type->align_regions;
look_further: look_further:
while (reg != 0) while (reg != 0)
{ {
@ -357,9 +391,9 @@ look_further:
printf(" need to allocate a new memalign region\n"); printf(" need to allocate a new memalign region\n");
#endif #endif
/* get align regions */ /* get align regions */
reg = allocate_region(align, (size<1024)?(1024/align):(((size-1)/align)+1)); reg = allocate_region(align, (size<1024)?(1024/align):(((size-1)/align)+1), type);
#ifdef CONFIG_LP_DEBUG_MALLOC #ifdef CONFIG_LP_DEBUG_MALLOC
printf(" ... returned %p\n", align_regions); printf(" ... returned %p\n", reg);
#endif #endif
} }
if (reg == 0) { if (reg == 0) {
@ -393,14 +427,24 @@ look_further:
goto look_further; // end condition is once a new region is allocated - it always has enough space goto look_further; // end condition is once a new region is allocated - it always has enough space
} }
void *memalign(size_t align, size_t size)
{
return alloc_aligned(align, size, heap);
}
void *dma_memalign(size_t align, size_t size)
{
return alloc_aligned(align, size, dma);
}
/* This is for debugging purposes. */ /* This is for debugging purposes. */
#ifdef CONFIG_LP_DEBUG_MALLOC #ifdef CONFIG_LP_DEBUG_MALLOC
void print_malloc_map(void) void print_malloc_map(void)
{ {
void *ptr = hstart; void *ptr = heap->start;
int free_memory = 0; int free_memory = 0;
while (ptr < hend) { while (ptr < heap->end) {
hdrtype_t hdr = *((hdrtype_t *) ptr); hdrtype_t hdr = *((hdrtype_t *) ptr);
if (!HAS_MAGIC(hdr)) { if (!HAS_MAGIC(hdr)) {
@ -414,7 +458,7 @@ void print_malloc_map(void)
/* FIXME: Verify the size of the block. */ /* FIXME: Verify the size of the block. */
printf("%x: %s (%x bytes)\n", printf("%x: %s (%x bytes)\n",
(unsigned int)(ptr - hstart), (unsigned int)(ptr - heap->start),
hdr & FLAG_FREE ? "FREE" : "USED", SIZE(hdr)); hdr & FLAG_FREE ? "FREE" : "USED", SIZE(hdr));
if (hdr & FLAG_FREE) if (hdr & FLAG_FREE)

View File

@ -218,6 +218,7 @@ struct lb_gpios {
#define LB_TAG_VDAT 0x0015 #define LB_TAG_VDAT 0x0015
#define LB_TAG_VBNV 0x0019 #define LB_TAG_VBNV 0x0019
#define LB_TAB_VBOOT_HANDOFF 0x0020 #define LB_TAB_VBOOT_HANDOFF 0x0020
#define LB_TAB_DMA 0x0022
struct lb_range { struct lb_range {
uint32_t tag; uint32_t tag;
uint32_t size; uint32_t size;
@ -332,4 +333,9 @@ void uart_fill_lb(void *data);
void lb_add_serial(struct lb_serial *serial, void *data); void lb_add_serial(struct lb_serial *serial, void *data);
void lb_add_console(uint16_t consoletype, void *data); void lb_add_console(uint16_t consoletype, void *data);
/* Define this in mainboard.c to add board-specific table entries. */
void lb_board(struct lb_header *header);
struct lb_record *lb_new_record(struct lb_header *header);
#endif /* COREBOOT_TABLES_H */ #endif /* COREBOOT_TABLES_H */

View File

@ -77,7 +77,7 @@ static struct lb_record *lb_last_record(struct lb_header *header)
return rec; return rec;
} }
static struct lb_record *lb_new_record(struct lb_header *header) struct lb_record *lb_new_record(struct lb_header *header)
{ {
struct lb_record *rec; struct lb_record *rec;
rec = lb_last_record(header); rec = lb_last_record(header);
@ -298,6 +298,8 @@ static void lb_strings(struct lb_header *header)
} }
void __attribute__((weak)) lb_board(struct lb_header *header) { /* NOOP */ }
static struct lb_forward *lb_forward(struct lb_header *header, struct lb_header *next_header) static struct lb_forward *lb_forward(struct lb_header *header, struct lb_header *next_header)
{ {
struct lb_record *rec; struct lb_record *rec;
@ -425,6 +427,9 @@ unsigned long write_coreboot_table(
#endif #endif
add_cbmem_pointers(head); add_cbmem_pointers(head);
/* Add board-specific table entries, if any. */
lb_board(head);
/* Remember where my valid memory ranges are */ /* Remember where my valid memory ranges are */
return lb_table_fini(head); return lb_table_fini(head);
} }

View File

@ -45,6 +45,10 @@
#define DRAM_START (CONFIG_SYS_SDRAM_BASE >> 20) #define DRAM_START (CONFIG_SYS_SDRAM_BASE >> 20)
#define DRAM_SIZE CONFIG_DRAM_SIZE_MB #define DRAM_SIZE CONFIG_DRAM_SIZE_MB
/* Arbitrary range of DMA memory for depthcharge's drivers */
#define DMA_START (0x77300000)
#define DMA_SIZE (0x00100000)
static struct edid edid = { static struct edid edid = {
.ha = 1366, .ha = 1366,
.va = 768, .va = 768,
@ -452,6 +456,7 @@ static void mainboard_enable(device_t dev)
/* set up caching for the DRAM */ /* set up caching for the DRAM */
mmu_config_range(DRAM_START, DRAM_SIZE, DCACHE_WRITEBACK); mmu_config_range(DRAM_START, DRAM_SIZE, DCACHE_WRITEBACK);
mmu_config_range(DMA_START >> 20, DMA_SIZE >> 20, DCACHE_OFF);
tlb_invalidate_all(); tlb_invalidate_all();
/* this is going to move, but we must have it now and we're /* this is going to move, but we must have it now and we're
@ -472,3 +477,14 @@ struct chip_operations mainboard_ops = {
.name = "Samsung/Google ARM Chromebook", .name = "Samsung/Google ARM Chromebook",
.enable_dev = mainboard_enable, .enable_dev = mainboard_enable,
}; };
void lb_board(struct lb_header *header)
{
struct lb_range *dma;
dma = (struct lb_range *)lb_new_record(header);
dma->tag = LB_TAB_DMA;
dma->size = sizeof(*dma);
dma->range_start = (intptr_t)DMA_START;
dma->range_size = DMA_SIZE;
}

View File

@ -46,6 +46,10 @@
#define DRAM_SIZE CONFIG_DRAM_SIZE_MB #define DRAM_SIZE CONFIG_DRAM_SIZE_MB
#define DRAM_END (DRAM_START + DRAM_SIZE) /* plus one... */ #define DRAM_END (DRAM_START + DRAM_SIZE) /* plus one... */
/* Arbitrary range of DMA memory for depthcharge's drivers */
#define DMA_START (0x77300000)
#define DMA_SIZE (0x00100000)
static struct edid edid = { static struct edid edid = {
.ha = 1366, .ha = 1366,
.va = 768, .va = 768,
@ -320,6 +324,7 @@ static void mainboard_enable(device_t dev)
mmu_init(); mmu_init();
mmu_config_range(0, DRAM_START, DCACHE_OFF); mmu_config_range(0, DRAM_START, DCACHE_OFF);
mmu_config_range(DRAM_START, DRAM_SIZE, DCACHE_WRITEBACK); mmu_config_range(DRAM_START, DRAM_SIZE, DCACHE_WRITEBACK);
mmu_config_range(DMA_START >> 20, DMA_SIZE >> 20, DCACHE_OFF);
mmu_config_range(DRAM_END, 4096 - DRAM_END, DCACHE_OFF); mmu_config_range(DRAM_END, 4096 - DRAM_END, DCACHE_OFF);
dcache_invalidate_all(); dcache_invalidate_all();
dcache_mmu_enable(); dcache_mmu_enable();
@ -342,3 +347,14 @@ struct chip_operations mainboard_ops = {
.name = "Samsung/Google ARM Chromebook", .name = "Samsung/Google ARM Chromebook",
.enable_dev = mainboard_enable, .enable_dev = mainboard_enable,
}; };
void lb_board(struct lb_header *header)
{
struct lb_range *dma;
dma = (struct lb_range *)lb_new_record(header);
dma->tag = LB_TAB_DMA;
dma->size = sizeof(*dma);
dma->range_start = (intptr_t)DMA_START;
dma->range_size = DMA_SIZE;
}