blob: e8edbf13302aad06875703c5b680dd3513c4bb91 [file] [log] [blame]
Will Deacone790f1d2012-12-18 17:53:14 +00001/*
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.
5 *
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.
10 *
11 * Copyright (C) 2013 ARM Limited
12 *
13 * Author: Will Deacon <will.deacon@arm.com>
14 */
15
16#define pr_fmt(fmt) "psci: " fmt
17
18#include <linux/init.h>
19#include <linux/of.h>
Mark Rutland00ef54b2013-10-24 20:30:14 +010020#include <linux/smp.h>
Ashwin Chaugulec814ca02014-05-07 10:18:36 -040021#include <linux/delay.h>
Mark Rutlandbff60792015-07-31 15:46:16 +010022#include <linux/psci.h>
Laura Abbottf8fee94e2017-01-10 13:35:49 -080023#include <linux/mm.h>
Mark Rutlandbff60792015-07-31 15:46:16 +010024
Ashwin Chaugulee71246a2014-04-17 14:38:41 -040025#include <uapi/linux/psci.h>
Will Deacone790f1d2012-12-18 17:53:14 +000026
27#include <asm/compiler.h>
Mark Rutlandcd1aebf2013-10-24 20:30:15 +010028#include <asm/cpu_ops.h>
Will Deacone790f1d2012-12-18 17:53:14 +000029#include <asm/errno.h>
Mark Rutland00ef54b2013-10-24 20:30:14 +010030#include <asm/smp_plat.h>
Lorenzo Pieralisi18910ab2013-09-27 10:25:02 +010031
Lorenzo Pieralisi819a8822015-05-13 14:12:46 +010032static int __init cpu_psci_cpu_init(unsigned int cpu)
Mark Rutland00ef54b2013-10-24 20:30:14 +010033{
34 return 0;
35}
36
Mark Rutlandcd1aebf2013-10-24 20:30:15 +010037static int __init cpu_psci_cpu_prepare(unsigned int cpu)
Mark Rutland00ef54b2013-10-24 20:30:14 +010038{
Mark Rutland00ef54b2013-10-24 20:30:14 +010039 if (!psci_ops.cpu_on) {
40 pr_err("no cpu_on method, not booting CPU%d\n", cpu);
41 return -ENODEV;
42 }
43
Mark Rutland00ef54b2013-10-24 20:30:14 +010044 return 0;
45}
46
Mark Rutland652af892013-10-24 20:30:16 +010047static int cpu_psci_cpu_boot(unsigned int cpu)
48{
Laura Abbottf8fee94e2017-01-10 13:35:49 -080049 int err = psci_ops.cpu_on(cpu_logical_map(cpu), __pa_symbol(secondary_entry));
Mark Rutland652af892013-10-24 20:30:16 +010050 if (err)
Vladimir Murzin288ac262014-02-28 09:57:47 +000051 pr_err("failed to boot CPU%d (%d)\n", cpu, err);
Mark Rutland652af892013-10-24 20:30:16 +010052
53 return err;
54}
55
Mark Rutland831ccf72013-10-24 20:30:19 +010056#ifdef CONFIG_HOTPLUG_CPU
57static int cpu_psci_cpu_disable(unsigned int cpu)
58{
59 /* Fail early if we don't have CPU_OFF support */
60 if (!psci_ops.cpu_off)
61 return -EOPNOTSUPP;
Mark Rutlandff3010e2015-04-22 18:10:26 +010062
63 /* Trusted OS will deny CPU_OFF */
64 if (psci_tos_resident_on(cpu))
65 return -EPERM;
66
Mark Rutland831ccf72013-10-24 20:30:19 +010067 return 0;
68}
69
70static void cpu_psci_cpu_die(unsigned int cpu)
71{
72 int ret;
73 /*
74 * There are no known implementations of PSCI actually using the
75 * power state field, pass a sensible default for now.
76 */
Mark Rutlandc8cc4272015-04-30 17:59:03 +010077 u32 state = PSCI_POWER_STATE_TYPE_POWER_DOWN <<
78 PSCI_0_2_POWER_STATE_TYPE_SHIFT;
Mark Rutland831ccf72013-10-24 20:30:19 +010079
80 ret = psci_ops.cpu_off(state);
81
Vladimir Murzin288ac262014-02-28 09:57:47 +000082 pr_crit("unable to power off CPU%u (%d)\n", cpu, ret);
Mark Rutland831ccf72013-10-24 20:30:19 +010083}
Ashwin Chaugulec814ca02014-05-07 10:18:36 -040084
85static int cpu_psci_cpu_kill(unsigned int cpu)
86{
87 int err, i;
88
89 if (!psci_ops.affinity_info)
Mark Rutland6b99c68c2015-04-20 17:55:30 +010090 return 0;
Ashwin Chaugulec814ca02014-05-07 10:18:36 -040091 /*
92 * cpu_kill could race with cpu_die and we can
93 * potentially end up declaring this cpu undead
94 * while it is dying. So, try again a few times.
95 */
96
97 for (i = 0; i < 10; i++) {
98 err = psci_ops.affinity_info(cpu_logical_map(cpu), 0);
99 if (err == PSCI_0_2_AFFINITY_LEVEL_OFF) {
100 pr_info("CPU%d killed.\n", cpu);
Mark Rutland6b99c68c2015-04-20 17:55:30 +0100101 return 0;
Ashwin Chaugulec814ca02014-05-07 10:18:36 -0400102 }
103
104 msleep(10);
105 pr_info("Retrying again to check for CPU kill\n");
106 }
107
108 pr_warn("CPU%d may not have shut down cleanly (AFFINITY_INFO reports %d)\n",
109 cpu, err);
Mark Rutland6b99c68c2015-04-20 17:55:30 +0100110 return -ETIMEDOUT;
Ashwin Chaugulec814ca02014-05-07 10:18:36 -0400111}
Mark Rutland831ccf72013-10-24 20:30:19 +0100112#endif
113
Mark Rutlandcd1aebf2013-10-24 20:30:15 +0100114const struct cpu_operations cpu_psci_ops = {
Mark Rutland00ef54b2013-10-24 20:30:14 +0100115 .name = "psci",
Lorenzo Pieralisi18910ab2013-09-27 10:25:02 +0100116#ifdef CONFIG_CPU_IDLE
Lorenzo Pieralisi8b6f2492016-02-01 18:01:30 +0100117 .cpu_init_idle = psci_cpu_init_idle,
118 .cpu_suspend = psci_cpu_suspend_enter,
Lorenzo Pieralisi18910ab2013-09-27 10:25:02 +0100119#endif
Mark Rutlandcd1aebf2013-10-24 20:30:15 +0100120 .cpu_init = cpu_psci_cpu_init,
121 .cpu_prepare = cpu_psci_cpu_prepare,
Mark Rutland652af892013-10-24 20:30:16 +0100122 .cpu_boot = cpu_psci_cpu_boot,
Mark Rutland831ccf72013-10-24 20:30:19 +0100123#ifdef CONFIG_HOTPLUG_CPU
124 .cpu_disable = cpu_psci_cpu_disable,
125 .cpu_die = cpu_psci_cpu_die,
Ashwin Chaugulec814ca02014-05-07 10:18:36 -0400126 .cpu_kill = cpu_psci_cpu_kill,
Mark Rutland831ccf72013-10-24 20:30:19 +0100127#endif
Mark Rutland00ef54b2013-10-24 20:30:14 +0100128};
129