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