1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
|
/* IBM_PROLOG_BEGIN_TAG */
/* This is an automatically generated prolog. */
/* */
/* $Source: src/kernel/cpumgr.C $ */
/* */
/* OpenPOWER HostBoot Project */
/* */
/* Contributors Listed Below - COPYRIGHT 2010,2018 */
/* [+] International Business Machines Corp. */
/* */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); */
/* you may not use this file except in compliance with the License. */
/* You may obtain a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or */
/* implied. See the License for the specific language governing */
/* permissions and limitations under the License. */
/* */
/* IBM_PROLOG_END_TAG */
#include <assert.h>
#include <kernel/cpumgr.H>
#include <kernel/task.H>
#include <kernel/cpu.H>
#include <kernel/scheduler.H>
#include <kernel/taskmgr.H>
#include <kernel/pagemgr.H>
#include <kernel/console.H>
#include <util/singleton.H>
#include <arch/ppc.H>
#include <kernel/timemgr.H>
#include <sys/sync.h>
#include <kernel/cpuid.H>
#include <kernel/ptmgr.H>
#include <kernel/heapmgr.H>
#include <kernel/intmsghandler.H>
#include <errno.h>
#include <kernel/deferred.H>
#include <kernel/misc.H>
#include <kernel/terminate.H>
#include <kernel/hbterminatetypes.H>
#include <kernel/kernel_reasoncodes.H>
#include <kernel/cpuid.H>
#include <kernel/doorbell.H>
#include <arch/pvrformat.H>
cpu_t** CpuManager::cv_cpus[KERNEL_MAX_SUPPORTED_NODES];
bool CpuManager::cv_shutdown_requested = false;
uint64_t CpuManager::cv_shutdown_status = 0;
size_t CpuManager::cv_cpuSeq = 0;
uint8_t CpuManager::cv_forcedMemPeriodic = 0;
InteractiveDebug CpuManager::cv_interactive_debug;
CpuManager::CpuManager() : iv_lastStartTimebase(0)
{
for (int i = 0; i < KERNEL_MAX_SUPPORTED_NODES; i++)
cv_cpus[i] = NULL;
memset(&cv_interactive_debug, '\0', sizeof(cv_interactive_debug));
}
cpu_t* CpuManager::getMasterCPU()
{
for (int i = 0; i < KERNEL_MAX_SUPPORTED_NODES; i++)
{
if (NULL == cv_cpus[i])
{
continue;
}
for (int j = 0; j < KERNEL_MAX_SUPPORTED_CPUS_PER_NODE; j++)
{
if ((cv_cpus[i][j] != NULL) && (cv_cpus[i][j]->master))
{
return cv_cpus[i][j];
}
}
}
return NULL;
}
void CpuManager::init()
{
// For the initial boot we only want to set up CPU objects for the threads
// on this core. Otherwise we waste memory with kernel / idle task stacks.
//
// As long as the CPU object pointer is NULL, the start.S code won't
// enter the kernel, so we skip initializing all the other CPUs for now.
// Determine number of threads on this core.
size_t threads = getThreadCount();
// Set up CPU structure.
cv_cpus[getPIR() / KERNEL_MAX_SUPPORTED_CPUS_PER_NODE] =
new cpu_t*[KERNEL_MAX_SUPPORTED_CPUS_PER_NODE]();
// Create CPU objects starting at the thread-0 for this core.
size_t baseCpu = getCpuId() & ~(threads-1);
for (size_t i = 0; i < threads; i++)
Singleton<CpuManager>::instance().startCPU(i + baseCpu);
}
void CpuManager::init_slave_smp(cpu_t* cpu)
{
Singleton<CpuManager>::instance().startSlaveCPU(cpu);
}
void CpuManager::requestShutdown(uint64_t i_status, uint32_t i_error_data)
{
cv_shutdown_status = i_status;
__sync_synchronize();
cv_shutdown_requested = true;
// If the shutdown was not called with a Good shutdown status
// then we know we are shutting down due to error. We need to
// figure out if the error provided is a PLID or reasoncode
// and write it appropriately.
// Hostboot PLIDs always start with 0x9 (32-bit)
static const uint64_t PLID_MASK = 0x0000000090000000;
if (i_status != SHUTDOWN_STATUS_GOOD)
{
if ((i_status & 0x00000000F0000000) == PLID_MASK)
{
termWritePlid(TI_SHUTDOWN, i_status);
}
else
{
termWriteSRC(TI_SHUTDOWN,i_status, 0, i_error_data);
}
printk("TI initiated on all threads (shutdown)\n");
}
class ExecuteShutdown : public DeferredWork
{
public:
void masterPreWork()
{
// The stats can be retrieved from global variables as needed.
// This can be uncommented for debug if desired
#ifdef __MEMSTATS__
if(c->master)
HeapManager::stats();
#endif
}
void activeMainWork()
{
KernelMisc::shutdown();
}
void nonactiveMainWork()
{
// Something wasn't synchronized correctly if we got to here.
// Should not have CPUs coming online while trying to execute
// a shutdown.
kassert(false);
}
};
DeferredQueue::insert(new ExecuteShutdown());
}
void CpuManager::startCPU(ssize_t i)
{
// Save away the current timebase for TB synchronization.
iv_lastStartTimebase = getTB();
bool currentCPU = false;
if (i < 0)
{
i = getCpuId();
currentCPU = true;
}
else if (getCpuId() == (uint64_t)i)
{
currentCPU = true;
}
size_t nodeId = i / KERNEL_MAX_SUPPORTED_CPUS_PER_NODE;
size_t cpuId = i % KERNEL_MAX_SUPPORTED_CPUS_PER_NODE;
// Initialize node structure.
if (NULL == cv_cpus[nodeId])
{
cv_cpus[nodeId] = new cpu_t*[KERNEL_MAX_SUPPORTED_CPUS_PER_NODE]();
}
// Initialize CPU structure.
if (NULL == cv_cpus[nodeId][cpuId])
{
printkd("Start pir 0x%lx...", i);
cpu_t* cpu = cv_cpus[nodeId][cpuId] = new cpu_t();
// Initialize CPU.
cpu->cpu = i;
if (currentCPU)
{
cpu->master = true;
}
else
{
cpu->master = false;
}
cpu->scheduler = &Singleton<Scheduler>::instance();
cpu->scheduler_extra = NULL;
const size_t kernel_page_count = 4;
const size_t kernel_page_offset = kernel_page_count * PAGESIZE -
8 * sizeof(uint64_t);
cpu->kernel_stack_bottom = PageManager::allocatePage(kernel_page_count);
cpu->kernel_stack = reinterpret_cast<void*>(
reinterpret_cast<uintptr_t>(cpu->kernel_stack_bottom) +
kernel_page_offset);
cpu->xscom_mutex = NULL;
// xscom workaround for HW822317 : Power8 Errata.
// Need to make the xscom mutex a per-core mutex to prevent
// multi-threaded access to the HMER.
if ((CpuID::getCpuType() == CORE_POWER8_MURANO) ||
(CpuID::getCpuType() == CORE_POWER8_VENICE) ||
(CpuID::getCpuType() == CORE_POWER8_NAPLES))
{
const size_t num_threads = getThreadCount();
size_t cpu_idx = (cpuId / num_threads) * num_threads;
for(size_t i = 0; i < getThreadCount(); ++i)
{
if ((NULL != cv_cpus[nodeId][cpu_idx + i]) &&
(NULL != cv_cpus[nodeId][cpu_idx + i]->xscom_mutex))
{
cpu->xscom_mutex =
cv_cpus[nodeId][cpu_idx + i]->xscom_mutex;
break;
}
}
}
if (NULL == cpu->xscom_mutex)
{
cpu->xscom_mutex = new mutex_t;
mutex_init(cpu->xscom_mutex);
}
// Create idle task.
cpu->idle_task = TaskManager::createIdleTask();
cpu->idle_task->cpu = cpu;
cpu->periodic_count = 0;
// Call TimeManager setup for a CPU.
TimeManager::init_cpu(cpu);
printkd("done\n");
}
if (currentCPU)
{
setDEC(TimeManager::getTimeSliceCount());
activateCPU(getCpu(i));
}
return;
}
void CpuManager::startSlaveCPU(cpu_t* cpu)
{
// Activate CPU.
activateCPU(cpu);
// Sync timebase with master.
while(getTB() < iv_lastStartTimebase)
{
class SyncTimebase : public DeferredWork
{
public:
void masterPreWork()
{
iv_timebase = getTB();
}
void activeMainWork()
{
if (getTB() < iv_timebase)
{
setTB(iv_timebase);
}
}
private:
uint64_t iv_timebase;
};
SyncTimebase* deferred = new SyncTimebase();
DeferredQueue::insert(deferred, true /* only if empty */);
DeferredQueue::execute();
}
// Update decrementer.
setDEC(TimeManager::getTimeSliceCount());
return;
}
void CpuManager::activateCPU(cpu_t * i_cpu)
{
// Set active.
i_cpu->active = true;
// Update sequence ID.
do
{
uint64_t old_seq = cv_cpuSeq;
i_cpu->cpu_start_seqid = old_seq + 1 + (1ull << 32);
if (__sync_bool_compare_and_swap(&cv_cpuSeq, old_seq,
i_cpu->cpu_start_seqid))
{
break;
}
} while (1);
i_cpu->cpu_start_seqid >>= 32;
// Verify / set SPRs.
uint64_t msr = getMSR();
msr |= 0x1000; // MSR[ME] is not saved on initial wakeup, but we set on
// entering userspace, so ignore this bit in assert.
kassert(WAKEUP_MSR_VALUE == msr);
setLPCR(WAKEUP_LPCR_VALUE);
setRPR(WAKEUP_RPR_VALUE);
setPSSCR(PSSCR_NAP_VALUE); // init NAP value (stop level 1)
}
void CpuManager::deactivateCPU(cpu_t * i_cpu)
{
// Set inactive.
i_cpu->active = false;
// Update sequence ID.
do
{
uint64_t old_seq = cv_cpuSeq;
uint64_t new_seq = old_seq - 1 + (1ull << 32);
if (__sync_bool_compare_and_swap(&cv_cpuSeq, old_seq, new_seq))
{
break;
}
} while(1);
}
void CpuManager::executePeriodics(cpu_t * i_cpu)
{
if(i_cpu->master)
{
if (cv_interactive_debug.isReady())
{
cv_interactive_debug.startDebugTask();
}
bool forceMemoryPeriodic = __sync_fetch_and_and(&cv_forcedMemPeriodic,
0);
++(i_cpu->periodic_count);
if((0 == (i_cpu->periodic_count % CPU_PERIODIC_CHECK_MEMORY)) ||
(forceMemoryPeriodic))
{
uint64_t pcntAvail = PageManager::queryAvail();
if((pcntAvail < PageManager::LOWMEM_NORM_LIMIT) ||
(forceMemoryPeriodic))
{
VmmManager::flushPageTable();
++(i_cpu->periodic_count); // prevent another flush below
if(pcntAvail < PageManager::LOWMEM_CRIT_LIMIT)
{
VmmManager::castOutPages(VmmManager::CRITICAL);
}
else
{
VmmManager::castOutPages(VmmManager::NORMAL);
}
}
}
if(0 == (i_cpu->periodic_count % CPU_PERIODIC_FLUSH_PAGETABLE))
{
VmmManager::flushPageTable();
}
if((0 == (i_cpu->periodic_count % CPU_PERIODIC_DEFRAG)) ||
(forceMemoryPeriodic))
{
class MemoryCoalesce : public DeferredWork
{
public:
void masterPreWork()
{
setThreadPriorityVeryHigh();
HeapManager::coalesce();
PageManager::coalesce();
setThreadPriorityHigh();
}
};
DeferredQueue::insert(new MemoryCoalesce());
}
}
DeferredQueue::execute();
}
void CpuManager::startCore(uint64_t pir,uint64_t i_threads)
{
size_t threads = getThreadCount();
pir = pir & ~(threads-1);
if (pir >=
(KERNEL_MAX_SUPPORTED_NODES * KERNEL_MAX_SUPPORTED_CPUS_PER_NODE))
{
TASK_SETRTN(TaskManager::getCurrentTask(), -ENXIO);
return;
}
for(size_t i = 0; i < threads; i++)
{
// Only start the threads we were told to start
if( i_threads & (0x8000000000000000 >> i) )
{
Singleton<CpuManager>::instance().startCPU(pir + i);
}
}
__sync_synchronize();
//Send a message to userspace that a core with this base pir is being added
// userspace will know which threads on the core to expect already
InterruptMsgHdlr::addCpuCore(pir);
for(size_t i = 0; i < threads; i++)
{
// Only wakeup the threads we were told to wakeup
if( i_threads & (0x8000000000000000 >> i) )
{
printk("Dbell pir 0x%lx\n", pir + i);
//Initiate the Doorbell for this core/pir
send_doorbell_wakeup(pir + i);
}
}
return;
};
size_t CpuManager::getThreadCount()
{
size_t threads = 0;
switch (CpuID::getCpuType())
{
case CORE_POWER8_VENICE:
case CORE_POWER8_MURANO:
case CORE_POWER8_NAPLES:
threads = 8;
break;
case CORE_POWER9_NIMBUS:
case CORE_POWER9_CUMULUS:
threads = 4;
break;
case CORE_UNKNOWN:
default:
PVR_t l_pvr( getPVR() );
printk("cputype=%d, pvr=%.8X\n",
CpuID::getCpuType(), l_pvr.word);
kassert(false);
break;
}
return threads;
}
void CpuManager::forceMemoryPeriodic()
{
cv_forcedMemPeriodic = 1;
}
void CpuManager::critAssert(uint64_t i_failAddr)
{
/*@
* @errortype
* @moduleid KERNEL::MOD_KERNEL_INVALID
* @reasoncode KERNEL::RC_SHUTDOWN
* @userdata1 Failing address
* @userdata2 <unused>
* @devdesc Kernel encountered an unhandled exception.
* @custdesc Boot firmware has crashed with an internal
* error.
*/
/* create SRC amd call terminate immediate*/
termWriteSRC(TI_CRIT_ASSERT,KERNEL::RC_SHUTDOWN, i_failAddr);
class ExecuteCritAssert : public DeferredWork
{
public:
void masterPreWork()
{
// print status to the console.
printk("TI initiated on all threads (crit_assert)\n");
}
void activeMainWork()
{
// Call the function to perform the TI
terminateExecuteTI();
}
void nonactiveMainWork()
{
// Something wasn't synchronized correctly if we got to here.
// Should not have CPUs coming online while trying to execute
// a shutdown.
terminateExecuteTI();
}
};
DeferredQueue::insert(new ExecuteCritAssert());
// Force executeion of the deferred queue.
DeferredQueue::execute();
}
|