xref: /rk3399_rockchip-uboot/drivers/cpu/rockchip_amp.c (revision 49cfbee5b0f5f60b2089257e5ddae83202d00c23)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2021 Rockchip Electronics Co., Ltd
4  */
5 
6 #include <common.h>
7 #include <amp.h>
8 #include <bidram.h>
9 #include <boot_rkimg.h>
10 #include <config.h>
11 #include <sysmem.h>
12 #include <asm/gic.h>
13 #include <asm/io.h>
14 #include <asm/arch/rockchip_smccc.h>
15 
16 /*
17  * [Design Principles]
18  *
19  * [amp.img]
20  *	The amp image with FIT format which consists of non-linux firmwares.
21  *	Please refer to: driver/cpu/amp.its.
22  *
23  *	amp.img generation: ./tools/mkimage -f drivers/cpu/amp.its -E -p 0xe00 amp.img
24  *
25  * [linux]
26  *	We still use the traditional solution for a better compatibility:
27  *	boot.img/recovery.img with FIT format or Android format.
28  *
29  *	The developer need add "/configurations/conf/linux" node to configure:
30  *	description, arch, cpu, thumb, hyp, udelay(optional) properties.
31  *	The addresses depend on U-Boot: kernel_addr_r, fdt_addr_r and
32  *	ramdisk_addr_r. Please refer to: driver/cpu/amp.its.
33  *
34  * [memory management]
35  *	U-Boot is not responsible for memory distribution/fixup any more, please
36  *	handle this on kernel dts "/memory".
37  *
38  * [trust]
39  *	The AMP feature requires trust support.
40  */
41 
42 #define AMP_PART	"amp"
43 #define gicd_readl(offset)	readl((void *)GICD_BASE + (offset))
44 #define gicd_writel(v, offset)	writel(v, (void *)GICD_BASE + (offset))
45 
46 typedef struct boot_args {
47 	ulong arg0;
48 	ulong arg1;
49 	ulong arg2;
50 	ulong arg3;
51 } boot_args_t;
52 
53 typedef struct boot_cpu {
54 	u32 arch;
55 	u32 state;
56 	u32 entry;
57 	u32 linux_os;
58 } boot_cpu_t;
59 
60 static boot_cpu_t g_bootcpu;
61 
62 static u32 fit_get_u32_default(const void *fit, int noffset,
63 			       const char *prop, u32 def)
64 {
65 	const fdt32_t *val;
66 
67 	val = fdt_getprop(fit, noffset, prop, NULL);
68 	if (!val)
69 		return def;
70 
71 	return fdt32_to_cpu(*val);
72 }
73 
74 static int load_linux_for_nonboot_cpu(u32 cpu, u32 aarch64, u32 load,
75 				      u32 *entry, boot_args_t *args)
76 {
77 	static const char *boot_cmd[] = {
78 		"boot_fit", "boot_android ${devtype} ${devnum}" };
79 	int i, ret;
80 
81 	env_set_hex("bootm_states_unmask", BOOTM_STATE_OS_GO);
82 	for (i = 0; i < ARRAY_SIZE(boot_cmd); i++) {
83 		ret = run_command(boot_cmd[i], 0);
84 		if (!ret)
85 			break;
86 	}
87 	env_set("bootm_states_unmask", NULL);
88 	if (ret) {
89 		AMP_E("Load linux failed, ret=%d\n", ret);
90 		return ret;
91 	}
92 
93 	/* linux boot args */
94 	if (aarch64) {
95 		args->arg0 = (ulong)gd->fdt_blob;
96 		args->arg1 = 0;
97 		args->arg2 = 0;
98 	} else {
99 		args->arg0 = 0;
100 		args->arg1 = 0;
101 		args->arg2 = (ulong)gd->fdt_blob;
102 	}
103 
104 	/* don't need call cleanup_before_linux() as this nonboot cpu is clean */
105 	board_quiesce_devices(&images);
106 	flush_dcache_all();
107 
108 	/* fixup: ramdisk/fdt/entry depend on U-Boot */
109 	*entry = env_get_ulong("kernel_addr_r", 16, 0);
110 
111 	return 0;
112 }
113 
114 static int is_default_pe_state(u32 pe_state)
115 {
116 #ifdef CONFIG_ARM64
117 	return (pe_state == PE_STATE(1, 1, 0, 0));
118 #else
119 	return (pe_state == PE_STATE(0, 0, 0, 0));
120 #endif
121 }
122 
123 static void setup_sync_bits_for_linux(void)
124 {
125 	u32 val, num_irq, offset;
126 
127 	val = gicd_readl(GICD_CTLR);
128 	val &= ~0x3;
129 	gicd_writel(val, GICD_CTLR);
130 
131 	num_irq = 32 * ((gicd_readl(GICD_TYPER) & 0x1F) + 1);
132 	offset = ((num_irq - 1) / 4) * 4;
133 	gicd_writel(0x0, GICD_IPRIORITYRn + offset);
134 }
135 
136 static int smc_cpu_on(u32 cpu, u32 pe_state, u32 entry, boot_args_t *args)
137 {
138 	int ret;
139 
140 	AMP_I("Brought up cpu[%x] with state 0x%x, entry 0x%08x ...",
141 	      cpu, pe_state, entry);
142 
143 	if (is_default_pe_state(pe_state))
144 		goto finish;
145 
146 	ret = sip_smc_amp_cfg(AMP_PE_STATE, cpu, pe_state, 0);
147 	if (ret) {
148 		AMP_E("smc pe-state, ret=%d\n", ret);
149 		return ret;
150 	}
151 
152 	ret = sip_smc_amp_cfg(AMP_BOOT_ARG01, cpu, args->arg0, args->arg1);
153 	if (ret) {
154 		AMP_E("smc boot arg01, ret=%d\n", ret);
155 		return ret;
156 	}
157 
158 	ret = sip_smc_amp_cfg(AMP_BOOT_ARG23, cpu, args->arg2, args->arg3);
159 	if (ret) {
160 		AMP_E("smc boot arg23, ret=%d\n", ret);
161 		return ret;
162 	}
163 
164 finish:
165 	ret = psci_cpu_on(cpu, entry);
166 	if (ret) {
167 		printf("cpu up failed, ret=%d\n", ret);
168 		return ret;
169 	}
170 	printf("OK\n");
171 
172 	return 0;
173 }
174 
175 static int brought_up_amp(void *fit, int noffset,
176 			  boot_cpu_t *bootcpu, int is_linux)
177 {
178 	const char *desc;
179 	boot_args_t args;
180 	u32 cpu, aarch64, hyp;
181 	u32 load, thumb, us;
182 	u32 pe_state, entry;
183 	int data_size;
184 	int ret;
185 	u8  arch = -ENODATA;
186 
187 	desc = fdt_getprop(fit, noffset, "description", NULL);
188 	cpu = fit_get_u32_default(fit, noffset, "cpu", -ENODATA);
189 	hyp = fit_get_u32_default(fit, noffset, "hyp", 0);
190 	thumb = fit_get_u32_default(fit, noffset, "thumb", 0);
191 	load = fit_get_u32_default(fit, noffset, "load", -ENODATA);
192 	us = fit_get_u32_default(fit, noffset, "udelay", 0);
193 	fit_image_get_arch(fit, noffset, &arch);
194 	fit_image_get_data_size(fit, noffset, &data_size);
195 	memset(&args, 0, sizeof(args));
196 
197 	if (!desc || cpu == -ENODATA || arch == -ENODATA ||
198 	    (load == -ENODATA && !is_linux)) {
199 		AMP_E("Property missing!\n");
200 		return -EINVAL;
201 	}
202 	aarch64 = (arch == IH_ARCH_ARM) ? 0 : 1;
203 	pe_state = PE_STATE(aarch64, hyp, thumb, 0);
204 	entry = load;
205 
206 #ifdef DEBUG
207 	AMP_I("       desc: %s\n", desc);
208 	AMP_I("        cpu: 0x%x\n", cpu);
209 	AMP_I("    aarch64: %d\n", aarch64);
210 	AMP_I("        hyp: %d\n", hyp);
211 	AMP_I("      thumb: %d\n", thumb);
212 	AMP_I("      entry: 0x%08x\n", entry);
213 	AMP_I("   pe_state: 0x%08x\n", pe_state);
214 	AMP_I("   linux-os: %d\n\n", is_linux);
215 #endif
216 
217 	if ((read_mpidr() & 0x0fff) == cpu) {
218 		bootcpu->arch = arch;
219 		bootcpu->entry = entry;
220 		bootcpu->state = pe_state;
221 		bootcpu->linux_os = is_linux;
222 		return 0;
223 	}
224 
225 	/* === only nonboot cpu can reach here === */
226 
227 	/* load or check */
228 	if (is_linux) {
229 		ret = load_linux_for_nonboot_cpu(cpu,
230 				aarch64, load, &entry, &args);
231 		if (ret)
232 			return ret;
233 		/*
234 		 * Must setup before jump to linux.
235 		 * This is an appointment on RK amp solution to handle
236 		 * GIC configure competition.
237 		 */
238 		setup_sync_bits_for_linux();
239 	} else {
240 		if (!sysmem_alloc_base_by_name(desc,
241 				(phys_addr_t)load, data_size))
242 			return -ENXIO;
243 	}
244 
245 	/* wakeup */
246 	ret = smc_cpu_on(cpu, pe_state, entry, &args);
247 	if (ret)
248 		return ret;
249 
250 	if (us)
251 		udelay(us);
252 
253 	return 0;
254 }
255 
256 static int brought_up_all_amp(void *fit, const char *fit_uname_cfg)
257 {
258 	int loadables_index;
259 	int linux_noffset;
260 	int conf_noffset;
261 	int cpu_noffset;
262 	int ret;
263 	const char *uname;
264 
265 	conf_noffset = fit_conf_get_node(fit, fit_uname_cfg);
266 	if (conf_noffset < 0)
267 		return conf_noffset;
268 
269 	linux_noffset = fdt_subnode_offset(fit, conf_noffset, "linux");
270 	if (linux_noffset > 0) {
271 		ret = brought_up_amp(fit, linux_noffset, &g_bootcpu, 1);
272 		if (ret)
273 			return ret;
274 	}
275 
276 	for (loadables_index = 0;
277 	     uname = fdt_stringlist_get(fit, conf_noffset,
278 			FIT_LOADABLE_PROP, loadables_index, NULL), uname;
279 	     loadables_index++) {
280 		cpu_noffset = fit_image_get_node(fit, uname);
281 		if (cpu_noffset < 0)
282 			return cpu_noffset;
283 
284 		ret = brought_up_amp(fit, cpu_noffset, &g_bootcpu, 0);
285 		if (ret)
286 			return ret;
287 	}
288 
289 	/* === only boot cpu can reach here === */
290 
291 	if (!g_bootcpu.linux_os) {
292 		flush_dcache_all();
293 		AMP_I("Brought up cpu[%x, self] with state 0x%x, entry 0x%08x ...",
294 		      (u32)read_mpidr() & 0x0fff, g_bootcpu.state, g_bootcpu.entry);
295 		cleanup_before_linux();
296 		printf("OK\n");
297 		armv8_switch_to_el2(0, 0, 0, g_bootcpu.state, (u64)g_bootcpu.entry,
298 		     g_bootcpu.arch == IH_ARCH_ARM ? ES_TO_AARCH32 : ES_TO_AARCH64);
299 	}
300 
301 	/* return: boot cpu continue to boot linux */
302 	return 0;
303 }
304 
305 int amp_cpus_on(void)
306 {
307 	struct blk_desc *dev_desc;
308 	bootm_headers_t images;
309 	disk_partition_t part;
310 	void *fit;
311 	int ret = 0;
312 
313 	dev_desc = rockchip_get_bootdev();
314 	if (!dev_desc)
315 		return -EIO;
316 
317 	if (part_get_info_by_name(dev_desc, AMP_PART, &part) < 0)
318 		return -ENODEV;
319 
320 	fit = malloc(part.size * part.blksz);
321 	if (!fit) {
322 		AMP_E("No memory, please increase CONFIG_SYS_MALLOC_LEN\n");
323 		return -ENOMEM;
324 	}
325 
326 	if (blk_dread(dev_desc, part.start, part.size, fit) != part.size) {
327 		ret = -EIO;
328 		goto out;
329 	}
330 
331 	if (fdt_check_header(fit)) {
332 		AMP_E("Not fit\n");
333 		ret = -EINVAL;
334 		goto out;
335 	}
336 
337 	/* Load loadables */
338 	memset(&images, 0, sizeof(images));
339 	images.fit_uname_cfg = "conf";
340 	images.fit_hdr_os = fit;
341 	images.verify = 1;
342 	ret = boot_get_loadable(0, NULL, &images, IH_ARCH_DEFAULT, NULL, NULL);
343 	if (ret) {
344 		AMP_E("Load loadables, ret=%d\n", ret);
345 		goto out;
346 	}
347 	flush_dcache_all();
348 
349 	/* Wakeup */
350 	ret = brought_up_all_amp(images.fit_hdr_os, images.fit_uname_cfg);
351 	if (ret)
352 		AMP_E("Brought up amps, ret=%d\n", ret);
353 out:
354 	free(fit);
355 
356 	return ret;
357 }
358 
359 int arm64_switch_amp_pe(bootm_headers_t *images)
360 {
361 	images->os.arch = g_bootcpu.arch;
362 	return g_bootcpu.state;
363 }
364 
365