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