first public release
[dpdk.git] / lib / librte_eal / common / include / rte_memzone.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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  *  version: DPDK.L.1.2.3-3
34  */
35
36 #ifndef _RTE_MEMZONE_H_
37 #define _RTE_MEMZONE_H_
38
39 /**
40  * @file
41  * RTE Memzone
42  *
43  * The goal of the memzone allocator is to reserve contiguous
44  * portions of physical memory. These zones are identified by a name.
45  *
46  * The memzone descriptors are shared by all partitions and are
47  * located in a known place of physical memory. This zone is accessed
48  * using rte_eal_get_configuration(). The lookup (by name) of a
49  * memory zone can be done in any partition and returns the same
50  * physical address.
51  *
52  * A reserved memory zone cannot be unreserved. The reservation shall
53  * be done at initialization time only.
54  */
55
56 #include <rte_memory.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_SIZE_HINT_ONLY 0x00000004   /**< Use available page size */
65
66 /**
67  * A structure describing a memzone, which is a contiguous portion of
68  * physical memory identified by a name.
69  */
70 struct rte_memzone {
71
72 #define RTE_MEMZONE_NAMESIZE 32       /**< Maximum length of memory zone name.*/
73         char name[RTE_MEMZONE_NAMESIZE];  /**< Name of the memory zone. */
74
75         phys_addr_t phys_addr;            /**< Start physical address. */
76         union {
77                 void *addr;                   /**< Start virtual address. */
78                 uint64_t addr_64;             /**< Makes sure addr is always 64-bits */
79         };
80         uint64_t len;                     /**< Length of the memzone. */
81
82         uint64_t hugepage_sz;             /**< The page size of underlying memory */
83
84         int32_t socket_id;                /**< NUMA socket ID. */
85
86         uint32_t flags;                   /**< Characteristics of this memzone. */
87 } __attribute__((__packed__));
88
89 /**
90  * Reserve a portion of physical memory.
91  *
92  * This function reserves some memory and returns a pointer to a
93  * correctly filled memzone descriptor. If the allocation cannot be
94  * done, return NULL. Note: A reserved zone cannot be freed.
95  *
96  * @param name
97  *   The name of the memzone. If it already exists, the function will
98  *   fail and return NULL.
99  * @param len
100  *   The size of the memory to be reserved. If it
101  *   is 0, the biggest contiguous zone will be reserved.
102  * @param socket_id
103  *   The socket identifier in the case of
104  *   NUMA. The value can be SOCKET_ID_ANY if there is no NUMA
105  *   constraint for the reserved zone.
106  * @param flags
107  *   The flags parameter is used to request memzones to be
108  *   taken from 1GB or 2MB hugepages.
109  *   - RTE_MEMZONE_2MB - Reserve from 2MB pages
110  *   - RTE_MEMZONE_1GB - Reserve from 1GB pages
111  *   - RTE_MEMZONE_SIZE_HINT_ONLY - Allow alternative page size to be used if
112  *                                  the requested page size is unavailable.
113  *                                  If this flag is not set, the function
114  *                                  will return error on an unavailable size
115  *                                  request.
116  * @return
117  *   A pointer to a correctly-filled read-only memzone descriptor, or NULL
118  *   on error.
119  *   On error case, rte_errno will be set appropriately:
120  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
121  *    - E_RTE_SECONDARY - function was called from a secondary process instance
122  *    - ENOSPC - the maximum number of memzones has already been allocated
123  *    - EEXIST - a memzone with the same name already exists
124  *    - ENOMEM - no appropriate memory area found in which to create memzone
125  *    - EINVAL - invalid parameters
126  */
127 const struct rte_memzone *rte_memzone_reserve(const char *name,
128                                               uint64_t len, int socket_id,
129                                               unsigned flags);
130
131 /**
132  * Reserve a portion of physical memory with alignment on a specified
133  * boundary.
134  *
135  * This function reserves some memory with alignment on a specified
136  * boundary, and returns a pointer to a correctly filled memzone
137  * descriptor. If the allocation cannot be done or if the alignment
138  * is not a power of 2, returns NULL.
139  * Note: A reserved zone cannot be freed.
140  *
141  * @param name
142  *   The name of the memzone. If it already exists, the function will
143  *   fail and return NULL.
144  * @param len
145  *   The size of the memory to be reserved. If it
146  *   is 0, the biggest contiguous zone will be reserved.
147  * @param align
148  *   Alignment for resulting memzone. Must be a power of 2.
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  * @return
164  *   A pointer to a correctly-filled read-only memzone descriptor, or NULL
165  *   on error.
166  *   On error case, rte_errno will be set appropriately:
167  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
168  *    - E_RTE_SECONDARY - function was called from a secondary process instance
169  *    - ENOSPC - the maximum number of memzones has already been allocated
170  *    - EEXIST - a memzone with the same name already exists
171  *    - ENOMEM - no appropriate memory area found in which to create memzone
172  *    - EINVAL - invalid parameters
173  */
174 const struct rte_memzone *rte_memzone_reserve_aligned(const char *name,
175                                               uint64_t len, int socket_id, unsigned flags,
176                                               unsigned align);
177
178 /**
179  * Lookup for a memzone.
180  *
181  * Get a pointer to a descriptor of an already reserved memory
182  * zone identified by the name given as an argument.
183  *
184  * @param name
185  *   The name of the memzone.
186  * @return
187  *   A pointer to a read-only memzone descriptor.
188  */
189 const struct rte_memzone *rte_memzone_lookup(const char *name);
190
191 /**
192  * Dump all reserved memzones to the console.
193  */
194 void rte_memzone_dump(void);
195
196 #ifdef __cplusplus
197 }
198 #endif
199
200 #endif /* _RTE_MEMZONE_H_ */