1/*
2 * Copyright (c) 2018 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder.  You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Author: Matteo Andreozzi
38 */
39
40#ifndef __MEM_QOS_POLICY_HH__
41#define __MEM_QOS_POLICY_HH__
42
43#include "base/trace.hh"
44#include "debug/QOS.hh"
45#include "mem/qos/mem_ctrl.hh"
46#include "mem/packet.hh"
47#include "sim/system.hh"
48
49namespace QoS {
50
51/**
52 * QoS Policy base class
53 *
54 * QoS Policy base class: all QoS policies derive from this class to
55 * implement specific QoS scheduling algorithms
56 *
57 */
58class Policy : public SimObject
59{
60  public:
61    using Params = QoSPolicyParams;
62    Policy(const Params* p);
63
64    virtual ~Policy();
65
66    virtual void regStats() override {};
67
68    virtual void init() override {};
69
70    /**
71     * Setting a pointer to the Memory Controller implementing
72     * the policy.
73     */
74    void setMemCtrl(MemCtrl* mem) { memCtrl = mem; };
75
76    /**
77     * Builds a MasterID/value pair given a master input.
78     * This will be lookuped in the system list of masters in order
79     * to retrieve the associated MasterID.
80     * In case the master name/object cannot be resolved, the pairing
81     * method will panic.
82     *
83     * @param master Master to lookup in the system
84     * @param value Value to be associated with the MasterID
85     * @return A MasterID/Value pair.
86     */
87    template <typename M, typename T>
88    std::pair<MasterID, T> pair(M master, T value);
89
90    /**
91     * Schedules data - must be defined by derived class
92     *
93     * @param mId master id to schedule
94     * @param data data to schedule
95     * @return QoS priority value
96     */
97    virtual uint8_t schedule(const MasterID mId, const uint64_t data) = 0;
98
99    /**
100     * Schedules a packet. Non virtual interface for the scheduling
101     * method requiring a master ID.
102     *
103     * @param pkt pointer to packet to schedule
104     * @return QoS priority value
105     */
106    uint8_t schedule(const PacketPtr pkt);
107
108  protected:
109    /** Pointer to parent memory controller implementing the policy */
110    MemCtrl* memCtrl;
111};
112
113template <typename M, typename T>
114std::pair<MasterID, T>
115Policy::pair(M master, T value)
116{
117    auto id = memCtrl->system()->lookupMasterId(master);
118
119    panic_if(id == Request::invldMasterId,
120             "Unable to find master %s\n", master);
121
122    DPRINTF(QOS,
123            "Master %s [id %d] associated with QoS data %d\n",
124            master, id, value);
125
126    return std::pair<MasterID, T>(id, value);
127}
128
129} // namespace QoS
130
131#endif /* __MEM_QOS_POLICY_HH__ */
132