summaryrefslogtreecommitdiff
path: root/src/sim/dvfs_handler.hh
blob: 6e495fff5c36b21a9a39b53dc1390d3ed77461d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * Copyright (c) 2013-2014 ARM Limited
 * All rights reserved
 *
 * The license below extends only to copyright in the software and shall
 * not be construed as granting a license to any other intellectual
 * property including but not limited to intellectual property relating
 * to a hardware implementation of the functionality of the software
 * licensed hereunder.  You may use the software subject to the license
 * terms below provided that you ensure that this notice is replicated
 * unmodified and in its entirety in all distributions of the software,
 * modified or unmodified, in source code or in binary form.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors: Vasileios Spiliopoulos
 *          Akash Bagdia
 *          Stephan Diestelhorst
 */

/**
 * @file
 * DVFSHandler and DomainConfig class declaration used for managing voltage
 * and frequency scaling of the various DVFS domains in the system (with each
 * domain having their independent domain configuration information)
 */


#ifndef __SIM_DVFS_HANDLER_HH__
#define __SIM_DVFS_HANDLER_HH__

#include <vector>

#include "debug/DVFS.hh"
#include "params/ClockDomain.hh"
#include "params/DVFSHandler.hh"
#include "params/VoltageDomain.hh"
#include "sim/clock_domain.hh"
#include "sim/eventq.hh"
#include "sim/sim_object.hh"
#include "sim/voltage_domain.hh"

/**
 * DVFS Handler class, maintains a list of all the domains it can handle.
 * Each entry of that list is an object of the DomainConfig class, and the
 * handler uses the methods provided by that class to get access to the
 * configuration of each domain. The handler is responsible for setting/getting
 * clock periods and voltages from clock/voltage domains.
 * The handler acts the bridge between software configurable information
 * for each domain as provided to the controller and the hardware
 * implementation details for those domains.
 */
class DVFSHandler : public SimObject
{
  public:
    typedef DVFSHandlerParams Params;
    DVFSHandler(const Params *p);

    typedef SrcClockDomain::DomainID DomainID;
    typedef SrcClockDomain::PerfLevel PerfLevel;

    /**
     * Get the number of domains assigned to this DVFS handler.
     * @return Number of domains
     */
    uint32_t numDomains() const { return domainIDList.size(); }

    /**
     * Get the n-th domain ID, from the domains managed by this handler.
     * @return Domain ID
     */
    DomainID domainID(uint32_t index) const;

    /**
     * Check whether a domain ID is known to the handler or not.
     * @param domain_id Domain ID to check
     * @return Domain ID known to handler?
     */
    bool validDomainID(DomainID domain_id) const;

    /**
     * Get transition latency to switch between performance levels.
     * @return Transition latency
     */
    Tick transLatency() const { return _transLatency; }

    /**
     * Set a new performance level for the specified domain.  The actual update
     * will be delayed by transLatency().
     *
     * @param domain_id Software visible ID of the domain to be configured
     * @param perf_level Requested performance level (0 - fast, >0 slower)
     * @return status whether the setting was successful
     */
    bool perfLevel(DomainID domain_id, PerfLevel perf_level);

    /**
     * Get the current performance level of a domain.  While a change request is
     * in-flight, will return the current (i.e. old, unmodified) value.
     *
     * @param domain_id Domain ID to query
     * @return Current performance level of the specified domain
     */
    PerfLevel perfLevel(DomainID domain_id) const {
         assert(isEnabled());
         return findDomain(domain_id)->perfLevel();
    }

    /**
     * Read the clock period of the specified domain at the specified
     * performance level.
     * @param domain_id Domain ID to query
     * @param perf_level Performance level of interest
     * @return Clock period in ticks for the requested performance level of
     * the respective domain
     */
    Tick clkPeriodAtPerfLevel(DomainID domain_id, PerfLevel perf_level) const
    {
        SrcClockDomain *d = findDomain(domain_id);
        assert(d);
        PerfLevel n = d->numPerfLevels();
        if (perf_level < n)
            return d->clkPeriodAtPerfLevel(perf_level);

        warn("DVFSHandler %s reads illegal frequency level %u from "\
             "SrcClockDomain %s. Returning 0\n", name(), perf_level, d->name());
        return Tick(0);
    }

