aboutsummaryrefslogtreecommitdiff
path: root/arch/arm/mach-hisi/platmcpm.c
blob: 143fcdf3fd9f8a60d836b956cb284126d9279d97 (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
/*
 * Copyright (c) 2013-2014 Linaro Ltd.
 * Copyright (c) 2013-2014 Hisilicon Limited.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 */
#include <linux/delay.h>
#include <linux/io.h>
#include <linux/of_address.h>

#include <asm/cputype.h>
#include <asm/cp15.h>
#include <asm/mcpm.h>

#include "core.h"

#define SC_CPU_RESET_STATUS(x)		(0x1520 + (x << 3))

/*
 * bits definition in SC_CPU_RESET_STATUS[x]
 * 1 -- reset status; 0 -- unreset status
 */
#define CORE_WFI_STATUS(x)		(1 << (x + 16))

#define HIP04_MAX_CLUSTERS		4
#define HIP04_MAX_CPUS_PER_CLUSTER	4

#define POLL_MSEC			10
#define TIMEOUT_MSEC			1000

#define HIP04_CORE_PLUG_ON		0xa1
#define HIP04_CORE_PLUG_OFF		0xa2
#define HIP04_CORE_WAIT			0x5a

struct hip04_secondary_cpu_data {
	u32	relocation_entry;
	u32	relocation_size;
};

static void __iomem *relocation, *sysctrl;
static int hip04_cpu_table[HIP04_MAX_CLUSTERS][HIP04_MAX_CPUS_PER_CLUSTER];
static DEFINE_SPINLOCK(boot_lock);
static struct hip04_secondary_cpu_data hip04_boot;

static int hip04_mcpm_power_up(unsigned int cpu, unsigned int cluster)
{
	if (!relocation)
		return -ENODEV;
	if (cluster >= HIP04_MAX_CLUSTERS || cpu >= HIP04_MAX_CPUS_PER_CLUSTER)
		return -EINVAL;

	spin_lock_irq(&boot_lock);

	if (hip04_cpu_table[cluster][cpu])
		goto out;

	/*
	 * Word 0: stage index for SMP boot protocol in HiP04
	 * Word 1: Entry address of plug on (MCPM entry in kernel)
	 * Word 2/3: not used in kernel
	 * Word 4: Bootwrapper Entry (set in UEFI)
	 */
	writel_relaxed(HIP04_CORE_PLUG_ON, relocation);
	writel_relaxed(virt_to_phys(mcpm_entry_point), relocation + 4);

out:
	hip04_cpu_table[cluster][cpu]++;
	spin_unlock_irq(&boot_lock);

	return 0;
}

static void hip04_mcpm_power_down(void)
{
	unsigned int mpidr, cpu, cluster;
	bool skip_wfi = false;

	mpidr = read_cpuid_mpidr();
	cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
	cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);

	__mcpm_cpu_going_down(cpu, cluster);

	spin_lock(&boot_lock);
	BUG_ON(__mcpm_cluster_state(cluster) != CLUSTER_UP);
	hip04_cpu_table[cluster][cpu]--;
	if (hip04_cpu_table[cluster][cpu] == 1) {
		/* A power_up request went ahead of us. */
		skip_wfi = true;
	} else if (hip04_cpu_table[cluster][cpu] > 1) {
		pr_err("Cluster %d CPU%d boots multiple times\n", cluster, cpu);
		BUG();
	}
	spin_unlock(&boot_lock);

	v7_exit_coherency_flush(louis);

	__mcpm_cpu_down(cpu, cluster);

	if (!skip_wfi)
		wfi();
}

static int hip04_mcpm_wait_for_powerdown(unsigned int cpu, unsigned int cluster)
{
	unsigned int data, tries;

	BUG_ON(cluster >= HIP04_MAX_CLUSTERS ||
	       cpu >= HIP04_MAX_CPUS_PER_CLUSTER);

	for (tries = 0; tries < TIMEOUT_MSEC / POLL_MSEC; tries++) {
		data = readl_relaxed(sysctrl + SC_CPU_RESET_STATUS(cluster));
		if (!(data & CORE_WFI_STATUS(cpu))) {
			msleep(POLL_MSEC);
			continue;
		}
		return 0;
	}
	return -ETIMEDOUT;
}

static void hip04_mcpm_powered_up(void)
{
	if (!relocation)
		return;
	spin_lock(&boot_lock);
	writel_relaxed(HIP04_CORE_WAIT, relocation);
	writel_relaxed(0, relocation + 4);
	spin_unlock(&boot_lock);
}

static const struct mcpm_platform_ops hip04_mcpm_ops = {
	.power_up		= hip04_mcpm_power_up,
	.power_down		= hip04_mcpm_power_down,
	.wait_for_powerdown	= hip04_mcpm_wait_for_powerdown,
	.powered_up		= hip04_mcpm_powered_up,
};

static bool __init hip04_cpu_table_init(void)
{
	unsigned int mpidr, cpu, cluster;

	mpidr = read_cpuid_mpidr();
	cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
	cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);

	if (cluster >= HIP04_MAX_CLUSTERS ||
	    cpu >= HIP04_MAX_CPUS_PER_CLUSTER) {
		pr_err("%s: boot CPU is out of bound!\n", __func__);
		return false;
	}
	hip04_cpu_table[cluster][cpu] = 1;
	return true;
}

static int __init hip04_mcpm_init(void)
{
	struct device_node *np;
	int ret = -ENODEV;

	np = of_find_compatible_node(NULL, NULL, "hisilicon,sysctrl");
	if (!np)
		goto err;

	if (of_property_read_u32(np, "relocation-entry",
				 &hip04_boot.relocation_entry)) {
		pr_err("failed to get relocation-entry\n");
		ret = -EINVAL;
		goto err;
	}
	if (of_property_read_u32(np, "relocation-size",
				 &hip04_boot.relocation_size)) {
		pr_err("failed to get relocation-size\n");
		ret = -EINVAL;
		goto err;
	}

	relocation = ioremap(hip04_boot.relocation_entry,
			     hip04_boot.relocation_size);
	if (!relocation) {
		pr_err("failed to map relocation space\n");
		ret = -ENOMEM;
		goto err;
	}

	if (!hip04_cpu_table_init())
		return -EINVAL;
	ret = mcpm_platform_register(&hip04_mcpm_ops);
	if (!ret) {
		mcpm_sync_init(NULL);
		pr_info("HiP04 MCPM initialized\n");
	}
	mcpm_smp_set_ops();
	return ret;
err:
	return ret;
}
early_initcall(hip04_mcpm_init);