mm_pgalloc.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /****************************************************************************
  2. * mm/mm_gran/mm_pgalloc.c
  3. *
  4. * Copyright (C) 2014 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <assert.h>
  40. #include <nuttx/mm/gran.h>
  41. #include <nuttx/pgalloc.h>
  42. #include "mm_gran/mm_gran.h"
  43. #ifdef CONFIG_MM_PGALLOC
  44. /****************************************************************************
  45. * Pre-processor Definitions
  46. ****************************************************************************/
  47. /* Configuration ************************************************************/
  48. /* CONFIG_MM_PGALLOC - Enable page allocator support
  49. * CONFIG_MM_PGSIZE - The page size. Must be one of {1024, 2048,
  50. * 4096, 8192, or 16384}. This is easily extensible, but only those
  51. * values are currently support.
  52. * CONFIG_DEBUG_PGALLOC - Just like CONFIG_DEBUG_MM, but only generates
  53. * output from the page allocation logic.
  54. *
  55. * Dependencies: CONFIG_ARCH_USE_MMU and CONFIG_GRAN
  56. */
  57. /* Debug */
  58. #ifdef CONFIG_CPP_HAVE_VARARGS
  59. # ifdef CONFIG_DEBUG_PGALLOC
  60. # define pgaerr(format, ...) _err(format, ##__VA_ARGS__)
  61. # define pgawarn(format, ...) _warn(format, ##__VA_ARGS__)
  62. # define pgainfo(format, ...) _info(format, ##__VA_ARGS__)
  63. # else
  64. # define pgaerr(format, ...) merr(format, ##__VA_ARGS__)
  65. # define pgawarn(format, ...) mwarn(format, ##__VA_ARGS__)
  66. # define pgainfo(format, ...) minfo(format, ##__VA_ARGS__)
  67. # endif
  68. #else
  69. # ifdef CONFIG_DEBUG_PGALLOC
  70. # define pgaerr _err
  71. # define pgawarn _warn
  72. # define pgainfo _info
  73. # else
  74. # define pgaerr merr
  75. # define pgawarn mwarn
  76. # define pgainfo minfo
  77. # endif
  78. #endif
  79. /****************************************************************************
  80. * Private Data
  81. ****************************************************************************/
  82. #ifndef CONFIG_GRAN_SINGLE
  83. /* The state of the page allocator */
  84. static GRAN_HANDLE g_pgalloc;
  85. #endif
  86. /****************************************************************************
  87. * Private Functions
  88. ****************************************************************************/
  89. /****************************************************************************
  90. * Public Functions
  91. ****************************************************************************/
  92. /****************************************************************************
  93. * Name: mm_pginitialize
  94. *
  95. * Description:
  96. * Initialize the page allocator.
  97. *
  98. * Input Parameters:
  99. * heap_start - The physical address of the start of memory region that
  100. * will be used for the page allocator heap
  101. * heap_size - The size (in bytes) of the memory region that will be used
  102. * for the page allocator heap.
  103. *
  104. * Returned Value:
  105. * None
  106. *
  107. ****************************************************************************/
  108. void mm_pginitialize(FAR void *heap_start, size_t heap_size)
  109. {
  110. #ifdef CONFIG_GRAN_SINGLE
  111. int ret;
  112. ret = gran_initialize(heap_start, heap_size, MM_PGSHIFT, MM_PGSHIFT);
  113. DEBUGASSERT(ret == OK);
  114. UNUSED(ret);
  115. #else
  116. g_pgalloc = gran_initialize(heap_start, heap_size, MM_PGSHIFT, MM_PGSHIFT);
  117. DEBUGASSERT(pg_alloc != NULL);
  118. #endif
  119. }
  120. /****************************************************************************
  121. * Name: mm_pgreserve
  122. *
  123. * Description:
  124. * Reserve memory in the page memory pool. This will reserve the pages
  125. * that contain the start and end addresses plus all of the pages
  126. * in between. This should be done early in the initialization sequence
  127. * before any other allocations are made.
  128. *
  129. * Reserved memory can never be allocated (it can be freed however which
  130. * essentially unreserves the memory).
  131. *
  132. * Input Parameters:
  133. * start - The address of the beginning of the region to be reserved.
  134. * size - The size of the region to be reserved
  135. *
  136. * Returned Value:
  137. * None
  138. *
  139. ****************************************************************************/
  140. void mm_pgreserve(uintptr_t start, size_t size)
  141. {
  142. #ifdef CONFIG_GRAN_SINGLE
  143. gran_reserve(start, size);
  144. #else
  145. gran_reserve(g_pgalloc, start, size);
  146. #endif
  147. }
  148. /****************************************************************************
  149. * Name: mm_pgalloc
  150. *
  151. * Description:
  152. * Allocate page memory from the page memory pool.
  153. *
  154. * Input Parameters:
  155. * npages - The number of pages to allocate, each of size CONFIG_MM_PGSIZE.
  156. *
  157. * Returned Value:
  158. * On success, a non-zero, physical address of the allocated page memory
  159. * is returned. Zero is returned on failure. NOTE: This is an unmapped
  160. * physical address and cannot be used until it is appropriately mapped.
  161. *
  162. ****************************************************************************/
  163. uintptr_t mm_pgalloc(unsigned int npages)
  164. {
  165. #ifdef CONFIG_GRAN_SINGLE
  166. return (uintptr_t)gran_alloc((size_t)1 << MM_PGSHIFT);
  167. #else
  168. return (uintptr_t)gran_alloc(g_pgalloc, (size_t)1 << MM_PGSHIFT);
  169. #endif
  170. }
  171. /****************************************************************************
  172. * Name: mm_pgfree
  173. *
  174. * Description:
  175. * Return page memory to the page memory pool.
  176. *
  177. * Input Parameters:
  178. * paddr - A physical address to a page in the page memory pool previously
  179. * allocated by mm_pgalloc.
  180. * npages - The number of contiguous pages to be return to the page memory
  181. * pool, beginning with the page at paddr;
  182. *
  183. * Returned Value:
  184. * None
  185. *
  186. ****************************************************************************/
  187. void mm_pgfree(uintptr_t paddr, unsigned int npages)
  188. {
  189. #ifdef CONFIG_GRAN_SINGLE
  190. gran_free((FAR void *)paddr, (size_t)npages << MM_PGSHIFT);
  191. #else
  192. gran_free(g_pgalloc, (FAR void *)paddr, (size_t)npages << MM_PGSHIFT);
  193. #endif
  194. }
  195. #endif /* CONFIG_MM_PGALLOC */