    /**
     * Read the voltage of the specified domain at the specified
     * performance level.
     * @param domain_id Domain ID to query
     * @param perf_level Performance level of interest
     * @return Voltage for the requested performance level of the respective
     * domain
     */
    double voltageAtPerfLevel(DomainID domain_id, PerfLevel perf_level) const
    {
        VoltageDomain *d = findDomain(domain_id)->voltageDomain();
        assert(d);
        PerfLevel n = d->numVoltages();
        if (perf_level < n)
            return d->voltage(perf_level);

        // Request outside of the range of the voltage domain
        if (n == 1) {
            DPRINTF(DVFS, "DVFS: Request for perf-level %i for single-point "\
                    "voltage domain %s.  Returning voltage at level 0: %.2f "\
                    "V\n", perf_level, d->name(), d->voltage(0));
            // Special case for single point voltage domain -> same voltage for
            // all points
            return d->voltage(0);
        }

        warn("DVFSHandler %s reads illegal voltage level %u from "\
             "VoltageDomain %s. Returning 0 V\n", name(), perf_level, d->name());
        return 0.;
    }

    /**
     * Get the total number of available performance levels.
     *
     * @param domain_id Domain ID to query
     * @return Number of performance levels that where configured for the
     * respective domain
     */
    PerfLevel numPerfLevels(PerfLevel domain_id) const
    {
        return findDomain(domain_id)->numPerfLevels();
    }

    /**
     * Check enable status of the DVFS handler, when the handler is disabled, no
     * request should be sent to the handler.
     * @return True, if the handler is enabled
     */
    bool isEnabled() const { return enableHandler; }

    void serialize(CheckpointOut &cp) const M5_ATTR_OVERRIDE;
    void unserialize(CheckpointIn &cp) M5_ATTR_OVERRIDE;

  private:
    typedef std::map<DomainID, SrcClockDomain*> Domains;
    Domains domains;

    /**
      * List of IDs avaiable in the domain list
      */
    std::vector<DomainID> domainIDList;

    /**
      * Clock domain of the system the handler is instantiated.
      */
    SrcClockDomain* sysClkDomain;

    /**
     * Search for a domain based on the domain ID.
     *
     * @param domain_id Domain ID to search for
     * @return Pointer to the source clock domain with matching ID.
     */
    SrcClockDomain *findDomain(DomainID domain_id) const {
        auto it = domains.find(domain_id);
        panic_if(it == domains.end(),
                 "DVFS: Could not find a domain for ID %d.\n",domain_id );
        return domains.find(domain_id)->second;
    }

    /**
     * Disabling the DVFS handler ensures that all the DVFS migration requests
     * are ignored. Domains remain at their default frequency and voltage.
     */
    bool enableHandler;


    /**
     * This corresponds to the maximum transition latency associated with the
     * hardware transitioning from a particular performance level to the other
     */
    const Tick _transLatency;



    /**
     * Update performance level event, encapsulates all the required information
     * for a future call to change a domain's performance level.
     */
    struct UpdateEvent : public Event {
        UpdateEvent() : Event(DVFS_Update_Pri), domainIDToSet(0),
                        perfLevelToSet(0) {}

        /**
         * Static pointer to the single DVFS hander for all the update events
         */
        static DVFSHandler *dvfsHandler;

        /**
         * ID of the domain that will be changed by the in-flight event
         */
        DomainID domainIDToSet;

        /**
         * Target performance level of the in-flight event
         */
        PerfLevel perfLevelToSet;

        /**
         * Updates the performance level by modifying the clock and the voltage
         * of the associated clocked objects.  Gets information from
         * domainIDToSet and perfLevelToSet for easier calling through an
         * event.
         */
        void updatePerfLevel();

        void process() { updatePerfLevel(); }
    };

    typedef std::map<DomainID, UpdateEvent> UpdatePerfLevelEvents;
    /**
     * Map from domain IDs -> perf level update events, records in-flight change
     * requests per domain ID.
     */
    UpdatePerfLevelEvents updatePerfLevelEvents;
};

#endif // __SIM_DVFS_HANDLER_HH__