Blame view

arch/arm/mach-uniphier/platsmp.c 5.42 KB
ba56a9876   Masahiro Yamada   ARM: UniPhier: ad...
1
2
3
4
5
6
7
8
9
10
11
12
13
  /*
   * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   */
b1e4006ae   Masahiro Yamada   ARM: uniphier: re...
14
  #define pr_fmt(fmt)		"uniphier: " fmt
ba56a9876   Masahiro Yamada   ARM: UniPhier: ad...
15
16
  #include <linux/init.h>
  #include <linux/io.h>
b1e4006ae   Masahiro Yamada   ARM: uniphier: re...
17
18
19
20
21
22
23
  #include <linux/ioport.h>
  #include <linux/of.h>
  #include <linux/of_address.h>
  #include <linux/sizes.h>
  #include <asm/cacheflush.h>
  #include <asm/hardware/cache-uniphier.h>
  #include <asm/pgtable.h>
ba56a9876   Masahiro Yamada   ARM: UniPhier: ad...
24
25
  #include <asm/smp.h>
  #include <asm/smp_scu.h>
b1e4006ae   Masahiro Yamada   ARM: uniphier: re...
26
27
28
29
30
  /*
   * The secondary CPUs check this register from the boot ROM for the jump
   * destination.  After that, it can be reused as a scratch register.
   */
  #define UNIPHIER_SBC_ROM_BOOT_RSV2	0x1208
ba56a9876   Masahiro Yamada   ARM: UniPhier: ad...
31

b1e4006ae   Masahiro Yamada   ARM: uniphier: re...
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  static void __iomem *uniphier_smp_rom_boot_rsv2;
  static unsigned int uniphier_smp_max_cpus;
  
  extern char uniphier_smp_trampoline;
  extern char uniphier_smp_trampoline_jump;
  extern char uniphier_smp_trampoline_poll_addr;
  extern char uniphier_smp_trampoline_end;
  
  /*
   * Copy trampoline code to the tail of the 1st section of the page table used
   * in the boot ROM.  This area is directly accessible by the secondary CPUs
   * for all the UniPhier SoCs.
   */
  static const phys_addr_t uniphier_smp_trampoline_dest_end = SECTION_SIZE;
  static phys_addr_t uniphier_smp_trampoline_dest;
  
  static int __init uniphier_smp_copy_trampoline(phys_addr_t poll_addr)
