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
),
64 FetchUnit::~FetchUnit()
66 std::list
<FetchBlock
*>::iterator fetch_it
= fetchBuffer
.begin();
67 std::list
<FetchBlock
*>::iterator end_it
= fetchBuffer
.end();
68 while (fetch_it
!= end_it
) {
69 delete (*fetch_it
)->block
;
76 std::list
<FetchBlock
*>::iterator pend_it
= pendingFetch
.begin();
77 std::list
<FetchBlock
*>::iterator pend_end
= pendingFetch
.end();
78 while (pend_it
!= pend_end
) {
79 if ((*pend_it
)->block
) {
80 delete (*pend_it
)->block
;
90 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 predecoder
.setTC(cpu
->thread
[tid
]->getTC());
113 predecoder
.moreBytes(instPC
, inst
->instAddr(), mach_inst
);
114 assert(predecoder
.extMachInstReady());
115 ext_inst
= predecoder
.getExtMachInst(instPC
);
117 inst
->pcState(instPC
);
118 inst
->setMachInst(ext_inst
);
122 FetchUnit::removeAddrDependency(DynInstPtr inst
)
124 inst
->unsetMemAddr();
128 FetchUnit::getRequest(DynInstPtr inst
, int stage_num
, int res_idx
,
129 int slot_num
, unsigned cmd
)
131 ScheduleEntry
* sched_entry
= *inst
->curSkedEntry
;
132 CacheRequest
* cache_req
= dynamic_cast<CacheRequest
*>(reqs
[slot_num
]);
134 if (!inst
->validMemAddr()) {
135 panic("Mem. Addr. must be set before requesting cache access\n");
138 assert(sched_entry
->cmd
== InitiateFetch
);
140 DPRINTF(InOrderCachePort
,
141 "[tid:%i]: Fetch request from [sn:%i] for addr %08p\n",
142 inst
->readTid(), inst
->seqNum
, inst
->getMemAddr());
144 cache_req
->setRequest(inst
, stage_num
, id
, slot_num
,
145 sched_entry
->cmd
, MemCmd::ReadReq
,
146 inst
->curSkedEntry
->idx
);
152 FetchUnit::setupMemRequest(DynInstPtr inst
, CacheReqPtr cache_req
,
153 int acc_size
, int flags
)
155 ThreadID tid
= inst
->readTid();
156 Addr aligned_addr
= cacheBlockAlign(inst
->getMemAddr());
157 if (cache_req
->memReq
== NULL
) {
159 new Request(tid
, aligned_addr
, acc_size
, flags
,
160 inst
->instAddr(), cpu
->readCpuId(), tid
);
161 DPRINTF(InOrderCachePort
, "[sn:%i] Created memReq @%x, ->%x\n",
162 inst
->seqNum
, &cache_req
->memReq
, cache_req
->memReq
);
166 std::list
<FetchUnit::FetchBlock
*>::iterator
167 FetchUnit::findBlock(std::list
<FetchBlock
*> &fetch_blocks
, int asid
,
170 std::list
<FetchBlock
*>::iterator fetch_it
= fetch_blocks
.begin();
171 std::list
<FetchBlock
*>::iterator end_it
= fetch_blocks
.end();
173 while (fetch_it
!= end_it
) {
174 if ((*fetch_it
)->asid
== asid
&&
175 (*fetch_it
)->addr
== block_addr
) {
185 std::list
<FetchUnit::FetchBlock
*>::iterator
186 FetchUnit::findReplacementBlock()
188 std::list
<FetchBlock
*>::iterator fetch_it
= fetchBuffer
.begin();
189 std::list
<FetchBlock
*>::iterator end_it
= fetchBuffer
.end();
191 while (fetch_it
!= end_it
) {
192 if ((*fetch_it
)->cnt
== 0) {
195 DPRINTF(InOrderCachePort
, "Block %08p has %i insts pending.\n",
196 (*fetch_it
)->addr
, (*fetch_it
)->cnt
);
205 FetchUnit::markBlockUsed(std::list
<FetchBlock
*>::iterator block_it
)
207 // Move block from whatever location it is in fetch buffer
208 // to the back (represents most-recently-used location)
209 if (block_it
!= fetchBuffer
.end()) {
210 FetchBlock
*mru_blk
= *block_it
;
211 fetchBuffer
.erase(block_it
);
212 fetchBuffer
.push_back(mru_blk
);
217 FetchUnit::blocksInUse()
219 std::list
<FetchBlock
*>::iterator fetch_it
= fetchBuffer
.begin();
220 std::list
<FetchBlock
*>::iterator end_it
= fetchBuffer
.end();
223 while (fetch_it
!= end_it
) {
224 if ((*fetch_it
)->cnt
> 0)
234 FetchUnit::execute(int slot_num
)
236 CacheReqPtr cache_req
= dynamic_cast<CacheReqPtr
>(reqs
[slot_num
]);
239 if (cachePortBlocked
&& cache_req
->cmd
== InitiateFetch
) {
240 DPRINTF(InOrderCachePort
, "Cache Port Blocked. Cannot Access\n");
241 cache_req
->done(false);
245 DynInstPtr inst
= cache_req
->inst
;
246 ThreadID tid
= inst
->readTid();
247 Addr block_addr
= cacheBlockAlign(inst
->getMemAddr());
248 int asid
= cpu
->asid
[tid
];
250 inst
->fault
= NoFault
;
252 switch (cache_req
->cmd
)
256 // Check to see if we've already got this request buffered
257 // or pending to be buffered
258 bool do_fetch
= true;
259 int total_pending
= pendingFetch
.size() + blocksInUse();
261 std::list
<FetchBlock
*>::iterator pending_it
;
262 pending_it
= findBlock(pendingFetch
, asid
, block_addr
);
263 if (pending_it
!= pendingFetch
.end()) {
264 (*pending_it
)->cnt
++;
267 DPRINTF(InOrderCachePort
, "%08p is a pending fetch block "
268 "(pending:%i).\n", block_addr
,
270 } else if (total_pending
< fetchBuffSize
) {
271 std::list
<FetchBlock
*>::iterator buff_it
;
272 buff_it
= findBlock(fetchBuffer
, asid
, block_addr
);
273 if (buff_it
!= fetchBuffer
.end()) {
277 DPRINTF(InOrderCachePort
, "%08p is in fetch buffer "
278 "(pending:%i).\n", block_addr
, (*buff_it
)->cnt
);
283 DPRINTF(InOrderCachePort
, "Inst. [sn:%i] marked to be filled "
284 "through fetch buffer.\n", inst
->seqNum
);
285 cache_req
->fetchBufferFill
= true;
286 cache_req
->setCompleted(true);
290 // Check to see if there is room in the fetchbuffer for this instruction.
291 // If not, block this request.
292 if (total_pending
>= fetchBuffSize
) {
293 DPRINTF(InOrderCachePort
, "No room available in fetch buffer.\n");
294 cache_req
->done(false);
298 doTLBAccess(inst
, cache_req
, cacheBlkSize
, 0, TheISA::TLB::Execute
);
300 if (inst
->fault
== NoFault
) {
301 DPRINTF(InOrderCachePort
,
302 "[tid:%u]: Initiating fetch access to %s for "
303 "addr:%#x (block:%#x)\n", tid
, name(),
304 cache_req
->inst
->getMemAddr(), block_addr
);
306 cache_req
->reqData
= new uint8_t[cacheBlkSize
];
308 inst
->setCurResSlot(slot_num
);
312 if (cache_req
->isMemAccPending()) {
313 pendingFetch
.push_back(new FetchBlock(asid
, block_addr
));
315 // mark replacement block
323 if (cache_req
->fetchBufferFill
) {
324 // Block request if it's depending on a previous fetch, but it hasnt made it yet
325 std::list
<FetchBlock
*>::iterator fetch_it
= findBlock(fetchBuffer
, asid
, block_addr
);
326 if (fetch_it
== fetchBuffer
.end()) {
327 DPRINTF(InOrderCachePort
, "%#x not available yet\n",
329 cache_req
->setCompleted(false);
333 // Make New Instruction
334 createMachInst(fetch_it
, inst
);
335 if (inst
->traceData
) {
336 inst
->traceData
->setStaticInst(inst
->staticInst
);
337 inst
->traceData
->setPC(inst
->pcState());
340 // FetchBuffer Book-Keeping
342 assert((*fetch_it
)->cnt
>= 0);
343 markBlockUsed(fetch_it
);
349 if (cache_req
->isMemAccComplete()) {
350 if (fetchBuffer
.size() >= fetchBuffSize
) {
351 // If there is no replacement block, then we'll just have
352 // to wait till that gets cleared before satisfying the fetch
353 // for this instruction
354 std::list
<FetchBlock
*>::iterator repl_it
=
355 findReplacementBlock();
356 if (repl_it
== fetchBuffer
.end()) {
357 DPRINTF(InOrderCachePort
, "Unable to find replacement block"
358 " and complete fetch.\n");
359 cache_req
->setCompleted(false);
363 delete [] (*repl_it
)->block
;
365 fetchBuffer
.erase(repl_it
);
368 DPRINTF(InOrderCachePort
,
369 "[tid:%i]: Completing Fetch Access for [sn:%i]\n",
372 // Make New Instruction
373 std::list
<FetchBlock
*>::iterator fetch_it
=
374 findBlock(pendingFetch
, asid
, block_addr
);
376 assert(fetch_it
!= pendingFetch
.end());
377 assert((*fetch_it
)->valid
);
379 createMachInst(fetch_it
, inst
);
380 if (inst
->traceData
) {
381 inst
->traceData
->setStaticInst(inst
->staticInst
);
382 inst
->traceData
->setPC(inst
->pcState());
386 // Update instructions waiting on new fetch block
387 FetchBlock
*new_block
= (*fetch_it
);
389 assert(new_block
->cnt
>= 0);
391 // Finally, update FetchBuffer w/Pending Block into the
393 pendingFetch
.erase(fetch_it
);
394 fetchBuffer
.push_back(new_block
);
396 DPRINTF(InOrderCachePort
, "[tid:%i]: Instruction [sn:%i] is: %s\n",
398 inst
->staticInst
->disassemble(inst
->instAddr()));
400 inst
->unsetMemAddr();
404 DPRINTF(InOrderCachePort
,
405 "[tid:%i]: [sn:%i]: Unable to Complete Fetch Access\n",
407 DPRINTF(InOrderStall
,
408 "STALL: [tid:%i]: Fetch miss from %08p\n",
409 tid
, cache_req
->inst
->instAddr());
410 cache_req
->setCompleted(false);
411 // NOTE: For SwitchOnCacheMiss ThreadModel, we *don't* switch on
412 // fetch miss, but we could ...
413 // cache_req->setMemStall(true);
418 fatal("Unrecognized command to %s", resName
);
423 FetchUnit::processCacheCompletion(PacketPtr pkt
)
425 // Cast to correct packet type
426 // @todo: use pkt Sender state here to be consistent with other
428 CacheReqPacket
* cache_pkt
= dynamic_cast<CacheReqPacket
*>(pkt
);
431 DPRINTF(InOrderCachePort
, "Finished request for %x\n",
432 cache_pkt
->getAddr());
434 if (processSquash(cache_pkt
))
437 Addr block_addr
= cacheBlockAlign(cache_pkt
->cacheReq
->
438 getInst()->getMemAddr());
440 DPRINTF(InOrderCachePort
,
441 "[tid:%u]: [sn:%i]: Waking from fetch access to addr:%#x(phys:%#x), size:%i\n",
442 cache_pkt
->cacheReq
->getInst()->readTid(),
443 cache_pkt
->cacheReq
->getInst()->seqNum
,
444 block_addr
, cache_pkt
->getAddr(), cache_pkt
->getSize());
446 // Cast to correct request type
447 CacheRequest
*cache_req
= dynamic_cast<CacheReqPtr
>(
448 findRequest(cache_pkt
->cacheReq
->getInst(), cache_pkt
->instIdx
));
451 panic("[tid:%u]: [sn:%i]: Can't find slot for fetch access to "
452 "addr. %08p\n", cache_pkt
->cacheReq
->getInst()->readTid(),
453 cache_pkt
->cacheReq
->getInst()->seqNum
,
457 // Get resource request info
458 unsigned stage_num
= cache_req
->getStageNum();
459 DynInstPtr inst
= cache_req
->inst
;
460 ThreadID tid
= cache_req
->inst
->readTid();
461 short asid
= cpu
->asid
[tid
];
463 assert(!cache_req
->isSquashed());
464 assert(inst
->curSkedEntry
->cmd
== CompleteFetch
);
466 DPRINTF(InOrderCachePort
,
467 "[tid:%u]: [sn:%i]: Processing fetch access for block %#x\n",
468 tid
, inst
->seqNum
, block_addr
);
470 std::list
<FetchBlock
*>::iterator pend_it
= findBlock(pendingFetch
, asid
,
472 assert(pend_it
!= pendingFetch
.end());
474 // Copy Data to pendingFetch queue...
475 (*pend_it
)->block
= new uint8_t[cacheBlkSize
];
476 memcpy((*pend_it
)->block
, cache_pkt
->getPtr
<uint8_t>(), cacheBlkSize
);
477 (*pend_it
)->valid
= true;
479 cache_req
->setMemAccPending(false);
480 cache_req
->setMemAccCompleted();
482 if (cache_req
->isMemStall() &&
483 cpu
->threadModel
== InOrderCPU::SwitchOnCacheMiss
) {
484 DPRINTF(InOrderCachePort
, "[tid:%u] Waking up from Cache Miss.\n",
487 cpu
->activateContext(tid
);
489 DPRINTF(ThreadModel
, "Activating [tid:%i] after return from cache"
493 // Wake up the CPU (if it went to sleep and was waiting on this
494 // completion event).
497 DPRINTF(Activity
, "[tid:%u] Activating %s due to cache completion\n",
498 tid
, cpu
->pipelineStage
[stage_num
]->name());
500 cpu
->switchToActive(stage_num
);
504 FetchUnit::squashCacheRequest(CacheReqPtr req_ptr
)
506 DynInstPtr inst
= req_ptr
->getInst();
507 ThreadID tid
= inst
->readTid();
508 Addr block_addr
= cacheBlockAlign(inst
->getMemAddr());
509 int asid
= cpu
->asid
[tid
];
511 // Check Fetch Buffer (or pending fetch) for this block and
512 // update pending counts
513 std::list
<FetchBlock
*>::iterator buff_it
= findBlock(fetchBuffer
,
516 if (buff_it
!= fetchBuffer
.end()) {
518 DPRINTF(InOrderCachePort
, "[sn:%i] Removing Pending Access "
519 "for Fetch Buffer block %08p (cnt=%i)\n", inst
->seqNum
,
520 block_addr
, (*buff_it
)->cnt
);
521 assert((*buff_it
)->cnt
>= 0);
523 std::list
<FetchBlock
*>::iterator block_it
= findBlock(pendingFetch
,
526 if (block_it
!= pendingFetch
.end()) {
528 DPRINTF(InOrderCachePort
, "[sn:%i] Removing Pending Access "
529 "for Pending Buffer Block %08p (cnt=%i)\n",
531 block_addr
, (*block_it
)->cnt
);
532 assert((*block_it
)->cnt
>= 0);
533 if ((*block_it
)->cnt
== 0) {
534 if ((*block_it
)->block
) {
535 delete [] (*block_it
)->block
;
538 pendingFetch
.erase(block_it
);
543 CacheUnit::squashCacheRequest(req_ptr
);
547 FetchUnit::trap(Fault fault
, ThreadID tid
, DynInstPtr inst
)