1 #include "cfam_access.hpp"
2 #include "p9_cfam.hpp"
3 #include "registration.hpp"
4 #include "targeting.hpp"
5 
6 #include <fstream>
7 #include <iostream>
8 #include <phosphor-logging/elog-errors.hpp>
9 #include <phosphor-logging/elog.hpp>
10 #include <sstream>
11 #include <xyz/openbmc_project/Common/error.hpp>
12 
13 /* File /var/lib/obmc/cfam_overrides requires whitespace-separated parameters
14 Pos Address Data Mask with one register write per line. For example:
15 0 0x283F 0x12345678 0xF0F0F0F0
16 0 0x283F 0x87654321 0x0F0F0F0F
17 Blank lines and comment lines beginning with # will be ignored. */
18 
19 namespace openpower
20 {
21 namespace p9
22 {
23 
24 using namespace openpower::cfam::access;
25 using namespace openpower::targeting;
26 using namespace openpower::util;
27 
28 void CFAMOverride()
29 {
30     size_t pos = 0;
31     cfam_address_t address = 0;
32     cfam_data_t data = 0;
33     cfam_mask_t mask = 0;
34 
35     Targeting targets;
36 
37     std::string line;
38 
39     std::ifstream overrides("/var/lib/obmc/cfam_overrides");
40 
41     if (overrides.is_open())
42     {
43         while (std::getline(overrides, line))
44         {
45             if (!line.empty())
46             {
47                 line.erase(0, line.find_first_not_of(" \t\r\n"));
48                 if (!line.empty() && line.at(0) != '#')
49                 {
50                     mask = 0xFFFFFFFF;
51                     if (sscanf(line.c_str(), "%zu %hx %x %x", &pos, &address,
52                                &data, &mask) >= 3)
53                     {
54                         const auto& target = targets.getTarget(pos);
55                         writeRegWithMask(target, address, data, mask);
56                     }
57                     else
58                     {
59                         namespace error =
60                             sdbusplus::xyz::openbmc_project::Common::Error;
61                         namespace metadata =
62                             phosphor::logging::xyz::openbmc_project::Common;
63                         phosphor::logging::elog<error::InvalidArgument>(
64                             metadata::InvalidArgument::ARGUMENT_NAME("line"),
65                             metadata::InvalidArgument::ARGUMENT_VALUE(
66                                 line.c_str()));
67                     }
68                 }
69             }
70         }
71         overrides.close();
72     }
73 
74     return;
75 }
76 
77 REGISTER_PROCEDURE("CFAMOverride", CFAMOverride)
78 
79 } // namespace p9
80 } // namespace openpower
81