• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *
4  * Copyright (C) 2013 ARM Limited
5  *
6  * Author: Will Deacon <will.deacon@arm.com>
7  */
8 
9 #define pr_fmt(fmt) "psci: " fmt
10 
11 #include <linux/init.h>
12 #include <linux/of.h>
13 #include <linux/smp.h>
14 #include <linux/delay.h>
15 #include <linux/psci.h>
16 #include <linux/mm.h>
17 
18 #include <uapi/linux/psci.h>
19 
20 #include <asm/cpu_ops.h>
21 #include <asm/errno.h>
22 #include <asm/smp_plat.h>
23 
cpu_psci_cpu_init(unsigned int cpu)24 static int __init cpu_psci_cpu_init(unsigned int cpu)
25 {
26 	return 0;
27 }
28 
cpu_psci_cpu_prepare(unsigned int cpu)29 static int __init cpu_psci_cpu_prepare(unsigned int cpu)
30 {
31 	if (!psci_ops.cpu_on) {
32 		pr_err("no cpu_on method, not booting CPU%d\n", cpu);
33 		return -ENODEV;
34 	}
35 
36 	return 0;
37 }
38 
cpu_psci_cpu_boot(unsigned int cpu)39 static int cpu_psci_cpu_boot(unsigned int cpu)
40 {
41 	int err = psci_ops.cpu_on(cpu_logical_map(cpu),
42 				  __pa_function(secondary_entry));
43 	if (err)
44 		pr_err("failed to boot CPU%d (%d)\n", cpu, err);
45 
46 	return err;
47 }
48 
49 #ifdef CONFIG_HOTPLUG_CPU
cpu_psci_cpu_can_disable(unsigned int cpu)50 static bool cpu_psci_cpu_can_disable(unsigned int cpu)
51 {
52 	return !psci_tos_resident_on(cpu);
53 }
54 
cpu_psci_cpu_disable(unsigned int cpu)55 static int cpu_psci_cpu_disable(unsigned int cpu)
56 {
57 	/* Fail early if we don't have CPU_OFF support */
58 	if (!psci_ops.cpu_off)
59 		return -EOPNOTSUPP;
60 
61 	/* Trusted OS will deny CPU_OFF */
62 	if (psci_tos_resident_on(cpu))
63 		return -EPERM;
64 
65 	return 0;
66 }
67 
cpu_psci_cpu_die(unsigned int cpu)68 static void cpu_psci_cpu_die(unsigned int cpu)
69 {
70 	int ret;
71 	/*
72 	 * There are no known implementations of PSCI actually using the
73 	 * power state field, pass a sensible default for now.
74 	 */
75 	u32 state = PSCI_POWER_STATE_TYPE_POWER_DOWN <<
76 		    PSCI_0_2_POWER_STATE_TYPE_SHIFT;
77 
78 	ret = psci_ops.cpu_off(state);
79 
80 	pr_crit("unable to power off CPU%u (%d)\n", cpu, ret);
81 }
82 
cpu_psci_cpu_kill(unsigned int cpu)83 static int cpu_psci_cpu_kill(unsigned int cpu)
84 {
85 	int err;
86 	unsigned long start, end;
87 
88 	if (!psci_ops.affinity_info)
89 		return 0;
90 	/*
91 	 * cpu_kill could race with cpu_die and we can
92 	 * potentially end up declaring this cpu undead
93 	 * while it is dying. So, try again a few times.
94 	 */
95 
96 	start = jiffies;
97 	end = start + msecs_to_jiffies(100);
98 	do {
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 (polled %d ms)\n", cpu,
102 				jiffies_to_msecs(jiffies - start));
103 			return 0;
104 		}
105 
106 		usleep_range(100, 1000);
107 	} while (time_before(jiffies, end));
108 
109 	pr_warn("CPU%d may not have shut down cleanly (AFFINITY_INFO reports %d)\n",
110 			cpu, err);
111 	return -ETIMEDOUT;
112 }
113 #endif
114 
115 const struct cpu_operations cpu_psci_ops = {
116 	.name		= "psci",
117 	.cpu_init	= cpu_psci_cpu_init,
118 	.cpu_prepare	= cpu_psci_cpu_prepare,
119 	.cpu_boot	= cpu_psci_cpu_boot,
120 #ifdef CONFIG_HOTPLUG_CPU
121 	.cpu_can_disable = cpu_psci_cpu_can_disable,
122 	.cpu_disable	= cpu_psci_cpu_disable,
123 	.cpu_die	= cpu_psci_cpu_die,
124 	.cpu_kill	= cpu_psci_cpu_kill,
125 #endif
126 };
127 
128