New upstream version 17.11.4
[deb_dpdk.git] / lib / librte_eal / common / include / rte_memory.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _RTE_MEMORY_H_
35 #define _RTE_MEMORY_H_
36
37 /**
38  * @file
39  *
40  * Memory-related RTE API.
41  */
42
43 #include <stdint.h>
44 #include <stddef.h>
45 #include <stdio.h>
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 #include <rte_common.h>
52 #include <rte_config.h>
53
54 __extension__
55 enum rte_page_sizes {
56         RTE_PGSIZE_4K    = 1ULL << 12,
57         RTE_PGSIZE_64K   = 1ULL << 16,
58         RTE_PGSIZE_256K  = 1ULL << 18,
59         RTE_PGSIZE_2M    = 1ULL << 21,
60         RTE_PGSIZE_16M   = 1ULL << 24,
61         RTE_PGSIZE_256M  = 1ULL << 28,
62         RTE_PGSIZE_512M  = 1ULL << 29,
63         RTE_PGSIZE_1G    = 1ULL << 30,
64         RTE_PGSIZE_4G    = 1ULL << 32,
65         RTE_PGSIZE_16G   = 1ULL << 34,
66 };
67
68 #define SOCKET_ID_ANY -1                    /**< Any NUMA socket. */
69 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1) /**< Cache line mask. */
70
71 #define RTE_CACHE_LINE_ROUNDUP(size) \
72         (RTE_CACHE_LINE_SIZE * ((size + RTE_CACHE_LINE_SIZE - 1) / RTE_CACHE_LINE_SIZE))
73 /**< Return the first cache-aligned value greater or equal to size. */
74
75 /**< Cache line size in terms of log2 */
76 #if RTE_CACHE_LINE_SIZE == 64
77 #define RTE_CACHE_LINE_SIZE_LOG2 6
78 #elif RTE_CACHE_LINE_SIZE == 128
79 #define RTE_CACHE_LINE_SIZE_LOG2 7
80 #else
81 #error "Unsupported cache line size"
82 #endif
83
84 #define RTE_CACHE_LINE_MIN_SIZE 64      /**< Minimum Cache line size. */
85
86 /**
87  * Force alignment to cache line.
88  */
89 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
90
91 /**
92  * Force minimum cache line alignment.
93  */
94 #define __rte_cache_min_aligned __rte_aligned(RTE_CACHE_LINE_MIN_SIZE)
95
96 typedef uint64_t phys_addr_t; /**< Physical address. */
97 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
98 /**
99  * IO virtual address type.
100  * When the physical addressing mode (IOVA as PA) is in use,
101  * the translation from an IO virtual address (IOVA) to a physical address
102  * is a direct mapping, i.e. the same value.
103  * Otherwise, in virtual mode (IOVA as VA), an IOMMU may do the translation.
104  */
105 typedef uint64_t rte_iova_t;
106 #define RTE_BAD_IOVA ((rte_iova_t)-1)
107
108 /**
109  * Physical memory segment descriptor.
110  */
111 struct rte_memseg {
112         RTE_STD_C11
113         union {
114                 phys_addr_t phys_addr;  /**< deprecated - Start physical address. */
115                 rte_iova_t iova;        /**< Start IO address. */
116         };
117         RTE_STD_C11
118         union {
119                 void *addr;         /**< Start virtual address. */
120                 uint64_t addr_64;   /**< Makes sure addr is always 64 bits */
121         };
122         size_t len;               /**< Length of the segment. */
123         uint64_t hugepage_sz;       /**< The pagesize of underlying memory */
124         int32_t socket_id;          /**< NUMA socket ID. */
125         uint32_t nchannel;          /**< Number of channels. */
126         uint32_t nrank;             /**< Number of ranks. */
127 } __rte_packed;
128
129 /**
130  * Lock page in physical memory and prevent from swapping.
131  *
132  * @param virt
133  *   The virtual address.
134  * @return
135  *   0 on success, negative on error.
136  */
137 int rte_mem_lock_page(const void *virt);
138
139 /**
140  * Get physical address of any mapped virtual address in the current process.
141  * It is found by browsing the /proc/self/pagemap special file.
142  * The page must be locked.
143  *
144  * @param virt
145  *   The virtual address.
146  * @return
147  *   The physical address or RTE_BAD_IOVA on error.
148  */
149 phys_addr_t rte_mem_virt2phy(const void *virt);
150
151 /**
152  * Get IO virtual address of any mapped virtual address in the current process.
153  *
154  * @param virt
155  *   The virtual address.
156  * @return
157  *   The IO address or RTE_BAD_IOVA on error.
158  */
159 rte_iova_t rte_mem_virt2iova(const void *virt);
160
161 /**
162  * Get the layout of the available physical memory.
163  *
164  * It can be useful for an application to have the full physical
165  * memory layout to decide the size of a memory zone to reserve. This
166  * table is stored in rte_config (see rte_eal_get_configuration()).
167  *
168  * @return
169  *  - On success, return a pointer to a read-only table of struct
170  *    rte_physmem_desc elements, containing the layout of all
171  *    addressable physical memory. The last element of the table
172  *    contains a NULL address.
173  *  - On error, return NULL. This should not happen since it is a fatal
174  *    error that will probably cause the entire system to panic.
175  */
176 const struct rte_memseg *rte_eal_get_physmem_layout(void);
177
178 /**
179  * Dump the physical memory layout to a file.
180  *
181  * @param f
182  *   A pointer to a file for output
183  */
184 void rte_dump_physmem_layout(FILE *f);
185
186 /**
187  * Get the total amount of available physical memory.
188  *
189  * @return
190  *    The total amount of available physical memory in bytes.
191  */
192 uint64_t rte_eal_get_physmem_size(void);
193
194 /**
195  * Get the number of memory channels.
196  *
197  * @return
198  *   The number of memory channels on the system. The value is 0 if unknown
199  *   or not the same on all devices.
200  */
201 unsigned rte_memory_get_nchannel(void);
202
203 /**
204  * Get the number of memory ranks.
205  *
206  * @return
207  *   The number of memory ranks on the system. The value is 0 if unknown or
208  *   not the same on all devices.
209  */
210 unsigned rte_memory_get_nrank(void);
211
212 /* check memsegs iovas are within a range based on dma mask */
213 int rte_eal_check_dma_mask(uint8_t maskbits);
214
215 /**
216  * Drivers based on uio will not load unless physical
217  * addresses are obtainable. It is only possible to get
218  * physical addresses when running as a privileged user.
219  *
220  * @return
221  *   1 if the system is able to obtain physical addresses.
222  *   0 if using DMA addresses through an IOMMU.
223  */
224 int rte_eal_using_phys_addrs(void);
225
226 #ifdef __cplusplus
227 }
228 #endif
229
230 #endif /* _RTE_MEMORY_H_ */