1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup RTEMSBSPsARMCycV |
---|
5 | */ |
---|
6 | |
---|
7 | /* |
---|
8 | * Copyright (c) 2017, 2019 embedded brains GmbH |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.org/license/LICENSE. |
---|
13 | */ |
---|
14 | |
---|
15 | #include <bsp/bootcard.h> |
---|
16 | #include <bsp/fdt.h> |
---|
17 | #include <bsp/linker-symbols.h> |
---|
18 | |
---|
19 | #ifdef BSP_FDT_IS_SUPPORTED |
---|
20 | |
---|
21 | #include <bsp/arm-cp15-start.h> |
---|
22 | |
---|
23 | #include <libcpu/arm-cp15.h> |
---|
24 | |
---|
25 | #include <libfdt.h> |
---|
26 | |
---|
27 | #include <rtems/sysinit.h> |
---|
28 | |
---|
29 | #define AREA_COUNT_MAX 16 |
---|
30 | |
---|
31 | static const char memory_path[] = "/memory"; |
---|
32 | |
---|
33 | static const char reserved_memory_path[] = "/reserved-memory"; |
---|
34 | |
---|
35 | static void adjust_memory_size(const void *fdt, Memory_Area *area) |
---|
36 | { |
---|
37 | int node; |
---|
38 | |
---|
39 | node = fdt_path_offset_namelen( |
---|
40 | fdt, |
---|
41 | memory_path, |
---|
42 | (int) sizeof(memory_path) - 1 |
---|
43 | ); |
---|
44 | |
---|
45 | if (node >= 0) { |
---|
46 | int len; |
---|
47 | const void *val; |
---|
48 | uintptr_t begin; |
---|
49 | uintptr_t size; |
---|
50 | uintptr_t a_bit; |
---|
51 | |
---|
52 | val = fdt_getprop(fdt, node, "reg", &len); |
---|
53 | if (len == 8) { |
---|
54 | begin = fdt32_to_cpu(((fdt32_t *) val)[0]); |
---|
55 | size = fdt32_to_cpu(((fdt32_t *) val)[1]); |
---|
56 | } else { |
---|
57 | begin = 0; |
---|
58 | size = 0; |
---|
59 | } |
---|
60 | |
---|
61 | /* |
---|
62 | * Remove a bit to avoid problems with speculative memory accesses beyond |
---|
63 | * the valid memory area. |
---|
64 | */ |
---|
65 | a_bit = 0x100000; |
---|
66 | if (size >= a_bit) { |
---|
67 | size -= a_bit; |
---|
68 | } |
---|
69 | |
---|
70 | if ( |
---|
71 | begin == 0 |
---|
72 | && size > (uintptr_t) bsp_section_work_end |
---|
73 | && (uintptr_t) bsp_section_nocache_end |
---|
74 | < (uintptr_t) bsp_section_work_end |
---|
75 | ) { |
---|
76 | _Memory_Set_end(area, (void *) (begin + size)); |
---|
77 | } |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | static Memory_Area *find_area( |
---|
82 | Memory_Area *areas, |
---|
83 | size_t area_count, |
---|
84 | uint32_t begin |
---|
85 | ) |
---|
86 | { |
---|
87 | size_t i; |
---|
88 | |
---|
89 | for (i = 0; i < area_count; ++i) { |
---|
90 | uintptr_t b; |
---|
91 | uintptr_t e; |
---|
92 | |
---|
93 | b = (uintptr_t) _Memory_Get_begin(&areas[i]); |
---|
94 | e = (uintptr_t) _Memory_Get_end(&areas[i]); |
---|
95 | |
---|
96 | if (b <= begin && begin < e) { |
---|
97 | return &areas[i]; |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | return NULL; |
---|
102 | } |
---|
103 | |
---|
104 | static size_t remove_reserved_memory( |
---|
105 | const void *fdt, |
---|
106 | Memory_Area *areas, |
---|
107 | size_t area_count |
---|
108 | ) |
---|
109 | { |
---|
110 | int node; |
---|
111 | |
---|
112 | node = fdt_path_offset_namelen( |
---|
113 | fdt, |
---|
114 | reserved_memory_path, |
---|
115 | (int) sizeof(reserved_memory_path) - 1 |
---|
116 | ); |
---|
117 | |
---|
118 | if (node >= 0) { |
---|
119 | node = fdt_first_subnode(fdt, node); |
---|
120 | |
---|
121 | while (node >= 0) { |
---|
122 | int len; |
---|
123 | const void *val; |
---|
124 | uintptr_t area_end; |
---|
125 | uintptr_t hole_begin; |
---|
126 | uintptr_t hole_end; |
---|
127 | Memory_Area *area; |
---|
128 | |
---|
129 | val = fdt_getprop(fdt, node, "reg", &len); |
---|
130 | if (len == 8) { |
---|
131 | hole_begin = fdt32_to_cpu(((fdt32_t *) val)[0]); |
---|
132 | hole_end = hole_begin + fdt32_to_cpu(((fdt32_t *) val)[1]); |
---|
133 | } else { |
---|
134 | rtems_panic("unexpected reserved memory area"); |
---|
135 | } |
---|
136 | |
---|
137 | area = find_area(areas, area_count, hole_begin); |
---|
138 | area_end = (uintptr_t) _Memory_Get_end(area); |
---|
139 | _Memory_Set_end(area, (void *) hole_end); |
---|
140 | |
---|
141 | if (hole_end <= area_end) { |
---|
142 | if (area_count >= AREA_COUNT_MAX) { |
---|
143 | rtems_panic("too many reserved memory areas"); |
---|
144 | } |
---|
145 | |
---|
146 | area = &areas[area_count]; |
---|
147 | ++area_count; |
---|
148 | _Memory_Initialize(area, (void *) hole_end, (void *) area_end); |
---|
149 | } |
---|
150 | |
---|
151 | node = fdt_next_subnode(fdt, node); |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | return area_count; |
---|
156 | } |
---|
157 | |
---|
158 | static Memory_Area _Memory_Areas[AREA_COUNT_MAX]; |
---|
159 | |
---|
160 | static void bsp_memory_initialize(void) |
---|
161 | { |
---|
162 | size_t area_count; |
---|
163 | const void *fdt; |
---|
164 | size_t i; |
---|
165 | |
---|
166 | _Memory_Initialize( |
---|
167 | &_Memory_Areas[0], |
---|
168 | bsp_section_work_begin, |
---|
169 | bsp_section_work_end |
---|
170 | ); |
---|
171 | |
---|
172 | area_count = 1; |
---|
173 | fdt = bsp_fdt_get(); |
---|
174 | adjust_memory_size(fdt, &_Memory_Areas[0]); |
---|
175 | area_count = remove_reserved_memory(fdt, &_Memory_Areas[0], area_count); |
---|
176 | |
---|
177 | for (i = 0; i < area_count; ++i) { |
---|
178 | arm_cp15_set_translation_table_entries( |
---|
179 | _Memory_Get_begin(&_Memory_Areas[i]), |
---|
180 | _Memory_Get_end(&_Memory_Areas[i]), |
---|
181 | ARMV7_MMU_READ_WRITE_CACHED |
---|
182 | ); |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | RTEMS_SYSINIT_ITEM( |
---|
187 | bsp_memory_initialize, |
---|
188 | RTEMS_SYSINIT_MEMORY, |
---|
189 | RTEMS_SYSINIT_ORDER_MIDDLE |
---|
190 | ); |
---|
191 | |
---|
192 | #else /* !BSP_FDT_IS_SUPPORTED */ |
---|
193 | |
---|
194 | static Memory_Area _Memory_Areas[] = { |
---|
195 | MEMORY_INITIALIZER(bsp_section_work_begin, bsp_section_work_end) |
---|
196 | }; |
---|
197 | |
---|
198 | #endif /* BSP_FDT_IS_SUPPORTED */ |
---|
199 | |
---|
200 | static const Memory_Information _Memory_Information = |
---|
201 | MEMORY_INFORMATION_INITIALIZER( _Memory_Areas ); |
---|
202 | |
---|
203 | const Memory_Information *_Memory_Get( void ) |
---|
204 | { |
---|
205 | return &_Memory_Information; |
---|
206 | } |
---|