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

#include <kernel/segmentmgr.H>
#include <kernel/segment.H>
#include <kernel/devicesegment.H>

bool SegmentManager::handlePageFault(task_t* i_task, uint64_t i_addr,
                                     bool i_store)
{
    return Singleton<SegmentManager>::instance().
                _handlePageFault(i_task, i_addr, i_store);
}

void SegmentManager::addSegment(Segment* i_segment, size_t i_segId)
{
    Singleton<SegmentManager>::instance()._addSegment(i_segment, i_segId);
}

void SegmentManager::initSLB()
{
    Singleton<SegmentManager>::instance()._initSLB();
}

uint64_t SegmentManager::findPhysicalAddress(uint64_t i_vaddr)
{
    return Singleton<SegmentManager>::instance()._findPhysicalAddress(i_vaddr);
}

void SegmentManager::updateRefCount( uint64_t i_vaddr,
				     PageTableManager::UsageStats_t i_stats )
{
    Singleton<SegmentManager>::instance()._updateRefCount(i_vaddr,i_stats);
}

void SegmentManager::castOutPages(uint64_t i_type)
{
    Singleton<SegmentManager>::instance()._castOutPages(i_type);
}

void* SegmentManager::devMap(void* ra, uint64_t i_devDataSize)
{
    return Singleton<SegmentManager>::instance()._devMap(ra, i_devDataSize);
}

int SegmentManager::devUnmap(void* ea)
{
    return Singleton<SegmentManager>::instance()._devUnmap(ea);
}

bool SegmentManager::_handlePageFault(task_t* i_task, uint64_t i_addr,
                                      bool i_store)
{
    size_t segId = getSegmentIdFromAddress(i_addr);

    // Call contained segment object to handle page fault.
    if ((segId < MAX_SEGMENTS) && (NULL != iv_segments[segId]))
    {
        return iv_segments[segId]->handlePageFault(i_task, i_addr, i_store);
    }

    return false;
}

void SegmentManager::_addSegment(Segment* i_segment, size_t i_segId)
{
    kassert(i_segId < MAX_SEGMENTS);
    iv_segments[i_segId] = i_segment;
}

void SegmentManager::_initSLB()
{
    // Flush SLB.
    asm volatile("slbia" ::: "memory");
    isync(); // Ensure slbia completes prior to slbmtes.

    register uint64_t slbRS, slbRB;

    // Default segment descriptors.
    // ESID = 0, V = 1, Index = 1.
    slbRB = 0x0000000008000001;
    // B = 01 (1TB), VSID = 0, Ks = 0, Kp = 1, NLCLP = 0
    slbRS = 0x4000000000000400;

    // Add all segments to SLB.
    for (size_t i = 0; i < MAX_SEGMENTS; i++)
    {
        // Add segment to SLB.
        if (NULL != iv_segments[i])
        {
            asm volatile("slbmte %0, %1" :: "r"(slbRS), "r"(slbRB) : "memory");
        }

        // Increment ESID, VSID, Index.
        slbRB += 0x0000010000000001;
        slbRS += 0x0000000001000000;
    }

    isync(); // Ensure slbmtes complete prior to continuing on.
}

uint64_t SegmentManager::_findPhysicalAddress(uint64_t i_vaddr) const
{
    uint64_t paddr = -EFAULT;
    size_t segId = getSegmentIdFromAddress(i_vaddr);

    if ((segId < MAX_SEGMENTS) && (NULL != iv_segments[segId]))
    {
        paddr = iv_segments[segId]->findPhysicalAddress(i_vaddr);
    }

    return paddr;
}

void SegmentManager::_updateRefCount( uint64_t i_vaddr,
				      PageTableManager::UsageStats_t i_stats )
{
    // Get segment ID from effective address.
    size_t segId = getSegmentIdFromAddress(i_vaddr);

    // Call contained segment object to update the reference count
    if ((segId < MAX_SEGMENTS) && (NULL != iv_segments[segId]))
    {
        iv_segments[segId]->updateRefCount( i_vaddr, i_stats );
    }
}

void SegmentManager::_castOutPages(uint64_t i_type)
{
    for (size_t i = 0; i < MAX_SEGMENTS; i++)
    {
        if (NULL != iv_segments[i])
        {
            iv_segments[i]->castOutPages(i_type);
        }
    }
}

void* SegmentManager::_devMap(void* ra, uint64_t i_devDataSize)
{
    void* ea = NULL;
    for (size_t i = MMIO_FIRST_SEGMENT_ID; i <= MMIO_LAST_SEGMENT_ID; i++)
    {
        if (NULL == iv_segments[i]) continue;

        ea = reinterpret_cast<DeviceSegment*>(iv_segments[i])->
                devMap(ra, i_devDataSize);

        if (ea != NULL) break;
    }

    if (ea == NULL)
    {
        printk("SegmentManager: Ran out of device segment blocks.\n");
    }

    return ea;
}

int SegmentManager::_devUnmap(void* ea)
{
    size_t segId = getSegmentIdFromAddress(reinterpret_cast<uint64_t>(ea));
    if ((segId < MMIO_FIRST_SEGMENT_ID) ||
        (segId > MMIO_LAST_SEGMENT_ID) ||
        (NULL == iv_segments[segId]))
    {
        return -EINVAL;
    }

    return reinterpret_cast<DeviceSegment*>(iv_segments[segId])->devUnmap(ea);
}


OpenPOWER on IntegriCloud