ba56a9876   Masahiro Yamada   ARM: UniPhier: ad...
49
  {
b1e4006ae   Masahiro Yamada   ARM: uniphier: re...
50
51
  	size_t trmp_size;
  	static void __iomem *trmp_base;
ba56a9876   Masahiro Yamada   ARM: UniPhier: ad...
52

b1e4006ae   Masahiro Yamada   ARM: uniphier: re...
53
54
55
56
  	if (!uniphier_cache_l2_is_enabled()) {
  		pr_warn("outer cache is needed for SMP, but not enabled
  ");
  		return -ENODEV;
ba56a9876   Masahiro Yamada   ARM: UniPhier: ad...
57
  	}
b1e4006ae   Masahiro Yamada   ARM: uniphier: re...
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
  	uniphier_cache_l2_set_locked_ways(1);
  
  	outer_flush_all();
  
  	trmp_size = &uniphier_smp_trampoline_end - &uniphier_smp_trampoline;
  	uniphier_smp_trampoline_dest = uniphier_smp_trampoline_dest_end -
  								trmp_size;
  
  	uniphier_cache_l2_touch_range(uniphier_smp_trampoline_dest,
  				      uniphier_smp_trampoline_dest_end);
  
  	trmp_base = ioremap_cache(uniphier_smp_trampoline_dest, trmp_size);
  	if (!trmp_base) {
  		pr_err("failed to map trampoline destination area
  ");
  		return -ENOMEM;
  	}
  
  	memcpy(trmp_base, &uniphier_smp_trampoline, trmp_size);
  
  	writel(virt_to_phys(secondary_startup),
  	       trmp_base + (&uniphier_smp_trampoline_jump -
  			    &uniphier_smp_trampoline));
  
  	writel(poll_addr, trmp_base + (&uniphier_smp_trampoline_poll_addr -
  				       &uniphier_smp_trampoline));
  
  	flush_cache_all();	/* flush out trampoline code to outer cache */
  
  	iounmap(trmp_base);
  
  	return 0;
  }
  
  static int __init uniphier_smp_prepare_trampoline(unsigned int max_cpus)
  {
  	struct device_node *np;
  	struct resource res;
  	phys_addr_t rom_rsv2_phys;
  	int ret;
  
  	np = of_find_compatible_node(NULL, NULL,
  				"socionext,uniphier-system-bus-controller");
  	ret = of_address_to_resource(np, 1, &res);
  	if (ret) {
  		pr_err("failed to get resource of system-bus-controller
  ");
  		return ret;
  	}
  
  	rom_rsv2_phys = res.start + UNIPHIER_SBC_ROM_BOOT_RSV2;
  
  	ret = uniphier_smp_copy_trampoline(rom_rsv2_phys);
  	if (ret)
  		return ret;
  
  	uniphier_smp_rom_boot_rsv2 = ioremap(rom_rsv2_phys, sizeof(SZ_4));
  	if (!uniphier_smp_rom_boot_rsv2) {
  		pr_err("failed to map ROM_BOOT_RSV2 register
  ");
  		return -ENOMEM;
  	}
  
  	writel(uniphier_smp_trampoline_dest, uniphier_smp_rom_boot_rsv2);
  	asm("sev"); /* Bring up all secondary CPUs to the trampoline code */
  
  	uniphier_smp_max_cpus = max_cpus;	/* save for later use */
  
  	return 0;
  }
  
  static void __init uniphier_smp_unprepare_trampoline(void)
  {
  	iounmap(uniphier_smp_rom_boot_rsv2);
  
  	if (uniphier_smp_trampoline_dest)
  		outer_inv_range(uniphier_smp_trampoline_dest,
  				uniphier_smp_trampoline_dest_end);
  
  	uniphier_cache_l2_set_locked_ways(0);
  }
  
  static int __init uniphier_smp_enable_scu(void)
  {
  	unsigned long scu_base_phys = 0;
  	void __iomem *scu_base;
ba56a9876   Masahiro Yamada   ARM: UniPhier: ad...
144
145
146
147
148
149
  	if (scu_a9_has_base())
  		scu_base_phys = scu_a9_get_base();
  
  	if (!scu_base_phys) {
  		pr_err("failed to get scu base
  ");
b1e4006ae   Masahiro Yamada   ARM: uniphier: re...
150
  		return -ENODEV;
ba56a9876   Masahiro Yamada   ARM: UniPhier: ad...
151
152
153
154
  	}
  
  	scu_base = ioremap(scu_base_phys, SZ_128);
  	if (!scu_base) {
b1e4006ae   Masahiro Yamada   ARM: uniphier: re...
155
156
157
  		pr_err("failed to map scu base
  ");
  		return -ENOMEM;
ba56a9876   Masahiro Yamada   ARM: UniPhier: ad...
158
159
160
161
  	}
  
  	scu_enable(scu_base);
  	iounmap(scu_base);
b1e4006ae   Masahiro Yamada   ARM: uniphier: re...
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
  	return 0;
  }
  
  static void __init uniphier_smp_prepare_cpus(unsigned int max_cpus)
  {
  	static cpumask_t only_cpu_0 = { CPU_BITS_CPU0 };
  	int ret;
  
  	ret = uniphier_smp_prepare_trampoline(max_cpus);
  	if (ret)
  		goto err;
  
  	ret = uniphier_smp_enable_scu();
  	if (ret)
  		goto err;
ba56a9876   Masahiro Yamada   ARM: UniPhier: ad...
177
178
179
180
181
  	return;
  err:
  	pr_warn("disabling SMP
  ");
  	init_cpu_present(&only_cpu_0);
b1e4006ae   Masahiro Yamada   ARM: uniphier: re...
182
  	uniphier_smp_unprepare_trampoline();
ba56a9876   Masahiro Yamada   ARM: UniPhier: ad...
183
  }
b1e4006ae   Masahiro Yamada   ARM: uniphier: re...
184
185
  static int __init uniphier_smp_boot_secondary(unsigned int cpu,
  					      struct task_struct *idle)
ba56a9876   Masahiro Yamada   ARM: UniPhier: ad...
186
  {
b1e4006ae   Masahiro Yamada   ARM: uniphier: re...
187
188
  	if (WARN_ON_ONCE(!uniphier_smp_rom_boot_rsv2))
  		return -EFAULT;
ba56a9876   Masahiro Yamada   ARM: UniPhier: ad...
189

b1e4006ae   Masahiro Yamada   ARM: uniphier: re...
190
191
  	writel(cpu, uniphier_smp_rom_boot_rsv2);
  	readl(uniphier_smp_rom_boot_rsv2); /* relax */
ba56a9876   Masahiro Yamada   ARM: UniPhier: ad...
192

b1e4006ae   Masahiro Yamada   ARM: uniphier: re...
193
194
195
196
197
198
  	asm("sev"); /* wake up secondary CPUs sleeping in the trampoline */
  
  	if (cpu == uniphier_smp_max_cpus - 1) {
  		/* clean up resources if this is the last CPU */
  		uniphier_smp_unprepare_trampoline();
  	}
ba56a9876   Masahiro Yamada   ARM: UniPhier: ad...
199

b1e4006ae   Masahiro Yamada   ARM: uniphier: re...
200
  	return 0;
ba56a9876   Masahiro Yamada   ARM: UniPhier: ad...
201
  }
b1e4006ae   Masahiro Yamada   ARM: uniphier: re...
202
  static struct smp_operations uniphier_smp_ops __initdata = {
ba56a9876   Masahiro Yamada   ARM: UniPhier: ad...
203
  	.smp_prepare_cpus	= uniphier_smp_prepare_cpus,
b1e4006ae   Masahiro Yamada   ARM: uniphier: re...
204
  	.smp_boot_secondary	= uniphier_smp_boot_secondary,
ba56a9876   Masahiro Yamada   ARM: UniPhier: ad...
205
206
207
  };
  CPU_METHOD_OF_DECLARE(uniphier_smp, "socionext,uniphier-smp",
  		      &uniphier_smp_ops);