source: mainline/kernel/arch/sparc64/src/smp/sun4v/smp.c@ 40043e8

Last change on this file since 40043e8 was 40043e8, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

Restructering halt/reboot files

The function halt() and reboot() used to be stored
in two different c files and two different header
files which were named differently than the c files.
This commit merges those two functions into the
same file and provides an unique header file (shutdown.h)
for accessing them.

  • Property mode set to 100644
File size: 12.5 KB
RevLine 
[95c4776]1/*
2 * Copyright (c) 2006 Jakub Jermar
[5ea37b1]3 * Copyright (c) 2009 Pavel Rimsky
[95c4776]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 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
[c5429fe]30/** @addtogroup kernel_sparc64
[95c4776]31 * @{
32 */
33/** @file
34 */
35
36#include <smp/smp.h>
37#include <smp/ipi.h>
38#include <genarch/ofw/ofw_tree.h>
39#include <cpu.h>
40#include <arch/cpu.h>
41#include <arch/boot/boot.h>
42#include <arch.h>
43#include <config.h>
44#include <macros.h>
[83dab11]45#include <stdbool.h>
46#include <stddef.h>
47#include <stdint.h>
[95c4776]48#include <synch/waitq.h>
[bab75df6]49#include <stdio.h>
[95c4776]50#include <arch/sun4v/hypercall.h>
51#include <arch/sun4v/md.h>
52#include <arch/sun4v/ipi.h>
53#include <time/delay.h>
54#include <arch/smp/sun4v/smp.h>
[19f857a]55#include <str.h>
[5cbb170]56#include <errno.h>
[95c4776]57
58/** hypervisor code of the "running" state of the CPU */
59#define CPU_STATE_RUNNING 2
60
61/** maximum possible number of processor cores */
62#define MAX_NUM_CORES 8
63
64/** needed in the CPU_START hypercall */
65extern void kernel_image_start(void);
66
67/** needed in the CPU_START hypercall */
68extern void *trap_table;
69
70/** number of execution units detected */
71uint8_t exec_unit_count = 0;
72
73/** execution units (processor cores) */
74exec_unit_t exec_units[MAX_NUM_CORES];
75
76/** CPU structures */
77extern cpu_t *cpus;
78
79/** maximum number of strands per a physical core detected */
80unsigned int max_core_strands = 0;
81
82#if 0
83/**
84 * Proposes the optimal number of ready threads for each virtual processor
85 * in the given processor core so that the processor core is as busy as the
86 * average processor core. The proposed number of ready threads will be
87 * stored to the proposed_nrdy variable of the cpu_arch_t struture.
88 */
[1433ecda]89bool calculate_optimal_nrdy(exec_unit_t *exec_unit)
90{
[95c4776]91
92 /* calculate the number of threads the core will steal */
[036e97c]93 int avg = atomic_load(&nrdy) / exec_unit_count;
94 int to_steal = avg - atomic_load(&(exec_units->nrdy));
[95c4776]95 if (to_steal < 0) {
96 return true;
97 } else if (to_steal == 0) {
98 return false;
99 }
100
101 /* initialize the proposals with the real numbers of ready threads */
102 unsigned int k;
103 for (k = 0; k < exec_unit->strand_count; k++) {
104 exec_units->cpus[k]->arch.proposed_nrdy =
[036e97c]105 atomic_load(&(exec_unit->cpus[k]->nrdy));
[95c4776]106 }
107
108 /* distribute the threads to be stolen to the core's CPUs */
109 int j;
110 for (j = to_steal; j > 0; j--) {
111 unsigned int k;
112 unsigned int least_busy = 0;
113 unsigned int least_busy_nrdy =
[1433ecda]114 exec_unit->cpus[0]->arch.proposed_nrdy;
[95c4776]115
116 /* for each stolen thread, give it to the least busy CPU */
117 for (k = 0; k < exec_unit->strand_count; k++) {
[1433ecda]118 if (exec_unit->cpus[k]->arch.proposed_nrdy < least_busy_nrdy) {
[95c4776]119 least_busy = k;
120 least_busy_nrdy =
[1433ecda]121 exec_unit->cpus[k]->arch.proposed_nrdy;
[95c4776]122 }
123 }
124 exec_unit->cpus[least_busy]->arch.proposed_nrdy++;
125 }
126
127 return false;
128}
129#endif
130
131/**
132 * Finds out which execution units belong to particular CPUs. By execution unit
133 * we mean the physical core the logical processor is backed by. Since each
134 * Niagara physical core has just one integer execution unit and we will
135 * ignore other execution units than the integer ones, we will use the terms
136 * "integer execution unit", "execution unit" and "physical core"
137 * interchangeably.
138 *
139 * The physical cores are detected by browsing the children of the CPU node
140 * in the machine description and looking for a node representing an integer
141 * execution unit. Once the integer execution unit of a particular CPU is
142 * known, the ID of the CPU is added to the list of cpuids of the corresponding
143 * execution unit structure (exec_unit_t). If an execution unit is encountered
144 * for the first time, a new execution unit structure (exec_unit_t) must be
145 * created first and added to the execution units array (exec_units).
146 *
147 * If the function fails to find an execution unit for a CPU (this may happen
148 * on machines with older firmware or on Simics), it performs a fallback code
149 * which pretends there exists just one execution unit and all CPUs belong to
150 * it.
151 *
152 * Finally, the array of all execution units is reordered such that its element
153 * which represents the physical core of the the bootstrap CPU is at index 0.
154 * Moreover, the array of CPU IDs within the BSP's physical core structure is
155 * reordered such that the element which represents the ID of the BSP is at
156 * index 0. This is done because we would like the CPUs to be woken up
157 * such that the 0-index CPU of the 0-index execution unit is
158 * woken up first. And since the BSP is already woken up, we would like it to be
159 * at 0-th position of the 0-th execution unit structure.
160 *
161 * Apart from that, the code also counts the total number of CPUs and stores
162 * it to the global config.cpu_count variable.
163 */
164static void detect_execution_units(void)
165{
166 /* ID of the bootstrap processor */
167 uint64_t myid;
168
169 /* total number of CPUs detected */
170 size_t cpu_count = 0;
171
172 /* will be set to 1 if detecting the physical cores fails */
173 bool exec_unit_assign_error = 0;
174
175 /* index of the bootstrap physical core in the array of cores */
176 unsigned int bsp_exec_unit_index = 0;
177
178 /* index of the BSP ID inside the array of bootstrap core's cpuids */
179 unsigned int bsp_core_strand_index = 0;
180
181 __hypercall_fast_ret1(0, 0, 0, 0, 0, CPU_MYID, &myid);
182 md_node_t node = md_get_root();
183
184 /* walk through all the CPU nodes in the MD*/
185 while (md_next_node(&node, "cpu")) {
186
187 uint64_t cpuid;
188 md_get_integer_property(node, "id", &cpuid);
189 cpu_count++;
190
191 /*
[7c3fb9b]192 * if failed in previous CPUs, don't try
193 * to detect physical cores any more
194 */
[95c4776]195 if (exec_unit_assign_error)
196 continue;
197
198 /* detect exec. unit for the CPU represented by current node */
199 uint64_t exec_unit_id = 0;
200 md_child_iter_t it = md_get_child_iterator(node);
201
202 while (md_next_child(&it)) {
203 md_node_t child = md_get_child_node(it);
[566da7f8]204 const char *exec_unit_type = "";
[95c4776]205 md_get_string_property(child, "type", &exec_unit_type);
206
207 /* each physical core has just 1 integer exec. unit */
208 if (str_cmp(exec_unit_type, "integer") == 0) {
209 exec_unit_id = child;
210 break;
211 }
212 }
213
214 /* execution unit detected successfully */
215 if (exec_unit_id != 0) {
216
217 /* find the exec. unit in array of existing units */
218 unsigned int i = 0;
219 for (i = 0; i < exec_unit_count; i++) {
220 if (exec_units[i].exec_unit_id == exec_unit_id)
221 break;
222 }
223
224 /*
225 * execution unit just met has not been met before, so
226 * create a new entry in array of all execution units
227 */
228 if (i == exec_unit_count) {
229 exec_units[i].exec_unit_id = exec_unit_id;
230 exec_units[i].strand_count = 0;
[e3306d04]231 atomic_store(&(exec_units[i].nrdy), 0);
[ba7371f9]232 spinlock_initialize(&(exec_units[i].proposed_nrdy_lock), "exec_units[].proposed_nrdy_lock");
[95c4776]233 exec_unit_count++;
234 }
235
236 /*
237 * remember the exec. unit and strand of the BSP
238 */
239 if (cpuid == myid) {
240 bsp_exec_unit_index = i;
241 bsp_core_strand_index = exec_units[i].strand_count;
242 }
243
244 /* add the CPU just met to the exec. unit's list */
245 exec_units[i].cpuids[exec_units[i].strand_count] = cpuid;
246 exec_units[i].strand_count++;
247 max_core_strands =
[1433ecda]248 exec_units[i].strand_count > max_core_strands ?
249 exec_units[i].strand_count : max_core_strands;
[95c4776]250
[7c3fb9b]251 /* detecting execution unit failed */
[95c4776]252 } else {
253 exec_unit_assign_error = 1;
254 }
[1b20da0]255 }
[95c4776]256
257 /* save the number of CPUs to a globally accessible variable */
258 config.cpu_count = cpu_count;
259
260 /*
[7c3fb9b]261 * A fallback code which will be executed if finding out which
262 * execution units belong to particular CPUs fails. Pretend there
263 * exists just one execution unit and all CPUs belong to it.
264 */
[95c4776]265 if (exec_unit_assign_error) {
266 bsp_exec_unit_index = 0;
267 exec_unit_count = 1;
268 exec_units[0].strand_count = cpu_count;
269 exec_units[0].exec_unit_id = 1;
[ba7371f9]270 spinlock_initialize(&(exec_units[0].proposed_nrdy_lock), "exec_units[0].proposed_nrdy_lock");
[e3306d04]271 atomic_store(&(exec_units[0].nrdy), 0);
[95c4776]272 max_core_strands = cpu_count;
273
274 /* browse CPUs again, assign them the fictional exec. unit */
275 node = md_get_root();
276 unsigned int i = 0;
277
278 while (md_next_node(&node, "cpu")) {
279 uint64_t cpuid;
280 md_get_integer_property(node, "id", &cpuid);
281 if (cpuid == myid) {
282 bsp_core_strand_index = i;
283 }
284 exec_units[0].cpuids[i++] = cpuid;
285 }
286 }
287
288 /*
[7c3fb9b]289 * Reorder the execution units array elements and the cpuid array
290 * elements so that the BSP will always be the very first CPU of
291 * the very first execution unit.
292 */
[95c4776]293 exec_unit_t temp_exec_unit = exec_units[0];
294 exec_units[0] = exec_units[bsp_exec_unit_index];
295 exec_units[bsp_exec_unit_index] = temp_exec_unit;
296
297 uint64_t temp_cpuid = exec_units[0].cpuids[0];
298 exec_units[0].cpuids[0] = exec_units[0].cpuids[bsp_exec_unit_index];
299 exec_units[0].cpuids[bsp_core_strand_index] = temp_cpuid;
300
301}
302
303/**
304 * Determine number of processors and detect physical cores. On Simics
305 * copy the code which will be executed by the AP when the BSP sends an
306 * IPI to it in order to make it execute HelenOS code.
307 */
308void smp_init(void)
309{
310 detect_execution_units();
311}
312
313/**
314 * For each CPU sets the value of cpus[i].arch.id, where i is the
315 * index of the CPU in the cpus variable, to the cpuid of the i-th processor
316 * to be run. The CPUs are run such that the CPU represented by cpus[0]
317 * is run first, cpus[1] is run after it, and cpus[cpu_count - 1] is run as the
318 * last one.
319 *
320 * The CPU IDs are set such that during waking the CPUs up the
321 * processor cores will be alternated, i.e. first one CPU from the first core
322 * will be run, after that one CPU from the second CPU core will be run,...
323 * then one CPU from the last core will be run, after that another CPU
324 * from the first core will be run, then another CPU from the second core
325 * will be run,... then another CPU from the last core will be run, and so on.
326 */
327static void init_cpuids(void)
328{
329 unsigned int cur_core_strand;
330 unsigned int cur_core;
331 unsigned int cur_cpu = 0;
332
333 for (cur_core_strand = 0; cur_core_strand < max_core_strands; cur_core_strand++) {
334 for (cur_core = 0; cur_core < exec_unit_count; cur_core++) {
335 if (cur_core_strand > exec_units[cur_core].strand_count)
336 continue;
337
338 cpus[cur_cpu].arch.exec_unit = &(exec_units[cur_core]);
[036e97c]339 atomic_add(&(exec_units[cur_core].nrdy), atomic_load(&(cpus[cur_cpu].nrdy)));
[95c4776]340 cpus[cur_cpu].arch.id = exec_units[cur_core].cpuids[cur_core_strand];
341 exec_units[cur_core].cpus[cur_core_strand] = &(cpus[cur_cpu]);
342 cur_cpu++;
343 }
344 }
345}
346
347/**
348 * Wakes up a single CPU.
349 *
350 * @param cpuid ID of the CPU to be woken up
351 */
352static bool wake_cpu(uint64_t cpuid)
353{
354#ifdef CONFIG_SIMICS_SMP_HACK
355 ipi_unicast_to((void (*)(void)) 1234, cpuid);
356#else
357 /* stop the CPU before making it execute our code */
358 if (__hypercall_fast1(CPU_STOP, cpuid) != EOK)
359 return false;
[a35b458]360
[95c4776]361 /* wait for the CPU to stop */
362 uint64_t state;
[4872160]363 __hypercall_fast_ret1(cpuid, 0, 0, 0, 0, CPU_STATE, &state);
364 while (state == CPU_STATE_RUNNING)
365 __hypercall_fast_ret1(cpuid, 0, 0, 0, 0, CPU_STATE, &state);
[a35b458]366
[95c4776]367 /* make the CPU run again and execute HelenOS code */
[4872160]368 if (__hypercall_fast4(CPU_START, cpuid,
369 (uint64_t) KA2PA(kernel_image_start), KA2PA(trap_table),
[b97b348]370 physmem_base) != EOK)
[4872160]371 return false;
[95c4776]372#endif
[a35b458]373
[897fd8f1]374 if (waitq_sleep_timeout(&ap_completion_wq, 10000000,
375 SYNCH_FLAGS_NONE, NULL) == ETIMEOUT)
[5ea37b1]376 printf("%s: waiting for processor (cpuid = %" PRIu64 ") timed out\n",
[4872160]377 __func__, cpuid);
[a35b458]378
[95c4776]379 return true;
380}
381
382/** Wake application processors up. */
383void kmp(void *arg)
384{
385 init_cpuids();
386
387 unsigned int i;
388
389 for (i = 1; i < config.cpu_count; i++) {
390 wake_cpu(cpus[i].arch.id);
391 }
392}
393
394/** @}
395 */
Note: See TracBrowser for help on using the repository browser.