blob: 0a946f65841c1ac5cb202068b2964107d54ddb7c [file] [log] [blame]
Matt Spinler2c05aa72017-02-28 09:48:13 -06001#pragma once
2
3#include <memory>
4#include <vector>
Matt Spinlerc3bffed2017-03-10 09:05:30 -06005#include "filedescriptor.hpp"
Matt Spinler2c05aa72017-02-28 09:48:13 -06006
7namespace openpower
8{
9namespace targeting
10{
11
12constexpr auto fsiMasterDevPath =
Edward A. James8316b772017-04-24 14:20:48 -050013 "/sys/devices/platform/gpio-fsi/fsi0/slave@00:00/raw";
14constexpr auto fsiMasterDevPathOld =
Matt Spinler2c05aa72017-02-28 09:48:13 -060015 "/sys/devices/platform/fsi-master/slave@00:00/raw";
16
Edward A. James8316b772017-04-24 14:20:48 -050017constexpr auto fsiSlaveBaseDir =
Edward A. James23763b12017-07-06 14:23:32 -050018 "/sys/devices/platform/gpio-fsi/fsi0/slave@00:00/00:00:00:0a/fsi1/";
Edward A. James8316b772017-04-24 14:20:48 -050019constexpr auto fsiSlaveBaseDirOld =
20 "/sys/devices/platform/fsi-master/slave@00:00/hub@00/";
21
22typedef uint32_t (*swap_endian_t)(uint32_t);
Matt Spinler2c05aa72017-02-28 09:48:13 -060023
24/**
25 * Represents a specific P9 processor in the system. Used by
26 * the access APIs to specify the chip to operate on.
27 */
28class 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
Edward A. James8316b772017-04-24 14:20:48 -050037 * @param[in] - The function pointer for swapping endianness
Matt Spinler2c05aa72017-02-28 09:48:13 -060038 */
Edward A. James8316b772017-04-24 14:20:48 -050039 Target(size_t position, const std::string& devPath,
40 const swap_endian_t swapper) :
41 pos(position), cfamPath(devPath), doSwapEndian(swapper)
Matt Spinler2c05aa72017-02-28 09:48:13 -060042 {
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 /**
Matt Spinlerc3bffed2017-03-10 09:05:30 -060060 * Returns the CFAM sysfs path
Matt Spinler2c05aa72017-02-28 09:48:13 -060061 */
Matt Spinlerc3bffed2017-03-10 09:05:30 -060062 inline auto getCFAMPath() const
Matt Spinler2c05aa72017-02-28 09:48:13 -060063 {
Matt Spinlerc3bffed2017-03-10 09:05:30 -060064 return cfamPath;
Matt Spinler2c05aa72017-02-28 09:48:13 -060065 }
66
Matt Spinlerc3bffed2017-03-10 09:05:30 -060067 /**
68 * Returns the file descriptor to use
69 * for read/writeCFAM operations.
70 */
71 int getCFAMFD();
72
Edward A. James8316b772017-04-24 14:20:48 -050073 /**
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
Matt Spinler2c05aa72017-02-28 09:48:13 -060083 private:
84
85 /**
86 * The logical position of this target
87 */
88 size_t pos;
89
90 /**
Matt Spinlerc3bffed2017-03-10 09:05:30 -060091 * The sysfs device path for the CFAM
Matt Spinler2c05aa72017-02-28 09:48:13 -060092 */
Matt Spinlerc3bffed2017-03-10 09:05:30 -060093 const std::string cfamPath;
94
95 /**
96 * The file descriptor to use for read/writeCFAMReg
97 */
98 std::unique_ptr<openpower::util::FileDescriptor> cfamFD;
Edward A. James8316b772017-04-24 14:20:48 -050099
100 /**
101 * The function pointer for swapping endianness
102 */
103 const swap_endian_t doSwapEndian;
Matt Spinler2c05aa72017-02-28 09:48:13 -0600104};
105
106
107/**
108 * Class that manages processor targeting for FSI operations.
109 */
110class 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
Michael Tritzbe407162017-03-30 16:52:24 -0500154 /**
155 * Returns a target by position.
156 */
157 std::unique_ptr<Target>& getTarget(size_t pos);
158
Matt Spinler2c05aa72017-02-28 09:48:13 -0600159 private:
160
161 /**
162 * The path to the fsi-master sysfs device to access
163 */
Edward A. James8316b772017-04-24 14:20:48 -0500164 std::string fsiMasterPath;
Matt Spinler2c05aa72017-02-28 09:48:13 -0600165
166 /**
167 * The path to the fsi slave sysfs base directory
168 */
Edward A. James8316b772017-04-24 14:20:48 -0500169 std::string fsiSlaveBasePath;
Matt Spinler2c05aa72017-02-28 09:48:13 -0600170
171 /**
172 * A container of Targets in the system
173 */
174 std::vector<std::unique_ptr<Target>> targets;
175};
176
177}
178}