f31227b466ce69a059fe66a43f0ccd69ae5bddaa
[dpdk.git] / lib / librte_eal / common / include / rte_memory.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #ifndef _RTE_MEMORY_H_
6 #define _RTE_MEMORY_H_
7
8 /**
9  * @file
10  *
11  * Memory-related RTE API.
12  */
13
14 #include <stdint.h>
15 #include <stddef.h>
16 #include <stdio.h>
17
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21
22 #include <rte_common.h>
23 #include <rte_compat.h>
24 #include <rte_config.h>
25
26 /* forward declaration for pointers */
27 struct rte_memseg_list;
28
29 __extension__
30 enum rte_page_sizes {
31         RTE_PGSIZE_4K    = 1ULL << 12,
32         RTE_PGSIZE_64K   = 1ULL << 16,
33         RTE_PGSIZE_256K  = 1ULL << 18,
34         RTE_PGSIZE_2M    = 1ULL << 21,
35         RTE_PGSIZE_16M   = 1ULL << 24,
36         RTE_PGSIZE_256M  = 1ULL << 28,
37         RTE_PGSIZE_512M  = 1ULL << 29,
38         RTE_PGSIZE_1G    = 1ULL << 30,
39         RTE_PGSIZE_4G    = 1ULL << 32,
40         RTE_PGSIZE_16G   = 1ULL << 34,
41 };
42
43 #define SOCKET_ID_ANY -1                    /**< Any NUMA socket. */
44 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1) /**< Cache line mask. */
45
46 #define RTE_CACHE_LINE_ROUNDUP(size) \
47         (RTE_CACHE_LINE_SIZE * ((size + RTE_CACHE_LINE_SIZE - 1) / RTE_CACHE_LINE_SIZE))
48 /**< Return the first cache-aligned value greater or equal to size. */
49
50 /**< Cache line size in terms of log2 */
51 #if RTE_CACHE_LINE_SIZE == 64
52 #define RTE_CACHE_LINE_SIZE_LOG2 6
53 #elif RTE_CACHE_LINE_SIZE == 128
54 #define RTE_CACHE_LINE_SIZE_LOG2 7
55 #else
56 #error "Unsupported cache line size"
57 #endif
58
59 #define RTE_CACHE_LINE_MIN_SIZE 64      /**< Minimum Cache line size. */
60
61 /**
62  * Force alignment to cache line.
63  */
64 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
65
66 /**
67  * Force minimum cache line alignment.
68  */
69 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
70
71 typedef uint64_t phys_addr_t; /**< Physical address. */
72 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
73 /**
74  * IO virtual address type.
75  * When the physical addressing mode (IOVA as PA) is in use,
76  * the translation from an IO virtual address (IOVA) to a physical address
77  * is a direct mapping, i.e. the same value.
78  * Otherwise, in virtual mode (IOVA as VA), an IOMMU may do the translation.
79  */
80 typedef uint64_t rte_iova_t;
81 #define RTE_BAD_IOVA ((rte_iova_t)-1)
82
83 /**
84  * Physical memory segment descriptor.
85  */
86 struct rte_memseg {
87         RTE_STD_C11
88         union {
89                 phys_addr_t phys_addr;  /**< deprecated - Start physical address. */
90                 rte_iova_t iova;        /**< Start IO address. */
91         };
92         RTE_STD_C11
93         union {
94                 void *addr;         /**< Start virtual address. */
95                 uint64_t addr_64;   /**< Makes sure addr is always 64 bits */
96         };
97         size_t len;               /**< Length of the segment. */
98         uint64_t hugepage_sz;       /**< The pagesize of underlying memory */
99         int32_t socket_id;          /**< NUMA socket ID. */
100         uint32_t nchannel;          /**< Number of channels. */
101         uint32_t nrank;             /**< Number of ranks. */
102 } __rte_packed;
103
104 /**
105  * Lock page in physical memory and prevent from swapping.
106  *
107  * @param virt
108  *   The virtual address.
109  * @return
110  *   0 on success, negative on error.
111  */
112 int rte_mem_lock_page(const void *virt);
113
114 /**
115  * Get physical address of any mapped virtual address in the current process.
116  * It is found by browsing the /proc/self/pagemap special file.
117  * The page must be locked.
118  *
119  * @param virt
120  *   The virtual address.
121  * @return
122  *   The physical address or RTE_BAD_IOVA on error.
123  */
124 phys_addr_t rte_mem_virt2phy(const void *virt);
125
126 /**
127  * Get IO virtual address of any mapped virtual address in the current process.
128  *
129  * @param virt
130  *   The virtual address.
131  * @return
132  *   The IO address or RTE_BAD_IOVA on error.
133  */
134 rte_iova_t rte_mem_virt2iova(const void *virt);
135
136 /**
137  * Get virtual memory address corresponding to iova address.
138  *
139  * @param iova
140  *   The iova address.
141  * @return
142  *   Virtual address corresponding to iova address (or NULL if address does not
143  *   exist within DPDK memory map).
144  */
145 __rte_experimental void *
146 rte_mem_iova2virt(rte_iova_t iova);
147
148 /**
149  * Get memseg to which a particular virtual address belongs.
150  *
151  * @param virt
152  *   The virtual address.
153  * @param msl
154  *   The memseg list in which to look up based on ``virt`` address
155  *   (can be NULL).
156  * @return
157  *   Memseg pointer on success, or NULL on error.
158  */
159 __rte_experimental struct rte_memseg *
160 rte_mem_virt2memseg(const void *virt, const struct rte_memseg_list *msl);
161
162 /**
163  * Get memseg list corresponding to virtual memory address.
164  *
165  * @param virt
166  *   The virtual address.
167  * @return
168  *   Memseg list to which this virtual address belongs to.
169  */
170 __rte_experimental struct rte_memseg_list *
171 rte_mem_virt2memseg_list(const void *virt);
172
173 /**
174  * Memseg walk function prototype.
175  *
176  * Returning 0 will continue walk
177  * Returning 1 will stop the walk
178  * Returning -1 will stop the walk and report error
179  */
180 typedef int (*rte_memseg_walk_t)(const struct rte_memseg_list *msl,
181                 const struct rte_memseg *ms, void *arg);
182
183 /**
184  * Memseg contig walk function prototype. This will trigger a callback on every
185  * VA-contiguous are starting at memseg ``ms``, so total valid VA space at each
186  * callback call will be [``ms->addr``, ``ms->addr + len``).
187  *
188  * Returning 0 will continue walk
189  * Returning 1 will stop the walk
190  * Returning -1 will stop the walk and report error
191  */
192 typedef int (*rte_memseg_contig_walk_t)(const struct rte_memseg_list *msl,
193                 const struct rte_memseg *ms, size_t len, void *arg);
194
195 /**
196  * Memseg list walk function prototype. This will trigger a callback on every
197  * allocated memseg list.
198  *
199  * Returning 0 will continue walk
200  * Returning 1 will stop the walk
201  * Returning -1 will stop the walk and report error
202  */
203 typedef int (*rte_memseg_list_walk_t)(const struct rte_memseg_list *msl,
204                 void *arg);
205
206 /**
207  * Walk list of all memsegs.
208  *
209  * @param func
210  *   Iterator function
211  * @param arg
212  *   Argument passed to iterator
213  * @return
214  *   0 if walked over the entire list
215  *   1 if stopped by the user
216  *   -1 if user function reported error
217  */
218 int __rte_experimental
219 rte_memseg_walk(rte_memseg_walk_t func, void *arg);
220
221 /**
222  * Walk each VA-contiguous area.
223  *
224  * @param func
225  *   Iterator function
226  * @param arg
227  *   Argument passed to iterator
228  * @return
229  *   0 if walked over the entire list
230  *   1 if stopped by the user
231  *   -1 if user function reported error
232  */
233 int __rte_experimental
234 rte_memseg_contig_walk(rte_memseg_contig_walk_t func, void *arg);
235
236 /**
237  * Walk each allocated memseg list.
238  *
239  * @param func
240  *   Iterator function
241  * @param arg
242  *   Argument passed to iterator
243  * @return
244  *   0 if walked over the entire list
245  *   1 if stopped by the user
246  *   -1 if user function reported error
247  */
248 int __rte_experimental
249 rte_memseg_list_walk(rte_memseg_list_walk_t func, void *arg);
250
251 /**
252  * Dump the physical memory layout to a file.
253  *
254  * @param f
255  *   A pointer to a file for output
256  */
257 void rte_dump_physmem_layout(FILE *f);
258
259 /**
260  * Get the total amount of available physical memory.
261  *
262  * @return
263  *    The total amount of available physical memory in bytes.
264  */
265 uint64_t rte_eal_get_physmem_size(void);
266
267 /**
268  * Get the number of memory channels.
269  *
270  * @return
271  *   The number of memory channels on the system. The value is 0 if unknown
272  *   or not the same on all devices.
273  */
274 unsigned rte_memory_get_nchannel(void);
275
276 /**
277  * Get the number of memory ranks.
278  *
279  * @return
280  *   The number of memory ranks on the system. The value is 0 if unknown or
281  *   not the same on all devices.
282  */
283 unsigned rte_memory_get_nrank(void);
284
285 /**
286  * Drivers based on uio will not load unless physical
287  * addresses are obtainable. It is only possible to get
288  * physical addresses when running as a privileged user.
289  *
290  * @return
291  *   1 if the system is able to obtain physical addresses.
292  *   0 if using DMA addresses through an IOMMU.
293  */
294 int rte_eal_using_phys_addrs(void);
295
296 #ifdef __cplusplus
297 }
298 #endif
299
300 #endif /* _RTE_MEMORY_H_ */