blob: c2fdc22e4c0686a81a7bf4205fc66d32b0024754 [file] [log] [blame]
Achin Guptadfd6f0c2013-02-26 15:33:25 +00001/*
2 * arch/arm/mach-vexpress/tc2_pm_psci.c - TC2 PSCI support
3 *
4 * Created by: Achin Gupta, December 2012
5 * Copyright: (C) 2012 ARM Limited
6 *
7 * Some portions of this file were originally written by Nicolas Pitre
8 * Copyright: (C) 2012 Linaro Limited
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/init.h>
16#include <linux/kernel.h>
17#include <linux/spinlock.h>
18#include <linux/errno.h>
19
Jon Medhurste64a97a2013-03-21 11:15:56 +000020#include <asm/mcpm.h>
Achin Guptadfd6f0c2013-02-26 15:33:25 +000021#include <asm/proc-fns.h>
22#include <asm/cacheflush.h>
23#include <asm/psci.h>
24#include <asm/atomic.h>
25#include <asm/cputype.h>
Jon Medhurstb62c31f2013-03-21 12:00:09 +000026#include <asm/cp15.h>
Achin Guptadfd6f0c2013-02-26 15:33:25 +000027
28#include <mach/motherboard.h>
29#include <mach/tc2.h>
30
31#include <linux/vexpress.h>
32
33/*
34 * Platform specific state id understood by the firmware and used to
35 * program the power controller
36 */
37#define PSCI_POWER_STATE_ID 0
38
39static atomic_t tc2_pm_use_count[TC2_MAX_CPUS][TC2_MAX_CLUSTERS];
40
41static int tc2_pm_psci_power_up(unsigned int cpu, unsigned int cluster)
42{
43 unsigned int mpidr = (cluster << 8) | cpu;
44 int ret = 0;
45
46 BUG_ON(!psci_ops.cpu_on);
47
48 switch (atomic_inc_return(&tc2_pm_use_count[cpu][cluster])) {
49 case 1:
50 /*
51 * This is a request to power up a cpu that linux thinks has
52 * been powered down. Retries are needed if the firmware has
53 * seen the power down request as yet.
54 */
55 do
56 ret = psci_ops.cpu_on(mpidr,
Jon Medhurste64a97a2013-03-21 11:15:56 +000057 virt_to_phys(mcpm_entry_point));
Achin Guptadfd6f0c2013-02-26 15:33:25 +000058 while (ret == -EAGAIN);
59
60 return ret;
61 case 2:
62 /* This power up request has overtaken a power down request */
63 return ret;
64 default:
65 /* Any other value is a bug */
66 BUG();
67 }
68}
69
70static void tc2_pm_psci_power_down(void)
71{
72 struct psci_power_state power_state;
73 unsigned int mpidr, cpu, cluster;
74
75 mpidr = read_cpuid_mpidr();
76 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
77 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
78
79 BUG_ON(!psci_ops.cpu_off);
80
81 switch (atomic_dec_return(&tc2_pm_use_count[cpu][cluster])) {
82 case 1:
83 /*
84 * Overtaken by a power up. Flush caches, exit coherency,
85 * return & fake a reset
86 */
Jon Medhurstb62c31f2013-03-21 12:00:09 +000087 set_cr(get_cr() & ~CR_C);
Achin Guptadfd6f0c2013-02-26 15:33:25 +000088
89 flush_cache_louis();
90
Jon Medhurstb62c31f2013-03-21 12:00:09 +000091 asm volatile ("clrex");
92 set_auxcr(get_auxcr() & ~(1 << 6));
Achin Guptadfd6f0c2013-02-26 15:33:25 +000093
94 return;
95 case 0:
96 /* A normal request to possibly power down the cluster */
97 power_state.id = PSCI_POWER_STATE_ID;
98 power_state.type = PSCI_POWER_STATE_TYPE_POWER_DOWN;
99 power_state.affinity_level = PSCI_POWER_STATE_AFFINITY_LEVEL1;
100
101 psci_ops.cpu_off(power_state);
102
103 /* On success this function never returns */
104 default:
105 /* Any other value is a bug */
106 BUG();
107 }
108}
109
110static void tc2_pm_psci_suspend(u64 unused)
111{
112 struct psci_power_state power_state;
113
114 BUG_ON(!psci_ops.cpu_suspend);
115
116 /* On TC2 always attempt to power down the cluster */
117 power_state.id = PSCI_POWER_STATE_ID;
118 power_state.type = PSCI_POWER_STATE_TYPE_POWER_DOWN;
119 power_state.affinity_level = PSCI_POWER_STATE_AFFINITY_LEVEL1;
120
Jon Medhurste64a97a2013-03-21 11:15:56 +0000121 psci_ops.cpu_suspend(power_state, virt_to_phys(mcpm_entry_point));
Achin Guptadfd6f0c2013-02-26 15:33:25 +0000122
123 /* On success this function never returns */
124 BUG();
125}
126
Jon Medhurste64a97a2013-03-21 11:15:56 +0000127static const struct mcpm_platform_ops tc2_pm_power_ops = {
Achin Guptadfd6f0c2013-02-26 15:33:25 +0000128 .power_up = tc2_pm_psci_power_up,
129 .power_down = tc2_pm_psci_power_down,
130 .suspend = tc2_pm_psci_suspend,
131};
132
133static void __init tc2_pm_usage_count_init(void)
134{
135 unsigned int mpidr, cpu, cluster;
136
137 mpidr = read_cpuid_mpidr();
138 cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
139 cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
140
141 pr_debug("%s: cpu %u cluster %u\n", __func__, cpu, cluster);
142 BUG_ON(cluster >= TC2_MAX_CLUSTERS ||
143 cpu >= vexpress_spc_get_nb_cpus(cluster));
144
145 atomic_set(&tc2_pm_use_count[cpu][cluster], 1);
146}
147
148static int __init tc2_pm_psci_init(void)
149{
150 int ret;
151
152 ret = psci_probe();
153 if (ret) {
154 pr_debug("psci not found. Aborting psci init\n");
155 return -ENODEV;
156 }
157
Jon Medhurst521caf62013-06-19 10:17:00 +0100158 if (!vexpress_spc_check_loaded()) {
159 pr_debug("spc not found. Aborting psci init\n");
160 return -ENODEV;
161 }
162
Achin Guptadfd6f0c2013-02-26 15:33:25 +0000163 tc2_pm_usage_count_init();
164
Jon Medhurste64a97a2013-03-21 11:15:56 +0000165 ret = mcpm_platform_register(&tc2_pm_power_ops);
Achin Guptadfd6f0c2013-02-26 15:33:25 +0000166 if (!ret)
Jon Medhurste64a97a2013-03-21 11:15:56 +0000167 ret = mcpm_sync_init(NULL);
Achin Guptadfd6f0c2013-02-26 15:33:25 +0000168 if (!ret)
169 pr_info("TC2 power management initialized\n");
170 return ret;
171}
172
173early_initcall(tc2_pm_psci_init);