2 * Copyright (c) 2007 MIPS Technologies, Inc.
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.
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.
28 * Authors: Korey Sewell
32 #ifndef __CPU_INORDER_RESOURCE_HH__
33 #define __CPU_INORDER_RESOURCE_HH__
39 #include "base/types.hh"
40 #include "cpu/inst_seq.hh"
41 #include "cpu/inorder/inorder_dyn_inst.hh"
42 #include "cpu/inorder/pipeline_traits.hh"
43 #include "sim/eventq.hh"
44 #include "sim/sim_object.hh"
49 class ResourceRequest;
51 typedef ResourceRequest ResReq;
52 typedef ResourceRequest* ResReqPtr;
56 typedef ThePipeline::DynInstPtr DynInstPtr;
58 friend class ResourceEvent;
59 friend class ResourceRequest;
62 Resource(std::string res_name, int res_id, int res_width,
63 int res_latency, InOrderCPU *_cpu);
67 /** Return name of this resource */
68 virtual std::string name();
70 /** Define this function if resource, has a port to connect to an outside
73 virtual Port* getPort(const std::string &if_name, int idx)
76 /** Return ID for this resource */
77 int getId() { return id; }
79 /** Any extra initiliazation stuff can be set up using this function that
80 * should get called before the simulation starts (tick 0)
83 virtual void initSlots();
85 /** Register Stats for this resource */
86 virtual void regStats() { }
88 /** Resources that care about thread activation override this. */
89 virtual void activateThread(ThreadID tid) { }
91 /** Deactivate Thread. Default action is to squash all instructions
92 * from deactivated thread.
94 virtual void deactivateThread(ThreadID tid);
96 /** Resources that care about thread activation override this. */
97 virtual void suspendThread(ThreadID tid) { }
99 /** Will be called the cycle before a context switch. Any bookkeeping
100 * that needs to be kept for that, can be done here
102 virtual void updateAfterContextSwitch(DynInstPtr inst, ThreadID tid) { }
104 /** Resources that care when an instruction has been graduated
107 virtual void instGraduated(InstSeqNum seq_num, ThreadID tid) { }
109 /** Request usage of this resource. Returns a ResourceRequest object
110 * with all the necessary resource information
112 virtual ResourceRequest* request(DynInstPtr inst);
114 /** Get the next available slot in this resource. Instruction is passed
115 * so that resources can check the instruction before allocating a slot
118 virtual int getSlot(DynInstPtr inst);
120 /** Find the slot that this instruction is using in a resource */
121 virtual int findSlot(DynInstPtr inst);
123 /** Free a resource slot */
124 virtual void freeSlot(int slot_idx);
126 /** Request usage of a resource for this instruction. If this instruction
127 * already has made this request to this resource, and that request is
128 * uncompleted this function will just return that request
130 virtual ResourceRequest* getRequest(DynInstPtr _inst, int stage_num,
131 int res_idx, int slot_num,
134 /** Schedule Execution of This Resource For A Given Slot*/
135 void scheduleExecution(int slot_idx);
137 /** Execute the function of this resource. The Default is action
138 * is to do nothing. More specific models will derive from this
139 * class and define their own execute function.
141 virtual void execute(int slot_idx);
143 /** Fetch on behalf of an instruction. Will check to see
144 * if instruction is actually in resource before
145 * trying to fetch. Needs to be defined for derived units.
147 virtual Fault doFetchAccess(DynInstPtr inst)
148 { panic("doFetchAccess undefined for %s", name()); return NoFault; }
150 /** Read/Write on behalf of an instruction. Will check to see
151 * if instruction is actually in resource before
152 * trying to do access.Needs to be defined for derived units.
154 virtual Fault doCacheAccess(DynInstPtr inst, uint64_t *res=NULL)
155 { panic("doCacheAccess undefined for %s", name()); return NoFault; }
157 /** Squash All Requests After This Seq Num */
158 virtual void squash(DynInstPtr inst, int stage_num,
159 InstSeqNum squash_seq_num, ThreadID tid);
161 virtual void squashDueToMemStall(DynInstPtr inst, int stage_num,
162 InstSeqNum squash_seq_num, ThreadID tid);
164 /** The number of instructions available that this resource can
169 /** The number of instructions using this resource */
172 /** Schedule resource event, regardless of its current state. */
173 void scheduleEvent(int slot_idx, int delay);
175 /** Find instruction in list, Schedule resource event, regardless of its
177 bool scheduleEvent(DynInstPtr inst, int delay);
179 /** Unschedule resource event, regardless of its current state. */
180 void unscheduleEvent(int slot_idx);
182 /** Unschedule resource event, regardless of its current state. */
183 bool unscheduleEvent(DynInstPtr inst);
185 /** Return the number of cycles in 'Tick' format */
186 Tick ticks(int numCycles);
188 /** Find the request that corresponds to this instruction */
189 virtual ResReqPtr findRequest(DynInstPtr inst);
192 void rejectRequest(DynInstPtr inst);
194 /** Request a Resource again. Some resources have to special process this
195 * in subsequent accesses.
197 virtual void requestAgain(DynInstPtr inst, bool &try_request);
199 /** Return Latency of Resource */
200 /* Can be overridden for complex cases */
201 virtual int getLatency(int slot_num) { return latency; }
204 /** The name of this resource */
207 /** ID of the resource. The Resource Pool uses this # to identify this
212 /** The number of instructions the resource can simultaneously
217 /** Constant latency for this resource.
218 * Note: Dynamic latency resources set this to 0 and
219 * manage the latency themselves
224 /** List of all Requests the Resource is Servicing. Each request
225 represents part of the resource's bandwidth
227 std::vector<ResReqPtr> reqs;
229 /** A list of all the available execution slots for this resource.
230 * This correlates with the actual resource event idx.
232 std::vector<int> availSlots;
234 /** The CPU(s) that this resource interacts with */
238 /** The resource event used for scheduling resource slots on the
241 ResourceEvent *resourceEvent;
243 /** Default denied resource request pointer*/
247 class ResourceEvent : public Event
250 /** Pointer to the CPU. */
254 /// Resource events that come before other associated CPU events
255 /// (for InOrderCPU model).
256 /// check src/sim/eventq.hh for more event priorities.
257 enum InOrderPriority {
258 Resource_Event_Pri = 45,
261 /** The Resource Slot that this event is servicing */
264 /** Constructs a resource event. */
266 ResourceEvent(Resource *res, int slot_idx);
267 virtual ~ResourceEvent() { }
269 /** Initialize data for this resource event. */
270 virtual void init(Resource *res, int slot_idx);
272 /** Processes a resource event. */
273 virtual void process();
275 /** Returns the description of the resource event. */
276 const char *description();
278 /** Set slot idx for event */
279 void setSlot(int slot) { slotIdx = slot; }
281 /** Schedule resource event, regardless of its current state. */
282 void scheduleEvent(int delay);
284 /** Unschedule resource event, regardless of its current state. */
285 void unscheduleEvent()
293 class ResourceRequest
296 typedef ThePipeline::DynInstPtr DynInstPtr;
300 static int maxReqCount;
302 friend class Resource;
305 ResourceRequest(Resource *_res);
307 virtual ~ResourceRequest();
311 virtual void setRequest(DynInstPtr _inst, int stage_num,
312 int res_idx, int slot_num, unsigned _cmd);
314 virtual void clearRequest();
316 /** Acknowledge that this is a request is done and remove
319 void done(bool completed = true);
321 /////////////////////////////////////////////
323 // GET RESOURCE REQUEST IDENTIFICATION / INFO
325 /////////////////////////////////////////////
326 /** Get Resource Index */
327 int getResIdx() { return resIdx; }
329 /** Get Slot Number */
330 int getSlot() { return slotNum; }
331 int getComplSlot() { return complSlotNum; }
332 bool hasSlot() { return slotNum >= 0; }
334 /** Get Stage Number */
335 int getStageNum() { return stageNum; }
337 /** Set/Get Thread Ids */
338 void setTid(ThreadID _tid) { tid = _tid; }
339 ThreadID getTid() { return tid; }
341 /** Instruction this request is for */
342 DynInstPtr getInst() { return inst; }
344 /** Data from this request. Overridden by Resource-Specific Request
347 virtual PacketDataPtr getData() { return NULL; }
349 /** Pointer to Resource that is being used */
352 /** Instruction being used */
355 /** Not guaranteed to be set, used for debugging */
358 /** Command For This Resource */
365 ////////////////////////////////////////
367 // GET RESOURCE REQUEST STATUS FROM VARIABLES
369 ////////////////////////////////////////
370 /** Get/Set Completed variables */
371 bool isCompleted() { return completed; }
372 void setCompleted(bool cond = true) { completed = cond; }
374 /** Get/Set Squashed variables */
375 bool isSquashed() { return squashed; }
376 void setSquashed() { squashed = true; }
378 /** Get/Set IsProcessing variables */
379 bool isProcessing() { return processing; }
380 void setProcessing() { processing = true; }
382 /** Get/Set IsWaiting variables */
383 bool isMemStall() { return memStall; }
384 void setMemStall(bool stall = true) { memStall = stall; }
387 /** Resource Identification */
394 /** Resource Request Status */
402 #endif //__CPU_INORDER_RESOURCE_HH__