blob: 9cd652971a664fe645571f528ba983d16a2eac11 [file] [log] [blame]
Damjan Marion68b4da62018-09-30 18:26:20 +02001/*
2 * Copyright (c) 2018 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef included_palloc_h
17#define included_palloc_h
18#include <vppinfra/format.h>
19#include <vppinfra/pool.h>
20
21#define PMALLOC_LOG2_BLOCK_SZ CLIB_LOG2_CACHE_LINE_BYTES
22#define PMALLOC_BLOCK_SZ (1 << 6)
23
24#define CLIB_PMALLOC_NUMA_LOCAL 0xffffffff
25
26typedef struct
27{
28 u32 start, prev, next;
29 u32 size:31;
30 u32 used:1;
31} clib_pmalloc_chunk_t;
32
33STATIC_ASSERT_SIZEOF (clib_pmalloc_chunk_t, 16);
34
35typedef struct
36{
37 u32 index;
38 u32 arena_index;
39 uword pa;
40 clib_pmalloc_chunk_t *chunks;
41 u32 first_chunk_index;
42 u32 n_free_chunks;
43 u32 n_free_blocks;
44} clib_pmalloc_page_t;
45
46typedef struct
47{
48 u32 index;
49 u32 flags;
50#define CLIB_PMALLOC_ARENA_F_SHARED_MEM (1 << 0)
51 int fd;
52 u32 numa_node;
53 u32 first_page_index;
Damjan Marion567e61d2018-10-24 17:08:26 +020054 u32 log2_subpage_sz;
55 u32 subpages_per_page;
Damjan Marion68b4da62018-09-30 18:26:20 +020056 u32 n_pages;
57 u8 *name;
58 u32 *page_indices;
59} clib_pmalloc_arena_t;
60
61typedef struct
62{
Damjan Marionc04e2b02018-10-25 15:56:04 +020063 /* flags */
64 u32 flags;
65#define CLIB_PMALLOC_F_NO_PAGEMAP (1 << 0)
66
Damjan Marion567e61d2018-10-24 17:08:26 +020067 /* base VA address */
Damjan Marion68b4da62018-09-30 18:26:20 +020068 u8 *base;
Damjan Marion567e61d2018-10-24 17:08:26 +020069
70 /* default page size - typically 2M */
71 u32 def_log2_page_sz;
72
73 /* system page size - typically 4K */
74 u32 sys_log2_page_sz;
75
76 /* maximum number of pages, limited by VA preallocation size */
Damjan Marion68b4da62018-09-30 18:26:20 +020077 u32 max_pages;
Damjan Marion567e61d2018-10-24 17:08:26 +020078
79 /* vector of pages - each page have own alloc pool and it can be split
80 into subpages (i.e. 2M page build out of 512 4K pages) */
Damjan Marion68b4da62018-09-30 18:26:20 +020081 clib_pmalloc_page_t *pages;
Damjan Marion567e61d2018-10-24 17:08:26 +020082
83 /* hash used to find chunk index out of VA, chunk index is defined
84 per page */
Damjan Marion68b4da62018-09-30 18:26:20 +020085 uword *chunk_index_by_va;
Damjan Marion567e61d2018-10-24 17:08:26 +020086
87 /* alloc arenas are group of pages which share same attributes
88 shared arenas are represented by FD and they are not grovable
89 private arenas are growable */
Damjan Marion68b4da62018-09-30 18:26:20 +020090 clib_pmalloc_arena_t *arenas;
Damjan Marion567e61d2018-10-24 17:08:26 +020091
92 /* vector of per numa node alloc arena indices
93 each numa node have own default privat alloc arena */
Damjan Marion68b4da62018-09-30 18:26:20 +020094 u32 *default_arena_for_numa_node;
95
Damjan Marion567e61d2018-10-24 17:08:26 +020096 /* VA to PA lookup table */
97 uword *lookup_table;
98
99 /* lookup page size - equals to smalles subpage used */
100 u32 lookup_log2_page_sz;
101
102 /* last error */
Damjan Marion68b4da62018-09-30 18:26:20 +0200103 clib_error_t *error;
104} clib_pmalloc_main_t;
105
106
107int clib_pmalloc_init (clib_pmalloc_main_t * pm, uword size);
108void *clib_pmalloc_alloc_aligned_on_numa (clib_pmalloc_main_t * pm,
109 uword size, uword align,
110 u32 numa_node);
111void *clib_pmalloc_alloc_aligned (clib_pmalloc_main_t * pm, uword size,
112 uword align);
113void clib_pmalloc_free (clib_pmalloc_main_t * pm, void *va);
114
115void *clib_pmalloc_create_shared_arena (clib_pmalloc_main_t * pm, char *name,
Damjan Marion567e61d2018-10-24 17:08:26 +0200116 uword size, u32 log2_page_sz,
117 u32 numa_node);
Damjan Marion68b4da62018-09-30 18:26:20 +0200118
119void *clib_pmalloc_alloc_from_arena (clib_pmalloc_main_t * pm, void *arena_va,
120 uword size, uword align);
121
122format_function_t format_pmalloc;
123
124always_inline clib_error_t *
125clib_pmalloc_last_error (clib_pmalloc_main_t * pm)
126{
127 return pm->error;
128}
129
130always_inline u32
131clib_pmalloc_get_page_index (clib_pmalloc_main_t * pm, void *va)
132{
133 uword index = (pointer_to_uword (va) - pointer_to_uword (pm->base)) >>
Damjan Marion567e61d2018-10-24 17:08:26 +0200134 pm->def_log2_page_sz;
Damjan Marion68b4da62018-09-30 18:26:20 +0200135
136 ASSERT (index < vec_len (pm->pages));
137
138 return index;
139}
140
141always_inline clib_pmalloc_arena_t *
142clib_pmalloc_get_arena (clib_pmalloc_main_t * pm, void *va)
143{
144 u32 index = clib_pmalloc_get_page_index (pm, va);
145 return pm->arenas + pm->pages[index].arena_index;
146}
147
148always_inline uword
149clib_pmalloc_get_pa (clib_pmalloc_main_t * pm, void *va)
150{
Damjan Marion567e61d2018-10-24 17:08:26 +0200151 uword index = (pointer_to_uword (va) - pointer_to_uword (pm->base)) >>
152 pm->lookup_log2_page_sz;
153 return pointer_to_uword (va) - pm->lookup_table[index];
Damjan Marion68b4da62018-09-30 18:26:20 +0200154}
155
156
157#endif /* included_palloc_h */
158
159/*
160 * fd.io coding-style-patch-verification: ON
161 *
162 * Local Variables:
163 * eval: (c-set-style "gnu")
164 * End:
165 */