misc: Merge branch v20.1.0.3 hotfix into develop
[gem5.git] / src / mem / qos / q_policy.hh
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_Q_POLICY_HH__
41 #define __MEM_QOS_Q_POLICY_HH__
42
43 #include <deque>
44 #include <unordered_set>
45
46 #include "mem/packet.hh"
47 #include "params/QoSMemCtrl.hh"
48
49 namespace QoS {
50
51 class MemCtrl;
52
53 /**
54 * QoS Queue Policy
55 *
56 * The QoS Queue Policy class implements algorithms to schedule packets
57 * within the same QoS priority queue
58 */
59 class QueuePolicy
60 {
61 public:
62 typedef std::deque<PacketPtr> PacketQueue;
63
64 /**
65 * This factory method is used for generating the queue policy. It takes
66 * the memory controller params as an argument and returns the related
67 * QueuePolicy object. If no particular QueuePolicy has been specified in
68 * the QoSMemCtrlParams, the method will default to a LIFO queue policy.
69 *
70 * @param p QoS::MemCtrl parameter variable
71 * @return Pointer to the QueuePolicy
72 */
73 static QueuePolicy* create(const QoSMemCtrlParams &p);
74
75 /**
76 * This method is called by the memory controller after it enqueues a
77 * packet. It is a way for the queue policy to hook into the packet
78 * enqueuing and update relevant metadata. This will then be used once
79 * the QueuePolicy::selectPacket will be called.
80 *
81 * @param pkt Enqueued packet
82 */
83 virtual void enqueuePacket(PacketPtr pkt) {};
84
85 /**
86 * Policy selector.
87 * The implementation of this virtual method selects the packet
88 * to be serviced from the packet queue passed as an argument.
89 *
90 * @param queue Packet queue
91 * @return Iterator pointing to the packet in the queue to be
92 * serviced
93 */
94 virtual PacketQueue::iterator selectPacket(PacketQueue* queue) = 0;
95
96 /**
97 * Setting a pointer to the Memory Controller implementing
98 * the policy.
99 */
100 void setMemCtrl(MemCtrl* mem) { memCtrl = mem; };
101
102 virtual ~QueuePolicy() {};
103
104 protected:
105 QueuePolicy(const QoSMemCtrlParams &p)
106 : memCtrl(nullptr)
107 {}
108
109 /** Pointer to parent memory controller implementing the policy */
110 MemCtrl* memCtrl;
111 };
112
113 /** Last In First Out Queue Policy */
114 class LifoQueuePolicy : public QueuePolicy
115 {
116 public:
117 LifoQueuePolicy(const QoSMemCtrlParams &p)
118 : QueuePolicy(p)
119 {}
120
121 /**
122 * Implements LIFO packet select policy
123 *
124 * @param queue The queue in which to select a packet
125 * @return Iterator to the selected packet
126 */
127 PacketQueue::iterator
128 selectPacket(PacketQueue* queue) override
129 {
130 return queue->end();
131 }
132 };
133
134 /** First In First Out Queue Policy */
135 class FifoQueuePolicy : public QueuePolicy
136 {
137 public:
138 FifoQueuePolicy(const QoSMemCtrlParams &p)
139 : QueuePolicy(p)
140 {}
141
142 /**
143 * Implements FCFS packet select policy
144 *
145 * @param queue The queue in which to select a packet
146 * @return Iterator to the selected packet
147 */
148 PacketQueue::iterator
149 selectPacket(PacketQueue* queue) override
150 {
151 return queue->begin();
152 }
153 };
154
155 /**
156 * Least Recently Granted Queue Policy
157 * It selects packets from the queue with a round
158 * robin-like policy: using the requestor id as a switching
159 * parameter rather than switching over a time quantum.
160 */
161 class LrgQueuePolicy : public QueuePolicy
162 {
163 public:
164 LrgQueuePolicy(const QoSMemCtrlParams &p)
165 : QueuePolicy(p)
166 {}
167
168 void enqueuePacket(PacketPtr pkt) override;
169
170 /**
171 * Implements LRG packet select policy
172 *
173 * @param queue The queue in which to select a packet
174 * @return Iterator to the selected packet
175 */
176 PacketQueue::iterator
177 selectPacket(PacketQueue* queue) override;
178
179 protected:
180 /**
181 * Support structure for lrg algorithms:
182 * keeps track of serviced requestors,
183 * always serve the front element.
184 */
185 std::list<RequestorID> toServe;
186 };
187
188 } // namespace QoS
189
190 #endif /* __MEM_QOS_Q_POLICY_HH__ */