New upstream version 18.08
[deb_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 #define RTE_MEMSEG_FLAG_DO_NOT_FREE (1 << 0)
87 /**< Prevent this segment from being freed back to the OS. */
88 struct rte_memseg {
89         RTE_STD_C11
90         union {
91                 phys_addr_t phys_addr;  /**< deprecated - Start physical address. */
92                 rte_iova_t iova;        /**< Start IO address. */
93         };
94         RTE_STD_C11
95         union {
96                 void *addr;         /**< Start virtual address. */
97                 uint64_t addr_64;   /**< Makes sure addr is always 64 bits */
98         };
99         size_t len;               /**< Length of the segment. */
100         uint64_t hugepage_sz;       /**< The pagesize of underlying memory */
101         int32_t socket_id;          /**< NUMA socket ID. */
102         uint32_t nchannel;          /**< Number of channels. */
103         uint32_t nrank;             /**< Number of ranks. */
104         uint32_t flags;             /**< Memseg-specific flags */
105 } __rte_packed;
106
107 /**
108  * Lock page in physical memory and prevent from swapping.
109  *
110  * @param virt
111  *   The virtual address.
112  * @return
113  *   0 on success, negative on error.
114  */
115 int rte_mem_lock_page(const void *virt);
116
117 /**
118  * Get physical address of any mapped virtual address in the current process.
119  * It is found by browsing the /proc/self/pagemap special file.
120  * The page must be locked.
121  *
122  * @param virt
123  *   The virtual address.
124  * @return
125  *   The physical address or RTE_BAD_IOVA on error.
126  */
127 phys_addr_t rte_mem_virt2phy(const void *virt);
128
129 /**
130  * Get IO virtual address of any mapped virtual address in the current process.
131  *
132  * @param virt
133  *   The virtual address.
134  * @return
135  *   The IO address or RTE_BAD_IOVA on error.
136  */
137 rte_iova_t rte_mem_virt2iova(const void *virt);
138
139 /**
140  * Get virtual memory address corresponding to iova address.
141  *
142  * @note This function read-locks the memory hotplug subsystem, and thus cannot
143  *       be used within memory-related callback functions.
144  *
145  * @param iova
146  *   The iova address.
147  * @return
148  *   Virtual address corresponding to iova address (or NULL if address does not
149  *   exist within DPDK memory map).
150  */
151 __rte_experimental void *
152 rte_mem_iova2virt(rte_iova_t iova);
153
154 /**
155  * Get memseg to which a particular virtual address belongs.
156  *
157  * @param virt
158  *   The virtual address.
159  * @param msl
160  *   The memseg list in which to look up based on ``virt`` address
161  *   (can be NULL).
162  * @return
163  *   Memseg pointer on success, or NULL on error.
164  */
165 __rte_experimental struct rte_memseg *
166 rte_mem_virt2memseg(const void *virt, const struct rte_memseg_list *msl);
167
168 /**
169  * Get memseg list corresponding to virtual memory address.
170  *
171  * @param virt
172  *   The virtual address.
173  * @return
174  *   Memseg list to which this virtual address belongs to.
175  */
176 __rte_experimental struct rte_memseg_list *
177 rte_mem_virt2memseg_list(const void *virt);
178
179 /**
180  * Memseg walk function prototype.
181  *
182  * Returning 0 will continue walk
183  * Returning 1 will stop the walk
184  * Returning -1 will stop the walk and report error
185  */
186 typedef int (*rte_memseg_walk_t)(const struct rte_memseg_list *msl,
187                 const struct rte_memseg *ms, void *arg);
188
189 /**
190  * Memseg contig walk function prototype. This will trigger a callback on every
191  * VA-contiguous are starting at memseg ``ms``, so total valid VA space at each
192  * callback call will be [``ms->addr``, ``ms->addr + len``).
193  *
194  * Returning 0 will continue walk
195  * Returning 1 will stop the walk
196  * Returning -1 will stop the walk and report error
197  */
198 typedef int (*rte_memseg_contig_walk_t)(const struct rte_memseg_list *msl,
199                 const struct rte_memseg *ms, size_t len, void *arg);
200
201 /**
202  * Memseg list walk function prototype. This will trigger a callback on every
203  * allocated memseg list.
204  *
205  * Returning 0 will continue walk
206  * Returning 1 will stop the walk
207  * Returning -1 will stop the walk and report error
208  */
209 typedef int (*rte_memseg_list_walk_t)(const struct rte_memseg_list *msl,
210                 void *arg);
211
212 /**
213  * Walk list of all memsegs.
214  *
215  * @note This function read-locks the memory hotplug subsystem, and thus cannot
216  *       be used within memory-related callback functions.
217  *
218  * @param func
219  *   Iterator function
220  * @param arg
221  *   Argument passed to iterator
222  * @return
223  *   0 if walked over the entire list
224  *   1 if stopped by the user
225  *   -1 if user function reported error
226  */
227 int __rte_experimental
228 rte_memseg_walk(rte_memseg_walk_t func, void *arg);
229
230 /**
231  * Walk each VA-contiguous area.
232  *
233  * @note This function read-locks the memory hotplug subsystem, and thus cannot
234  *       be used within memory-related callback functions.
235  *
236  * @param func
237  *   Iterator function
238  * @param arg
239  *   Argument passed to iterator
240  * @return
241  *   0 if walked over the entire list
242  *   1 if stopped by the user
243  *   -1 if user function reported error
244  */
245 int __rte_experimental
246 rte_memseg_contig_walk(rte_memseg_contig_walk_t func, void *arg);
247
248 /**
249  * Walk each allocated memseg list.
250  *
251  * @note This function read-locks the memory hotplug subsystem, and thus cannot
252  *       be used within memory-related callback functions.
253  *
254  * @param func
255  *   Iterator function
256  * @param arg
257  *   Argument passed to iterator
258  * @return
259  *   0 if walked over the entire list
260  *   1 if stopped by the user
261  *   -1 if user function reported error
262  */
263 int __rte_experimental
264 rte_memseg_list_walk(rte_memseg_list_walk_t func, void *arg);
265
266 /**
267  * Walk list of all memsegs without performing any locking.
268  *
269  * @note This function does not perform any locking, and is only safe to call
270  *       from within memory-related callback functions.
271  *
272  * @param func
273  *   Iterator function
274  * @param arg
275  *   Argument passed to iterator
276  * @return
277  *   0 if walked over the entire list
278  *   1 if stopped by the user
279  *   -1 if user function reported error
280  */
281 int __rte_experimental
282 rte_memseg_walk_thread_unsafe(rte_memseg_walk_t func, void *arg);
283
284 /**
285  * Walk each VA-contiguous area without performing any locking.
286  *
287  * @note This function does not perform any locking, and is only safe to call
288  *       from within memory-related callback functions.
289  *
290  * @param func
291  *   Iterator function
292  * @param arg
293  *   Argument passed to iterator
294  * @return
295  *   0 if walked over the entire list
296  *   1 if stopped by the user
297  *   -1 if user function reported error
298  */
299 int __rte_experimental
300 rte_memseg_contig_walk_thread_unsafe(rte_memseg_contig_walk_t func, void *arg);
301
302 /**
303  * Walk each allocated memseg list without performing any locking.
304  *
305  * @note This function does not perform any locking, and is only safe to call
306  *       from within memory-related callback functions.
307  *
308  * @param func
309  *   Iterator function
310  * @param arg
311  *   Argument passed to iterator
312  * @return
313  *   0 if walked over the entire list
314  *   1 if stopped by the user
315  *   -1 if user function reported error
316  */
317 int __rte_experimental
318 rte_memseg_list_walk_thread_unsafe(rte_memseg_list_walk_t func, void *arg);
319
320 /**
321  * Dump the physical memory layout to a file.
322  *
323  * @note This function read-locks the memory hotplug subsystem, and thus cannot
324  *       be used within memory-related callback functions.
325  *
326  * @param f
327  *   A pointer to a file for output
328  */
329 void rte_dump_physmem_layout(FILE *f);
330
331 /**
332  * Get the total amount of available physical memory.
333  *
334  * @note This function read-locks the memory hotplug subsystem, and thus cannot
335  *       be used within memory-related callback functions.
336  *
337  * @return
338  *    The total amount of available physical memory in bytes.
339  */
340 uint64_t rte_eal_get_physmem_size(void);
341
342 /**
343  * Get the number of memory channels.
344  *
345  * @return
346  *   The number of memory channels on the system. The value is 0 if unknown
347  *   or not the same on all devices.
348  */
349 unsigned rte_memory_get_nchannel(void);
350
351 /**
352  * Get the number of memory ranks.
353  *
354  * @return
355  *   The number of memory ranks on the system. The value is 0 if unknown or
356  *   not the same on all devices.
357  */
358 unsigned rte_memory_get_nrank(void);
359
360 /**
361  * Drivers based on uio will not load unless physical
362  * addresses are obtainable. It is only possible to get
363  * physical addresses when running as a privileged user.
364  *
365  * @return
366  *   1 if the system is able to obtain physical addresses.
367  *   0 if using DMA addresses through an IOMMU.
368  */
369 int rte_eal_using_phys_addrs(void);
370
371
372 /**
373  * Enum indicating which kind of memory event has happened. Used by callbacks to
374  * distinguish between memory allocations and deallocations.
375  */
376 enum rte_mem_event {
377         RTE_MEM_EVENT_ALLOC = 0, /**< Allocation event. */
378         RTE_MEM_EVENT_FREE,      /**< Deallocation event. */
379 };
380 #define RTE_MEM_EVENT_CALLBACK_NAME_LEN 64
381 /**< maximum length of callback name */
382
383 /**
384  * Function typedef used to register callbacks for memory events.
385  */
386 typedef void (*rte_mem_event_callback_t)(enum rte_mem_event event_type,
387                 const void *addr, size_t len, void *arg);
388
389 /**
390  * Function used to register callbacks for memory events.
391  *
392  * @note callbacks will happen while memory hotplug subsystem is write-locked,
393  *       therefore some functions (e.g. `rte_memseg_walk()`) will cause a
394  *       deadlock when called from within such callbacks.
395  *
396  * @note mem event callbacks not being supported is an expected error condition,
397  *       so user code needs to handle this situation. In these cases, return
398  *       value will be -1, and rte_errno will be set to ENOTSUP.
399  *
400  * @param name
401  *   Name associated with specified callback to be added to the list.
402  *
403  * @param clb
404  *   Callback function pointer.
405  *
406  * @param arg
407  *   Argument to pass to the callback.
408  *
409  * @return
410  *   0 on successful callback register
411  *   -1 on unsuccessful callback register, with rte_errno value indicating
412  *   reason for failure.
413  */
414 int __rte_experimental
415 rte_mem_event_callback_register(const char *name, rte_mem_event_callback_t clb,
416                 void *arg);
417
418 /**
419  * Function used to unregister callbacks for memory events.
420  *
421  * @param name
422  *   Name associated with specified callback to be removed from the list.
423  *
424  * @param arg
425  *   Argument to look for among callbacks with specified callback name.
426  *
427  * @return
428  *   0 on successful callback unregister
429  *   -1 on unsuccessful callback unregister, with rte_errno value indicating
430  *   reason for failure.
431  */
432 int __rte_experimental
433 rte_mem_event_callback_unregister(const char *name, void *arg);
434
435
436 #define RTE_MEM_ALLOC_VALIDATOR_NAME_LEN 64
437 /**< maximum length of alloc validator name */
438 /**
439  * Function typedef used to register memory allocation validation callbacks.
440  *
441  * Returning 0 will allow allocation attempt to continue. Returning -1 will
442  * prevent allocation from succeeding.
443  */
444 typedef int (*rte_mem_alloc_validator_t)(int socket_id,
445                 size_t cur_limit, size_t new_len);
446
447 /**
448  * @brief Register validator callback for memory allocations.
449  *
450  * Callbacks registered by this function will be called right before memory
451  * allocator is about to trigger allocation of more pages from the system if
452  * said allocation will bring total memory usage above specified limit on
453  * specified socket. User will be able to cancel pending allocation if callback
454  * returns -1.
455  *
456  * @note callbacks will happen while memory hotplug subsystem is write-locked,
457  *       therefore some functions (e.g. `rte_memseg_walk()`) will cause a
458  *       deadlock when called from within such callbacks.
459  *
460  * @note validator callbacks not being supported is an expected error condition,
461  *       so user code needs to handle this situation. In these cases, return
462  *       value will be -1, and rte_errno will be set to ENOTSUP.
463  *
464  * @param name
465  *   Name associated with specified callback to be added to the list.
466  *
467  * @param clb
468  *   Callback function pointer.
469  *
470  * @param socket_id
471  *   Socket ID on which to watch for allocations.
472  *
473  * @param limit
474  *   Limit above which to trigger callbacks.
475  *
476  * @return
477  *   0 on successful callback register
478  *   -1 on unsuccessful callback register, with rte_errno value indicating
479  *   reason for failure.
480  */
481 int __rte_experimental
482 rte_mem_alloc_validator_register(const char *name,
483                 rte_mem_alloc_validator_t clb, int socket_id, size_t limit);
484
485 /**
486  * @brief Unregister validator callback for memory allocations.
487  *
488  * @param name
489  *   Name associated with specified callback to be removed from the list.
490  *
491  * @param socket_id
492  *   Socket ID on which to watch for allocations.
493  *
494  * @return
495  *   0 on successful callback unregister
496  *   -1 on unsuccessful callback unregister, with rte_errno value indicating
497  *   reason for failure.
498  */
499 int __rte_experimental
500 rte_mem_alloc_validator_unregister(const char *name, int socket_id);
501
502 #ifdef __cplusplus
503 }
504 #endif
505
506 #endif /* _RTE_MEMORY_H_ */