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.
89 phys_addr_t phys_addr; /**< deprecated - Start physical address. */
90 rte_iova_t iova; /**< Start IO address. */
94 void *addr; /**< Start virtual address. */
95 uint64_t addr_64; /**< Makes sure addr is always 64 bits */
97 size_t len; /**< Length of the segment. */
98 uint64_t hugepage_sz; /**< The pagesize of underlying memory */
99 int32_t socket_id; /**< NUMA socket ID. */
100 uint32_t nchannel; /**< Number of channels. */
101 uint32_t nrank; /**< Number of ranks. */
105 * Lock page in physical memory and prevent from swapping.
108 * The virtual address.
110 * 0 on success, negative on error.
112 int rte_mem_lock_page(const void *virt);
115 * Get physical address of any mapped virtual address in the current process.
116 * It is found by browsing the /proc/self/pagemap special file.
117 * The page must be locked.
120 * The virtual address.
122 * The physical address or RTE_BAD_IOVA on error.
124 phys_addr_t rte_mem_virt2phy(const void *virt);
127 * Get IO virtual address of any mapped virtual address in the current process.
130 * The virtual address.
132 * The IO address or RTE_BAD_IOVA on error.
134 rte_iova_t rte_mem_virt2iova(const void *virt);
137 * Get virtual memory address corresponding to iova address.
139 * @note This function read-locks the memory hotplug subsystem, and thus cannot
140 * be used within memory-related callback functions.
145 * Virtual address corresponding to iova address (or NULL if address does not
146 * exist within DPDK memory map).
148 __rte_experimental void *
149 rte_mem_iova2virt(rte_iova_t iova);
152 * Get memseg to which a particular virtual address belongs.
155 * The virtual address.
157 * The memseg list in which to look up based on ``virt`` address
160 * Memseg pointer on success, or NULL on error.
162 __rte_experimental struct rte_memseg *
163 rte_mem_virt2memseg(const void *virt, const struct rte_memseg_list *msl);
166 * Get memseg list corresponding to virtual memory address.
169 * The virtual address.
171 * Memseg list to which this virtual address belongs to.
173 __rte_experimental struct rte_memseg_list *
174 rte_mem_virt2memseg_list(const void *virt);
177 * Memseg walk function prototype.
179 * Returning 0 will continue walk
180 * Returning 1 will stop the walk
181 * Returning -1 will stop the walk and report error
183 typedef int (*rte_memseg_walk_t)(const struct rte_memseg_list *msl,
184 const struct rte_memseg *ms, void *arg);
187 * Memseg contig walk function prototype. This will trigger a callback on every
188 * VA-contiguous are starting at memseg ``ms``, so total valid VA space at each
189 * callback call will be [``ms->addr``, ``ms->addr + len``).
191 * Returning 0 will continue walk
192 * Returning 1 will stop the walk
193 * Returning -1 will stop the walk and report error
195 typedef int (*rte_memseg_contig_walk_t)(const struct rte_memseg_list *msl,
196 const struct rte_memseg *ms, size_t len, void *arg);
199 * Memseg list walk function prototype. This will trigger a callback on every
200 * allocated memseg list.
202 * Returning 0 will continue walk
203 * Returning 1 will stop the walk
204 * Returning -1 will stop the walk and report error
206 typedef int (*rte_memseg_list_walk_t)(const struct rte_memseg_list *msl,
210 * Walk list of all memsegs.
212 * @note This function read-locks the memory hotplug subsystem, and thus cannot
213 * be used within memory-related callback functions.
218 * Argument passed to iterator
220 * 0 if walked over the entire list
221 * 1 if stopped by the user
222 * -1 if user function reported error
224 int __rte_experimental
225 rte_memseg_walk(rte_memseg_walk_t func, void *arg);
228 * Walk each VA-contiguous area.
230 * @note This function read-locks the memory hotplug subsystem, and thus cannot
231 * be used within memory-related callback functions.
236 * Argument passed to iterator
238 * 0 if walked over the entire list
239 * 1 if stopped by the user
240 * -1 if user function reported error
242 int __rte_experimental
243 rte_memseg_contig_walk(rte_memseg_contig_walk_t func, void *arg);
246 * Walk each allocated memseg list.
248 * @note This function read-locks the memory hotplug subsystem, and thus cannot
249 * be used within memory-related callback functions.
254 * Argument passed to iterator
256 * 0 if walked over the entire list
257 * 1 if stopped by the user
258 * -1 if user function reported error
260 int __rte_experimental
261 rte_memseg_list_walk(rte_memseg_list_walk_t func, void *arg);
264 * Dump the physical memory layout to a file.
266 * @note This function read-locks the memory hotplug subsystem, and thus cannot
267 * be used within memory-related callback functions.
270 * A pointer to a file for output
272 void rte_dump_physmem_layout(FILE *f);
275 * Get the total amount of available physical memory.
277 * @note This function read-locks the memory hotplug subsystem, and thus cannot
278 * be used within memory-related callback functions.
281 * The total amount of available physical memory in bytes.
283 uint64_t rte_eal_get_physmem_size(void);
286 * Get the number of memory channels.
289 * The number of memory channels on the system. The value is 0 if unknown
290 * or not the same on all devices.
292 unsigned rte_memory_get_nchannel(void);
295 * Get the number of memory ranks.
298 * The number of memory ranks on the system. The value is 0 if unknown or
299 * not the same on all devices.
301 unsigned rte_memory_get_nrank(void);
304 * Drivers based on uio will not load unless physical
305 * addresses are obtainable. It is only possible to get
306 * physical addresses when running as a privileged user.
309 * 1 if the system is able to obtain physical addresses.
310 * 0 if using DMA addresses through an IOMMU.
312 int rte_eal_using_phys_addrs(void);
316 * Enum indicating which kind of memory event has happened. Used by callbacks to
317 * distinguish between memory allocations and deallocations.
320 RTE_MEM_EVENT_ALLOC = 0, /**< Allocation event. */
321 RTE_MEM_EVENT_FREE, /**< Deallocation event. */
323 #define RTE_MEM_EVENT_CALLBACK_NAME_LEN 64
324 /**< maximum length of callback name */
327 * Function typedef used to register callbacks for memory events.
329 typedef void (*rte_mem_event_callback_t)(enum rte_mem_event event_type,
330 const void *addr, size_t len);
333 * Function used to register callbacks for memory events.
335 * @note callbacks will happen while memory hotplug subsystem is write-locked,
336 * therefore some functions (e.g. `rte_memseg_walk()`) will cause a
337 * deadlock when called from within such callbacks.
340 * Name associated with specified callback to be added to the list.
343 * Callback function pointer.
346 * 0 on successful callback register
347 * -1 on unsuccessful callback register, with rte_errno value indicating
348 * reason for failure.
350 int __rte_experimental
351 rte_mem_event_callback_register(const char *name, rte_mem_event_callback_t clb);
354 * Function used to unregister callbacks for memory events.
357 * Name associated with specified callback to be removed from the list.
360 * 0 on successful callback unregister
361 * -1 on unsuccessful callback unregister, with rte_errno value indicating
362 * reason for failure.
364 int __rte_experimental
365 rte_mem_event_callback_unregister(const char *name);
368 #define RTE_MEM_ALLOC_VALIDATOR_NAME_LEN 64
369 /**< maximum length of alloc validator name */
371 * Function typedef used to register memory allocation validation callbacks.
373 * Returning 0 will allow allocation attempt to continue. Returning -1 will
374 * prevent allocation from succeeding.
376 typedef int (*rte_mem_alloc_validator_t)(int socket_id,
377 size_t cur_limit, size_t new_len);
380 * @brief Register validator callback for memory allocations.
382 * Callbacks registered by this function will be called right before memory
383 * allocator is about to trigger allocation of more pages from the system if
384 * said allocation will bring total memory usage above specified limit on
385 * specified socket. User will be able to cancel pending allocation if callback
388 * @note callbacks will happen while memory hotplug subsystem is write-locked,
389 * therefore some functions (e.g. `rte_memseg_walk()`) will cause a
390 * deadlock when called from within such callbacks.
393 * Name associated with specified callback to be added to the list.
396 * Callback function pointer.
399 * Socket ID on which to watch for allocations.
402 * Limit above which to trigger callbacks.
405 * 0 on successful callback register
406 * -1 on unsuccessful callback register, with rte_errno value indicating
407 * reason for failure.
409 int __rte_experimental
410 rte_mem_alloc_validator_register(const char *name,
411 rte_mem_alloc_validator_t clb, int socket_id, size_t limit);
414 * @brief Unregister validator callback for memory allocations.
417 * Name associated with specified callback to be removed from the list.
420 * Socket ID on which to watch for allocations.
423 * 0 on successful callback unregister
424 * -1 on unsuccessful callback unregister, with rte_errno value indicating
425 * reason for failure.
427 int __rte_experimental
428 rte_mem_alloc_validator_unregister(const char *name, int socket_id);
434 #endif /* _RTE_MEMORY_H_ */