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