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