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/utility.hh"
38 #include "config/the_isa.hh"
39 #include "cpu/inorder/resources/cache_unit.hh"
40 #include "cpu/inorder/resources/fetch_unit.hh"
41 #include "cpu/inorder/cpu.hh"
42 #include "cpu/inorder/pipeline_traits.hh"
43 #include "cpu/inorder/resource_pool.hh"
44 #include "debug/Activity.hh"
45 #include "debug/InOrderCachePort.hh"
46 #include "debug/InOrderStall.hh"
47 #include "debug/RefCount.hh"
48 #include "debug/ThreadModel.hh"
49 #include "mem/request.hh"
52 using namespace TheISA
;
53 using namespace ThePipeline
;
55 FetchUnit::FetchUnit(string res_name
, int res_id
, int res_width
,
56 Cycles res_latency
, InOrderCPU
*_cpu
,
57 ThePipeline::Params
*params
)
58 : CacheUnit(res_name
, res_id
, res_width
, res_latency
, _cpu
, params
),
59 instSize(sizeof(TheISA::MachInst
)), fetchBuffSize(params
->fetchBuffSize
)
61 for (int tid
= 0; tid
< MaxThreads
; tid
++)
62 decoder
[tid
] = new Decoder
;
65 FetchUnit::~FetchUnit()
67 std::list
<FetchBlock
*>::iterator fetch_it
= fetchBuffer
.begin();
68 std::list
<FetchBlock
*>::iterator end_it
= fetchBuffer
.end();
69 while (fetch_it
!= end_it
) {
70 delete (*fetch_it
)->block
;
77 std::list
<FetchBlock
*>::iterator pend_it
= pendingFetch
.begin();
78 std::list
<FetchBlock
*>::iterator pend_end
= pendingFetch
.end();
79 while (pend_it
!= pend_end
) {
80 if ((*pend_it
)->block
) {
81 delete (*pend_it
)->block
;
91 FetchUnit::createMachInst(std::list
<FetchBlock
*>::iterator fetch_it
,
94 Addr block_addr
= cacheBlockAlign(inst
->getMemAddr());
95 Addr fetch_addr
= inst
->getMemAddr();
96 unsigned fetch_offset
= (fetch_addr
- block_addr
) / instSize
;
97 ThreadID tid
= inst
->readTid();
98 TheISA::PCState instPC
= inst
->pcState();
101 DPRINTF(InOrderCachePort
, "Creating instruction [sn:%i] w/fetch data @"
102 "addr:%08p block:%08p\n", inst
->seqNum
, fetch_addr
, block_addr
);
104 assert((*fetch_it
)->valid
);
106 TheISA::MachInst
*fetchInsts
=
107 reinterpret_cast<TheISA::MachInst
*>((*fetch_it
)->block
);
110 TheISA::gtoh(fetchInsts
[fetch_offset
]);
112 decoder
[tid
]->moreBytes(instPC
, inst
->instAddr(), mach_inst
);
113 assert(decoder
[tid
]->instReady());
114 inst
->setStaticInst(decoder
[tid
]->decode(instPC
));
115 inst
->pcState(instPC
);
119 FetchUnit::removeAddrDependency(DynInstPtr inst
)
121 inst
->unsetMemAddr();
125 FetchUnit::getRequest(DynInstPtr inst
, int stage_num
, int res_idx
,
126 int slot_num
, unsigned cmd
)
128 ScheduleEntry
* sched_entry
= *inst
->curSkedEntry
;
129 CacheRequest
* cache_req
= dynamic_cast<CacheRequest
*>(reqs
[slot_num
]);
131 if (!inst
->validMemAddr()) {
132 panic("Mem. Addr. must be set before requesting cache access\n");
135 assert(sched_entry
->cmd
== InitiateFetch
);
137 DPRINTF(InOrderCachePort
,
138 "[tid:%i]: Fetch request from [sn:%i] for addr %08p\n",
139 inst
->readTid(), inst
->seqNum
, inst
->getMemAddr());
141 cache_req
->setRequest(inst
, stage_num
, id
, slot_num
,
142 sched_entry
->cmd
, MemCmd::ReadReq
,
143 inst
->curSkedEntry
->idx
);
149 FetchUnit::setupMemRequest(DynInstPtr inst
, CacheReqPtr cache_req
,
150 int acc_size
, int flags
)
152 ThreadID tid
= inst
->readTid();
153 Addr aligned_addr
= cacheBlockAlign(inst
->getMemAddr());
154 if (cache_req
->memReq
== NULL
) {
156 new Request(tid
, aligned_addr
, acc_size
, flags
,
157 cpu
->instMasterId(), inst
->instAddr(), cpu
->readCpuId(),
159 DPRINTF(InOrderCachePort
, "[sn:%i] Created memReq @%x, ->%x\n",
160 inst
->seqNum
, &cache_req
->memReq
, cache_req
->memReq
);
164 std::list
<FetchUnit::FetchBlock
*>::iterator
165 FetchUnit::findBlock(std::list
<FetchBlock
*> &fetch_blocks
, int asid
,
168 std::list
<FetchBlock
*>::iterator fetch_it
= fetch_blocks
.begin();
169 std::list
<FetchBlock
*>::iterator end_it
= fetch_blocks
.end();
171 while (fetch_it
!= end_it
) {
172 if ((*fetch_it
)->asid
== asid
&&
173 (*fetch_it
)->addr
== block_addr
) {
183 std::list
<FetchUnit::FetchBlock
*>::iterator
184 FetchUnit::findReplacementBlock()
186 std::list
<FetchBlock
*>::iterator fetch_it
= fetchBuffer
.begin();
187 std::list
<FetchBlock
*>::iterator end_it
= fetchBuffer
.end();
189 while (fetch_it
!= end_it
) {
190 if ((*fetch_it
)->cnt
== 0) {
193 DPRINTF(InOrderCachePort
, "Block %08p has %i insts pending.\n",
194 (*fetch_it
)->addr
, (*fetch_it
)->cnt
);
203 FetchUnit::markBlockUsed(std::list
<FetchBlock
*>::iterator block_it
)
205 // Move block from whatever location it is in fetch buffer
206 // to the back (represents most-recently-used location)
207 if (block_it
!= fetchBuffer
.end()) {
208 FetchBlock
*mru_blk
= *block_it
;
209 fetchBuffer
.erase(block_it
);
210 fetchBuffer
.push_back(mru_blk
);
215 FetchUnit::blocksInUse()
217 std::list
<FetchBlock
*>::iterator fetch_it
= fetchBuffer
.begin();
218 std::list
<FetchBlock
*>::iterator end_it
= fetchBuffer
.end();
221 while (fetch_it
!= end_it
) {
222 if ((*fetch_it
)->cnt
> 0)
232 FetchUnit::clearFetchBuffer()
234 std::list
<FetchBlock
*>::iterator fetch_it
= fetchBuffer
.begin();
235 std::list
<FetchBlock
*>::iterator end_it
= fetchBuffer
.end();
237 while (fetch_it
!= end_it
) {
238 if ((*fetch_it
)->block
) {
239 delete [] (*fetch_it
)->block
;
248 FetchUnit::execute(int slot_num
)
250 CacheReqPtr cache_req
= dynamic_cast<CacheReqPtr
>(reqs
[slot_num
]);
253 if (cachePortBlocked
&& cache_req
->cmd
== InitiateFetch
) {
254 DPRINTF(InOrderCachePort
, "Cache Port Blocked. Cannot Access\n");
255 cache_req
->done(false);
259 DynInstPtr inst
= cache_req
->inst
;
260 ThreadID tid
= inst
->readTid();
261 Addr block_addr
= cacheBlockAlign(inst
->getMemAddr());
262 int asid
= cpu
->asid
[tid
];
264 if (inst
->fault
!= NoFault
) {
265 DPRINTF(InOrderCachePort
,
266 "[tid:%i]: [sn:%i]: Detected %s fault @ %x. Forwarding to "
267 "next stage.\n", tid
, inst
->seqNum
, inst
->fault
->name(),
268 cacheBlockAlign(inst
->getMemAddr()));
269 finishCacheUnitReq(inst
, cache_req
);
273 switch (cache_req
->cmd
)
277 // Check to see if we've already got this request buffered
278 // or pending to be buffered
279 bool do_fetch
= true;
280 int total_pending
= pendingFetch
.size() + blocksInUse();
282 std::list
<FetchBlock
*>::iterator pending_it
;
283 pending_it
= findBlock(pendingFetch
, asid
, block_addr
);
284 if (pending_it
!= pendingFetch
.end()) {
285 (*pending_it
)->cnt
++;
288 DPRINTF(InOrderCachePort
, "%08p is a pending fetch block "
289 "(pending:%i).\n", block_addr
,
291 } else if (total_pending
< fetchBuffSize
) {
292 std::list
<FetchBlock
*>::iterator buff_it
;
293 buff_it
= findBlock(fetchBuffer
, asid
, block_addr
);
294 if (buff_it
!= fetchBuffer
.end()) {
298 DPRINTF(InOrderCachePort
, "%08p is in fetch buffer "
299 "(pending:%i).\n", block_addr
, (*buff_it
)->cnt
);
304 DPRINTF(InOrderCachePort
, "Inst. [sn:%i] marked to be filled "
305 "through fetch buffer.\n", inst
->seqNum
);
306 cache_req
->fetchBufferFill
= true;
307 cache_req
->setCompleted(true);
311 // Check to see if there is room in the fetchbuffer for this instruction.
312 // If not, block this request.
313 if (total_pending
>= fetchBuffSize
) {
314 DPRINTF(InOrderCachePort
, "No room available in fetch buffer.\n");
315 cache_req
->done(false);
319 doTLBAccess(inst
, cache_req
, cacheBlkSize
, Request::INST_FETCH
, TheISA::TLB::Execute
);
321 if (inst
->fault
== NoFault
) {
322 DPRINTF(InOrderCachePort
,
323 "[tid:%u]: Initiating fetch access to %s for "
324 "addr:%#x (block:%#x)\n", tid
, name(),
325 cache_req
->inst
->getMemAddr(), block_addr
);
327 cache_req
->reqData
= new uint8_t[cacheBlkSize
];
329 inst
->setCurResSlot(slot_num
);
333 if (cache_req
->isMemAccPending()) {
334 pendingFetch
.push_back(new FetchBlock(asid
, block_addr
));
336 // mark replacement block
344 if (inst
->fault
!= NoFault
) {
345 DPRINTF(InOrderCachePort
,
346 "[tid:%i]: [sn:%i]: Detected %s fault @ %x. Forwarding to "
347 "next stage.\n", tid
, inst
->seqNum
, inst
->fault
->name(),
349 finishCacheUnitReq(inst
, cache_req
);
353 if (cache_req
->fetchBufferFill
) {
354 // Block request if it's depending on a previous fetch, but it hasnt made it yet
355 std::list
<FetchBlock
*>::iterator fetch_it
= findBlock(fetchBuffer
, asid
, block_addr
);
356 if (fetch_it
== fetchBuffer
.end()) {
357 DPRINTF(InOrderCachePort
, "%#x not available yet\n",
359 cache_req
->setCompleted(false);
363 // Make New Instruction
364 createMachInst(fetch_it
, inst
);
365 if (inst
->traceData
) {
366 inst
->traceData
->setStaticInst(inst
->staticInst
);
367 inst
->traceData
->setPC(inst
->pcState());
370 // FetchBuffer Book-Keeping
372 assert((*fetch_it
)->cnt
>= 0);
373 markBlockUsed(fetch_it
);
379 if (cache_req
->isMemAccComplete()) {
380 if (fetchBuffer
.size() >= fetchBuffSize
) {
381 // If there is no replacement block, then we'll just have
382 // to wait till that gets cleared before satisfying the fetch
383 // for this instruction
384 std::list
<FetchBlock
*>::iterator repl_it
=
385 findReplacementBlock();
386 if (repl_it
== fetchBuffer
.end()) {
387 DPRINTF(InOrderCachePort
, "Unable to find replacement block"
388 " and complete fetch.\n");
389 cache_req
->setCompleted(false);
393 delete [] (*repl_it
)->block
;
395 fetchBuffer
.erase(repl_it
);
398 DPRINTF(InOrderCachePort
,
399 "[tid:%i]: Completing Fetch Access for [sn:%i]\n",
402 // Make New Instruction
403 std::list
<FetchBlock
*>::iterator fetch_it
=
404 findBlock(pendingFetch
, asid
, block_addr
);
406 assert(fetch_it
!= pendingFetch
.end());
407 assert((*fetch_it
)->valid
);
409 createMachInst(fetch_it
, inst
);
410 if (inst
->traceData
) {
411 inst
->traceData
->setStaticInst(inst
->staticInst
);
412 inst
->traceData
->setPC(inst
->pcState());
416 // Update instructions waiting on new fetch block
417 FetchBlock
*new_block
= (*fetch_it
);
419 assert(new_block
->cnt
>= 0);
421 // Finally, update FetchBuffer w/Pending Block into the
423 pendingFetch
.erase(fetch_it
);
424 fetchBuffer
.push_back(new_block
);
426 DPRINTF(InOrderCachePort
, "[tid:%i]: Instruction [sn:%i] is: %s\n",
428 inst
->staticInst
->disassemble(inst
->instAddr()));
430 inst
->unsetMemAddr();
434 DPRINTF(InOrderCachePort
,
435 "[tid:%i]: [sn:%i]: Unable to Complete Fetch Access\n",
437 DPRINTF(InOrderStall
,
438 "STALL: [tid:%i]: Fetch miss from %08p\n",
439 tid
, cache_req
->inst
->instAddr());
440 cache_req
->setCompleted(false);
441 // NOTE: For SwitchOnCacheMiss ThreadModel, we *don't* switch on
442 // fetch miss, but we could ...
443 // cache_req->setMemStall(true);
448 fatal("Unrecognized command to %s", resName
);
453 FetchUnit::processCacheCompletion(PacketPtr pkt
)
455 // Cast to correct packet type
456 // @todo: use pkt Sender state here to be consistent with other
458 CacheReqPacket
* cache_pkt
= dynamic_cast<CacheReqPacket
*>(pkt
);
461 DPRINTF(InOrderCachePort
, "Finished request for %x\n",
462 cache_pkt
->getAddr());
464 if (processSquash(cache_pkt
))
467 Addr block_addr
= cacheBlockAlign(cache_pkt
->cacheReq
->
468 getInst()->getMemAddr());
470 DPRINTF(InOrderCachePort
,
471 "[tid:%u]: [sn:%i]: Waking from fetch access to addr:%#x(phys:%#x), size:%i\n",
472 cache_pkt
->cacheReq
->getInst()->readTid(),
473 cache_pkt
->cacheReq
->getInst()->seqNum
,
474 block_addr
, cache_pkt
->getAddr(), cache_pkt
->getSize());
476 // Cast to correct request type
477 CacheRequest
*cache_req
= dynamic_cast<CacheReqPtr
>(
478 findRequest(cache_pkt
->cacheReq
->getInst(), cache_pkt
->instIdx
));
481 panic("[tid:%u]: [sn:%i]: Can't find slot for fetch access to "
482 "addr. %08p\n", cache_pkt
->cacheReq
->getInst()->readTid(),
483 cache_pkt
->cacheReq
->getInst()->seqNum
,
487 // Get resource request info
488 unsigned stage_num
= cache_req
->getStageNum();
489 DynInstPtr inst
= cache_req
->inst
;
490 ThreadID tid
= cache_req
->inst
->readTid();
491 short asid
= cpu
->asid
[tid
];
493 assert(!cache_req
->isSquashed());
494 assert(inst
->curSkedEntry
->cmd
== CompleteFetch
);
496 DPRINTF(InOrderCachePort
,
497 "[tid:%u]: [sn:%i]: Processing fetch access for block %#x\n",
498 tid
, inst
->seqNum
, block_addr
);
500 std::list
<FetchBlock
*>::iterator pend_it
= findBlock(pendingFetch
, asid
,
502 assert(pend_it
!= pendingFetch
.end());
504 // Copy Data to pendingFetch queue...
505 (*pend_it
)->block
= new uint8_t[cacheBlkSize
];
506 memcpy((*pend_it
)->block
, cache_pkt
->getPtr
<uint8_t>(), cacheBlkSize
);
507 (*pend_it
)->valid
= true;
509 cache_req
->setMemAccPending(false);
510 cache_req
->setMemAccCompleted();
512 if (cache_req
->isMemStall() &&
513 cpu
->threadModel
== InOrderCPU::SwitchOnCacheMiss
) {
514 DPRINTF(InOrderCachePort
, "[tid:%u] Waking up from Cache Miss.\n",
517 cpu
->activateContext(tid
);
519 DPRINTF(ThreadModel
, "Activating [tid:%i] after return from cache"
523 // Wake up the CPU (if it went to sleep and was waiting on this
524 // completion event).
527 DPRINTF(Activity
, "[tid:%u] Activating %s due to cache completion\n",
528 tid
, cpu
->pipelineStage
[stage_num
]->name());
530 cpu
->switchToActive(stage_num
);
534 FetchUnit::squashCacheRequest(CacheReqPtr req_ptr
)
536 DynInstPtr inst
= req_ptr
->getInst();
537 ThreadID tid
= inst
->readTid();
538 Addr block_addr
= cacheBlockAlign(inst
->getMemAddr());
539 int asid
= cpu
->asid
[tid
];
541 // Check Fetch Buffer (or pending fetch) for this block and
542 // update pending counts
543 std::list
<FetchBlock
*>::iterator buff_it
= findBlock(fetchBuffer
,
546 if (buff_it
!= fetchBuffer
.end()) {
548 DPRINTF(InOrderCachePort
, "[sn:%i] Removing Pending Access "
549 "for Fetch Buffer block %08p (cnt=%i)\n", inst
->seqNum
,
550 block_addr
, (*buff_it
)->cnt
);
551 assert((*buff_it
)->cnt
>= 0);
553 std::list
<FetchBlock
*>::iterator block_it
= findBlock(pendingFetch
,
556 if (block_it
!= pendingFetch
.end()) {
558 DPRINTF(InOrderCachePort
, "[sn:%i] Removing Pending Access "
559 "for Pending Buffer Block %08p (cnt=%i)\n",
561 block_addr
, (*block_it
)->cnt
);
562 assert((*block_it
)->cnt
>= 0);
563 if ((*block_it
)->cnt
== 0) {
564 if ((*block_it
)->block
) {
565 delete [] (*block_it
)->block
;
568 pendingFetch
.erase(block_it
);
573 CacheUnit::squashCacheRequest(req_ptr
);
577 FetchUnit::trap(Fault fault
, ThreadID tid
, DynInstPtr inst
)
580 decoder
[tid
]->reset();
582 //@todo: squash using dummy inst seq num
583 squash(NULL
, NumStages
- 1, 0, tid
);
585 //@todo: make sure no blocks are in use
586 assert(blocksInUse() == 0);
587 assert(pendingFetch
.size() == 0);
589 //@todo: clear pendingFetch and fetchBuffer