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