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 =
14     "/sys/devices/platform/gpio-fsi/fsi0/slave@00:00/raw";
15 constexpr auto fsiMasterDevPathOld =
16     "/sys/devices/platform/fsi-master/slave@00:00/raw";
17 
18 constexpr auto fsiSlaveBaseDir =
19     "/sys/devices/platform/gpio-fsi/fsi0/slave@00:00/00:00:00:0a/fsi1/";
20 constexpr auto fsiSlaveBaseDirOld =
21     "/sys/devices/platform/fsi-master/slave@00:00/hub@00/";
22 
23 typedef uint32_t (*swap_endian_t)(uint32_t);
24 
25 /**
26  * Represents a specific P9 processor in the system.  Used by
27  * the access APIs to specify the chip to operate on.
28  */
29 class Target
30 {
31   public:
32     /**
33      * Constructor
34      *
35      * @param[in] - The logical position of the target
36      * @param[in] - The sysfs device path
37      * @param[in] - The function pointer for swapping endianness
38      */
39     Target(size_t position, const std::string& devPath,
40            const swap_endian_t swapper) :
41         pos(position),
42         cfamPath(devPath), doSwapEndian(swapper)
43     {
44     }
45 
46     Target() = delete;
47     ~Target() = default;
48     Target(const Target&) = default;
49     Target(Target&&) = default;
50     Target& operator=(Target&&) = default;
51 
52     /**
53      * Returns the position
54      */
55     inline auto getPos() const
56     {
57         return pos;
58     }
59 
60     /**
61      * Returns the CFAM sysfs path
62      */
63     inline auto getCFAMPath() const
64     {
65         return cfamPath;
66     }
67 
68     /**
69      * Returns the file descriptor to use
70      * for read/writeCFAM operations.
71      */
72     int getCFAMFD();
73 
74     /**
75      * Returns correct byte-order data. (May or may not swap it depending
76      * on the function received during construction from Targeting and the
77      * host endianness).
78      */
79     inline uint32_t swapEndian(uint32_t data) const
80     {
81         return doSwapEndian(data);
82     }
83 
84   private:
85     /**
86      * The logical position of this target
87      */
88     size_t pos;
89 
90     /**
91      * The sysfs device path for the CFAM
92      */
93     const std::string cfamPath;
94 
95     /**
96      * The file descriptor to use for read/writeCFAMReg
97      */
98     std::unique_ptr<openpower::util::FileDescriptor> cfamFD;
99 
100     /**
101      * The function pointer for swapping endianness
102      */
103     const swap_endian_t doSwapEndian;
104 };
105 
106 /**
107  * Class that manages processor targeting for FSI operations.
108  */
109 class Targeting
110 {
111   public:
112     /**
113      * Scans sysfs to find all processors and creates Target objects
114      * for them.
115      * @param[in] fsiMasterDev - the sysfs device for the master
116      * @param[in] fsiSlaveDirectory - the base sysfs dir for slaves
117      */
118     Targeting(const std::string& fsiMasterDev, const std::string& fsiSlaveDir);
119 
120     Targeting() : Targeting(fsiMasterDevPath, fsiSlaveBaseDir)
121     {
122     }
123 
124     ~Targeting() = default;
125     Targeting(const Targeting&) = default;
126     Targeting(Targeting&&) = default;
127     Targeting& operator=(Targeting&&) = default;
128 
129     /**
130      * Returns a const iterator to the first target
131      */
132     inline auto begin()
133     {
134         return targets.cbegin();
135     }
136 
137     /**
138      * Returns a const iterator to the last (highest position) target.
139      */
140     inline auto end()
141     {
142         return targets.cend();
143     }
144 
145     /**
146      * Returns the number of targets
147      */
148     inline auto size()
149     {
150         return targets.size();
151     }
152 
153     /**
154      * Returns a target by position.
155      */
156     std::unique_ptr<Target>& getTarget(size_t pos);
157 
158   private:
159     /**
160      * The path to the fsi-master sysfs device to access
161      */
162     std::string fsiMasterPath;
163 
164     /**
165      * The path to the fsi slave sysfs base directory
166      */
167     std::string fsiSlaveBasePath;
168 
169     /**
170      * A container of Targets in the system
171      */
172     std::vector<std::unique_ptr<Target>> targets;
173 };
174 
175 } // namespace targeting
176 } // namespace openpower
177