ce8500bd804ef84b976d93d5a7d03a9704a05a1f
[dpdk.git] / lib / librte_eal / common / include / rte_memory.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 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
35 #ifndef _RTE_MEMORY_H_
36 #define _RTE_MEMORY_H_
37
38 /**
39  * @file
40  *
41  * Memory-related RTE API.
42  */
43
44 #include <stdint.h>
45 #include <stddef.h>
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 enum rte_page_sizes {
52         RTE_PGSIZE_4K = 1 << 12,
53         RTE_PGSIZE_2M = RTE_PGSIZE_4K << 9,
54         RTE_PGSIZE_1G = RTE_PGSIZE_2M <<9
55 };
56
57 #define SOCKET_ID_ANY -1                    /**< Any NUMA socket. */
58 #define CACHE_LINE_SIZE 64                  /**< Cache line size. */
59 #define CACHE_LINE_MASK (CACHE_LINE_SIZE-1) /**< Cache line mask. */
60
61 #define CACHE_LINE_ROUNDUP(size) \
62         (CACHE_LINE_SIZE * ((size + CACHE_LINE_SIZE - 1) / CACHE_LINE_SIZE))
63 /**< Return the first cache-aligned value greater or equal to size. */
64
65 /**
66  * Force alignment to cache line.
67  */
68 #define __rte_cache_aligned __attribute__((__aligned__(CACHE_LINE_SIZE)))
69
70 typedef uint64_t phys_addr_t; /**< Physical address definition. */
71
72 /**
73  * Physical memory segment descriptor.
74  */
75 struct rte_memseg {
76         phys_addr_t phys_addr;      /**< Start physical address. */
77         union {
78                 void *addr;         /**< Start virtual address. */
79                 uint64_t addr_64;   /**< Makes sure addr is always 64 bits */
80         };
81         size_t len;               /**< Length of the segment. */
82         size_t hugepage_sz;       /**< The pagesize of underlying memory */
83         int32_t socket_id;          /**< NUMA socket ID. */
84         uint32_t nchannel;          /**< Number of channels. */
85         uint32_t nrank;             /**< Number of ranks. */
86 } __attribute__((__packed__));
87
88
89 /**
90  * Get the layout of the available physical memory.
91  *
92  * It can be useful for an application to have the full physical
93  * memory layout to decide the size of a memory zone to reserve. This
94  * table is stored in rte_config (see rte_eal_get_configuration()).
95  *
96  * @return
97  *  - On success, return a pointer to a read-only table of struct
98  *    rte_physmem_desc elements, containing the layout of all
99  *    addressable physical memory. The last element of the table
100  *    contains a NULL address.
101  *  - On error, return NULL. This should not happen since it is a fatal
102  *    error that will probably cause the entire system to panic.
103  */
104 const struct rte_memseg *rte_eal_get_physmem_layout(void);
105
106 /**
107  * Dump the physical memory layout to the console.
108  */
109 void rte_dump_physmem_layout(void);
110
111 /**
112  * Get the total amount of available physical memory.
113  *
114  * @return
115  *    The total amount of available physical memory in bytes.
116  */
117 uint64_t rte_eal_get_physmem_size(void);
118
119 /**
120  * Get the number of memory channels.
121  *
122  * @return
123  *   The number of memory channels on the system. The value is 0 if unknown
124  *   or not the same on all devices.
125  */
126 unsigned rte_memory_get_nchannel(void);
127
128 /**
129  * Get the number of memory ranks.
130  *
131  * @return
132  *   The number of memory ranks on the system. The value is 0 if unknown or
133  *   not the same on all devices.
134  */
135 unsigned rte_memory_get_nrank(void);
136
137 #ifdef __cplusplus
138 }
139 #endif
140
141 #endif /* _RTE_MEMORY_H_ */