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