2 * Copyright (c) 2011 The Regents of The University of Michigan
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
35 #include "arch/isa_traits.hh"
36 #include "arch/locked_mem.hh"
37 #include "arch/predecoder.hh"
38 #include "arch/utility.hh"
39 #include "config/the_isa.hh"
40 #include "cpu/inorder/resources/cache_unit.hh"
41 #include "cpu/inorder/resources/fetch_unit.hh"
42 #include "cpu/inorder/cpu.hh"
43 #include "cpu/inorder/pipeline_traits.hh"
44 #include "cpu/inorder/resource_pool.hh"
45 #include "debug/Activity.hh"
46 #include "debug/InOrderCachePort.hh"
47 #include "debug/InOrderStall.hh"
48 #include "debug/RefCount.hh"
49 #include "debug/ThreadModel.hh"
50 #include "mem/request.hh"
53 using namespace TheISA
;
54 using namespace ThePipeline
;
56 FetchUnit::FetchUnit(string res_name
, int res_id
, int res_width
,
57 int res_latency
, InOrderCPU
*_cpu
,
58 ThePipeline::Params
*params
)
59 : CacheUnit(res_name
, res_id
, res_width
, res_latency
, _cpu
, params
),
60 instSize(sizeof(TheISA::MachInst
)), fetchBuffSize(params
->fetchBuffSize
)
62 for (int tid
= 0; tid
< MaxThreads
; tid
++)
63 predecoder
[tid
] = new Predecoder(NULL
);
66 FetchUnit::~FetchUnit()
68 std::list
<FetchBlock
*>::iterator fetch_it
= fetchBuffer
.begin();
69 std::list
<FetchBlock
*>::iterator end_it
= fetchBuffer
.end();
70 while (fetch_it
!= end_it
) {
71 delete (*fetch_it
)->block
;
78 std::list
<FetchBlock
*>::iterator pend_it
= pendingFetch
.begin();
79 std::list
<FetchBlock
*>::iterator pend_end
= pendingFetch
.end();
80 while (pend_it
!= pend_end
) {
81 if ((*pend_it
)->block
) {
82 delete (*pend_it
)->block
;
92 FetchUnit::createMachInst(std::list
<FetchBlock
*>::iterator fetch_it
,
96 Addr block_addr
= cacheBlockAlign(inst
->getMemAddr());
97 Addr fetch_addr
= inst
->getMemAddr();
98 unsigned fetch_offset
= (fetch_addr
- block_addr
) / instSize
;
99 ThreadID tid
= inst
->readTid();
100 TheISA::PCState instPC
= inst
->pcState();
103 DPRINTF(InOrderCachePort
, "Creating instruction [sn:%i] w/fetch data @"
104 "addr:%08p block:%08p\n", inst
->seqNum
, fetch_addr
, block_addr
);
106 assert((*fetch_it
)->valid
);
108 TheISA::MachInst
*fetchInsts
=
109 reinterpret_cast<TheISA::MachInst
*>((*fetch_it
)->block
);
112 TheISA::gtoh(fetchInsts
[fetch_offset
]);
114 predecoder
[tid
]->setTC(cpu
->thread
[tid
]->getTC());
115 predecoder
[tid
]->moreBytes(instPC
, inst
->instAddr(), mach_inst
);
116 assert(predecoder
[tid
]->extMachInstReady());
117 ext_inst
= predecoder
[tid
]->getExtMachInst(instPC
);
119 inst
->pcState(instPC
);
120 inst
->setMachInst(ext_inst
);
124 FetchUnit::removeAddrDependency(DynInstPtr inst
)
126 inst
->unsetMemAddr();
130 FetchUnit::getRequest(DynInstPtr inst
, int stage_num
, int res_idx
,
131 int slot_num
, unsigned cmd
)
133 ScheduleEntry
* sched_entry
= *inst
->curSkedEntry
;
134 CacheRequest
* cache_req
= dynamic_cast<CacheRequest
*>(reqs
[slot_num
]);
136 if (!inst
->validMemAddr()) {
137 panic("Mem. Addr. must be set before requesting cache access\n");
140 assert(sched_entry
->cmd
== InitiateFetch
);
142 DPRINTF(InOrderCachePort
,
143 "[tid:%i]: Fetch request from [sn:%i] for addr %08p\n",
144 inst
->readTid(), inst
->seqNum
, inst
->getMemAddr());
146 cache_req
->setRequest(inst
, stage_num
, id
, slot_num
,
147 sched_entry
->cmd
, MemCmd::ReadReq
,
148 inst
->curSkedEntry
->idx
);
154 FetchUnit::setupMemRequest(DynInstPtr inst
, CacheReqPtr cache_req
,
155 int acc_size
, int flags
)
157 ThreadID tid
= inst
->readTid();
158 Addr aligned_addr
= cacheBlockAlign(inst
->getMemAddr());
159 if (cache_req
->memReq
== NULL
) {
161 new Request(tid
, aligned_addr
, acc_size
, flags
,
162 inst
->instAddr(), cpu
->readCpuId(), tid
);
163 DPRINTF(InOrderCachePort
, "[sn:%i] Created memReq @%x, ->%x\n",
164 inst
->seqNum
, &cache_req
->memReq
, cache_req
->memReq
);
168 std::list
<FetchUnit::FetchBlock
*>::iterator
169 FetchUnit::findBlock(std::list
<FetchBlock
*> &fetch_blocks
, int asid
,
172 std::list
<FetchBlock
*>::iterator fetch_it
= fetch_blocks
.begin();
173 std::list
<FetchBlock
*>::iterator end_it
= fetch_blocks
.end();
175 while (fetch_it
!= end_it
) {
176 if ((*fetch_it
)->asid
== asid
&&
177 (*fetch_it
)->addr
== block_addr
) {
187 std::list
<FetchUnit::FetchBlock
*>::iterator
188 FetchUnit::findReplacementBlock()
190 std::list
<FetchBlock
*>::iterator fetch_it
= fetchBuffer
.begin();
191 std::list
<FetchBlock
*>::iterator end_it
= fetchBuffer
.end();
193 while (fetch_it
!= end_it
) {
194 if ((*fetch_it
)->cnt
== 0) {
197 DPRINTF(InOrderCachePort
, "Block %08p has %i insts pending.\n",
198 (*fetch_it
)->addr
, (*fetch_it
)->cnt
);
207 FetchUnit::markBlockUsed(std::list
<FetchBlock
*>::iterator block_it
)
209 // Move block from whatever location it is in fetch buffer
210 // to the back (represents most-recently-used location)
211 if (block_it
!= fetchBuffer
.end()) {
212 FetchBlock
*mru_blk
= *block_it
;
213 fetchBuffer
.erase(block_it
);
214 fetchBuffer
.push_back(mru_blk
);
219 FetchUnit::blocksInUse()
221 std::list
<FetchBlock
*>::iterator fetch_it
= fetchBuffer
.begin();
222 std::list
<FetchBlock
*>::iterator end_it
= fetchBuffer
.end();
225 while (fetch_it
!= end_it
) {
226 if ((*fetch_it
)->cnt
> 0)
236 FetchUnit::clearFetchBuffer()
238 std::list
<FetchBlock
*>::iterator fetch_it
= fetchBuffer
.begin();
239 std::list
<FetchBlock
*>::iterator end_it
= fetchBuffer
.end();
241 while (fetch_it
!= end_it
) {
242 if ((*fetch_it
)->block
) {
243 delete [] (*fetch_it
)->block
;
252 FetchUnit::execute(int slot_num
)
254 CacheReqPtr cache_req
= dynamic_cast<CacheReqPtr
>(reqs
[slot_num
]);
257 if (cachePortBlocked
&& cache_req
->cmd
== InitiateFetch
) {
258 DPRINTF(InOrderCachePort
, "Cache Port Blocked. Cannot Access\n");
259 cache_req
->done(false);
263 DynInstPtr inst
= cache_req
->inst
;
264 ThreadID tid
= inst
->readTid();
265 Addr block_addr
= cacheBlockAlign(inst
->getMemAddr());
266 int asid
= cpu
->asid
[tid
];
268 if (inst
->fault
!= NoFault
) {
269 DPRINTF(InOrderCachePort
,
270 "[tid:%i]: [sn:%i]: Detected %s fault @ %x. Forwarding to "
271 "next stage.\n", tid
, inst
->seqNum
, inst
->fault
->name(),
272 cacheBlockAlign(inst
->getMemAddr()));
273 finishCacheUnitReq(inst
, cache_req
);
277 switch (cache_req
->cmd
)
281 // Check to see if we've already got this request buffered
282 // or pending to be buffered
283 bool do_fetch
= true;
284 int total_pending
= pendingFetch
.size() + blocksInUse();
286 std::list
<FetchBlock
*>::iterator pending_it
;
287 pending_it
= findBlock(pendingFetch
, asid
, block_addr
);
288 if (pending_it
!= pendingFetch
.end()) {
289 (*pending_it
)->cnt
++;
292 DPRINTF(InOrderCachePort
, "%08p is a pending fetch block "
293 "(pending:%i).\n", block_addr
,
295 } else if (total_pending
< fetchBuffSize
) {
296 std::list
<FetchBlock
*>::iterator buff_it
;
297 buff_it
= findBlock(fetchBuffer
, asid
, block_addr
);
298 if (buff_it
!= fetchBuffer
.end()) {
302 DPRINTF(InOrderCachePort
, "%08p is in fetch buffer "
303 "(pending:%i).\n", block_addr
, (*buff_it
)->cnt
);
308 DPRINTF(InOrderCachePort
, "Inst. [sn:%i] marked to be filled "
309 "through fetch buffer.\n", inst
->seqNum
);
310 cache_req
->fetchBufferFill
= true;
311 cache_req
->setCompleted(true);
315 // Check to see if there is room in the fetchbuffer for this instruction.
316 // If not, block this request.
317 if (total_pending
>= fetchBuffSize
) {
318 DPRINTF(InOrderCachePort
, "No room available in fetch buffer.\n");
319 cache_req
->done(false);
323 doTLBAccess(inst
, cache_req
, cacheBlkSize
, Request::INST_FETCH
, TheISA::TLB::Execute
);
325 if (inst
->fault
== NoFault
) {
326 DPRINTF(InOrderCachePort
,
327 "[tid:%u]: Initiating fetch access to %s for "
328 "addr:%#x (block:%#x)\n", tid
, name(),
329 cache_req
->inst
->getMemAddr(), block_addr
);
331 cache_req
->reqData
= new uint8_t[cacheBlkSize
];
333 inst
->setCurResSlot(slot_num
);
337 if (cache_req
->isMemAccPending()) {
338 pendingFetch
.push_back(new FetchBlock(asid
, block_addr
));
340 // mark replacement block
348 if (inst
->fault
!= NoFault
) {
349 DPRINTF(InOrderCachePort
,
350 "[tid:%i]: [sn:%i]: Detected %s fault @ %x. Forwarding to "
351 "next stage.\n", tid
, inst
->seqNum
, inst
->fault
->name(),
353 finishCacheUnitReq(inst
, cache_req
);
357 if (cache_req
->fetchBufferFill
) {
358 // Block request if it's depending on a previous fetch, but it hasnt made it yet
359 std::list
<FetchBlock
*>::iterator fetch_it
= findBlock(fetchBuffer
, asid
, block_addr
);
360 if (fetch_it
== fetchBuffer
.end()) {
361 DPRINTF(InOrderCachePort
, "%#x not available yet\n",
363 cache_req
->setCompleted(false);
367 // Make New Instruction
368 createMachInst(fetch_it
, inst
);
369 if (inst
->traceData
) {
370 inst
->traceData
->setStaticInst(inst
->staticInst
);
371 inst
->traceData
->setPC(inst
->pcState());
374 // FetchBuffer Book-Keeping
376 assert((*fetch_it
)->cnt
>= 0);
377 markBlockUsed(fetch_it
);
383 if (cache_req
->isMemAccComplete()) {
384 if (fetchBuffer
.size() >= fetchBuffSize
) {
385 // If there is no replacement block, then we'll just have
386 // to wait till that gets cleared before satisfying the fetch
387 // for this instruction
388 std::list
<FetchBlock
*>::iterator repl_it
=
389 findReplacementBlock();
390 if (repl_it
== fetchBuffer
.end()) {
391 DPRINTF(InOrderCachePort
, "Unable to find replacement block"
392 " and complete fetch.\n");
393 cache_req
->setCompleted(false);
397 delete [] (*repl_it
)->block
;
399 fetchBuffer
.erase(repl_it
);
402 DPRINTF(InOrderCachePort
,
403 "[tid:%i]: Completing Fetch Access for [sn:%i]\n",
406 // Make New Instruction
407 std::list
<FetchBlock
*>::iterator fetch_it
=
408 findBlock(pendingFetch
, asid
, block_addr
);
410 assert(fetch_it
!= pendingFetch
.end());
411 assert((*fetch_it
)->valid
);
413 createMachInst(fetch_it
, inst
);
414 if (inst
->traceData
) {
415 inst
->traceData
->setStaticInst(inst
->staticInst
);
416 inst
->traceData
->setPC(inst
->pcState());
420 // Update instructions waiting on new fetch block
421 FetchBlock
*new_block
= (*fetch_it
);
423 assert(new_block
->cnt
>= 0);
425 // Finally, update FetchBuffer w/Pending Block into the
427 pendingFetch
.erase(fetch_it
);
428 fetchBuffer
.push_back(new_block
);
430 DPRINTF(InOrderCachePort
, "[tid:%i]: Instruction [sn:%i] is: %s\n",
432 inst
->staticInst
->disassemble(inst
->instAddr()));
434 inst
->unsetMemAddr();
438 DPRINTF(InOrderCachePort
,
439 "[tid:%i]: [sn:%i]: Unable to Complete Fetch Access\n",
441 DPRINTF(InOrderStall
,
442 "STALL: [tid:%i]: Fetch miss from %08p\n",
443 tid
, cache_req
->inst
->instAddr());
444 cache_req
->setCompleted(false);
445 // NOTE: For SwitchOnCacheMiss ThreadModel, we *don't* switch on
446 // fetch miss, but we could ...
447 // cache_req->setMemStall(true);
452 fatal("Unrecognized command to %s", resName
);
457 FetchUnit::processCacheCompletion(PacketPtr pkt
)
459 // Cast to correct packet type
460 // @todo: use pkt Sender state here to be consistent with other
462 CacheReqPacket
* cache_pkt
= dynamic_cast<CacheReqPacket
*>(pkt
);
465 DPRINTF(InOrderCachePort
, "Finished request for %x\n",
466 cache_pkt
->getAddr());
468 if (processSquash(cache_pkt
))
471 Addr block_addr
= cacheBlockAlign(cache_pkt
->cacheReq
->
472 getInst()->getMemAddr());
474 DPRINTF(InOrderCachePort
,
475 "[tid:%u]: [sn:%i]: Waking from fetch access to addr:%#x(phys:%#x), size:%i\n",
476 cache_pkt
->cacheReq
->getInst()->readTid(),
477 cache_pkt
->cacheReq
->getInst()->seqNum
,
478 block_addr
, cache_pkt
->getAddr(), cache_pkt
->getSize());
480 // Cast to correct request type
481 CacheRequest
*cache_req
= dynamic_cast<CacheReqPtr
>(
482 findRequest(cache_pkt
->cacheReq
->getInst(), cache_pkt
->instIdx
));
485 panic("[tid:%u]: [sn:%i]: Can't find slot for fetch access to "
486 "addr. %08p\n", cache_pkt
->cacheReq
->getInst()->readTid(),
487 cache_pkt
->cacheReq
->getInst()->seqNum
,
491 // Get resource request info
492 unsigned stage_num
= cache_req
->getStageNum();
493 DynInstPtr inst
= cache_req
->inst
;
494 ThreadID tid
= cache_req
->inst
->readTid();
495 short asid
= cpu
->asid
[tid
];
497 assert(!cache_req
->isSquashed());
498 assert(inst
->curSkedEntry
->cmd
== CompleteFetch
);
500 DPRINTF(InOrderCachePort
,
501 "[tid:%u]: [sn:%i]: Processing fetch access for block %#x\n",
502 tid
, inst
->seqNum
, block_addr
);
504 std::list
<FetchBlock
*>::iterator pend_it
= findBlock(pendingFetch
, asid
,
506 assert(pend_it
!= pendingFetch
.end());
508 // Copy Data to pendingFetch queue...
509 (*pend_it
)->block
= new uint8_t[cacheBlkSize
];
510 memcpy((*pend_it
)->block
, cache_pkt
->getPtr
<uint8_t>(), cacheBlkSize
);
511 (*pend_it
)->valid
= true;
513 cache_req
->setMemAccPending(false);
514 cache_req
->setMemAccCompleted();
516 if (cache_req
->isMemStall() &&
517 cpu
->threadModel
== InOrderCPU::SwitchOnCacheMiss
) {
518 DPRINTF(InOrderCachePort
, "[tid:%u] Waking up from Cache Miss.\n",
521 cpu
->activateContext(tid
);
523 DPRINTF(ThreadModel
, "Activating [tid:%i] after return from cache"
527 // Wake up the CPU (if it went to sleep and was waiting on this
528 // completion event).
531 DPRINTF(Activity
, "[tid:%u] Activating %s due to cache completion\n",
532 tid
, cpu
->pipelineStage
[stage_num
]->name());
534 cpu
->switchToActive(stage_num
);
538 FetchUnit::squashCacheRequest(CacheReqPtr req_ptr
)
540 DynInstPtr inst
= req_ptr
->getInst();
541 ThreadID tid
= inst
->readTid();
542 Addr block_addr
= cacheBlockAlign(inst
->getMemAddr());
543 int asid
= cpu
->asid
[tid
];
545 // Check Fetch Buffer (or pending fetch) for this block and
546 // update pending counts
547 std::list
<FetchBlock
*>::iterator buff_it
= findBlock(fetchBuffer
,
550 if (buff_it
!= fetchBuffer
.end()) {
552 DPRINTF(InOrderCachePort
, "[sn:%i] Removing Pending Access "
553 "for Fetch Buffer block %08p (cnt=%i)\n", inst
->seqNum
,
554 block_addr
, (*buff_it
)->cnt
);
555 assert((*buff_it
)->cnt
>= 0);
557 std::list
<FetchBlock
*>::iterator block_it
= findBlock(pendingFetch
,
560 if (block_it
!= pendingFetch
.end()) {
562 DPRINTF(InOrderCachePort
, "[sn:%i] Removing Pending Access "
563 "for Pending Buffer Block %08p (cnt=%i)\n",
565 block_addr
, (*block_it
)->cnt
);
566 assert((*block_it
)->cnt
>= 0);
567 if ((*block_it
)->cnt
== 0) {
568 if ((*block_it
)->block
) {
569 delete [] (*block_it
)->block
;
572 pendingFetch
.erase(block_it
);
577 CacheUnit::squashCacheRequest(req_ptr
);
581 FetchUnit::trap(Fault fault
, ThreadID tid
, DynInstPtr inst
)
584 predecoder
[tid
]->reset();
586 //@todo: squash using dummy inst seq num
587 squash(NULL
, NumStages
- 1, 0, tid
);
589 //@todo: make sure no blocks are in use
590 assert(blocksInUse() == 0);
591 assert(pendingFetch
.size() == 0);
593 //@todo: clear pendingFetch and fetchBuffer