1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2000, 2001, 2002, 2003 Broadcom Corporation
4 */
5
6 #include <linux/init.h>
7 #include <linux/kernel.h>
8 #include <linux/linkage.h>
9 #include <linux/mm.h>
10 #include <linux/memblock.h>
11 #include <linux/pm.h>
12 #include <linux/smp.h>
13
14 #include <asm/bootinfo.h>
15 #include <asm/reboot.h>
16 #include <asm/setup.h>
17 #include <asm/sibyte/board.h>
18 #include <asm/smp-ops.h>
19
20 #include <asm/fw/cfe/cfe_api.h>
21 #include <asm/fw/cfe/cfe_error.h>
22
23 /* Max ram addressable in 32-bit segments */
24 #ifdef CONFIG_64BIT
25 #define MAX_RAM_SIZE (~0ULL)
26 #else
27 #ifdef CONFIG_HIGHMEM
28 #ifdef CONFIG_PHYS_ADDR_T_64BIT
29 #define MAX_RAM_SIZE (~0ULL)
30 #else
31 #define MAX_RAM_SIZE (0xffffffffULL)
32 #endif
33 #else
34 #define MAX_RAM_SIZE (0x1fffffffULL)
35 #endif
36 #endif
37
38 #define SIBYTE_MAX_MEM_REGIONS 8
39 phys_addr_t board_mem_region_addrs[SIBYTE_MAX_MEM_REGIONS];
40 phys_addr_t board_mem_region_sizes[SIBYTE_MAX_MEM_REGIONS];
41 unsigned int board_mem_region_count;
42
43 int cfe_cons_handle;
44
45 #ifdef CONFIG_BLK_DEV_INITRD
46 extern unsigned long initrd_start, initrd_end;
47 #endif
48
cfe_linux_exit(void * arg)49 static void __noreturn cfe_linux_exit(void *arg)
50 {
51 int warm = *(int *)arg;
52
53 if (smp_processor_id()) {
54 static int reboot_smp;
55
56 /* Don't repeat the process from another CPU */
57 if (!reboot_smp) {
58 /* Get CPU 0 to do the cfe_exit */
59 reboot_smp = 1;
60 smp_call_function(cfe_linux_exit, arg, 0);
61 }
62 } else {
63 printk("Passing control back to CFE...\n");
64 cfe_exit(warm, 0);
65 printk("cfe_exit returned??\n");
66 }
67 while (1);
68 }
69
cfe_linux_restart(char * command)70 static void __noreturn cfe_linux_restart(char *command)
71 {
72 static const int zero;
73
74 cfe_linux_exit((void *)&zero);
75 }
76
cfe_linux_halt(void)77 static void __noreturn cfe_linux_halt(void)
78 {
79 static const int one = 1;
80
81 cfe_linux_exit((void *)&one);
82 }
83
prom_meminit(void)84 static __init void prom_meminit(void)
85 {
86 u64 addr, size, type; /* regardless of PHYS_ADDR_T_64BIT */
87 int mem_flags = 0;
88 unsigned int idx;
89 int rd_flag;
90 #ifdef CONFIG_BLK_DEV_INITRD
91 unsigned long initrd_pstart;
92 unsigned long initrd_pend;
93
94 initrd_pstart = CPHYSADDR(initrd_start);
95 initrd_pend = CPHYSADDR(initrd_end);
96 if (initrd_start &&
97 ((initrd_pstart > MAX_RAM_SIZE)
98 || (initrd_pend > MAX_RAM_SIZE))) {
99 panic("initrd out of addressable memory");
100 }
101
102 #endif /* INITRD */
103
104 for (idx = 0; cfe_enummem(idx, mem_flags, &addr, &size, &type) != CFE_ERR_NOMORE;
105 idx++) {
106 rd_flag = 0;
107 if (type == CFE_MI_AVAILABLE) {
108 /*
109 * See if this block contains (any portion of) the
110 * ramdisk
111 */
112 #ifdef CONFIG_BLK_DEV_INITRD
113 if (initrd_start) {
114 if ((initrd_pstart > addr) &&
115 (initrd_pstart < (addr + size))) {
116 memblock_add(addr,
117 initrd_pstart - addr);
118 rd_flag = 1;
119 }
120 if ((initrd_pend > addr) &&
121 (initrd_pend < (addr + size))) {
122 memblock_add(initrd_pend,
123 (addr + size) - initrd_pend);
124 rd_flag = 1;
125 }
126 }
127 #endif
128 if (!rd_flag) {
129 if (addr > MAX_RAM_SIZE)
130 continue;
131 if (addr+size > MAX_RAM_SIZE)
132 size = MAX_RAM_SIZE - (addr+size) + 1;
133 /*
134 * memcpy/__copy_user prefetch, which
135 * will cause a bus error for
136 * KSEG/KUSEG addrs not backed by RAM.
137 * Hence, reserve some padding for the
138 * prefetch distance.
139 */
140 if (size > 512)
141 size -= 512;
142 memblock_add(addr, size);
143 }
144 board_mem_region_addrs[board_mem_region_count] = addr;
145 board_mem_region_sizes[board_mem_region_count] = size;
146 board_mem_region_count++;
147 if (board_mem_region_count ==
148 SIBYTE_MAX_MEM_REGIONS) {
149 /*
150 * Too many regions. Need to configure more
151 */
152 while(1);
153 }
154 }
155 }
156 #ifdef CONFIG_BLK_DEV_INITRD
157 if (initrd_start) {
158 memblock_add(initrd_pstart, initrd_pend - initrd_pstart);
159 memblock_reserve(initrd_pstart, initrd_pend - initrd_pstart);
160 }
161 #endif
162 }
163
164 #ifdef CONFIG_BLK_DEV_INITRD
initrd_setup(char * str)165 static int __init initrd_setup(char *str)
166 {
167 char rdarg[64];
168 int idx;
169 char *tmp, *endptr;
170 unsigned long initrd_size;
171
172 /* Make a copy of the initrd argument so we can smash it up here */
173 for (idx = 0; idx < sizeof(rdarg)-1; idx++) {
174 if (!str[idx] || (str[idx] == ' ')) break;
175 rdarg[idx] = str[idx];
176 }
177
178 rdarg[idx] = 0;
179 str = rdarg;
180
181 /*
182 *Initrd location comes in the form "<hex size of ramdisk in bytes>@<location in memory>"
183 * e.g. initrd=3abfd@80010000. This is set up by the loader.
184 */
185 for (tmp = str; *tmp != '@'; tmp++) {
186 if (!*tmp) {
187 goto fail;
188 }
189 }
190 *tmp = 0;
191 tmp++;
192 if (!*tmp) {
193 goto fail;
194 }
195 initrd_size = simple_strtoul(str, &endptr, 16);
196 if (*endptr) {
197 *(tmp-1) = '@';
198 goto fail;
199 }
200 *(tmp-1) = '@';
201 initrd_start = simple_strtoul(tmp, &endptr, 16);
202 if (*endptr) {
203 goto fail;
204 }
205 initrd_end = initrd_start + initrd_size;
206 printk("Found initrd of %lx@%lx\n", initrd_size, initrd_start);
207 return 1;
208 fail:
209 printk("Bad initrd argument. Disabling initrd\n");
210 initrd_start = 0;
211 initrd_end = 0;
212 return 1;
213 }
214
215 #endif
216
217 extern const struct plat_smp_ops sb_smp_ops;
218 extern const struct plat_smp_ops bcm1480_smp_ops;
219
220 /*
221 * prom_init is called just after the cpu type is determined, from setup_arch()
222 */
prom_init(void)223 void __init prom_init(void)
224 {
225 uint64_t cfe_ept, cfe_handle;
226 unsigned int cfe_eptseal;
227 int argc = fw_arg0;
228 char **envp = (char **) fw_arg2;
229 int *prom_vec = (int *) fw_arg3;
230
231 _machine_restart = cfe_linux_restart;
232 _machine_halt = cfe_linux_halt;
233 pm_power_off = cfe_linux_halt;
234
235 /*
236 * Check if a loader was used; if NOT, the 4 arguments are
237 * what CFE gives us (handle, 0, EPT and EPTSEAL)
238 */
239 if (argc < 0) {
240 cfe_handle = (uint64_t)(long)argc;
241 cfe_ept = (long)envp;
242 cfe_eptseal = (uint32_t)(unsigned long)prom_vec;
243 } else {
244 if ((int32_t)(long)prom_vec < 0) {
245 /*
246 * Old loader; all it gives us is the handle,
247 * so use the "known" entrypoint and assume
248 * the seal.
249 */
250 cfe_handle = (uint64_t)(long)prom_vec;
251 cfe_ept = (uint64_t)((int32_t)0x9fc00500);
252 cfe_eptseal = CFE_EPTSEAL;
253 } else {
254 /*
255 * Newer loaders bundle the handle/ept/eptseal
256 * Note: prom_vec is in the loader's useg
257 * which is still alive in the TLB.
258 */
259 cfe_handle = (uint64_t)((int32_t *)prom_vec)[0];
260 cfe_ept = (uint64_t)((int32_t *)prom_vec)[2];
261 cfe_eptseal = (unsigned int)((uint32_t *)prom_vec)[3];
262 }
263 }
264 if (cfe_eptseal != CFE_EPTSEAL) {
265 /* too early for panic to do any good */
266 printk("CFE's entrypoint seal doesn't match. Spinning.");
267 while (1) ;
268 }
269 cfe_init(cfe_handle, cfe_ept);
270 /*
271 * Get the handle for (at least) prom_putchar, possibly for
272 * boot console
273 */
274 cfe_cons_handle = cfe_getstdhandle(CFE_STDHANDLE_CONSOLE);
275 if (cfe_getenv("LINUX_CMDLINE", arcs_cmdline, COMMAND_LINE_SIZE) < 0) {
276 if (argc >= 0) {
277 /* The loader should have set the command line */
278 /* too early for panic to do any good */
279 printk("LINUX_CMDLINE not defined in cfe.");
280 while (1) ;
281 }
282 }
283
284 #ifdef CONFIG_BLK_DEV_INITRD
285 {
286 char *ptr;
287 /* Need to find out early whether we've got an initrd. So scan
288 the list looking now */
289 for (ptr = arcs_cmdline; *ptr; ptr++) {
290 while (*ptr == ' ') {
291 ptr++;
292 }
293 if (!strncmp(ptr, "initrd=", 7)) {
294 initrd_setup(ptr+7);
295 break;
296 } else {
297 while (*ptr && (*ptr != ' ')) {
298 ptr++;
299 }
300 }
301 }
302 }
303 #endif /* CONFIG_BLK_DEV_INITRD */
304
305 /* Not sure this is needed, but it's the safe way. */
306 arcs_cmdline[COMMAND_LINE_SIZE-1] = 0;
307
308 prom_meminit();
309
310 #if defined(CONFIG_SIBYTE_BCM112X) || defined(CONFIG_SIBYTE_SB1250)
311 register_smp_ops(&sb_smp_ops);
312 #endif
313 #if defined(CONFIG_SIBYTE_BCM1x55) || defined(CONFIG_SIBYTE_BCM1x80)
314 register_smp_ops(&bcm1480_smp_ops);
315 #endif
316 }
317
prom_putchar(char c)318 void prom_putchar(char c)
319 {
320 int ret;
321
322 while ((ret = cfe_write(cfe_cons_handle, &c, 1)) == 0)
323 ;
324 }
325