xref: /freebsd/sys/arm/annapurna/alpine/alpine_machdep_mp.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
1 /*-
2  * Copyright (c) 2013 Ruslan Bukin <br@bsdpad.com>
3  * Copyright (c) 2015 Semihalf
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/lock.h>
35 #include <sys/mutex.h>
36 #include <sys/smp.h>
37 #include <sys/cpuset.h>
38 
39 #include <vm/vm.h>
40 #include <vm/pmap.h>
41 
42 #include <machine/smp.h>
43 #include <machine/fdt.h>
44 #include <machine/intr.h>
45 #include <machine/cpu-v6.h>
46 
47 #include <dev/fdt/fdt_common.h>
48 #include <dev/ofw/ofw_cpu.h>
49 #include <dev/ofw/ofw_bus_subr.h>
50 
51 #define AL_CPU_RESUME_WATERMARK_REG		0x00
52 #define AL_CPU_RESUME_FLAGS_REG			0x04
53 #define AL_CPU_RESUME_PCPU_RADDR_REG(cpu)	(0x08 + 0x04 + 8*(cpu))
54 #define AL_CPU_RESUME_PCPU_FLAGS(cpu)		(0x08 + 8*(cpu))
55 
56 /* Per-CPU flags */
57 #define AL_CPU_RESUME_FLG_PERCPU_DONT_RESUME	(1 << 2)
58 
59 /* The expected magic number for validating the resume addresses */
60 #define AL_CPU_RESUME_MAGIC_NUM			0xf0e1d200
61 #define AL_CPU_RESUME_MAGIC_NUM_MASK		0xffffff00
62 
63 /* The expected minimal version number for validating the capabilities */
64 #define AL_CPU_RESUME_MIN_VER			0x000000c3
65 #define AL_CPU_RESUME_MIN_VER_MASK		0x000000ff
66 
67 /* Field controlling the boot-up of companion cores */
68 #define AL_NB_INIT_CONTROL		(0x8)
69 #define AL_NB_CONFIG_STATUS_PWR_CTRL(cpu)	(0x2020 + (cpu)*0x100)
70 
71 #define SERDES_NUM_GROUPS	4
72 #define SERDES_GROUP_SIZE	0x400
73 
74 extern bus_addr_t al_devmap_pa;
75 extern bus_addr_t al_devmap_size;
76 
77 extern void mpentry(void);
78 
79 int alpine_serdes_resource_get(uint32_t group, bus_space_tag_t *tag,
80     bus_addr_t *baddr);
81 static int platform_mp_get_core_cnt(void);
82 static int alpine_get_cpu_resume_base(u_long *pbase, u_long *psize);
83 static int alpine_get_nb_base(u_long *pbase, u_long *psize);
84 static int alpine_get_serdes_base(u_long *pbase, u_long *psize);
85 int alpine_serdes_resource_get(uint32_t group, bus_space_tag_t *tag,
86     bus_addr_t *baddr);
87 static boolean_t alpine_validate_cpu(u_int, phandle_t, u_int, pcell_t *);
88 
89 static boolean_t
90 alpine_validate_cpu(u_int id, phandle_t child, u_int addr_cell, pcell_t *reg)
91 {
92 	return fdt_is_compatible(child, "arm,cortex-a15");
93 }
94 
95 static int
96 platform_mp_get_core_cnt(void)
97 {
98 	static int ncores = 0;
99 	int nchilds;
100 	uint32_t reg;
101 
102 	/* Calculate ncores value only once */
103 	if (ncores)
104 		return (ncores);
105 
106 	reg = cp15_l2ctlr_get();
107 	ncores = CPUV7_L2CTLR_NPROC(reg);
108 
109 	nchilds = ofw_cpu_early_foreach(alpine_validate_cpu, false);
110 
111 	/* Limit CPUs if DTS has configured less than available */
112 	if ((nchilds > 0) && (nchilds < ncores)) {
113 		printf("SMP: limiting number of active CPUs to %d out of %d\n",
114 		    nchilds, ncores);
115 		ncores = nchilds;
116 	}
117 
118 	return (ncores);
119 }
120 
121 void
122 platform_mp_init_secondary(void)
123 {
124 
125 	intr_pic_init_secondary();
126 }
127 
128 void
129 platform_mp_setmaxid(void)
130 {
131 
132 	mp_ncpus = platform_mp_get_core_cnt();
133 	mp_maxid = mp_ncpus - 1;
134 }
135 
136 int
137 platform_mp_probe(void)
138 {
139 	return (1);
140 }
141 
142 static int
143 alpine_get_cpu_resume_base(u_long *pbase, u_long *psize)
144 {
145 	phandle_t node;
146 	u_long base = 0;
147 	u_long size = 0;
148 
149 	if (pbase == NULL || psize == NULL)
150 		return (EINVAL);
151 
152 	if ((node = OF_finddevice("/")) == -1)
153 		return (EFAULT);
154 
155 	if ((node =
156 	    ofw_bus_find_compatible(node, "annapurna-labs,al-cpu-resume")) == 0)
157 		return (EFAULT);
158 
159 	if (fdt_regsize(node, &base, &size))
160 		return (EFAULT);
161 
162 	*pbase = base;
163 	*psize = size;
164 
165 	return (0);
166 }
167 
168 static int
169 alpine_get_nb_base(u_long *pbase, u_long *psize)
170 {
171 	phandle_t node;
172 	u_long base = 0;
173 	u_long size = 0;
174 
175 	if (pbase == NULL || psize == NULL)
176 		return (EINVAL);
177 
178 	if ((node = OF_finddevice("/")) == -1)
179 		return (EFAULT);
180 
181 	if ((node =
182 	    ofw_bus_find_compatible(node, "annapurna-labs,al-nb-service")) == 0)
183 		return (EFAULT);
184 
185 	if (fdt_regsize(node, &base, &size))
186 		return (EFAULT);
187 
188 	*pbase = base;
189 	*psize = size;
190 
191 	return (0);
192 }
193 
194 void
195 platform_mp_start_ap(void)
196 {
197 	uint32_t physaddr;
198 	vm_offset_t vaddr;
199 	uint32_t val;
200 	uint32_t start_mask;
201 	u_long cpu_resume_base;
202 	u_long nb_base;
203 	u_long cpu_resume_size;
204 	u_long nb_size;
205 	bus_addr_t cpu_resume_baddr;
206 	bus_addr_t nb_baddr;
207 	int a;
208 
209 	if (alpine_get_cpu_resume_base(&cpu_resume_base, &cpu_resume_size))
210 		panic("Couldn't resolve cpu_resume_base address\n");
211 
212 	if (alpine_get_nb_base(&nb_base, &nb_size))
213 		panic("Couldn't resolve_nb_base address\n");
214 
215 	/* Proceed with start addresses for additional CPUs */
216 	if (bus_space_map(fdtbus_bs_tag, al_devmap_pa + cpu_resume_base,
217 	    cpu_resume_size, 0, &cpu_resume_baddr))
218 		panic("Couldn't map CPU-resume area");
219 	if (bus_space_map(fdtbus_bs_tag, al_devmap_pa + nb_base,
220 	    nb_size, 0, &nb_baddr))
221 		panic("Couldn't map NB-service area");
222 
223 	/* Proceed with start addresses for additional CPUs */
224 	val = bus_space_read_4(fdtbus_bs_tag, cpu_resume_baddr,
225 	    AL_CPU_RESUME_WATERMARK_REG);
226 	if (((val & AL_CPU_RESUME_MAGIC_NUM_MASK) != AL_CPU_RESUME_MAGIC_NUM) ||
227 	    ((val & AL_CPU_RESUME_MIN_VER_MASK) < AL_CPU_RESUME_MIN_VER)) {
228 		panic("CPU-resume device is not compatible");
229 	}
230 
231 	vaddr = (vm_offset_t)mpentry;
232 	physaddr = pmap_kextract(vaddr);
233 
234 	for (a = 1; a < platform_mp_get_core_cnt(); a++) {
235 		/* Power up the core */
236 		bus_space_write_4(fdtbus_bs_tag, nb_baddr,
237 		    AL_NB_CONFIG_STATUS_PWR_CTRL(a), 0);
238 		mb();
239 
240 		/* Enable resume */
241 		val = bus_space_read_4(fdtbus_bs_tag, cpu_resume_baddr,
242 		    AL_CPU_RESUME_PCPU_FLAGS(a));
243 		val &= ~AL_CPU_RESUME_FLG_PERCPU_DONT_RESUME;
244 		bus_space_write_4(fdtbus_bs_tag, cpu_resume_baddr,
245 		    AL_CPU_RESUME_PCPU_FLAGS(a), val);
246 		mb();
247 
248 		/* Set resume physical address */
249 		bus_space_write_4(fdtbus_bs_tag, cpu_resume_baddr,
250 		    AL_CPU_RESUME_PCPU_RADDR_REG(a), physaddr);
251 		mb();
252 	}
253 
254 	/* Release cores from reset */
255 	if (bus_space_map(fdtbus_bs_tag, al_devmap_pa + nb_base,
256 	    nb_size, 0, &nb_baddr))
257 		panic("Couldn't map NB-service area");
258 
259 	start_mask = (1 << platform_mp_get_core_cnt()) - 1;
260 
261 	/* Release cores from reset */
262 	val = bus_space_read_4(fdtbus_bs_tag, nb_baddr, AL_NB_INIT_CONTROL);
263 	val |= start_mask;
264 	bus_space_write_4(fdtbus_bs_tag, nb_baddr, AL_NB_INIT_CONTROL, val);
265 	dsb();
266 
267 	bus_space_unmap(fdtbus_bs_tag, nb_baddr, nb_size);
268 	bus_space_unmap(fdtbus_bs_tag, cpu_resume_baddr, cpu_resume_size);
269 }
270 
271 static int
272 alpine_get_serdes_base(u_long *pbase, u_long *psize)
273 {
274 	phandle_t node;
275 	u_long base = 0;
276 	u_long size = 0;
277 
278 	if (pbase == NULL || psize == NULL)
279 		return (EINVAL);
280 
281 	if ((node = OF_finddevice("/")) == -1)
282 		return (EFAULT);
283 
284 	if ((node =
285 	    ofw_bus_find_compatible(node, "annapurna-labs,al-serdes")) == 0)
286 		return (EFAULT);
287 
288 	if (fdt_regsize(node, &base, &size))
289 		return (EFAULT);
290 
291 	*pbase = base;
292 	*psize = size;
293 
294 	return (0);
295 }
296 
297 int
298 alpine_serdes_resource_get(uint32_t group, bus_space_tag_t *tag, bus_addr_t *baddr)
299 {
300 	u_long serdes_base, serdes_size;
301 	int ret;
302 	static bus_addr_t baddr_mapped[SERDES_NUM_GROUPS];
303 
304 	if (group >= SERDES_NUM_GROUPS)
305 		return (EINVAL);
306 
307 	if (baddr_mapped[group]) {
308 		*tag = fdtbus_bs_tag;
309 		*baddr = baddr_mapped[group];
310 		return (0);
311 	}
312 
313 	ret = alpine_get_serdes_base(&serdes_base, &serdes_size);
314 	if (ret)
315 		return (ret);
316 
317 	ret = bus_space_map(fdtbus_bs_tag,
318 	    al_devmap_pa + serdes_base + group * SERDES_GROUP_SIZE,
319 	    (SERDES_NUM_GROUPS - group) * SERDES_GROUP_SIZE, 0, baddr);
320 	if (ret)
321 		return (ret);
322 
323 	baddr_mapped[group] = *baddr;
324 
325 	return (0);
326 }
327 
328 void
329 platform_ipi_send(cpuset_t cpus, u_int ipi)
330 {
331 
332 	pic_ipi_send(cpus, ipi);
333 }
334