xref: /openbmc/openpower-proc-control/targeting.hpp (revision b65423413a236431a5ac53123bf568427610c190)
1 #pragma once
2 
3 #include <memory>
4 #include <vector>
5 #include "filedescriptor.hpp"
6 
7 namespace openpower
8 {
9 namespace targeting
10 {
11 
12 constexpr auto fsiMasterDevPath =
13     "/sys/devices/platform/fsi-master/slave@00:00/raw";
14 
15 constexpr auto fsiSlaveBaseDir = "/sys/devices/platform/fsi-master/slave@00:00/hub@00/";
16 
17 /**
18  * Represents a specific P9 processor in the system.  Used by
19  * the access APIs to specify the chip to operate on.
20  */
21 class Target
22 {
23     public:
24 
25         /**
26          * Constructor
27          *
28          * @param[in] - The logical position of the target
29          * @param[in] - The sysfs device path
30          */
31         Target(size_t position, const std::string& devPath) :
32             pos(position), cfamPath(devPath)
33         {
34         }
35 
36         Target() = delete;
37         ~Target() = default;
38         Target(const Target&) = default;
39         Target(Target&&) = default;
40         Target& operator=(Target&&) = default;
41 
42         /**
43          * Returns the position
44          */
45         inline auto getPos() const
46         {
47             return pos;
48         }
49 
50         /**
51          * Returns the CFAM sysfs path
52          */
53         inline auto getCFAMPath() const
54         {
55             return cfamPath;
56         }
57 
58         /**
59          * Returns the file descriptor to use
60          * for read/writeCFAM operations.
61          */
62         int getCFAMFD();
63 
64     private:
65 
66         /**
67          * The logical position of this target
68          */
69         size_t pos;
70 
71         /**
72          * The sysfs device path for the CFAM
73          */
74         const std::string cfamPath;
75 
76         /**
77          * The file descriptor to use for read/writeCFAMReg
78          */
79         std::unique_ptr<openpower::util::FileDescriptor> cfamFD;
80 };
81 
82 
83 /**
84  * Class that manages processor targeting for FSI operations.
85  */
86 class Targeting
87 {
88     public:
89 
90         /**
91          * Scans sysfs to find all processors and creates Target objects
92          * for them.
93          * @param[in] fsiMasterDev - the sysfs device for the master
94          * @param[in] fsiSlaveDirectory - the base sysfs dir for slaves
95          */
96         Targeting(const std::string& fsiMasterDev,
97                   const std::string& fsiSlaveDir);
98 
99         Targeting() : Targeting(fsiMasterDevPath, fsiSlaveBaseDir) {}
100 
101         ~Targeting() = default;
102         Targeting(const Targeting&) = default;
103         Targeting(Targeting&&) = default;
104         Targeting& operator=(Targeting&&) = default;
105 
106         /**
107          * Returns a const iterator to the first target
108          */
109         inline auto begin()
110         {
111             return targets.cbegin();
112         }
113 
114         /**
115          * Returns a const iterator to the last (highest position) target.
116          */
117         inline auto end()
118         {
119             return targets.cend();
120         }
121 
122         /**
123          * Returns the number of targets
124          */
125         inline auto size()
126         {
127             return targets.size();
128         }
129 
130     private:
131 
132         /**
133          * The path to the fsi-master sysfs device to access
134          */
135         const std::string fsiMasterPath;
136 
137         /**
138          * The path to the fsi slave sysfs base directory
139          */
140         const std::string fsiSlaveBasePath;
141 
142         /**
143          * A container of Targets in the system
144          */
145         std::vector<std::unique_ptr<Target>> targets;
146 };
147 
148 }
149 }
150