blob: 6d86b50b6a0e01dac409795b862476ae03d1d59f [file] [log] [blame]
Matt Spinler2c05aa72017-02-28 09:48:13 -06001#pragma once
2
Patrick Venturef78d9042018-11-01 15:39:53 -07003#include "filedescriptor.hpp"
4
Matt Spinler2c05aa72017-02-28 09:48:13 -06005#include <memory>
6#include <vector>
7
8namespace openpower
9{
10namespace targeting
11{
12
13constexpr auto fsiMasterDevPath =
Edward A. James8316b772017-04-24 14:20:48 -050014 "/sys/devices/platform/gpio-fsi/fsi0/slave@00:00/raw";
15constexpr auto fsiMasterDevPathOld =
Matt Spinler2c05aa72017-02-28 09:48:13 -060016 "/sys/devices/platform/fsi-master/slave@00:00/raw";
17
Edward A. James8316b772017-04-24 14:20:48 -050018constexpr auto fsiSlaveBaseDir =
Edward A. James23763b12017-07-06 14:23:32 -050019 "/sys/devices/platform/gpio-fsi/fsi0/slave@00:00/00:00:00:0a/fsi1/";
Edward A. James8316b772017-04-24 14:20:48 -050020constexpr auto fsiSlaveBaseDirOld =
21 "/sys/devices/platform/fsi-master/slave@00:00/hub@00/";
22
23typedef uint32_t (*swap_endian_t)(uint32_t);
Matt Spinler2c05aa72017-02-28 09:48:13 -060024
25/**
26 * Represents a specific P9 processor in the system. Used by
27 * the access APIs to specify the chip to operate on.
28 */
29class Target
30{
Patrick Venturef78d9042018-11-01 15:39:53 -070031 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 }
Matt Spinler2c05aa72017-02-28 09:48:13 -060045
Patrick Venturef78d9042018-11-01 15:39:53 -070046 Target() = delete;
47 ~Target() = default;
48 Target(const Target&) = default;
49 Target(Target&&) = default;
50 Target& operator=(Target&&) = default;
Matt Spinler2c05aa72017-02-28 09:48:13 -060051
Patrick Venturef78d9042018-11-01 15:39:53 -070052 /**
53 * Returns the position
54 */
55 inline auto getPos() const
56 {
57 return pos;
58 }
Matt Spinler2c05aa72017-02-28 09:48:13 -060059
Patrick Venturef78d9042018-11-01 15:39:53 -070060 /**
61 * Returns the CFAM sysfs path
62 */
63 inline auto getCFAMPath() const
64 {
65 return cfamPath;
66 }
Matt Spinler2c05aa72017-02-28 09:48:13 -060067
Patrick Venturef78d9042018-11-01 15:39:53 -070068 /**
69 * Returns the file descriptor to use
70 * for read/writeCFAM operations.
71 */
72 int getCFAMFD();
Matt Spinler2c05aa72017-02-28 09:48:13 -060073
Patrick Venturef78d9042018-11-01 15:39:53 -070074 /**
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 }
Matt Spinlerc3bffed2017-03-10 09:05:30 -060083
Patrick Venturef78d9042018-11-01 15:39:53 -070084 private:
85 /**
86 * The logical position of this target
87 */
88 size_t pos;
Edward A. James8316b772017-04-24 14:20:48 -050089
Patrick Venturef78d9042018-11-01 15:39:53 -070090 /**
91 * The sysfs device path for the CFAM
92 */
93 const std::string cfamPath;
Matt Spinler2c05aa72017-02-28 09:48:13 -060094
Patrick Venturef78d9042018-11-01 15:39:53 -070095 /**
96 * The file descriptor to use for read/writeCFAMReg
97 */
98 std::unique_ptr<openpower::util::FileDescriptor> cfamFD;
Matt Spinler2c05aa72017-02-28 09:48:13 -060099
Patrick Venturef78d9042018-11-01 15:39:53 -0700100 /**
101 * The function pointer for swapping endianness
102 */
103 const swap_endian_t doSwapEndian;
Matt Spinler2c05aa72017-02-28 09:48:13 -0600104};
105
Matt Spinler2c05aa72017-02-28 09:48:13 -0600106/**
107 * Class that manages processor targeting for FSI operations.
108 */
109class Targeting
110{
Patrick Venturef78d9042018-11-01 15:39:53 -0700111 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);
Matt Spinler2c05aa72017-02-28 09:48:13 -0600119
Patrick Venturef78d9042018-11-01 15:39:53 -0700120 Targeting() : Targeting(fsiMasterDevPath, fsiSlaveBaseDir)
121 {
122 }
Matt Spinler2c05aa72017-02-28 09:48:13 -0600123
Patrick Venturef78d9042018-11-01 15:39:53 -0700124 ~Targeting() = default;
125 Targeting(const Targeting&) = default;
126 Targeting(Targeting&&) = default;
127 Targeting& operator=(Targeting&&) = default;
Matt Spinler2c05aa72017-02-28 09:48:13 -0600128
Patrick Venturef78d9042018-11-01 15:39:53 -0700129 /**
130 * Returns a const iterator to the first target
131 */
132 inline auto begin()
133 {
134 return targets.cbegin();
135 }
Matt Spinler2c05aa72017-02-28 09:48:13 -0600136
Patrick Venturef78d9042018-11-01 15:39:53 -0700137 /**
138 * Returns a const iterator to the last (highest position) target.
139 */
140 inline auto end()
141 {
142 return targets.cend();
143 }
Matt Spinler2c05aa72017-02-28 09:48:13 -0600144
Patrick Venturef78d9042018-11-01 15:39:53 -0700145 /**
146 * Returns the number of targets
147 */
148 inline auto size()
149 {
150 return targets.size();
151 }
Matt Spinler2c05aa72017-02-28 09:48:13 -0600152
Patrick Venturef78d9042018-11-01 15:39:53 -0700153 /**
154 * Returns a target by position.
155 */
156 std::unique_ptr<Target>& getTarget(size_t pos);
Matt Spinler2c05aa72017-02-28 09:48:13 -0600157
Patrick Venturef78d9042018-11-01 15:39:53 -0700158 private:
159 /**
160 * The path to the fsi-master sysfs device to access
161 */
162 std::string fsiMasterPath;
Michael Tritzbe407162017-03-30 16:52:24 -0500163
Patrick Venturef78d9042018-11-01 15:39:53 -0700164 /**
165 * The path to the fsi slave sysfs base directory
166 */
167 std::string fsiSlaveBasePath;
Matt Spinler2c05aa72017-02-28 09:48:13 -0600168
Patrick Venturef78d9042018-11-01 15:39:53 -0700169 /**
170 * A container of Targets in the system
171 */
172 std::vector<std::unique_ptr<Target>> targets;
Matt Spinler2c05aa72017-02-28 09:48:13 -0600173};
174
Patrick Venturef78d9042018-11-01 15:39:53 -0700175} // namespace targeting
176} // namespace openpower