1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2023-09-07     zmshahaha    the first version
9  */
10 
11 #ifndef __MM_MEMBLOCK_H__
12 #define __MM_MEMBLOCK_H__
13 
14 #include "mm_page.h"
15 #include <rtdef.h>
16 
17 enum mmblk_flag
18 {
19     MEMBLOCK_NONE       = 0x0,      /**< normal region */
20     MEMBLOCK_HOTPLUG    = 0x1,      /**< hotpluggable region */
21     MEMBLOCK_NOMAP      = 0x2,      /**< don't add to kernel direct mapping */
22 };
23 
24 typedef rt_uint32_t mmblk_flag_t;
25 
26 /**
27  * @struct rt_mmblk_reg
28  *
29  * @brief A structure representing a region in rt_memblock
30  */
31 struct rt_mmblk_reg
32 {
33     rt_region_t memreg;             /**< used to indicate the extent of this area */
34     mmblk_flag_t flags;             /**< the flag of the region */
35     rt_bool_t alloc;                /**< is the node allocated */
36     rt_slist_t node;                /**< hook on rt_memblock */
37 };
38 
39 /**
40  * @struct rt_memblock
41  *
42  * @brief A structure representing physical memory block
43  */
44 struct rt_memblock
45 {
46     rt_slist_t reg_list;           /**< store the regions of the memory block */
47 };
48 
49 /**
50  * @brief Add a physical address range to the overall memory region
51  *
52  * @note The newly added region is strictly prohibited from overlapping with existing regions.
53  *
54  * @param name the name of the region
55  * @param start the beginning of the physical address range
56  * @param end the size of the physical address range
57  * @param flags the flags of the region
58  */
59 rt_err_t rt_memblock_add_memory(const char *name, rt_size_t start, rt_size_t end, mmblk_flag_t flags);
60 
61 /**
62  * @brief Add a physical address range to the reserved memory region
63  *
64  * @note The newly added region is strictly prohibited from overlapping with existing regions.
65  *
66  * @param name the name of the reseved region
67  * @param start the beginning of the physical address range
68  * @param end the size of the physical address range
69  * @param flags the flags of the region
70  */
71 rt_err_t rt_memblock_reserve_memory(const char *name, rt_size_t start, rt_size_t end, mmblk_flag_t flags);
72 
73 /**
74  * @brief To conclude the management of memory by the memblock.
75  *
76  * @note This function will free all usable to buddy system and map all memory without
77  * specified MEMBLOCK_NOMAP.
78  */
79 void rt_memblock_setup_memory_environment(void);
80 
81 /**
82  * @brief Get the memory memblock.
83  *
84  * @return Overall memory memblock.
85  */
86 struct rt_memblock *rt_memblock_get_memory(void);
87 
88 /**
89  * @brief Get the reserved memory memblock.
90  *
91  * @return Reserved memory memblock.
92  */
93 struct rt_memblock *rt_memblock_get_reserved(void);
94 
95 #endif /* __MM_MEMBLOCK_H__ */
96