12e0ebb077ad8d9ddb4ee198b32b5e228fb8aaf9
[dpdk.git] / lib / librte_eal / common / include / rte_memory.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_MEMORY_H_
35 #define _RTE_MEMORY_H_
36
37 /**
38  * @file
39  *
40  * Memory-related RTE API.
41  */
42
43 #include <stdint.h>
44 #include <stddef.h>
45 #include <stdio.h>
46
47 #ifdef RTE_EXEC_ENV_LINUXAPP
48 #include <exec-env/rte_dom0_common.h>
49 #endif
50
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54
55 #include <rte_common.h>
56
57 __extension__
58 enum rte_page_sizes {
59         RTE_PGSIZE_4K    = 1ULL << 12,
60         RTE_PGSIZE_64K   = 1ULL << 16,
61         RTE_PGSIZE_256K  = 1ULL << 18,
62         RTE_PGSIZE_2M    = 1ULL << 21,
63         RTE_PGSIZE_16M   = 1ULL << 24,
64         RTE_PGSIZE_256M  = 1ULL << 28,
65         RTE_PGSIZE_512M  = 1ULL << 29,
66         RTE_PGSIZE_1G    = 1ULL << 30,
67         RTE_PGSIZE_4G    = 1ULL << 32,
68         RTE_PGSIZE_16G   = 1ULL << 34,
69 };
70
71 #define SOCKET_ID_ANY -1                    /**< Any NUMA socket. */
72 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1) /**< Cache line mask. */
73
74 #define RTE_CACHE_LINE_ROUNDUP(size) \
75         (RTE_CACHE_LINE_SIZE * ((size + RTE_CACHE_LINE_SIZE - 1) / RTE_CACHE_LINE_SIZE))
76 /**< Return the first cache-aligned value greater or equal to size. */
77
78 /**< Cache line size in terms of log2 */
79 #if RTE_CACHE_LINE_SIZE == 64
80 #define RTE_CACHE_LINE_SIZE_LOG2 6
81 #elif RTE_CACHE_LINE_SIZE == 128
82 #define RTE_CACHE_LINE_SIZE_LOG2 7
83 #else
84 #error "Unsupported cache line size"
85 #endif
86
87 #define RTE_CACHE_LINE_MIN_SIZE 64      /**< Minimum Cache line size. */
88
89 /**
90  * Force alignment to cache line.
91  */
92 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
93
94 /**
95  * Force minimum cache line alignment.
96  */
97 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
98
99 typedef uint64_t phys_addr_t; /**< Physical address definition. */
100 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
101
102 /**
103  * Physical memory segment descriptor.
104  */
105 struct rte_memseg {
106         phys_addr_t phys_addr;      /**< Start physical address. */
107         union {
108                 void *addr;         /**< Start virtual address. */
109                 uint64_t addr_64;   /**< Makes sure addr is always 64 bits */
110         };
111         size_t len;               /**< Length of the segment. */
112         uint64_t hugepage_sz;       /**< The pagesize of underlying memory */
113         int32_t socket_id;          /**< NUMA socket ID. */
114         uint32_t nchannel;          /**< Number of channels. */
115         uint32_t nrank;             /**< Number of ranks. */
116 #ifdef RTE_LIBRTE_XEN_DOM0
117          /**< store segment MFNs */
118         uint64_t mfn[DOM0_NUM_MEMBLOCK];
119 #endif
120 } __rte_packed;
121
122 /**
123  * Lock page in physical memory and prevent from swapping.
124  *
125  * @param virt
126  *   The virtual address.
127  * @return
128  *   0 on success, negative on error.
129  */
130 int rte_mem_lock_page(const void *virt);
131
132 /**
133  * Get physical address of any mapped virtual address in the current process.
134  * It is found by browsing the /proc/self/pagemap special file.
135  * The page must be locked.
136  *
137  * @param virt
138  *   The virtual address.
139  * @return
140  *   The physical address or RTE_BAD_PHYS_ADDR on error.
141  */
142 phys_addr_t rte_mem_virt2phy(const void *virt);
143
144 /**
145  * Get the layout of the available physical memory.
146  *
147  * It can be useful for an application to have the full physical
148  * memory layout to decide the size of a memory zone to reserve. This
149  * table is stored in rte_config (see rte_eal_get_configuration()).
150  *
151  * @return
152  *  - On success, return a pointer to a read-only table of struct
153  *    rte_physmem_desc elements, containing the layout of all
154  *    addressable physical memory. The last element of the table
155  *    contains a NULL address.
156  *  - On error, return NULL. This should not happen since it is a fatal
157  *    error that will probably cause the entire system to panic.
158  */
159 const struct rte_memseg *rte_eal_get_physmem_layout(void);
160
161 /**
162  * Dump the physical memory layout to the console.
163  *
164  * @param f
165  *   A pointer to a file for output
166  */
167 void rte_dump_physmem_layout(FILE *f);
168
169 /**
170  * Get the total amount of available physical memory.
171  *
172  * @return
173  *    The total amount of available physical memory in bytes.
174  */
175 uint64_t rte_eal_get_physmem_size(void);
176
177 /**
178  * Get the number of memory channels.
179  *
180  * @return
181  *   The number of memory channels on the system. The value is 0 if unknown
182  *   or not the same on all devices.
183  */
184 unsigned rte_memory_get_nchannel(void);
185
186 /**
187  * Get the number of memory ranks.
188  *
189  * @return
190  *   The number of memory ranks on the system. The value is 0 if unknown or
191  *   not the same on all devices.
192  */
193 unsigned rte_memory_get_nrank(void);
194
195 #ifdef RTE_LIBRTE_XEN_DOM0
196
197 /**< Internal use only - should DOM0 memory mapping be used */
198 int rte_xen_dom0_supported(void);
199
200 /**< Internal use only - phys to virt mapping for xen */
201 phys_addr_t rte_xen_mem_phy2mch(int32_t, const phys_addr_t);
202
203 /**
204  * Return the physical address of elt, which is an element of the pool mp.
205  *
206  * @param memseg_id
207  *   Identifier of the memory segment owning the physical address. If
208  *   set to -1, find it automatically.
209  * @param phy_addr
210  *   physical address of elt.
211  *
212  * @return
213  *   The physical address or RTE_BAD_PHYS_ADDR on error.
214  */
215 static inline phys_addr_t
216 rte_mem_phy2mch(int32_t memseg_id, const phys_addr_t phy_addr)
217 {
218         if (rte_xen_dom0_supported())
219                 return rte_xen_mem_phy2mch(memseg_id, phy_addr);
220         else
221                 return phy_addr;
222 }
223
224 /**
225  * Memory init for supporting application running on Xen domain0.
226  *
227  * @param void
228  *
229  * @return
230  *       0: successfully
231  *       negative: error
232  */
233 int rte_xen_dom0_memory_init(void);
234
235 /**
236  * Attach to memory setments of primary process on Xen domain0.
237  *
238  * @param void
239  *
240  * @return
241  *       0: successfully
242  *       negative: error
243  */
244 int rte_xen_dom0_memory_attach(void);
245 #else
246 static inline int rte_xen_dom0_supported(void)
247 {
248         return 0;
249 }
250
251 static inline phys_addr_t
252 rte_mem_phy2mch(int32_t memseg_id __rte_unused, const phys_addr_t phy_addr)
253 {
254         return phy_addr;
255 }
256 #endif
257
258 #ifdef __cplusplus
259 }
260 #endif
261
262 #endif /* _RTE_MEMORY_H_ */