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