xref: /openbmc/bmcweb/features/redfish/lib/processor.hpp (revision cba4f44896bea631f409cbc9e52d4ddb7d2e86c1)
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 
18ac6a4445SGunnar Mills #include "health.hpp"
19ac6a4445SGunnar Mills 
20ac6a4445SGunnar Mills #include <boost/container/flat_map.hpp>
21ac6a4445SGunnar Mills #include <node.hpp>
22dba0c291SJonathan Doman #include <sdbusplus/message/native_types.hpp>
23dba0c291SJonathan Doman #include <sdbusplus/utility/dedup_variant.hpp>
24ac6a4445SGunnar Mills #include <utils/collection.hpp>
25ac6a4445SGunnar Mills #include <utils/json_utils.hpp>
26ac6a4445SGunnar Mills 
27ac6a4445SGunnar Mills namespace redfish
28ac6a4445SGunnar Mills {
29ac6a4445SGunnar Mills 
30ac6a4445SGunnar Mills using InterfacesProperties = boost::container::flat_map<
31ac6a4445SGunnar Mills     std::string,
32ac6a4445SGunnar Mills     boost::container::flat_map<std::string, dbus::utility::DbusVariantType>>;
33ac6a4445SGunnar Mills 
342bab9831SJonathan Doman using MapperGetSubTreeResponse = std::vector<
352bab9831SJonathan Doman     std::pair<std::string,
362bab9831SJonathan Doman               std::vector<std::pair<std::string, std::vector<std::string>>>>>;
372bab9831SJonathan Doman 
38ac6a4445SGunnar Mills inline void
39ac6a4445SGunnar Mills     getCpuDataByInterface(const std::shared_ptr<AsyncResp>& aResp,
40ac6a4445SGunnar Mills                           const InterfacesProperties& cpuInterfacesProperties)
41ac6a4445SGunnar Mills {
42ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG << "Get CPU resources by interface.";
43ac6a4445SGunnar Mills 
44ac6a4445SGunnar Mills     // Added for future purpose. Once present and functional attributes added
45ac6a4445SGunnar Mills     // in busctl call, need to add actual logic to fetch original values.
46ac6a4445SGunnar Mills     bool present = false;
47ac6a4445SGunnar Mills     const bool functional = true;
48ac6a4445SGunnar Mills     auto health = std::make_shared<HealthPopulate>(aResp);
49ac6a4445SGunnar Mills     health->populate();
50ac6a4445SGunnar Mills 
51ac6a4445SGunnar Mills     for (const auto& interface : cpuInterfacesProperties)
52ac6a4445SGunnar Mills     {
53ac6a4445SGunnar Mills         for (const auto& property : interface.second)
54ac6a4445SGunnar Mills         {
55ac6a4445SGunnar Mills             if (property.first == "CoreCount")
56ac6a4445SGunnar Mills             {
57ac6a4445SGunnar Mills                 const uint16_t* coresCount =
58ac6a4445SGunnar Mills                     std::get_if<uint16_t>(&property.second);
59ac6a4445SGunnar Mills                 if (coresCount == nullptr)
60ac6a4445SGunnar Mills                 {
61ac6a4445SGunnar Mills                     // Important property not in desired type
62ac6a4445SGunnar Mills                     messages::internalError(aResp->res);
63ac6a4445SGunnar Mills                     return;
64ac6a4445SGunnar Mills                 }
65ac6a4445SGunnar Mills                 if (*coresCount == 0)
66ac6a4445SGunnar Mills                 {
67ac6a4445SGunnar Mills                     // Slot is not populated, set status end return
68ac6a4445SGunnar Mills                     aResp->res.jsonValue["Status"]["State"] = "Absent";
69ac6a4445SGunnar Mills                     // HTTP Code will be set up automatically, just return
70ac6a4445SGunnar Mills                     return;
71ac6a4445SGunnar Mills                 }
72ac6a4445SGunnar Mills                 aResp->res.jsonValue["Status"]["State"] = "Enabled";
73ac6a4445SGunnar Mills                 present = true;
74ac6a4445SGunnar Mills                 aResp->res.jsonValue["TotalCores"] = *coresCount;
75ac6a4445SGunnar Mills             }
76dc3fa667SJonathan Doman             else if (property.first == "MaxSpeedInMhz")
77dc3fa667SJonathan Doman             {
78dc3fa667SJonathan Doman                 const uint32_t* value = std::get_if<uint32_t>(&property.second);
79dc3fa667SJonathan Doman                 if (value != nullptr)
80dc3fa667SJonathan Doman                 {
81dc3fa667SJonathan Doman                     aResp->res.jsonValue["MaxSpeedMHz"] = *value;
82dc3fa667SJonathan Doman                 }
83dc3fa667SJonathan Doman             }
84ac6a4445SGunnar Mills             else if (property.first == "Socket")
85ac6a4445SGunnar Mills             {
86ac6a4445SGunnar Mills                 const std::string* value =
87ac6a4445SGunnar Mills                     std::get_if<std::string>(&property.second);
88ac6a4445SGunnar Mills                 if (value != nullptr)
89ac6a4445SGunnar Mills                 {
90ac6a4445SGunnar Mills                     aResp->res.jsonValue["Socket"] = *value;
91ac6a4445SGunnar Mills                 }
92ac6a4445SGunnar Mills             }
93ac6a4445SGunnar Mills             else if (property.first == "ThreadCount")
94ac6a4445SGunnar Mills             {
95dc3fa667SJonathan Doman                 const uint16_t* value = std::get_if<uint16_t>(&property.second);
96ac6a4445SGunnar Mills                 if (value != nullptr)
97ac6a4445SGunnar Mills                 {
98ac6a4445SGunnar Mills                     aResp->res.jsonValue["TotalThreads"] = *value;
99ac6a4445SGunnar Mills                 }
100ac6a4445SGunnar Mills             }
101ac6a4445SGunnar Mills             else if (property.first == "Family")
102ac6a4445SGunnar Mills             {
103ac6a4445SGunnar Mills                 const std::string* value =
104ac6a4445SGunnar Mills                     std::get_if<std::string>(&property.second);
105ac6a4445SGunnar Mills                 if (value != nullptr)
106ac6a4445SGunnar Mills                 {
107ac6a4445SGunnar Mills                     aResp->res.jsonValue["ProcessorId"]["EffectiveFamily"] =
108ac6a4445SGunnar Mills                         *value;
109ac6a4445SGunnar Mills                 }
110ac6a4445SGunnar Mills             }
111ac6a4445SGunnar Mills             else if (property.first == "Id")
112ac6a4445SGunnar Mills             {
113ac6a4445SGunnar Mills                 const uint64_t* value = std::get_if<uint64_t>(&property.second);
114ac6a4445SGunnar Mills                 if (value != nullptr && *value != 0)
115ac6a4445SGunnar Mills                 {
116ac6a4445SGunnar Mills                     present = true;
117ac6a4445SGunnar Mills                     aResp->res
118ac6a4445SGunnar Mills                         .jsonValue["ProcessorId"]["IdentificationRegisters"] =
119ac6a4445SGunnar Mills                         boost::lexical_cast<std::string>(*value);
120ac6a4445SGunnar Mills                 }
121ac6a4445SGunnar Mills             }
122ac6a4445SGunnar Mills         }
123ac6a4445SGunnar Mills     }
124ac6a4445SGunnar Mills 
125ac6a4445SGunnar Mills     if (present == false)
126ac6a4445SGunnar Mills     {
127ac6a4445SGunnar Mills         aResp->res.jsonValue["Status"]["State"] = "Absent";
128ac6a4445SGunnar Mills         aResp->res.jsonValue["Status"]["Health"] = "OK";
129ac6a4445SGunnar Mills     }
130ac6a4445SGunnar Mills     else
131ac6a4445SGunnar Mills     {
132ac6a4445SGunnar Mills         aResp->res.jsonValue["Status"]["State"] = "Enabled";
133ac6a4445SGunnar Mills         if (functional)
134ac6a4445SGunnar Mills         {
135ac6a4445SGunnar Mills             aResp->res.jsonValue["Status"]["Health"] = "OK";
136ac6a4445SGunnar Mills         }
137ac6a4445SGunnar Mills         else
138ac6a4445SGunnar Mills         {
139ac6a4445SGunnar Mills             aResp->res.jsonValue["Status"]["Health"] = "Critical";
140ac6a4445SGunnar Mills         }
141ac6a4445SGunnar Mills     }
142ac6a4445SGunnar Mills 
143ac6a4445SGunnar Mills     return;
144ac6a4445SGunnar Mills }
145ac6a4445SGunnar Mills 
146ac6a4445SGunnar Mills inline void getCpuDataByService(std::shared_ptr<AsyncResp> aResp,
147ac6a4445SGunnar Mills                                 const std::string& cpuId,
148ac6a4445SGunnar Mills                                 const std::string& service,
149ac6a4445SGunnar Mills                                 const std::string& objPath)
150ac6a4445SGunnar Mills {
151ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG << "Get available system cpu resources by service.";
152ac6a4445SGunnar Mills 
153ac6a4445SGunnar Mills     crow::connections::systemBus->async_method_call(
154ac6a4445SGunnar Mills         [cpuId, service, objPath, aResp{std::move(aResp)}](
155ac6a4445SGunnar Mills             const boost::system::error_code ec,
156ac6a4445SGunnar Mills             const dbus::utility::ManagedObjectType& dbusData) {
157ac6a4445SGunnar Mills             if (ec)
158ac6a4445SGunnar Mills             {
159ac6a4445SGunnar Mills                 BMCWEB_LOG_DEBUG << "DBUS response error";
160ac6a4445SGunnar Mills                 messages::internalError(aResp->res);
161ac6a4445SGunnar Mills                 return;
162ac6a4445SGunnar Mills             }
163ac6a4445SGunnar Mills             aResp->res.jsonValue["Id"] = cpuId;
164ac6a4445SGunnar Mills             aResp->res.jsonValue["Name"] = "Processor";
165ac6a4445SGunnar Mills             aResp->res.jsonValue["ProcessorType"] = "CPU";
166ac6a4445SGunnar Mills 
167ac6a4445SGunnar Mills             bool slotPresent = false;
168ac6a4445SGunnar Mills             std::string corePath = objPath + "/core";
169ac6a4445SGunnar Mills             size_t totalCores = 0;
170ac6a4445SGunnar Mills             for (const auto& object : dbusData)
171ac6a4445SGunnar Mills             {
172ac6a4445SGunnar Mills                 if (object.first.str == objPath)
173ac6a4445SGunnar Mills                 {
174ac6a4445SGunnar Mills                     getCpuDataByInterface(aResp, object.second);
175ac6a4445SGunnar Mills                 }
176ac6a4445SGunnar Mills                 else if (boost::starts_with(object.first.str, corePath))
177ac6a4445SGunnar Mills                 {
178ac6a4445SGunnar Mills                     for (const auto& interface : object.second)
179ac6a4445SGunnar Mills                     {
180ac6a4445SGunnar Mills                         if (interface.first ==
181ac6a4445SGunnar Mills                             "xyz.openbmc_project.Inventory.Item")
182ac6a4445SGunnar Mills                         {
183ac6a4445SGunnar Mills                             for (const auto& property : interface.second)
184ac6a4445SGunnar Mills                             {
185ac6a4445SGunnar Mills                                 if (property.first == "Present")
186ac6a4445SGunnar Mills                                 {
187ac6a4445SGunnar Mills                                     const bool* present =
188ac6a4445SGunnar Mills                                         std::get_if<bool>(&property.second);
189ac6a4445SGunnar Mills                                     if (present != nullptr)
190ac6a4445SGunnar Mills                                     {
191ac6a4445SGunnar Mills                                         if (*present == true)
192ac6a4445SGunnar Mills                                         {
193ac6a4445SGunnar Mills                                             slotPresent = true;
194ac6a4445SGunnar Mills                                             totalCores++;
195ac6a4445SGunnar Mills                                         }
196ac6a4445SGunnar Mills                                     }
197ac6a4445SGunnar Mills                                 }
198ac6a4445SGunnar Mills                             }
199ac6a4445SGunnar Mills                         }
200ac6a4445SGunnar Mills                     }
201ac6a4445SGunnar Mills                 }
202ac6a4445SGunnar Mills             }
203ac6a4445SGunnar Mills             // In getCpuDataByInterface(), state and health are set
204ac6a4445SGunnar Mills             // based on the present and functional status. If core
205ac6a4445SGunnar Mills             // count is zero, then it has a higher precedence.
206ac6a4445SGunnar Mills             if (slotPresent)
207ac6a4445SGunnar Mills             {
208ac6a4445SGunnar Mills                 if (totalCores == 0)
209ac6a4445SGunnar Mills                 {
210ac6a4445SGunnar Mills                     // Slot is not populated, set status end return
211ac6a4445SGunnar Mills                     aResp->res.jsonValue["Status"]["State"] = "Absent";
212ac6a4445SGunnar Mills                     aResp->res.jsonValue["Status"]["Health"] = "OK";
213ac6a4445SGunnar Mills                 }
214ac6a4445SGunnar Mills                 aResp->res.jsonValue["TotalCores"] = totalCores;
215ac6a4445SGunnar Mills             }
216ac6a4445SGunnar Mills             return;
217ac6a4445SGunnar Mills         },
218ac6a4445SGunnar Mills         service, "/xyz/openbmc_project/inventory",
219ac6a4445SGunnar Mills         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
220ac6a4445SGunnar Mills }
221ac6a4445SGunnar Mills 
222ac6a4445SGunnar Mills inline void getCpuAssetData(std::shared_ptr<AsyncResp> aResp,
223ac6a4445SGunnar Mills                             const std::string& service,
224ac6a4445SGunnar Mills                             const std::string& objPath)
225ac6a4445SGunnar Mills {
226ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG << "Get Cpu Asset Data";
227ac6a4445SGunnar Mills     crow::connections::systemBus->async_method_call(
228ac6a4445SGunnar Mills         [objPath, aResp{std::move(aResp)}](
229ac6a4445SGunnar Mills             const boost::system::error_code ec,
230ac6a4445SGunnar Mills             const boost::container::flat_map<
231ac6a4445SGunnar Mills                 std::string, std::variant<std::string, uint32_t, uint16_t,
232ac6a4445SGunnar Mills                                           bool>>& properties) {
233ac6a4445SGunnar Mills             if (ec)
234ac6a4445SGunnar Mills             {
235ac6a4445SGunnar Mills                 BMCWEB_LOG_DEBUG << "DBUS response error";
236ac6a4445SGunnar Mills                 messages::internalError(aResp->res);
237ac6a4445SGunnar Mills                 return;
238ac6a4445SGunnar Mills             }
239ac6a4445SGunnar Mills 
240ac6a4445SGunnar Mills             for (const auto& property : properties)
241ac6a4445SGunnar Mills             {
242ac6a4445SGunnar Mills                 if (property.first == "SerialNumber")
243ac6a4445SGunnar Mills                 {
244ac6a4445SGunnar Mills                     const std::string* sn =
245ac6a4445SGunnar Mills                         std::get_if<std::string>(&property.second);
246ac6a4445SGunnar Mills                     if (sn != nullptr && !sn->empty())
247ac6a4445SGunnar Mills                     {
248ac6a4445SGunnar Mills                         aResp->res.jsonValue["SerialNumber"] = *sn;
249ac6a4445SGunnar Mills                     }
250ac6a4445SGunnar Mills                 }
251ac6a4445SGunnar Mills                 else if (property.first == "Model")
252ac6a4445SGunnar Mills                 {
253ac6a4445SGunnar Mills                     const std::string* model =
254ac6a4445SGunnar Mills                         std::get_if<std::string>(&property.second);
255ac6a4445SGunnar Mills                     if (model != nullptr && !model->empty())
256ac6a4445SGunnar Mills                     {
257ac6a4445SGunnar Mills                         aResp->res.jsonValue["Model"] = *model;
258ac6a4445SGunnar Mills                     }
259ac6a4445SGunnar Mills                 }
260ac6a4445SGunnar Mills                 else if (property.first == "Manufacturer")
261ac6a4445SGunnar Mills                 {
262ac6a4445SGunnar Mills 
263ac6a4445SGunnar Mills                     const std::string* mfg =
264ac6a4445SGunnar Mills                         std::get_if<std::string>(&property.second);
265ac6a4445SGunnar Mills                     if (mfg != nullptr)
266ac6a4445SGunnar Mills                     {
267ac6a4445SGunnar Mills                         aResp->res.jsonValue["Manufacturer"] = *mfg;
268ac6a4445SGunnar Mills 
269ac6a4445SGunnar Mills                         // Otherwise would be unexpected.
270ac6a4445SGunnar Mills                         if (mfg->find("Intel") != std::string::npos)
271ac6a4445SGunnar Mills                         {
272ac6a4445SGunnar Mills                             aResp->res.jsonValue["ProcessorArchitecture"] =
273ac6a4445SGunnar Mills                                 "x86";
274ac6a4445SGunnar Mills                             aResp->res.jsonValue["InstructionSet"] = "x86-64";
275ac6a4445SGunnar Mills                         }
276ac6a4445SGunnar Mills                         else if (mfg->find("IBM") != std::string::npos)
277ac6a4445SGunnar Mills                         {
278ac6a4445SGunnar Mills                             aResp->res.jsonValue["ProcessorArchitecture"] =
279ac6a4445SGunnar Mills                                 "Power";
280ac6a4445SGunnar Mills                             aResp->res.jsonValue["InstructionSet"] = "PowerISA";
281ac6a4445SGunnar Mills                         }
282ac6a4445SGunnar Mills                     }
283ac6a4445SGunnar Mills                 }
284*cba4f448SSunnySrivastava1984                 else if (property.first == "PartNumber")
285*cba4f448SSunnySrivastava1984                 {
286*cba4f448SSunnySrivastava1984                     const std::string* partNumber =
287*cba4f448SSunnySrivastava1984                         std::get_if<std::string>(&property.second);
288*cba4f448SSunnySrivastava1984 
289*cba4f448SSunnySrivastava1984                     if (partNumber == nullptr)
290*cba4f448SSunnySrivastava1984                     {
291*cba4f448SSunnySrivastava1984                         messages::internalError(aResp->res);
292*cba4f448SSunnySrivastava1984                         return;
293*cba4f448SSunnySrivastava1984                     }
294*cba4f448SSunnySrivastava1984                     aResp->res.jsonValue["PartNumber"] = *partNumber;
295*cba4f448SSunnySrivastava1984                 }
296*cba4f448SSunnySrivastava1984                 else if (property.first == "SparePartNumber")
297*cba4f448SSunnySrivastava1984                 {
298*cba4f448SSunnySrivastava1984                     const std::string* sparePartNumber =
299*cba4f448SSunnySrivastava1984                         std::get_if<std::string>(&property.second);
300*cba4f448SSunnySrivastava1984 
301*cba4f448SSunnySrivastava1984                     if (sparePartNumber == nullptr)
302*cba4f448SSunnySrivastava1984                     {
303*cba4f448SSunnySrivastava1984                         messages::internalError(aResp->res);
304*cba4f448SSunnySrivastava1984                         return;
305*cba4f448SSunnySrivastava1984                     }
306*cba4f448SSunnySrivastava1984                     aResp->res.jsonValue["SparePartNumber"] = *sparePartNumber;
307*cba4f448SSunnySrivastava1984                 }
308ac6a4445SGunnar Mills             }
309ac6a4445SGunnar Mills         },
310ac6a4445SGunnar Mills         service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
311ac6a4445SGunnar Mills         "xyz.openbmc_project.Inventory.Decorator.Asset");
312ac6a4445SGunnar Mills }
313ac6a4445SGunnar Mills 
314ac6a4445SGunnar Mills inline void getCpuRevisionData(std::shared_ptr<AsyncResp> aResp,
315ac6a4445SGunnar Mills                                const std::string& service,
316ac6a4445SGunnar Mills                                const std::string& objPath)
317ac6a4445SGunnar Mills {
318ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG << "Get Cpu Revision Data";
319ac6a4445SGunnar Mills     crow::connections::systemBus->async_method_call(
320ac6a4445SGunnar Mills         [objPath, aResp{std::move(aResp)}](
321ac6a4445SGunnar Mills             const boost::system::error_code ec,
322ac6a4445SGunnar Mills             const boost::container::flat_map<
323ac6a4445SGunnar Mills                 std::string, std::variant<std::string, uint32_t, uint16_t,
324ac6a4445SGunnar Mills                                           bool>>& properties) {
325ac6a4445SGunnar Mills             if (ec)
326ac6a4445SGunnar Mills             {
327ac6a4445SGunnar Mills                 BMCWEB_LOG_DEBUG << "DBUS response error";
328ac6a4445SGunnar Mills                 messages::internalError(aResp->res);
329ac6a4445SGunnar Mills                 return;
330ac6a4445SGunnar Mills             }
331ac6a4445SGunnar Mills 
332ac6a4445SGunnar Mills             for (const auto& property : properties)
333ac6a4445SGunnar Mills             {
334ac6a4445SGunnar Mills                 if (property.first == "Version")
335ac6a4445SGunnar Mills                 {
336ac6a4445SGunnar Mills                     const std::string* ver =
337ac6a4445SGunnar Mills                         std::get_if<std::string>(&property.second);
338ac6a4445SGunnar Mills                     if (ver != nullptr)
339ac6a4445SGunnar Mills                     {
340ac6a4445SGunnar Mills                         aResp->res.jsonValue["Version"] = *ver;
341ac6a4445SGunnar Mills                     }
342ac6a4445SGunnar Mills                     break;
343ac6a4445SGunnar Mills                 }
344ac6a4445SGunnar Mills             }
345ac6a4445SGunnar Mills         },
346ac6a4445SGunnar Mills         service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
347ac6a4445SGunnar Mills         "xyz.openbmc_project.Inventory.Decorator.Revision");
348ac6a4445SGunnar Mills }
349ac6a4445SGunnar Mills 
350ac6a4445SGunnar Mills inline void getAcceleratorDataByService(std::shared_ptr<AsyncResp> aResp,
351ac6a4445SGunnar Mills                                         const std::string& acclrtrId,
352ac6a4445SGunnar Mills                                         const std::string& service,
353ac6a4445SGunnar Mills                                         const std::string& objPath)
354ac6a4445SGunnar Mills {
355ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG
356ac6a4445SGunnar Mills         << "Get available system Accelerator resources by service.";
357ac6a4445SGunnar Mills     crow::connections::systemBus->async_method_call(
358ac6a4445SGunnar Mills         [acclrtrId, aResp{std::move(aResp)}](
359ac6a4445SGunnar Mills             const boost::system::error_code ec,
360ac6a4445SGunnar Mills             const boost::container::flat_map<
361ac6a4445SGunnar Mills                 std::string, std::variant<std::string, uint32_t, uint16_t,
362ac6a4445SGunnar Mills                                           bool>>& properties) {
363ac6a4445SGunnar Mills             if (ec)
364ac6a4445SGunnar Mills             {
365ac6a4445SGunnar Mills                 BMCWEB_LOG_DEBUG << "DBUS response error";
366ac6a4445SGunnar Mills                 messages::internalError(aResp->res);
367ac6a4445SGunnar Mills                 return;
368ac6a4445SGunnar Mills             }
369ac6a4445SGunnar Mills             aResp->res.jsonValue["Id"] = acclrtrId;
370ac6a4445SGunnar Mills             aResp->res.jsonValue["Name"] = "Processor";
371ac6a4445SGunnar Mills             const bool* accPresent = nullptr;
372ac6a4445SGunnar Mills             const bool* accFunctional = nullptr;
373ac6a4445SGunnar Mills 
374ac6a4445SGunnar Mills             for (const auto& property : properties)
375ac6a4445SGunnar Mills             {
376ac6a4445SGunnar Mills                 if (property.first == "Functional")
377ac6a4445SGunnar Mills                 {
378ac6a4445SGunnar Mills                     accFunctional = std::get_if<bool>(&property.second);
379ac6a4445SGunnar Mills                 }
380ac6a4445SGunnar Mills                 else if (property.first == "Present")
381ac6a4445SGunnar Mills                 {
382ac6a4445SGunnar Mills                     accPresent = std::get_if<bool>(&property.second);
383ac6a4445SGunnar Mills                 }
384ac6a4445SGunnar Mills             }
385ac6a4445SGunnar Mills 
386ac6a4445SGunnar Mills             std::string state = "Enabled";
387ac6a4445SGunnar Mills             std::string health = "OK";
388ac6a4445SGunnar Mills 
389ac6a4445SGunnar Mills             if (accPresent != nullptr && *accPresent == false)
390ac6a4445SGunnar Mills             {
391ac6a4445SGunnar Mills                 state = "Absent";
392ac6a4445SGunnar Mills             }
393ac6a4445SGunnar Mills 
394ac6a4445SGunnar Mills             if ((accFunctional != nullptr) && (*accFunctional == false))
395ac6a4445SGunnar Mills             {
396ac6a4445SGunnar Mills                 if (state == "Enabled")
397ac6a4445SGunnar Mills                 {
398ac6a4445SGunnar Mills                     health = "Critical";
399ac6a4445SGunnar Mills                 }
400ac6a4445SGunnar Mills             }
401ac6a4445SGunnar Mills 
402ac6a4445SGunnar Mills             aResp->res.jsonValue["Status"]["State"] = state;
403ac6a4445SGunnar Mills             aResp->res.jsonValue["Status"]["Health"] = health;
404ac6a4445SGunnar Mills             aResp->res.jsonValue["ProcessorType"] = "Accelerator";
405ac6a4445SGunnar Mills         },
406ac6a4445SGunnar Mills         service, objPath, "org.freedesktop.DBus.Properties", "GetAll", "");
407ac6a4445SGunnar Mills }
408ac6a4445SGunnar Mills 
409dba0c291SJonathan Doman // OperatingConfig D-Bus Types
410dba0c291SJonathan Doman using TurboProfileProperty = std::vector<std::tuple<uint32_t, size_t>>;
411dba0c291SJonathan Doman using BaseSpeedPrioritySettingsProperty =
412dba0c291SJonathan Doman     std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>;
413dba0c291SJonathan Doman // uint32_t and size_t may or may not be the same type, requiring a dedup'd
414dba0c291SJonathan Doman // variant
415dba0c291SJonathan Doman using OperatingConfigProperties = std::vector<std::pair<
416dba0c291SJonathan Doman     std::string,
417dba0c291SJonathan Doman     sdbusplus::utility::dedup_variant<uint32_t, size_t, TurboProfileProperty,
418dba0c291SJonathan Doman                                       BaseSpeedPrioritySettingsProperty>>>;
419dba0c291SJonathan Doman 
420dba0c291SJonathan Doman /**
421dba0c291SJonathan Doman  * Fill out the HighSpeedCoreIDs in a Processor resource from the given
422dba0c291SJonathan Doman  * OperatingConfig D-Bus property.
423dba0c291SJonathan Doman  *
424dba0c291SJonathan Doman  * @param[in,out]   aResp               Async HTTP response.
425dba0c291SJonathan Doman  * @param[in]       baseSpeedSettings   Full list of base speed priority groups,
426dba0c291SJonathan Doman  *                                      to use to determine the list of high
427dba0c291SJonathan Doman  *                                      speed cores.
428dba0c291SJonathan Doman  */
429dba0c291SJonathan Doman inline void highSpeedCoreIdsHandler(
430dba0c291SJonathan Doman     const std::shared_ptr<AsyncResp>& aResp,
431dba0c291SJonathan Doman     const BaseSpeedPrioritySettingsProperty& baseSpeedSettings)
432dba0c291SJonathan Doman {
433dba0c291SJonathan Doman     // The D-Bus property does not indicate which bucket is the "high
434dba0c291SJonathan Doman     // priority" group, so let's discern that by looking for the one with
435dba0c291SJonathan Doman     // highest base frequency.
436dba0c291SJonathan Doman     auto highPriorityGroup = baseSpeedSettings.cend();
437dba0c291SJonathan Doman     uint32_t highestBaseSpeed = 0;
438dba0c291SJonathan Doman     for (auto it = baseSpeedSettings.cbegin(); it != baseSpeedSettings.cend();
439dba0c291SJonathan Doman          ++it)
440dba0c291SJonathan Doman     {
441dba0c291SJonathan Doman         const uint32_t baseFreq = std::get<uint32_t>(*it);
442dba0c291SJonathan Doman         if (baseFreq > highestBaseSpeed)
443dba0c291SJonathan Doman         {
444dba0c291SJonathan Doman             highestBaseSpeed = baseFreq;
445dba0c291SJonathan Doman             highPriorityGroup = it;
446dba0c291SJonathan Doman         }
447dba0c291SJonathan Doman     }
448dba0c291SJonathan Doman 
449dba0c291SJonathan Doman     nlohmann::json& jsonCoreIds = aResp->res.jsonValue["HighSpeedCoreIDs"];
450dba0c291SJonathan Doman     jsonCoreIds = nlohmann::json::array();
451dba0c291SJonathan Doman 
452dba0c291SJonathan Doman     // There may not be any entries in the D-Bus property, so only populate
453dba0c291SJonathan Doman     // if there was actually something there.
454dba0c291SJonathan Doman     if (highPriorityGroup != baseSpeedSettings.cend())
455dba0c291SJonathan Doman     {
456dba0c291SJonathan Doman         jsonCoreIds = std::get<std::vector<uint32_t>>(*highPriorityGroup);
457dba0c291SJonathan Doman     }
458dba0c291SJonathan Doman }
459dba0c291SJonathan Doman 
460dba0c291SJonathan Doman /**
461dba0c291SJonathan Doman  * Fill out OperatingConfig related items in a Processor resource by requesting
462dba0c291SJonathan Doman  * data from the given D-Bus object.
463dba0c291SJonathan Doman  *
464dba0c291SJonathan Doman  * @param[in,out]   aResp       Async HTTP response.
465dba0c291SJonathan Doman  * @param[in]       cpuId       CPU D-Bus name.
466dba0c291SJonathan Doman  * @param[in]       service     D-Bus service to query.
467dba0c291SJonathan Doman  * @param[in]       objPath     D-Bus object to query.
468dba0c291SJonathan Doman  */
469dba0c291SJonathan Doman inline void getCpuConfigData(const std::shared_ptr<AsyncResp>& aResp,
470dba0c291SJonathan Doman                              const std::string& cpuId,
471dba0c291SJonathan Doman                              const std::string& service,
472dba0c291SJonathan Doman                              const std::string& objPath)
473dba0c291SJonathan Doman {
474dba0c291SJonathan Doman     BMCWEB_LOG_INFO << "Getting CPU operating configs for " << cpuId;
475dba0c291SJonathan Doman 
476dba0c291SJonathan Doman     // First, GetAll CurrentOperatingConfig properties on the object
477dba0c291SJonathan Doman     crow::connections::systemBus->async_method_call(
478dba0c291SJonathan Doman         [aResp, cpuId, service](
479dba0c291SJonathan Doman             const boost::system::error_code ec,
480dba0c291SJonathan Doman             const std::vector<
481dba0c291SJonathan Doman                 std::pair<std::string,
482dba0c291SJonathan Doman                           std::variant<sdbusplus::message::object_path, bool>>>&
483dba0c291SJonathan Doman                 properties) {
484dba0c291SJonathan Doman             if (ec)
485dba0c291SJonathan Doman             {
486dba0c291SJonathan Doman                 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
487dba0c291SJonathan Doman                                    << ec.message();
488dba0c291SJonathan Doman                 messages::internalError(aResp->res);
489dba0c291SJonathan Doman                 return;
490dba0c291SJonathan Doman             }
491dba0c291SJonathan Doman 
492dba0c291SJonathan Doman             nlohmann::json& json = aResp->res.jsonValue;
493dba0c291SJonathan Doman 
494dba0c291SJonathan Doman             for (const auto& [dbusPropName, variantVal] : properties)
495dba0c291SJonathan Doman             {
496dba0c291SJonathan Doman                 if (dbusPropName == "AppliedConfig")
497dba0c291SJonathan Doman                 {
498dba0c291SJonathan Doman                     const sdbusplus::message::object_path* dbusPathWrapper =
499dba0c291SJonathan Doman                         std::get_if<sdbusplus::message::object_path>(
500dba0c291SJonathan Doman                             &variantVal);
501dba0c291SJonathan Doman                     if (dbusPathWrapper == nullptr)
502dba0c291SJonathan Doman                     {
503dba0c291SJonathan Doman                         continue;
504dba0c291SJonathan Doman                     }
505dba0c291SJonathan Doman 
506dba0c291SJonathan Doman                     const std::string& dbusPath = dbusPathWrapper->str;
507dba0c291SJonathan Doman                     std::string uri = "/redfish/v1/Systems/system/Processors/" +
508dba0c291SJonathan Doman                                       cpuId + "/OperatingConfigs";
509dba0c291SJonathan Doman                     json["OperatingConfigs"] = {{"@odata.id", uri}};
510dba0c291SJonathan Doman 
511dba0c291SJonathan Doman                     // Reuse the D-Bus config object name for the Redfish
512dba0c291SJonathan Doman                     // URI
513dba0c291SJonathan Doman                     size_t baseNamePos = dbusPath.rfind('/');
514dba0c291SJonathan Doman                     if (baseNamePos == std::string::npos ||
515dba0c291SJonathan Doman                         baseNamePos == (dbusPath.size() - 1))
516dba0c291SJonathan Doman                     {
517dba0c291SJonathan Doman                         // If the AppliedConfig was somehow not a valid path,
518dba0c291SJonathan Doman                         // skip adding any more properties, since everything
519dba0c291SJonathan Doman                         // else is tied to this applied config.
520dba0c291SJonathan Doman                         messages::internalError(aResp->res);
521dba0c291SJonathan Doman                         break;
522dba0c291SJonathan Doman                     }
523dba0c291SJonathan Doman                     uri += '/';
524dba0c291SJonathan Doman                     uri += dbusPath.substr(baseNamePos + 1);
525dba0c291SJonathan Doman                     json["AppliedOperatingConfig"] = {{"@odata.id", uri}};
526dba0c291SJonathan Doman 
527dba0c291SJonathan Doman                     // Once we found the current applied config, queue another
528dba0c291SJonathan Doman                     // request to read the base freq core ids out of that
529dba0c291SJonathan Doman                     // config.
530dba0c291SJonathan Doman                     crow::connections::systemBus->async_method_call(
531dba0c291SJonathan Doman                         [aResp](
532dba0c291SJonathan Doman                             const boost::system::error_code ec,
533dba0c291SJonathan Doman                             const std::variant<
534dba0c291SJonathan Doman                                 BaseSpeedPrioritySettingsProperty>& property) {
535dba0c291SJonathan Doman                             if (ec)
536dba0c291SJonathan Doman                             {
537dba0c291SJonathan Doman                                 BMCWEB_LOG_WARNING
538dba0c291SJonathan Doman                                     << "D-Bus Property Get error: " << ec;
539dba0c291SJonathan Doman                                 messages::internalError(aResp->res);
540dba0c291SJonathan Doman                                 return;
541dba0c291SJonathan Doman                             }
542dba0c291SJonathan Doman                             auto baseSpeedList =
543dba0c291SJonathan Doman                                 std::get_if<BaseSpeedPrioritySettingsProperty>(
544dba0c291SJonathan Doman                                     &property);
545dba0c291SJonathan Doman                             if (baseSpeedList != nullptr)
546dba0c291SJonathan Doman                             {
547dba0c291SJonathan Doman                                 highSpeedCoreIdsHandler(aResp, *baseSpeedList);
548dba0c291SJonathan Doman                             }
549dba0c291SJonathan Doman                         },
550dba0c291SJonathan Doman                         service, dbusPath, "org.freedesktop.DBus.Properties",
551dba0c291SJonathan Doman                         "Get",
552dba0c291SJonathan Doman                         "xyz.openbmc_project.Inventory.Item.Cpu."
553dba0c291SJonathan Doman                         "OperatingConfig",
554dba0c291SJonathan Doman                         "BaseSpeedPrioritySettings");
555dba0c291SJonathan Doman                 }
556dba0c291SJonathan Doman                 else if (dbusPropName == "BaseSpeedPriorityEnabled")
557dba0c291SJonathan Doman                 {
558dba0c291SJonathan Doman                     const bool* state = std::get_if<bool>(&variantVal);
559dba0c291SJonathan Doman                     if (state != nullptr)
560dba0c291SJonathan Doman                     {
561dba0c291SJonathan Doman                         json["BaseSpeedPriorityState"] =
562dba0c291SJonathan Doman                             *state ? "Enabled" : "Disabled";
563dba0c291SJonathan Doman                     }
564dba0c291SJonathan Doman                 }
565dba0c291SJonathan Doman             }
566dba0c291SJonathan Doman         },
567dba0c291SJonathan Doman         service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
568dba0c291SJonathan Doman         "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig");
569dba0c291SJonathan Doman }
570dba0c291SJonathan Doman 
571*cba4f448SSunnySrivastava1984 /**
572*cba4f448SSunnySrivastava1984  * @brief Fill out location info of a processor by
573*cba4f448SSunnySrivastava1984  * requesting data from the given D-Bus object.
574*cba4f448SSunnySrivastava1984  *
575*cba4f448SSunnySrivastava1984  * @param[in,out]   aResp       Async HTTP response.
576*cba4f448SSunnySrivastava1984  * @param[in]       service     D-Bus service to query.
577*cba4f448SSunnySrivastava1984  * @param[in]       objPath     D-Bus object to query.
578*cba4f448SSunnySrivastava1984  */
579*cba4f448SSunnySrivastava1984 inline void getCpuLocationCode(std::shared_ptr<AsyncResp> aResp,
580*cba4f448SSunnySrivastava1984                                const std::string& service,
581*cba4f448SSunnySrivastava1984                                const std::string& objPath)
582*cba4f448SSunnySrivastava1984 {
583*cba4f448SSunnySrivastava1984     BMCWEB_LOG_DEBUG << "Get Cpu Location Data";
584*cba4f448SSunnySrivastava1984     crow::connections::systemBus->async_method_call(
585*cba4f448SSunnySrivastava1984         [objPath,
586*cba4f448SSunnySrivastava1984          aResp{std::move(aResp)}](const boost::system::error_code ec,
587*cba4f448SSunnySrivastava1984                                   const std::variant<std::string>& property) {
588*cba4f448SSunnySrivastava1984             if (ec)
589*cba4f448SSunnySrivastava1984             {
590*cba4f448SSunnySrivastava1984                 BMCWEB_LOG_DEBUG << "DBUS response error";
591*cba4f448SSunnySrivastava1984                 messages::internalError(aResp->res);
592*cba4f448SSunnySrivastava1984                 return;
593*cba4f448SSunnySrivastava1984             }
594*cba4f448SSunnySrivastava1984 
595*cba4f448SSunnySrivastava1984             const std::string* value = std::get_if<std::string>(&property);
596*cba4f448SSunnySrivastava1984 
597*cba4f448SSunnySrivastava1984             if (value == nullptr)
598*cba4f448SSunnySrivastava1984             {
599*cba4f448SSunnySrivastava1984                 // illegal value
600*cba4f448SSunnySrivastava1984                 BMCWEB_LOG_DEBUG << "Location code value error";
601*cba4f448SSunnySrivastava1984                 messages::internalError(aResp->res);
602*cba4f448SSunnySrivastava1984                 return;
603*cba4f448SSunnySrivastava1984             }
604*cba4f448SSunnySrivastava1984 
605*cba4f448SSunnySrivastava1984             aResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
606*cba4f448SSunnySrivastava1984                 *value;
607*cba4f448SSunnySrivastava1984         },
608*cba4f448SSunnySrivastava1984         service, objPath, "org.freedesktop.DBus.Properties", "Get",
609*cba4f448SSunnySrivastava1984         "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode");
610*cba4f448SSunnySrivastava1984 }
611*cba4f448SSunnySrivastava1984 
612ac6a4445SGunnar Mills inline void getProcessorData(std::shared_ptr<AsyncResp> aResp,
6132bab9831SJonathan Doman                              const std::string& processorId)
614ac6a4445SGunnar Mills {
615ac6a4445SGunnar Mills     BMCWEB_LOG_DEBUG << "Get available system processor resources.";
616ac6a4445SGunnar Mills 
617ac6a4445SGunnar Mills     crow::connections::systemBus->async_method_call(
6182bab9831SJonathan Doman         [processorId,
6192bab9831SJonathan Doman          aResp{std::move(aResp)}](const boost::system::error_code ec,
6202bab9831SJonathan Doman                                   const MapperGetSubTreeResponse& subtree) {
621ac6a4445SGunnar Mills             if (ec)
622ac6a4445SGunnar Mills             {
623ac6a4445SGunnar Mills                 BMCWEB_LOG_DEBUG << "DBUS response error";
624ac6a4445SGunnar Mills                 messages::internalError(aResp->res);
625ac6a4445SGunnar Mills                 return;
626ac6a4445SGunnar Mills             }
6272bab9831SJonathan Doman             for (const auto& [objectPath, serviceMap] : subtree)
628ac6a4445SGunnar Mills             {
6292bab9831SJonathan Doman                 // Ignore any objects which don't end with our desired cpu name
6302bab9831SJonathan Doman                 if (!boost::ends_with(objectPath, processorId))
631ac6a4445SGunnar Mills                 {
6322bab9831SJonathan Doman                     continue;
633ac6a4445SGunnar Mills                 }
6342bab9831SJonathan Doman 
6352bab9831SJonathan Doman                 // Process the first object which does match our cpu name
6362bab9831SJonathan Doman                 // suffix, and potentially ignore any other matching objects.
6372bab9831SJonathan Doman                 // Assume all interfaces we want to process must be on the same
6382bab9831SJonathan Doman                 // object.
6392bab9831SJonathan Doman 
6402bab9831SJonathan Doman                 for (const auto& [serviceName, interfaceList] : serviceMap)
641ac6a4445SGunnar Mills                 {
6422bab9831SJonathan Doman                     for (const auto& interface : interfaceList)
6432bab9831SJonathan Doman                     {
6442bab9831SJonathan Doman                         if (interface ==
6452bab9831SJonathan Doman                             "xyz.openbmc_project.Inventory.Decorator.Asset")
6462bab9831SJonathan Doman                         {
6472bab9831SJonathan Doman                             getCpuAssetData(aResp, serviceName, objectPath);
648ac6a4445SGunnar Mills                         }
6492bab9831SJonathan Doman                         else if (interface == "xyz.openbmc_project.Inventory."
6502bab9831SJonathan Doman                                               "Decorator.Revision")
651ac6a4445SGunnar Mills                         {
6522bab9831SJonathan Doman                             getCpuRevisionData(aResp, serviceName, objectPath);
653ac6a4445SGunnar Mills                         }
6542bab9831SJonathan Doman                         else if (interface ==
6552bab9831SJonathan Doman                                  "xyz.openbmc_project.Inventory.Item.Cpu")
656ac6a4445SGunnar Mills                         {
6572bab9831SJonathan Doman                             getCpuDataByService(aResp, processorId, serviceName,
6582bab9831SJonathan Doman                                                 objectPath);
6592bab9831SJonathan Doman                         }
6602bab9831SJonathan Doman                         else if (interface == "xyz.openbmc_project.Inventory."
6612bab9831SJonathan Doman                                               "Item.Accelerator")
6622bab9831SJonathan Doman                         {
6632bab9831SJonathan Doman                             getAcceleratorDataByService(
6642bab9831SJonathan Doman                                 aResp, processorId, serviceName, objectPath);
665ac6a4445SGunnar Mills                         }
666dba0c291SJonathan Doman                         else if (interface ==
667dba0c291SJonathan Doman                                  "xyz.openbmc_project.Control.Processor."
668dba0c291SJonathan Doman                                  "CurrentOperatingConfig")
669dba0c291SJonathan Doman                         {
670dba0c291SJonathan Doman                             getCpuConfigData(aResp, processorId, serviceName,
671dba0c291SJonathan Doman                                              objectPath);
672dba0c291SJonathan Doman                         }
673*cba4f448SSunnySrivastava1984                         else if (interface == "xyz.openbmc_project.Inventory."
674*cba4f448SSunnySrivastava1984                                               "Decorator.LocationCode")
675*cba4f448SSunnySrivastava1984                         {
676*cba4f448SSunnySrivastava1984                             getCpuLocationCode(aResp, serviceName, objectPath);
677*cba4f448SSunnySrivastava1984                         }
678ac6a4445SGunnar Mills                     }
679ac6a4445SGunnar Mills                 }
680ac6a4445SGunnar Mills                 return;
681ac6a4445SGunnar Mills             }
682ac6a4445SGunnar Mills             // Object not found
683ac6a4445SGunnar Mills             messages::resourceNotFound(aResp->res, "Processor", processorId);
684ac6a4445SGunnar Mills             return;
685ac6a4445SGunnar Mills         },
686ac6a4445SGunnar Mills         "xyz.openbmc_project.ObjectMapper",
687ac6a4445SGunnar Mills         "/xyz/openbmc_project/object_mapper",
688ac6a4445SGunnar Mills         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
6892bab9831SJonathan Doman         "/xyz/openbmc_project/inventory", 0,
690*cba4f448SSunnySrivastava1984         std::array<const char*, 6>{
6912bab9831SJonathan Doman             "xyz.openbmc_project.Inventory.Decorator.Asset",
6922bab9831SJonathan Doman             "xyz.openbmc_project.Inventory.Decorator.Revision",
6932bab9831SJonathan Doman             "xyz.openbmc_project.Inventory.Item.Cpu",
694*cba4f448SSunnySrivastava1984             "xyz.openbmc_project.Inventory.Decorator.LocationCode",
695dba0c291SJonathan Doman             "xyz.openbmc_project.Inventory.Item.Accelerator",
696dba0c291SJonathan Doman             "xyz.openbmc_project.Control.Processor.CurrentOperatingConfig"});
697ac6a4445SGunnar Mills }
698ac6a4445SGunnar Mills 
699dba0c291SJonathan Doman /**
700dba0c291SJonathan Doman  * Request all the properties for the given D-Bus object and fill out the
701dba0c291SJonathan Doman  * related entries in the Redfish OperatingConfig response.
702dba0c291SJonathan Doman  *
703dba0c291SJonathan Doman  * @param[in,out]   aResp       Async HTTP response.
704dba0c291SJonathan Doman  * @param[in]       service     D-Bus service name to query.
705dba0c291SJonathan Doman  * @param[in]       objPath     D-Bus object to query.
706dba0c291SJonathan Doman  */
707dba0c291SJonathan Doman inline void getOperatingConfigData(const std::shared_ptr<AsyncResp>& aResp,
708dba0c291SJonathan Doman                                    const std::string& service,
709dba0c291SJonathan Doman                                    const std::string& objPath)
710dba0c291SJonathan Doman {
711dba0c291SJonathan Doman     crow::connections::systemBus->async_method_call(
712dba0c291SJonathan Doman         [aResp](boost::system::error_code ec,
713dba0c291SJonathan Doman                 const OperatingConfigProperties& properties) {
714dba0c291SJonathan Doman             if (ec)
715dba0c291SJonathan Doman             {
716dba0c291SJonathan Doman                 BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
717dba0c291SJonathan Doman                                    << ec.message();
718dba0c291SJonathan Doman                 messages::internalError(aResp->res);
719dba0c291SJonathan Doman                 return;
720dba0c291SJonathan Doman             }
721dba0c291SJonathan Doman 
722dba0c291SJonathan Doman             nlohmann::json& json = aResp->res.jsonValue;
723dba0c291SJonathan Doman             for (const auto& [key, variant] : properties)
724dba0c291SJonathan Doman             {
725dba0c291SJonathan Doman                 if (key == "AvailableCoreCount")
726dba0c291SJonathan Doman                 {
727dba0c291SJonathan Doman                     const size_t* cores = std::get_if<size_t>(&variant);
728dba0c291SJonathan Doman                     if (cores != nullptr)
729dba0c291SJonathan Doman                     {
730dba0c291SJonathan Doman                         json["TotalAvailableCoreCount"] = *cores;
731dba0c291SJonathan Doman                     }
732dba0c291SJonathan Doman                 }
733dba0c291SJonathan Doman                 else if (key == "BaseSpeed")
734dba0c291SJonathan Doman                 {
735dba0c291SJonathan Doman                     const uint32_t* speed = std::get_if<uint32_t>(&variant);
736dba0c291SJonathan Doman                     if (speed != nullptr)
737dba0c291SJonathan Doman                     {
738dba0c291SJonathan Doman                         json["BaseSpeedMHz"] = *speed;
739dba0c291SJonathan Doman                     }
740dba0c291SJonathan Doman                 }
741dba0c291SJonathan Doman                 else if (key == "MaxJunctionTemperature")
742dba0c291SJonathan Doman                 {
743dba0c291SJonathan Doman                     const uint32_t* temp = std::get_if<uint32_t>(&variant);
744dba0c291SJonathan Doman                     if (temp != nullptr)
745dba0c291SJonathan Doman                     {
746dba0c291SJonathan Doman                         json["MaxJunctionTemperatureCelsius"] = *temp;
747dba0c291SJonathan Doman                     }
748dba0c291SJonathan Doman                 }
749dba0c291SJonathan Doman                 else if (key == "MaxSpeed")
750dba0c291SJonathan Doman                 {
751dba0c291SJonathan Doman                     const uint32_t* speed = std::get_if<uint32_t>(&variant);
752dba0c291SJonathan Doman                     if (speed != nullptr)
753dba0c291SJonathan Doman                     {
754dba0c291SJonathan Doman                         json["MaxSpeedMHz"] = *speed;
755dba0c291SJonathan Doman                     }
756dba0c291SJonathan Doman                 }
757dba0c291SJonathan Doman                 else if (key == "PowerLimit")
758dba0c291SJonathan Doman                 {
759dba0c291SJonathan Doman                     const uint32_t* tdp = std::get_if<uint32_t>(&variant);
760dba0c291SJonathan Doman                     if (tdp != nullptr)
761dba0c291SJonathan Doman                     {
762dba0c291SJonathan Doman                         json["TDPWatts"] = *tdp;
763dba0c291SJonathan Doman                     }
764dba0c291SJonathan Doman                 }
765dba0c291SJonathan Doman                 else if (key == "TurboProfile")
766dba0c291SJonathan Doman                 {
767dba0c291SJonathan Doman                     const auto* turboList =
768dba0c291SJonathan Doman                         std::get_if<TurboProfileProperty>(&variant);
769dba0c291SJonathan Doman                     if (turboList == nullptr)
770dba0c291SJonathan Doman                     {
771dba0c291SJonathan Doman                         continue;
772dba0c291SJonathan Doman                     }
773dba0c291SJonathan Doman 
774dba0c291SJonathan Doman                     nlohmann::json& turboArray = json["TurboProfile"];
775dba0c291SJonathan Doman                     turboArray = nlohmann::json::array();
776dba0c291SJonathan Doman                     for (const auto& [turboSpeed, coreCount] : *turboList)
777dba0c291SJonathan Doman                     {
778dba0c291SJonathan Doman                         turboArray.push_back({{"ActiveCoreCount", coreCount},
779dba0c291SJonathan Doman                                               {"MaxSpeedMHz", turboSpeed}});
780dba0c291SJonathan Doman                     }
781dba0c291SJonathan Doman                 }
782dba0c291SJonathan Doman                 else if (key == "BaseSpeedPrioritySettings")
783dba0c291SJonathan Doman                 {
784dba0c291SJonathan Doman                     const auto* baseSpeedList =
785dba0c291SJonathan Doman                         std::get_if<BaseSpeedPrioritySettingsProperty>(
786dba0c291SJonathan Doman                             &variant);
787dba0c291SJonathan Doman                     if (baseSpeedList == nullptr)
788dba0c291SJonathan Doman                     {
789dba0c291SJonathan Doman                         continue;
790dba0c291SJonathan Doman                     }
791dba0c291SJonathan Doman 
792dba0c291SJonathan Doman                     nlohmann::json& baseSpeedArray =
793dba0c291SJonathan Doman                         json["BaseSpeedPrioritySettings"];
794dba0c291SJonathan Doman                     baseSpeedArray = nlohmann::json::array();
795dba0c291SJonathan Doman                     for (const auto& [baseSpeed, coreList] : *baseSpeedList)
796dba0c291SJonathan Doman                     {
797dba0c291SJonathan Doman                         baseSpeedArray.push_back(
798dba0c291SJonathan Doman                             {{"CoreCount", coreList.size()},
799dba0c291SJonathan Doman                              {"CoreIDs", coreList},
800dba0c291SJonathan Doman                              {"BaseSpeedMHz", baseSpeed}});
801dba0c291SJonathan Doman                     }
802dba0c291SJonathan Doman                 }
803dba0c291SJonathan Doman             }
804dba0c291SJonathan Doman         },
805dba0c291SJonathan Doman         service, objPath, "org.freedesktop.DBus.Properties", "GetAll",
806dba0c291SJonathan Doman         "xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig");
807dba0c291SJonathan Doman }
808dba0c291SJonathan Doman 
809dba0c291SJonathan Doman class OperatingConfigCollection : public Node
810dba0c291SJonathan Doman {
811dba0c291SJonathan Doman   public:
812dba0c291SJonathan Doman     OperatingConfigCollection(App& app) :
813dba0c291SJonathan Doman         Node(app,
814dba0c291SJonathan Doman              "/redfish/v1/Systems/system/Processors/<str>/OperatingConfigs/",
815dba0c291SJonathan Doman              std::string())
816dba0c291SJonathan Doman     {
817dba0c291SJonathan Doman         // Defined by Redfish spec privilege registry
818dba0c291SJonathan Doman         entityPrivileges = {
819dba0c291SJonathan Doman             {boost::beast::http::verb::get, {{"Login"}}},
820dba0c291SJonathan Doman             {boost::beast::http::verb::head, {{"Login"}}},
821dba0c291SJonathan Doman             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
822dba0c291SJonathan Doman             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
823dba0c291SJonathan Doman             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
824dba0c291SJonathan Doman             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
825dba0c291SJonathan Doman     }
826dba0c291SJonathan Doman 
827dba0c291SJonathan Doman   private:
828dba0c291SJonathan Doman     void doGet(crow::Response& res, const crow::Request& req,
829dba0c291SJonathan Doman                const std::vector<std::string>& params) override
830dba0c291SJonathan Doman     {
831dba0c291SJonathan Doman         if (params.size() != 1)
832dba0c291SJonathan Doman         {
833dba0c291SJonathan Doman             messages::internalError(res);
834dba0c291SJonathan Doman             res.end();
835dba0c291SJonathan Doman             return;
836dba0c291SJonathan Doman         }
837dba0c291SJonathan Doman 
838dba0c291SJonathan Doman         const std::string& cpuName = params[0];
839dba0c291SJonathan Doman         res.jsonValue["@odata.type"] =
840dba0c291SJonathan Doman             "#OperatingConfigCollection.OperatingConfigCollection";
841dba0c291SJonathan Doman         res.jsonValue["@odata.id"] = req.url;
842dba0c291SJonathan Doman         res.jsonValue["Name"] = "Operating Config Collection";
843dba0c291SJonathan Doman 
844dba0c291SJonathan Doman         auto asyncResp = std::make_shared<AsyncResp>(res);
845dba0c291SJonathan Doman 
846dba0c291SJonathan Doman         // First find the matching CPU object so we know how to constrain our
847dba0c291SJonathan Doman         // search for related Config objects.
848dba0c291SJonathan Doman         crow::connections::systemBus->async_method_call(
849dba0c291SJonathan Doman             [asyncResp, cpuName](const boost::system::error_code ec,
850dba0c291SJonathan Doman                                  const std::vector<std::string>& objects) {
851dba0c291SJonathan Doman                 if (ec)
852dba0c291SJonathan Doman                 {
853dba0c291SJonathan Doman                     BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
854dba0c291SJonathan Doman                                        << ec.message();
855dba0c291SJonathan Doman                     messages::internalError(asyncResp->res);
856dba0c291SJonathan Doman                     return;
857dba0c291SJonathan Doman                 }
858dba0c291SJonathan Doman 
859dba0c291SJonathan Doman                 for (const std::string& object : objects)
860dba0c291SJonathan Doman                 {
861dba0c291SJonathan Doman                     if (!boost::ends_with(object, cpuName))
862dba0c291SJonathan Doman                     {
863dba0c291SJonathan Doman                         continue;
864dba0c291SJonathan Doman                     }
865dba0c291SJonathan Doman 
866dba0c291SJonathan Doman                     // Not expected that there will be multiple matching CPU
867dba0c291SJonathan Doman                     // objects, but if there are just use the first one.
868dba0c291SJonathan Doman 
869dba0c291SJonathan Doman                     // Use the common search routine to construct the Collection
870dba0c291SJonathan Doman                     // of all Config objects under this CPU.
871dba0c291SJonathan Doman                     collection_util::getCollectionMembers(
872dba0c291SJonathan Doman                         asyncResp,
873dba0c291SJonathan Doman                         "/redfish/v1/Systems/system/Processors/" + cpuName +
874dba0c291SJonathan Doman                             "/OperatingConfigs",
875dba0c291SJonathan Doman                         {"xyz.openbmc_project.Inventory.Item.Cpu."
876dba0c291SJonathan Doman                          "OperatingConfig"},
877dba0c291SJonathan Doman                         object.c_str());
878dba0c291SJonathan Doman                     return;
879dba0c291SJonathan Doman                 }
880dba0c291SJonathan Doman             },
881dba0c291SJonathan Doman             "xyz.openbmc_project.ObjectMapper",
882dba0c291SJonathan Doman             "/xyz/openbmc_project/object_mapper",
883dba0c291SJonathan Doman             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
884dba0c291SJonathan Doman             "/xyz/openbmc_project/inventory", 0,
885dba0c291SJonathan Doman             std::array<const char*, 1>{"xyz.openbmc_project.Control.Processor."
886dba0c291SJonathan Doman                                        "CurrentOperatingConfig"});
887dba0c291SJonathan Doman     }
888dba0c291SJonathan Doman };
889dba0c291SJonathan Doman 
890dba0c291SJonathan Doman class OperatingConfig : public Node
891dba0c291SJonathan Doman {
892dba0c291SJonathan Doman   public:
893dba0c291SJonathan Doman     OperatingConfig(App& app) :
894dba0c291SJonathan Doman         Node(app,
895dba0c291SJonathan Doman              "/redfish/v1/Systems/system/Processors/<str>/OperatingConfigs/"
896dba0c291SJonathan Doman              "<str>/",
897dba0c291SJonathan Doman              std::string(), std::string())
898dba0c291SJonathan Doman     {
899dba0c291SJonathan Doman         // Defined by Redfish spec privilege registry
900dba0c291SJonathan Doman         entityPrivileges = {
901dba0c291SJonathan Doman             {boost::beast::http::verb::get, {{"Login"}}},
902dba0c291SJonathan Doman             {boost::beast::http::verb::head, {{"Login"}}},
903dba0c291SJonathan Doman             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
904dba0c291SJonathan Doman             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
905dba0c291SJonathan Doman             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
906dba0c291SJonathan Doman             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
907dba0c291SJonathan Doman     }
908dba0c291SJonathan Doman 
909dba0c291SJonathan Doman   private:
910dba0c291SJonathan Doman     void doGet(crow::Response& res, const crow::Request& req,
911dba0c291SJonathan Doman                const std::vector<std::string>& params) override
912dba0c291SJonathan Doman     {
913dba0c291SJonathan Doman         if (params.size() != 2)
914dba0c291SJonathan Doman         {
915dba0c291SJonathan Doman             messages::internalError(res);
916dba0c291SJonathan Doman             res.end();
917dba0c291SJonathan Doman             return;
918dba0c291SJonathan Doman         }
919dba0c291SJonathan Doman 
920dba0c291SJonathan Doman         const std::string& cpuName = params[0];
921dba0c291SJonathan Doman         const std::string& configName = params[1];
922dba0c291SJonathan Doman 
923dba0c291SJonathan Doman         auto asyncResp = std::make_shared<AsyncResp>(res);
924dba0c291SJonathan Doman 
925dba0c291SJonathan Doman         // Ask for all objects implementing OperatingConfig so we can search for
926dba0c291SJonathan Doman         // one with a matching name
927dba0c291SJonathan Doman         crow::connections::systemBus->async_method_call(
928dba0c291SJonathan Doman             [asyncResp, cpuName, configName,
929dba0c291SJonathan Doman              reqUrl{req.url}](boost::system::error_code ec,
930dba0c291SJonathan Doman                               const MapperGetSubTreeResponse& subtree) {
931dba0c291SJonathan Doman                 if (ec)
932dba0c291SJonathan Doman                 {
933dba0c291SJonathan Doman                     BMCWEB_LOG_WARNING << "D-Bus error: " << ec << ", "
934dba0c291SJonathan Doman                                        << ec.message();
935dba0c291SJonathan Doman                     messages::internalError(asyncResp->res);
936dba0c291SJonathan Doman                     return;
937dba0c291SJonathan Doman                 }
938dba0c291SJonathan Doman                 const std::string expectedEnding = cpuName + '/' + configName;
939dba0c291SJonathan Doman                 for (const auto& [objectPath, serviceMap] : subtree)
940dba0c291SJonathan Doman                 {
941dba0c291SJonathan Doman                     // Ignore any configs without matching cpuX/configY
942dba0c291SJonathan Doman                     if (!boost::ends_with(objectPath, expectedEnding) ||
943dba0c291SJonathan Doman                         serviceMap.empty())
944dba0c291SJonathan Doman                     {
945dba0c291SJonathan Doman                         continue;
946dba0c291SJonathan Doman                     }
947dba0c291SJonathan Doman 
948dba0c291SJonathan Doman                     nlohmann::json& json = asyncResp->res.jsonValue;
949dba0c291SJonathan Doman                     json["@odata.type"] =
950dba0c291SJonathan Doman                         "#OperatingConfig.v1_0_0.OperatingConfig";
951dba0c291SJonathan Doman                     json["@odata.id"] = reqUrl;
952dba0c291SJonathan Doman                     json["Name"] = "Processor Profile";
953dba0c291SJonathan Doman                     json["Id"] = configName;
954dba0c291SJonathan Doman 
955dba0c291SJonathan Doman                     // Just use the first implementation of the object - not
956dba0c291SJonathan Doman                     // expected that there would be multiple matching services
957dba0c291SJonathan Doman                     getOperatingConfigData(asyncResp, serviceMap.begin()->first,
958dba0c291SJonathan Doman                                            objectPath);
959dba0c291SJonathan Doman                     return;
960dba0c291SJonathan Doman                 }
961dba0c291SJonathan Doman                 messages::resourceNotFound(asyncResp->res, "OperatingConfig",
962dba0c291SJonathan Doman                                            configName);
963dba0c291SJonathan Doman             },
964dba0c291SJonathan Doman             "xyz.openbmc_project.ObjectMapper",
965dba0c291SJonathan Doman             "/xyz/openbmc_project/object_mapper",
966dba0c291SJonathan Doman             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
967dba0c291SJonathan Doman             "/xyz/openbmc_project/inventory", 0,
968dba0c291SJonathan Doman             std::array<const char*, 1>{
969dba0c291SJonathan Doman                 "xyz.openbmc_project.Inventory.Item.Cpu.OperatingConfig"});
970dba0c291SJonathan Doman     }
971dba0c291SJonathan Doman };
972dba0c291SJonathan Doman 
973ac6a4445SGunnar Mills class ProcessorCollection : public Node
974ac6a4445SGunnar Mills {
975ac6a4445SGunnar Mills   public:
976ac6a4445SGunnar Mills     /*
977ac6a4445SGunnar Mills      * Default Constructor
978ac6a4445SGunnar Mills      */
979ac6a4445SGunnar Mills     ProcessorCollection(App& app) :
980ac6a4445SGunnar Mills         Node(app, "/redfish/v1/Systems/system/Processors/")
981ac6a4445SGunnar Mills     {
982ac6a4445SGunnar Mills         entityPrivileges = {
983ac6a4445SGunnar Mills             {boost::beast::http::verb::get, {{"Login"}}},
984ac6a4445SGunnar Mills             {boost::beast::http::verb::head, {{"Login"}}},
985ac6a4445SGunnar Mills             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
986ac6a4445SGunnar Mills             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
987ac6a4445SGunnar Mills             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
988ac6a4445SGunnar Mills             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
989ac6a4445SGunnar Mills     }
990ac6a4445SGunnar Mills 
991ac6a4445SGunnar Mills   private:
992ac6a4445SGunnar Mills     /**
993ac6a4445SGunnar Mills      * Functions triggers appropriate requests on DBus
994ac6a4445SGunnar Mills      */
995ac6a4445SGunnar Mills     void doGet(crow::Response& res, const crow::Request&,
996ac6a4445SGunnar Mills                const std::vector<std::string>&) override
997ac6a4445SGunnar Mills     {
998ac6a4445SGunnar Mills         res.jsonValue["@odata.type"] =
999ac6a4445SGunnar Mills             "#ProcessorCollection.ProcessorCollection";
1000ac6a4445SGunnar Mills         res.jsonValue["Name"] = "Processor Collection";
1001ac6a4445SGunnar Mills 
10029dedf572SGunnar Mills         res.jsonValue["@odata.id"] = "/redfish/v1/Systems/system/Processors";
1003ac6a4445SGunnar Mills         auto asyncResp = std::make_shared<AsyncResp>(res);
1004ac6a4445SGunnar Mills 
100505030b8eSGunnar Mills         collection_util::getCollectionMembers(
100605030b8eSGunnar Mills             asyncResp, "/redfish/v1/Systems/system/Processors",
1007ac6a4445SGunnar Mills             {"xyz.openbmc_project.Inventory.Item.Cpu",
1008ac6a4445SGunnar Mills              "xyz.openbmc_project.Inventory.Item.Accelerator"});
1009ac6a4445SGunnar Mills     }
1010ac6a4445SGunnar Mills };
1011ac6a4445SGunnar Mills 
1012ac6a4445SGunnar Mills class Processor : public Node
1013ac6a4445SGunnar Mills {
1014ac6a4445SGunnar Mills   public:
1015ac6a4445SGunnar Mills     /*
1016ac6a4445SGunnar Mills      * Default Constructor
1017ac6a4445SGunnar Mills      */
1018ac6a4445SGunnar Mills     Processor(App& app) :
1019ac6a4445SGunnar Mills         Node(app, "/redfish/v1/Systems/system/Processors/<str>/", std::string())
1020ac6a4445SGunnar Mills     {
1021ac6a4445SGunnar Mills         entityPrivileges = {
1022ac6a4445SGunnar Mills             {boost::beast::http::verb::get, {{"Login"}}},
1023ac6a4445SGunnar Mills             {boost::beast::http::verb::head, {{"Login"}}},
1024ac6a4445SGunnar Mills             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1025ac6a4445SGunnar Mills             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1026ac6a4445SGunnar Mills             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1027ac6a4445SGunnar Mills             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1028ac6a4445SGunnar Mills     }
1029ac6a4445SGunnar Mills 
1030ac6a4445SGunnar Mills   private:
1031ac6a4445SGunnar Mills     /**
1032ac6a4445SGunnar Mills      * Functions triggers appropriate requests on DBus
1033ac6a4445SGunnar Mills      */
1034ac6a4445SGunnar Mills     void doGet(crow::Response& res, const crow::Request&,
1035ac6a4445SGunnar Mills                const std::vector<std::string>& params) override
1036ac6a4445SGunnar Mills     {
1037ac6a4445SGunnar Mills         // Check if there is required param, truly entering this shall be
1038ac6a4445SGunnar Mills         // impossible
1039ac6a4445SGunnar Mills         if (params.size() != 1)
1040ac6a4445SGunnar Mills         {
1041ac6a4445SGunnar Mills             messages::internalError(res);
1042ac6a4445SGunnar Mills 
1043ac6a4445SGunnar Mills             res.end();
1044ac6a4445SGunnar Mills             return;
1045ac6a4445SGunnar Mills         }
1046ac6a4445SGunnar Mills         const std::string& processorId = params[0];
1047*cba4f448SSunnySrivastava1984         res.jsonValue["@odata.type"] = "#Processor.v1_11_0.Processor";
1048ac6a4445SGunnar Mills         res.jsonValue["@odata.id"] =
1049ac6a4445SGunnar Mills             "/redfish/v1/Systems/system/Processors/" + processorId;
1050ac6a4445SGunnar Mills 
1051ac6a4445SGunnar Mills         auto asyncResp = std::make_shared<AsyncResp>(res);
1052ac6a4445SGunnar Mills 
10532bab9831SJonathan Doman         getProcessorData(asyncResp, processorId);
1054ac6a4445SGunnar Mills     }
1055ac6a4445SGunnar Mills };
1056ac6a4445SGunnar Mills 
1057ac6a4445SGunnar Mills } // namespace redfish
1058