summaryrefslogtreecommitdiffstats
path: root/src/kernel/segmentmgr.C
blob: 73caaf76f699614e6013a145f534a9b5f0de359f (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
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/kernel/segmentmgr.C $                                     */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2011,2015                        */
/* [+] 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 <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, bool i_nonCI,
    bool i_guarded)
{
    return Singleton<SegmentManager>::instance()._devMap(ra, i_devDataSize,
                                                         i_nonCI, i_guarded);
}

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, bool i_nonCI,
    bool i_guarded)
{
    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, i_nonCI, i_guarded);

        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