summaryrefslogtreecommitdiff
path: root/nuttx/mm
diff options
context:
space:
mode:
authorGregory Nutt <gnutt@nuttx.org>2014-08-23 16:35:52 -0600
committerGregory Nutt <gnutt@nuttx.org>2014-08-23 16:37:16 -0600
commit4fe901851582cf8b7745497231695cfff60e53df (patch)
tree16cffb0c9e598d4c7168bf28c34b422a0b5dbee5 /nuttx/mm
parent7fbe288b3e1fda8640dd9e770b3faa3cd03853b1 (diff)
downloadnuttx-4fe901851582cf8b7745497231695cfff60e53df.tar.gz
nuttx-4fe901851582cf8b7745497231695cfff60e53df.tar.bz2
nuttx-4fe901851582cf8b7745497231695cfff60e53df.zip
include/nuttx/pgalloc.h and mm/mm_pgalloc.c: Add a simple page allocator based on the existing NuttX granule allocator. I am not certain if the granule allocator is sufficiently deterministic for long range use, but it gets get a page allocator in place for testing very quickly.
Diffstat (limited to 'nuttx/mm')
-rw-r--r--nuttx/mm/Kconfig45
-rw-r--r--nuttx/mm/Makefile6
-rw-r--r--nuttx/mm/mm_gran.h16
-rw-r--r--nuttx/mm/mm_granalloc.c2
-rw-r--r--nuttx/mm/mm_graninit.c17
-rw-r--r--nuttx/mm/mm_granreserve.c2
-rw-r--r--nuttx/mm/mm_pgalloc.c223
7 files changed, 291 insertions, 20 deletions
diff --git a/nuttx/mm/Kconfig b/nuttx/mm/Kconfig
index 26f7125e4..0f7d56102 100644
--- a/nuttx/mm/Kconfig
+++ b/nuttx/mm/Kconfig
@@ -101,7 +101,7 @@ config GRAN
bool "Enable Granule Allocator"
default n
---help---
- Enable granual allocator support. Allocations will be aligned to the
+ Enable granule allocator support. Allocations will be aligned to the
granule size; allocations will be in units of the granule size.
Larger granules will give better performance and less overhead but
more losses of memory due to alignment and quantization waste.
@@ -136,5 +136,46 @@ config DEBUG_GRAN
default n
depends on GRAN && DEBUG
---help---
- Just like DEBUG_MM, but only generates ouput from the gran
+ Just like DEBUG_MM, but only generates output from the gran
allocation logic.
+
+config MM_PGALLOC
+ bool "Enable Page Allocator"
+ default n
+ depends on ARCH_HAVE_MMU
+ select GRAN
+ ---help---
+ Enable support for a MMU physical page allocator based on the
+ granule allocator.
+
+if MM_PGALLOC
+
+config MM_PGSIZE
+ int "Page Size"
+ default 4096
+ --help---
+ The MMU page size. Must be one of {1024, 2048, 4096, 8192, or
+ 16384}. This is easily extensible, but only those values are
+ currently support.
+
+config MM_PGPOOL_PADDR
+ hex "Page Memory Pool Start"
+ ---help---
+ Physical address of the start of the page memory pool. This
+ will be aligned to the page size if it is not already aligned.
+
+config MM_PGPOOL_SIZE
+ int "Page Memory Pool Size"
+ ---help---
+ The size of the page memory pool in bytes. This will be aligned
+ if it is not already aligned.
+
+config DEBUG_PGALLOC
+ bool "Page Allocator Debug"
+ default n
+ depends on DEBUG
+ ---help---
+ Just like DEBUG_MM, but only generates output from the page
+ allocation logic.
+
+endif # MM_PGALLOC
diff --git a/nuttx/mm/Makefile b/nuttx/mm/Makefile
index 56b1157d2..f605fc5d4 100644
--- a/nuttx/mm/Makefile
+++ b/nuttx/mm/Makefile
@@ -69,6 +69,12 @@ endif
ifeq ($(CONFIG_GRAN),y)
CSRCS += mm_graninit.c mm_granreserve.c mm_granalloc.c mm_granmark.c
CSRCS += mm_granfree.c mm_grancritical.c
+
+# A page allocator based on the granule allocator
+
+ifeq ($(CONFIG_MM_PGALLOC),y)
+CSRCS += mm_pgalloc.c
+endif
endif
BINDIR ?= bin
diff --git a/nuttx/mm/mm_gran.h b/nuttx/mm/mm_gran.h
index 657aa68b6..8836a5b42 100644
--- a/nuttx/mm/mm_gran.h
+++ b/nuttx/mm/mm_gran.h
@@ -63,19 +63,19 @@
#ifdef CONFIG_CPP_HAVE_VARARGS
# ifdef CONFIG_DEBUG_GRAM
-# define gramdbg(format, ...) dbg(format, ##__VA_ARGS__)
-# define gramvdbg(format, ...) vdbg(format, ##__VA_ARGS__)
+# define grandbg(format, ...) dbg(format, ##__VA_ARGS__)
+# define granvdbg(format, ...) vdbg(format, ##__VA_ARGS__)
# else
-# define gramdbg(format, ...) mdbg(format, ##__VA_ARGS__)
-# define gramvdbg(format, ...) mvdbg(format, ##__VA_ARGS__)
+# define grandbg(format, ...) mdbg(format, ##__VA_ARGS__)
+# define granvdbg(format, ...) mvdbg(format, ##__VA_ARGS__)
# endif
#else
# ifdef CONFIG_DEBUG_GRAM
-# define gramdbg dbg
-# define gramvdbg vdbg
+# define grandbg dbg
+# define granvdbg vdbg
# else
-# define gramdbg (void)
-# define gramvdbg (void)
+# define grandbg (void)
+# define granvdbg (void)
# endif
#endif
diff --git a/nuttx/mm/mm_granalloc.c b/nuttx/mm/mm_granalloc.c
index 71647f954..6e6d375d1 100644
--- a/nuttx/mm/mm_granalloc.c
+++ b/nuttx/mm/mm_granalloc.c
@@ -235,7 +235,7 @@ static inline FAR void *gran_common_alloc(FAR struct gran_s *priv, size_t size)
}
/* Set up for the next time through the loop. Perform a 64
- * bit shift to move to the next gram position andi ncrement
+ * bit shift to move to the next gran position andi ncrement
* to the next candidate allocation address.
*/
diff --git a/nuttx/mm/mm_graninit.c b/nuttx/mm/mm_graninit.c
index 89ccd7166..673e65ac7 100644
--- a/nuttx/mm/mm_graninit.c
+++ b/nuttx/mm/mm_graninit.c
@@ -102,7 +102,7 @@ gran_common_initialize(FAR void *heapstart, size_t heapsize, uint8_t log2gran,
unsigned int alignedsize;
unsigned int ngranules;
- /* Check parameters if debug is on. Note the size of a granual is
+ /* Check parameters if debug is on. Note the size of a granule is
* limited to 2**31 bytes and that the size of the granule must be greater
* than the alignment size.
*/
@@ -159,16 +159,17 @@ gran_common_initialize(FAR void *heapstart, size_t heapsize, uint8_t log2gran,
* granule size (log2gran). Larger granules will give better performance
* and less overhead but more losses of memory due to quantization waste.
* Additional memory waste can occur from alignment; log2align should be
- * set to 0 unless you are using the granule allocator to manage DMA memory
- * and your hardware has specific memory alignment requirements.
+ * set to 0 unless you are using the granule allocator to manage DMA
+ * or page-aligned memory and your hardware has specific memory alignment
+ * requirements.
*
- * Geneneral Usage Summary. This is an example using the GCC section
+ * General Usage Summary. This is an example using the GCC section
* attribute to position a DMA heap in memory (logic in the linker script
* would assign the section .dmaheap to the DMA memory.
*
* FAR uint32_t g_dmaheap[DMAHEAP_SIZE] __attribute__((section(.dmaheap)));
*
- * The heap is created by calling gran_initialize(). Here the granual size
+ * The heap is created by calling gran_initialize(). Here the granule size
* is set to 64 bytes (2**6) and the alignment to 16 bytes (2**4):
*
* GRAN_HANDLE handle = gran_initialize(g_dmaheap, DMAHEAP_SIZE, 6, 4);
@@ -189,9 +190,9 @@ gran_common_initialize(FAR void *heapstart, size_t heapsize, uint8_t log2gran,
* heapstart - Start of the granule allocation heap
* heapsize - Size of heap in bytes
* log2gran - Log base 2 of the size of one granule. 0->1 byte,
- * 1->2 bytes, 2->4 bytes, 3-> 8 bytes, etc.
+ * 1->2 bytes, 2->4 bytes, 3->8 bytes, etc.
* log2align - Log base 2 of required alignment. 0->1 byte,
- * 1->2 bytes, 2->4 bytes, 3-> 8 bytes, etc. Note that
+ * 1->2 bytes, 2->4 bytes, 3->8 bytes, etc. Note that
* log2gran must be greater than or equal to log2align
* so that all contiguous granules in memory will meet
* the minimum alignment requirement. A value of zero
@@ -199,7 +200,7 @@ gran_common_initialize(FAR void *heapstart, size_t heapsize, uint8_t log2gran,
*
* Returned Value:
* On success, a non-NULL handle is returned that may be used with other
- * granual allocator interfaces.
+ * granule allocator interfaces.
*
****************************************************************************/
diff --git a/nuttx/mm/mm_granreserve.c b/nuttx/mm/mm_granreserve.c
index e336a228b..da2a4a38e 100644
--- a/nuttx/mm/mm_granreserve.c
+++ b/nuttx/mm/mm_granreserve.c
@@ -93,7 +93,7 @@ static inline void gran_common_reserve(FAR struct gran_s *priv,
start &= ~mask;
end = (end + mask) & ~mask;
- /* Calculate the new size in granuales */
+ /* Calculate the new size in granules */
ngranules = ((end - start) >> priv->log2gran) + 1;
diff --git a/nuttx/mm/mm_pgalloc.c b/nuttx/mm/mm_pgalloc.c
new file mode 100644
index 000000000..f270ce67a
--- /dev/null
+++ b/nuttx/mm/mm_pgalloc.c
@@ -0,0 +1,223 @@
+/****************************************************************************
+ * mm/mm_pgalloc.c
+ *
+ * Copyright (C) 2014 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <gnutt@nuttx.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name NuttX nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <assert.h>
+
+#include <nuttx/gran.h>
+#include <nuttx/pgalloc.h>
+
+#include "mm_gran.h"
+
+#ifdef CONFIG_MM_PGALLOC
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+/* Configuration ************************************************************/
+/* CONFIG_MM_PGALLOC - Enable page allocator support
+ * CONFIG_MM_PGSIZE - The page size. Must be one of {1024, 2048,
+ * 4096, 8192, or 16384}. This is easily extensible, but only those
+ * values are currently support.
+ * CONFIG_MM_PGPOOL_PADDR - Physical address of the start of the page
+ * memory pool. This will be aligned to the page size if it is not
+ * already aligned.
+ * CONFIG_MM_PGPOOL_SIZE - The size of the page memory pool in bytes. This
+ * will be aligned if it is not already aligned.
+ * CONFIG_DEBUG_PGALLOC - Just like CONFIG_DEBUG_MM, but only generates
+ * output from the page allocation logic.
+ *
+ * Dependencies: CONFIG_ARCH_HAVE_MMU and CONFIG_GRAN
+ */
+
+/* Debug */
+
+#ifdef CONFIG_CPP_HAVE_VARARGS
+# ifdef CONFIG_DEBUG_PGALLOC
+# define pgadbg(format, ...) dbg(format, ##__VA_ARGS__)
+# define pgavdbg(format, ...) vdbg(format, ##__VA_ARGS__)
+# else
+# define pgadbg(format, ...) mdbg(format, ##__VA_ARGS__)
+# define pgavdbg(format, ...) mvdbg(format, ##__VA_ARGS__)
+# endif
+#else
+# ifdef CONFIG_DEBUG_PGALLOC
+# define pgadbg dbg
+# define pgavdbg vdbg
+# else
+# define pgadbg (void)
+# define pgavdbg (void)
+# endif
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+#ifndef CONFIG_GRAN_SINGLE
+/* The state of the page allocator */
+
+static GRAN_HANDLE g_pgalloc;
+#endif
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: mm_pginitialize
+ *
+ * Description:
+ * Initialize the page allocator.
+ *
+ * Input Parameters:
+ * None
+ *
+ * Returned Value:
+ * Mpme
+ *
+ ****************************************************************************/
+
+void mm_pginitialize(void)
+{
+#ifdef CONFIG_GRAN_SINGLE
+ int ret;
+
+ ret = gran_initialize((FAR void *)CONFIG_MM_PGPOOL_PADDR,
+ CONFIG_MM_PGPOOL_SIZE,
+ MM_PGSHIFT, MM_PGSHIFT);
+ DEBUGASSERT(ret == OK);
+ UNUSED(ret);
+
+#else
+ g_pgalloc = gran_initialize((FAR void *)CONFIG_MM_PGPOOL_PADDR,
+ CONFIG_MM_PGPOOL_SIZE,
+ MM_PGSHIFT, MM_PGSHIFT);
+ DEBUGASSERT(pg_alloc != NULL);
+
+#endif
+}
+
+/****************************************************************************
+ * Name: mm_pgreserve
+ *
+ * Description:
+ * Reserve memory in the page memory pool. This will reserve the pages
+ * that contain the start and end addresses plus all of the pages
+ * in between. This should be done early in the initialization sequence
+ * before any other allocations are made.
+ *
+ * Reserved memory can never be allocated (it can be freed however which
+ * essentially unreserves the memory).
+ *
+ * Input Parameters:
+ * start - The address of the beginning of the region to be reserved.
+ * size - The size of the region to be reserved
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+void mm_pgreserve(uintptr_t start, size_t size)
+{
+#ifdef CONFIG_GRAN_SINGLE
+ gran_reserve(start, size);
+#else
+ gran_reserve(g_pgalloc, start, size);
+#endif
+}
+
+/****************************************************************************
+ * Name: mm_pgalloc
+ *
+ * Description:
+ * Allocate page memory from the page memory pool.
+ *
+ * Input Parameters:
+ * npages - The number of pages to allocate, each of size CONFIG_MM_PGSIZE.
+ *
+ * Returned Value:
+ * On success, a non-zero, physical address of the allocated page memory
+ * is returned. Zero is returned on failure. NOTE: This is an unmapped
+ * physical address and cannot be used until it is appropriately mapped.
+ *
+ ****************************************************************************/
+
+uintptr_t mm_pgalloc(unsigned int npages)
+{
+#ifdef CONFIG_GRAN_SINGLE
+ return (uintptr_t)gran_alloc((size_t)1 << MM_PGSHIFT);
+#else
+ return (uintptr_t)gran_alloc(g_pgalloc, (size_t)1 << MM_PGSHIFT);
+#endif
+}
+
+/****************************************************************************
+ * Name: mm_pgfree
+ *
+ * Description:
+ * Return page memory to the page memory pool.
+ *
+ * Input Parameters:
+ * paddr - A physical address to a page in the page memory pool previously
+ * allocated by mm_pgalloc.
+ * npages - The number of contiguous pages to be return to the page memory
+ * pool, beginning with the page at paddr;
+ *
+ * Returned Value:
+ * None
+ *
+ ****************************************************************************/
+
+void mm_pgfree(uintptr_t paddr, unsigned int npages)
+{
+#ifdef CONFIG_GRAN_SINGLE
+ gran_free((FAR void*)paddr, (size_t)npages << MM_PGSHIFT);
+#else
+ gran_free(g_pgalloc, (FAR void*)paddr, (size_t)npages << MM_PGSHIFT);
+#endif
+}
+
+#endif /* CONFIG_MM_PGALLOC */