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, Pit and Kirby
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.
BUG=chrome-os-partner:21969
TEST=Manual
Change-Id: Ia9c249249e936bbc3eb76e7b4822af2230ffb186
Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/167155
diff --git a/payloads/libpayload/arch/armv7/coreboot.c b/payloads/libpayload/arch/armv7/coreboot.c
index c55c9a3..6e3a736 100644
--- a/payloads/libpayload/arch/armv7/coreboot.c
+++ b/payloads/libpayload/arch/armv7/coreboot.c
@@ -108,6 +108,12 @@
}
#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)
{
struct cb_cbmem_tab *const cbmem = (struct cb_cbmem_tab *)ptr;
@@ -262,6 +268,9 @@
cb_parse_vbnv(ptr, info);
break;
#endif
+ case CB_TAG_DMA:
+ cb_parse_dma(ptr);
+ break;
case CB_TAG_TIMESTAMPS:
cb_parse_tstamp(ptr, info);
break;
diff --git a/payloads/libpayload/include/coreboot_tables.h b/payloads/libpayload/include/coreboot_tables.h
index 2501594..2b78e5e 100644
--- a/payloads/libpayload/include/coreboot_tables.h
+++ b/payloads/libpayload/include/coreboot_tables.h
@@ -187,6 +187,7 @@
#define CB_TAG_VDAT 0x0015
#define CB_TAG_VBNV 0x0019
#define CB_TAG_VBOOT_HANDOFF 0x0020
+#define CB_TAG_DMA 0x0022
struct cb_range {
uint32_t tag;
uint32_t size;
diff --git a/payloads/libpayload/include/stdlib.h b/payloads/libpayload/include/stdlib.h
index 92a13e3..8c8c200 100644
--- a/payloads/libpayload/include/stdlib.h
+++ b/payloads/libpayload/include/stdlib.h
@@ -112,6 +112,9 @@
void *realloc(void *ptr, size_t size);
void *memalign(size_t align, size_t size);
#endif
+void init_dma_memory(void *start, u32 size);
+void *dma_malloc(size_t size);
+void *dma_memalign(size_t align, size_t size);
/** @} */
/**
diff --git a/payloads/libpayload/libc/malloc.c b/payloads/libpayload/libc/malloc.c
index 7044b42..fbd362a 100644
--- a/payloads/libpayload/libc/malloc.c
+++ b/payloads/libpayload/libc/malloc.c
@@ -45,10 +45,17 @@
#define IN_MALLOC_C
#include <libpayload.h>
+struct memory_type {
+ void *start;
+ void *end;
+ struct align_region_t* align_regions;
+};
+
extern char _heap, _eheap; /* Defined in the ldscript. */
-static void *hstart = (void *)&_heap;
-static void *hend = (void *)&_eheap;
+static struct memory_type default_type = { (void *)&_heap, (void *)&_eheap, NULL };
+static struct memory_type *const heap = &default_type;
+static struct memory_type *dma = &default_type;
typedef unsigned int hdrtype_t;
@@ -69,7 +76,7 @@
#define IS_FREE(_h) (((_h) & (MAGIC | FLAG_FREE)) == (MAGIC | FLAG_FREE))
#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);
#ifdef CONFIG_LP_DEBUG_MALLOC
@@ -77,6 +84,23 @@
static int minimal_free = 0;
#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)
{
*start = FREE_BLOCK(size);
@@ -87,10 +111,10 @@
#endif
}
-static void *alloc(int len)
+static void *alloc(int len, struct memory_type *type)
{
hdrtype_t header;
- hdrtype_t volatile *ptr = (hdrtype_t volatile *) hstart;
+ hdrtype_t volatile *ptr = (hdrtype_t volatile *)type->start;
/* Align the size. */
len = (len + 3) & ~3;
@@ -141,17 +165,17 @@
ptr = (hdrtype_t volatile *)((int)ptr + HDRSIZE + size);
- } while (ptr < (hdrtype_t *) hend);
+ } while (ptr < (hdrtype_t *) type->end);
/* Nothing available. */
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;
hdrtype_t hdr = *((hdrtype_t *) ptr);
unsigned int size = 0;
@@ -164,7 +188,7 @@
size = SIZE(hdr);
nptr = ptr + HDRSIZE + SIZE(hdr);
- while (nptr < hend) {
+ while (nptr < type->end) {
hdrtype_t nhdr = *((hdrtype_t *) nptr);
if (!(IS_FREE(nhdr)))
@@ -185,15 +209,18 @@
void free(void *ptr)
{
hdrtype_t hdr;
-
- if (free_aligned(ptr)) return;
-
- ptr -= HDRSIZE;
+ struct memory_type *type = heap;
/* Sanity check. */
- if (ptr < hstart || ptr >= hend)
- return;
+ if (ptr < type->start || ptr >= type->end) {
+ type = dma;
+ if (ptr < type->start || ptr >= type->end)
+ return;
+ }
+ if (free_aligned(ptr, type)) return;
+
+ ptr -= HDRSIZE;
hdr = *((hdrtype_t *) ptr);
/* Not our header (we're probably poisoned). */
@@ -205,18 +232,23 @@
return;
*((hdrtype_t *) ptr) = FREE_BLOCK(SIZE(hdr));
- _consolidate();
+ _consolidate(type);
}
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)
{
size_t total = nmemb * size;
- void *ptr = alloc(total);
+ void *ptr = alloc(total, heap);
if (ptr)
memset(ptr, 0, total);
@@ -228,15 +260,19 @@
{
void *ret, *pptr;
unsigned int osize;
+ struct memory_type *type = heap;
if (ptr == NULL)
- return alloc(size);
+ return alloc(size, type);
pptr = ptr - HDRSIZE;
if (!HAS_MAGIC(*((hdrtype_t *) pptr)))
return NULL;
+ if (ptr < type->start || ptr >= type->end)
+ type = dma;
+
/* Get the original size of the block. */
osize = SIZE(*((hdrtype_t *) pptr));
@@ -246,7 +282,7 @@
* reallocated the new space.
*/
free(ptr);
- ret = alloc(size);
+ ret = alloc(size, type);
/*
* if ret == NULL, then doh - failure.
@@ -281,14 +317,12 @@
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)
+static struct align_region_t *allocate_region(int alignment, int num_elements, struct memory_type *type)
{
struct align_region_t *new_region;
#ifdef CONFIG_LP_DEBUG_MALLOC
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
new_region = malloc(sizeof(struct align_region_t));
@@ -296,7 +330,7 @@
if (!new_region)
return NULL;
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) {
free(new_region);
return NULL;
@@ -304,16 +338,16 @@
new_region->start_data = (void*)((u32)(new_region->start + num_elements + alignment - 1) & (~(alignment-1)));
new_region->size = num_elements * alignment;
new_region->free = num_elements;
- new_region->next = align_regions;
+ new_region->next = type->align_regions;
memset(new_region->start, 0, num_elements);
- align_regions = new_region;
+ type->align_regions = 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)
{
if ((addr >= reg->start_data) && (addr < reg->start_data + reg->size))
@@ -333,16 +367,16 @@
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 (align_regions == 0) {
- align_regions = malloc(sizeof(struct align_region_t));
- if (align_regions == NULL)
+ if (type->align_regions == 0) {
+ type->align_regions = malloc(sizeof(struct align_region_t));
+ if (type->align_regions == 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:
while (reg != 0)
{
@@ -361,9 +395,9 @@
printf(" need to allocate a new memalign region\n");
#endif
/* 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
- printf(" ... returned %p\n", align_regions);
+ printf(" ... returned %p\n", reg);
#endif
}
if (reg == 0) {
@@ -397,14 +431,24 @@
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. */
#ifdef CONFIG_LP_DEBUG_MALLOC
void print_malloc_map(void)
{
- void *ptr = hstart;
+ void *ptr = heap->start;
int free_memory = 0;
- while (ptr < hend) {
+ while (ptr < heap->end) {
hdrtype_t hdr = *((hdrtype_t *) ptr);
if (!HAS_MAGIC(hdr)) {
@@ -418,7 +462,7 @@
/* FIXME: Verify the size of the block. */
printf("%x: %s (%x bytes)\n",
- (unsigned int)(ptr - hstart),
+ (unsigned int)(ptr - heap->start),
hdr & FLAG_FREE ? "FREE" : "USED", SIZE(hdr));
if (hdr & FLAG_FREE)
diff --git a/src/include/boot/coreboot_tables.h b/src/include/boot/coreboot_tables.h
index 82cff68..1ce2be7 100644
--- a/src/include/boot/coreboot_tables.h
+++ b/src/include/boot/coreboot_tables.h
@@ -216,6 +216,7 @@
#define LB_TAG_VDAT 0x0015
#define LB_TAG_VBNV 0x0019
#define LB_TAB_VBOOT_HANDOFF 0x0020
+#define LB_TAB_DMA 0x0022
struct lb_range {
uint32_t tag;
uint32_t size;
@@ -331,4 +332,9 @@
void fill_lb_gpios(struct lb_gpios *gpios);
+/* 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 */
diff --git a/src/lib/coreboot_table.c b/src/lib/coreboot_table.c
index b331869..a097cd3 100644
--- a/src/lib/coreboot_table.c
+++ b/src/lib/coreboot_table.c
@@ -79,7 +79,7 @@
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;
rec = lb_last_record(header);
@@ -364,6 +364,8 @@
}
+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)
{
struct lb_record *rec;
@@ -576,6 +578,9 @@
#endif
add_cbmem_pointers(head);
+ /* Add board-specific table entries, if any. */
+ lb_board(head);
+
/* Remember where my valid memory ranges are */
return lb_table_fini(head);
}
diff --git a/src/mainboard/google/kirby/mainboard.c b/src/mainboard/google/kirby/mainboard.c
index baa53bd..0f5feaa 100644
--- a/src/mainboard/google/kirby/mainboard.c
+++ b/src/mainboard/google/kirby/mainboard.c
@@ -45,6 +45,10 @@
#define DRAM_START (CONFIG_SYS_SDRAM_BASE >> 20)
#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 = {
.ha = 1366,
.va = 768,
@@ -441,6 +445,7 @@
/* set up caching for the DRAM */
mmu_config_range(DRAM_START, DRAM_SIZE, DCACHE_WRITEBACK);
+ mmu_config_range(DMA_START >> 20, DMA_SIZE >> 20, DCACHE_OFF);
tlb_invalidate_all();
/* this is going to move, but we must have it now and we're
@@ -459,3 +464,14 @@
.name = "Google ARM Chromebook",
.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;
+}
diff --git a/src/mainboard/google/pit/mainboard.c b/src/mainboard/google/pit/mainboard.c
index f85cf00..f8ab9c3 100644
--- a/src/mainboard/google/pit/mainboard.c
+++ b/src/mainboard/google/pit/mainboard.c
@@ -45,6 +45,10 @@
#define DRAM_START (CONFIG_SYS_SDRAM_BASE >> 20)
#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 = {
.ha = 1366,
.va = 768,
@@ -462,6 +466,7 @@
/* set up caching for the DRAM */
mmu_config_range(DRAM_START, DRAM_SIZE, DCACHE_WRITEBACK);
+ mmu_config_range(DMA_START >> 20, DMA_SIZE >> 20, DCACHE_OFF);
tlb_invalidate_all();
/* this is going to move, but we must have it now and we're
@@ -482,3 +487,14 @@
.name = "Samsung/Google ARM Chromebook",
.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;
+}
diff --git a/src/mainboard/google/snow/mainboard.c b/src/mainboard/google/snow/mainboard.c
index 3167a14..7468d17 100644
--- a/src/mainboard/google/snow/mainboard.c
+++ b/src/mainboard/google/snow/mainboard.c
@@ -46,6 +46,10 @@
#define DRAM_SIZE CONFIG_DRAM_SIZE_MB
#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 = {
.ha = 1366,
.va = 768,
@@ -330,6 +334,7 @@
mmu_init();
mmu_config_range(0, DRAM_START, DCACHE_OFF);
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);
dcache_invalidate_all();
dcache_mmu_enable();
@@ -352,3 +357,14 @@
.name = "Samsung/Google ARM Chromebook",
.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;
+}