xref: /openbmc/bmcweb/features/redfish/lib/processor.hpp (revision 5e7e2dc585dc7a7e62d2b648b541e7f50c39ea5d)
1ac6a4445SGunnar Mills /*
2ac6a4445SGunnar Mills // Copyright (c) 2018 Intel Corporation
3ac6a4445SGunnar Mills //
4ac6a4445SGunnar Mills // Licensed under the Apache License, Version 2.0 (the "License");
5ac6a4445SGunnar Mills // you may not use this file except in compliance with the License.
6ac6a4445SGunnar Mills // You may obtain a copy of the License at
7ac6a4445SGunnar Mills //
8ac6a4445SGunnar Mills //      http://www.apache.org/licenses/LICENSE-2.0
9ac6a4445SGunnar Mills //
10ac6a4445SGunnar Mills // Unless required by applicable law or agreed to in writing, software
11ac6a4445SGunnar Mills // distributed under the License is distributed on an "AS IS" BASIS,
12ac6a4445SGunnar Mills // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ac6a4445SGunnar Mills // See the License for the specific language governing permissions and
14ac6a4445SGunnar Mills // limitations under the License.
15ac6a4445SGunnar Mills */
16ac6a4445SGunnar Mills #pragma once
17ac6a4445SGunnar Mills 
183ccb3adbSEd Tanous #include "app.hpp"
191e1e598dSJonathan Doman #include "dbus_singleton.hpp"
207a1dbc48SGeorge Liu #include "dbus_utility.hpp"
211e1e598dSJonathan Doman #include "error_messages.hpp"
22ac6a4445SGunnar Mills #include "health.hpp"
233ccb3adbSEd Tanous #include "query.hpp"
243ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
253ccb3adbSEd Tanous #include "utils/collection.hpp"
263ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
273ccb3adbSEd Tanous #include "utils/json_utils.hpp"
28ac6a4445SGunnar Mills 
29ac6a4445SGunnar Mills #include <boost/container/flat_map.hpp>
30e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
311e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
32dba0c291SJonathan Doman #include <sdbusplus/message/native_types.hpp>
33351053f2SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
34dba0c291SJonathan Doman #include <sdbusplus/utility/dedup_variant.hpp>
35ac6a4445SGunnar Mills 
367a1dbc48SGeorge Liu #include <array>
377a1dbc48SGeorge Liu #include <string_view>
387a1dbc48SGeorge Liu 
39ac6a4445SGunnar Mills namespace redfish
40ac6a4445SGunnar Mills {
41ac6a4445SGunnar Mills 
42c951448aSJonathan Doman // Interfaces which imply a D-Bus object represents a Processor
437a1dbc48SGeorge Liu constexpr std::array<std::string_view, 2> processorInterfaces = {
44c951448aSJonathan Doman     "xyz.openbmc_project.Inventory.Item.Cpu",
45c951448aSJonathan Doman     "xyz.openbmc_project.Inventory.Item.Accelerator"};
462bab9831SJonathan Doman 
4771b82f26SSharad Yadav /**
4871b82f26SSharad Yadav  * @brief Fill out uuid info of a processor by
4971b82f26SSharad Yadav  * requesting data from the given D-Bus object.
5071b82f26SSharad Yadav  *
5171b82f26SSharad Yadav  * @param[in,out]   aResp       Async HTTP response.
5271b82f26SSharad Yadav  * @param[in]       service     D-Bus service to query.
5371b82f26SSharad Yadav  * @param[in]       objPath     D-Bus object to query.
5471b82f26SSharad Yadav  */
5571b82f26SSharad Yadav inline void getProcessorUUID(std::shared_ptr<bmcweb::AsyncResp> aResp,
5671b82f26SSharad Yadav                              const std::string& service,
5771b82f26SSharad Yadav                              const std::string& objPath)
5871b82f26SSharad Yadav {
5971b82f26SSharad Yadav     BMCWEB_LOG_DEBUG << "Get Processor UUID";
601e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
611e1e598dSJonathan Doman         *crow::connections::systemBus, service, objPath,
621e1e598dSJonathan Doman         "xyz.openbmc_project.Common.UUID", "UUID",
63*5e7e2dc5SEd Tanous         [objPath, aResp{std::move(aResp)}](const boost::system::error_code& ec,
641e1e598dSJonathan Doman                                            const std::string& property) {
6571b82f26SSharad Yadav         if (ec)
6671b82f26SSharad Yadav         {
6771b82f26SSharad Yadav             BMCWEB_LOG_DEBUG << "DBUS response error";
6871b82f26SSharad Yadav             messages::internalError(aResp->res);
6971b82f26SSharad Yadav             return;
7071b82f26SSharad Yadav         }
711e1e598dSJonathan Doman         aResp->res.jsonValue["UUID"] = property;
721e1e598dSJonathan Doman         });
7371b82f26SSharad Yadav }
7471b82f26SSharad Yadav 
75711ac7a9SEd Tanous inline void getCpuDataByInterface(
76711ac7a9SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
77711ac7a9SEd Tanous     const dbus::utility::DBusInteracesMap& cpuInterfacesProperties)
78ac6a4445SGunnar Mills {
79ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG << "Get CPU resources by interface.";
80ac6a4445SGunnar Mills 
81a1649ec6SChicago Duan     // Set the default value of state
82a1649ec6SChicago Duan     aResp->res.jsonValue["Status"]["State"] = "Enabled";
83a1649ec6SChicago Duan     aResp->res.jsonValue["Status"]["Health"] = "OK";
84ac6a4445SGunnar Mills 
85ac6a4445SGunnar Mills     for (const auto& interface : cpuInterfacesProperties)
86ac6a4445SGunnar Mills     {
87ac6a4445SGunnar Mills         for (const auto& property : interface.second)
88ac6a4445SGunnar Mills         {
89a1649ec6SChicago Duan             if (property.first == "Present")
90ac6a4445SGunnar Mills             {
91a1649ec6SChicago Duan                 const bool* cpuPresent = std::get_if<bool>(&property.second);
92a1649ec6SChicago Duan                 if (cpuPresent == nullptr)
93ac6a4445SGunnar Mills                 {
94ac6a4445SGunnar Mills                     // Important property not in desired type
95ac6a4445SGunnar Mills                     messages::internalError(aResp->res);
96ac6a4445SGunnar Mills                     return;
97ac6a4445SGunnar Mills                 }
98e05aec50SEd Tanous                 if (!*cpuPresent)
99ac6a4445SGunnar Mills                 {
100a1649ec6SChicago Duan                     // Slot is not populated
101ac6a4445SGunnar Mills                     aResp->res.jsonValue["Status"]["State"] = "Absent";
102a1649ec6SChicago Duan                 }
103a1649ec6SChicago Duan             }
104a1649ec6SChicago Duan             else if (property.first == "Functional")
105a1649ec6SChicago Duan             {
106a1649ec6SChicago Duan                 const bool* cpuFunctional = std::get_if<bool>(&property.second);
107a1649ec6SChicago Duan                 if (cpuFunctional == nullptr)
108a1649ec6SChicago Duan                 {
109a1649ec6SChicago Duan                     messages::internalError(aResp->res);
110ac6a4445SGunnar Mills                     return;
111ac6a4445SGunnar Mills                 }
112e05aec50SEd Tanous                 if (!*cpuFunctional)
113a1649ec6SChicago Duan                 {
114a1649ec6SChicago Duan                     aResp->res.jsonValue["Status"]["Health"] = "Critical";
115a1649ec6SChicago Duan                 }
116a1649ec6SChicago Duan             }
117a1649ec6SChicago Duan             else if (property.first == "CoreCount")
118a1649ec6SChicago Duan             {
119a1649ec6SChicago Duan                 const uint16_t* coresCount =
120a1649ec6SChicago Duan                     std::get_if<uint16_t>(&property.second);
121a1649ec6SChicago Duan                 if (coresCount == nullptr)
122a1649ec6SChicago Duan                 {
123a1649ec6SChicago Duan                     messages::internalError(aResp->res);
124a1649ec6SChicago Duan                     return;
125a1649ec6SChicago Duan                 }
126ac6a4445SGunnar Mills                 aResp->res.jsonValue["TotalCores"] = *coresCount;
127ac6a4445SGunnar Mills             }
128dc3fa667SJonathan Doman             else if (property.first == "MaxSpeedInMhz")
129dc3fa667SJonathan Doman             {
130dc3fa667SJonathan Doman                 const uint32_t* value = std::get_if<uint32_t>(&property.second);
131dc3fa667SJonathan Doman                 if (value != nullptr)
132dc3fa667SJonathan Doman                 {
133dc3fa667SJonathan Doman                     aResp->res.jsonValue["MaxSpeedMHz"] = *value;
134dc3fa667SJonathan Doman                 }
135dc3fa667SJonathan Doman             }
136ac6a4445SGunnar Mills             else if (property.first == "Socket")
137ac6a4445SGunnar Mills             {
138ac6a4445SGunnar Mills                 const std::string* value =
139ac6a4445SGunnar Mills                     std::get_if<std::string>(&property.second);
140ac6a4445SGunnar Mills                 if (value != nullptr)
141ac6a4445SGunnar Mills                 {
142ac6a4445SGunnar Mills                     aResp->res.jsonValue["Socket"] = *value;
143ac6a4445SGunnar Mills                 }
144ac6a4445SGunnar Mills             }
145ac6a4445SGunnar Mills             else if (property.first == "ThreadCount")
146ac6a4445SGunnar Mills             {
147dc3fa667SJonathan Doman                 const uint16_t* value = std::get_if<uint16_t>(&property.second);
148ac6a4445SGunnar Mills                 if (value != nullptr)
149ac6a4445SGunnar Mills                 {
150ac6a4445SGunnar Mills                     aResp->res.jsonValue["TotalThreads"] = *value;
151ac6a4445SGunnar Mills                 }
152ac6a4445SGunnar Mills             }
1531930fbd4SBrandon Kim             else if (property.first == "EffectiveFamily")
154ac6a4445SGunnar Mills             {
1551930fbd4SBrandon Kim                 const uint16_t* value = std::get_if<uint16_t>(&property.second);
1566169de2cSBrad Bishop                 if (value != nullptr && *value != 2)
157ac6a4445SGunnar Mills                 {
158ac6a4445SGunnar Mills                     aResp->res.jsonValue["ProcessorId"]["EffectiveFamily"] =
159866e4862SEd Tanous                         "0x" + intToHexString(*value, 4);
160ac6a4445SGunnar Mills                 }
161ac6a4445SGunnar Mills             }
1621930fbd4SBrandon Kim             else if (property.first == "EffectiveModel")
1631930fbd4SBrandon Kim             {
1641930fbd4SBrandon Kim                 const uint16_t* value = std::get_if<uint16_t>(&property.second);
1651930fbd4SBrandon Kim                 if (value == nullptr)
1661930fbd4SBrandon Kim                 {
1671930fbd4SBrandon Kim                     messages::internalError(aResp->res);
1681930fbd4SBrandon Kim                     return;
1691930fbd4SBrandon Kim                 }
1706169de2cSBrad Bishop                 if (*value != 0)
1716169de2cSBrad Bishop                 {
1721930fbd4SBrandon Kim                     aResp->res.jsonValue["ProcessorId"]["EffectiveModel"] =
173866e4862SEd Tanous                         "0x" + intToHexString(*value, 4);
1741930fbd4SBrandon Kim                 }
1756169de2cSBrad Bishop             }
176ac6a4445SGunnar Mills             else if (property.first == "Id")
177ac6a4445SGunnar Mills             {
178ac6a4445SGunnar Mills                 const uint64_t* value = std::get_if<uint64_t>(&property.second);
179ac6a4445SGunnar Mills                 if (value != nullptr && *value != 0)
180ac6a4445SGunnar Mills                 {
181ac6a4445SGunnar Mills                     aResp->res
182ac6a4445SGunnar Mills                         .jsonValue["ProcessorId"]["IdentificationRegisters"] =
183866e4862SEd Tanous                         "0x" + intToHexString(*value, 16);
184ac6a4445SGunnar Mills                 }
185ac6a4445SGunnar Mills             }
1861930fbd4SBrandon Kim             else if (property.first == "Microcode")
1871930fbd4SBrandon Kim             {
1881930fbd4SBrandon Kim                 const uint32_t* value = std::get_if<uint32_t>(&property.second);
1891930fbd4SBrandon Kim                 if (value == nullptr)
1901930fbd4SBrandon Kim                 {
1911930fbd4SBrandon Kim                     messages::internalError(aResp->res);
1921930fbd4SBrandon Kim                     return;
1931930fbd4SBrandon Kim                 }
1946169de2cSBrad Bishop                 if (*value != 0)
1956169de2cSBrad Bishop                 {
1961930fbd4SBrandon Kim                     aResp->res.jsonValue["ProcessorId"]["MicrocodeInfo"] =
197866e4862SEd Tanous                         "0x" + intToHexString(*value, 8);
1981930fbd4SBrandon Kim                 }
1996169de2cSBrad Bishop             }
2001930fbd4SBrandon Kim             else if (property.first == "Step")
2011930fbd4SBrandon Kim             {
2021930fbd4SBrandon Kim                 const uint16_t* value = std::get_if<uint16_t>(&property.second);
2031930fbd4SBrandon Kim                 if (value == nullptr)
2041930fbd4SBrandon Kim                 {
2051930fbd4SBrandon Kim                     messages::internalError(aResp->res);
2061930fbd4SBrandon Kim                     return;
2071930fbd4SBrandon Kim                 }
2086169de2cSBrad Bishop                 if (*value != 0)
2096169de2cSBrad Bishop                 {
2101930fbd4SBrandon Kim                     aResp->res.jsonValue["ProcessorId"]["Step"] =
211866e4862SEd Tanous                         "0x" + intToHexString(*value, 4);
2121930fbd4SBrandon Kim                 }
213ac6a4445SGunnar Mills             }
214ac6a4445SGunnar Mills         }
215ac6a4445SGunnar Mills     }
2166169de2cSBrad Bishop }
217ac6a4445SGunnar Mills 
2188d1b46d7Szhanghch05 inline void getCpuDataByService(std::shared_ptr<bmcweb::AsyncResp> aResp,
219ac6a4445SGunnar Mills                                 const std::string& cpuId,
220ac6a4445SGunnar Mills                                 const std::string& service,
221ac6a4445SGunnar Mills                                 const std::string& objPath)
222ac6a4445SGunnar Mills {
223ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG << "Get available system cpu resources by service.";
224ac6a4445SGunnar Mills 
225ac6a4445SGunnar Mills     crow::connections::systemBus->async_method_call(
226ac6a4445SGunnar Mills         [cpuId, service, objPath, aResp{std::move(aResp)}](
227*5e7e2dc5SEd Tanous             const boost::system::error_code& ec,
228ac6a4445SGunnar Mills             const dbus::utility::ManagedObjectType& dbusData) {
229ac6a4445SGunnar Mills         if (ec)
230ac6a4445SGunnar Mills         {
231ac6a4445SGunnar Mills             BMCWEB_LOG_DEBUG << "DBUS response error";
232ac6a4445SGunnar Mills             messages::internalError(aResp->res);
233ac6a4445SGunnar Mills             return;
234ac6a4445SGunnar Mills         }
235ac6a4445SGunnar Mills         aResp->res.jsonValue["Id"] = cpuId;
236ac6a4445SGunnar Mills         aResp->res.jsonValue["Name"] = "Processor";
237ac6a4445SGunnar Mills         aResp->res.jsonValue["ProcessorType"] = "CPU";
238ac6a4445SGunnar Mills 
239ac6a4445SGunnar Mills         bool slotPresent = false;
240ac6a4445SGunnar Mills         std::string corePath = objPath + "/core";
241ac6a4445SGunnar Mills         size_t totalCores = 0;
242ac6a4445SGunnar Mills         for (const auto& object : dbusData)
243ac6a4445SGunnar Mills         {
244ac6a4445SGunnar Mills             if (object.first.str == objPath)
245ac6a4445SGunnar Mills             {
246ac6a4445SGunnar Mills                 getCpuDataByInterface(aResp, object.second);
247ac6a4445SGunnar Mills             }
24811ba3979SEd Tanous             else if (object.first.str.starts_with(corePath))
249ac6a4445SGunnar Mills             {
250ac6a4445SGunnar Mills                 for (const auto& interface : object.second)
251ac6a4445SGunnar Mills                 {
252002d39b4SEd Tanous                     if (interface.first == "xyz.openbmc_project.Inventory.Item")
253ac6a4445SGunnar Mills                     {
254ac6a4445SGunnar Mills                         for (const auto& property : interface.second)
255ac6a4445SGunnar Mills                         {
256ac6a4445SGunnar Mills                             if (property.first == "Present")
257ac6a4445SGunnar Mills                             {
258ac6a4445SGunnar Mills                                 const bool* present =
259ac6a4445SGunnar Mills                                     std::get_if<bool>(&property.second);
260ac6a4445SGunnar Mills                                 if (present != nullptr)
261ac6a4445SGunnar Mills                                 {
262e05aec50SEd Tanous                                     if (*present)
263ac6a4445SGunnar Mills                                     {
264ac6a4445SGunnar Mills                                         slotPresent = true;
265ac6a4445SGunnar Mills                                         totalCores++;
266ac6a4445SGunnar Mills                                     }
267ac6a4445SGunnar Mills                                 }
268ac6a4445SGunnar Mills                             }
269ac6a4445SGunnar Mills                         }
270ac6a4445SGunnar Mills                     }
271ac6a4445SGunnar Mills                 }
272ac6a4445SGunnar Mills             }
273ac6a4445SGunnar Mills         }
274ac6a4445SGunnar Mills         // In getCpuDataByInterface(), state and health are set
275ac6a4445SGunnar Mills         // based on the present and functional status. If core
276ac6a4445SGunnar Mills         // count is zero, then it has a higher precedence.
277ac6a4445SGunnar Mills         if (slotPresent)
278ac6a4445SGunnar Mills         {
279ac6a4445SGunnar Mills             if (totalCores == 0)
280ac6a4445SGunnar Mills             {
281ac6a4445SGunnar Mills                 // Slot is not populated, set status end return
282ac6a4445SGunnar Mills                 aResp->res.jsonValue["Status"]["State"] = "Absent";
283ac6a4445SGunnar Mills                 aResp->res.jsonValue["Status"]["Health"] = "OK";
284ac6a4445SGunnar Mills             }
285ac6a4445SGunnar Mills             aResp->res.jsonValue["TotalCores"] = totalCores;
286ac6a4445SGunnar Mills         }
287ac6a4445SGunnar Mills         return;
288ac6a4445SGunnar Mills         },
289ac6a4445SGunnar Mills         service, "/xyz/openbmc_project/inventory",
290ac6a4445SGunnar Mills         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
291ac6a4445SGunnar Mills }
292ac6a4445SGunnar Mills 
2938d1b46d7Szhanghch05 inline void getCpuAssetData(std::shared_ptr<bmcweb::AsyncResp> aResp,
294ac6a4445SGunnar Mills                             const std::string& service,
295ac6a4445SGunnar Mills                             const std::string& objPath)
296ac6a4445SGunnar Mills {
297ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG << "Get Cpu Asset Data";
298351053f2SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
299351053f2SKrzysztof Grobelny         *crow::connections::systemBus, service, objPath,
300351053f2SKrzysztof Grobelny         "xyz.openbmc_project.Inventory.Decorator.Asset",
301ac6a4445SGunnar Mills         [objPath, aResp{std::move(aResp)}](
302*5e7e2dc5SEd Tanous             const boost::system::error_code& ec,
303351053f2SKrzysztof Grobelny             const dbus::utility::DBusPropertiesMap& properties) {
304ac6a4445SGunnar Mills         if (ec)
305ac6a4445SGunnar Mills         {
306ac6a4445SGunnar Mills             BMCWEB_LOG_DEBUG << "DBUS response error";
307ac6a4445SGunnar Mills             messages::internalError(aResp->res);
308ac6a4445SGunnar Mills             return;
309ac6a4445SGunnar Mills         }
310ac6a4445SGunnar Mills 
311351053f2SKrzysztof Grobelny         const std::string* serialNumber = nullptr;
312351053f2SKrzysztof Grobelny         const std::string* model = nullptr;
313351053f2SKrzysztof Grobelny         const std::string* manufacturer = nullptr;
314351053f2SKrzysztof Grobelny         const std::string* partNumber = nullptr;
315351053f2SKrzysztof Grobelny         const std::string* sparePartNumber = nullptr;
316351053f2SKrzysztof Grobelny 
317351053f2SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
318351053f2SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), properties, "SerialNumber",
319351053f2SKrzysztof Grobelny             serialNumber, "Model", model, "Manufacturer", manufacturer,
320351053f2SKrzysztof Grobelny             "PartNumber", partNumber, "SparePartNumber", sparePartNumber);
321351053f2SKrzysztof Grobelny 
322351053f2SKrzysztof Grobelny         if (!success)
323ac6a4445SGunnar Mills         {
324351053f2SKrzysztof Grobelny             messages::internalError(aResp->res);
325351053f2SKrzysztof Grobelny             return;
326ac6a4445SGunnar Mills         }
327351053f2SKrzysztof Grobelny 
328351053f2SKrzysztof Grobelny         if (serialNumber != nullptr && !serialNumber->empty())
329ac6a4445SGunnar Mills         {
330351053f2SKrzysztof Grobelny             aResp->res.jsonValue["SerialNumber"] = *serialNumber;
331351053f2SKrzysztof Grobelny         }
332351053f2SKrzysztof Grobelny 
333351053f2SKrzysztof Grobelny         if ((model != nullptr) && !model->empty())
334ac6a4445SGunnar Mills         {
335ac6a4445SGunnar Mills             aResp->res.jsonValue["Model"] = *model;
336ac6a4445SGunnar Mills         }
337ac6a4445SGunnar Mills 
338351053f2SKrzysztof Grobelny         if (manufacturer != nullptr)
339ac6a4445SGunnar Mills         {
340351053f2SKrzysztof Grobelny             aResp->res.jsonValue["Manufacturer"] = *manufacturer;
341ac6a4445SGunnar Mills 
342ac6a4445SGunnar Mills             // Otherwise would be unexpected.
343351053f2SKrzysztof Grobelny             if (manufacturer->find("Intel") != std::string::npos)
344ac6a4445SGunnar Mills             {
345002d39b4SEd Tanous                 aResp->res.jsonValue["ProcessorArchitecture"] = "x86";
346ac6a4445SGunnar Mills                 aResp->res.jsonValue["InstructionSet"] = "x86-64";
347ac6a4445SGunnar Mills             }
348351053f2SKrzysztof Grobelny             else if (manufacturer->find("IBM") != std::string::npos)
349ac6a4445SGunnar Mills             {
350002d39b4SEd Tanous                 aResp->res.jsonValue["ProcessorArchitecture"] = "Power";
351ac6a4445SGunnar Mills                 aResp->res.jsonValue["InstructionSet"] = "PowerISA";
352ac6a4445SGunnar Mills             }
353ac6a4445SGunnar Mills         }
354cba4f448SSunnySrivastava1984 
355351053f2SKrzysztof Grobelny         if (partNumber != nullptr)
356cba4f448SSunnySrivastava1984         {
357cba4f448SSunnySrivastava1984             aResp->res.jsonValue["PartNumber"] = *partNumber;
358cba4f448SSunnySrivastava1984         }
359cba4f448SSunnySrivastava1984 
3606169de2cSBrad Bishop         if (sparePartNumber != nullptr && !sparePartNumber->empty())
361cba4f448SSunnySrivastava1984         {
362cba4f448SSunnySrivastava1984             aResp->res.jsonValue["SparePartNumber"] = *sparePartNumber;
363cba4f448SSunnySrivastava1984         }
364351053f2SKrzysztof Grobelny         });
365ac6a4445SGunnar Mills }
366ac6a4445SGunnar Mills 
3678d1b46d7Szhanghch05 inline void getCpuRevisionData(std::shared_ptr<bmcweb::AsyncResp> aResp,
368ac6a4445SGunnar Mills                                const std::string& service,
369ac6a4445SGunnar Mills                                const std::string& objPath)
370ac6a4445SGunnar Mills {
371ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG << "Get Cpu Revision Data";
372351053f2SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
373351053f2SKrzysztof Grobelny         *crow::connections::systemBus, service, objPath,
374351053f2SKrzysztof Grobelny         "xyz.openbmc_project.Inventory.Decorator.Revision",
375ac6a4445SGunnar Mills         [objPath, aResp{std::move(aResp)}](
376*5e7e2dc5SEd Tanous             const boost::system::error_code& ec,
377351053f2SKrzysztof Grobelny             const dbus::utility::DBusPropertiesMap& properties) {
378ac6a4445SGunnar Mills         if (ec)
379ac6a4445SGunnar Mills         {
380ac6a4445SGunnar Mills             BMCWEB_LOG_DEBUG << "DBUS response error";
381ac6a4445SGunnar Mills             messages::internalError(aResp->res);
382ac6a4445SGunnar Mills             return;
383ac6a4445SGunnar Mills         }
384ac6a4445SGunnar Mills 
385351053f2SKrzysztof Grobelny         const std::string* version = nullptr;
386351053f2SKrzysztof Grobelny 
387351053f2SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
388351053f2SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), properties, "Version", version);
389351053f2SKrzysztof Grobelny 
390351053f2SKrzysztof Grobelny         if (!success)
391ac6a4445SGunnar Mills         {
392351053f2SKrzysztof Grobelny             messages::internalError(aResp->res);
393351053f2SKrzysztof Grobelny             return;
394351053f2SKrzysztof Grobelny         }
395351053f2SKrzysztof Grobelny 
396351053f2SKrzysztof Grobelny         if (version != nullptr)
397ac6a4445SGunnar Mills         {
398351053f2SKrzysztof Grobelny             aResp->res.jsonValue["Version"] = *version;
399ac6a4445SGunnar Mills         }
400351053f2SKrzysztof Grobelny         });
401ac6a4445SGunnar Mills }
402ac6a4445SGunnar Mills 
4038d1b46d7Szhanghch05 inline void getAcceleratorDataByService(
4048d1b46d7Szhanghch05     std::shared_ptr<bmcweb::AsyncResp> aResp, const std::string& acclrtrId,
4058d1b46d7Szhanghch05     const std::string& service, const std::string& objPath)
406ac6a4445SGunnar Mills {
407ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG
408ac6a4445SGunnar Mills         << "Get available system Accelerator resources by service.";
409351053f2SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
410351053f2SKrzysztof Grobelny         *crow::connections::systemBus, service, objPath, "",
411ac6a4445SGunnar Mills         [acclrtrId, aResp{std::move(aResp)}](
412*5e7e2dc5SEd Tanous             const boost::system::error_code& ec,
413351053f2SKrzysztof Grobelny             const dbus::utility::DBusPropertiesMap& properties) {
414ac6a4445SGunnar Mills         if (ec)
415ac6a4445SGunnar Mills         {
416ac6a4445SGunnar Mills             BMCWEB_LOG_DEBUG << "DBUS response error";
417ac6a4445SGunnar Mills             messages::internalError(aResp->res);
418ac6a4445SGunnar Mills             return;
419ac6a4445SGunnar Mills         }
420ac6a4445SGunnar Mills 
421351053f2SKrzysztof Grobelny         const bool* functional = nullptr;
422351053f2SKrzysztof Grobelny         const bool* present = nullptr;
423351053f2SKrzysztof Grobelny 
424351053f2SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
425351053f2SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), properties, "Functional",
426351053f2SKrzysztof Grobelny             functional, "Present", present);
427351053f2SKrzysztof Grobelny 
428351053f2SKrzysztof Grobelny         if (!success)
429ac6a4445SGunnar Mills         {
430351053f2SKrzysztof Grobelny             messages::internalError(aResp->res);
431351053f2SKrzysztof Grobelny             return;
432ac6a4445SGunnar Mills         }
433ac6a4445SGunnar Mills 
434ac6a4445SGunnar Mills         std::string state = "Enabled";
435ac6a4445SGunnar Mills         std::string health = "OK";
436ac6a4445SGunnar Mills 
437351053f2SKrzysztof Grobelny         if (present != nullptr && !*present)
438ac6a4445SGunnar Mills         {
439ac6a4445SGunnar Mills             state = "Absent";
440ac6a4445SGunnar Mills         }
441ac6a4445SGunnar Mills 
442351053f2SKrzysztof Grobelny         if (functional != nullptr && !*functional)
443ac6a4445SGunnar Mills         {
444ac6a4445SGunnar Mills             if (state == "Enabled")
445ac6a4445SGunnar Mills             {
446ac6a4445SGunnar Mills                 health = "Critical";
447ac6a4445SGunnar Mills             }
448ac6a4445SGunnar Mills         }
449ac6a4445SGunnar Mills 
450351053f2SKrzysztof Grobelny         aResp->res.jsonValue["Id"] = acclrtrId;
451351053f2SKrzysztof Grobelny         aResp->res.jsonValue["Name"] = "Processor";
452ac6a4445SGunnar Mills         aResp->res.jsonValue["Status"]["State"] = state;
453ac6a4445SGunnar Mills         aResp->res.jsonValue["Status"]["Health"] = health;
454ac6a4445SGunnar Mills         aResp->res.jsonValue["ProcessorType"] = "Accelerator";
455351053f2SKrzysztof Grobelny         });
456ac6a4445SGunnar Mills }
457ac6a4445SGunnar Mills 
458dba0c291SJonathan Doman // OperatingConfig D-Bus Types
459dba0c291SJonathan Doman using TurboProfileProperty = std::vector<std::tuple<uint32_t, size_t>>;
460dba0c291SJonathan Doman using BaseSpeedPrioritySettingsProperty =
461dba0c291SJonathan Doman     std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>;
462dba0c291SJonathan Doman // uint32_t and size_t may or may not be the same type, requiring a dedup'd
463dba0c291SJonathan Doman // variant
464dba0c291SJonathan Doman 
465dba0c291SJonathan Doman /**
466dba0c291SJonathan Doman  * Fill out the HighSpeedCoreIDs in a Processor resource from the given
467dba0c291SJonathan Doman  * OperatingConfig D-Bus property.
468dba0c291SJonathan Doman  *
469dba0c291SJonathan Doman  * @param[in,out]   aResp               Async HTTP response.
470dba0c291SJonathan Doman  * @param[in]       baseSpeedSettings   Full list of base speed priority groups,
471dba0c291SJonathan Doman  *                                      to use to determine the list of high
472dba0c291SJonathan Doman  *                                      speed cores.
473dba0c291SJonathan Doman  */
474dba0c291SJonathan Doman inline void highSpeedCoreIdsHandler(
4758d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
476dba0c291SJonathan Doman     const BaseSpeedPrioritySettingsProperty& baseSpeedSettings)
477dba0c291SJonathan Doman {
478dba0c291SJonathan Doman     // The D-Bus property does not indicate which bucket is the "high
479dba0c291SJonathan Doman     // priority" group, so let's discern that by looking for the one with
480dba0c291SJonathan Doman     // highest base frequency.
481dba0c291SJonathan Doman     auto highPriorityGroup = baseSpeedSettings.cend();
482dba0c291SJonathan Doman     uint32_t highestBaseSpeed = 0;
483dba0c291SJonathan Doman     for (auto it = baseSpeedSettings.cbegin(); it != baseSpeedSettings.cend();
484dba0c291SJonathan Doman          ++it)
485dba0c291SJonathan Doman     {
486dba0c291SJonathan Doman         const uint32_t baseFreq = std::get<uint32_t>(*it);
487dba0c291SJonathan Doman         if (baseFreq > highestBaseSpeed)
488dba0c291SJonathan Doman         {
489dba0c291SJonathan Doman             highestBaseSpeed = baseFreq;
490dba0c291SJonathan Doman             highPriorityGroup = it;
491dba0c291SJonathan Doman         }
492dba0c291SJonathan Doman     }
493dba0c291SJonathan Doman 
494dba0c291SJonathan Doman     nlohmann::json& jsonCoreIds = aResp->res.jsonValue["HighSpeedCoreIDs"];
495dba0c291SJonathan Doman     jsonCoreIds = nlohmann::json::array();
496dba0c291SJonathan Doman 
497dba0c291SJonathan Doman     // There may not be any entries in the D-Bus property, so only populate
498dba0c291SJonathan Doman     // if there was actually something there.
499dba0c291SJonathan Doman     if (highPriorityGroup != baseSpeedSettings.cend())
500dba0c291SJonathan Doman     {
501dba0c291SJonathan Doman         jsonCoreIds = std::get<std::vector<uint32_t>>(*highPriorityGroup);
502dba0c291SJonathan Doman     }
503dba0c291SJonathan Doman }
504dba0c291SJonathan Doman 
505dba0c291SJonathan Doman /**
506dba0c291SJonathan Doman  * Fill out OperatingConfig related items in a Processor resource by requesting
507dba0c291SJonathan Doman  * data from the given D-Bus object.
508dba0c291SJonathan Doman  *
509dba0c291SJonathan Doman  * @param[in,out]   aResp       Async HTTP response.
510dba0c291SJonathan Doman  * @param[in]       cpuId       CPU D-Bus name.
511dba0c291SJonathan Doman  * @param[in]       service     D-Bus service to query.
512dba0c291SJonathan Doman  * @param[in]       objPath     D-Bus object to query.
513dba0c291SJonathan Doman  */
5148d1b46d7Szhanghch05 inline void getCpuConfigData(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
515dba0c291SJonathan Doman                              const std::string& cpuId,
516dba0c291SJonathan Doman                              const std::string& service,
517dba0c291SJonathan Doman                              const std::string& objPath)
518dba0c291SJonathan Doman {
519dba0c291SJonathan Doman     BMCWEB_LOG_INFO << "Getting CPU operating configs for " << cpuId;
520dba0c291SJonathan Doman 
521dba0c291SJonathan Doman     // First, GetAll CurrentOperatingConfig properties on the object
522351053f2SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
523351053f2SKrzysztof Grobelny         *crow::connections::systemBus, service, objPath,
524351053f2SKrzysztof Grobelny         "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig",
525351053f2SKrzysztof Grobelny         [aResp, cpuId,
526*5e7e2dc5SEd Tanous          service](const boost::system::error_code& ec,
527351053f2SKrzysztof Grobelny                   const dbus::utility::DBusPropertiesMap& properties) {
528dba0c291SJonathan Doman         if (ec)
529dba0c291SJonathan Doman         {
530002d39b4SEd Tanous             BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", " << ec.message();
531dba0c291SJonathan Doman             messages::internalError(aResp->res);
532dba0c291SJonathan Doman             return;
533dba0c291SJonathan Doman         }
534dba0c291SJonathan Doman 
535dba0c291SJonathan Doman         nlohmann::json& json = aResp->res.jsonValue;
536dba0c291SJonathan Doman 
537351053f2SKrzysztof Grobelny         const sdbusplus::message::object_path* appliedConfig = nullptr;
538351053f2SKrzysztof Grobelny         const bool* baseSpeedPriorityEnabled = nullptr;
539351053f2SKrzysztof Grobelny 
540351053f2SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
541351053f2SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), properties, "AppliedConfig",
542351053f2SKrzysztof Grobelny             appliedConfig, "BaseSpeedPriorityEnabled",
543351053f2SKrzysztof Grobelny             baseSpeedPriorityEnabled);
544351053f2SKrzysztof Grobelny 
545351053f2SKrzysztof Grobelny         if (!success)
546dba0c291SJonathan Doman         {
547351053f2SKrzysztof Grobelny             messages::internalError(aResp->res);
548351053f2SKrzysztof Grobelny             return;
549dba0c291SJonathan Doman         }
550dba0c291SJonathan Doman 
551351053f2SKrzysztof Grobelny         if (appliedConfig != nullptr)
552351053f2SKrzysztof Grobelny         {
553351053f2SKrzysztof Grobelny             const std::string& dbusPath = appliedConfig->str;
5541476687dSEd Tanous             nlohmann::json::object_t operatingConfig;
555eddfc437SWilly Tu             operatingConfig["@odata.id"] = crow::utility::urlFromPieces(
556eddfc437SWilly Tu                 "redfish", "v1", "Systems", "system", "Processors", cpuId,
557eddfc437SWilly Tu                 "OperatingConfigs");
5581476687dSEd Tanous             json["OperatingConfigs"] = std::move(operatingConfig);
559dba0c291SJonathan Doman 
560dba0c291SJonathan Doman             // Reuse the D-Bus config object name for the Redfish
561dba0c291SJonathan Doman             // URI
562dba0c291SJonathan Doman             size_t baseNamePos = dbusPath.rfind('/');
563dba0c291SJonathan Doman             if (baseNamePos == std::string::npos ||
564dba0c291SJonathan Doman                 baseNamePos == (dbusPath.size() - 1))
565dba0c291SJonathan Doman             {
566dba0c291SJonathan Doman                 // If the AppliedConfig was somehow not a valid path,
567dba0c291SJonathan Doman                 // skip adding any more properties, since everything
568dba0c291SJonathan Doman                 // else is tied to this applied config.
569dba0c291SJonathan Doman                 messages::internalError(aResp->res);
570351053f2SKrzysztof Grobelny                 return;
571dba0c291SJonathan Doman             }
5721476687dSEd Tanous             nlohmann::json::object_t appliedOperatingConfig;
573eddfc437SWilly Tu             appliedOperatingConfig["@odata.id"] = crow::utility::urlFromPieces(
574eddfc437SWilly Tu                 "redfish", "v1", "Systems", "system", "Processors", cpuId,
575eddfc437SWilly Tu                 "OperatingConfigs", dbusPath.substr(baseNamePos + 1));
576351053f2SKrzysztof Grobelny             json["AppliedOperatingConfig"] = std::move(appliedOperatingConfig);
577dba0c291SJonathan Doman 
578dba0c291SJonathan Doman             // Once we found the current applied config, queue another
579dba0c291SJonathan Doman             // request to read the base freq core ids out of that
580dba0c291SJonathan Doman             // config.
581002d39b4SEd Tanous             sdbusplus::asio::getProperty<BaseSpeedPrioritySettingsProperty>(
5821e1e598dSJonathan Doman                 *crow::connections::systemBus, service, dbusPath,
5831e1e598dSJonathan Doman                 "xyz.openbmc_project.Inventory.Item.Cpu."
5841e1e598dSJonathan Doman                 "OperatingConfig",
5851e1e598dSJonathan Doman                 "BaseSpeedPrioritySettings",
586351053f2SKrzysztof Grobelny                 [aResp](
587*5e7e2dc5SEd Tanous                     const boost::system::error_code& ec2,
588351053f2SKrzysztof Grobelny                     const BaseSpeedPrioritySettingsProperty& baseSpeedList) {
5898a592810SEd Tanous                 if (ec2)
590dba0c291SJonathan Doman                 {
591351053f2SKrzysztof Grobelny                     BMCWEB_LOG_WARNING << "D-Bus Property Get error: " << ec2;
592dba0c291SJonathan Doman                     messages::internalError(aResp->res);
593dba0c291SJonathan Doman                     return;
594dba0c291SJonathan Doman                 }
5951e1e598dSJonathan Doman 
5961e1e598dSJonathan Doman                 highSpeedCoreIdsHandler(aResp, baseSpeedList);
5971e1e598dSJonathan Doman                 });
598dba0c291SJonathan Doman         }
599351053f2SKrzysztof Grobelny 
600351053f2SKrzysztof Grobelny         if (baseSpeedPriorityEnabled != nullptr)
601dba0c291SJonathan Doman         {
602dba0c291SJonathan Doman             json["BaseSpeedPriorityState"] =
603351053f2SKrzysztof Grobelny                 *baseSpeedPriorityEnabled ? "Enabled" : "Disabled";
604dba0c291SJonathan Doman         }
605351053f2SKrzysztof Grobelny         });
606dba0c291SJonathan Doman }
607dba0c291SJonathan Doman 
608cba4f448SSunnySrivastava1984 /**
609cba4f448SSunnySrivastava1984  * @brief Fill out location info of a processor by
610cba4f448SSunnySrivastava1984  * requesting data from the given D-Bus object.
611cba4f448SSunnySrivastava1984  *
612cba4f448SSunnySrivastava1984  * @param[in,out]   aResp       Async HTTP response.
613cba4f448SSunnySrivastava1984  * @param[in]       service     D-Bus service to query.
614cba4f448SSunnySrivastava1984  * @param[in]       objPath     D-Bus object to query.
615cba4f448SSunnySrivastava1984  */
6168d1b46d7Szhanghch05 inline void getCpuLocationCode(std::shared_ptr<bmcweb::AsyncResp> aResp,
617cba4f448SSunnySrivastava1984                                const std::string& service,
618cba4f448SSunnySrivastava1984                                const std::string& objPath)
619cba4f448SSunnySrivastava1984 {
620cba4f448SSunnySrivastava1984     BMCWEB_LOG_DEBUG << "Get Cpu Location Data";
6211e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
6221e1e598dSJonathan Doman         *crow::connections::systemBus, service, objPath,
6231e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
624*5e7e2dc5SEd Tanous         [objPath, aResp{std::move(aResp)}](const boost::system::error_code& ec,
6251e1e598dSJonathan Doman                                            const std::string& property) {
626cba4f448SSunnySrivastava1984         if (ec)
627cba4f448SSunnySrivastava1984         {
628cba4f448SSunnySrivastava1984             BMCWEB_LOG_DEBUG << "DBUS response error";
629cba4f448SSunnySrivastava1984             messages::internalError(aResp->res);
630cba4f448SSunnySrivastava1984             return;
631cba4f448SSunnySrivastava1984         }
632cba4f448SSunnySrivastava1984 
633cba4f448SSunnySrivastava1984         aResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
6341e1e598dSJonathan Doman             property;
6351e1e598dSJonathan Doman         });
636cba4f448SSunnySrivastava1984 }
637cba4f448SSunnySrivastava1984 
638c951448aSJonathan Doman /**
63949e429caSJonathan Doman  * Populate the unique identifier in a Processor resource by requesting data
64049e429caSJonathan Doman  * from the given D-Bus object.
64149e429caSJonathan Doman  *
64249e429caSJonathan Doman  * @param[in,out]   aResp       Async HTTP response.
64349e429caSJonathan Doman  * @param[in]       service     D-Bus service to query.
64449e429caSJonathan Doman  * @param[in]       objPath     D-Bus object to query.
64549e429caSJonathan Doman  */
64649e429caSJonathan Doman inline void getCpuUniqueId(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
64749e429caSJonathan Doman                            const std::string& service,
64849e429caSJonathan Doman                            const std::string& objectPath)
64949e429caSJonathan Doman {
65049e429caSJonathan Doman     BMCWEB_LOG_DEBUG << "Get CPU UniqueIdentifier";
6511e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
6521e1e598dSJonathan Doman         *crow::connections::systemBus, service, objectPath,
6531e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Decorator.UniqueIdentifier",
6541e1e598dSJonathan Doman         "UniqueIdentifier",
655*5e7e2dc5SEd Tanous         [aResp](const boost::system::error_code& ec, const std::string& id) {
6561e1e598dSJonathan Doman         if (ec)
65749e429caSJonathan Doman         {
65849e429caSJonathan Doman             BMCWEB_LOG_ERROR << "Failed to read cpu unique id: " << ec;
65949e429caSJonathan Doman             messages::internalError(aResp->res);
66049e429caSJonathan Doman             return;
66149e429caSJonathan Doman         }
662002d39b4SEd Tanous         aResp->res.jsonValue["ProcessorId"]["ProtectedIdentificationNumber"] =
663002d39b4SEd Tanous             id;
6641e1e598dSJonathan Doman         });
66549e429caSJonathan Doman }
66649e429caSJonathan Doman 
66749e429caSJonathan Doman /**
668c951448aSJonathan Doman  * Find the D-Bus object representing the requested Processor, and call the
669c951448aSJonathan Doman  * handler with the results. If matching object is not found, add 404 error to
670c951448aSJonathan Doman  * response and don't call the handler.
671c951448aSJonathan Doman  *
672c951448aSJonathan Doman  * @param[in,out]   resp            Async HTTP response.
673c951448aSJonathan Doman  * @param[in]       processorId     Redfish Processor Id.
674c951448aSJonathan Doman  * @param[in]       handler         Callback to continue processing request upon
675c951448aSJonathan Doman  *                                  successfully finding object.
676c951448aSJonathan Doman  */
677c951448aSJonathan Doman template <typename Handler>
6788d1b46d7Szhanghch05 inline void getProcessorObject(const std::shared_ptr<bmcweb::AsyncResp>& resp,
679c951448aSJonathan Doman                                const std::string& processorId,
680c951448aSJonathan Doman                                Handler&& handler)
681ac6a4445SGunnar Mills {
682ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG << "Get available system processor resources.";
683ac6a4445SGunnar Mills 
684c951448aSJonathan Doman     // GetSubTree on all interfaces which provide info about a Processor
685e99073f5SGeorge Liu     constexpr std::array<std::string_view, 8> interfaces = {
686e99073f5SGeorge Liu         "xyz.openbmc_project.Common.UUID",
687e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Decorator.Asset",
688e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Decorator.Revision",
689e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Cpu",
690e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Decorator.LocationCode",
691e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Item.Accelerator",
692e99073f5SGeorge Liu         "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig",
693e99073f5SGeorge Liu         "xyz.openbmc_project.Inventory.Decorator.UniqueIdentifier"};
694e99073f5SGeorge Liu     dbus::utility::getSubTree(
695e99073f5SGeorge Liu         "/xyz/openbmc_project/inventory", 0, interfaces,
696c951448aSJonathan Doman         [resp, processorId, handler = std::forward<Handler>(handler)](
697e99073f5SGeorge Liu             const boost::system::error_code& ec,
698e99073f5SGeorge Liu             const dbus::utility::MapperGetSubTreeResponse& subtree) {
699ac6a4445SGunnar Mills         if (ec)
700ac6a4445SGunnar Mills         {
701c951448aSJonathan Doman             BMCWEB_LOG_DEBUG << "DBUS response error: " << ec;
702c951448aSJonathan Doman             messages::internalError(resp->res);
703ac6a4445SGunnar Mills             return;
704ac6a4445SGunnar Mills         }
7052bab9831SJonathan Doman         for (const auto& [objectPath, serviceMap] : subtree)
706ac6a4445SGunnar Mills         {
7072bab9831SJonathan Doman             // Ignore any objects which don't end with our desired cpu name
70811ba3979SEd Tanous             if (!objectPath.ends_with(processorId))
709ac6a4445SGunnar Mills             {
7102bab9831SJonathan Doman                 continue;
711ac6a4445SGunnar Mills             }
7122bab9831SJonathan Doman 
713c951448aSJonathan Doman             bool found = false;
714c951448aSJonathan Doman             // Filter out objects that don't have the CPU-specific
715c951448aSJonathan Doman             // interfaces to make sure we can return 404 on non-CPUs
716c951448aSJonathan Doman             // (e.g. /redfish/../Processors/dimm0)
7172bab9831SJonathan Doman             for (const auto& [serviceName, interfaceList] : serviceMap)
718ac6a4445SGunnar Mills             {
719c951448aSJonathan Doman                 if (std::find_first_of(
720c951448aSJonathan Doman                         interfaceList.begin(), interfaceList.end(),
721c951448aSJonathan Doman                         processorInterfaces.begin(),
722c951448aSJonathan Doman                         processorInterfaces.end()) != interfaceList.end())
7232bab9831SJonathan Doman                 {
724c951448aSJonathan Doman                     found = true;
725c951448aSJonathan Doman                     break;
726c951448aSJonathan Doman                 }
727c951448aSJonathan Doman             }
728c951448aSJonathan Doman 
729c951448aSJonathan Doman             if (!found)
7302bab9831SJonathan Doman             {
731c951448aSJonathan Doman                 continue;
732ac6a4445SGunnar Mills             }
733c951448aSJonathan Doman 
734c951448aSJonathan Doman             // Process the first object which does match our cpu name and
735c951448aSJonathan Doman             // required interfaces, and potentially ignore any other
736c951448aSJonathan Doman             // matching objects. Assume all interfaces we want to process
737c951448aSJonathan Doman             // must be on the same object path.
738c951448aSJonathan Doman 
7398a592810SEd Tanous             handler(objectPath, serviceMap);
740ac6a4445SGunnar Mills             return;
741ac6a4445SGunnar Mills         }
742c951448aSJonathan Doman         messages::resourceNotFound(resp->res, "Processor", processorId);
743e99073f5SGeorge Liu         });
744ac6a4445SGunnar Mills }
745ac6a4445SGunnar Mills 
7468d1b46d7Szhanghch05 inline void getProcessorData(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
747c951448aSJonathan Doman                              const std::string& processorId,
748c951448aSJonathan Doman                              const std::string& objectPath,
7495df6eda2SShantappa Teekappanavar                              const dbus::utility::MapperServiceMap& serviceMap)
750c951448aSJonathan Doman {
751c951448aSJonathan Doman     for (const auto& [serviceName, interfaceList] : serviceMap)
752c951448aSJonathan Doman     {
753c951448aSJonathan Doman         for (const auto& interface : interfaceList)
754c951448aSJonathan Doman         {
755c951448aSJonathan Doman             if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
756c951448aSJonathan Doman             {
757c951448aSJonathan Doman                 getCpuAssetData(aResp, serviceName, objectPath);
758c951448aSJonathan Doman             }
7590fda0f12SGeorge Liu             else if (interface ==
7600fda0f12SGeorge Liu                      "xyz.openbmc_project.Inventory.Decorator.Revision")
761c951448aSJonathan Doman             {
762c951448aSJonathan Doman                 getCpuRevisionData(aResp, serviceName, objectPath);
763c951448aSJonathan Doman             }
764c951448aSJonathan Doman             else if (interface == "xyz.openbmc_project.Inventory.Item.Cpu")
765c951448aSJonathan Doman             {
766c951448aSJonathan Doman                 getCpuDataByService(aResp, processorId, serviceName,
767c951448aSJonathan Doman                                     objectPath);
768c951448aSJonathan Doman             }
7690fda0f12SGeorge Liu             else if (interface ==
7700fda0f12SGeorge Liu                      "xyz.openbmc_project.Inventory.Item.Accelerator")
771c951448aSJonathan Doman             {
772c951448aSJonathan Doman                 getAcceleratorDataByService(aResp, processorId, serviceName,
773c951448aSJonathan Doman                                             objectPath);
774c951448aSJonathan Doman             }
7750fda0f12SGeorge Liu             else if (
7760fda0f12SGeorge Liu                 interface ==
7770fda0f12SGeorge Liu                 "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig")
778c951448aSJonathan Doman             {
779c951448aSJonathan Doman                 getCpuConfigData(aResp, processorId, serviceName, objectPath);
780c951448aSJonathan Doman             }
7810fda0f12SGeorge Liu             else if (interface ==
7820fda0f12SGeorge Liu                      "xyz.openbmc_project.Inventory.Decorator.LocationCode")
783c951448aSJonathan Doman             {
784c951448aSJonathan Doman                 getCpuLocationCode(aResp, serviceName, objectPath);
785c951448aSJonathan Doman             }
78671b82f26SSharad Yadav             else if (interface == "xyz.openbmc_project.Common.UUID")
78771b82f26SSharad Yadav             {
78871b82f26SSharad Yadav                 getProcessorUUID(aResp, serviceName, objectPath);
78971b82f26SSharad Yadav             }
7900fda0f12SGeorge Liu             else if (interface ==
7910fda0f12SGeorge Liu                      "xyz.openbmc_project.Inventory.Decorator.UniqueIdentifier")
79249e429caSJonathan Doman             {
79349e429caSJonathan Doman                 getCpuUniqueId(aResp, serviceName, objectPath);
79449e429caSJonathan Doman             }
795c951448aSJonathan Doman         }
796c951448aSJonathan Doman     }
797c951448aSJonathan Doman }
798c951448aSJonathan Doman 
799dba0c291SJonathan Doman /**
800dba0c291SJonathan Doman  * Request all the properties for the given D-Bus object and fill out the
801dba0c291SJonathan Doman  * related entries in the Redfish OperatingConfig response.
802dba0c291SJonathan Doman  *
803dba0c291SJonathan Doman  * @param[in,out]   aResp       Async HTTP response.
804dba0c291SJonathan Doman  * @param[in]       service     D-Bus service name to query.
805dba0c291SJonathan Doman  * @param[in]       objPath     D-Bus object to query.
806dba0c291SJonathan Doman  */
8078d1b46d7Szhanghch05 inline void
8088d1b46d7Szhanghch05     getOperatingConfigData(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
809dba0c291SJonathan Doman                            const std::string& service,
810dba0c291SJonathan Doman                            const std::string& objPath)
811dba0c291SJonathan Doman {
812351053f2SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
813351053f2SKrzysztof Grobelny         *crow::connections::systemBus, service, objPath,
814351053f2SKrzysztof Grobelny         "xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig",
815*5e7e2dc5SEd Tanous         [aResp](const boost::system::error_code& ec,
816351053f2SKrzysztof Grobelny                 const dbus::utility::DBusPropertiesMap& properties) {
817dba0c291SJonathan Doman         if (ec)
818dba0c291SJonathan Doman         {
819002d39b4SEd Tanous             BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", " << ec.message();
820dba0c291SJonathan Doman             messages::internalError(aResp->res);
821dba0c291SJonathan Doman             return;
822dba0c291SJonathan Doman         }
823dba0c291SJonathan Doman 
824351053f2SKrzysztof Grobelny         const size_t* availableCoreCount = nullptr;
825351053f2SKrzysztof Grobelny         const uint32_t* baseSpeed = nullptr;
826351053f2SKrzysztof Grobelny         const uint32_t* maxJunctionTemperature = nullptr;
827351053f2SKrzysztof Grobelny         const uint32_t* maxSpeed = nullptr;
828351053f2SKrzysztof Grobelny         const uint32_t* powerLimit = nullptr;
829351053f2SKrzysztof Grobelny         const TurboProfileProperty* turboProfile = nullptr;
830351053f2SKrzysztof Grobelny         const BaseSpeedPrioritySettingsProperty* baseSpeedPrioritySettings =
831351053f2SKrzysztof Grobelny             nullptr;
832351053f2SKrzysztof Grobelny 
833351053f2SKrzysztof Grobelny         const bool success = sdbusplus::unpackPropertiesNoThrow(
834351053f2SKrzysztof Grobelny             dbus_utils::UnpackErrorPrinter(), properties, "AvailableCoreCount",
835351053f2SKrzysztof Grobelny             availableCoreCount, "BaseSpeed", baseSpeed,
836351053f2SKrzysztof Grobelny             "MaxJunctionTemperature", maxJunctionTemperature, "MaxSpeed",
837351053f2SKrzysztof Grobelny             maxSpeed, "PowerLimit", powerLimit, "TurboProfile", turboProfile,
838351053f2SKrzysztof Grobelny             "BaseSpeedPrioritySettings", baseSpeedPrioritySettings);
839351053f2SKrzysztof Grobelny 
840351053f2SKrzysztof Grobelny         if (!success)
841dba0c291SJonathan Doman         {
842351053f2SKrzysztof Grobelny             messages::internalError(aResp->res);
843351053f2SKrzysztof Grobelny             return;
844dba0c291SJonathan Doman         }
845dba0c291SJonathan Doman 
846351053f2SKrzysztof Grobelny         nlohmann::json& json = aResp->res.jsonValue;
847351053f2SKrzysztof Grobelny 
848351053f2SKrzysztof Grobelny         if (availableCoreCount != nullptr)
849351053f2SKrzysztof Grobelny         {
850351053f2SKrzysztof Grobelny             json["TotalAvailableCoreCount"] = *availableCoreCount;
851351053f2SKrzysztof Grobelny         }
852351053f2SKrzysztof Grobelny 
853351053f2SKrzysztof Grobelny         if (baseSpeed != nullptr)
854351053f2SKrzysztof Grobelny         {
855351053f2SKrzysztof Grobelny             json["BaseSpeedMHz"] = *baseSpeed;
856351053f2SKrzysztof Grobelny         }
857351053f2SKrzysztof Grobelny 
858351053f2SKrzysztof Grobelny         if (maxJunctionTemperature != nullptr)
859351053f2SKrzysztof Grobelny         {
860351053f2SKrzysztof Grobelny             json["MaxJunctionTemperatureCelsius"] = *maxJunctionTemperature;
861351053f2SKrzysztof Grobelny         }
862351053f2SKrzysztof Grobelny 
863351053f2SKrzysztof Grobelny         if (maxSpeed != nullptr)
864351053f2SKrzysztof Grobelny         {
865351053f2SKrzysztof Grobelny             json["MaxSpeedMHz"] = *maxSpeed;
866351053f2SKrzysztof Grobelny         }
867351053f2SKrzysztof Grobelny 
868351053f2SKrzysztof Grobelny         if (powerLimit != nullptr)
869351053f2SKrzysztof Grobelny         {
870351053f2SKrzysztof Grobelny             json["TDPWatts"] = *powerLimit;
871351053f2SKrzysztof Grobelny         }
872351053f2SKrzysztof Grobelny 
873351053f2SKrzysztof Grobelny         if (turboProfile != nullptr)
874351053f2SKrzysztof Grobelny         {
875dba0c291SJonathan Doman             nlohmann::json& turboArray = json["TurboProfile"];
876dba0c291SJonathan Doman             turboArray = nlohmann::json::array();
877351053f2SKrzysztof Grobelny             for (const auto& [turboSpeed, coreCount] : *turboProfile)
878dba0c291SJonathan Doman             {
8791476687dSEd Tanous                 nlohmann::json::object_t turbo;
8801476687dSEd Tanous                 turbo["ActiveCoreCount"] = coreCount;
8811476687dSEd Tanous                 turbo["MaxSpeedMHz"] = turboSpeed;
8821476687dSEd Tanous                 turboArray.push_back(std::move(turbo));
883dba0c291SJonathan Doman             }
884dba0c291SJonathan Doman         }
885dba0c291SJonathan Doman 
886351053f2SKrzysztof Grobelny         if (baseSpeedPrioritySettings != nullptr)
887351053f2SKrzysztof Grobelny         {
888351053f2SKrzysztof Grobelny             nlohmann::json& baseSpeedArray = json["BaseSpeedPrioritySettings"];
889dba0c291SJonathan Doman             baseSpeedArray = nlohmann::json::array();
890351053f2SKrzysztof Grobelny             for (const auto& [baseSpeedMhz, coreList] :
891351053f2SKrzysztof Grobelny                  *baseSpeedPrioritySettings)
892dba0c291SJonathan Doman             {
8931476687dSEd Tanous                 nlohmann::json::object_t speed;
8941476687dSEd Tanous                 speed["CoreCount"] = coreList.size();
8951476687dSEd Tanous                 speed["CoreIDs"] = coreList;
896351053f2SKrzysztof Grobelny                 speed["BaseSpeedMHz"] = baseSpeedMhz;
8971476687dSEd Tanous                 baseSpeedArray.push_back(std::move(speed));
898dba0c291SJonathan Doman             }
899dba0c291SJonathan Doman         }
900351053f2SKrzysztof Grobelny         });
901dba0c291SJonathan Doman }
902dba0c291SJonathan Doman 
9033cde86f1SJonathan Doman /**
9043cde86f1SJonathan Doman  * Handle the D-Bus response from attempting to set the CPU's AppliedConfig
9053cde86f1SJonathan Doman  * property. Main task is to translate error messages into Redfish errors.
9063cde86f1SJonathan Doman  *
9073cde86f1SJonathan Doman  * @param[in,out]   resp    HTTP response.
9083cde86f1SJonathan Doman  * @param[in]       setPropVal  Value which we attempted to set.
9093cde86f1SJonathan Doman  * @param[in]       ec      D-Bus response error code.
9103cde86f1SJonathan Doman  * @param[in]       msg     D-Bus response message.
9113cde86f1SJonathan Doman  */
9123cde86f1SJonathan Doman inline void
9133cde86f1SJonathan Doman     handleAppliedConfigResponse(const std::shared_ptr<bmcweb::AsyncResp>& resp,
9143cde86f1SJonathan Doman                                 const std::string& setPropVal,
915*5e7e2dc5SEd Tanous                                 const boost::system::error_code& ec,
91659d494eeSPatrick Williams                                 const sdbusplus::message_t& msg)
9173cde86f1SJonathan Doman {
9183cde86f1SJonathan Doman     if (!ec)
9193cde86f1SJonathan Doman     {
9203cde86f1SJonathan Doman         BMCWEB_LOG_DEBUG << "Set Property succeeded";
9213cde86f1SJonathan Doman         return;
9223cde86f1SJonathan Doman     }
9233cde86f1SJonathan Doman 
9243cde86f1SJonathan Doman     BMCWEB_LOG_DEBUG << "Set Property failed: " << ec;
9253cde86f1SJonathan Doman 
9263cde86f1SJonathan Doman     const sd_bus_error* dbusError = msg.get_error();
9273cde86f1SJonathan Doman     if (dbusError == nullptr)
9283cde86f1SJonathan Doman     {
9293cde86f1SJonathan Doman         messages::internalError(resp->res);
9303cde86f1SJonathan Doman         return;
9313cde86f1SJonathan Doman     }
9323cde86f1SJonathan Doman 
9333cde86f1SJonathan Doman     // The asio error code doesn't know about our custom errors, so we have to
9343cde86f1SJonathan Doman     // parse the error string. Some of these D-Bus -> Redfish translations are a
9353cde86f1SJonathan Doman     // stretch, but it's good to try to communicate something vaguely useful.
9363cde86f1SJonathan Doman     if (strcmp(dbusError->name,
9373cde86f1SJonathan Doman                "xyz.openbmc_project.Common.Error.InvalidArgument") == 0)
9383cde86f1SJonathan Doman     {
9393cde86f1SJonathan Doman         // Service did not like the object_path we tried to set.
9403cde86f1SJonathan Doman         messages::propertyValueIncorrect(
9413cde86f1SJonathan Doman             resp->res, "AppliedOperatingConfig/@odata.id", setPropVal);
9423cde86f1SJonathan Doman     }
9433cde86f1SJonathan Doman     else if (strcmp(dbusError->name,
9443cde86f1SJonathan Doman                     "xyz.openbmc_project.Common.Error.NotAllowed") == 0)
9453cde86f1SJonathan Doman     {
9463cde86f1SJonathan Doman         // Service indicates we can never change the config for this processor.
9473cde86f1SJonathan Doman         messages::propertyNotWritable(resp->res, "AppliedOperatingConfig");
9483cde86f1SJonathan Doman     }
9493cde86f1SJonathan Doman     else if (strcmp(dbusError->name,
9503cde86f1SJonathan Doman                     "xyz.openbmc_project.Common.Error.Unavailable") == 0)
9513cde86f1SJonathan Doman     {
9523cde86f1SJonathan Doman         // Service indicates the config cannot be changed right now, but maybe
9533cde86f1SJonathan Doman         // in a different system state.
9543cde86f1SJonathan Doman         messages::resourceInStandby(resp->res);
9553cde86f1SJonathan Doman     }
9563cde86f1SJonathan Doman     else
9573cde86f1SJonathan Doman     {
9583cde86f1SJonathan Doman         messages::internalError(resp->res);
9593cde86f1SJonathan Doman     }
9603cde86f1SJonathan Doman }
9613cde86f1SJonathan Doman 
9623cde86f1SJonathan Doman /**
9633cde86f1SJonathan Doman  * Handle the PATCH operation of the AppliedOperatingConfig property. Do basic
9643cde86f1SJonathan Doman  * validation of the input data, and then set the D-Bus property.
9653cde86f1SJonathan Doman  *
9663cde86f1SJonathan Doman  * @param[in,out]   resp            Async HTTP response.
9673cde86f1SJonathan Doman  * @param[in]       processorId     Processor's Id.
9683cde86f1SJonathan Doman  * @param[in]       appliedConfigUri    New property value to apply.
9693cde86f1SJonathan Doman  * @param[in]       cpuObjectPath   Path of CPU object to modify.
9703cde86f1SJonathan Doman  * @param[in]       serviceMap      Service map for CPU object.
9713cde86f1SJonathan Doman  */
9723cde86f1SJonathan Doman inline void patchAppliedOperatingConfig(
9733cde86f1SJonathan Doman     const std::shared_ptr<bmcweb::AsyncResp>& resp,
9743cde86f1SJonathan Doman     const std::string& processorId, const std::string& appliedConfigUri,
9755df6eda2SShantappa Teekappanavar     const std::string& cpuObjectPath,
9765df6eda2SShantappa Teekappanavar     const dbus::utility::MapperServiceMap& serviceMap)
9773cde86f1SJonathan Doman {
9783cde86f1SJonathan Doman     // Check that the property even exists by checking for the interface
9793cde86f1SJonathan Doman     const std::string* controlService = nullptr;
9803cde86f1SJonathan Doman     for (const auto& [serviceName, interfaceList] : serviceMap)
9813cde86f1SJonathan Doman     {
9823cde86f1SJonathan Doman         if (std::find(interfaceList.begin(), interfaceList.end(),
9833cde86f1SJonathan Doman                       "xyz.openbmc_project.Control.Processor."
9843cde86f1SJonathan Doman                       "CurrentOperatingConfig") != interfaceList.end())
9853cde86f1SJonathan Doman         {
9863cde86f1SJonathan Doman             controlService = &serviceName;
9873cde86f1SJonathan Doman             break;
9883cde86f1SJonathan Doman         }
9893cde86f1SJonathan Doman     }
9903cde86f1SJonathan Doman 
9913cde86f1SJonathan Doman     if (controlService == nullptr)
9923cde86f1SJonathan Doman     {
9933cde86f1SJonathan Doman         messages::internalError(resp->res);
9943cde86f1SJonathan Doman         return;
9953cde86f1SJonathan Doman     }
9963cde86f1SJonathan Doman 
9973cde86f1SJonathan Doman     // Check that the config URI is a child of the cpu URI being patched.
9983cde86f1SJonathan Doman     std::string expectedPrefix("/redfish/v1/Systems/system/Processors/");
9993cde86f1SJonathan Doman     expectedPrefix += processorId;
10003cde86f1SJonathan Doman     expectedPrefix += "/OperatingConfigs/";
100111ba3979SEd Tanous     if (!appliedConfigUri.starts_with(expectedPrefix) ||
10023cde86f1SJonathan Doman         expectedPrefix.size() == appliedConfigUri.size())
10033cde86f1SJonathan Doman     {
10043cde86f1SJonathan Doman         messages::propertyValueIncorrect(
10053cde86f1SJonathan Doman             resp->res, "AppliedOperatingConfig/@odata.id", appliedConfigUri);
10063cde86f1SJonathan Doman         return;
10073cde86f1SJonathan Doman     }
10083cde86f1SJonathan Doman 
10093cde86f1SJonathan Doman     // Generate the D-Bus path of the OperatingConfig object, by assuming it's a
10103cde86f1SJonathan Doman     // direct child of the CPU object.
10113cde86f1SJonathan Doman     // Strip the expectedPrefix from the config URI to get the "filename", and
10123cde86f1SJonathan Doman     // append to the CPU's path.
10133cde86f1SJonathan Doman     std::string configBaseName = appliedConfigUri.substr(expectedPrefix.size());
10143cde86f1SJonathan Doman     sdbusplus::message::object_path configPath(cpuObjectPath);
10153cde86f1SJonathan Doman     configPath /= configBaseName;
10163cde86f1SJonathan Doman 
10173cde86f1SJonathan Doman     BMCWEB_LOG_INFO << "Setting config to " << configPath.str;
10183cde86f1SJonathan Doman 
10193cde86f1SJonathan Doman     // Set the property, with handler to check error responses
10203cde86f1SJonathan Doman     crow::connections::systemBus->async_method_call(
1021*5e7e2dc5SEd Tanous         [resp, appliedConfigUri](const boost::system::error_code& ec,
102259d494eeSPatrick Williams                                  const sdbusplus::message_t& msg) {
10233cde86f1SJonathan Doman         handleAppliedConfigResponse(resp, appliedConfigUri, ec, msg);
10243cde86f1SJonathan Doman         },
10253cde86f1SJonathan Doman         *controlService, cpuObjectPath, "org.freedesktop.DBus.Properties",
10263cde86f1SJonathan Doman         "Set", "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig",
1027168e20c1SEd Tanous         "AppliedConfig", dbus::utility::DbusVariantType(std::move(configPath)));
10283cde86f1SJonathan Doman }
10293cde86f1SJonathan Doman 
103071a24ca4SNikhil Namjoshi inline void handleProcessorHead(crow::App& app, const crow::Request& req,
103171a24ca4SNikhil Namjoshi                                 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
103271a24ca4SNikhil Namjoshi                                 const std::string& /* systemName */,
103371a24ca4SNikhil Namjoshi                                 const std::string& /* processorId */)
103471a24ca4SNikhil Namjoshi {
103571a24ca4SNikhil Namjoshi     if (!redfish::setUpRedfishRoute(app, req, aResp))
103671a24ca4SNikhil Namjoshi     {
103771a24ca4SNikhil Namjoshi         return;
103871a24ca4SNikhil Namjoshi     }
103971a24ca4SNikhil Namjoshi     aResp->res.addHeader(
104071a24ca4SNikhil Namjoshi         boost::beast::http::field::link,
104171a24ca4SNikhil Namjoshi         "</redfish/v1/JsonSchemas/Processor/Processor.json>; rel=describedby");
104271a24ca4SNikhil Namjoshi }
104371a24ca4SNikhil Namjoshi 
104471a24ca4SNikhil Namjoshi inline void handleProcessorCollectionHead(
104571a24ca4SNikhil Namjoshi     crow::App& app, const crow::Request& req,
104671a24ca4SNikhil Namjoshi     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
104771a24ca4SNikhil Namjoshi     const std::string& /* systemName */)
104871a24ca4SNikhil Namjoshi {
104971a24ca4SNikhil Namjoshi     if (!redfish::setUpRedfishRoute(app, req, aResp))
105071a24ca4SNikhil Namjoshi     {
105171a24ca4SNikhil Namjoshi         return;
105271a24ca4SNikhil Namjoshi     }
105371a24ca4SNikhil Namjoshi     aResp->res.addHeader(
105471a24ca4SNikhil Namjoshi         boost::beast::http::field::link,
105571a24ca4SNikhil Namjoshi         "</redfish/v1/JsonSchemas/ProcessorCollection/ProcessorCollection.json>; rel=describedby");
105671a24ca4SNikhil Namjoshi }
105771a24ca4SNikhil Namjoshi 
10587e860f15SJohn Edward Broadbent inline void requestRoutesOperatingConfigCollection(App& app)
1059dba0c291SJonathan Doman {
1060dba0c291SJonathan Doman 
10617e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
10627e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/Processors/<str>/OperatingConfigs/")
1063ed398213SEd Tanous         .privileges(redfish::privileges::getOperatingConfigCollection)
1064002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1065002d39b4SEd Tanous             [&app](const crow::Request& req,
106645ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
10677e860f15SJohn Edward Broadbent                    const std::string& cpuName) {
10683ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
106945ca1b86SEd Tanous         {
107045ca1b86SEd Tanous             return;
107145ca1b86SEd Tanous         }
10728d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
1073dba0c291SJonathan Doman             "#OperatingConfigCollection.OperatingConfigCollection";
10748d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] = req.url;
10750fda0f12SGeorge Liu         asyncResp->res.jsonValue["Name"] = "Operating Config Collection";
1076dba0c291SJonathan Doman 
10777e860f15SJohn Edward Broadbent         // First find the matching CPU object so we know how to
10787e860f15SJohn Edward Broadbent         // constrain our search for related Config objects.
10797a1dbc48SGeorge Liu         const std::array<std::string_view, 1> interfaces = {
10807a1dbc48SGeorge Liu             "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig"};
10817a1dbc48SGeorge Liu         dbus::utility::getSubTreePaths(
10827a1dbc48SGeorge Liu             "/xyz/openbmc_project/inventory", 0, interfaces,
1083002d39b4SEd Tanous             [asyncResp, cpuName](
10847a1dbc48SGeorge Liu                 const boost::system::error_code& ec,
1085002d39b4SEd Tanous                 const dbus::utility::MapperGetSubTreePathsResponse& objects) {
1086dba0c291SJonathan Doman             if (ec)
1087dba0c291SJonathan Doman             {
1088dba0c291SJonathan Doman                 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
1089dba0c291SJonathan Doman                                    << ec.message();
1090dba0c291SJonathan Doman                 messages::internalError(asyncResp->res);
1091dba0c291SJonathan Doman                 return;
1092dba0c291SJonathan Doman             }
1093dba0c291SJonathan Doman 
1094dba0c291SJonathan Doman             for (const std::string& object : objects)
1095dba0c291SJonathan Doman             {
109611ba3979SEd Tanous                 if (!object.ends_with(cpuName))
1097dba0c291SJonathan Doman                 {
1098dba0c291SJonathan Doman                     continue;
1099dba0c291SJonathan Doman                 }
1100dba0c291SJonathan Doman 
11017e860f15SJohn Edward Broadbent                 // Not expected that there will be multiple matching
11027e860f15SJohn Edward Broadbent                 // CPU objects, but if there are just use the first
11037e860f15SJohn Edward Broadbent                 // one.
1104dba0c291SJonathan Doman 
11057e860f15SJohn Edward Broadbent                 // Use the common search routine to construct the
11067e860f15SJohn Edward Broadbent                 // Collection of all Config objects under this CPU.
11077a1dbc48SGeorge Liu                 constexpr std::array<std::string_view, 1> interface {
11087a1dbc48SGeorge Liu                     "xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig"
11097a1dbc48SGeorge Liu                 };
1110dba0c291SJonathan Doman                 collection_util::getCollectionMembers(
1111dba0c291SJonathan Doman                     asyncResp,
1112ae9031f0SWilly Tu                     crow::utility::urlFromPieces("redfish", "v1", "Systems",
1113ae9031f0SWilly Tu                                                  "system", "Processors",
1114ae9031f0SWilly Tu                                                  cpuName, "OperatingConfigs"),
11157a1dbc48SGeorge Liu                     interface, object.c_str());
1116dba0c291SJonathan Doman                 return;
1117dba0c291SJonathan Doman             }
11187a1dbc48SGeorge Liu             });
11197e860f15SJohn Edward Broadbent         });
1120dba0c291SJonathan Doman }
1121dba0c291SJonathan Doman 
11227e860f15SJohn Edward Broadbent inline void requestRoutesOperatingConfig(App& app)
1123dba0c291SJonathan Doman {
11247e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
11257e860f15SJohn Edward Broadbent         app,
11267e860f15SJohn Edward Broadbent         "/redfish/v1/Systems/system/Processors/<str>/OperatingConfigs/<str>/")
1127ed398213SEd Tanous         .privileges(redfish::privileges::getOperatingConfig)
1128002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1129002d39b4SEd Tanous             [&app](const crow::Request& req,
113045ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1131002d39b4SEd Tanous                    const std::string& cpuName, const std::string& configName) {
11323ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
113345ca1b86SEd Tanous         {
113445ca1b86SEd Tanous             return;
113545ca1b86SEd Tanous         }
11367e860f15SJohn Edward Broadbent         // Ask for all objects implementing OperatingConfig so we can search
11377e860f15SJohn Edward Broadbent         // for one with a matching name
1138e99073f5SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
1139e99073f5SGeorge Liu             "xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig"};
1140e99073f5SGeorge Liu         dbus::utility::getSubTree(
1141e99073f5SGeorge Liu             "/xyz/openbmc_project/inventory", 0, interfaces,
11425df6eda2SShantappa Teekappanavar             [asyncResp, cpuName, configName, reqUrl{req.url}](
1143e99073f5SGeorge Liu                 const boost::system::error_code& ec,
11445df6eda2SShantappa Teekappanavar                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
1145dba0c291SJonathan Doman             if (ec)
1146dba0c291SJonathan Doman             {
1147dba0c291SJonathan Doman                 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
1148dba0c291SJonathan Doman                                    << ec.message();
1149dba0c291SJonathan Doman                 messages::internalError(asyncResp->res);
1150dba0c291SJonathan Doman                 return;
1151dba0c291SJonathan Doman             }
1152002d39b4SEd Tanous             const std::string expectedEnding = cpuName + '/' + configName;
1153dba0c291SJonathan Doman             for (const auto& [objectPath, serviceMap] : subtree)
1154dba0c291SJonathan Doman             {
1155dba0c291SJonathan Doman                 // Ignore any configs without matching cpuX/configY
115611ba3979SEd Tanous                 if (!objectPath.ends_with(expectedEnding) || serviceMap.empty())
1157dba0c291SJonathan Doman                 {
1158dba0c291SJonathan Doman                     continue;
1159dba0c291SJonathan Doman                 }
1160dba0c291SJonathan Doman 
1161dba0c291SJonathan Doman                 nlohmann::json& json = asyncResp->res.jsonValue;
1162002d39b4SEd Tanous                 json["@odata.type"] = "#OperatingConfig.v1_0_0.OperatingConfig";
1163dba0c291SJonathan Doman                 json["@odata.id"] = reqUrl;
1164dba0c291SJonathan Doman                 json["Name"] = "Processor Profile";
1165dba0c291SJonathan Doman                 json["Id"] = configName;
1166dba0c291SJonathan Doman 
1167dba0c291SJonathan Doman                 // Just use the first implementation of the object - not
11687e860f15SJohn Edward Broadbent                 // expected that there would be multiple matching
11697e860f15SJohn Edward Broadbent                 // services
1170002d39b4SEd Tanous                 getOperatingConfigData(asyncResp, serviceMap.begin()->first,
1171002d39b4SEd Tanous                                        objectPath);
1172dba0c291SJonathan Doman                 return;
1173dba0c291SJonathan Doman             }
1174002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "OperatingConfig",
1175002d39b4SEd Tanous                                        configName);
1176e99073f5SGeorge Liu             });
11777e860f15SJohn Edward Broadbent         });
1178ac6a4445SGunnar Mills }
1179ac6a4445SGunnar Mills 
11807e860f15SJohn Edward Broadbent inline void requestRoutesProcessorCollection(App& app)
11817e860f15SJohn Edward Broadbent {
1182ac6a4445SGunnar Mills     /**
1183ac6a4445SGunnar Mills      * Functions triggers appropriate requests on DBus
1184ac6a4445SGunnar Mills      */
118522d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Processors/")
118671a24ca4SNikhil Namjoshi         .privileges(redfish::privileges::headProcessorCollection)
118771a24ca4SNikhil Namjoshi         .methods(boost::beast::http::verb::head)(
118871a24ca4SNikhil Namjoshi             std::bind_front(handleProcessorCollectionHead, std::ref(app)));
118971a24ca4SNikhil Namjoshi 
119071a24ca4SNikhil Namjoshi     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Processors/")
1191ed398213SEd Tanous         .privileges(redfish::privileges::getProcessorCollection)
11927e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
119345ca1b86SEd Tanous             [&app](const crow::Request& req,
119422d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
119522d268cbSEd Tanous                    const std::string& systemName) {
11963ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
119745ca1b86SEd Tanous         {
119845ca1b86SEd Tanous             return;
119945ca1b86SEd Tanous         }
120022d268cbSEd Tanous         if (systemName != "system")
120122d268cbSEd Tanous         {
120222d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
120322d268cbSEd Tanous                                        systemName);
120422d268cbSEd Tanous             return;
120522d268cbSEd Tanous         }
120622d268cbSEd Tanous 
120771a24ca4SNikhil Namjoshi         asyncResp->res.addHeader(
120871a24ca4SNikhil Namjoshi             boost::beast::http::field::link,
120971a24ca4SNikhil Namjoshi             "</redfish/v1/JsonSchemas/ProcessorCollection/ProcessorCollection.json>; rel=describedby");
121071a24ca4SNikhil Namjoshi 
12118d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
1212ac6a4445SGunnar Mills             "#ProcessorCollection.ProcessorCollection";
12138d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Processor Collection";
1214ac6a4445SGunnar Mills 
12158d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] =
12168d1b46d7Szhanghch05             "/redfish/v1/Systems/system/Processors";
1217ac6a4445SGunnar Mills 
121805030b8eSGunnar Mills         collection_util::getCollectionMembers(
1219ae9031f0SWilly Tu             asyncResp,
1220ae9031f0SWilly Tu             boost::urls::url("/redfish/v1/Systems/system/Processors"),
12217a1dbc48SGeorge Liu             processorInterfaces);
12227e860f15SJohn Edward Broadbent         });
1223ac6a4445SGunnar Mills }
1224ac6a4445SGunnar Mills 
12257e860f15SJohn Edward Broadbent inline void requestRoutesProcessor(App& app)
12267e860f15SJohn Edward Broadbent {
1227ac6a4445SGunnar Mills     /**
1228ac6a4445SGunnar Mills      * Functions triggers appropriate requests on DBus
1229ac6a4445SGunnar Mills      */
12307e860f15SJohn Edward Broadbent 
123122d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Processors/<str>/")
123271a24ca4SNikhil Namjoshi         .privileges(redfish::privileges::headProcessor)
123371a24ca4SNikhil Namjoshi         .methods(boost::beast::http::verb::head)(
123471a24ca4SNikhil Namjoshi             std::bind_front(handleProcessorHead, std::ref(app)));
123571a24ca4SNikhil Namjoshi 
123671a24ca4SNikhil Namjoshi     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Processors/<str>/")
1237ed398213SEd Tanous         .privileges(redfish::privileges::getProcessor)
12387e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
123945ca1b86SEd Tanous             [&app](const crow::Request& req,
12407e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
124122d268cbSEd Tanous                    const std::string& systemName,
12427e860f15SJohn Edward Broadbent                    const std::string& processorId) {
12433ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
124445ca1b86SEd Tanous         {
124545ca1b86SEd Tanous             return;
124645ca1b86SEd Tanous         }
124722d268cbSEd Tanous         if (systemName != "system")
124822d268cbSEd Tanous         {
124922d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
125022d268cbSEd Tanous                                        systemName);
125122d268cbSEd Tanous             return;
125222d268cbSEd Tanous         }
125322d268cbSEd Tanous 
125471a24ca4SNikhil Namjoshi         asyncResp->res.addHeader(
125571a24ca4SNikhil Namjoshi             boost::beast::http::field::link,
125671a24ca4SNikhil Namjoshi             "</redfish/v1/JsonSchemas/Processor/Processor.json>; rel=describedby");
12578d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
12588d1b46d7Szhanghch05             "#Processor.v1_11_0.Processor";
1259eddfc437SWilly Tu         asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
1260eddfc437SWilly Tu             "redfish", "v1", "Systems", "system", "Processors", processorId);
1261ac6a4445SGunnar Mills 
12628a592810SEd Tanous         getProcessorObject(
12638a592810SEd Tanous             asyncResp, processorId,
12648a592810SEd Tanous             std::bind_front(getProcessorData, asyncResp, processorId));
12657e860f15SJohn Edward Broadbent         });
12663cde86f1SJonathan Doman 
126722d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/Processors/<str>/")
1268ed398213SEd Tanous         .privileges(redfish::privileges::patchProcessor)
12697e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
127045ca1b86SEd Tanous             [&app](const crow::Request& req,
12717e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
127222d268cbSEd Tanous                    const std::string& systemName,
12737e860f15SJohn Edward Broadbent                    const std::string& processorId) {
12743ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
127545ca1b86SEd Tanous         {
127645ca1b86SEd Tanous             return;
127745ca1b86SEd Tanous         }
127822d268cbSEd Tanous         if (systemName != "system")
127922d268cbSEd Tanous         {
128022d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
128122d268cbSEd Tanous                                        systemName);
128222d268cbSEd Tanous             return;
128322d268cbSEd Tanous         }
128422d268cbSEd Tanous 
12853cde86f1SJonathan Doman         std::optional<nlohmann::json> appliedConfigJson;
128615ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res,
12877e860f15SJohn Edward Broadbent                                       "AppliedOperatingConfig",
12883cde86f1SJonathan Doman                                       appliedConfigJson))
12893cde86f1SJonathan Doman         {
12903cde86f1SJonathan Doman             return;
12913cde86f1SJonathan Doman         }
12923cde86f1SJonathan Doman 
12933cde86f1SJonathan Doman         if (appliedConfigJson)
12943cde86f1SJonathan Doman         {
1295f8fe53e7SEd Tanous             std::string appliedConfigUri;
12963cde86f1SJonathan Doman             if (!json_util::readJson(*appliedConfigJson, asyncResp->res,
12973cde86f1SJonathan Doman                                      "@odata.id", appliedConfigUri))
12983cde86f1SJonathan Doman             {
12993cde86f1SJonathan Doman                 return;
13003cde86f1SJonathan Doman             }
13017e860f15SJohn Edward Broadbent             // Check for 404 and find matching D-Bus object, then run
13027e860f15SJohn Edward Broadbent             // property patch handlers if that all succeeds.
13038a592810SEd Tanous             getProcessorObject(asyncResp, processorId,
13048a592810SEd Tanous                                std::bind_front(patchAppliedOperatingConfig,
13057e860f15SJohn Edward Broadbent                                                asyncResp, processorId,
13068a592810SEd Tanous                                                appliedConfigUri));
13073cde86f1SJonathan Doman         }
13087e860f15SJohn Edward Broadbent         });
13093cde86f1SJonathan Doman }
1310ac6a4445SGunnar Mills 
1311ac6a4445SGunnar Mills } // namespace redfish
1312