New upstream version 18.02
[deb_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_memory.h>
27 #include <rte_common.h>
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 #define RTE_MEMZONE_2MB            0x00000001   /**< Use 2MB pages. */
34 #define RTE_MEMZONE_1GB            0x00000002   /**< Use 1GB pages. */
35 #define RTE_MEMZONE_16MB           0x00000100   /**< Use 16MB pages. */
36 #define RTE_MEMZONE_16GB           0x00000200   /**< Use 16GB pages. */
37 #define RTE_MEMZONE_256KB          0x00010000   /**< Use 256KB pages. */
38 #define RTE_MEMZONE_256MB          0x00020000   /**< Use 256MB pages. */
39 #define RTE_MEMZONE_512MB          0x00040000   /**< Use 512MB pages. */
40 #define RTE_MEMZONE_4GB            0x00080000   /**< Use 4GB pages. */
41 #define RTE_MEMZONE_SIZE_HINT_ONLY 0x00000004   /**< Use available page size */
42
43 /**
44  * A structure describing a memzone, which is a contiguous portion of
45  * physical memory identified by a name.
46  */
47 struct rte_memzone {
48
49 #define RTE_MEMZONE_NAMESIZE 32       /**< Maximum length of memory zone name.*/
50         char name[RTE_MEMZONE_NAMESIZE];  /**< Name of the memory zone. */
51
52         RTE_STD_C11
53         union {
54                 phys_addr_t phys_addr;        /**< deprecated - Start physical address. */
55                 rte_iova_t iova;              /**< Start IO address. */
56         };
57         RTE_STD_C11
58         union {
59                 void *addr;                   /**< Start virtual address. */
60                 uint64_t addr_64;             /**< Makes sure addr is always 64-bits */
61         };
62         size_t len;                       /**< Length of the memzone. */
63
64         uint64_t hugepage_sz;             /**< The page size of underlying memory */
65
66         int32_t socket_id;                /**< NUMA socket ID. */
67
68         uint32_t flags;                   /**< Characteristics of this memzone. */
69         uint32_t memseg_id;               /**< Memseg it belongs. */
70 } __attribute__((__packed__));
71
72 /**
73  * Reserve a portion of physical memory.
74  *
75  * This function reserves some memory and returns a pointer to a
76  * correctly filled memzone descriptor. If the allocation cannot be
77  * done, return NULL.
78  *
79  * @param name
80  *   The name of the memzone. If it already exists, the function will
81  *   fail and return NULL.
82  * @param len
83  *   The size of the memory to be reserved. If it
84  *   is 0, the biggest contiguous zone will be reserved.
85  * @param socket_id
86  *   The socket identifier in the case of
87  *   NUMA. The value can be SOCKET_ID_ANY if there is no NUMA
88  *   constraint for the reserved zone.
89  * @param flags
90  *   The flags parameter is used to request memzones to be
91  *   taken from specifically sized hugepages.
92  *   - RTE_MEMZONE_2MB - Reserved from 2MB pages
93  *   - RTE_MEMZONE_1GB - Reserved from 1GB pages
94  *   - RTE_MEMZONE_16MB - Reserved from 16MB pages
95  *   - RTE_MEMZONE_16GB - Reserved from 16GB pages
96  *   - RTE_MEMZONE_256KB - Reserved from 256KB pages
97  *   - RTE_MEMZONE_256MB - Reserved from 256MB pages
98  *   - RTE_MEMZONE_512MB - Reserved from 512MB pages
99  *   - RTE_MEMZONE_4GB - Reserved from 4GB pages
100  *   - RTE_MEMZONE_SIZE_HINT_ONLY - Allow alternative page size to be used if
101  *                                  the requested page size is unavailable.
102  *                                  If this flag is not set, the function
103  *                                  will return error on an unavailable size
104  *                                  request.
105  * @return
106  *   A pointer to a correctly-filled read-only memzone descriptor, or NULL
107  *   on error.
108  *   On error case, rte_errno will be set appropriately:
109  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
110  *    - E_RTE_SECONDARY - function was called from a secondary process instance
111  *    - ENOSPC - the maximum number of memzones has already been allocated
112  *    - EEXIST - a memzone with the same name already exists
113  *    - ENOMEM - no appropriate memory area found in which to create memzone
114  *    - EINVAL - invalid parameters
115  */
116 const struct rte_memzone *rte_memzone_reserve(const char *name,
117                                               size_t len, int socket_id,
118                                               unsigned flags);
119
120 /**
121  * Reserve a portion of physical memory with alignment on a specified
122  * boundary.
123  *
124  * This function reserves some memory with alignment on a specified
125  * boundary, and returns a pointer to a correctly filled memzone
126  * descriptor. If the allocation cannot be done or if the alignment
127  * is not a power of 2, returns NULL.
128  *
129  * @param name
130  *   The name of the memzone. If it already exists, the function will
131  *   fail and return NULL.
132  * @param len
133  *   The size of the memory to be reserved. If it
134  *   is 0, the biggest contiguous zone will be reserved.
135  * @param socket_id
136  *   The socket identifier in the case of
137  *   NUMA. The value can be SOCKET_ID_ANY if there is no NUMA
138  *   constraint for the reserved zone.
139  * @param flags
140  *   The flags parameter is used to request memzones to be
141  *   taken from specifically sized hugepages.
142  *   - RTE_MEMZONE_2MB - Reserved from 2MB pages
143  *   - RTE_MEMZONE_1GB - Reserved from 1GB pages
144  *   - RTE_MEMZONE_16MB - Reserved from 16MB pages
145  *   - RTE_MEMZONE_16GB - Reserved from 16GB pages
146  *   - RTE_MEMZONE_256KB - Reserved from 256KB pages
147  *   - RTE_MEMZONE_256MB - Reserved from 256MB pages
148  *   - RTE_MEMZONE_512MB - Reserved from 512MB pages
149  *   - RTE_MEMZONE_4GB - Reserved from 4GB pages
150  *   - RTE_MEMZONE_SIZE_HINT_ONLY - Allow alternative page size to be used if
151  *                                  the requested page size is unavailable.
152  *                                  If this flag is not set, the function
153  *                                  will return error on an unavailable size
154  *                                  request.
155  * @param align
156  *   Alignment for resulting memzone. Must be a power of 2.
157  * @return
158  *   A pointer to a correctly-filled read-only memzone descriptor, or NULL
159  *   on error.
160  *   On error case, rte_errno will be set appropriately:
161  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
162  *    - E_RTE_SECONDARY - function was called from a secondary process instance
163  *    - ENOSPC - the maximum number of memzones has already been allocated
164  *    - EEXIST - a memzone with the same name already exists
165  *    - ENOMEM - no appropriate memory area found in which to create memzone
166  *    - EINVAL - invalid parameters
167  */
168 const struct rte_memzone *rte_memzone_reserve_aligned(const char *name,
169                         size_t len, int socket_id,
170                         unsigned flags, unsigned align);
171
172 /**
173  * Reserve a portion of physical memory with specified alignment and
174  * boundary.
175  *
176  * This function reserves some memory with specified alignment and
177  * boundary, and returns a pointer to a correctly filled memzone
178  * descriptor. If the allocation cannot be done or if the alignment
179  * or boundary are not a power of 2, returns NULL.
180  * Memory buffer is reserved in a way, that it wouldn't cross specified
181  * boundary. That implies that requested length should be less or equal
182  * then boundary.
183  *
184  * @param name
185  *   The name of the memzone. If it already exists, the function will
186  *   fail and return NULL.
187  * @param len
188  *   The size of the memory to be reserved. If it
189  *   is 0, the biggest contiguous zone will be reserved.
190  * @param socket_id
191  *   The socket identifier in the case of
192  *   NUMA. The value can be SOCKET_ID_ANY if there is no NUMA
193  *   constraint for the reserved zone.
194  * @param flags
195  *   The flags parameter is used to request memzones to be
196  *   taken from specifically sized hugepages.
197  *   - RTE_MEMZONE_2MB - Reserved from 2MB pages
198  *   - RTE_MEMZONE_1GB - Reserved from 1GB pages
199  *   - RTE_MEMZONE_16MB - Reserved from 16MB pages
200  *   - RTE_MEMZONE_16GB - Reserved from 16GB pages
201  *   - RTE_MEMZONE_256KB - Reserved from 256KB pages
202  *   - RTE_MEMZONE_256MB - Reserved from 256MB pages
203  *   - RTE_MEMZONE_512MB - Reserved from 512MB pages
204  *   - RTE_MEMZONE_4GB - Reserved from 4GB pages
205  *   - RTE_MEMZONE_SIZE_HINT_ONLY - Allow alternative page size to be used if
206  *                                  the requested page size is unavailable.
207  *                                  If this flag is not set, the function
208  *                                  will return error on an unavailable size
209  *                                  request.
210  * @param align
211  *   Alignment for resulting memzone. Must be a power of 2.
212  * @param bound
213  *   Boundary for resulting memzone. Must be a power of 2 or zero.
214  *   Zero value implies no boundary condition.
215  * @return
216  *   A pointer to a correctly-filled read-only memzone descriptor, or NULL
217  *   on error.
218  *   On error case, rte_errno will be set appropriately:
219  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
220  *    - E_RTE_SECONDARY - function was called from a secondary process instance
221  *    - ENOSPC - the maximum number of memzones has already been allocated
222  *    - EEXIST - a memzone with the same name already exists
223  *    - ENOMEM - no appropriate memory area found in which to create memzone
224  *    - EINVAL - invalid parameters
225  */
226 const struct rte_memzone *rte_memzone_reserve_bounded(const char *name,
227                         size_t len, int socket_id,
228                         unsigned flags, unsigned align, unsigned bound);
229
230 /**
231  * Free a memzone.
232  *
233  * @param mz
234  *   A pointer to the memzone
235  * @return
236  *  -EINVAL - invalid parameter.
237  *  0 - success
238  */
239 int rte_memzone_free(const struct rte_memzone *mz);
240
241 /**
242  * Lookup for a memzone.
243  *
244  * Get a pointer to a descriptor of an already reserved memory
245  * zone identified by the name given as an argument.
246  *
247  * @param name
248  *   The name of the memzone.
249  * @return
250  *   A pointer to a read-only memzone descriptor.
251  */
252 const struct rte_memzone *rte_memzone_lookup(const char *name);
253
254 /**
255  * Dump all reserved memzones to a file.
256  *
257  * @param f
258  *   A pointer to a file for output
259  */
260 void rte_memzone_dump(FILE *f);
261
262 /**
263  * Walk list of all memzones
264  *
265  * @param func
266  *   Iterator function
267  * @param arg
268  *   Argument passed to iterator
269  */
270 void rte_memzone_walk(void (*func)(const struct rte_memzone *, void *arg),
271                       void *arg);
272
273 #ifdef __cplusplus
274 }
275 #endif
276
277 #endif /* _RTE_MEMZONE_H_ */