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