mm_pgalloc.c 7.3 KB

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