4cf8ea9c05fd66878417d6bfd971b849142cc16e
[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 enum rte_page_sizes {
56         RTE_PGSIZE_4K = 1 << 12,
57         RTE_PGSIZE_2M = RTE_PGSIZE_4K << 9,
58         RTE_PGSIZE_1G = RTE_PGSIZE_2M <<9
59 };
60
61 #define SOCKET_ID_ANY -1                    /**< Any NUMA socket. */
62 #ifndef CACHE_LINE_SIZE
63 #define CACHE_LINE_SIZE 64                  /**< Cache line size. */
64 #endif
65 #define CACHE_LINE_MASK (CACHE_LINE_SIZE-1) /**< Cache line mask. */
66
67 #define CACHE_LINE_ROUNDUP(size) \
68         (CACHE_LINE_SIZE * ((size + CACHE_LINE_SIZE - 1) / CACHE_LINE_SIZE))
69 /**< Return the first cache-aligned value greater or equal to size. */
70
71 /**
72  * Force alignment to cache line.
73  */
74 #define __rte_cache_aligned __attribute__((__aligned__(CACHE_LINE_SIZE)))
75
76 typedef uint64_t phys_addr_t; /**< Physical address definition. */
77 #define RTE_BAD_PHYS_ADDR ((phys_addr_t)-1)
78
79 /**
80  * Physical memory segment descriptor.
81  */
82 struct rte_memseg {
83         phys_addr_t phys_addr;      /**< Start physical address. */
84         union {
85                 void *addr;         /**< Start virtual address. */
86                 uint64_t addr_64;   /**< Makes sure addr is always 64 bits */
87         };
88 #ifdef RTE_LIBRTE_IVSHMEM
89         phys_addr_t ioremap_addr; /**< Real physical address inside the VM */
90 #endif
91         size_t len;               /**< Length of the segment. */
92         size_t hugepage_sz;       /**< The pagesize of underlying memory */
93         int32_t socket_id;          /**< NUMA socket ID. */
94         uint32_t nchannel;          /**< Number of channels. */
95         uint32_t nrank;             /**< Number of ranks. */
96 #ifdef RTE_LIBRTE_XEN_DOM0
97          /**< store segment MFNs */
98         uint64_t mfn[DOM0_NUM_MEMBLOCK];
99 #endif
100 } __attribute__((__packed__));
101
102 /**
103  * Lock page in physical memory and prevent from swapping.
104  *
105  * @param virt
106  *   The virtual address.
107  * @return
108  *   0 on success, negative on error.
109  */
110 int rte_mem_lock_page(const void *virt);
111
112 /**
113  * Get physical address of any mapped virtual address in the current process.
114  * It is found by browsing the /proc/self/pagemap special file.
115  * The page must be locked.
116  *
117  * @param virt
118  *   The virtual address.
119  * @return
120  *   The physical address or RTE_BAD_PHYS_ADDR on error.
121  */
122 phys_addr_t rte_mem_virt2phy(const void *virt);
123
124 /**
125  * Get the layout of the available physical memory.
126  *
127  * It can be useful for an application to have the full physical
128  * memory layout to decide the size of a memory zone to reserve. This
129  * table is stored in rte_config (see rte_eal_get_configuration()).
130  *
131  * @return
132  *  - On success, return a pointer to a read-only table of struct
133  *    rte_physmem_desc elements, containing the layout of all
134  *    addressable physical memory. The last element of the table
135  *    contains a NULL address.
136  *  - On error, return NULL. This should not happen since it is a fatal
137  *    error that will probably cause the entire system to panic.
138  */
139 const struct rte_memseg *rte_eal_get_physmem_layout(void);
140
141 /**
142  * Dump the physical memory layout to the console.
143  *
144  * @param f
145  *   A pointer to a file for output
146  */
147 void rte_dump_physmem_layout(FILE *f);
148
149 /**
150  * Get the total amount of available physical memory.
151  *
152  * @return
153  *    The total amount of available physical memory in bytes.
154  */
155 uint64_t rte_eal_get_physmem_size(void);
156
157 /**
158  * Get the number of memory channels.
159  *
160  * @return
161  *   The number of memory channels on the system. The value is 0 if unknown
162  *   or not the same on all devices.
163  */
164 unsigned rte_memory_get_nchannel(void);
165
166 /**
167  * Get the number of memory ranks.
168  *
169  * @return
170  *   The number of memory ranks on the system. The value is 0 if unknown or
171  *   not the same on all devices.
172  */
173 unsigned rte_memory_get_nrank(void);
174
175 #ifdef RTE_LIBRTE_XEN_DOM0
176 /**
177  * Return the physical address of elt, which is an element of the pool mp.
178  *
179  * @param memseg_id
180  *   The mempool is from which memory segment.
181  * @param phy_addr
182  *   physical address of elt.
183  *
184  * @return
185  *   The physical address or error.
186  */
187 phys_addr_t rte_mem_phy2mch(uint32_t memseg_id, const phys_addr_t phy_addr);
188
189 /**
190  * Memory init for supporting application running on Xen domain0.
191  *
192  * @param void
193  *
194  * @return
195  *       0: successfully
196  *       negative: error
197  */
198 int rte_xen_dom0_memory_init(void);
199
200 /**
201  * Attach to memory setments of primary process on Xen domain0.
202  *
203  * @param void
204  *
205  * @return
206  *       0: successfully
207  *       negative: error
208  */
209 int rte_xen_dom0_memory_attach(void);
210 #endif
211 #ifdef __cplusplus
212 }
213 #endif
214
215 #endif /* _RTE_MEMORY_H_ */