2 * Copyright (c) 2017 Ruslan Bukin <br@bsdpad.com>
3 * Copyright (c) 2012-2015 Robert N. M. Watson
4 * Copyright (c) 2013 SRI International
7 * This software was developed by SRI International and the University of
8 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
9 * ("CTSRD"), as part of the DARPA CRASH research programme.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
40 #include <machine/hwfunc.h>
41 #include <machine/smp.h>
43 #include <mips/beri/beri_mp.h>
45 #include <dev/fdt/fdt_common.h>
55 static phandle_t cpu_of_nodes[MAXCPU];
56 static device_t picmap[MAXCPU];
59 platform_processor_id(void)
69 platform_cpu_mask(cpuset_t *mask)
71 int ncores, ncpus, nthreads;
75 struct spin_entry *se;
77 ncores = beri_get_ncores();
78 nthreads = beri_get_nthreads();
79 KASSERT(ncores <= 0x10000, ("%s: too many cores %d", __func__, ncores));
80 KASSERT(nthreads <= 0x10000, ("%s: too many threads %d", __func__,
82 KASSERT(ncores < 0xffff || nthreads < 0xffff,
83 ("%s: cores x thread (%d x %d) would overflow", __func__, ncores,
85 ncpus = ncores * nthreads;
86 if (MAXCPU > 1 && ncpus > MAXCPU)
87 printf("%s: Hardware supports more CPUs (%d) than kernel (%d)\n",
88 __func__, ncpus, MAXCPU);
89 printf("%s: hardware has %d cores with %d threads each\n", __func__,
92 if ((cpus = OF_finddevice("/cpus")) <= 0) {
93 printf("%s: no \"/cpus\" device found in FDT\n", __func__);
96 if ((cpu = OF_child(cpus)) <= 0) {
97 printf("%s: no children of \"/cpus\" found in FDT\n", __func__);
102 if (OF_getprop(cpu, "reg", ®, sizeof(reg)) <= 0) {
103 printf("%s: cpu device with no reg property\n",
108 printf("%s: cpu ID too large (%d > %d)\n", __func__,
112 cpu_of_nodes[reg] = cpu;
115 if (OF_getprop(cpu, "enable-method", &prop,
116 sizeof(prop)) <= 0 && OF_getprop(OF_parent(cpu),
117 "enable-method", &prop, sizeof(prop)) <= 0) {
118 printf("%s: CPU %d has no enable-method "
119 "property\n", __func__, reg);
122 if (strcmp("spin-table", prop) != 0) {
123 printf("%s: CPU %d enable-method is '%s' not "
124 "'spin-table'\n", __func__, reg, prop);
128 if (OF_getprop(cpu, "cpu-release-addr", &se,
130 printf("%s: CPU %d has missing or invalid "
131 "cpu-release-addr\n", __func__, reg);
134 if (se->entry_addr != 1) {
135 printf("%s: CPU %d has uninitialized spin "
136 "entry\n", __func__, reg);
142 } while ((cpu = OF_peer(cpu)) > 0);
147 * If we run into any problems determining the CPU layout,
150 * XXX: panic instead?
157 platform_init_secondary(int cpuid)
162 ipi = platform_ipi_hardintr_num();
164 ic = devclass_get_device(devclass_find("beripic"), cpuid);
166 beripic_setup_ipi(ic, cpuid, ipi);
168 /* Unmask the interrupt */
170 mips_wr_status(mips_rd_status() | (((1 << ipi) << 8) << 2));
175 platform_ipi_send(int cpuid)
178 mips_sync(); /* Ordering, liveness. */
180 beripic_send_ipi(picmap[cpuid], cpuid);
184 platform_ipi_clear(void)
188 cpuid = platform_processor_id();
190 beripic_clear_ipi(picmap[cpuid], cpuid);
194 * XXXBED: Set via FDT?
197 platform_ipi_hardintr_num(void)
204 platform_ipi_softintr_num(void)
211 * XXXBED: Fine for MT, will need something better for multi-core.
214 platform_smp_topo(void)
217 return (smp_topo_none());
221 platform_init_ap(int cpuid)
224 u_int clock_int_mask;
226 KASSERT(cpuid < MAXCPU, ("%s: invalid CPU id %d", __func__, cpuid));
228 /* Make sure coprocessors are enabled. */
229 status = mips_rd_status();
230 status |= (MIPS_SR_COP_0_BIT | MIPS_SR_COP_1_BIT);
231 #if defined(CPU_CHERI)
232 status |= MIPS_SR_COP_2_BIT;
234 mips_wr_status(status);
238 /* Enable HDWRD instruction in userspace. Also enables statcounters. */
239 hwrena = mips_rd_hwrena();
240 hwrena |= (MIPS_HWRENA_CC | MIPS_HWRENA_CCRES | MIPS_HWRENA_CPUNUM |
241 MIPS_HWRENA_BERI_STATCOUNTERS_MASK);
242 mips_wr_hwrena(hwrena);
246 * Enable per-thread timer.
248 clock_int_mask = hard_int_mask(5);
249 set_intr_mask(clock_int_mask);
253 * BERI startup conforms to the spin-table start method defined in the
254 * ePAPR 1.0 spec. The initial spin waiting for an address is started
255 * by the CPU firmware.
258 platform_start_ap(int cpuid)
262 struct spin_entry *se;
264 KASSERT(cpuid != 0, ("%s: can't start CPU 0!\n", __func__));
265 KASSERT((cpuid > 0 && cpuid < MAXCPU),
266 ("%s: invalid CPU id %d", __func__, cpuid));
268 cpu = cpu_of_nodes[cpuid];
269 if (OF_getprop(cpu, "status", &prop, sizeof(prop)) <= 0) {
271 printf("%s: CPU %d has no status property, "
272 "trying parent\n", __func__, cpuid);
273 if (OF_getprop(OF_parent(cpu), "status", &prop,
275 panic("%s: CPU %d has no status property", __func__,
278 if (strcmp("disabled", prop) != 0)
279 panic("%s: CPU %d status is '%s' not 'disabled'",
280 __func__, cpuid, prop);
282 if (OF_getprop(cpu, "enable-method", &prop, sizeof(prop)) <= 0) {
284 printf("%s: CPU %d has no enable-method, "
285 "trying parent\n", __func__, cpuid);
286 if (OF_getprop(OF_parent(cpu), "enable-method", &prop,
288 panic("%s: CPU %d has no enable-method property",
291 if (strcmp("spin-table", prop) != 0)
292 panic("%s: CPU %d enable-method is '%s' not "
293 "'spin-table'", __func__, cpuid, prop);
295 if (OF_getprop(cpu, "cpu-release-addr", &se, sizeof(se)) <= 0)
296 panic("%s: CPU %d has missing or invalid cpu-release-addr",
300 printf("%s: writing %p to %p\n", __func__, mpentry,
303 mips_sync(); /* Ordering. */
304 se->entry_addr = (intptr_t)mpentry;
305 mips_sync(); /* Liveness. */