4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
34 #ifndef _RTE_MEMORY_H_
35 #define _RTE_MEMORY_H_
40 * Memory-related RTE API.
47 #ifdef RTE_EXEC_ENV_LINUXAPP
48 #include <exec-env/rte_dom0_common.h>
55 #include <rte_common.h>
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,
70 #define SOCKET_ID_ANY -1 /**< Any NUMA socket. */
71 #ifndef RTE_CACHE_LINE_SIZE
72 #define RTE_CACHE_LINE_SIZE 64 /**< Cache line size. */
74 #define RTE_CACHE_LINE_MASK (RTE_CACHE_LINE_SIZE-1) /**< Cache line mask. */
76 #define RTE_CACHE_LINE_ROUNDUP(size) \
77 (RTE_CACHE_LINE_SIZE * ((size + RTE_CACHE_LINE_SIZE - 1) / RTE_CACHE_LINE_SIZE))
78 /**< Return the first cache-aligned value greater or equal to size. */
81 * Force alignment to cache line.
83 #define __rte_cache_aligned __attribute__((__aligned__(RTE_CACHE_LINE_SIZE)))
85 typedef uint64_t phys_addr_t; /**< Physical address definition. */
86 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
89 * Physical memory segment descriptor.
92 phys_addr_t phys_addr; /**< Start physical address. */
94 void *addr; /**< Start virtual address. */
95 uint64_t addr_64; /**< Makes sure addr is always 64 bits */
97 #ifdef RTE_LIBRTE_IVSHMEM
98 phys_addr_t ioremap_addr; /**< Real physical address inside the VM */
100 size_t len; /**< Length of the segment. */
101 uint64_t hugepage_sz; /**< The pagesize of underlying memory */
102 int32_t socket_id; /**< NUMA socket ID. */
103 uint32_t nchannel; /**< Number of channels. */
104 uint32_t nrank; /**< Number of ranks. */
105 #ifdef RTE_LIBRTE_XEN_DOM0
106 /**< store segment MFNs */
107 uint64_t mfn[DOM0_NUM_MEMBLOCK];
109 } __attribute__((__packed__));
112 * Lock page in physical memory and prevent from swapping.
115 * The virtual address.
117 * 0 on success, negative on error.
119 int rte_mem_lock_page(const void *virt);
122 * Get physical address of any mapped virtual address in the current process.
123 * It is found by browsing the /proc/self/pagemap special file.
124 * The page must be locked.
127 * The virtual address.
129 * The physical address or RTE_BAD_PHYS_ADDR on error.
131 phys_addr_t rte_mem_virt2phy(const void *virt);
134 * Get the layout of the available physical memory.
136 * It can be useful for an application to have the full physical
137 * memory layout to decide the size of a memory zone to reserve. This
138 * table is stored in rte_config (see rte_eal_get_configuration()).
141 * - On success, return a pointer to a read-only table of struct
142 * rte_physmem_desc elements, containing the layout of all
143 * addressable physical memory. The last element of the table
144 * contains a NULL address.
145 * - On error, return NULL. This should not happen since it is a fatal
146 * error that will probably cause the entire system to panic.
148 const struct rte_memseg *rte_eal_get_physmem_layout(void);
151 * Dump the physical memory layout to the console.
154 * A pointer to a file for output
156 void rte_dump_physmem_layout(FILE *f);
159 * Get the total amount of available physical memory.
162 * The total amount of available physical memory in bytes.
164 uint64_t rte_eal_get_physmem_size(void);
167 * Get the number of memory channels.
170 * The number of memory channels on the system. The value is 0 if unknown
171 * or not the same on all devices.
173 unsigned rte_memory_get_nchannel(void);
176 * Get the number of memory ranks.
179 * The number of memory ranks on the system. The value is 0 if unknown or
180 * not the same on all devices.
182 unsigned rte_memory_get_nrank(void);
184 #ifdef RTE_LIBRTE_XEN_DOM0
186 /**< Internal use only - should DOM0 memory mapping be used */
187 extern int is_xen_dom0_supported(void);
189 /**< Internal use only - phys to virt mapping for xen */
190 phys_addr_t rte_xen_mem_phy2mch(uint32_t, const phys_addr_t);
193 * Return the physical address of elt, which is an element of the pool mp.
196 * The mempool is from which memory segment.
198 * physical address of elt.
201 * The physical address or error.
203 static inline phys_addr_t
204 rte_mem_phy2mch(uint32_t memseg_id, const phys_addr_t phy_addr)
206 if (is_xen_dom0_supported())
207 return rte_xen_mem_phy2mch(memseg_id, phy_addr);
213 * Memory init for supporting application running on Xen domain0.
221 int rte_xen_dom0_memory_init(void);
224 * Attach to memory setments of primary process on Xen domain0.
232 int rte_xen_dom0_memory_attach(void);
234 static inline int is_xen_dom0_supported(void)
239 static inline phys_addr_t
240 rte_mem_phy2mch(uint32_t memseg_id __rte_unused, const phys_addr_t phy_addr)
250 #endif /* _RTE_MEMORY_H_ */