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