mem: add contig walk function
[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 __extension__
27 enum rte_page_sizes {
28         RTE_PGSIZE_4K    = 1ULL << 12,
29         RTE_PGSIZE_64K   = 1ULL << 16,
30         RTE_PGSIZE_256K  = 1ULL << 18,
31         RTE_PGSIZE_2M    = 1ULL << 21,
32         RTE_PGSIZE_16M   = 1ULL << 24,
33         RTE_PGSIZE_256M  = 1ULL << 28,
34         RTE_PGSIZE_512M  = 1ULL << 29,
35         RTE_PGSIZE_1G    = 1ULL << 30,
36         RTE_PGSIZE_4G    = 1ULL << 32,
37         RTE_PGSIZE_16G   = 1ULL << 34,
38 };
39
40 #define SOCKET_ID_ANY -1                    /**< Any NUMA socket. */
41 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1) /**< Cache line mask. */
42
43 #define RTE_CACHE_LINE_ROUNDUP(size) \
44         (RTE_CACHE_LINE_SIZE * ((size + RTE_CACHE_LINE_SIZE - 1) / RTE_CACHE_LINE_SIZE))
45 /**< Return the first cache-aligned value greater or equal to size. */
46
47 /**< Cache line size in terms of log2 */
48 #if RTE_CACHE_LINE_SIZE == 64
49 #define RTE_CACHE_LINE_SIZE_LOG2 6
50 #elif RTE_CACHE_LINE_SIZE == 128
51 #define RTE_CACHE_LINE_SIZE_LOG2 7
52 #else
53 #error "Unsupported cache line size"
54 #endif
55
56 #define RTE_CACHE_LINE_MIN_SIZE 64      /**< Minimum Cache line size. */
57
58 /**
59  * Force alignment to cache line.
60  */
61 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
62
63 /**
64  * Force minimum cache line alignment.
65  */
66 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
67
68 typedef uint64_t phys_addr_t; /**< Physical address. */
69 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
70 /**
71  * IO virtual address type.
72  * When the physical addressing mode (IOVA as PA) is in use,
73  * the translation from an IO virtual address (IOVA) to a physical address
74  * is a direct mapping, i.e. the same value.
75  * Otherwise, in virtual mode (IOVA as VA), an IOMMU may do the translation.
76  */
77 typedef uint64_t rte_iova_t;
78 #define RTE_BAD_IOVA ((rte_iova_t)-1)
79
80 /**
81  * Physical memory segment descriptor.
82  */
83 struct rte_memseg {
84         RTE_STD_C11
85         union {
86                 phys_addr_t phys_addr;  /**< deprecated - Start physical address. */
87                 rte_iova_t iova;        /**< Start IO address. */
88         };
89         RTE_STD_C11
90         union {
91                 void *addr;         /**< Start virtual address. */
92                 uint64_t addr_64;   /**< Makes sure addr is always 64 bits */
93         };
94         size_t len;               /**< Length of the segment. */
95         uint64_t hugepage_sz;       /**< The pagesize of underlying memory */
96         int32_t socket_id;          /**< NUMA socket ID. */
97         uint32_t nchannel;          /**< Number of channels. */
98         uint32_t nrank;             /**< Number of ranks. */
99 } __rte_packed;
100
101 /**
102  * Lock page in physical memory and prevent from swapping.
103  *
104  * @param virt
105  *   The virtual address.
106  * @return
107  *   0 on success, negative on error.
108  */
109 int rte_mem_lock_page(const void *virt);
110
111 /**
112  * Get physical address of any mapped virtual address in the current process.
113  * It is found by browsing the /proc/self/pagemap special file.
114  * The page must be locked.
115  *
116  * @param virt
117  *   The virtual address.
118  * @return
119  *   The physical address or RTE_BAD_IOVA on error.
120  */
121 phys_addr_t rte_mem_virt2phy(const void *virt);
122
123 /**
124  * Get IO virtual address of any mapped virtual address in the current process.
125  *
126  * @param virt
127  *   The virtual address.
128  * @return
129  *   The IO address or RTE_BAD_IOVA on error.
130  */
131 rte_iova_t rte_mem_virt2iova(const void *virt);
132
133 /**
134  * Memseg walk function prototype.
135  *
136  * Returning 0 will continue walk
137  * Returning 1 will stop the walk
138  * Returning -1 will stop the walk and report error
139  */
140 typedef int (*rte_memseg_walk_t)(const struct rte_memseg *ms, void *arg);
141
142 /**
143  * Memseg contig walk function prototype. This will trigger a callback on every
144  * VA-contiguous are starting at memseg ``ms``, so total valid VA space at each
145  * callback call will be [``ms->addr``, ``ms->addr + len``).
146  *
147  * Returning 0 will continue walk
148  * Returning 1 will stop the walk
149  * Returning -1 will stop the walk and report error
150  */
151 typedef int (*rte_memseg_contig_walk_t)(const struct rte_memseg *ms,
152                 size_t len, void *arg);
153
154 /**
155  * Walk list of all memsegs.
156  *
157  * @param func
158  *   Iterator function
159  * @param arg
160  *   Argument passed to iterator
161  * @return
162  *   0 if walked over the entire list
163  *   1 if stopped by the user
164  *   -1 if user function reported error
165  */
166 int __rte_experimental
167 rte_memseg_walk(rte_memseg_walk_t func, void *arg);
168
169 /**
170  * Walk each VA-contiguous area.
171  *
172  * @param func
173  *   Iterator function
174  * @param arg
175  *   Argument passed to iterator
176  * @return
177  *   0 if walked over the entire list
178  *   1 if stopped by the user
179  *   -1 if user function reported error
180  */
181 int __rte_experimental
182 rte_memseg_contig_walk(rte_memseg_contig_walk_t func, void *arg);
183
184 /**
185  * Get the layout of the available physical memory.
186  *
187  * It can be useful for an application to have the full physical
188  * memory layout to decide the size of a memory zone to reserve. This
189  * table is stored in rte_config (see rte_eal_get_configuration()).
190  *
191  * @return
192  *  - On success, return a pointer to a read-only table of struct
193  *    rte_physmem_desc elements, containing the layout of all
194  *    addressable physical memory. The last element of the table
195  *    contains a NULL address.
196  *  - On error, return NULL. This should not happen since it is a fatal
197  *    error that will probably cause the entire system to panic.
198  */
199 const struct rte_memseg *rte_eal_get_physmem_layout(void);
200
201 /**
202  * Dump the physical memory layout to a file.
203  *
204  * @param f
205  *   A pointer to a file for output
206  */
207 void rte_dump_physmem_layout(FILE *f);
208
209 /**
210  * Get the total amount of available physical memory.
211  *
212  * @return
213  *    The total amount of available physical memory in bytes.
214  */
215 uint64_t rte_eal_get_physmem_size(void);
216
217 /**
218  * Get the number of memory channels.
219  *
220  * @return
221  *   The number of memory channels on the system. The value is 0 if unknown
222  *   or not the same on all devices.
223  */
224 unsigned rte_memory_get_nchannel(void);
225
226 /**
227  * Get the number of memory ranks.
228  *
229  * @return
230  *   The number of memory ranks on the system. The value is 0 if unknown or
231  *   not the same on all devices.
232  */
233 unsigned rte_memory_get_nrank(void);
234
235 /**
236  * Drivers based on uio will not load unless physical
237  * addresses are obtainable. It is only possible to get
238  * physical addresses when running as a privileged user.
239  *
240  * @return
241  *   1 if the system is able to obtain physical addresses.
242  *   0 if using DMA addresses through an IOMMU.
243  */
244 int rte_eal_using_phys_addrs(void);
245
246 #ifdef __cplusplus
247 }
248 #endif
249
250 #endif /* _RTE_MEMORY_H_ */