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 #include "cpu/inorder/resource_pool.hh"
33 #include "cpu/inorder/resources/resource_list.hh"
39 using namespace ThePipeline
;
41 ResourcePool::ResourcePool(InOrderCPU
*_cpu
, ThePipeline::Params
*params
)
44 //@todo: use this function to instantiate the resources in resource pool.
45 //This will help in the auto-generation of this pipeline model.
46 //ThePipeline::addResources(resources, memObjects);
48 // Declare Resource Objects
49 // name - id - bandwidth - latency - CPU - Parameters
50 // --------------------------------------------------
51 resources
.push_back(new FetchSeqUnit("Fetch-Seq-Unit", FetchSeq
,
52 StageWidth
* 2, 0, _cpu
, params
));
54 memObjects
.push_back(ICache
);
55 resources
.push_back(new CacheUnit("icache_port", ICache
,
56 StageWidth
* MaxThreads
, 0, _cpu
,
59 resources
.push_back(new DecodeUnit("Decode-Unit", Decode
,
60 StageWidth
, 0, _cpu
, params
));
62 resources
.push_back(new BranchPredictor("Branch-Predictor", BPred
,
63 StageWidth
, 0, _cpu
, params
));
65 resources
.push_back(new InstBuffer("Fetch-Buffer-T0", FetchBuff
, 4,
68 resources
.push_back(new UseDefUnit("RegFile-Manager", RegManager
,
69 StageWidth
* MaxThreads
, 0, _cpu
,
72 resources
.push_back(new AGENUnit("AGEN-Unit", AGEN
,
73 StageWidth
, 0, _cpu
, params
));
75 resources
.push_back(new ExecutionUnit("Execution-Unit", ExecUnit
,
76 StageWidth
, 0, _cpu
, params
));
78 resources
.push_back(new MultDivUnit("Mult-Div-Unit", MDU
, 5, 0, _cpu
,
81 memObjects
.push_back(DCache
);
82 resources
.push_back(new CacheUnit("dcache_port", DCache
,
83 StageWidth
* MaxThreads
, 0, _cpu
,
86 resources
.push_back(new GraduationUnit("Graduation-Unit", Grad
,
87 StageWidth
* MaxThreads
, 0, _cpu
,
90 resources
.push_back(new InstBuffer("Fetch-Buffer-T1", FetchBuff2
, 4,
97 for (int i
=0; i
< resources
.size(); i
++) {
98 DPRINTF(Resource
, "Initializing resource: %s.\n",
99 resources
[i
]->name());
101 resources
[i
]->init();
108 return cpu
->name() + ".ResourcePool";
113 ResourcePool::regStats()
115 DPRINTF(Resource
, "Registering Stats Throughout Resource Pool.\n");
117 int num_resources
= resources
.size();
119 for (int idx
= 0; idx
< num_resources
; idx
++) {
120 resources
[idx
]->regStats();
125 ResourcePool::getPort(const std::string
&if_name
, int idx
)
127 DPRINTF(Resource
, "Binding %s in Resource Pool.\n", if_name
);
129 for (int i
= 0; i
< memObjects
.size(); i
++) {
130 int obj_idx
= memObjects
[i
];
131 Port
*port
= resources
[obj_idx
]->getPort(if_name
, idx
);
133 DPRINTF(Resource
, "%s set to resource %s(#%i) in Resource Pool.\n",
134 if_name
, resources
[obj_idx
]->name(), obj_idx
);
143 ResourcePool::getPortIdx(const std::string
&port_name
)
145 DPRINTF(Resource
, "Finding Port Idx for %s.\n", port_name
);
147 for (int i
= 0; i
< memObjects
.size(); i
++) {
148 unsigned obj_idx
= memObjects
[i
];
149 Port
*port
= resources
[obj_idx
]->getPort(port_name
, obj_idx
);
151 DPRINTF(Resource
, "Returning Port Idx %i for %s.\n", obj_idx
,
161 ResourcePool::getResIdx(const std::string
&res_name
)
163 DPRINTF(Resource
, "Finding Resource Idx for %s.\n", res_name
);
165 int num_resources
= resources
.size();
167 for (int idx
= 0; idx
< num_resources
; idx
++) {
168 if (resources
[idx
]->name() == res_name
)
176 ResourcePool::request(int res_idx
, DynInstPtr inst
)
178 //Make Sure This is a valid resource ID
179 assert(res_idx
>= 0 && res_idx
< resources
.size());
181 return resources
[res_idx
]->request(inst
);
185 ResourcePool::squash(DynInstPtr inst
, int res_idx
, InstSeqNum done_seq_num
,
188 resources
[res_idx
]->squash(inst
, ThePipeline::NumStages
-1, done_seq_num
,
193 ResourcePool::slotsAvail(int res_idx
)
195 return resources
[res_idx
]->slotsAvail();
199 ResourcePool::slotsInUse(int res_idx
)
201 return resources
[res_idx
]->slotsInUse();
204 //@todo: split this function and call this version schedulePoolEvent
205 // and use this scheduleEvent for scheduling a specific event on
207 //@todo: For arguments that arent being used in a ResPoolEvent, a dummyParam
208 // or some typedef can be used to signify what's important info
209 // to the event construction
211 ResourcePool::scheduleEvent(InOrderCPU::CPUEventType e_type
, DynInstPtr inst
,
212 int delay
, int res_idx
, ThreadID tid
)
218 case InOrderCPU::ActivateThread
:
220 DPRINTF(Resource
, "Scheduling Activate Thread Resource Pool Event "
221 "for tick %i, [tid:%i].\n", curTick
+ delay
,
223 ResPoolEvent
*res_pool_event
=
224 new ResPoolEvent(this,
227 inst
->squashingStage
,
230 mainEventQueue
.schedule(res_pool_event
,
231 curTick
+ cpu
->ticks(delay
));
235 case InOrderCPU::HaltThread
:
236 case InOrderCPU::DeactivateThread
:
239 DPRINTF(Resource
, "Scheduling Deactivate Thread Resource Pool "
240 "Event for tick %i.\n", curTick
+ delay
);
241 ResPoolEvent
*res_pool_event
=
242 new ResPoolEvent(this,
245 inst
->squashingStage
,
249 mainEventQueue
.schedule(res_pool_event
,
250 curTick
+ cpu
->ticks(delay
));
255 case InOrderCPU::SuspendThread
:
258 DPRINTF(Resource
, "Scheduling Suspend Thread Resource Pool Event for tick %i.\n",
260 ResPoolEvent
*res_pool_event
= new ResPoolEvent(this,
263 inst
->squashingStage
,
267 mainEventQueue
.schedule(res_pool_event
, curTick
+ cpu
->ticks(delay
));
272 case ResourcePool::InstGraduated
:
274 DPRINTF(Resource
, "Scheduling Inst-Graduated Resource Pool "
275 "Event for tick %i.\n", curTick
+ delay
);
276 ResPoolEvent
*res_pool_event
=
277 new ResPoolEvent(this,e_type
,
279 inst
->squashingStage
,
282 mainEventQueue
.schedule(res_pool_event
,
283 curTick
+ cpu
->ticks(delay
));
288 case ResourcePool::SquashAll
:
290 DPRINTF(Resource
, "Scheduling Squash Resource Pool Event for "
291 "tick %i.\n", curTick
+ delay
);
292 ResPoolEvent
*res_pool_event
=
293 new ResPoolEvent(this,e_type
,
295 inst
->squashingStage
,
298 mainEventQueue
.schedule(res_pool_event
,
299 curTick
+ cpu
->ticks(delay
));
303 case InOrderCPU::SquashFromMemStall
:
305 DPRINTF(Resource
, "Scheduling Squash Due to Memory Stall Resource "
306 "Pool Event for tick %i.\n",
308 ResPoolEvent
*res_pool_event
=
309 new ResPoolEvent(this,e_type
,
311 inst
->squashingStage
,
314 mainEventQueue
.schedule(res_pool_event
,
315 curTick
+ cpu
->ticks(delay
));
319 case ResourcePool::UpdateAfterContextSwitch
:
321 DPRINTF(Resource
, "Scheduling UpdatePC Resource Pool Event for tick %i.\n",
323 ResPoolEvent
*res_pool_event
= new ResPoolEvent(this,e_type
,
325 inst
->squashingStage
,
328 mainEventQueue
.schedule(res_pool_event
, curTick
+ cpu
->ticks(delay
));
334 DPRINTF(Resource
, "Ignoring Unrecognized CPU Event (%s).\n",
335 InOrderCPU::eventNames
[e_type
]);
340 ResourcePool::unscheduleEvent(int res_idx
, DynInstPtr inst
)
342 resources
[res_idx
]->unscheduleEvent(inst
);
346 ResourcePool::squashAll(DynInstPtr inst
, int stage_num
,
347 InstSeqNum done_seq_num
, ThreadID tid
)
349 DPRINTF(Resource
, "[tid:%i] Broadcasting Squash All Event "
350 " starting w/stage %i for all instructions above [sn:%i].\n",
351 tid
, stage_num
, done_seq_num
);
353 int num_resources
= resources
.size();
355 for (int idx
= 0; idx
< num_resources
; idx
++) {
356 resources
[idx
]->squash(inst
, stage_num
, done_seq_num
, tid
);
361 ResourcePool::squashDueToMemStall(DynInstPtr inst
, int stage_num
,
362 InstSeqNum done_seq_num
, ThreadID tid
)
364 DPRINTF(Resource
, "[tid:%i] Broadcasting SquashDueToMemStall Event"
365 " starting w/stage %i for all instructions above [sn:%i].\n",
366 tid
, stage_num
, done_seq_num
);
368 int num_resources
= resources
.size();
370 for (int idx
= 0; idx
< num_resources
; idx
++) {
371 resources
[idx
]->squashDueToMemStall(inst
, stage_num
, done_seq_num
,
377 ResourcePool::activateAll(ThreadID tid
)
379 bool do_activate
= cpu
->threadModel
!= InOrderCPU::SwitchOnCacheMiss
||
380 cpu
->numActiveThreads() < 1 ||
381 cpu
->activeThreadId() == tid
;
385 DPRINTF(Resource
, "[tid:%i] Broadcasting Thread Activation to all "
386 "resources.\n", tid
);
388 int num_resources
= resources
.size();
390 for (int idx
= 0; idx
< num_resources
; idx
++) {
391 resources
[idx
]->activateThread(tid
);
394 DPRINTF(Resource
, "[tid:%i] Ignoring Thread Activation to all "
395 "resources.\n", tid
);
400 ResourcePool::deactivateAll(ThreadID tid
)
402 DPRINTF(Resource
, "[tid:%i] Broadcasting Thread Deactivation to all "
403 "resources.\n", tid
);
405 int num_resources
= resources
.size();
407 for (int idx
= 0; idx
< num_resources
; idx
++) {
408 resources
[idx
]->deactivateThread(tid
);
413 ResourcePool::suspendAll(ThreadID tid
)
415 DPRINTF(Resource
, "[tid:%i] Broadcasting Thread Suspension to all resources.\n",
418 int num_resources
= resources
.size();
420 for (int idx
= 0; idx
< num_resources
; idx
++) {
421 resources
[idx
]->suspendThread(tid
);
426 ResourcePool::instGraduated(InstSeqNum seq_num
, ThreadID tid
)
428 DPRINTF(Resource
, "[tid:%i] Broadcasting [sn:%i] graduation to all "
429 "resources.\n", tid
, seq_num
);
431 int num_resources
= resources
.size();
433 for (int idx
= 0; idx
< num_resources
; idx
++) {
434 resources
[idx
]->instGraduated(seq_num
, tid
);
439 ResourcePool::updateAfterContextSwitch(DynInstPtr inst
, ThreadID tid
)
441 DPRINTF(Resource
, "[tid:%i] Broadcasting Update PC to all resources.\n",
444 int num_resources
= resources
.size();
446 for (int idx
= 0; idx
< num_resources
; idx
++) {
447 resources
[idx
]->updateAfterContextSwitch(inst
, tid
);
451 ResourcePool::ResPoolEvent::ResPoolEvent(ResourcePool
*_resPool
)
452 : Event((Event::Priority
)((unsigned)CPU_Tick_Pri
+5)), resPool(_resPool
),
453 eventType((InOrderCPU::CPUEventType
) Default
)
456 ResourcePool::ResPoolEvent::ResPoolEvent(ResourcePool
*_resPool
,
457 InOrderCPU::CPUEventType e_type
,
462 : Event(CPU_Tick_Pri
), resPool(_resPool
),
463 eventType(e_type
), inst(_inst
), seqNum(seq_num
),
464 stageNum(stage_num
), tid(_tid
)
469 ResourcePool::ResPoolEvent::process()
473 case InOrderCPU::ActivateThread
:
474 resPool
->activateAll(tid
);
477 case InOrderCPU::DeactivateThread
:
478 case InOrderCPU::HaltThread
:
479 resPool
->deactivateAll(tid
);
482 case InOrderCPU::SuspendThread
:
483 resPool
->suspendAll(tid
);
486 case ResourcePool::InstGraduated
:
487 resPool
->instGraduated(seqNum
, tid
);
490 case ResourcePool::SquashAll
:
491 resPool
->squashAll(inst
, stageNum
, seqNum
, tid
);
494 case InOrderCPU::SquashFromMemStall
:
495 resPool
->squashDueToMemStall(inst
, stageNum
, seqNum
, tid
);
498 case ResourcePool::UpdateAfterContextSwitch
:
499 resPool
->updateAfterContextSwitch(inst
, tid
);
503 fatal("Unrecognized Event Type");
506 resPool
->cpu
->cpuEventRemoveList
.push(this);
511 ResourcePool::ResPoolEvent::description()
513 return "Resource Pool event";
516 /** Schedule resource event, regardless of its current state. */
518 ResourcePool::ResPoolEvent::scheduleEvent(int delay
)
521 mainEventQueue
.reschedule(this,curTick
+ resPool
->cpu
->ticks(delay
));
522 else if (!scheduled())
523 mainEventQueue
.schedule(this,curTick
+ resPool
->cpu
->ticks(delay
));
526 /** Unschedule resource event, regardless of its current state. */
528 ResourcePool::ResPoolEvent::unscheduleEvent()