config: clean cache line size selection scheme
[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 RTE_EXEC_ENV_LINUXAPP
48 #include <exec-env/rte_dom0_common.h>
49 #endif
50
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54
55 #include <rte_common.h>
56
57 enum rte_page_sizes {
58         RTE_PGSIZE_4K    = 1ULL << 12,
59         RTE_PGSIZE_64K   = 1ULL << 16,
60         RTE_PGSIZE_256K  = 1ULL << 18,
61         RTE_PGSIZE_2M    = 1ULL << 21,
62         RTE_PGSIZE_16M   = 1ULL << 24,
63         RTE_PGSIZE_256M  = 1ULL << 28,
64         RTE_PGSIZE_512M  = 1ULL << 29,
65         RTE_PGSIZE_1G    = 1ULL << 30,
66         RTE_PGSIZE_4G    = 1ULL << 32,
67         RTE_PGSIZE_16G   = 1ULL << 34,
68 };
69
70 #define SOCKET_ID_ANY -1                    /**< Any NUMA socket. */
71 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1) /**< Cache line mask. */
72
73 #define RTE_CACHE_LINE_ROUNDUP(size) \
74         (RTE_CACHE_LINE_SIZE * ((size + RTE_CACHE_LINE_SIZE - 1) / RTE_CACHE_LINE_SIZE))
75 /**< Return the first cache-aligned value greater or equal to size. */
76
77 /**
78  * Force alignment to cache line.
79  */
80 #define __rte_cache_aligned __rte_aligned(RTE_CACHE_LINE_SIZE)
81
82 typedef uint64_t phys_addr_t; /**< Physical address definition. */
83 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
84
85 /**
86  * Physical memory segment descriptor.
87  */
88 struct rte_memseg {
89         phys_addr_t phys_addr;      /**< Start physical address. */
90         union {
91                 void *addr;         /**< Start virtual address. */
92                 uint64_t addr_64;   /**< Makes sure addr is always 64 bits */
93         };
94 #ifdef RTE_LIBRTE_IVSHMEM
95         phys_addr_t ioremap_addr; /**< Real physical address inside the VM */
96 #endif
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. */
102 #ifdef RTE_LIBRTE_XEN_DOM0
103          /**< store segment MFNs */
104         uint64_t mfn[DOM0_NUM_MEMBLOCK];
105 #endif
106 } __rte_packed;
107
108 /**
109  * Lock page in physical memory and prevent from swapping.
110  *
111  * @param virt
112  *   The virtual address.
113  * @return
114  *   0 on success, negative on error.
115  */
116 int rte_mem_lock_page(const void *virt);
117
118 /**
119  * Get physical address of any mapped virtual address in the current process.
120  * It is found by browsing the /proc/self/pagemap special file.
121  * The page must be locked.
122  *
123  * @param virt
124  *   The virtual address.
125  * @return
126  *   The physical address or RTE_BAD_PHYS_ADDR on error.
127  */
128 phys_addr_t rte_mem_virt2phy(const void *virt);
129
130 /**
131  * Get the layout of the available physical memory.
132  *
133  * It can be useful for an application to have the full physical
134  * memory layout to decide the size of a memory zone to reserve. This
135  * table is stored in rte_config (see rte_eal_get_configuration()).
136  *
137  * @return
138  *  - On success, return a pointer to a read-only table of struct
139  *    rte_physmem_desc elements, containing the layout of all
140  *    addressable physical memory. The last element of the table
141  *    contains a NULL address.
142  *  - On error, return NULL. This should not happen since it is a fatal
143  *    error that will probably cause the entire system to panic.
144  */
145 const struct rte_memseg *rte_eal_get_physmem_layout(void);
146
147 /**
148  * Dump the physical memory layout to the console.
149  *
150  * @param f
151  *   A pointer to a file for output
152  */
153 void rte_dump_physmem_layout(FILE *f);
154
155 /**
156  * Get the total amount of available physical memory.
157  *
158  * @return
159  *    The total amount of available physical memory in bytes.
160  */
161 uint64_t rte_eal_get_physmem_size(void);
162
163 /**
164  * Get the number of memory channels.
165  *
166  * @return
167  *   The number of memory channels on the system. The value is 0 if unknown
168  *   or not the same on all devices.
169  */
170 unsigned rte_memory_get_nchannel(void);
171
172 /**
173  * Get the number of memory ranks.
174  *
175  * @return
176  *   The number of memory ranks on the system. The value is 0 if unknown or
177  *   not the same on all devices.
178  */
179 unsigned rte_memory_get_nrank(void);
180
181 #ifdef RTE_LIBRTE_XEN_DOM0
182
183 /**< Internal use only - should DOM0 memory mapping be used */
184 int rte_xen_dom0_supported(void);
185
186 /**< Internal use only - phys to virt mapping for xen */
187 phys_addr_t rte_xen_mem_phy2mch(uint32_t, const phys_addr_t);
188
189 /**
190  * Return the physical address of elt, which is an element of the pool mp.
191  *
192  * @param memseg_id
193  *   The mempool is from which memory segment.
194  * @param phy_addr
195  *   physical address of elt.
196  *
197  * @return
198  *   The physical address or error.
199  */
200 static inline phys_addr_t
201 rte_mem_phy2mch(uint32_t memseg_id, const phys_addr_t phy_addr)
202 {
203         if (rte_xen_dom0_supported())
204                 return rte_xen_mem_phy2mch(memseg_id, phy_addr);
205         else
206                 return phy_addr;
207 }
208
209 /**
210  * Memory init for supporting application running on Xen domain0.
211  *
212  * @param void
213  *
214  * @return
215  *       0: successfully
216  *       negative: error
217  */
218 int rte_xen_dom0_memory_init(void);
219
220 /**
221  * Attach to memory setments of primary process on Xen domain0.
222  *
223  * @param void
224  *
225  * @return
226  *       0: successfully
227  *       negative: error
228  */
229 int rte_xen_dom0_memory_attach(void);
230 #else
231 static inline int rte_xen_dom0_supported(void)
232 {
233         return 0;
234 }
235
236 static inline phys_addr_t
237 rte_mem_phy2mch(uint32_t memseg_id __rte_unused, const phys_addr_t phy_addr)
238 {
239         return phy_addr;
240 }
241 #endif
242
243 #ifdef __cplusplus
244 }
245 #endif
246
247 #endif /* _RTE_MEMORY_H_ */