lib: use SPDX tag for Intel copyright files
[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
24 __extension__
25 enum rte_page_sizes {
26         RTE_PGSIZE_4K    = 1ULL << 12,
27         RTE_PGSIZE_64K   = 1ULL << 16,
28         RTE_PGSIZE_256K  = 1ULL << 18,
29         RTE_PGSIZE_2M    = 1ULL << 21,
30         RTE_PGSIZE_16M   = 1ULL << 24,
31         RTE_PGSIZE_256M  = 1ULL << 28,
32         RTE_PGSIZE_512M  = 1ULL << 29,
33         RTE_PGSIZE_1G    = 1ULL << 30,
34         RTE_PGSIZE_4G    = 1ULL << 32,
35         RTE_PGSIZE_16G   = 1ULL << 34,
36 };
37
38 #define SOCKET_ID_ANY -1                    /**< Any NUMA socket. */
39 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1) /**< Cache line mask. */
40
41 #define RTE_CACHE_LINE_ROUNDUP(size) \
42         (RTE_CACHE_LINE_SIZE * ((size + RTE_CACHE_LINE_SIZE - 1) / RTE_CACHE_LINE_SIZE))
43 /**< Return the first cache-aligned value greater or equal to size. */
44
45 /**< Cache line size in terms of log2 */
46 #if RTE_CACHE_LINE_SIZE == 64
47 #define RTE_CACHE_LINE_SIZE_LOG2 6
48 #elif RTE_CACHE_LINE_SIZE == 128
49 #define RTE_CACHE_LINE_SIZE_LOG2 7
50 #else
51 #error "Unsupported cache line size"
52 #endif
53
54 #define RTE_CACHE_LINE_MIN_SIZE 64      /**< Minimum Cache line size. */
55
56 /**
57  * Force alignment to cache line.
58  */
59 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
60
61 /**
62  * Force minimum cache line alignment.
63  */
64 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
65
66 typedef uint64_t phys_addr_t; /**< Physical address. */
67 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
68 /**
69  * IO virtual address type.
70  * When the physical addressing mode (IOVA as PA) is in use,
71  * the translation from an IO virtual address (IOVA) to a physical address
72  * is a direct mapping, i.e. the same value.
73  * Otherwise, in virtual mode (IOVA as VA), an IOMMU may do the translation.
74  */
75 typedef uint64_t rte_iova_t;
76 #define RTE_BAD_IOVA ((rte_iova_t)-1)
77
78 /**
79  * Physical memory segment descriptor.
80  */
81 struct rte_memseg {
82         RTE_STD_C11
83         union {
84                 phys_addr_t phys_addr;  /**< deprecated - Start physical address. */
85                 rte_iova_t iova;        /**< Start IO address. */
86         };
87         RTE_STD_C11
88         union {
89                 void *addr;         /**< Start virtual address. */
90                 uint64_t addr_64;   /**< Makes sure addr is always 64 bits */
91         };
92         size_t len;               /**< Length of the segment. */
93         uint64_t hugepage_sz;       /**< The pagesize of underlying memory */
94         int32_t socket_id;          /**< NUMA socket ID. */
95         uint32_t nchannel;          /**< Number of channels. */
96         uint32_t nrank;             /**< Number of ranks. */
97 } __rte_packed;
98
99 /**
100  * Lock page in physical memory and prevent from swapping.
101  *
102  * @param virt
103  *   The virtual address.
104  * @return
105  *   0 on success, negative on error.
106  */
107 int rte_mem_lock_page(const void *virt);
108
109 /**
110  * Get physical address of any mapped virtual address in the current process.
111  * It is found by browsing the /proc/self/pagemap special file.
112  * The page must be locked.
113  *
114  * @param virt
115  *   The virtual address.
116  * @return
117  *   The physical address or RTE_BAD_IOVA on error.
118  */
119 phys_addr_t rte_mem_virt2phy(const void *virt);
120
121 /**
122  * Get IO virtual address of any mapped virtual address in the current process.
123  *
124  * @param virt
125  *   The virtual address.
126  * @return
127  *   The IO address or RTE_BAD_IOVA on error.
128  */
129 rte_iova_t rte_mem_virt2iova(const void *virt);
130
131 /**
132  * Get the layout of the available physical memory.
133  *
134  * It can be useful for an application to have the full physical
135  * memory layout to decide the size of a memory zone to reserve. This
136  * table is stored in rte_config (see rte_eal_get_configuration()).
137  *
138  * @return
139  *  - On success, return a pointer to a read-only table of struct
140  *    rte_physmem_desc elements, containing the layout of all
141  *    addressable physical memory. The last element of the table
142  *    contains a NULL address.
143  *  - On error, return NULL. This should not happen since it is a fatal
144  *    error that will probably cause the entire system to panic.
145  */
146 const struct rte_memseg *rte_eal_get_physmem_layout(void);
147
148 /**
149  * Dump the physical memory layout to a file.
150  *
151  * @param f
152  *   A pointer to a file for output
153  */
154 void rte_dump_physmem_layout(FILE *f);
155
156 /**
157  * Get the total amount of available physical memory.
158  *
159  * @return
160  *    The total amount of available physical memory in bytes.
161  */
162 uint64_t rte_eal_get_physmem_size(void);
163
164 /**
165  * Get the number of memory channels.
166  *
167  * @return
168  *   The number of memory channels on the system. The value is 0 if unknown
169  *   or not the same on all devices.
170  */
171 unsigned rte_memory_get_nchannel(void);
172
173 /**
174  * Get the number of memory ranks.
175  *
176  * @return
177  *   The number of memory ranks on the system. The value is 0 if unknown or
178  *   not the same on all devices.
179  */
180 unsigned rte_memory_get_nrank(void);
181
182 /**
183  * Drivers based on uio will not load unless physical
184  * addresses are obtainable. It is only possible to get
185  * physical addresses when running as a privileged user.
186  *
187  * @return
188  *   1 if the system is able to obtain physical addresses.
189  *   0 if using DMA addresses through an IOMMU.
190  */
191 int rte_eal_using_phys_addrs(void);
192
193 #ifdef __cplusplus
194 }
195 #endif
196
197 #endif /* _RTE_MEMORY_H_ */