summaryrefslogtreecommitdiffstats
path: root/src/kernel/deferred.C
blob: dc64948576ecdb0671882994250f30924eb7844d (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
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
/*  IBM_PROLOG_BEGIN_TAG
 *  This is an automatically generated prolog.
 *
 *  $Source: src/kernel/deferred.C $
 *
 *  IBM CONFIDENTIAL
 *
 *  COPYRIGHT International Business Machines Corp. 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 <kernel/deferred.H>
#include <kernel/cpumgr.H>
#include <util/singleton.H>
#include <assert.h>
#include <arch/ppc.H>

/** Extract the DeferredWork pointer part of a iv_cpus_and_next instance var. */
#define DEFERRED_QUEUE_GET_NEXT_PTR(item) \
    reinterpret_cast<DeferredWork*>((item)->iv_cpus_and_next & 0xFFFFFFFF)

/** Set the DeferredWork pointer part of a iv_cpus_and_next instance var. */
#define DEFERRED_QUEUE_SET_NEXT_PTR(item) \
    (item)->iv_cpus_and_next = \
            ((item)->iv_cpus_and_next & 0xFFFFFFFF00000000ull) | \
            reinterpret_cast<uint64_t>(item)

/** Extract the CPU count portion of a iv_cpus_and_next instance var. */
#define DEFERRED_QUEUE_GET_CPU_COUNT(item) (item)->iv_cpus_and_next >> 32

// Initialize the work queue.
DeferredQueue::DeferredQueue() : lock(), iv_cpus_and_next(0) {}

DeferredQueue::~DeferredQueue()
{
    // Ensure that all work is completed.
    kassert(0 == iv_cpus_and_next);
}

void DeferredQueue::insert(DeferredWork* i_work)
{
    // Call singleton insert.
    Singleton<DeferredQueue>::instance()._insert(i_work);
}

void DeferredQueue::execute()
{
    // Call singleton execute.
    Singleton<DeferredQueue>::instance()._execute();
}

void DeferredQueue::_insert(DeferredWork* i_work)
{
    lock.lock();

    // NULL pointer implies empty, so just add work item.
    if (0 == iv_cpus_and_next)
    {
        iv_cpus_and_next = reinterpret_cast<uint64_t>(i_work);
    }
    else
    {
        // Follow linked list to last work item.
        DeferredWork* tail = DEFERRED_QUEUE_GET_NEXT_PTR(this);
        while (NULL != DEFERRED_QUEUE_GET_NEXT_PTR(tail))
        {
            tail = DEFERRED_QUEUE_GET_NEXT_PTR(tail);
        }

        // Add work item to the end of the list.
        DEFERRED_QUEUE_SET_NEXT_PTR(i_work);
    }

    lock.unlock();
}

void DeferredQueue::_execute()
{
    uint64_t cpus_and_next = 0;

    // Increment the CPU count for pointer references.
    do
    {
        cpus_and_next = iv_cpus_and_next;

        if (0 == cpus_and_next) // No work to execute.
        {
            return;
        }

    } while(!__sync_bool_compare_and_swap(&iv_cpus_and_next,
                                          cpus_and_next,
                                          cpus_and_next + (1ull << 32)));

    // Extract the item pointer.
    DeferredWork* item =
            reinterpret_cast<DeferredWork*>(cpus_and_next & 0xFFFFFFFF);

    // Execute the extracted item.
    item->start();
}

void DeferredQueue::_complete(DeferredWork* i_work)
{
    lock.lock();

    // Update list-head to pop item off.
    uint64_t new_ptr =
            reinterpret_cast<uint64_t>(DEFERRED_QUEUE_GET_NEXT_PTR(i_work));
    uint64_t old_ptr = 0;

    do
    {
        old_ptr = iv_cpus_and_next;
    } while(!__sync_bool_compare_and_swap(&iv_cpus_and_next, old_ptr, new_ptr));

    // Get the CPU count from the old object pointer and wait until those
    // CPUs get into i_work.
    old_ptr >>= 32;
    while (DEFERRED_QUEUE_GET_CPU_COUNT(i_work) != old_ptr)
    {
        setThreadPriorityLow();
    }
    setThreadPriorityHigh();

    lock.unlock();
}

DeferredWork::DeferredWork() : iv_barrier(), iv_cpus_and_next(0),
                               iv_activeSeqId(0),
                               iv_releasePre(false), iv_releasePost(false)
{
    uint32_t cpuCount;

    // Read the current CPU count and sequence number.
    CpuManager::getCpuCountAndSeqId(cpuCount, iv_activeSeqId);
    // Initialize the barrier with the number of active CPUs.
    iv_barrier.init(cpuCount);
}

DeferredWork::~DeferredWork()
{
    // Ensure the work item was removed from the queue chain and no
    // CPUs are still inside it.
    kassert(0 == iv_cpus_and_next);
}

void DeferredWork::start()
{
    // Increment object reference count.
    __sync_add_and_fetch(&iv_cpus_and_next, 1ull << 32);

    // Get our CPU object and determine if we were active when the item
    // was created. (Our sequence # being less or equal work item sequence #)
    cpu_t* cpu = CpuManager::getCurrentCPU();
    bool active = cpu->cpu_start_seqid <= iv_activeSeqId;

    // Synchronize active CPUs.
    if (active)
    {
        _waitForCpus();
    }

    // Call masterPre step.
    if (cpu->master)
    {
        _masterPre();
    }
    else
    {
        _waitAtPre();
    }

    // Call MainWork step.
    if (active)
    {
        activeMainWork();
        _waitForCpus();
    }
    else
    {
        nonactiveMainWork();
    }

    // Call masterPost step.
    if (cpu->master)
    {
        _masterPost();
    }
    else
    {
        _waitAtPost();
    }

    // Release reference to this object.
    _cleanup();
}

void DeferredWork::_waitForCpus()
{
    iv_barrier.wait();
}

void DeferredWork::_masterPre()
{
    masterPreWork();

    // Ensure memory ops are globally visible before releasing all CPUs.
    lwsync();
    iv_releasePre = true;
}

void DeferredWork::_waitAtPre()
{
    while(!iv_releasePre)
    {
        setThreadPriorityLow();
    }
    isync();  // Prevent spec. execution past this point until released.
    setThreadPriorityHigh();
}

void DeferredWork::_masterPost()
{
    masterPostWork();

    // Remove ourself from the queue chain now.
    Singleton<DeferredQueue>::instance()._complete(this);

    // Ensure memory ops are globally visible before releasing all CPUs.
    lwsync();
    iv_releasePost = true;
}

void DeferredWork::_waitAtPost()
{
    while(!iv_releasePost)
    {
        setThreadPriorityLow();
    }
    isync();  // Prevent spec. execution past this point until released.
    setThreadPriorityHigh();
}

void DeferredWork::_cleanup()
{
    // Decrement reference count.
    uint64_t cpu_count =
        __sync_sub_and_fetch(&iv_cpus_and_next, 1ull << 32) >> 32;

    // If the last object, delete this work item.
    if (0 == cpu_count)
    {
        delete this;
    }
}
OpenPOWER on IntegriCloud