New upstream version 17.11-rc3
[deb_dpdk.git] / lib / librte_eal / common / include / rte_memzone.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
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  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * 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  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software 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 FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _RTE_MEMZONE_H_
35 #define _RTE_MEMZONE_H_
36
37 /**
38  * @file
39  * RTE Memzone
40  *
41  * The goal of the memzone allocator is to reserve contiguous
42  * portions of physical memory. These zones are identified by a name.
43  *
44  * The memzone descriptors are shared by all partitions and are
45  * located in a known place of physical memory. This zone is accessed
46  * using rte_eal_get_configuration(). The lookup (by name) of a
47  * memory zone can be done in any partition and returns the same
48  * physical address.
49  *
50  * A reserved memory zone cannot be unreserved. The reservation shall
51  * be done at initialization time only.
52  */
53
54 #include <stdio.h>
55 #include <rte_memory.h>
56 #include <rte_common.h>
57
58 #ifdef __cplusplus
59 extern "C" {
60 #endif
61
62 #define RTE_MEMZONE_2MB            0x00000001   /**< Use 2MB pages. */
63 #define RTE_MEMZONE_1GB            0x00000002   /**< Use 1GB pages. */
64 #define RTE_MEMZONE_16MB           0x00000100   /**< Use 16MB pages. */
65 #define RTE_MEMZONE_16GB           0x00000200   /**< Use 16GB pages. */
66 #define RTE_MEMZONE_256KB          0x00010000   /**< Use 256KB pages. */
67 #define RTE_MEMZONE_256MB          0x00020000   /**< Use 256MB pages. */
68 #define RTE_MEMZONE_512MB          0x00040000   /**< Use 512MB pages. */
69 #define RTE_MEMZONE_4GB            0x00080000   /**< Use 4GB pages. */
70 #define RTE_MEMZONE_SIZE_HINT_ONLY 0x00000004   /**< Use available page size */
71
72 /**
73  * A structure describing a memzone, which is a contiguous portion of
74  * physical memory identified by a name.
75  */
76 struct rte_memzone {
77
78 #define RTE_MEMZONE_NAMESIZE 32       /**< Maximum length of memory zone name.*/
79         char name[RTE_MEMZONE_NAMESIZE];  /**< Name of the memory zone. */
80
81         RTE_STD_C11
82         union {
83                 phys_addr_t phys_addr;        /**< deprecated - Start physical address. */
84                 rte_iova_t iova;              /**< Start IO address. */
85         };
86         RTE_STD_C11
87         union {
88                 void *addr;                   /**< Start virtual address. */
89                 uint64_t addr_64;             /**< Makes sure addr is always 64-bits */
90         };
91         size_t len;                       /**< Length of the memzone. */
92
93         uint64_t hugepage_sz;             /**< The page size of underlying memory */
94
95         int32_t socket_id;                /**< NUMA socket ID. */
96
97         uint32_t flags;                   /**< Characteristics of this memzone. */
98         uint32_t memseg_id;               /**< Memseg it belongs. */
99 } __attribute__((__packed__));
100
101 /**
102  * Reserve a portion of physical memory.
103  *
104  * This function reserves some memory and returns a pointer to a
105  * correctly filled memzone descriptor. If the allocation cannot be
106  * done, return NULL.
107  *
108  * @param name
109  *   The name of the memzone. If it already exists, the function will
110  *   fail and return NULL.
111  * @param len
112  *   The size of the memory to be reserved. If it
113  *   is 0, the biggest contiguous zone will be reserved.
114  * @param socket_id
115  *   The socket identifier in the case of
116  *   NUMA. The value can be SOCKET_ID_ANY if there is no NUMA
117  *   constraint for the reserved zone.
118  * @param flags
119  *   The flags parameter is used to request memzones to be
120  *   taken from specifically sized hugepages.
121  *   - RTE_MEMZONE_2MB - Reserved from 2MB pages
122  *   - RTE_MEMZONE_1GB - Reserved from 1GB pages
123  *   - RTE_MEMZONE_16MB - Reserved from 16MB pages
124  *   - RTE_MEMZONE_16GB - Reserved from 16GB pages
125  *   - RTE_MEMZONE_256KB - Reserved from 256KB pages
126  *   - RTE_MEMZONE_256MB - Reserved from 256MB pages
127  *   - RTE_MEMZONE_512MB - Reserved from 512MB pages
128  *   - RTE_MEMZONE_4GB - Reserved from 4GB pages
129  *   - RTE_MEMZONE_SIZE_HINT_ONLY - Allow alternative page size to be used if
130  *                                  the requested page size is unavailable.
131  *                                  If this flag is not set, the function
132  *                                  will return error on an unavailable size
133  *                                  request.
134  * @return
135  *   A pointer to a correctly-filled read-only memzone descriptor, or NULL
136  *   on error.
137  *   On error case, rte_errno will be set appropriately:
138  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
139  *    - E_RTE_SECONDARY - function was called from a secondary process instance
140  *    - ENOSPC - the maximum number of memzones has already been allocated
141  *    - EEXIST - a memzone with the same name already exists
142  *    - ENOMEM - no appropriate memory area found in which to create memzone
143  *    - EINVAL - invalid parameters
144  */
145 const struct rte_memzone *rte_memzone_reserve(const char *name,
146                                               size_t len, int socket_id,
147                                               unsigned flags);
148
149 /**
150  * Reserve a portion of physical memory with alignment on a specified
151  * boundary.
152  *
153  * This function reserves some memory with alignment on a specified
154  * boundary, and returns a pointer to a correctly filled memzone
155  * descriptor. If the allocation cannot be done or if the alignment
156  * is not a power of 2, returns NULL.
157  *
158  * @param name
159  *   The name of the memzone. If it already exists, the function will
160  *   fail and return NULL.
161  * @param len
162  *   The size of the memory to be reserved. If it
163  *   is 0, the biggest contiguous zone will be reserved.
164  * @param socket_id
165  *   The socket identifier in the case of
166  *   NUMA. The value can be SOCKET_ID_ANY if there is no NUMA
167  *   constraint for the reserved zone.
168  * @param flags
169  *   The flags parameter is used to request memzones to be
170  *   taken from specifically sized hugepages.
171  *   - RTE_MEMZONE_2MB - Reserved from 2MB pages
172  *   - RTE_MEMZONE_1GB - Reserved from 1GB pages
173  *   - RTE_MEMZONE_16MB - Reserved from 16MB pages
174  *   - RTE_MEMZONE_16GB - Reserved from 16GB pages
175  *   - RTE_MEMZONE_256KB - Reserved from 256KB pages
176  *   - RTE_MEMZONE_256MB - Reserved from 256MB pages
177  *   - RTE_MEMZONE_512MB - Reserved from 512MB pages
178  *   - RTE_MEMZONE_4GB - Reserved from 4GB pages
179  *   - RTE_MEMZONE_SIZE_HINT_ONLY - Allow alternative page size to be used if
180  *                                  the requested page size is unavailable.
181  *                                  If this flag is not set, the function
182  *                                  will return error on an unavailable size
183  *                                  request.
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  * @param name
214  *   The name of the memzone. If it already exists, the function will
215  *   fail and return NULL.
216  * @param len
217  *   The size of the memory to be reserved. If it
218  *   is 0, the biggest contiguous zone will be reserved.
219  * @param socket_id
220  *   The socket identifier in the case of
221  *   NUMA. The value can be SOCKET_ID_ANY if there is no NUMA
222  *   constraint for the reserved zone.
223  * @param flags
224  *   The flags parameter is used to request memzones to be
225  *   taken from specifically sized hugepages.
226  *   - RTE_MEMZONE_2MB - Reserved from 2MB pages
227  *   - RTE_MEMZONE_1GB - Reserved from 1GB pages
228  *   - RTE_MEMZONE_16MB - Reserved from 16MB pages
229  *   - RTE_MEMZONE_16GB - Reserved from 16GB pages
230  *   - RTE_MEMZONE_256KB - Reserved from 256KB pages
231  *   - RTE_MEMZONE_256MB - Reserved from 256MB pages
232  *   - RTE_MEMZONE_512MB - Reserved from 512MB pages
233  *   - RTE_MEMZONE_4GB - Reserved from 4GB pages
234  *   - RTE_MEMZONE_SIZE_HINT_ONLY - Allow alternative page size to be used if
235  *                                  the requested page size is unavailable.
236  *                                  If this flag is not set, the function
237  *                                  will return error on an unavailable size
238  *                                  request.
239  * @param align
240  *   Alignment for resulting memzone. Must be a power of 2.
241  * @param bound
242  *   Boundary for resulting memzone. Must be a power of 2 or zero.
243  *   Zero value implies no boundary condition.
244  * @return
245  *   A pointer to a correctly-filled read-only memzone descriptor, or NULL
246  *   on error.
247  *   On error case, rte_errno will be set appropriately:
248  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
249  *    - E_RTE_SECONDARY - function was called from a secondary process instance
250  *    - ENOSPC - the maximum number of memzones has already been allocated
251  *    - EEXIST - a memzone with the same name already exists
252  *    - ENOMEM - no appropriate memory area found in which to create memzone
253  *    - EINVAL - invalid parameters
254  */
255 const struct rte_memzone *rte_memzone_reserve_bounded(const char *name,
256                         size_t len, int socket_id,
257                         unsigned flags, unsigned align, unsigned bound);
258
259 /**
260  * Free a memzone.
261  *
262  * @param mz
263  *   A pointer to the memzone
264  * @return
265  *  -EINVAL - invalid parameter.
266  *  0 - success
267  */
268 int rte_memzone_free(const struct rte_memzone *mz);
269
270 /**
271  * Lookup for a memzone.
272  *
273  * Get a pointer to a descriptor of an already reserved memory
274  * zone identified by the name given as an argument.
275  *
276  * @param name
277  *   The name of the memzone.
278  * @return
279  *   A pointer to a read-only memzone descriptor.
280  */
281 const struct rte_memzone *rte_memzone_lookup(const char *name);
282
283 /**
284  * Dump all reserved memzones to a file.
285  *
286  * @param f
287  *   A pointer to a file for output
288  */
289 void rte_memzone_dump(FILE *f);
290
291 /**
292  * Walk list of all memzones
293  *
294  * @param func
295  *   Iterator function
296  * @param arg
297  *   Argument passed to iterator
298  */
299 void rte_memzone_walk(void (*func)(const struct rte_memzone *, void *arg),
300                       void *arg);
301
302 #ifdef __cplusplus
303 }
304 #endif
305
306 #endif /* _RTE_MEMZONE_H_ */