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      */
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      */
43     inline auto getPos() const
44     {
45         return pos;
46     }
47 
48     /**
49      * Returns the CFAM sysfs path
50      */
51     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 
93     Targeting() : Targeting(fsiMasterDevPath, fsiSlaveBaseDir)
94     {}
95 
96     ~Targeting() = default;
97     Targeting(const Targeting&) = default;
98     Targeting(Targeting&&) = default;
99     Targeting& operator=(Targeting&&) = default;
100 
101     /**
102      * Returns a const iterator to the first target
103      */
104     inline auto begin()
105     {
106         return targets.cbegin();
107     }
108 
109     /**
110      * Returns a const iterator to the last (highest position) target.
111      */
112     inline auto end()
113     {
114         return targets.cend();
115     }
116 
117     /**
118      * Returns the number of targets
119      */
120     inline auto size()
121     {
122         return targets.size();
123     }
124 
125     /**
126      * Returns a target by position.
127      */
128     std::unique_ptr<Target>& getTarget(size_t pos);
129 
130   private:
131     /**
132      * The path to the fsi-master sysfs device to access
133      */
134     std::string fsiMasterPath;
135 
136     /**
137      * The path to the fsi slave sysfs base directory
138      */
139     std::string fsiSlaveBasePath;
140 
141     /**
142      * A container of Targets in the system
143      */
144     std::vector<std::unique_ptr<Target>> targets;
145 };
146 
147 } // namespace targeting
148 } // namespace openpower
149