1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 * Memory-related RTE API.
22 #include <rte_common.h>
23 #include <rte_compat.h>
24 #include <rte_config.h>
26 /* forward declaration for pointers */
27 struct rte_memseg_list;
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,
43 #define SOCKET_ID_ANY -1 /**< Any NUMA socket. */
44 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1) /**< Cache line mask. */
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. */
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
56 #error "Unsupported cache line size"
59 #define RTE_CACHE_LINE_MIN_SIZE 64 /**< Minimum Cache line size. */
62 * Force alignment to cache line.
64 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
67 * Force minimum cache line alignment.
69 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
71 typedef uint64_t phys_addr_t; /**< Physical address. */
72 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
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.
80 typedef uint64_t rte_iova_t;
81 #define RTE_BAD_IOVA ((rte_iova_t)-1)
84 * Physical memory segment descriptor.
86 #define RTE_MEMSEG_FLAG_DO_NOT_FREE (1 << 0)
87 /**< Prevent this segment from being freed back to the OS. */
91 phys_addr_t phys_addr; /**< deprecated - Start physical address. */
92 rte_iova_t iova; /**< Start IO address. */
96 void *addr; /**< Start virtual address. */
97 uint64_t addr_64; /**< Makes sure addr is always 64 bits */
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 */
108 * Lock page in physical memory and prevent from swapping.
111 * The virtual address.
113 * 0 on success, negative on error.
115 int rte_mem_lock_page(const void *virt);
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.
123 * The virtual address.
125 * The physical address or RTE_BAD_IOVA on error.
127 phys_addr_t rte_mem_virt2phy(const void *virt);
130 * Get IO virtual address of any mapped virtual address in the current process.
133 * The virtual address.
135 * The IO address or RTE_BAD_IOVA on error.
137 rte_iova_t rte_mem_virt2iova(const void *virt);
140 * Get virtual memory address corresponding to iova address.
142 * @note This function read-locks the memory hotplug subsystem, and thus cannot
143 * be used within memory-related callback functions.
148 * Virtual address corresponding to iova address (or NULL if address does not
149 * exist within DPDK memory map).
151 __rte_experimental void *
152 rte_mem_iova2virt(rte_iova_t iova);
155 * Get memseg to which a particular virtual address belongs.
158 * The virtual address.
160 * The memseg list in which to look up based on ``virt`` address
163 * Memseg pointer on success, or NULL on error.
165 __rte_experimental struct rte_memseg *
166 rte_mem_virt2memseg(const void *virt, const struct rte_memseg_list *msl);
169 * Get memseg list corresponding to virtual memory address.
172 * The virtual address.
174 * Memseg list to which this virtual address belongs to.
176 __rte_experimental struct rte_memseg_list *
177 rte_mem_virt2memseg_list(const void *virt);
180 * Memseg walk function prototype.
182 * Returning 0 will continue walk
183 * Returning 1 will stop the walk
184 * Returning -1 will stop the walk and report error
186 typedef int (*rte_memseg_walk_t)(const struct rte_memseg_list *msl,
187 const struct rte_memseg *ms, void *arg);
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``).
194 * Returning 0 will continue walk
195 * Returning 1 will stop the walk
196 * Returning -1 will stop the walk and report error
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);
202 * Memseg list walk function prototype. This will trigger a callback on every
203 * allocated memseg list.
205 * Returning 0 will continue walk
206 * Returning 1 will stop the walk
207 * Returning -1 will stop the walk and report error
209 typedef int (*rte_memseg_list_walk_t)(const struct rte_memseg_list *msl,
213 * Walk list of all memsegs.
215 * @note This function read-locks the memory hotplug subsystem, and thus cannot
216 * be used within memory-related callback functions.
221 * Argument passed to iterator
223 * 0 if walked over the entire list
224 * 1 if stopped by the user
225 * -1 if user function reported error
227 int __rte_experimental
228 rte_memseg_walk(rte_memseg_walk_t func, void *arg);
231 * Walk each VA-contiguous area.
233 * @note This function read-locks the memory hotplug subsystem, and thus cannot
234 * be used within memory-related callback functions.
239 * Argument passed to iterator
241 * 0 if walked over the entire list
242 * 1 if stopped by the user
243 * -1 if user function reported error
245 int __rte_experimental
246 rte_memseg_contig_walk(rte_memseg_contig_walk_t func, void *arg);
249 * Walk each allocated memseg list.
251 * @note This function read-locks the memory hotplug subsystem, and thus cannot
252 * be used within memory-related callback functions.
257 * Argument passed to iterator
259 * 0 if walked over the entire list
260 * 1 if stopped by the user
261 * -1 if user function reported error
263 int __rte_experimental
264 rte_memseg_list_walk(rte_memseg_list_walk_t func, void *arg);
267 * Walk list of all memsegs without performing any locking.
269 * @note This function does not perform any locking, and is only safe to call
270 * from within memory-related callback functions.
275 * Argument passed to iterator
277 * 0 if walked over the entire list
278 * 1 if stopped by the user
279 * -1 if user function reported error
281 int __rte_experimental
282 rte_memseg_walk_thread_unsafe(rte_memseg_walk_t func, void *arg);
285 * Walk each VA-contiguous area without performing any locking.
287 * @note This function does not perform any locking, and is only safe to call
288 * from within memory-related callback functions.
293 * Argument passed to iterator
295 * 0 if walked over the entire list
296 * 1 if stopped by the user
297 * -1 if user function reported error
299 int __rte_experimental
300 rte_memseg_contig_walk_thread_unsafe(rte_memseg_contig_walk_t func, void *arg);
303 * Walk each allocated memseg list without performing any locking.
305 * @note This function does not perform any locking, and is only safe to call
306 * from within memory-related callback functions.
311 * Argument passed to iterator
313 * 0 if walked over the entire list
314 * 1 if stopped by the user
315 * -1 if user function reported error
317 int __rte_experimental
318 rte_memseg_list_walk_thread_unsafe(rte_memseg_list_walk_t func, void *arg);
321 * Return file descriptor associated with a particular memseg (if available).
323 * @note This function read-locks the memory hotplug subsystem, and thus cannot
324 * be used within memory-related callback functions.
326 * @note This returns an internal file descriptor. Performing any operations on
327 * this file descriptor is inherently dangerous, so it should be treated
328 * as read-only for all intents and purposes.
331 * A pointer to memseg for which to get file descriptor.
334 * Valid file descriptor in case of success.
335 * -1 in case of error, with ``rte_errno`` set to the following values:
336 * - EINVAL - ``ms`` pointer was NULL or did not point to a valid memseg
337 * - ENODEV - ``ms`` fd is not available
338 * - ENOENT - ``ms`` is an unused segment
339 * - ENOTSUP - segment fd's are not supported
341 int __rte_experimental
342 rte_memseg_get_fd(const struct rte_memseg *ms);
345 * Return file descriptor associated with a particular memseg (if available).
347 * @note This function does not perform any locking, and is only safe to call
348 * from within memory-related callback functions.
350 * @note This returns an internal file descriptor. Performing any operations on
351 * this file descriptor is inherently dangerous, so it should be treated
352 * as read-only for all intents and purposes.
355 * A pointer to memseg for which to get file descriptor.
358 * Valid file descriptor in case of success.
359 * -1 in case of error, with ``rte_errno`` set to the following values:
360 * - EINVAL - ``ms`` pointer was NULL or did not point to a valid memseg
361 * - ENODEV - ``ms`` fd is not available
362 * - ENOENT - ``ms`` is an unused segment
363 * - ENOTSUP - segment fd's are not supported
365 int __rte_experimental
366 rte_memseg_get_fd_thread_unsafe(const struct rte_memseg *ms);
369 * Get offset into segment file descriptor associated with a particular memseg
372 * @note This function read-locks the memory hotplug subsystem, and thus cannot
373 * be used within memory-related callback functions.
376 * A pointer to memseg for which to get file descriptor.
378 * A pointer to offset value where the result will be stored.
381 * Valid file descriptor in case of success.
382 * -1 in case of error, with ``rte_errno`` set to the following values:
383 * - EINVAL - ``ms`` pointer was NULL or did not point to a valid memseg
384 * - EINVAL - ``offset`` pointer was NULL
385 * - ENODEV - ``ms`` fd is not available
386 * - ENOENT - ``ms`` is an unused segment
387 * - ENOTSUP - segment fd's are not supported
389 int __rte_experimental
390 rte_memseg_get_fd_offset(const struct rte_memseg *ms, size_t *offset);
393 * Get offset into segment file descriptor associated with a particular memseg
396 * @note This function does not perform any locking, and is only safe to call
397 * from within memory-related callback functions.
400 * A pointer to memseg for which to get file descriptor.
402 * A pointer to offset value where the result will be stored.
405 * Valid file descriptor in case of success.
406 * -1 in case of error, with ``rte_errno`` set to the following values:
407 * - EINVAL - ``ms`` pointer was NULL or did not point to a valid memseg
408 * - EINVAL - ``offset`` pointer was NULL
409 * - ENODEV - ``ms`` fd is not available
410 * - ENOENT - ``ms`` is an unused segment
411 * - ENOTSUP - segment fd's are not supported
413 int __rte_experimental
414 rte_memseg_get_fd_offset_thread_unsafe(const struct rte_memseg *ms,
418 * Dump the physical memory layout to a file.
420 * @note This function read-locks the memory hotplug subsystem, and thus cannot
421 * be used within memory-related callback functions.
424 * A pointer to a file for output
426 void rte_dump_physmem_layout(FILE *f);
429 * Get the total amount of available physical memory.
431 * @note This function read-locks the memory hotplug subsystem, and thus cannot
432 * be used within memory-related callback functions.
435 * The total amount of available physical memory in bytes.
437 uint64_t rte_eal_get_physmem_size(void);
440 * Get the number of memory channels.
443 * The number of memory channels on the system. The value is 0 if unknown
444 * or not the same on all devices.
446 unsigned rte_memory_get_nchannel(void);
449 * Get the number of memory ranks.
452 * The number of memory ranks on the system. The value is 0 if unknown or
453 * not the same on all devices.
455 unsigned rte_memory_get_nrank(void);
458 * Drivers based on uio will not load unless physical
459 * addresses are obtainable. It is only possible to get
460 * physical addresses when running as a privileged user.
463 * 1 if the system is able to obtain physical addresses.
464 * 0 if using DMA addresses through an IOMMU.
466 int rte_eal_using_phys_addrs(void);
470 * Enum indicating which kind of memory event has happened. Used by callbacks to
471 * distinguish between memory allocations and deallocations.
474 RTE_MEM_EVENT_ALLOC = 0, /**< Allocation event. */
475 RTE_MEM_EVENT_FREE, /**< Deallocation event. */
477 #define RTE_MEM_EVENT_CALLBACK_NAME_LEN 64
478 /**< maximum length of callback name */
481 * Function typedef used to register callbacks for memory events.
483 typedef void (*rte_mem_event_callback_t)(enum rte_mem_event event_type,
484 const void *addr, size_t len, void *arg);
487 * Function used to register callbacks for memory events.
489 * @note callbacks will happen while memory hotplug subsystem is write-locked,
490 * therefore some functions (e.g. `rte_memseg_walk()`) will cause a
491 * deadlock when called from within such callbacks.
493 * @note mem event callbacks not being supported is an expected error condition,
494 * so user code needs to handle this situation. In these cases, return
495 * value will be -1, and rte_errno will be set to ENOTSUP.
498 * Name associated with specified callback to be added to the list.
501 * Callback function pointer.
504 * Argument to pass to the callback.
507 * 0 on successful callback register
508 * -1 on unsuccessful callback register, with rte_errno value indicating
509 * reason for failure.
511 int __rte_experimental
512 rte_mem_event_callback_register(const char *name, rte_mem_event_callback_t clb,
516 * Function used to unregister callbacks for memory events.
519 * Name associated with specified callback to be removed from the list.
522 * Argument to look for among callbacks with specified callback name.
525 * 0 on successful callback unregister
526 * -1 on unsuccessful callback unregister, with rte_errno value indicating
527 * reason for failure.
529 int __rte_experimental
530 rte_mem_event_callback_unregister(const char *name, void *arg);
533 #define RTE_MEM_ALLOC_VALIDATOR_NAME_LEN 64
534 /**< maximum length of alloc validator name */
536 * Function typedef used to register memory allocation validation callbacks.
538 * Returning 0 will allow allocation attempt to continue. Returning -1 will
539 * prevent allocation from succeeding.
541 typedef int (*rte_mem_alloc_validator_t)(int socket_id,
542 size_t cur_limit, size_t new_len);
545 * @brief Register validator callback for memory allocations.
547 * Callbacks registered by this function will be called right before memory
548 * allocator is about to trigger allocation of more pages from the system if
549 * said allocation will bring total memory usage above specified limit on
550 * specified socket. User will be able to cancel pending allocation if callback
553 * @note callbacks will happen while memory hotplug subsystem is write-locked,
554 * therefore some functions (e.g. `rte_memseg_walk()`) will cause a
555 * deadlock when called from within such callbacks.
557 * @note validator callbacks not being supported is an expected error condition,
558 * so user code needs to handle this situation. In these cases, return
559 * value will be -1, and rte_errno will be set to ENOTSUP.
562 * Name associated with specified callback to be added to the list.
565 * Callback function pointer.
568 * Socket ID on which to watch for allocations.
571 * Limit above which to trigger callbacks.
574 * 0 on successful callback register
575 * -1 on unsuccessful callback register, with rte_errno value indicating
576 * reason for failure.
578 int __rte_experimental
579 rte_mem_alloc_validator_register(const char *name,
580 rte_mem_alloc_validator_t clb, int socket_id, size_t limit);
583 * @brief Unregister validator callback for memory allocations.
586 * Name associated with specified callback to be removed from the list.
589 * Socket ID on which to watch for allocations.
592 * 0 on successful callback unregister
593 * -1 on unsuccessful callback unregister, with rte_errno value indicating
594 * reason for failure.
596 int __rte_experimental
597 rte_mem_alloc_validator_unregister(const char *name, int socket_id);
603 #endif /* _RTE_MEMORY_H_ */