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