199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause 299a2dd95SBruce Richardson * Copyright(c) 2010-2014 Intel Corporation 399a2dd95SBruce Richardson */ 499a2dd95SBruce Richardson 599a2dd95SBruce Richardson #ifndef _RTE_MEMORY_H_ 699a2dd95SBruce Richardson #define _RTE_MEMORY_H_ 799a2dd95SBruce Richardson 899a2dd95SBruce Richardson /** 999a2dd95SBruce Richardson * @file 1099a2dd95SBruce Richardson * 1199a2dd95SBruce Richardson * Memory-related RTE API. 1299a2dd95SBruce Richardson */ 1399a2dd95SBruce Richardson 1499a2dd95SBruce Richardson #include <stdint.h> 1599a2dd95SBruce Richardson #include <stddef.h> 1699a2dd95SBruce Richardson #include <stdio.h> 1799a2dd95SBruce Richardson 182edd037cSDmitry Kozlyuk #include <rte_bitops.h> 1999a2dd95SBruce Richardson #include <rte_common.h> 2099a2dd95SBruce Richardson #include <rte_config.h> 214dd146e5SArtemy Kovalyov #include <rte_eal_memconfig.h> 2299a2dd95SBruce Richardson #include <rte_fbarray.h> 2399a2dd95SBruce Richardson 24719834a6SMattias Rönnblom #ifdef __cplusplus 25719834a6SMattias Rönnblom extern "C" { 26719834a6SMattias Rönnblom #endif 27719834a6SMattias Rönnblom 2899a2dd95SBruce Richardson #define RTE_PGSIZE_4K (1ULL << 12) 2999a2dd95SBruce Richardson #define RTE_PGSIZE_64K (1ULL << 16) 3099a2dd95SBruce Richardson #define RTE_PGSIZE_256K (1ULL << 18) 3199a2dd95SBruce Richardson #define RTE_PGSIZE_2M (1ULL << 21) 3299a2dd95SBruce Richardson #define RTE_PGSIZE_16M (1ULL << 24) 3399a2dd95SBruce Richardson #define RTE_PGSIZE_256M (1ULL << 28) 3499a2dd95SBruce Richardson #define RTE_PGSIZE_512M (1ULL << 29) 3599a2dd95SBruce Richardson #define RTE_PGSIZE_1G (1ULL << 30) 3699a2dd95SBruce Richardson #define RTE_PGSIZE_4G (1ULL << 32) 3799a2dd95SBruce Richardson #define RTE_PGSIZE_16G (1ULL << 34) 3899a2dd95SBruce Richardson 3999a2dd95SBruce Richardson #define SOCKET_ID_ANY -1 /**< Any NUMA socket. */ 4099a2dd95SBruce Richardson 412edd037cSDmitry Kozlyuk /** Prevent this segment from being freed back to the OS. */ 422edd037cSDmitry Kozlyuk #define RTE_MEMSEG_FLAG_DO_NOT_FREE RTE_BIT32(0) 432edd037cSDmitry Kozlyuk /** This segment is not filled with zeros. */ 442edd037cSDmitry Kozlyuk #define RTE_MEMSEG_FLAG_DIRTY RTE_BIT32(1) 452edd037cSDmitry Kozlyuk 4699a2dd95SBruce Richardson /** 4799a2dd95SBruce Richardson * Physical memory segment descriptor. 4899a2dd95SBruce Richardson */ 49*fba98755SAndre Muezerie struct __rte_packed_begin rte_memseg { 5099a2dd95SBruce Richardson rte_iova_t iova; /**< Start IO address. */ 5199a2dd95SBruce Richardson union { 5299a2dd95SBruce Richardson void *addr; /**< Start virtual address. */ 5399a2dd95SBruce Richardson uint64_t addr_64; /**< Makes sure addr is always 64 bits */ 5499a2dd95SBruce Richardson }; 5599a2dd95SBruce Richardson size_t len; /**< Length of the segment. */ 5699a2dd95SBruce Richardson uint64_t hugepage_sz; /**< The pagesize of underlying memory */ 5799a2dd95SBruce Richardson int32_t socket_id; /**< NUMA socket ID. */ 5899a2dd95SBruce Richardson uint32_t nchannel; /**< Number of channels. */ 5999a2dd95SBruce Richardson uint32_t nrank; /**< Number of ranks. */ 6099a2dd95SBruce Richardson uint32_t flags; /**< Memseg-specific flags */ 61*fba98755SAndre Muezerie } __rte_packed_end; 6299a2dd95SBruce Richardson 6399a2dd95SBruce Richardson /** 6499a2dd95SBruce Richardson * memseg list is a special case as we need to store a bunch of other data 6599a2dd95SBruce Richardson * together with the array itself. 6699a2dd95SBruce Richardson */ 6799a2dd95SBruce Richardson struct rte_memseg_list { 6899a2dd95SBruce Richardson union { 6999a2dd95SBruce Richardson void *base_va; 7099a2dd95SBruce Richardson /**< Base virtual address for this memseg list. */ 7199a2dd95SBruce Richardson uint64_t addr_64; 7299a2dd95SBruce Richardson /**< Makes sure addr is always 64-bits */ 7399a2dd95SBruce Richardson }; 7499a2dd95SBruce Richardson uint64_t page_sz; /**< Page size for all memsegs in this list. */ 7599a2dd95SBruce Richardson int socket_id; /**< Socket ID for all memsegs in this list. */ 7699a2dd95SBruce Richardson volatile uint32_t version; /**< version number for multiprocess sync. */ 7799a2dd95SBruce Richardson size_t len; /**< Length of memory area covered by this memseg list. */ 7899a2dd95SBruce Richardson unsigned int external; /**< 1 if this list points to external memory */ 7999a2dd95SBruce Richardson unsigned int heap; /**< 1 if this list points to a heap */ 8099a2dd95SBruce Richardson struct rte_fbarray memseg_arr; 8199a2dd95SBruce Richardson }; 8299a2dd95SBruce Richardson 8399a2dd95SBruce Richardson /** 8499a2dd95SBruce Richardson * Lock page in physical memory and prevent from swapping. 8599a2dd95SBruce Richardson * 8699a2dd95SBruce Richardson * @param virt 8799a2dd95SBruce Richardson * The virtual address. 8899a2dd95SBruce Richardson * @return 8999a2dd95SBruce Richardson * 0 on success, negative on error. 9099a2dd95SBruce Richardson */ 9199a2dd95SBruce Richardson int rte_mem_lock_page(const void *virt); 9299a2dd95SBruce Richardson 9399a2dd95SBruce Richardson /** 9499a2dd95SBruce Richardson * Get physical address of any mapped virtual address in the current process. 9599a2dd95SBruce Richardson * It is found by browsing the /proc/self/pagemap special file. 9699a2dd95SBruce Richardson * The page must be locked. 9799a2dd95SBruce Richardson * 9899a2dd95SBruce Richardson * @param virt 9999a2dd95SBruce Richardson * The virtual address. 10099a2dd95SBruce Richardson * @return 10199a2dd95SBruce Richardson * The physical address or RTE_BAD_IOVA on error. 10299a2dd95SBruce Richardson */ 10399a2dd95SBruce Richardson phys_addr_t rte_mem_virt2phy(const void *virt); 10499a2dd95SBruce Richardson 10599a2dd95SBruce Richardson /** 10699a2dd95SBruce Richardson * Get IO virtual address of any mapped virtual address in the current process. 10799a2dd95SBruce Richardson * 10899a2dd95SBruce Richardson * @note This function will not check internal page table. Instead, in IOVA as 10999a2dd95SBruce Richardson * PA mode, it will fall back to getting real physical address (which may 11099a2dd95SBruce Richardson * not match the expected IOVA, such as what was specified for external 11199a2dd95SBruce Richardson * memory). 11299a2dd95SBruce Richardson * 11399a2dd95SBruce Richardson * @param virt 11499a2dd95SBruce Richardson * The virtual address. 11599a2dd95SBruce Richardson * @return 11699a2dd95SBruce Richardson * The IO address or RTE_BAD_IOVA on error. 11799a2dd95SBruce Richardson */ 11899a2dd95SBruce Richardson rte_iova_t rte_mem_virt2iova(const void *virt); 11999a2dd95SBruce Richardson 12099a2dd95SBruce Richardson /** 12199a2dd95SBruce Richardson * Get virtual memory address corresponding to iova address. 12299a2dd95SBruce Richardson * 12399a2dd95SBruce Richardson * @note This function read-locks the memory hotplug subsystem, and thus cannot 12499a2dd95SBruce Richardson * be used within memory-related callback functions. 12599a2dd95SBruce Richardson * 12699a2dd95SBruce Richardson * @param iova 12799a2dd95SBruce Richardson * The iova address. 12899a2dd95SBruce Richardson * @return 12999a2dd95SBruce Richardson * Virtual address corresponding to iova address (or NULL if address does not 13099a2dd95SBruce Richardson * exist within DPDK memory map). 13199a2dd95SBruce Richardson */ 13299a2dd95SBruce Richardson void * 13399a2dd95SBruce Richardson rte_mem_iova2virt(rte_iova_t iova); 13499a2dd95SBruce Richardson 13599a2dd95SBruce Richardson /** 13699a2dd95SBruce Richardson * Get memseg to which a particular virtual address belongs. 13799a2dd95SBruce Richardson * 13899a2dd95SBruce Richardson * @param virt 13999a2dd95SBruce Richardson * The virtual address. 14099a2dd95SBruce Richardson * @param msl 14199a2dd95SBruce Richardson * The memseg list in which to look up based on ``virt`` address 14299a2dd95SBruce Richardson * (can be NULL). 14399a2dd95SBruce Richardson * @return 14499a2dd95SBruce Richardson * Memseg pointer on success, or NULL on error. 14599a2dd95SBruce Richardson */ 14699a2dd95SBruce Richardson struct rte_memseg * 14799a2dd95SBruce Richardson rte_mem_virt2memseg(const void *virt, const struct rte_memseg_list *msl); 14899a2dd95SBruce Richardson 14999a2dd95SBruce Richardson /** 15099a2dd95SBruce Richardson * Get memseg list corresponding to virtual memory address. 15199a2dd95SBruce Richardson * 15299a2dd95SBruce Richardson * @param virt 15399a2dd95SBruce Richardson * The virtual address. 15499a2dd95SBruce Richardson * @return 15599a2dd95SBruce Richardson * Memseg list to which this virtual address belongs to. 15699a2dd95SBruce Richardson */ 15799a2dd95SBruce Richardson struct rte_memseg_list * 15899a2dd95SBruce Richardson rte_mem_virt2memseg_list(const void *virt); 15999a2dd95SBruce Richardson 16099a2dd95SBruce Richardson /** 16199a2dd95SBruce Richardson * Memseg walk function prototype. 16299a2dd95SBruce Richardson * 16399a2dd95SBruce Richardson * Returning 0 will continue walk 16499a2dd95SBruce Richardson * Returning 1 will stop the walk 16599a2dd95SBruce Richardson * Returning -1 will stop the walk and report error 16699a2dd95SBruce Richardson */ 16799a2dd95SBruce Richardson typedef int (*rte_memseg_walk_t)(const struct rte_memseg_list *msl, 16899a2dd95SBruce Richardson const struct rte_memseg *ms, void *arg); 16999a2dd95SBruce Richardson 17099a2dd95SBruce Richardson /** 17199a2dd95SBruce Richardson * Memseg contig walk function prototype. This will trigger a callback on every 17299a2dd95SBruce Richardson * VA-contiguous area starting at memseg ``ms``, so total valid VA space at each 17399a2dd95SBruce Richardson * callback call will be [``ms->addr``, ``ms->addr + len``). 17499a2dd95SBruce Richardson * 17599a2dd95SBruce Richardson * Returning 0 will continue walk 17699a2dd95SBruce Richardson * Returning 1 will stop the walk 17799a2dd95SBruce Richardson * Returning -1 will stop the walk and report error 17899a2dd95SBruce Richardson */ 17999a2dd95SBruce Richardson typedef int (*rte_memseg_contig_walk_t)(const struct rte_memseg_list *msl, 18099a2dd95SBruce Richardson const struct rte_memseg *ms, size_t len, void *arg); 18199a2dd95SBruce Richardson 18299a2dd95SBruce Richardson /** 18399a2dd95SBruce Richardson * Memseg list walk function prototype. This will trigger a callback on every 18499a2dd95SBruce Richardson * allocated memseg list. 18599a2dd95SBruce Richardson * 18699a2dd95SBruce Richardson * Returning 0 will continue walk 18799a2dd95SBruce Richardson * Returning 1 will stop the walk 18899a2dd95SBruce Richardson * Returning -1 will stop the walk and report error 18999a2dd95SBruce Richardson */ 19099a2dd95SBruce Richardson typedef int (*rte_memseg_list_walk_t)(const struct rte_memseg_list *msl, 19199a2dd95SBruce Richardson void *arg); 19299a2dd95SBruce Richardson 19399a2dd95SBruce Richardson /** 19499a2dd95SBruce Richardson * Walk list of all memsegs. 19599a2dd95SBruce Richardson * 19699a2dd95SBruce Richardson * @note This function read-locks the memory hotplug subsystem, and thus cannot 19799a2dd95SBruce Richardson * be used within memory-related callback functions. 19899a2dd95SBruce Richardson * 19999a2dd95SBruce Richardson * @note This function will also walk through externally allocated segments. It 20099a2dd95SBruce Richardson * is up to the user to decide whether to skip through these segments. 20199a2dd95SBruce Richardson * 20299a2dd95SBruce Richardson * @param func 20399a2dd95SBruce Richardson * Iterator function 20499a2dd95SBruce Richardson * @param arg 20599a2dd95SBruce Richardson * Argument passed to iterator 20699a2dd95SBruce Richardson * @return 20799a2dd95SBruce Richardson * 0 if walked over the entire list 20899a2dd95SBruce Richardson * 1 if stopped by the user 20999a2dd95SBruce Richardson * -1 if user function reported error 21099a2dd95SBruce Richardson */ 21199a2dd95SBruce Richardson int 21299a2dd95SBruce Richardson rte_memseg_walk(rte_memseg_walk_t func, void *arg); 21399a2dd95SBruce Richardson 21499a2dd95SBruce Richardson /** 21599a2dd95SBruce Richardson * Walk each VA-contiguous area. 21699a2dd95SBruce Richardson * 21799a2dd95SBruce Richardson * @note This function read-locks the memory hotplug subsystem, and thus cannot 21899a2dd95SBruce Richardson * be used within memory-related callback functions. 21999a2dd95SBruce Richardson * 22099a2dd95SBruce Richardson * @note This function will also walk through externally allocated segments. It 22199a2dd95SBruce Richardson * is up to the user to decide whether to skip through these segments. 22299a2dd95SBruce Richardson * 22399a2dd95SBruce Richardson * @param func 22499a2dd95SBruce Richardson * Iterator function 22599a2dd95SBruce Richardson * @param arg 22699a2dd95SBruce Richardson * Argument passed to iterator 22799a2dd95SBruce Richardson * @return 22899a2dd95SBruce Richardson * 0 if walked over the entire list 22999a2dd95SBruce Richardson * 1 if stopped by the user 23099a2dd95SBruce Richardson * -1 if user function reported error 23199a2dd95SBruce Richardson */ 23299a2dd95SBruce Richardson int 23399a2dd95SBruce Richardson rte_memseg_contig_walk(rte_memseg_contig_walk_t func, void *arg); 23499a2dd95SBruce Richardson 23599a2dd95SBruce Richardson /** 23699a2dd95SBruce Richardson * Walk each allocated memseg list. 23799a2dd95SBruce Richardson * 23899a2dd95SBruce Richardson * @note This function read-locks the memory hotplug subsystem, and thus cannot 23999a2dd95SBruce Richardson * be used within memory-related callback functions. 24099a2dd95SBruce Richardson * 24199a2dd95SBruce Richardson * @note This function will also walk through externally allocated segments. It 24299a2dd95SBruce Richardson * is up to the user to decide whether to skip through these segments. 24399a2dd95SBruce Richardson * 24499a2dd95SBruce Richardson * @param func 24599a2dd95SBruce Richardson * Iterator function 24699a2dd95SBruce Richardson * @param arg 24799a2dd95SBruce Richardson * Argument passed to iterator 24899a2dd95SBruce Richardson * @return 24999a2dd95SBruce Richardson * 0 if walked over the entire list 25099a2dd95SBruce Richardson * 1 if stopped by the user 25199a2dd95SBruce Richardson * -1 if user function reported error 25299a2dd95SBruce Richardson */ 25399a2dd95SBruce Richardson int 2544dd146e5SArtemy Kovalyov rte_memseg_list_walk(rte_memseg_list_walk_t func, void *arg) 2554dd146e5SArtemy Kovalyov __rte_locks_excluded(rte_mcfg_mem_get_lock()); 25699a2dd95SBruce Richardson 25799a2dd95SBruce Richardson /** 25899a2dd95SBruce Richardson * Walk list of all memsegs without performing any locking. 25999a2dd95SBruce Richardson * 26099a2dd95SBruce Richardson * @note This function does not perform any locking, and is only safe to call 26199a2dd95SBruce Richardson * from within memory-related callback functions. 26299a2dd95SBruce Richardson * 26399a2dd95SBruce Richardson * @param func 26499a2dd95SBruce Richardson * Iterator function 26599a2dd95SBruce Richardson * @param arg 26699a2dd95SBruce Richardson * Argument passed to iterator 26799a2dd95SBruce Richardson * @return 26899a2dd95SBruce Richardson * 0 if walked over the entire list 26999a2dd95SBruce Richardson * 1 if stopped by the user 27099a2dd95SBruce Richardson * -1 if user function reported error 27199a2dd95SBruce Richardson */ 27299a2dd95SBruce Richardson int 27399a2dd95SBruce Richardson rte_memseg_walk_thread_unsafe(rte_memseg_walk_t func, void *arg); 27499a2dd95SBruce Richardson 27599a2dd95SBruce Richardson /** 27699a2dd95SBruce Richardson * Walk each VA-contiguous area without performing any locking. 27799a2dd95SBruce Richardson * 27899a2dd95SBruce Richardson * @note This function does not perform any locking, and is only safe to call 27999a2dd95SBruce Richardson * from within memory-related callback functions. 28099a2dd95SBruce Richardson * 28199a2dd95SBruce Richardson * @param func 28299a2dd95SBruce Richardson * Iterator function 28399a2dd95SBruce Richardson * @param arg 28499a2dd95SBruce Richardson * Argument passed to iterator 28599a2dd95SBruce Richardson * @return 28699a2dd95SBruce Richardson * 0 if walked over the entire list 28799a2dd95SBruce Richardson * 1 if stopped by the user 28899a2dd95SBruce Richardson * -1 if user function reported error 28999a2dd95SBruce Richardson */ 29099a2dd95SBruce Richardson int 29199a2dd95SBruce Richardson rte_memseg_contig_walk_thread_unsafe(rte_memseg_contig_walk_t func, void *arg); 29299a2dd95SBruce Richardson 29399a2dd95SBruce Richardson /** 29499a2dd95SBruce Richardson * Walk each allocated memseg list without performing any locking. 29599a2dd95SBruce Richardson * 29699a2dd95SBruce Richardson * @note This function does not perform any locking, and is only safe to call 29799a2dd95SBruce Richardson * from within memory-related callback functions. 29899a2dd95SBruce Richardson * 29999a2dd95SBruce Richardson * @param func 30099a2dd95SBruce Richardson * Iterator function 30199a2dd95SBruce Richardson * @param arg 30299a2dd95SBruce Richardson * Argument passed to iterator 30399a2dd95SBruce Richardson * @return 30499a2dd95SBruce Richardson * 0 if walked over the entire list 30599a2dd95SBruce Richardson * 1 if stopped by the user 30699a2dd95SBruce Richardson * -1 if user function reported error 30799a2dd95SBruce Richardson */ 30899a2dd95SBruce Richardson int 30999a2dd95SBruce Richardson rte_memseg_list_walk_thread_unsafe(rte_memseg_list_walk_t func, void *arg); 31099a2dd95SBruce Richardson 31199a2dd95SBruce Richardson /** 31299a2dd95SBruce Richardson * Return file descriptor associated with a particular memseg (if available). 31399a2dd95SBruce Richardson * 31499a2dd95SBruce Richardson * @note This function read-locks the memory hotplug subsystem, and thus cannot 31599a2dd95SBruce Richardson * be used within memory-related callback functions. 31699a2dd95SBruce Richardson * 31799a2dd95SBruce Richardson * @note This returns an internal file descriptor. Performing any operations on 31899a2dd95SBruce Richardson * this file descriptor is inherently dangerous, so it should be treated 31999a2dd95SBruce Richardson * as read-only for all intents and purposes. 32099a2dd95SBruce Richardson * 32199a2dd95SBruce Richardson * @param ms 32299a2dd95SBruce Richardson * A pointer to memseg for which to get file descriptor. 32399a2dd95SBruce Richardson * 32499a2dd95SBruce Richardson * @return 32599a2dd95SBruce Richardson * Valid file descriptor in case of success. 32699a2dd95SBruce Richardson * -1 in case of error, with ``rte_errno`` set to the following values: 32799a2dd95SBruce Richardson * - EINVAL - ``ms`` pointer was NULL or did not point to a valid memseg 32899a2dd95SBruce Richardson * - ENODEV - ``ms`` fd is not available 32999a2dd95SBruce Richardson * - ENOENT - ``ms`` is an unused segment 33099a2dd95SBruce Richardson * - ENOTSUP - segment fd's are not supported 33199a2dd95SBruce Richardson */ 33299a2dd95SBruce Richardson int 33399a2dd95SBruce Richardson rte_memseg_get_fd(const struct rte_memseg *ms); 33499a2dd95SBruce Richardson 33599a2dd95SBruce Richardson /** 33699a2dd95SBruce Richardson * Return file descriptor associated with a particular memseg (if available). 33799a2dd95SBruce Richardson * 33899a2dd95SBruce Richardson * @note This function does not perform any locking, and is only safe to call 33999a2dd95SBruce Richardson * from within memory-related callback functions. 34099a2dd95SBruce Richardson * 34199a2dd95SBruce Richardson * @note This returns an internal file descriptor. Performing any operations on 34299a2dd95SBruce Richardson * this file descriptor is inherently dangerous, so it should be treated 34399a2dd95SBruce Richardson * as read-only for all intents and purposes. 34499a2dd95SBruce Richardson * 34599a2dd95SBruce Richardson * @param ms 34699a2dd95SBruce Richardson * A pointer to memseg for which to get file descriptor. 34799a2dd95SBruce Richardson * 34899a2dd95SBruce Richardson * @return 34999a2dd95SBruce Richardson * Valid file descriptor in case of success. 35099a2dd95SBruce Richardson * -1 in case of error, with ``rte_errno`` set to the following values: 35199a2dd95SBruce Richardson * - EINVAL - ``ms`` pointer was NULL or did not point to a valid memseg 35299a2dd95SBruce Richardson * - ENODEV - ``ms`` fd is not available 35399a2dd95SBruce Richardson * - ENOENT - ``ms`` is an unused segment 35499a2dd95SBruce Richardson * - ENOTSUP - segment fd's are not supported 35599a2dd95SBruce Richardson */ 35699a2dd95SBruce Richardson int 35799a2dd95SBruce Richardson rte_memseg_get_fd_thread_unsafe(const struct rte_memseg *ms); 35899a2dd95SBruce Richardson 35999a2dd95SBruce Richardson /** 36099a2dd95SBruce Richardson * Get offset into segment file descriptor associated with a particular memseg 36199a2dd95SBruce Richardson * (if available). 36299a2dd95SBruce Richardson * 36399a2dd95SBruce Richardson * @note This function read-locks the memory hotplug subsystem, and thus cannot 36499a2dd95SBruce Richardson * be used within memory-related callback functions. 36599a2dd95SBruce Richardson * 36699a2dd95SBruce Richardson * @param ms 36799a2dd95SBruce Richardson * A pointer to memseg for which to get file descriptor. 36899a2dd95SBruce Richardson * @param offset 36999a2dd95SBruce Richardson * A pointer to offset value where the result will be stored. 37099a2dd95SBruce Richardson * 37199a2dd95SBruce Richardson * @return 37299a2dd95SBruce Richardson * Valid file descriptor in case of success. 37399a2dd95SBruce Richardson * -1 in case of error, with ``rte_errno`` set to the following values: 37499a2dd95SBruce Richardson * - EINVAL - ``ms`` pointer was NULL or did not point to a valid memseg 37599a2dd95SBruce Richardson * - EINVAL - ``offset`` pointer was NULL 37699a2dd95SBruce Richardson * - ENODEV - ``ms`` fd is not available 37799a2dd95SBruce Richardson * - ENOENT - ``ms`` is an unused segment 37899a2dd95SBruce Richardson * - ENOTSUP - segment fd's are not supported 37999a2dd95SBruce Richardson */ 38099a2dd95SBruce Richardson int 38199a2dd95SBruce Richardson rte_memseg_get_fd_offset(const struct rte_memseg *ms, size_t *offset); 38299a2dd95SBruce Richardson 38399a2dd95SBruce Richardson /** 38499a2dd95SBruce Richardson * Get offset into segment file descriptor associated with a particular memseg 38599a2dd95SBruce Richardson * (if available). 38699a2dd95SBruce Richardson * 38799a2dd95SBruce Richardson * @note This function does not perform any locking, and is only safe to call 38899a2dd95SBruce Richardson * from within memory-related callback functions. 38999a2dd95SBruce Richardson * 39099a2dd95SBruce Richardson * @param ms 39199a2dd95SBruce Richardson * A pointer to memseg for which to get file descriptor. 39299a2dd95SBruce Richardson * @param offset 39399a2dd95SBruce Richardson * A pointer to offset value where the result will be stored. 39499a2dd95SBruce Richardson * 39599a2dd95SBruce Richardson * @return 39699a2dd95SBruce Richardson * Valid file descriptor in case of success. 39799a2dd95SBruce Richardson * -1 in case of error, with ``rte_errno`` set to the following values: 39899a2dd95SBruce Richardson * - EINVAL - ``ms`` pointer was NULL or did not point to a valid memseg 39999a2dd95SBruce Richardson * - EINVAL - ``offset`` pointer was NULL 40099a2dd95SBruce Richardson * - ENODEV - ``ms`` fd is not available 40199a2dd95SBruce Richardson * - ENOENT - ``ms`` is an unused segment 40299a2dd95SBruce Richardson * - ENOTSUP - segment fd's are not supported 40399a2dd95SBruce Richardson */ 40499a2dd95SBruce Richardson int 40599a2dd95SBruce Richardson rte_memseg_get_fd_offset_thread_unsafe(const struct rte_memseg *ms, 40699a2dd95SBruce Richardson size_t *offset); 40799a2dd95SBruce Richardson 40899a2dd95SBruce Richardson /** 40999a2dd95SBruce Richardson * Register external memory chunk with DPDK. 41099a2dd95SBruce Richardson * 41199a2dd95SBruce Richardson * @note Using this API is mutually exclusive with ``rte_malloc`` family of 41299a2dd95SBruce Richardson * API's. 41399a2dd95SBruce Richardson * 41499a2dd95SBruce Richardson * @note This API will not perform any DMA mapping. It is expected that user 41599a2dd95SBruce Richardson * will do that themselves. 41699a2dd95SBruce Richardson * 41799a2dd95SBruce Richardson * @note Before accessing this memory in other processes, it needs to be 41899a2dd95SBruce Richardson * attached in each of those processes by calling ``rte_extmem_attach`` in 41999a2dd95SBruce Richardson * each other process. 42099a2dd95SBruce Richardson * 42199a2dd95SBruce Richardson * @param va_addr 42299a2dd95SBruce Richardson * Start of virtual area to register. Must be aligned by ``page_sz``. 42399a2dd95SBruce Richardson * @param len 42499a2dd95SBruce Richardson * Length of virtual area to register. Must be aligned by ``page_sz``. 42599a2dd95SBruce Richardson * @param iova_addrs 42699a2dd95SBruce Richardson * Array of page IOVA addresses corresponding to each page in this memory 42799a2dd95SBruce Richardson * area. Can be NULL, in which case page IOVA addresses will be set to 42899a2dd95SBruce Richardson * RTE_BAD_IOVA. 42999a2dd95SBruce Richardson * @param n_pages 43099a2dd95SBruce Richardson * Number of elements in the iova_addrs array. Ignored if ``iova_addrs`` 43199a2dd95SBruce Richardson * is NULL. 43299a2dd95SBruce Richardson * @param page_sz 43399a2dd95SBruce Richardson * Page size of the underlying memory 43499a2dd95SBruce Richardson * 43599a2dd95SBruce Richardson * @return 43699a2dd95SBruce Richardson * - 0 on success 43799a2dd95SBruce Richardson * - -1 in case of error, with rte_errno set to one of the following: 43899a2dd95SBruce Richardson * EINVAL - one of the parameters was invalid 43999a2dd95SBruce Richardson * EEXIST - memory chunk is already registered 44099a2dd95SBruce Richardson * ENOSPC - no more space in internal config to store a new memory chunk 44199a2dd95SBruce Richardson */ 44299a2dd95SBruce Richardson int 44399a2dd95SBruce Richardson rte_extmem_register(void *va_addr, size_t len, rte_iova_t iova_addrs[], 44499a2dd95SBruce Richardson unsigned int n_pages, size_t page_sz); 44599a2dd95SBruce Richardson 44699a2dd95SBruce Richardson /** 44799a2dd95SBruce Richardson * Unregister external memory chunk with DPDK. 44899a2dd95SBruce Richardson * 44999a2dd95SBruce Richardson * @note Using this API is mutually exclusive with ``rte_malloc`` family of 45099a2dd95SBruce Richardson * API's. 45199a2dd95SBruce Richardson * 45299a2dd95SBruce Richardson * @note This API will not perform any DMA unmapping. It is expected that user 45399a2dd95SBruce Richardson * will do that themselves. 45499a2dd95SBruce Richardson * 45599a2dd95SBruce Richardson * @note Before calling this function, all other processes must call 45699a2dd95SBruce Richardson * ``rte_extmem_detach`` to detach from the memory area. 45799a2dd95SBruce Richardson * 45899a2dd95SBruce Richardson * @param va_addr 45999a2dd95SBruce Richardson * Start of virtual area to unregister 46099a2dd95SBruce Richardson * @param len 46199a2dd95SBruce Richardson * Length of virtual area to unregister 46299a2dd95SBruce Richardson * 46399a2dd95SBruce Richardson * @return 46499a2dd95SBruce Richardson * - 0 on success 46599a2dd95SBruce Richardson * - -1 in case of error, with rte_errno set to one of the following: 46699a2dd95SBruce Richardson * EINVAL - one of the parameters was invalid 46799a2dd95SBruce Richardson * ENOENT - memory chunk was not found 46899a2dd95SBruce Richardson */ 46999a2dd95SBruce Richardson int 47099a2dd95SBruce Richardson rte_extmem_unregister(void *va_addr, size_t len); 47199a2dd95SBruce Richardson 47299a2dd95SBruce Richardson /** 47399a2dd95SBruce Richardson * Attach to external memory chunk registered in another process. 47499a2dd95SBruce Richardson * 47599a2dd95SBruce Richardson * @note Using this API is mutually exclusive with ``rte_malloc`` family of 47699a2dd95SBruce Richardson * API's. 47799a2dd95SBruce Richardson * 47899a2dd95SBruce Richardson * @note This API will not perform any DMA mapping. It is expected that user 47999a2dd95SBruce Richardson * will do that themselves. 48099a2dd95SBruce Richardson * 48199a2dd95SBruce Richardson * @param va_addr 48299a2dd95SBruce Richardson * Start of virtual area to register 48399a2dd95SBruce Richardson * @param len 48499a2dd95SBruce Richardson * Length of virtual area to register 48599a2dd95SBruce Richardson * 48699a2dd95SBruce Richardson * @return 48799a2dd95SBruce Richardson * - 0 on success 48899a2dd95SBruce Richardson * - -1 in case of error, with rte_errno set to one of the following: 48999a2dd95SBruce Richardson * EINVAL - one of the parameters was invalid 49099a2dd95SBruce Richardson * ENOENT - memory chunk was not found 49199a2dd95SBruce Richardson */ 49299a2dd95SBruce Richardson int 49399a2dd95SBruce Richardson rte_extmem_attach(void *va_addr, size_t len); 49499a2dd95SBruce Richardson 49599a2dd95SBruce Richardson /** 49699a2dd95SBruce Richardson * Detach from external memory chunk registered in another process. 49799a2dd95SBruce Richardson * 49899a2dd95SBruce Richardson * @note Using this API is mutually exclusive with ``rte_malloc`` family of 49999a2dd95SBruce Richardson * API's. 50099a2dd95SBruce Richardson * 50199a2dd95SBruce Richardson * @note This API will not perform any DMA unmapping. It is expected that user 50299a2dd95SBruce Richardson * will do that themselves. 50399a2dd95SBruce Richardson * 50499a2dd95SBruce Richardson * @param va_addr 50599a2dd95SBruce Richardson * Start of virtual area to unregister 50699a2dd95SBruce Richardson * @param len 50799a2dd95SBruce Richardson * Length of virtual area to unregister 50899a2dd95SBruce Richardson * 50999a2dd95SBruce Richardson * @return 51099a2dd95SBruce Richardson * - 0 on success 51199a2dd95SBruce Richardson * - -1 in case of error, with rte_errno set to one of the following: 51299a2dd95SBruce Richardson * EINVAL - one of the parameters was invalid 51399a2dd95SBruce Richardson * ENOENT - memory chunk was not found 51499a2dd95SBruce Richardson */ 51599a2dd95SBruce Richardson int 51699a2dd95SBruce Richardson rte_extmem_detach(void *va_addr, size_t len); 51799a2dd95SBruce Richardson 51899a2dd95SBruce Richardson /** 51999a2dd95SBruce Richardson * Dump the physical memory layout to a file. 52099a2dd95SBruce Richardson * 52199a2dd95SBruce Richardson * @note This function read-locks the memory hotplug subsystem, and thus cannot 52299a2dd95SBruce Richardson * be used within memory-related callback functions. 52399a2dd95SBruce Richardson * 52499a2dd95SBruce Richardson * @param f 52599a2dd95SBruce Richardson * A pointer to a file for output 52699a2dd95SBruce Richardson */ 52799a2dd95SBruce Richardson void rte_dump_physmem_layout(FILE *f); 52899a2dd95SBruce Richardson 52999a2dd95SBruce Richardson /** 53099a2dd95SBruce Richardson * Get the total amount of available physical memory. 53199a2dd95SBruce Richardson * 53299a2dd95SBruce Richardson * @note This function read-locks the memory hotplug subsystem, and thus cannot 53399a2dd95SBruce Richardson * be used within memory-related callback functions. 53499a2dd95SBruce Richardson * 53599a2dd95SBruce Richardson * @return 53699a2dd95SBruce Richardson * The total amount of available physical memory in bytes. 53799a2dd95SBruce Richardson */ 53899a2dd95SBruce Richardson uint64_t rte_eal_get_physmem_size(void); 53999a2dd95SBruce Richardson 54099a2dd95SBruce Richardson /** 54199a2dd95SBruce Richardson * Get the number of memory channels. 54299a2dd95SBruce Richardson * 54399a2dd95SBruce Richardson * @return 54499a2dd95SBruce Richardson * The number of memory channels on the system. The value is 0 if unknown 54599a2dd95SBruce Richardson * or not the same on all devices. 54699a2dd95SBruce Richardson */ 54799a2dd95SBruce Richardson unsigned rte_memory_get_nchannel(void); 54899a2dd95SBruce Richardson 54999a2dd95SBruce Richardson /** 55099a2dd95SBruce Richardson * Get the number of memory ranks. 55199a2dd95SBruce Richardson * 55299a2dd95SBruce Richardson * @return 55399a2dd95SBruce Richardson * The number of memory ranks on the system. The value is 0 if unknown or 55499a2dd95SBruce Richardson * not the same on all devices. 55599a2dd95SBruce Richardson */ 55699a2dd95SBruce Richardson unsigned rte_memory_get_nrank(void); 55799a2dd95SBruce Richardson 55899a2dd95SBruce Richardson /** 55999a2dd95SBruce Richardson * Check if all currently allocated memory segments are compliant with 56099a2dd95SBruce Richardson * supplied DMA address width. 56199a2dd95SBruce Richardson * 56299a2dd95SBruce Richardson * @param maskbits 56399a2dd95SBruce Richardson * Address width to check against. 56499a2dd95SBruce Richardson */ 56599a2dd95SBruce Richardson int rte_mem_check_dma_mask(uint8_t maskbits); 56699a2dd95SBruce Richardson 56799a2dd95SBruce Richardson /** 56899a2dd95SBruce Richardson * Check if all currently allocated memory segments are compliant with 56999a2dd95SBruce Richardson * supplied DMA address width. This function will use 57099a2dd95SBruce Richardson * rte_memseg_walk_thread_unsafe instead of rte_memseg_walk implying 57199a2dd95SBruce Richardson * memory_hotplug_lock will not be acquired avoiding deadlock during 57299a2dd95SBruce Richardson * memory initialization. 57399a2dd95SBruce Richardson * 57499a2dd95SBruce Richardson * This function is just for EAL core memory internal use. Drivers should 57599a2dd95SBruce Richardson * use the previous rte_mem_check_dma_mask. 57699a2dd95SBruce Richardson * 57799a2dd95SBruce Richardson * @param maskbits 57899a2dd95SBruce Richardson * Address width to check against. 57999a2dd95SBruce Richardson */ 58099a2dd95SBruce Richardson int rte_mem_check_dma_mask_thread_unsafe(uint8_t maskbits); 58199a2dd95SBruce Richardson 58299a2dd95SBruce Richardson /** 58399a2dd95SBruce Richardson * Set dma mask to use once memory initialization is done. Previous functions 58499a2dd95SBruce Richardson * rte_mem_check_dma_mask and rte_mem_check_dma_mask_thread_unsafe can not be 58599a2dd95SBruce Richardson * used safely until memory has been initialized. 58699a2dd95SBruce Richardson */ 58799a2dd95SBruce Richardson void rte_mem_set_dma_mask(uint8_t maskbits); 58899a2dd95SBruce Richardson 58999a2dd95SBruce Richardson /** 59099a2dd95SBruce Richardson * Drivers based on uio will not load unless physical 59199a2dd95SBruce Richardson * addresses are obtainable. It is only possible to get 59299a2dd95SBruce Richardson * physical addresses when running as a privileged user. 59399a2dd95SBruce Richardson * 59499a2dd95SBruce Richardson * @return 59599a2dd95SBruce Richardson * 1 if the system is able to obtain physical addresses. 59699a2dd95SBruce Richardson * 0 if using DMA addresses through an IOMMU. 59799a2dd95SBruce Richardson */ 59899a2dd95SBruce Richardson int rte_eal_using_phys_addrs(void); 59999a2dd95SBruce Richardson 60099a2dd95SBruce Richardson 60199a2dd95SBruce Richardson /** 60299a2dd95SBruce Richardson * Enum indicating which kind of memory event has happened. Used by callbacks to 60399a2dd95SBruce Richardson * distinguish between memory allocations and deallocations. 60499a2dd95SBruce Richardson */ 60599a2dd95SBruce Richardson enum rte_mem_event { 60699a2dd95SBruce Richardson RTE_MEM_EVENT_ALLOC = 0, /**< Allocation event. */ 60799a2dd95SBruce Richardson RTE_MEM_EVENT_FREE, /**< Deallocation event. */ 60899a2dd95SBruce Richardson }; 60999a2dd95SBruce Richardson #define RTE_MEM_EVENT_CALLBACK_NAME_LEN 64 61099a2dd95SBruce Richardson /**< maximum length of callback name */ 61199a2dd95SBruce Richardson 61299a2dd95SBruce Richardson /** 61399a2dd95SBruce Richardson * Function typedef used to register callbacks for memory events. 61499a2dd95SBruce Richardson */ 61599a2dd95SBruce Richardson typedef void (*rte_mem_event_callback_t)(enum rte_mem_event event_type, 61699a2dd95SBruce Richardson const void *addr, size_t len, void *arg); 61799a2dd95SBruce Richardson 61899a2dd95SBruce Richardson /** 61999a2dd95SBruce Richardson * Function used to register callbacks for memory events. 62099a2dd95SBruce Richardson * 62199a2dd95SBruce Richardson * @note callbacks will happen while memory hotplug subsystem is write-locked, 62299a2dd95SBruce Richardson * therefore some functions (e.g. `rte_memseg_walk()`) will cause a 62399a2dd95SBruce Richardson * deadlock when called from within such callbacks. 62499a2dd95SBruce Richardson * 62599a2dd95SBruce Richardson * @note mem event callbacks not being supported is an expected error condition, 62699a2dd95SBruce Richardson * so user code needs to handle this situation. In these cases, return 62799a2dd95SBruce Richardson * value will be -1, and rte_errno will be set to ENOTSUP. 62899a2dd95SBruce Richardson * 62999a2dd95SBruce Richardson * @param name 63099a2dd95SBruce Richardson * Name associated with specified callback to be added to the list. 63199a2dd95SBruce Richardson * 63299a2dd95SBruce Richardson * @param clb 63399a2dd95SBruce Richardson * Callback function pointer. 63499a2dd95SBruce Richardson * 63599a2dd95SBruce Richardson * @param arg 63699a2dd95SBruce Richardson * Argument to pass to the callback. 63799a2dd95SBruce Richardson * 63899a2dd95SBruce Richardson * @return 63999a2dd95SBruce Richardson * 0 on successful callback register 64099a2dd95SBruce Richardson * -1 on unsuccessful callback register, with rte_errno value indicating 64199a2dd95SBruce Richardson * reason for failure. 64299a2dd95SBruce Richardson */ 64399a2dd95SBruce Richardson int 64499a2dd95SBruce Richardson rte_mem_event_callback_register(const char *name, rte_mem_event_callback_t clb, 64599a2dd95SBruce Richardson void *arg); 64699a2dd95SBruce Richardson 64799a2dd95SBruce Richardson /** 64899a2dd95SBruce Richardson * Function used to unregister callbacks for memory events. 64999a2dd95SBruce Richardson * 65099a2dd95SBruce Richardson * @param name 65199a2dd95SBruce Richardson * Name associated with specified callback to be removed from the list. 65299a2dd95SBruce Richardson * 65399a2dd95SBruce Richardson * @param arg 65499a2dd95SBruce Richardson * Argument to look for among callbacks with specified callback name. 65599a2dd95SBruce Richardson * 65699a2dd95SBruce Richardson * @return 65799a2dd95SBruce Richardson * 0 on successful callback unregister 65899a2dd95SBruce Richardson * -1 on unsuccessful callback unregister, with rte_errno value indicating 65999a2dd95SBruce Richardson * reason for failure. 66099a2dd95SBruce Richardson */ 66199a2dd95SBruce Richardson int 66299a2dd95SBruce Richardson rte_mem_event_callback_unregister(const char *name, void *arg); 66399a2dd95SBruce Richardson 66499a2dd95SBruce Richardson 66599a2dd95SBruce Richardson #define RTE_MEM_ALLOC_VALIDATOR_NAME_LEN 64 66699a2dd95SBruce Richardson /**< maximum length of alloc validator name */ 66799a2dd95SBruce Richardson /** 66899a2dd95SBruce Richardson * Function typedef used to register memory allocation validation callbacks. 66999a2dd95SBruce Richardson * 67099a2dd95SBruce Richardson * Returning 0 will allow allocation attempt to continue. Returning -1 will 67199a2dd95SBruce Richardson * prevent allocation from succeeding. 67299a2dd95SBruce Richardson */ 67399a2dd95SBruce Richardson typedef int (*rte_mem_alloc_validator_t)(int socket_id, 67499a2dd95SBruce Richardson size_t cur_limit, size_t new_len); 67599a2dd95SBruce Richardson 67699a2dd95SBruce Richardson /** 67799a2dd95SBruce Richardson * @brief Register validator callback for memory allocations. 67899a2dd95SBruce Richardson * 67999a2dd95SBruce Richardson * Callbacks registered by this function will be called right before memory 68099a2dd95SBruce Richardson * allocator is about to trigger allocation of more pages from the system if 68199a2dd95SBruce Richardson * said allocation will bring total memory usage above specified limit on 68299a2dd95SBruce Richardson * specified socket. User will be able to cancel pending allocation if callback 68399a2dd95SBruce Richardson * returns -1. 68499a2dd95SBruce Richardson * 68599a2dd95SBruce Richardson * @note callbacks will happen while memory hotplug subsystem is write-locked, 68699a2dd95SBruce Richardson * therefore some functions (e.g. `rte_memseg_walk()`) will cause a 68799a2dd95SBruce Richardson * deadlock when called from within such callbacks. 68899a2dd95SBruce Richardson * 68999a2dd95SBruce Richardson * @note validator callbacks not being supported is an expected error condition, 69099a2dd95SBruce Richardson * so user code needs to handle this situation. In these cases, return 69199a2dd95SBruce Richardson * value will be -1, and rte_errno will be set to ENOTSUP. 69299a2dd95SBruce Richardson * 69399a2dd95SBruce Richardson * @param name 69499a2dd95SBruce Richardson * Name associated with specified callback to be added to the list. 69599a2dd95SBruce Richardson * 69699a2dd95SBruce Richardson * @param clb 69799a2dd95SBruce Richardson * Callback function pointer. 69899a2dd95SBruce Richardson * 69999a2dd95SBruce Richardson * @param socket_id 70099a2dd95SBruce Richardson * Socket ID on which to watch for allocations. 70199a2dd95SBruce Richardson * 70299a2dd95SBruce Richardson * @param limit 70399a2dd95SBruce Richardson * Limit above which to trigger callbacks. 70499a2dd95SBruce Richardson * 70599a2dd95SBruce Richardson * @return 70699a2dd95SBruce Richardson * 0 on successful callback register 70799a2dd95SBruce Richardson * -1 on unsuccessful callback register, with rte_errno value indicating 70899a2dd95SBruce Richardson * reason for failure. 70999a2dd95SBruce Richardson */ 71099a2dd95SBruce Richardson int 71199a2dd95SBruce Richardson rte_mem_alloc_validator_register(const char *name, 71299a2dd95SBruce Richardson rte_mem_alloc_validator_t clb, int socket_id, size_t limit); 71399a2dd95SBruce Richardson 71499a2dd95SBruce Richardson /** 71599a2dd95SBruce Richardson * @brief Unregister validator callback for memory allocations. 71699a2dd95SBruce Richardson * 71799a2dd95SBruce Richardson * @param name 71899a2dd95SBruce Richardson * Name associated with specified callback to be removed from the list. 71999a2dd95SBruce Richardson * 72099a2dd95SBruce Richardson * @param socket_id 72199a2dd95SBruce Richardson * Socket ID on which to watch for allocations. 72299a2dd95SBruce Richardson * 72399a2dd95SBruce Richardson * @return 72499a2dd95SBruce Richardson * 0 on successful callback unregister 72599a2dd95SBruce Richardson * -1 on unsuccessful callback unregister, with rte_errno value indicating 72699a2dd95SBruce Richardson * reason for failure. 72799a2dd95SBruce Richardson */ 72899a2dd95SBruce Richardson int 72999a2dd95SBruce Richardson rte_mem_alloc_validator_unregister(const char *name, int socket_id); 73099a2dd95SBruce Richardson 73199a2dd95SBruce Richardson #ifdef __cplusplus 73299a2dd95SBruce Richardson } 73399a2dd95SBruce Richardson #endif 73499a2dd95SBruce Richardson 73599a2dd95SBruce Richardson #endif /* _RTE_MEMORY_H_ */ 736