summaryrefstree
path: root/kernel/arch/arm64/mach-exynos/exynos-powermode-smp.c
blob: 28004221d85aec24d302e247e4578e473e504cfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
 *		http://www.samsung.com/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/cpumask.h>
#include <linux/of.h>
#include <linux/tick.h>

#include <asm/cputype.h>
#include <asm/smp_plat.h>
#include <asm/topology.h>

#include <mach/exynos-pm.h>
#include <mach/exynos-powermode-smp.h>
#include <mach/exynos-pm.h>
#include <mach/pmu.h>
#include <mach/pmu_cal_sys.h>
#include <mach/regs-clock.h>
#include <mach/regs-pmu.h>

static int boot_core_id __read_mostly;
static int boot_cluster_id __read_mostly;

static void store_boot_cpu_info(void)
{
	unsigned int mpidr = read_cpuid_mpidr();

	boot_core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
	boot_cluster_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);

	pr_info("A booting CPU: core %d cluster %d\n", boot_core_id,
						       boot_cluster_id);
}

static bool is_in_boot_cluster(unsigned int cpu)
{
	return boot_cluster_id == MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 1);
}

static void exynos_idle_clock_down(bool enable)
{
	unsigned int val;
	void __iomem *boot_cluster_reg, *nonboot_cluster_reg;

	boot_cluster_reg = EXYNOS7580_CPU_PWR_CTRL3;
	nonboot_cluster_reg = EXYNOS7580_APL_PWR_CTRL3;

	val = enable ? USE_L2QACTIVE : 0;

	__raw_writel(val, boot_cluster_reg);
	__raw_writel(val, nonboot_cluster_reg);
}

static DEFINE_SPINLOCK(cpd_lock);
static struct cpumask cpd_state_mask;

static void init_cpd_state_mask(void)
{
	cpumask_clear(&cpd_state_mask);
}

static void update_cpd_state(bool down, unsigned int cpu)
{
	if (down)
		cpumask_set_cpu(cpu, &cpd_state_mask);
	else
		cpumask_clear_cpu(cpu, &cpd_state_mask);
}

static unsigned int calc_expected_residency(unsigned int cpu)
{
	struct clock_event_device *dev = per_cpu(tick_cpu_device, cpu).evtdev;

	ktime_t now = ktime_get(), next = dev->next_event;

	return (unsigned int)ktime_to_us(ktime_sub(next, now));
}

static int is_busy(unsigned int target_residency, const struct cpumask *mask)
{
	int cpu;

	for_each_cpu_and(cpu, cpu_possible_mask, mask) {
		if (!cpumask_test_cpu(cpu, &cpd_state_mask))
			return -EBUSY;

		if (calc_expected_residency(cpu) < target_residency)
			return -EBUSY;
	}

	return 0;
}

#define LPC_FLAG_ADDR			(S5P_VA_SYSRAM_NS + 0x50)

static unsigned int lpc_reg_num __read_mostly;
static struct hw_info lpc_regs[MAX_NUM_REGS] __read_mostly;

static void set_lpc_flag(void)
{
	__raw_writel(0x1, LPC_FLAG_ADDR);
}

bool is_lpc_available(unsigned int target_residency)
{
	/*
	 * This logic comes from DCH.
	 */
	if (num_online_cpus() != 1)
		return false;

	if (is_busy(target_residency, cpu_online_mask))
		return false;

	if (check_hw_status(lpc_regs, lpc_reg_num))
		return false;

	if (exynos_lpc_prepare())
		return false;

	if (pwm_check_enable_cnt())
		return false;

	return true;
}

int determine_cpd(int index, int c2_index, unsigned int cpu,
		  unsigned int target_residency)
{
	exynos_cpu.power_down(cpu);

	if (index == c2_index)
		return c2_index;

	spin_lock(&cpd_lock);

	update_cpd_state(true, cpu);

	if (is_lpc_available(target_residency)) {
		set_lpc_flag();
		s3c24xx_serial_fifo_wait();
	}

	if (is_in_boot_cluster(cpu)) {
		index = c2_index;
		goto unlock;
	}

	if (is_busy(target_residency, cpu_coregroup_mask(cpu)))
		index = c2_index;
	else
		exynos_cpu_sequencer_ctrl(true);

unlock:
	spin_unlock(&cpd_lock);

	return index;
}

