mm_pgalloc.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /****************************************************************************
  2. * mm/mm_gran/mm_pgalloc.c
  3. *
  4. * Copyright (C) 2014, 2017 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. /* The state of the page allocator */
  83. static GRAN_HANDLE g_pgalloc;
  84. /****************************************************************************
  85. * Private Functions
  86. ****************************************************************************/
  87. /****************************************************************************
  88. * Public Functions
  89. ****************************************************************************/
  90. /****************************************************************************
  91. * Name: mm_pginitialize
  92. *
  93. * Description:
  94. * Initialize the page allocator.
  95. *
  96. * Input Parameters:
  97. * heap_start - The physical address of the start of memory region that
  98. * will be used for the page allocator heap
  99. * heap_size - The size (in bytes) of the memory region that will be used
  100. * for the page allocator heap.
  101. *
  102. * Returned Value:
  103. * None
  104. *
  105. ****************************************************************************/
  106. void mm_pginitialize(FAR void *heap_start, size_t heap_size)
  107. {
  108. g_pgalloc = gran_initialize(heap_start, heap_size, MM_PGSHIFT, MM_PGSHIFT);
  109. DEBUGASSERT(pg_alloc != NULL);
  110. }
  111. /****************************************************************************
  112. * Name: mm_pgreserve
  113. *
  114. * Description:
  115. * Reserve memory in the page memory pool. This will reserve the pages
  116. * that contain the start and end addresses plus all of the pages
  117. * in between. This should be done early in the initialization sequence
  118. * before any other allocations are made.
  119. *
  120. * Reserved memory can never be allocated (it can be freed however which
  121. * essentially unreserves the memory).
  122. *
  123. * Input Parameters:
  124. * start - The address of the beginning of the region to be reserved.
  125. * size - The size of the region to be reserved
  126. *
  127. * Returned Value:
  128. * None
  129. *
  130. ****************************************************************************/
  131. void mm_pgreserve(uintptr_t start, size_t size)
  132. {
  133. gran_reserve(g_pgalloc, start, size);
  134. }
  135. /****************************************************************************
  136. * Name: mm_pgalloc
  137. *
  138. * Description:
  139. * Allocate page memory from the page memory pool.
  140. *
  141. * Input Parameters:
  142. * npages - The number of pages to allocate, each of size CONFIG_MM_PGSIZE.
  143. *
  144. * Returned Value:
  145. * On success, a non-zero, physical address of the allocated page memory
  146. * is returned. Zero is returned on failure. NOTE: This is an unmapped
  147. * physical address and cannot be used until it is appropriately mapped.
  148. *
  149. ****************************************************************************/
  150. uintptr_t mm_pgalloc(unsigned int npages)
  151. {
  152. return (uintptr_t)gran_alloc(g_pgalloc, (size_t)1 << MM_PGSHIFT);
  153. }
  154. /****************************************************************************
  155. * Name: mm_pgfree
  156. *
  157. * Description:
  158. * Return page memory to the page memory pool.
  159. *
  160. * Input Parameters:
  161. * paddr - A physical address to a page in the page memory pool previously
  162. * allocated by mm_pgalloc.
  163. * npages - The number of contiguous pages to be return to the page memory
  164. * pool, beginning with the page at paddr;
  165. *
  166. * Returned Value:
  167. * None
  168. *
  169. ****************************************************************************/
  170. void mm_pgfree(uintptr_t paddr, unsigned int npages)
  171. {
  172. gran_free(g_pgalloc, (FAR void *)paddr, (size_t)npages << MM_PGSHIFT);
  173. }
  174. /****************************************************************************
  175. * Name: mm_pginfo
  176. *
  177. * Description:
  178. * Return information about the page allocator.
  179. *
  180. * Input Parameters:
  181. * handle - The handle previously returned by gran_initialize
  182. * info - Memory location to return the gran allocator info.
  183. *
  184. * Returned Value:
  185. * Zero (OK) is returned on success; a negated errno value is return on
  186. * any failure.
  187. *
  188. ****************************************************************************/
  189. void mm_pginfo(FAR struct pginfo_s *info)
  190. {
  191. struct graninfo_s graninfo;
  192. DEBUGASSERT(info != NULL);
  193. gran_info(g_pgalloc, &graninfo);
  194. info->ntotal = graninfo.ngranules;
  195. info->nfree = graninfo.nfree;
  196. info->mxfree = graninfo.mxfree;
  197. }
  198. #endif /* CONFIG_MM_PGALLOC */