mem: add iova2virt 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  * Get virtual memory address corresponding to iova address.
135  *
136  * @param iova
137  *   The iova address.
138  * @return
139  *   Virtual address corresponding to iova address (or NULL if address does not
140  *   exist within DPDK memory map).
141  */
142 __rte_experimental void *
143 rte_mem_iova2virt(rte_iova_t iova);
144
145 /**
146  * Memseg walk function prototype.
147  *
148  * Returning 0 will continue walk
149  * Returning 1 will stop the walk
150  * Returning -1 will stop the walk and report error
151  */
152 typedef int (*rte_memseg_walk_t)(const struct rte_memseg *ms, void *arg);
153
154 /**
155  * Memseg contig walk function prototype. This will trigger a callback on every
156  * VA-contiguous are starting at memseg ``ms``, so total valid VA space at each
157  * callback call will be [``ms->addr``, ``ms->addr + len``).
158  *
159  * Returning 0 will continue walk
160  * Returning 1 will stop the walk
161  * Returning -1 will stop the walk and report error
162  */
163 typedef int (*rte_memseg_contig_walk_t)(const struct rte_memseg *ms,
164                 size_t len, void *arg);
165
166 /**
167  * Walk list of all memsegs.
168  *
169  * @param func
170  *   Iterator function
171  * @param arg
172  *   Argument passed to iterator
173  * @return
174  *   0 if walked over the entire list
175  *   1 if stopped by the user
176  *   -1 if user function reported error
177  */
178 int __rte_experimental
179 rte_memseg_walk(rte_memseg_walk_t func, void *arg);
180
181 /**
182  * Walk each VA-contiguous area.
183  *
184  * @param func
185  *   Iterator function
186  * @param arg
187  *   Argument passed to iterator
188  * @return
189  *   0 if walked over the entire list
190  *   1 if stopped by the user
191  *   -1 if user function reported error
192  */
193 int __rte_experimental
194 rte_memseg_contig_walk(rte_memseg_contig_walk_t func, void *arg);
195
196 /**
197  * Get the layout of the available physical memory.
198  *
199  * It can be useful for an application to have the full physical
200  * memory layout to decide the size of a memory zone to reserve. This
201  * table is stored in rte_config (see rte_eal_get_configuration()).
202  *
203  * @return
204  *  - On success, return a pointer to a read-only table of struct
205  *    rte_physmem_desc elements, containing the layout of all
206  *    addressable physical memory. The last element of the table
207  *    contains a NULL address.
208  *  - On error, return NULL. This should not happen since it is a fatal
209  *    error that will probably cause the entire system to panic.
210  */
211 const struct rte_memseg *rte_eal_get_physmem_layout(void);
212
213 /**
214  * Dump the physical memory layout to a file.
215  *
216  * @param f
217  *   A pointer to a file for output
218  */
219 void rte_dump_physmem_layout(FILE *f);
220
221 /**
222  * Get the total amount of available physical memory.
223  *
224  * @return
225  *    The total amount of available physical memory in bytes.
226  */
227 uint64_t rte_eal_get_physmem_size(void);
228
229 /**
230  * Get the number of memory channels.
231  *
232  * @return
233  *   The number of memory channels on the system. The value is 0 if unknown
234  *   or not the same on all devices.
235  */
236 unsigned rte_memory_get_nchannel(void);
237
238 /**
239  * Get the number of memory ranks.
240  *
241  * @return
242  *   The number of memory ranks on the system. The value is 0 if unknown or
243  *   not the same on all devices.
244  */
245 unsigned rte_memory_get_nrank(void);
246
247 /**
248  * Drivers based on uio will not load unless physical
249  * addresses are obtainable. It is only possible to get
250  * physical addresses when running as a privileged user.
251  *
252  * @return
253  *   1 if the system is able to obtain physical addresses.
254  *   0 if using DMA addresses through an IOMMU.
255  */
256 int rte_eal_using_phys_addrs(void);
257
258 #ifdef __cplusplus
259 }
260 #endif
261
262 #endif /* _RTE_MEMORY_H_ */