void wakeup_from_c2(unsigned int cpu)
{
	exynos_cpu.power_up(cpu);

	spin_lock(&cpd_lock);

	update_cpd_state(false, cpu);

	if (is_in_boot_cluster(cpu))
		goto unlock;

	exynos_cpu_sequencer_ctrl(false);

unlock:
	spin_unlock(&cpd_lock);
}

static unsigned int lpm_reg_num __read_mostly;
static struct hw_info lpm_regs[MAX_NUM_REGS] __read_mostly;

int determine_lpm(void)
{
	if (check_hw_status(lpm_regs, lpm_reg_num))
		return SYS_AFTR;

	if (exynos_lpa_prepare())
		return SYS_AFTR;

	return SYS_LPA;
}

static void parse_dt_reg_list(struct device_node *np, const char *reg,
			      const char *val, struct hw_info *regs,
			      unsigned int *reg_num)
{
	unsigned int i, reg_len, val_len;
	const __be32 *reg_list, *val_list;

	reg_list = of_get_property(np, reg, &reg_len);
	val_list = of_get_property(np, val, &val_len);

	if (!reg_list) {
		pr_warn("%s property does not exist\n", reg);
		return;
	}

	if (!val_list) {
		pr_warn("%s property does not exist\n", val);
		return;
	}

	BUG_ON(reg_len != val_len);

	*reg_num = reg_len / sizeof(unsigned int);

	for (i = 0; i < *reg_num; i++) {
		regs[i].addr = ioremap(be32_to_cpup(reg_list++), SZ_32);
		BUG_ON(!regs[i].addr);
		regs[i].mask = be32_to_cpup(val_list++);
	}
}

static void exynos_lpm_dt_init(void)
{
	struct device_node *np = of_find_node_by_name(NULL, "low-power-mode");

	parse_dt_reg_list(np, "lpc-reg", "lpc-val", lpc_regs, &lpc_reg_num);
	parse_dt_reg_list(np, "lpm-reg", "lpm-val", lpm_regs, &lpm_reg_num);
}

static void exynos_sys_powerdown_set_clk(enum sys_powerdown mode)
{
}

static void exynos_sys_powerdown_restore_clk(enum sys_powerdown mode)
{
}

void exynos_prepare_sys_powerdown(enum sys_powerdown mode)
{
	exynos_set_wakeupmask(mode);

	exynos_pmu_cal_sys_prepare(mode);

	exynos_idle_clock_down(false);

	switch (mode) {
	case SYS_LPA:
		exynos_lpa_enter();
		break;
	default:
		break;
	}

	exynos_sys_powerdown_set_clk(mode);
}

void exynos_wakeup_sys_powerdown(enum sys_powerdown mode, bool early_wakeup)
{
	if (early_wakeup)
		exynos_pmu_cal_sys_earlywake(mode);
	else
		exynos_pmu_cal_sys_post(mode);

	exynos_clear_wakeupmask();

	exynos_idle_clock_down(true);

	exynos_sys_powerdown_restore_clk(mode);

	switch (mode) {
	case SYS_LPA:
		exynos_lpa_exit();
		break;
	default:
		break;
	}
}

bool exynos_sys_powerdown_enabled(void)
{
	/*
	 * When system enters low power mode,
	 * this bit changes automatically to high
	 */
	return !((__raw_readl(EXYNOS_PMU_CENTRAL_SEQ_CONFIGURATION) &
			CENTRALSEQ_PWR_CFG));
}

int __init exynos_powermode_init(void)
{
	store_boot_cpu_info();

	if (IS_ENABLED(CONFIG_CPU_IDLE_EXYNOS))
		exynos_idle_clock_down(true);

	init_cpd_state_mask();

	exynos_lpm_dt_init();

	exynos_pmu_cal_sys_init();

	return 0;
}
arch_initcall(exynos_powermode_init);