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