1 // Copyright (c) 2020 Intel Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #pragma once
15 
16 #include <peci.h>
17 
18 #include <boost/asio/io_context.hpp>
19 #include <sdbusplus/asio/connection.hpp>
20 
21 #include <bitset>
22 #include <iostream>
23 
24 namespace cpu_info
25 {
26 namespace sst
27 {
28 
29 /**
30  * Initialize SST subsystem.
31  *
32  * This will schedule work to be done when the host is ready, in order to
33  * retrieve all SST configuration info for all discoverable CPUs, and publish
34  * the info on new D-Bus objects on the given bus connection.
35  */
36 void init();
37 
38 class PECIError : public std::runtime_error
39 {
40     using std::runtime_error::runtime_error;
41 };
42 
43 bool checkPECIStatus(EPECIStatus libStatus, uint8_t completionCode);
44 
45 constexpr int extendedModel(CPUModel model)
46 {
47     return (model >> 16) & 0xF;
48 }
49 
50 /**
51  * Construct a list of indexes of the set bits in the input value.
52  * E.g. fn(0x7A) -> {1,3,4,5,6}
53  *
54  * @param[in]   mask    Bitmask to convert.
55  *
56  * @return  List of bit indexes.
57  */
58 std::vector<uint32_t> convertMaskToList(std::bitset<64> mask);
59 
60 using TurboEntry = std::tuple<uint32_t, size_t>;
61 
62 /**
63  * Abstract interface that must be implemented by backends, allowing discovery
64  * and control of a single CPU package.
65  */
66 class SSTInterface
67 {
68   public:
69     virtual ~SSTInterface()
70     {}
71 
72     /**
73      * Whether the interface is ready to be used, or we need to wait longer. The
74      * backend may need to wait e.g. for the host BIOS to initialize the
75      * interface.
76      */
77     virtual bool ready() = 0;
78 
79     /** Whether the processor supports the control ("set") functions. */
80     virtual bool supportsControl() = 0;
81 
82     /** Whether SST-PP is enabled on the processor. */
83     virtual bool ppEnabled() = 0;
84     /** Return the current SST-PP configuration level */
85     virtual unsigned int currentLevel() = 0;
86     /** Return the maximum valid SST-PP configuration level */
87     virtual unsigned int numLevels() = 0;
88 
89     /**
90      * Whether the given level is supported. The level indices may be
91      * discontinuous, so this function should be used before querying deeper
92      * properties of a level.
93      */
94     virtual bool levelSupported(unsigned int level) = 0;
95     /** Whether SST-BF is supported in a given level. */
96     virtual bool bfSupported(unsigned int level) = 0;
97     /** Whether SST-TF is supported in a given level. */
98     virtual bool tfSupported(unsigned int level) = 0;
99     /** Whether SST-BF is enabled in a given level. */
100     virtual bool bfEnabled(unsigned int level) = 0;
101     /** Whether SST-TF is enabled in a given level. */
102     virtual bool tfEnabled(unsigned int level) = 0;
103     /** Return the package Thermal Design Power in Watts for a given level. */
104     virtual unsigned int tdp(unsigned int level) = 0;
105     /** Return the number of cores enabled in a given level. */
106     virtual unsigned int coreCount(unsigned int level) = 0;
107     /** Return the list of enabled logical core indices for a given level. */
108     virtual std::vector<unsigned int> enabledCoreList(unsigned int level) = 0;
109     /**
110      * Return the list of TurboEntrys which define the SSE turbo profile for a
111      * given level.
112      */
113     virtual std::vector<TurboEntry> sseTurboProfile(unsigned int level) = 0;
114     /** Return the base frequency (P1) for a given level. */
115     virtual unsigned int p1Freq(unsigned int level) = 0;
116     /** Return the maximum single-core frequency (P0) for a given level. */
117     virtual unsigned int p0Freq(unsigned int level) = 0;
118     /**
119      * Return the DTS max or external Prochot temperature in degrees Celsius
120      * for a given level.
121      */
122     virtual unsigned int prochotTemp(unsigned int level) = 0;
123     /**
124      * Return the list of logical core indices which have high priority when
125      * SST-BF is enabled for a given level.
126      */
127     virtual std::vector<unsigned int>
128         bfHighPriorityCoreList(unsigned int level) = 0;
129     /** Return the high priority base frequency for a given level. */
130     virtual unsigned int bfHighPriorityFreq(unsigned int level) = 0;
131     /** Return the low priority base frequency for a given level. */
132     virtual unsigned int bfLowPriorityFreq(unsigned int level) = 0;
133 
134     /** Enable or disable SST-BF for the current configuration. */
135     virtual void setBfEnabled(bool enable) = 0;
136     /** Enable or disable SST-TF for the current configuration. */
137     virtual void setTfEnabled(bool enable) = 0;
138     /** Change the current configuration to the given level. */
139     virtual void setCurrentLevel(unsigned int level) = 0;
140 };
141 
142 /**
143  * BackendProvider represents a function which may create an SSTInterface given
144  * a CPU PECI address, and the CPU Model information. Usually the CPUModel is
145  * sufficient to determine if the backend is supported.
146  * Backend should return nullptr to indicate it doesn't support a given CPU.
147  * The SST upper layer will call the registered backend provider functions in
148  * arbitrary order until one of them returns a non-null pointer.
149  */
150 using BackendProvider =
151     std::function<std::unique_ptr<SSTInterface>(uint8_t, CPUModel)>;
152 
153 /**
154  * Backends should use 1 instance of the SSTProviderRegistration macro at file
155  * scope to register their provider function. This static struct instance
156  * register the backend before main() is run, and prevents the upper layer from
157  * having to know about which backends exist.
158  */
159 #define SSTProviderRegistration(fn)                                            \
160     struct fn##Register                                                        \
161     {                                                                          \
162         fn##Register()                                                         \
163         {                                                                      \
164             std::cerr << "Registering SST Provider " #fn << std::endl;         \
165             registerBackend(fn);                                               \
166         }                                                                      \
167     } fn##Instance;
168 void registerBackend(BackendProvider);
169 
170 } // namespace sst
171 } // namespace cpu_info
172