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