summaryrefslogtreecommitdiff
path: root/src/cpu.cpp
blob: 552a781ffa851675c4c9489ede469c31461e4caf (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
/*
// Copyright (c) 2018 Intel Corporation
//
// 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.
*/

#include "cpu.hpp"
#include "manager.hpp"
#include <map>
#include <cstdint>
#include <bitset>
#include <iostream>

namespace phosphor
{
namespace smbios
{

void Cpu::cpuSocket(uint8_t positionNum, uint8_t structLen, uint8_t *dataIn)
{
    std::string result;

    result = positionToString(positionNum, structLen, dataIn);

    processor::socket(result);

    location::locationCode(result);
}

std::string Cpu::processorSocket(std::string value)
{
    return processor::socket(value);
}

void Cpu::cpuFamily(uint8_t value)
{
    std::map<uint8_t, std::string>::const_iterator it = familyTable.find(value);
    if (it == familyTable.end())
    {
        processor::family("Unknown Processor Family");
    }
    else
    {
        processor::family(it->second);
    }
}

void Cpu::cpuManufacturer(uint8_t positionNum, uint8_t structLen,
                          uint8_t *dataIn)
{
    std::string result;

    result = positionToString(positionNum, structLen, dataIn);

    asset::manufacturer(result);
}

uint32_t Cpu::processorId(uint32_t value)
{
    return processor::id(value);
}

void Cpu::cpuVersion(uint8_t positionNum, uint8_t structLen, uint8_t *dataIn)
{
    std::string result;

    result = positionToString(positionNum, structLen, dataIn);

    processor::version(result);
    rev::version(result);
}

uint16_t Cpu::processorMaxSpeed(uint16_t value)
{
    return processor::maxSpeedInMhz(value);
}

void Cpu::cpuCharacteristics(uint16_t value)
{
    std::vector<processor::Capability> result;
    std::optional<processor::Capability> cap;

    std::bitset<16> charBits = value;
    for (uint8_t index = 0; index < charBits.size(); index++)
    {
        if (charBits.test(index))
        {
            if (cap = characteristicsTable[index])
            {
                result.emplace_back(*cap);
            }
        }
    }

    processor::characteristics(result);
}

uint16_t Cpu::processorCoreCount(uint16_t value)
{
    return processor::coreCount(value);
}

uint16_t Cpu::processorThreadCount(uint16_t value)
{
    return processor::threadCount(value);
}

static constexpr uint8_t maxOldVersionCount = 0xff;
void Cpu::processorInfoUpdate(void)
{
    uint8_t *dataIn = regionS[0].regionData;

    dataIn = smbiosTypePtr(dataIn, processorsType);
    if (dataIn == nullptr)
    {
        return;
    }

    for (uint8_t index = 0; index < cpuNum; index++)
    {
        dataIn = smbiosNextPtr(dataIn);
        if (dataIn == nullptr)
        {
            return;
        }
        dataIn = smbiosTypePtr(dataIn, processorsType);
        if (dataIn == nullptr)
        {
            return;
        }
    }

    auto cpuInfo = reinterpret_cast<struct ProcessorInfo *>(dataIn);

    constexpr uint32_t socketPopulatedMask = 1 << 6;
    if ((cpuInfo->status & socketPopulatedMask) == 0)
    {
        // Don't attempt to fill in any other details if the CPU is not present.
        present(false);
        return;
    }
    present(true);

    cpuSocket(cpuInfo->socketDesignation, cpuInfo->length, dataIn); // offset 4h
    cpuFamily(cpuInfo->family);                                     // offset 6h
    cpuManufacturer(cpuInfo->manufacturer, cpuInfo->length, dataIn);// offset 7h
    processorId(cpuInfo->id);                                       // offset 8h
    cpuVersion(cpuInfo->version, cpuInfo->length, dataIn);          // offset 10h
    processorMaxSpeed(cpuInfo->maxSpeed);                           // offset 14h
    
    if (cpuInfo->coreCount < maxOldVersionCount)                    // offset 23h or 2Ah
    {
        processorCoreCount((uint16_t)cpuInfo->coreCount);
    }
    else
    {
        processorCoreCount(cpuInfo->coreCount2);
    }

    if (cpuInfo->threadCount < maxOldVersionCount) // offset 25h or 2Eh)
    {
        processorThreadCount((uint16_t)cpuInfo->threadCount);
    }
    else
    {
        processorThreadCount(cpuInfo->threadCount2);
    }

    cpuCharacteristics(cpuInfo->characteristics); // offset 26h
}

} // namespace smbios
} // namespace phosphor