mm: hugetlb: fix hugepage memory leak caused by wrong reserve count
[linux/fpc-iii.git] / arch / arm64 / kernel / psci.c
blobf67f35b6edb12e4d34e1db17750b07a0bec72e39
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * Copyright (C) 2013 ARM Limited
13 * Author: Will Deacon <will.deacon@arm.com>
16 #define pr_fmt(fmt) "psci: " fmt
18 #include <linux/init.h>
19 #include <linux/of.h>
20 #include <linux/smp.h>
21 #include <linux/delay.h>
22 #include <linux/psci.h>
23 #include <linux/slab.h>
25 #include <uapi/linux/psci.h>
27 #include <asm/compiler.h>
28 #include <asm/cpu_ops.h>
29 #include <asm/errno.h>
30 #include <asm/smp_plat.h>
31 #include <asm/suspend.h>
33 static DEFINE_PER_CPU_READ_MOSTLY(u32 *, psci_power_state);
35 static int __maybe_unused cpu_psci_cpu_init_idle(unsigned int cpu)
37 int i, ret, count = 0;
38 u32 *psci_states;
39 struct device_node *state_node, *cpu_node;
41 cpu_node = of_get_cpu_node(cpu, NULL);
42 if (!cpu_node)
43 return -ENODEV;
46 * If the PSCI cpu_suspend function hook has not been initialized
47 * idle states must not be enabled, so bail out
49 if (!psci_ops.cpu_suspend)
50 return -EOPNOTSUPP;
52 /* Count idle states */
53 while ((state_node = of_parse_phandle(cpu_node, "cpu-idle-states",
54 count))) {
55 count++;
56 of_node_put(state_node);
59 if (!count)
60 return -ENODEV;
62 psci_states = kcalloc(count, sizeof(*psci_states), GFP_KERNEL);
63 if (!psci_states)
64 return -ENOMEM;
66 for (i = 0; i < count; i++) {
67 u32 state;
69 state_node = of_parse_phandle(cpu_node, "cpu-idle-states", i);
71 ret = of_property_read_u32(state_node,
72 "arm,psci-suspend-param",
73 &state);
74 if (ret) {
75 pr_warn(" * %s missing arm,psci-suspend-param property\n",
76 state_node->full_name);
77 of_node_put(state_node);
78 goto free_mem;
81 of_node_put(state_node);
82 pr_debug("psci-power-state %#x index %d\n", state, i);
83 if (!psci_power_state_is_valid(state)) {
84 pr_warn("Invalid PSCI power state %#x\n", state);
85 ret = -EINVAL;
86 goto free_mem;
88 psci_states[i] = state;
90 /* Idle states parsed correctly, initialize per-cpu pointer */
91 per_cpu(psci_power_state, cpu) = psci_states;
92 return 0;
94 free_mem:
95 kfree(psci_states);
96 return ret;
99 static int __init cpu_psci_cpu_init(unsigned int cpu)
101 return 0;
104 static int __init cpu_psci_cpu_prepare(unsigned int cpu)
106 if (!psci_ops.cpu_on) {
107 pr_err("no cpu_on method, not booting CPU%d\n", cpu);
108 return -ENODEV;
111 return 0;
114 static int cpu_psci_cpu_boot(unsigned int cpu)
116 int err = psci_ops.cpu_on(cpu_logical_map(cpu), __pa(secondary_entry));
117 if (err)
118 pr_err("failed to boot CPU%d (%d)\n", cpu, err);
120 return err;
123 #ifdef CONFIG_HOTPLUG_CPU
124 static int cpu_psci_cpu_disable(unsigned int cpu)
126 /* Fail early if we don't have CPU_OFF support */
127 if (!psci_ops.cpu_off)
128 return -EOPNOTSUPP;
130 /* Trusted OS will deny CPU_OFF */
131 if (psci_tos_resident_on(cpu))
132 return -EPERM;
134 return 0;
137 static void cpu_psci_cpu_die(unsigned int cpu)
139 int ret;
141 * There are no known implementations of PSCI actually using the
142 * power state field, pass a sensible default for now.
144 u32 state = PSCI_POWER_STATE_TYPE_POWER_DOWN <<
145 PSCI_0_2_POWER_STATE_TYPE_SHIFT;
147 ret = psci_ops.cpu_off(state);
149 pr_crit("unable to power off CPU%u (%d)\n", cpu, ret);
152 static int cpu_psci_cpu_kill(unsigned int cpu)
154 int err, i;
156 if (!psci_ops.affinity_info)
157 return 0;
159 * cpu_kill could race with cpu_die and we can
160 * potentially end up declaring this cpu undead
161 * while it is dying. So, try again a few times.
164 for (i = 0; i < 10; i++) {
165 err = psci_ops.affinity_info(cpu_logical_map(cpu), 0);
166 if (err == PSCI_0_2_AFFINITY_LEVEL_OFF) {
167 pr_info("CPU%d killed.\n", cpu);
168 return 0;
171 msleep(10);
172 pr_info("Retrying again to check for CPU kill\n");
175 pr_warn("CPU%d may not have shut down cleanly (AFFINITY_INFO reports %d)\n",
176 cpu, err);
177 return -ETIMEDOUT;
179 #endif
181 static int psci_suspend_finisher(unsigned long index)
183 u32 *state = __this_cpu_read(psci_power_state);
185 return psci_ops.cpu_suspend(state[index - 1],
186 virt_to_phys(cpu_resume));
189 static int __maybe_unused cpu_psci_cpu_suspend(unsigned long index)
191 int ret;
192 u32 *state = __this_cpu_read(psci_power_state);
194 * idle state index 0 corresponds to wfi, should never be called
195 * from the cpu_suspend operations
197 if (WARN_ON_ONCE(!index))
198 return -EINVAL;
200 if (!psci_power_state_loses_context(state[index - 1]))
201 ret = psci_ops.cpu_suspend(state[index - 1], 0);
202 else
203 ret = cpu_suspend(index, psci_suspend_finisher);
205 return ret;
208 const struct cpu_operations cpu_psci_ops = {
209 .name = "psci",
210 #ifdef CONFIG_CPU_IDLE
211 .cpu_init_idle = cpu_psci_cpu_init_idle,
212 .cpu_suspend = cpu_psci_cpu_suspend,
213 #endif
214 .cpu_init = cpu_psci_cpu_init,
215 .cpu_prepare = cpu_psci_cpu_prepare,
216 .cpu_boot = cpu_psci_cpu_boot,
217 #ifdef CONFIG_HOTPLUG_CPU
218 .cpu_disable = cpu_psci_cpu_disable,
219 .cpu_die = cpu_psci_cpu_die,
220 .cpu_kill = cpu_psci_cpu_kill,
221 #endif