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 "arch/predecoder.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/pipeline_traits.hh"
43 #include "cpu/inorder/cpu.hh"
44 #include "cpu/inorder/resource_pool.hh"
45 #include "mem/request.hh"
48 using namespace TheISA
;
49 using namespace ThePipeline
;
51 FetchUnit::FetchUnit(string res_name
, int res_id
, int res_width
,
52 int res_latency
, InOrderCPU
*_cpu
,
53 ThePipeline::Params
*params
)
54 : CacheUnit(res_name
, res_id
, res_width
, res_latency
, _cpu
, params
),
55 instSize(sizeof(TheISA::MachInst
)), fetchBuffSize(params
->fetchBuffSize
),
59 FetchUnit::~FetchUnit()
61 std::list
<FetchBlock
*>::iterator fetch_it
= fetchBuffer
.begin();
62 std::list
<FetchBlock
*>::iterator end_it
= fetchBuffer
.end();
63 while (fetch_it
!= end_it
) {
64 delete (*fetch_it
)->block
;
71 std::list
<FetchBlock
*>::iterator pend_it
= pendingFetch
.begin();
72 std::list
<FetchBlock
*>::iterator pend_end
= pendingFetch
.end();
73 while (pend_it
!= pend_end
) {
74 if ((*pend_it
)->block
) {
75 delete (*pend_it
)->block
;
85 FetchUnit::createMachInst(std::list
<FetchBlock
*>::iterator fetch_it
,
89 Addr block_addr
= cacheBlockAlign(inst
->getMemAddr());
90 Addr fetch_addr
= inst
->getMemAddr();
91 unsigned fetch_offset
= (fetch_addr
- block_addr
) / instSize
;
92 ThreadID tid
= inst
->readTid();
93 TheISA::PCState instPC
= inst
->pcState();
96 DPRINTF(InOrderCachePort
, "Creating instruction [sn:%i] w/fetch data @"
97 "addr:%08p block:%08p\n", inst
->seqNum
, fetch_addr
, block_addr
);
99 assert((*fetch_it
)->valid
);
101 TheISA::MachInst
*fetchInsts
=
102 reinterpret_cast<TheISA::MachInst
*>((*fetch_it
)->block
);
105 TheISA::gtoh(fetchInsts
[fetch_offset
]);
107 predecoder
.setTC(cpu
->thread
[tid
]->getTC());
108 predecoder
.moreBytes(instPC
, inst
->instAddr(), mach_inst
);
109 ext_inst
= predecoder
.getExtMachInst(instPC
);
111 inst
->pcState(instPC
);
112 inst
->setMachInst(ext_inst
);
116 FetchUnit::getSlot(DynInstPtr inst
)
118 if (tlbBlocked
[inst
->threadNumber
]) {
122 if (!inst
->validMemAddr()) {
123 panic("[tid:%i][sn:%i] Mem. Addr. must be set before requesting "
124 "cache access\n", inst
->readTid(), inst
->seqNum
);
127 int new_slot
= Resource::getSlot(inst
);
132 inst
->memTime
= curTick();
137 FetchUnit::removeAddrDependency(DynInstPtr inst
)
139 inst
->unsetMemAddr();
143 FetchUnit::getRequest(DynInstPtr inst
, int stage_num
, int res_idx
,
144 int slot_num
, unsigned cmd
)
146 ScheduleEntry
* sched_entry
= *inst
->curSkedEntry
;
147 CacheRequest
* cache_req
= dynamic_cast<CacheRequest
*>(reqs
[slot_num
]);
149 if (!inst
->validMemAddr()) {
150 panic("Mem. Addr. must be set before requesting cache access\n");
153 assert(sched_entry
->cmd
== InitiateFetch
);
155 DPRINTF(InOrderCachePort
,
156 "[tid:%i]: Fetch request from [sn:%i] for addr %08p\n",
157 inst
->readTid(), inst
->seqNum
, inst
->getMemAddr());
159 cache_req
->setRequest(inst
, stage_num
, id
, slot_num
,
160 sched_entry
->cmd
, MemCmd::ReadReq
,
161 inst
->curSkedEntry
->idx
);
167 FetchUnit::setupMemRequest(DynInstPtr inst
, CacheReqPtr cache_req
,
168 int acc_size
, int flags
)
170 ThreadID tid
= inst
->readTid();
171 Addr aligned_addr
= cacheBlockAlign(inst
->getMemAddr());
174 new Request(tid
, aligned_addr
, acc_size
, flags
,
175 inst
->instAddr(), cpu
->readCpuId(), tid
);
177 cache_req
->memReq
= inst
->fetchMemReq
;
180 std::list
<FetchUnit::FetchBlock
*>::iterator
181 FetchUnit::findBlock(std::list
<FetchBlock
*> &fetch_blocks
, int asid
,
184 std::list
<FetchBlock
*>::iterator fetch_it
= fetch_blocks
.begin();
185 std::list
<FetchBlock
*>::iterator end_it
= fetch_blocks
.end();
187 while (fetch_it
!= end_it
) {
188 if ((*fetch_it
)->asid
== asid
&&
189 (*fetch_it
)->addr
== block_addr
) {
199 std::list
<FetchUnit::FetchBlock
*>::iterator
200 FetchUnit::findReplacementBlock()
202 std::list
<FetchBlock
*>::iterator fetch_it
= fetchBuffer
.begin();
203 std::list
<FetchBlock
*>::iterator end_it
= fetchBuffer
.end();
205 while (fetch_it
!= end_it
) {
206 if ((*fetch_it
)->cnt
== 0) {
209 DPRINTF(InOrderCachePort
, "Block %08p has %i insts pending.\n",
210 (*fetch_it
)->addr
, (*fetch_it
)->cnt
);
219 FetchUnit::markBlockUsed(std::list
<FetchBlock
*>::iterator block_it
)
221 // Move block from whatever location it is in fetch buffer
222 // to the back (represents most-recently-used location)
223 if (block_it
!= fetchBuffer
.end()) {
224 FetchBlock
*mru_blk
= *block_it
;
225 fetchBuffer
.erase(block_it
);
226 fetchBuffer
.push_back(mru_blk
);
231 FetchUnit::execute(int slot_num
)
233 CacheReqPtr cache_req
= dynamic_cast<CacheReqPtr
>(reqs
[slot_num
]);
236 if (cachePortBlocked
&& cache_req
->cmd
== InitiateFetch
) {
237 DPRINTF(InOrderCachePort
, "Cache Port Blocked. Cannot Access\n");
238 cache_req
->done(false);
242 DynInstPtr inst
= cache_req
->inst
;
243 ThreadID tid
= inst
->readTid();
244 Addr block_addr
= cacheBlockAlign(inst
->getMemAddr());
245 int asid
= cpu
->asid
[tid
];
247 inst
->fault
= NoFault
;
249 switch (cache_req
->cmd
)
253 // Check to see if we've already got this request buffered
254 // or pending to be buffered
255 bool do_fetch
= true;
256 std::list
<FetchBlock
*>::iterator pending_it
;
257 pending_it
= findBlock(pendingFetch
, asid
, block_addr
);
258 if (pending_it
!= pendingFetch
.end()) {
259 (*pending_it
)->cnt
++;
262 DPRINTF(InOrderCachePort
, "%08p is a pending fetch block "
263 "(pending:%i).\n", block_addr
,
265 } else if (pendingFetch
.size() < fetchBuffSize
) {
266 std::list
<FetchBlock
*>::iterator buff_it
;
267 buff_it
= findBlock(fetchBuffer
, asid
, block_addr
);
268 if (buff_it
!= fetchBuffer
.end()) {
272 DPRINTF(InOrderCachePort
, "%08p is in fetch buffer"
273 "(pending:%i).\n", block_addr
, (*buff_it
)->cnt
);
278 DPRINTF(InOrderCachePort
, "Inst. [sn:%i] marked to be filled "
279 "through fetch buffer.\n", inst
->seqNum
);
280 cache_req
->fetchBufferFill
= true;
281 cache_req
->setCompleted(true);
285 // Check to see if there is room in the fetchbuffer for this instruction.
286 // If not, block this request.
287 if (pendingFetch
.size() >= fetchBuffSize
) {
288 DPRINTF(InOrderCachePort
, "No room available in fetch buffer.\n");
293 doTLBAccess(inst
, cache_req
, cacheBlkSize
, 0, TheISA::TLB::Execute
);
295 if (inst
->fault
== NoFault
) {
296 DPRINTF(InOrderCachePort
,
297 "[tid:%u]: Initiating fetch access to %s for "
298 "addr:%#x (block:%#x)\n", tid
, name(),
299 cache_req
->inst
->getMemAddr(), block_addr
);
301 cache_req
->reqData
= new uint8_t[cacheBlkSize
];
303 inst
->setCurResSlot(slot_num
);
307 if (cache_req
->isMemAccPending()) {
308 pendingFetch
.push_back(new FetchBlock(asid
, block_addr
));
316 if (cache_req
->fetchBufferFill
) {
317 // Block request if it's depending on a previous fetch, but it hasnt made it yet
318 std::list
<FetchBlock
*>::iterator fetch_it
= findBlock(fetchBuffer
, asid
, block_addr
);
319 if (fetch_it
== fetchBuffer
.end()) {
320 DPRINTF(InOrderCachePort
, "%#x not available yet\n",
322 cache_req
->setCompleted(false);
326 // Make New Instruction
327 createMachInst(fetch_it
, inst
);
328 if (inst
->traceData
) {
329 inst
->traceData
->setStaticInst(inst
->staticInst
);
330 inst
->traceData
->setPC(inst
->pcState());
333 // FetchBuffer Book-Keeping
335 assert((*fetch_it
)->cnt
>= 0);
336 markBlockUsed(fetch_it
);
342 if (cache_req
->isMemAccComplete()) {
343 if (fetchBuffer
.size() >= fetchBuffSize
) {
344 // If there is no replacement block, then we'll just have
345 // to wait till that gets cleared before satisfying the fetch
346 // for this instruction
347 std::list
<FetchBlock
*>::iterator repl_it
=
348 findReplacementBlock();
349 if (repl_it
== fetchBuffer
.end()) {
350 DPRINTF(InOrderCachePort
, "Unable to find replacement block"
351 " and complete fetch.\n");
352 cache_req
->setCompleted(false);
356 delete [] (*repl_it
)->block
;
358 fetchBuffer
.erase(repl_it
);
361 DPRINTF(InOrderCachePort
,
362 "[tid:%i]: Completing Fetch Access for [sn:%i]\n",
365 // Make New Instruction
366 std::list
<FetchBlock
*>::iterator fetch_it
=
367 findBlock(pendingFetch
, asid
, block_addr
);
369 assert(fetch_it
!= pendingFetch
.end());
370 assert((*fetch_it
)->valid
);
372 createMachInst(fetch_it
, inst
);
373 if (inst
->traceData
) {
374 inst
->traceData
->setStaticInst(inst
->staticInst
);
375 inst
->traceData
->setPC(inst
->pcState());
379 // Update instructions waiting on new fetch block
380 FetchBlock
*new_block
= (*fetch_it
);
382 assert(new_block
->cnt
>= 0);
384 // Finally, update FetchBuffer w/Pending Block into the
386 pendingFetch
.erase(fetch_it
);
387 fetchBuffer
.push_back(new_block
);
389 DPRINTF(InOrderCachePort
, "[tid:%i]: Instruction [sn:%i] is: %s\n",
391 inst
->staticInst
->disassemble(inst
->instAddr()));
393 inst
->unsetMemAddr();
395 delete cache_req
->dataPkt
;
399 DPRINTF(InOrderCachePort
,
400 "[tid:%i]: [sn:%i]: Unable to Complete Fetch Access\n",
402 DPRINTF(InOrderStall
,
403 "STALL: [tid:%i]: Fetch miss from %08p\n",
404 tid
, cache_req
->inst
->instAddr());
405 cache_req
->setCompleted(false);
406 // NOTE: For SwitchOnCacheMiss ThreadModel, we *don't* switch on
407 // fetch miss, but we could ...
408 // cache_req->setMemStall(true);
413 fatal("Unrecognized command to %s", resName
);
418 FetchUnit::processCacheCompletion(PacketPtr pkt
)
420 // Cast to correct packet type
421 CacheReqPacket
* cache_pkt
= dynamic_cast<CacheReqPacket
*>(pkt
);
424 if (cache_pkt
->cacheReq
->isSquashed()) {
425 DPRINTF(InOrderCachePort
,
426 "Ignoring completion of squashed access, [tid:%i] [sn:%i]\n",
427 cache_pkt
->cacheReq
->getInst()->readTid(),
428 cache_pkt
->cacheReq
->getInst()->seqNum
);
430 "Ignoring completion of squashed access, [tid:%i] [sn:%i]\n",
431 cache_pkt
->cacheReq
->getTid(),
432 cache_pkt
->cacheReq
->seqNum
);
434 cache_pkt
->cacheReq
->done();
435 cache_pkt
->cacheReq
->freeSlot();
442 Addr block_addr
= cacheBlockAlign(cache_pkt
->cacheReq
->
443 getInst()->getMemAddr());
445 DPRINTF(InOrderCachePort
,
446 "[tid:%u]: [sn:%i]: Waking from fetch access to addr:%#x(phys:%#x), size:%i\n",
447 cache_pkt
->cacheReq
->getInst()->readTid(),
448 cache_pkt
->cacheReq
->getInst()->seqNum
,
449 block_addr
, cache_pkt
->getAddr(), cache_pkt
->getSize());
451 // Cast to correct request type
452 CacheRequest
*cache_req
= dynamic_cast<CacheReqPtr
>(
453 findRequest(cache_pkt
->cacheReq
->getInst(), cache_pkt
->instIdx
));
456 panic("[tid:%u]: [sn:%i]: Can't find slot for fetch access to "
457 "addr. %08p\n", cache_pkt
->cacheReq
->getInst()->readTid(),
458 cache_pkt
->cacheReq
->getInst()->seqNum
,
462 // Get resource request info
463 unsigned stage_num
= cache_req
->getStageNum();
464 DynInstPtr inst
= cache_req
->inst
;
465 ThreadID tid
= cache_req
->inst
->readTid();
466 short asid
= cpu
->asid
[tid
];
468 assert(!cache_req
->isSquashed());
469 assert(inst
->curSkedEntry
->cmd
== CompleteFetch
);
471 DPRINTF(InOrderCachePort
,
472 "[tid:%u]: [sn:%i]: Processing fetch access for block %#x\n",
473 tid
, inst
->seqNum
, block_addr
);
475 std::list
<FetchBlock
*>::iterator pend_it
= findBlock(pendingFetch
, asid
,
477 assert(pend_it
!= pendingFetch
.end());
479 // Copy Data to pendingFetch queue...
480 (*pend_it
)->block
= new uint8_t[cacheBlkSize
];
481 memcpy((*pend_it
)->block
, cache_pkt
->getPtr
<uint8_t>(), cacheBlkSize
);
482 (*pend_it
)->valid
= true;
484 cache_req
->setMemAccPending(false);
485 cache_req
->setMemAccCompleted();
487 if (cache_req
->isMemStall() &&
488 cpu
->threadModel
== InOrderCPU::SwitchOnCacheMiss
) {
489 DPRINTF(InOrderCachePort
, "[tid:%u] Waking up from Cache Miss.\n",
492 cpu
->activateContext(tid
);
494 DPRINTF(ThreadModel
, "Activating [tid:%i] after return from cache"
498 // Wake up the CPU (if it went to sleep and was waiting on this
499 // completion event).
502 DPRINTF(Activity
, "[tid:%u] Activating %s due to cache completion\n",
503 tid
, cpu
->pipelineStage
[stage_num
]->name());
505 cpu
->switchToActive(stage_num
);
509 FetchUnit::squashCacheRequest(CacheReqPtr req_ptr
)
511 DynInstPtr inst
= req_ptr
->getInst();
512 ThreadID tid
= inst
->readTid();
513 Addr block_addr
= cacheBlockAlign(inst
->getMemAddr());
514 int asid
= cpu
->asid
[tid
];
516 // Check Fetch Buffer (or pending fetch) for this block and
517 // update pending counts
518 std::list
<FetchBlock
*>::iterator buff_it
= findBlock(fetchBuffer
,
521 if (buff_it
!= fetchBuffer
.end()) {
523 DPRINTF(InOrderCachePort
, "[sn:%i] Removing Pending Fetch "
524 "for Buffer block %08p (cnt=%i)\n", inst
->seqNum
,
525 block_addr
, (*buff_it
)->cnt
);
527 std::list
<FetchBlock
*>::iterator block_it
= findBlock(pendingFetch
,
530 if (block_it
!= pendingFetch
.end()) {
532 if ((*block_it
)->cnt
== 0) {
533 DPRINTF(InOrderCachePort
, "[sn:%i] Removing Pending Fetch "
534 "for block %08p (cnt=%i)\n", inst
->seqNum
,
535 block_addr
, (*block_it
)->cnt
);
536 if ((*block_it
)->block
) {
537 delete [] (*block_it
)->block
;
540 pendingFetch
.erase(block_it
);
545 CacheUnit::squashCacheRequest(req_ptr
);