ruby: move stall and wakeup functions to AbstractController
[gem5.git] / src / cpu / inorder / resource_pool.hh
1 /*
2 * Copyright (c) 2012 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 * Copyright (c) 2007 MIPS Technologies, Inc.
15 * All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions are
19 * met: redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer;
21 * redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution;
24 * neither the name of the copyright holders nor the names of its
25 * contributors may be used to endorse or promote products derived from
26 * this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 * Authors: Korey Sewell
41 *
42 */
43
44 #ifndef __CPU_INORDER_RESOURCE_POOL_HH__
45 #define __CPU_INORDER_RESOURCE_POOL_HH__
46
47 #include <string>
48 #include <vector>
49
50 #include "cpu/inorder/cpu.hh"
51 #include "cpu/inorder/inorder_dyn_inst.hh"
52 #include "cpu/inorder/pipeline_traits.hh"
53 #include "cpu/inorder/resource.hh"
54 #include "cpu/inst_seq.hh"
55 #include "params/InOrderCPU.hh"
56 #include "sim/eventq.hh"
57 #include "sim/sim_object.hh"
58
59 class CacheUnit;
60 class Event;
61 class FetchUnit;
62 class ResourceEvent;
63
64 class ResourcePool {
65 public:
66 typedef InOrderDynInst::DynInstPtr DynInstPtr;
67
68 public:
69 // List of Resource Pool Events that extends
70 // the list started by the CPU
71 // NOTE(1): Resource Pool also uses event list
72 // CPUEventType defined in inorder/cpu.hh
73 enum ResPoolEventType {
74 InstGraduated = InOrderCPU::NumCPUEvents,
75 SquashAll,
76 UpdateAfterContextSwitch,
77 Default
78 };
79
80 enum ResPoolEventPri {
81 ResPool_Pri = InOrderCPU::InOrderCPU_Pri - 5,
82 ResGrad_Pri,
83 ResSquash_Pri
84 };
85
86 class ResPoolEvent : public Event
87 {
88 protected:
89 /** Resource Pool */
90 ResourcePool *resPool;
91
92 public:
93 InOrderCPU::CPUEventType eventType;
94
95 DynInstPtr inst;
96
97 InstSeqNum seqNum;
98
99 int stageNum;
100
101 ThreadID tid;
102
103 public:
104 /** Constructs a resource event. */
105 ResPoolEvent(ResourcePool *_resPool,
106 InOrderCPU::CPUEventType e_type,
107 DynInstPtr _inst,
108 int stage_num,
109 InstSeqNum seq_num,
110 ThreadID _tid,
111 ResPoolEventPri res_pri = ResPool_Pri);
112
113 /** Set Type of Event To Be Scheduled */
114 void setEvent(InOrderCPU::CPUEventType e_type,
115 DynInstPtr _inst,
116 int stage_num,
117 InstSeqNum seq_num,
118 ThreadID _tid)
119 {
120 eventType = e_type;
121 inst = _inst;
122 seqNum = seq_num;
123 stageNum = stage_num;
124 tid = _tid;
125 }
126
127 /** Processes a resource event. */
128 void process();
129
130 /** Returns the description of the resource event. */
131 const char *description() const;
132
133 /** Schedule Event */
134 void scheduleEvent(Cycles delay);
135
136 /** Unschedule This Event */
137 void unscheduleEvent();
138 };
139
140 public:
141 ResourcePool(InOrderCPU *_cpu, ThePipeline::Params *params);
142 virtual ~ResourcePool();
143
144 std::string name();
145
146 std::string name(int res_idx) { return resources[res_idx]->name(); }
147
148 void init();
149
150 void print();
151
152 /** Register Statistics in All Resources */
153 void regStats();
154
155 /** Returns a specific resource. */
156 unsigned getResIdx(const ThePipeline::ResourceId &res_id);
157
158 /** Returns a pointer to a resource */
159 Resource* getResource(int res_idx) { return resources[res_idx]; }
160
161 /** Request usage of this resource. Returns -1 if not granted and
162 * a positive request tag if granted.
163 */
164 ResReqPtr request(int res_idx, DynInstPtr inst);
165
166 /** Squash The Resource */
167 void squash(DynInstPtr inst, int res_idx, InstSeqNum done_seq_num,
168 ThreadID tid);
169
170 /** Squash All Resources in Pool after Done Seq. Num */
171 void squashAll(DynInstPtr inst, int stage_num,
172 InstSeqNum done_seq_num, ThreadID tid);
173
174 /** Squash Resources in Pool after a memory stall
175 * NOTE: Only use during Switch-On-Miss Thread model
176 */
177 void squashDueToMemStall(DynInstPtr inst, int stage_num,
178 InstSeqNum done_seq_num, ThreadID tid);
179
180 /** Activate Thread in all resources */
181 void activateThread(ThreadID tid);
182
183 /** De-Activate Thread in all resources */
184 void deactivateThread(ThreadID tid);
185
186 /** Suspend Thread in all resources */
187 void suspendThread(ThreadID tid);
188
189 /** Broadcast Context Switch Update to all resources */
190 void updateAfterContextSwitch(DynInstPtr inst, ThreadID tid);
191
192 /** Broadcast graduation to all resources */
193 void instGraduated(InstSeqNum seq_num, ThreadID tid);
194
195 /** Broadcast trap to all resources */
196 void trap(Fault fault, ThreadID tid, DynInstPtr inst);
197
198 /** The number of instructions available that a resource can
199 * can still process.
200 */
201 int slotsAvail(int res_idx);
202
203 /** The number of instructions using a resource */
204 int slotsInUse(int res_idx);
205
206 /** Schedule resource event, regardless of its current state. */
207 void scheduleEvent(InOrderCPU::CPUEventType e_type, DynInstPtr inst = NULL,
208 Cycles delay = Cycles(0), int res_idx = 0,
209 ThreadID tid = 0);
210
211 /** UnSchedule resource event, regardless of its current state. */
212 void unscheduleEvent(int res_idx, DynInstPtr inst);
213
214 /** Tasks to perform when simulation starts */
215 virtual void startup() { }
216
217 /** The CPU(s) that this resource interacts with */
218 InOrderCPU *cpu;
219
220 DynInstPtr dummyInst[ThePipeline::MaxThreads];
221
222 /**
223 * Get a pointer to the (always present) instruction fetch unit.
224 *
225 * @return the instruction unit
226 */
227 FetchUnit *getInstUnit() const { return instUnit; }
228
229 /**
230 * Get a pointer to the (always present) data load/store unit.
231 *
232 * @return the data cache unit
233 */
234 CacheUnit *getDataUnit() const { return dataUnit; }
235
236 private:
237
238 /** The instruction fetch unit. */
239 FetchUnit *instUnit;
240
241 /** The data load/store unit. */
242 CacheUnit *dataUnit;
243
244 std::vector<Resource *> resources;
245
246 /** Resources that need to be updated on an inst. graduation */
247 std::vector<int> gradObjects;
248 };
249
250 #endif //__CPU_INORDER_RESOURCE_HH__