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