1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *
4  * Copyright (C) 2012 ARM Limited
5  *
6  * Author: Will Deacon <will.deacon@arm.com>
7  */
8 
9 #include <linux/init.h>
10 #include <linux/smp.h>
11 #include <linux/of.h>
12 #include <linux/delay.h>
13 #include <linux/psci.h>
14 
15 #include <uapi/linux/psci.h>
16 
17 #include <asm/psci.h>
18 #include <asm/smp_plat.h>
19 
20 /*
21  * psci_smp assumes that the following is true about PSCI:
22  *
23  * cpu_suspend   Suspend the execution on a CPU
24  * @state        we don't currently describe affinity levels, so just pass 0.
25  * @entry_point  the first instruction to be executed on return
26  * returns 0  success, < 0 on failure
27  *
28  * cpu_off       Power down a CPU
29  * @state        we don't currently describe affinity levels, so just pass 0.
30  * no return on successful call
31  *
32  * cpu_on        Power up a CPU
33  * @cpuid        cpuid of target CPU, as from MPIDR
34  * @entry_point  the first instruction to be executed on return
35  * returns 0  success, < 0 on failure
36  *
37  * migrate       Migrate the context to a different CPU
38  * @cpuid        cpuid of target CPU, as from MPIDR
39  * returns 0  success, < 0 on failure
40  *
41  */
42 
43 extern void secondary_startup(void);
44 
psci_boot_secondary(unsigned int cpu,struct task_struct * idle)45 static int psci_boot_secondary(unsigned int cpu, struct task_struct *idle)
46 {
47 	if (psci_ops.cpu_on)
48 #ifdef CONFIG_XIP_KERNEL
49 		return psci_ops.cpu_on(cpu_logical_map(cpu),
50 			((phys_addr_t)(&secondary_startup)
51 			- XIP_VIRT_ADDR(CONFIG_XIP_PHYS_ADDR)
52 			+ CONFIG_XIP_PHYS_ADDR));
53 #else
54 		return psci_ops.cpu_on(cpu_logical_map(cpu),
55 					virt_to_idmap(&secondary_startup));
56 #endif
57 	return -ENODEV;
58 }
59 
60 #ifdef CONFIG_HOTPLUG_CPU
psci_cpu_disable(unsigned int cpu)61 static int psci_cpu_disable(unsigned int cpu)
62 {
63 	/* Fail early if we don't have CPU_OFF support */
64 	if (!psci_ops.cpu_off)
65 		return -EOPNOTSUPP;
66 
67 	/* Trusted OS will deny CPU_OFF */
68 	if (psci_tos_resident_on(cpu))
69 		return -EPERM;
70 
71 	return 0;
72 }
73 
psci_cpu_die(unsigned int cpu)74 static void psci_cpu_die(unsigned int cpu)
75 {
76 	u32 state = PSCI_POWER_STATE_TYPE_POWER_DOWN <<
77 		    PSCI_0_2_POWER_STATE_TYPE_SHIFT;
78 
79 	if (psci_ops.cpu_off)
80 		psci_ops.cpu_off(state);
81 
82 	/* We should never return */
83 	panic("psci: cpu %d failed to shutdown\n", cpu);
84 }
85 
psci_cpu_kill(unsigned int cpu)86 static int psci_cpu_kill(unsigned int cpu)
87 {
88 	int err, i;
89 
90 	if (!psci_ops.affinity_info)
91 		return 1;
92 	/*
93 	 * cpu_kill could race with cpu_die and we can
94 	 * potentially end up declaring this cpu undead
95 	 * while it is dying. So, try again a few times.
96 	 */
97 
98 	for (i = 0; i < 10; i++) {
99 		err = psci_ops.affinity_info(cpu_logical_map(cpu), 0);
100 		if (err == PSCI_0_2_AFFINITY_LEVEL_OFF) {
101 			pr_info("CPU%d killed.\n", cpu);
102 			return 1;
103 		}
104 
105 		msleep(10);
106 		pr_info("Retrying again to check for CPU kill\n");
107 	}
108 
109 	pr_warn("CPU%d may not have shut down cleanly (AFFINITY_INFO reports %d)\n",
110 			cpu, err);
111 	/* Make platform_cpu_kill() fail. */
112 	return 0;
113 }
114 
115 #endif
116 
psci_smp_available(void)117 bool __init psci_smp_available(void)
118 {
119 	/* is cpu_on available at least? */
120 	return (psci_ops.cpu_on != NULL);
121 }
122 
123 const struct smp_operations psci_smp_ops __initconst = {
124 	.smp_boot_secondary	= psci_boot_secondary,
125 #ifdef CONFIG_HOTPLUG_CPU
126 	.cpu_disable		= psci_cpu_disable,
127 	.cpu_die		= psci_cpu_die,
128 	.cpu_kill		= psci_cpu_kill,
129 #endif
130 };
131