eal: simplify meson build of common directory
[dpdk.git] / lib / librte_eal / common / include / rte_memzone.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #ifndef _RTE_MEMZONE_H_
6 #define _RTE_MEMZONE_H_
7
8 /**
9  * @file
10  * RTE Memzone
11  *
12  * The goal of the memzone allocator is to reserve contiguous
13  * portions of physical memory. These zones are identified by a name.
14  *
15  * The memzone descriptors are shared by all partitions and are
16  * located in a known place of physical memory. This zone is accessed
17  * using rte_eal_get_configuration(). The lookup (by name) of a
18  * memory zone can be done in any partition and returns the same
19  * physical address.
20  *
21  * A reserved memory zone cannot be unreserved. The reservation shall
22  * be done at initialization time only.
23  */
24
25 #include <stdio.h>
26 #include <rte_compat.h>
27 #include <rte_memory.h>
28 #include <rte_common.h>
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 #define RTE_MEMZONE_2MB            0x00000001   /**< Use 2MB pages. */
35 #define RTE_MEMZONE_1GB            0x00000002   /**< Use 1GB pages. */
36 #define RTE_MEMZONE_16MB           0x00000100   /**< Use 16MB pages. */
37 #define RTE_MEMZONE_16GB           0x00000200   /**< Use 16GB pages. */
38 #define RTE_MEMZONE_256KB          0x00010000   /**< Use 256KB pages. */
39 #define RTE_MEMZONE_256MB          0x00020000   /**< Use 256MB pages. */
40 #define RTE_MEMZONE_512MB          0x00040000   /**< Use 512MB pages. */
41 #define RTE_MEMZONE_4GB            0x00080000   /**< Use 4GB pages. */
42 #define RTE_MEMZONE_SIZE_HINT_ONLY 0x00000004   /**< Use available page size */
43 #define RTE_MEMZONE_IOVA_CONTIG    0x00100000   /**< Ask for IOVA-contiguous memzone. */
44
45 /**
46  * A structure describing a memzone, which is a contiguous portion of
47  * physical memory identified by a name.
48  */
49 struct rte_memzone {
50
51 #define RTE_MEMZONE_NAMESIZE 32       /**< Maximum length of memory zone name.*/
52         char name[RTE_MEMZONE_NAMESIZE];  /**< Name of the memory zone. */
53
54         RTE_STD_C11
55         union {
56                 phys_addr_t phys_addr;        /**< deprecated - Start physical address. */
57                 rte_iova_t iova;              /**< Start IO address. */
58         };
59         RTE_STD_C11
60         union {
61                 void *addr;                   /**< Start virtual address. */
62                 uint64_t addr_64;             /**< Makes sure addr is always 64-bits */
63         };
64         size_t len;                       /**< Length of the memzone. */
65
66         uint64_t hugepage_sz;             /**< The page size of underlying memory */
67
68         int32_t socket_id;                /**< NUMA socket ID. */
69
70         uint32_t flags;                   /**< Characteristics of this memzone. */
71 } __attribute__((__packed__));
72
73 /**
74  * Reserve a portion of physical memory.
75  *
76  * This function reserves some memory and returns a pointer to a
77  * correctly filled memzone descriptor. If the allocation cannot be
78  * done, return NULL.
79  *
80  * @note Reserving memzones with len set to 0 will only attempt to allocate
81  *   memzones from memory that is already available. It will not trigger any
82  *   new allocations.
83  *
84  * @note: When reserving memzones with len set to 0, it is preferable to also
85  *   set a valid socket_id. Setting socket_id to SOCKET_ID_ANY is supported, but
86  *   will likely not yield expected results. Specifically, the resulting memzone
87  *   may not necessarily be the biggest memzone available, but rather biggest
88  *   memzone available on socket id corresponding to an lcore from which
89  *   reservation was called.
90  *
91  * @param name
92  *   The name of the memzone. If it already exists, the function will
93  *   fail and return NULL.
94  * @param len
95  *   The size of the memory to be reserved. If it
96  *   is 0, the biggest contiguous zone will be reserved.
97  * @param socket_id
98  *   The socket identifier in the case of
99  *   NUMA. The value can be SOCKET_ID_ANY if there is no NUMA
100  *   constraint for the reserved zone.
101  * @param flags
102  *   The flags parameter is used to request memzones to be
103  *   taken from specifically sized hugepages.
104  *   - RTE_MEMZONE_2MB - Reserved from 2MB pages
105  *   - RTE_MEMZONE_1GB - Reserved from 1GB pages
106  *   - RTE_MEMZONE_16MB - Reserved from 16MB pages
107  *   - RTE_MEMZONE_16GB - Reserved from 16GB pages
108  *   - RTE_MEMZONE_256KB - Reserved from 256KB pages
109  *   - RTE_MEMZONE_256MB - Reserved from 256MB pages
110  *   - RTE_MEMZONE_512MB - Reserved from 512MB pages
111  *   - RTE_MEMZONE_4GB - Reserved from 4GB pages
112  *   - RTE_MEMZONE_SIZE_HINT_ONLY - Allow alternative page size to be used if
113  *                                  the requested page size is unavailable.
114  *                                  If this flag is not set, the function
115  *                                  will return error on an unavailable size
116  *                                  request.
117  *   - RTE_MEMZONE_IOVA_CONTIG - Ensure reserved memzone is IOVA-contiguous.
118  *                               This option should be used when allocating
119  *                               memory intended for hardware rings etc.
120  * @return
121  *   A pointer to a correctly-filled read-only memzone descriptor, or NULL
122  *   on error.
123  *   On error case, rte_errno will be set appropriately:
124  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
125  *    - E_RTE_SECONDARY - function was called from a secondary process instance
126  *    - ENOSPC - the maximum number of memzones has already been allocated
127  *    - EEXIST - a memzone with the same name already exists
128  *    - ENOMEM - no appropriate memory area found in which to create memzone
129  *    - EINVAL - invalid parameters
130  */
131 const struct rte_memzone *rte_memzone_reserve(const char *name,
132                                               size_t len, int socket_id,
133                                               unsigned flags);
134
135 /**
136  * Reserve a portion of physical memory with alignment on a specified
137  * boundary.
138  *
139  * This function reserves some memory with alignment on a specified
140  * boundary, and returns a pointer to a correctly filled memzone
141  * descriptor. If the allocation cannot be done or if the alignment
142  * is not a power of 2, returns NULL.
143  *
144  * @note Reserving memzones with len set to 0 will only attempt to allocate
145  *   memzones from memory that is already available. It will not trigger any
146  *   new allocations.
147  *
148  * @note: When reserving memzones with len set to 0, it is preferable to also
149  *   set a valid socket_id. Setting socket_id to SOCKET_ID_ANY is supported, but
150  *   will likely not yield expected results. Specifically, the resulting memzone
151  *   may not necessarily be the biggest memzone available, but rather biggest
152  *   memzone available on socket id corresponding to an lcore from which
153  *   reservation was called.
154  *
155  * @param name
156  *   The name of the memzone. If it already exists, the function will
157  *   fail and return NULL.
158  * @param len
159  *   The size of the memory to be reserved. If it
160  *   is 0, the biggest contiguous zone will be reserved.
161  * @param socket_id
162  *   The socket identifier in the case of
163  *   NUMA. The value can be SOCKET_ID_ANY if there is no NUMA
164  *   constraint for the reserved zone.
165  * @param flags
166  *   The flags parameter is used to request memzones to be
167  *   taken from specifically sized hugepages.
168  *   - RTE_MEMZONE_2MB - Reserved from 2MB pages
169  *   - RTE_MEMZONE_1GB - Reserved from 1GB pages
170  *   - RTE_MEMZONE_16MB - Reserved from 16MB pages
171  *   - RTE_MEMZONE_16GB - Reserved from 16GB pages
172  *   - RTE_MEMZONE_256KB - Reserved from 256KB pages
173  *   - RTE_MEMZONE_256MB - Reserved from 256MB pages
174  *   - RTE_MEMZONE_512MB - Reserved from 512MB pages
175  *   - RTE_MEMZONE_4GB - Reserved from 4GB pages
176  *   - RTE_MEMZONE_SIZE_HINT_ONLY - Allow alternative page size to be used if
177  *                                  the requested page size is unavailable.
178  *                                  If this flag is not set, the function
179  *                                  will return error on an unavailable size
180  *                                  request.
181  *   - RTE_MEMZONE_IOVA_CONTIG - Ensure reserved memzone is IOVA-contiguous.
182  *                               This option should be used when allocating
183  *                               memory intended for hardware rings etc.
184  * @param align
185  *   Alignment for resulting memzone. Must be a power of 2.
186  * @return
187  *   A pointer to a correctly-filled read-only memzone descriptor, or NULL
188  *   on error.
189  *   On error case, rte_errno will be set appropriately:
190  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
191  *    - E_RTE_SECONDARY - function was called from a secondary process instance
192  *    - ENOSPC - the maximum number of memzones has already been allocated
193  *    - EEXIST - a memzone with the same name already exists
194  *    - ENOMEM - no appropriate memory area found in which to create memzone
195  *    - EINVAL - invalid parameters
196  */
197 const struct rte_memzone *rte_memzone_reserve_aligned(const char *name,
198                         size_t len, int socket_id,
199                         unsigned flags, unsigned align);
200
201 /**
202  * Reserve a portion of physical memory with specified alignment and
203  * boundary.
204  *
205  * This function reserves some memory with specified alignment and
206  * boundary, and returns a pointer to a correctly filled memzone
207  * descriptor. If the allocation cannot be done or if the alignment
208  * or boundary are not a power of 2, returns NULL.
209  * Memory buffer is reserved in a way, that it wouldn't cross specified
210  * boundary. That implies that requested length should be less or equal
211  * then boundary.
212  *
213  * @note Reserving memzones with len set to 0 will only attempt to allocate
214  *   memzones from memory that is already available. It will not trigger any
215  *   new allocations.
216  *
217  * @note: When reserving memzones with len set to 0, it is preferable to also
218  *   set a valid socket_id. Setting socket_id to SOCKET_ID_ANY is supported, but
219  *   will likely not yield expected results. Specifically, the resulting memzone
220  *   may not necessarily be the biggest memzone available, but rather biggest
221  *   memzone available on socket id corresponding to an lcore from which
222  *   reservation was called.
223  *
224  * @param name
225  *   The name of the memzone. If it already exists, the function will
226  *   fail and return NULL.
227  * @param len
228  *   The size of the memory to be reserved. If it
229  *   is 0, the biggest contiguous zone will be reserved.
230  * @param socket_id
231  *   The socket identifier in the case of
232  *   NUMA. The value can be SOCKET_ID_ANY if there is no NUMA
233  *   constraint for the reserved zone.
234  * @param flags
235  *   The flags parameter is used to request memzones to be
236  *   taken from specifically sized hugepages.
237  *   - RTE_MEMZONE_2MB - Reserved from 2MB pages
238  *   - RTE_MEMZONE_1GB - Reserved from 1GB pages
239  *   - RTE_MEMZONE_16MB - Reserved from 16MB pages
240  *   - RTE_MEMZONE_16GB - Reserved from 16GB pages
241  *   - RTE_MEMZONE_256KB - Reserved from 256KB pages
242  *   - RTE_MEMZONE_256MB - Reserved from 256MB pages
243  *   - RTE_MEMZONE_512MB - Reserved from 512MB pages
244  *   - RTE_MEMZONE_4GB - Reserved from 4GB pages
245  *   - RTE_MEMZONE_SIZE_HINT_ONLY - Allow alternative page size to be used if
246  *                                  the requested page size is unavailable.
247  *                                  If this flag is not set, the function
248  *                                  will return error on an unavailable size
249  *                                  request.
250  *   - RTE_MEMZONE_IOVA_CONTIG - Ensure reserved memzone is IOVA-contiguous.
251  *                               This option should be used when allocating
252  *                               memory intended for hardware rings etc.
253  * @param align
254  *   Alignment for resulting memzone. Must be a power of 2.
255  * @param bound
256  *   Boundary for resulting memzone. Must be a power of 2 or zero.
257  *   Zero value implies no boundary condition.
258  * @return
259  *   A pointer to a correctly-filled read-only memzone descriptor, or NULL
260  *   on error.
261  *   On error case, rte_errno will be set appropriately:
262  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
263  *    - E_RTE_SECONDARY - function was called from a secondary process instance
264  *    - ENOSPC - the maximum number of memzones has already been allocated
265  *    - EEXIST - a memzone with the same name already exists
266  *    - ENOMEM - no appropriate memory area found in which to create memzone
267  *    - EINVAL - invalid parameters
268  */
269 const struct rte_memzone *rte_memzone_reserve_bounded(const char *name,
270                         size_t len, int socket_id,
271                         unsigned flags, unsigned align, unsigned bound);
272
273 /**
274  * Free a memzone.
275  *
276  * @param mz
277  *   A pointer to the memzone
278  * @return
279  *  -EINVAL - invalid parameter.
280  *  0 - success
281  */
282 int rte_memzone_free(const struct rte_memzone *mz);
283
284 /**
285  * Lookup for a memzone.
286  *
287  * Get a pointer to a descriptor of an already reserved memory
288  * zone identified by the name given as an argument.
289  *
290  * @param name
291  *   The name of the memzone.
292  * @return
293  *   A pointer to a read-only memzone descriptor.
294  */
295 const struct rte_memzone *rte_memzone_lookup(const char *name);
296
297 /**
298  * Dump all reserved memzones to a file.
299  *
300  * @param f
301  *   A pointer to a file for output
302  */
303 void rte_memzone_dump(FILE *f);
304
305 /**
306  * Walk list of all memzones
307  *
308  * @param func
309  *   Iterator function
310  * @param arg
311  *   Argument passed to iterator
312  */
313 void rte_memzone_walk(void (*func)(const struct rte_memzone *, void *arg),
314                       void *arg);
315
316 #ifdef __cplusplus
317 }
318 #endif
319
320 #endif /* _RTE_MEMZONE_H_ */