Imported Upstream version 16.04
[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
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 #ifdef RTE_LIBRTE_IVSHMEM
86         phys_addr_t ioremap_addr;         /**< Real physical address inside the VM */
87 #endif
88         size_t len;                       /**< Length of the memzone. */
89
90         uint64_t hugepage_sz;             /**< The page size of underlying memory */
91
92         int32_t socket_id;                /**< NUMA socket ID. */
93
94         uint32_t flags;                   /**< Characteristics of this memzone. */
95         uint32_t memseg_id;               /**< Memseg it belongs. */
96 } __attribute__((__packed__));
97
98 /**
99  * Reserve a portion of physical memory.
100  *
101  * This function reserves some memory and returns a pointer to a
102  * correctly filled memzone descriptor. If the allocation cannot be
103  * done, return NULL.
104  *
105  * @param name
106  *   The name of the memzone. If it already exists, the function will
107  *   fail and return NULL.
108  * @param len
109  *   The size of the memory to be reserved. If it
110  *   is 0, the biggest contiguous zone will be reserved.
111  * @param socket_id
112  *   The socket identifier in the case of
113  *   NUMA. The value can be SOCKET_ID_ANY if there is no NUMA
114  *   constraint for the reserved zone.
115  * @param flags
116  *   The flags parameter is used to request memzones to be
117  *   taken from specifically sized hugepages.
118  *   - RTE_MEMZONE_2MB - Reserved from 2MB pages
119  *   - RTE_MEMZONE_1GB - Reserved from 1GB pages
120  *   - RTE_MEMZONE_16MB - Reserved from 16MB pages
121  *   - RTE_MEMZONE_16GB - Reserved from 16GB pages
122  *   - RTE_MEMZONE_256KB - Reserved from 256KB pages
123  *   - RTE_MEMZONE_256MB - Reserved from 256MB pages
124  *   - RTE_MEMZONE_512MB - Reserved from 512MB pages
125  *   - RTE_MEMZONE_4GB - Reserved from 4GB pages
126  *   - RTE_MEMZONE_SIZE_HINT_ONLY - Allow alternative page size to be used if
127  *                                  the requested page size is unavailable.
128  *                                  If this flag is not set, the function
129  *                                  will return error on an unavailable size
130  *                                  request.
131  * @return
132  *   A pointer to a correctly-filled read-only memzone descriptor, or NULL
133  *   on error.
134  *   On error case, rte_errno will be set appropriately:
135  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
136  *    - E_RTE_SECONDARY - function was called from a secondary process instance
137  *    - ENOSPC - the maximum number of memzones has already been allocated
138  *    - EEXIST - a memzone with the same name already exists
139  *    - ENOMEM - no appropriate memory area found in which to create memzone
140  *    - EINVAL - invalid parameters
141  */
142 const struct rte_memzone *rte_memzone_reserve(const char *name,
143                                               size_t len, int socket_id,
144                                               unsigned flags);
145
146 /**
147  * Reserve a portion of physical memory with alignment on a specified
148  * boundary.
149  *
150  * This function reserves some memory with alignment on a specified
151  * boundary, and returns a pointer to a correctly filled memzone
152  * descriptor. If the allocation cannot be done or if the alignment
153  * is not a power of 2, returns NULL.
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  * @param align
182  *   Alignment for resulting memzone. Must be a power of 2.
183  * @return
184  *   A pointer to a correctly-filled read-only memzone descriptor, or NULL
185  *   on error.
186  *   On error case, rte_errno will be set appropriately:
187  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
188  *    - E_RTE_SECONDARY - function was called from a secondary process instance
189  *    - ENOSPC - the maximum number of memzones has already been allocated
190  *    - EEXIST - a memzone with the same name already exists
191  *    - ENOMEM - no appropriate memory area found in which to create memzone
192  *    - EINVAL - invalid parameters
193  */
194 const struct rte_memzone *rte_memzone_reserve_aligned(const char *name,
195                         size_t len, int socket_id,
196                         unsigned flags, unsigned align);
197
198 /**
199  * Reserve a portion of physical memory with specified alignment and
200  * boundary.
201  *
202  * This function reserves some memory with specified alignment and
203  * boundary, and returns a pointer to a correctly filled memzone
204  * descriptor. If the allocation cannot be done or if the alignment
205  * or boundary are not a power of 2, returns NULL.
206  * Memory buffer is reserved in a way, that it wouldn't cross specified
207  * boundary. That implies that requested length should be less or equal
208  * then boundary.
209  *
210  * @param name
211  *   The name of the memzone. If it already exists, the function will
212  *   fail and return NULL.
213  * @param len
214  *   The size of the memory to be reserved. If it
215  *   is 0, the biggest contiguous zone will be reserved.
216  * @param socket_id
217  *   The socket identifier in the case of
218  *   NUMA. The value can be SOCKET_ID_ANY if there is no NUMA
219  *   constraint for the reserved zone.
220  * @param flags
221  *   The flags parameter is used to request memzones to be
222  *   taken from specifically sized hugepages.
223  *   - RTE_MEMZONE_2MB - Reserved from 2MB pages
224  *   - RTE_MEMZONE_1GB - Reserved from 1GB pages
225  *   - RTE_MEMZONE_16MB - Reserved from 16MB pages
226  *   - RTE_MEMZONE_16GB - Reserved from 16GB pages
227  *   - RTE_MEMZONE_256KB - Reserved from 256KB pages
228  *   - RTE_MEMZONE_256MB - Reserved from 256MB pages
229  *   - RTE_MEMZONE_512MB - Reserved from 512MB pages
230  *   - RTE_MEMZONE_4GB - Reserved from 4GB pages
231  *   - RTE_MEMZONE_SIZE_HINT_ONLY - Allow alternative page size to be used if
232  *                                  the requested page size is unavailable.
233  *                                  If this flag is not set, the function
234  *                                  will return error on an unavailable size
235  *                                  request.
236  * @param align
237  *   Alignment for resulting memzone. Must be a power of 2.
238  * @param bound
239  *   Boundary for resulting memzone. Must be a power of 2 or zero.
240  *   Zero value implies no boundary condition.
241  * @return
242  *   A pointer to a correctly-filled read-only memzone descriptor, or NULL
243  *   on error.
244  *   On error case, rte_errno will be set appropriately:
245  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
246  *    - E_RTE_SECONDARY - function was called from a secondary process instance
247  *    - ENOSPC - the maximum number of memzones has already been allocated
248  *    - EEXIST - a memzone with the same name already exists
249  *    - ENOMEM - no appropriate memory area found in which to create memzone
250  *    - EINVAL - invalid parameters
251  */
252 const struct rte_memzone *rte_memzone_reserve_bounded(const char *name,
253                         size_t len, int socket_id,
254                         unsigned flags, unsigned align, unsigned bound);
255
256 /**
257  * Free a memzone.
258  *
259  * Note: an IVSHMEM zone cannot be freed.
260  *
261  * @param mz
262  *   A pointer to the memzone
263  * @return
264  *  -EINVAL - invalid parameter, IVSHMEM memzone.
265  *  0 - success
266  */
267 int rte_memzone_free(const struct rte_memzone *mz);
268
269 /**
270  * Lookup for a memzone.
271  *
272  * Get a pointer to a descriptor of an already reserved memory
273  * zone identified by the name given as an argument.
274  *
275  * @param name
276  *   The name of the memzone.
277  * @return
278  *   A pointer to a read-only memzone descriptor.
279  */
280 const struct rte_memzone *rte_memzone_lookup(const char *name);
281
282 /**
283  * Dump all reserved memzones to the console.
284  *
285  * @param f
286  *   A pointer to a file for output
287  */
288 void rte_memzone_dump(FILE *f);
289
290 /**
291  * Walk list of all memzones
292  *
293  * @param func
294  *   Iterator function
295  * @param arg
296  *   Argument passed to iterator
297  */
298 void rte_memzone_walk(void (*func)(const struct rte_memzone *, void *arg),
299                       void *arg);
300
301 #ifdef __cplusplus
302 }
303 #endif
304
305 #endif /* _RTE_MEMZONE_H_ */