summaryrefslogtreecommitdiffstats
path: root/src/kernel/cpumgr.C
blob: 3bb52b63c7d826a0ee71b3e0e439f2deeecf6abf (plain)
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
/*  IBM_PROLOG_BEGIN_TAG
 *  This is an automatically generated prolog.
 *
 *  $Source: src/kernel/cpumgr.C $
 *
 *  IBM CONFIDENTIAL
 *
 *  COPYRIGHT International Business Machines Corp. 2010-2012
 *
 *  p1
 *
 *  Object Code Only (OCO) source materials
 *  Licensed Internal Code Source Materials
 *  IBM HostBoot Licensed Internal Code
 *
 *  The source code for this program is not published or other-
 *  wise divested of its trade secrets, irrespective of what has
 *  been deposited with the U.S. Copyright Office.
 *
 *  Origin: 30
 *
 *  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>

cpu_t* CpuManager::cv_cpus[CpuManager::MAXCPUS] = { NULL };
bool CpuManager::cv_shutdown_requested = false;
uint64_t CpuManager::cv_shutdown_status = 0;
Barrier CpuManager::cv_barrier;
bool CpuManager::cv_defrag = false;
size_t CpuManager::cv_cpuCount = 0;
InteractiveDebug CpuManager::cv_interactive_debug;

CpuManager::CpuManager()
{
    for (int i = 0; i < MAXCPUS; i++)
	cv_cpus[i] = NULL;

    memset(&cv_interactive_debug, '\0', sizeof(cv_interactive_debug));
}

cpu_t* CpuManager::getCurrentCPU()
{
    return cv_cpus[getPIR()];
}

cpu_t* CpuManager::getMasterCPU()
{
    for (int i = 0; i < MAXCPUS; i++)
        if (cv_cpus[i] != NULL)
            if (cv_cpus[i]->master)
                return cv_cpus[i];
    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 = -1;
    switch (CpuID::getCpuType())
    {
        case CORE_POWER7:
        case CORE_POWER7_PLUS:
            threads = 4;
            break;

        case CORE_POWER8_VENICE:
        case CORE_POWER8_MURANO:
            threads = 8;
            break;

        case CORE_UNKNOWN:
        default:
            kassert(false);
            break;
    }

    // Create CPU objects starting at the thread-0 for this core.
    size_t baseCpu = getPIR() & ~(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)
{
    cv_shutdown_status = i_status;
    __sync_synchronize();
    cv_shutdown_requested = true;
}

void CpuManager::startCPU(ssize_t i)
{
    bool currentCPU = false;
    if (i < 0)
    {
	i = getCpuId();
	currentCPU = true;
    }
    else if (getCpuId() == (uint64_t)i)
    {
	currentCPU = true;
    }

    // Initialize CPU structure.
    if (NULL == cv_cpus[i])
    {
	printk("Starting CPU %ld...", i);
	cpu_t* cpu = cv_cpus[i] = 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;
	cpu->kernel_stack =
	    (void*) (((uint64_t)PageManager::allocatePage(4)) + 16320);
        cpu->xscom_mutex = (mutex_t)MUTEX_INITIALIZER;

	// Create idle task.
	cpu->idle_task = TaskManager::createIdleTask();
	cpu->idle_task->cpu = cpu;
        cpu->periodic_count = 0;

	printk("done\n");
    }

    if (currentCPU)
    {
        setDEC(TimeManager::getTimeSliceCount());
        activateCPU(cv_cpus[i]);
    }
    return;
}

void CpuManager::startSlaveCPU(cpu_t* cpu)
{
    setDEC(TimeManager::getTimeSliceCount());
    activateCPU(cpu);

    return;
}

void CpuManager::activateCPU(cpu_t * i_cpu)
{
    i_cpu->active = true;
    __sync_fetch_and_add(&cv_cpuCount, 1);
    lwsync();
}

void CpuManager::executePeriodics(cpu_t * i_cpu)
{
    if(i_cpu->master)
    {
        if (cv_interactive_debug.isReady())
        {
            cv_interactive_debug.startDebugTask();
        }

        ++(i_cpu->periodic_count);
        if(0 == (i_cpu->periodic_count % CPU_PERIODIC_CHECK_MEMORY))
        {
            uint64_t pcntAvail = PageManager::queryAvail();
            if(pcntAvail < 16)
            {
                VmmManager::flushPageTable();
                ++(i_cpu->periodic_count);   // prevent another flush below
                if(pcntAvail < 5)
                {
                    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))
        {
            // set up barrier based on # cpus cv_barrier;
            // TODO whatif other cpus become active?
            isync(); // Ensure all instructions complete before this point, so
                     // we don't get a stale shutdown_requested.
            if(!cv_shutdown_requested)
            {
                cv_barrier.init(cv_cpuCount);
                lwsync();  // Ensure barrier init is globally visible before
                           // setting defrag = true.
                cv_defrag = true;
            }
        }
    }
    if(cv_defrag)
    {
        cv_barrier.wait();

        if(i_cpu->master)
        {
            HeapManager::coalesce();
            PageManager::coalesce();
            cv_defrag = false;
        }

        cv_barrier.wait();
    }
}

OpenPOWER on IntegriCloud