1 /*
2  * Copyright (c) 2023 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief Memory Banks Driver APIs
10  *
11  * This contains generic APIs to be used by a system-wide memory management
12  * driver to track page usage within memory banks.
13  *
14  * @note The caller of these functions needs to ensure proper locking
15  *       to protect the data when using these APIs.
16  */
17 
18 #ifndef ZEPHYR_INCLUDE_DRIVERS_MM_DRV_BANK_H
19 #define ZEPHYR_INCLUDE_DRIVERS_MM_DRV_BANK_H
20 
21 #include <zephyr/kernel.h>
22 #include <zephyr/sys/mem_stats.h>
23 #include <stdint.h>
24 
25 /**
26  * @brief Memory Banks Driver APIs
27  * @defgroup mm_drv_bank_apis Memory Banks Driver APIs
28  *
29  * This contains APIs for a system-wide memory management driver to
30  * track page usage within memory banks.
31  *
32  * @note The caller of these functions needs to ensure proper locking
33  *       to protect the data when using these APIs.
34  *
35  * @ingroup memory_management
36  * @{
37  */
38 
39 /**
40  * @brief Information about memory banks.
41  */
42 struct sys_mm_drv_bank {
43 	/** Number of unmapped pages. */
44 	uint32_t  unmapped_pages;
45 
46 	/** Number of mapped pages. */
47 	uint32_t  mapped_pages;
48 
49 	/** Maximum number of mapped pages since last counter reset. */
50 	uint32_t  max_mapped_pages;
51 };
52 
53 /**
54  * @brief Initialize a memory bank's data structure
55  *
56  * The driver may wish to track various information about the memory banks
57  * that it uses. This routine will initialize a generic structure containing
58  * that information. Since at the power on all memory banks are switched on
59  * it will start with all pages mapped. In next phase of driver initialization
60  * unused pages will be unmapped.
61  *
62  * @param[in,out] bank Pointer to the memory bank structure used for tracking
63  * @param[in] bank_pages Number of pages in the memory bank
64  */
65 void sys_mm_drv_bank_init(struct sys_mm_drv_bank *bank, uint32_t bank_pages);
66 
67 /**
68  * @brief Track the mapping of a page in the specified memory bank
69  *
70  * This function is used to update the number of mapped pages within the
71  * specified memory bank.
72  *
73  * @param[in,out] bank Pointer to the memory bank's data structure
74  *
75  * @return The number of pages mapped within the memory bank
76  */
77 uint32_t sys_mm_drv_bank_page_mapped(struct sys_mm_drv_bank *bank);
78 
79 /**
80  * @brief Track the unmapping of a page in the specified memory bank
81  *
82  * This function is used to update the number of unmapped pages within the
83  * specified memory bank.
84  *
85  * @param[in,out] bank Pointer to the memory bank's data structure
86  *
87  * @return The number of unmapped pages within the memory bank
88  */
89 uint32_t sys_mm_drv_bank_page_unmapped(struct sys_mm_drv_bank *bank);
90 
91 /**
92  * @brief Reset the max number of pages mapped in the bank
93  *
94  * This routine is used to reset the maximum number of pages mapped in
95  * the specified memory bank to the current number of pages mapped in
96  * that memory bank.
97  *
98  * @param[in,out] bank Pointer to the memory bank's data structure
99  */
100 void sys_mm_drv_bank_stats_reset_max(struct sys_mm_drv_bank *bank);
101 
102 /**
103  * @brief Retrieve the memory usage stats for the specified memory bank
104  *
105  * This routine extracts the system memory stats from the memory bank.
106  *
107  * @param[in] bank Pointer to the memory bank's data structure
108  * @param[in,out] stats Pointer to memory into which to copy the system memory stats
109  */
110 void sys_mm_drv_bank_stats_get(struct sys_mm_drv_bank *bank,
111 			       struct sys_memory_stats *stats);
112 
113 /**
114  * @}
115  */
116 
117 #endif /* ZEPHYR_INCLUDE_DRIVERS_MM_DRV_BANK_H */
118