]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/mips/beri/beri_mp.c
Update edk2 headers to stable202005
[FreeBSD/FreeBSD.git] / sys / mips / beri / beri_mp.c
1 /*-
2  * Copyright (c) 2017 Ruslan Bukin <br@bsdpad.com>
3  * Copyright (c) 2012-2015 Robert N. M. Watson
4  * Copyright (c) 2013 SRI International
5  * All rights reserved.
6  *
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.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/smp.h>
39
40 #include <machine/hwfunc.h>
41 #include <machine/smp.h>
42
43 #include <mips/beri/beri_mp.h>
44
45 #include <dev/fdt/fdt_common.h>
46
47 struct spin_entry {
48         uint64_t entry_addr;
49         uint64_t a0;
50         uint32_t rsvd1;
51         uint32_t pir;
52         uint64_t rsvd2;
53 };
54
55 static phandle_t cpu_of_nodes[MAXCPU];
56 static device_t picmap[MAXCPU];
57
58 int
59 platform_processor_id(void)
60 {
61         int cpu;
62
63         cpu = beri_get_cpu();
64
65         return (cpu);
66 }
67
68 void
69 platform_cpu_mask(cpuset_t *mask)
70 {
71         int ncores, ncpus, nthreads;
72         phandle_t cpus, cpu;
73         pcell_t reg;
74         char prop[16];
75         struct spin_entry *se;
76
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__,
81             nthreads));
82         KASSERT(ncores < 0xffff || nthreads < 0xffff,
83             ("%s: cores x thread (%d x %d) would overflow", __func__, ncores,
84             nthreads));
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__,
90             ncores, nthreads);
91
92         if ((cpus = OF_finddevice("/cpus")) <= 0) {
93                 printf("%s: no \"/cpus\" device found in FDT\n", __func__);
94                 goto error;
95         }
96         if ((cpu = OF_child(cpus)) <= 0) {
97                 printf("%s: no children of \"/cpus\" found in FDT\n", __func__);
98                 goto error;
99         }
100         CPU_ZERO(mask);
101         do {
102                 if (OF_getprop(cpu, "reg", &reg, sizeof(reg)) <= 0) {
103                         printf("%s: cpu device with no reg property\n",
104                             __func__);
105                         goto error;
106                 }
107                 if (reg > MAXCPU) {
108                         printf("%s: cpu ID too large (%d > %d)\n", __func__,
109                             reg, MAXCPU);
110                         continue;
111                 }
112                 cpu_of_nodes[reg] = cpu;
113
114                 if (reg != 0) {
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);
120                                 continue;
121                         }
122                         if (strcmp("spin-table", prop) != 0) {
123                                 printf("%s: CPU %d enable-method is '%s' not "
124                                     "'spin-table'\n", __func__, reg, prop);
125                                 continue;
126                         }
127
128                         if (OF_getprop(cpu, "cpu-release-addr", &se,
129                             sizeof(se)) <= 0) {
130                                 printf("%s: CPU %d has missing or invalid "
131                                     "cpu-release-addr\n", __func__, reg);
132                                 continue;
133                         }
134                         if (se->entry_addr != 1) {
135                                 printf("%s: CPU %d has uninitialized spin "
136                                     "entry\n", __func__, reg);
137                                 continue;
138                         }
139                 }
140
141                 CPU_SET(reg, mask);
142         } while ((cpu = OF_peer(cpu)) > 0);
143         return;
144
145 error:
146         /*
147          * If we run into any problems determining the CPU layout,
148          * fall back to UP.
149          *
150          * XXX: panic instead?
151          */
152         CPU_ZERO(mask);
153         CPU_SET(0, mask);
154 }
155
156 void
157 platform_init_secondary(int cpuid)
158 {
159         device_t ic;
160         int ipi;
161
162         ipi = platform_ipi_hardintr_num();
163
164         ic = devclass_get_device(devclass_find("beripic"), cpuid);
165         picmap[cpuid] = ic;
166         beripic_setup_ipi(ic, cpuid, ipi);
167
168         /* Unmask the interrupt */
169         if (cpuid != 0) {
170                 mips_wr_status(mips_rd_status() | (((1 << ipi) << 8) << 2));
171         }
172 }
173
174
175 void
176 platform_ipi_send(int cpuid)
177 {
178
179         mips_sync();    /* Ordering, liveness. */
180
181         beripic_send_ipi(picmap[cpuid], cpuid);
182 }
183
184 void
185 platform_ipi_clear(void)
186 {
187         int cpuid;
188
189         cpuid = platform_processor_id();
190
191         beripic_clear_ipi(picmap[cpuid], cpuid);
192 }
193
194 /*
195  * XXXBED: Set via FDT?
196  */
197 int
198 platform_ipi_hardintr_num(void)
199 {
200
201         return (4);
202 }
203
204 int
205 platform_ipi_softintr_num(void)
206 {
207
208         return (-1);
209 }
210
211 /*
212  * XXXBED: Fine for MT, will need something better for multi-core.
213  */
214 struct cpu_group *
215 platform_smp_topo(void)
216 {
217
218         return (smp_topo_none());
219 }
220
221 void
222 platform_init_ap(int cpuid)
223 {
224         uint32_t status;
225         u_int clock_int_mask;
226
227         KASSERT(cpuid < MAXCPU, ("%s: invalid CPU id %d", __func__, cpuid));
228
229         /* Make sure coprocessors are enabled. */
230         status = mips_rd_status();
231         status |= (MIPS_SR_COP_0_BIT | MIPS_SR_COP_1_BIT);
232 #if defined(CPU_CHERI)
233         status |= MIPS_SR_COP_2_BIT;
234 #endif
235         mips_wr_status(status);
236
237 #if 0
238         register_t hwrena;
239         /* Enable HDWRD instruction in userspace. Also enables statcounters. */
240         hwrena = mips_rd_hwrena();
241         hwrena |= (MIPS_HWRENA_CC | MIPS_HWRENA_CCRES | MIPS_HWRENA_CPUNUM |
242             MIPS_HWRENA_BERI_STATCOUNTERS_MASK);
243         mips_wr_hwrena(hwrena);
244 #endif
245
246         /*
247          * Enable per-thread timer.
248          */
249         clock_int_mask = hard_int_mask(5);
250         set_intr_mask(clock_int_mask);
251 }
252
253 /*
254  * BERI startup conforms to the spin-table start method defined in the
255  * ePAPR 1.0 spec.  The initial spin waiting for an address is started
256  * by the CPU firmware.
257  */
258 int
259 platform_start_ap(int cpuid)
260 {
261         phandle_t cpu;
262         char prop[16];
263         struct spin_entry *se;
264
265         KASSERT(cpuid != 0, ("%s: can't start CPU 0!\n", __func__));
266         KASSERT((cpuid > 0 && cpuid < MAXCPU),
267             ("%s: invalid CPU id %d", __func__, cpuid));
268
269         cpu = cpu_of_nodes[cpuid];
270         if (OF_getprop(cpu, "status", &prop, sizeof(prop)) <= 0) {
271                 if (bootverbose)
272                         printf("%s: CPU %d has no status property, "
273                             "trying parent\n", __func__, cpuid);
274                 if (OF_getprop(OF_parent(cpu), "status", &prop,
275                     sizeof(prop)) <= 0)
276                         panic("%s: CPU %d has no status property", __func__,
277                             cpuid);
278         }
279         if (strcmp("disabled", prop) != 0)
280                 panic("%s: CPU %d status is '%s' not 'disabled'",
281                     __func__, cpuid, prop);
282
283         if (OF_getprop(cpu, "enable-method", &prop, sizeof(prop)) <= 0) {
284                 if (bootverbose)
285                         printf("%s: CPU %d has no enable-method, "
286                             "trying parent\n", __func__, cpuid);
287                 if (OF_getprop(OF_parent(cpu), "enable-method", &prop,
288                     sizeof(prop)) <= 0)
289                         panic("%s: CPU %d has no enable-method property",
290                             __func__, cpuid);
291         }
292         if (strcmp("spin-table", prop) != 0)
293                 panic("%s: CPU %d enable-method is '%s' not "
294                     "'spin-table'", __func__, cpuid, prop);
295
296         if (OF_getprop(cpu, "cpu-release-addr", &se, sizeof(se)) <= 0)
297                 panic("%s: CPU %d has missing or invalid cpu-release-addr",
298                     __func__, cpuid);
299         se->pir = cpuid;
300         if (bootverbose)
301                 printf("%s: writing %p to %p\n", __func__, mpentry,
302                     &se->entry_addr);
303
304         mips_sync();    /* Ordering. */
305         se->entry_addr = (intptr_t)mpentry;
306         mips_sync();    /* Liveness. */
307
308         return (0);
309 }