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
),
60 FetchUnit::createMachInst(std::list
<FetchBlock
*>::iterator fetch_it
,
64 Addr block_addr
= cacheBlockAlign(inst
->getMemAddr());
65 Addr fetch_addr
= inst
->getMemAddr();
66 unsigned fetch_offset
= (fetch_addr
- block_addr
) / instSize
;
67 ThreadID tid
= inst
->readTid();
68 TheISA::PCState instPC
= inst
->pcState();
71 DPRINTF(InOrderCachePort
, "Creating instruction [sn:%i] w/fetch data @"
72 "addr:%08p block:%08p\n", inst
->seqNum
, fetch_addr
, block_addr
);
74 assert((*fetch_it
)->valid
);
76 TheISA::MachInst
*fetchInsts
=
77 reinterpret_cast<TheISA::MachInst
*>((*fetch_it
)->block
);
80 TheISA::gtoh(fetchInsts
[fetch_offset
]);
82 predecoder
.setTC(cpu
->thread
[tid
]->getTC());
83 predecoder
.moreBytes(instPC
, inst
->instAddr(), mach_inst
);
84 ext_inst
= predecoder
.getExtMachInst(instPC
);
86 inst
->pcState(instPC
);
87 inst
->setMachInst(ext_inst
);
91 FetchUnit::getSlot(DynInstPtr inst
)
93 if (tlbBlocked
[inst
->threadNumber
]) {
97 if (!inst
->validMemAddr()) {
98 panic("[tid:%i][sn:%i] Mem. Addr. must be set before requesting "
99 "cache access\n", inst
->readTid(), inst
->seqNum
);
102 int new_slot
= Resource::getSlot(inst
);
107 inst
->memTime
= curTick();
112 FetchUnit::removeAddrDependency(DynInstPtr inst
)
114 inst
->unsetMemAddr();
118 FetchUnit::getRequest(DynInstPtr inst
, int stage_num
, int res_idx
,
119 int slot_num
, unsigned cmd
)
121 ScheduleEntry
* sched_entry
= *inst
->curSkedEntry
;
122 CacheRequest
* cache_req
= dynamic_cast<CacheRequest
*>(reqs
[slot_num
]);
124 if (!inst
->validMemAddr()) {
125 panic("Mem. Addr. must be set before requesting cache access\n");
128 assert(sched_entry
->cmd
== InitiateFetch
);
130 DPRINTF(InOrderCachePort
,
131 "[tid:%i]: Fetch request from [sn:%i] for addr %08p\n",
132 inst
->readTid(), inst
->seqNum
, inst
->getMemAddr());
134 cache_req
->setRequest(inst
, stage_num
, id
, slot_num
,
135 sched_entry
->cmd
, MemCmd::ReadReq
,
136 inst
->curSkedEntry
->idx
);
142 FetchUnit::setupMemRequest(DynInstPtr inst
, CacheReqPtr cache_req
,
143 int acc_size
, int flags
)
145 ThreadID tid
= inst
->readTid();
146 Addr aligned_addr
= cacheBlockAlign(inst
->getMemAddr());
149 new Request(tid
, aligned_addr
, acc_size
, flags
,
150 inst
->instAddr(), cpu
->readCpuId(), tid
);
152 cache_req
->memReq
= inst
->fetchMemReq
;
155 std::list
<FetchUnit::FetchBlock
*>::iterator
156 FetchUnit::findBlock(std::list
<FetchBlock
*> &fetch_blocks
, int asid
,
159 std::list
<FetchBlock
*>::iterator fetch_it
= fetch_blocks
.begin();
160 std::list
<FetchBlock
*>::iterator end_it
= fetch_blocks
.end();
162 while (fetch_it
!= end_it
) {
163 if ((*fetch_it
)->asid
== asid
&&
164 (*fetch_it
)->addr
== block_addr
) {
174 std::list
<FetchUnit::FetchBlock
*>::iterator
175 FetchUnit::findReplacementBlock()
177 std::list
<FetchBlock
*>::iterator fetch_it
= fetchBuffer
.begin();
178 std::list
<FetchBlock
*>::iterator end_it
= fetchBuffer
.end();
180 while (fetch_it
!= end_it
) {
181 if ((*fetch_it
)->cnt
== 0) {
184 DPRINTF(InOrderCachePort
, "Block %08p has %i insts pending.\n",
185 (*fetch_it
)->addr
, (*fetch_it
)->cnt
);
194 FetchUnit::markBlockUsed(std::list
<FetchBlock
*>::iterator block_it
)
196 // Move block from whatever location it is in fetch buffer
197 // to the back (represents most-recently-used location)
198 if (block_it
!= fetchBuffer
.end()) {
199 FetchBlock
*mru_blk
= *block_it
;
200 fetchBuffer
.erase(block_it
);
201 fetchBuffer
.push_back(mru_blk
);
206 FetchUnit::execute(int slot_num
)
208 CacheReqPtr cache_req
= dynamic_cast<CacheReqPtr
>(reqs
[slot_num
]);
211 if (cachePortBlocked
) {
212 DPRINTF(InOrderCachePort
, "Cache Port Blocked. Cannot Access\n");
213 cache_req
->setCompleted(false);
217 DynInstPtr inst
= cache_req
->inst
;
218 ThreadID tid
= inst
->readTid();
219 Addr block_addr
= cacheBlockAlign(inst
->getMemAddr());
220 int asid
= cpu
->asid
[tid
];
222 inst
->fault
= NoFault
;
224 switch (cache_req
->cmd
)
228 // Check to see if we've already got this request buffered
229 // or pending to be buffered
230 bool do_fetch
= true;
231 std::list
<FetchBlock
*>::iterator pending_it
;
232 pending_it
= findBlock(pendingFetch
, asid
, block_addr
);
233 if (pending_it
!= pendingFetch
.end()) {
234 (*pending_it
)->cnt
++;
237 DPRINTF(InOrderCachePort
, "%08p is a pending fetch block "
238 "(pending:%i).\n", block_addr
,
240 } else if (pendingFetch
.size() < fetchBuffSize
) {
241 std::list
<FetchBlock
*>::iterator buff_it
;
242 buff_it
= findBlock(fetchBuffer
, asid
, block_addr
);
243 if (buff_it
!= fetchBuffer
.end()) {
247 DPRINTF(InOrderCachePort
, "%08p is in fetch buffer"
248 "(pending:%i).\n", block_addr
, (*buff_it
)->cnt
);
253 DPRINTF(InOrderCachePort
, "Inst. [sn:%i] marked to be filled "
254 "through fetch buffer.\n", inst
->seqNum
);
255 cache_req
->fetchBufferFill
= true;
256 cache_req
->setCompleted(true);
260 // Check to see if there is room in the fetchbuffer for this instruction.
261 // If not, block this request.
262 if (pendingFetch
.size() >= fetchBuffSize
) {
263 DPRINTF(InOrderCachePort
, "No room available in fetch buffer.\n");
264 cache_req
->setCompleted(false);
268 doTLBAccess(inst
, cache_req
, cacheBlkSize
, 0, TheISA::TLB::Execute
);
270 if (inst
->fault
== NoFault
) {
271 DPRINTF(InOrderCachePort
,
272 "[tid:%u]: Initiating fetch access to %s for "
273 "addr:%#x (block:%#x)\n", tid
, name(),
274 cache_req
->inst
->getMemAddr(), block_addr
);
276 cache_req
->reqData
= new uint8_t[cacheBlkSize
];
278 inst
->setCurResSlot(slot_num
);
282 if (cache_req
->isMemAccPending()) {
283 pendingFetch
.push_back(new FetchBlock(asid
, block_addr
));
291 if (cache_req
->fetchBufferFill
) {
292 // Block request if it's depending on a previous fetch, but it hasnt made it yet
293 std::list
<FetchBlock
*>::iterator fetch_it
= findBlock(fetchBuffer
, asid
, block_addr
);
294 if (fetch_it
== fetchBuffer
.end()) {
295 DPRINTF(InOrderCachePort
, "%#x not available yet\n",
297 cache_req
->setCompleted(false);
301 // Make New Instruction
302 createMachInst(fetch_it
, inst
);
303 if (inst
->traceData
) {
304 inst
->traceData
->setStaticInst(inst
->staticInst
);
305 inst
->traceData
->setPC(inst
->pcState());
308 // FetchBuffer Book-Keeping
310 assert((*fetch_it
)->cnt
>= 0);
311 markBlockUsed(fetch_it
);
317 if (cache_req
->isMemAccComplete()) {
318 if (fetchBuffer
.size() >= fetchBuffSize
) {
319 // If there is no replacement block, then we'll just have
320 // to wait till that gets cleared before satisfying the fetch
321 // for this instruction
322 std::list
<FetchBlock
*>::iterator repl_it
=
323 findReplacementBlock();
324 if (repl_it
== fetchBuffer
.end()) {
325 DPRINTF(InOrderCachePort
, "Unable to find replacement block"
326 " and complete fetch.\n");
327 cache_req
->setCompleted(false);
331 fetchBuffer
.erase(repl_it
);
334 DPRINTF(InOrderCachePort
,
335 "[tid:%i]: Completing Fetch Access for [sn:%i]\n",
338 // Make New Instruction
339 std::list
<FetchBlock
*>::iterator fetch_it
=
340 findBlock(pendingFetch
, asid
, block_addr
);
342 assert(fetch_it
!= pendingFetch
.end());
343 assert((*fetch_it
)->valid
);
345 createMachInst(fetch_it
, inst
);
346 if (inst
->traceData
) {
347 inst
->traceData
->setStaticInst(inst
->staticInst
);
348 inst
->traceData
->setPC(inst
->pcState());
352 // Update instructions waiting on new fetch block
353 FetchBlock
*new_block
= (*fetch_it
);
355 assert(new_block
->cnt
>= 0);
357 // Finally, update FetchBuffer w/Pending Block into the
359 pendingFetch
.erase(fetch_it
);
360 fetchBuffer
.push_back(new_block
);
362 DPRINTF(InOrderCachePort
, "[tid:%i]: Instruction [sn:%i] is: %s\n",
364 inst
->staticInst
->disassemble(inst
->instAddr()));
366 inst
->unsetMemAddr();
368 delete cache_req
->dataPkt
;
372 DPRINTF(InOrderCachePort
,
373 "[tid:%i]: [sn:%i]: Unable to Complete Fetch Access\n",
375 DPRINTF(InOrderStall
,
376 "STALL: [tid:%i]: Fetch miss from %08p\n",
377 tid
, cache_req
->inst
->instAddr());
378 cache_req
->setCompleted(false);
379 // NOTE: For SwitchOnCacheMiss ThreadModel, we *don't* switch on
380 // fetch miss, but we could ...
381 // cache_req->setMemStall(true);
386 fatal("Unrecognized command to %s", resName
);
391 FetchUnit::processCacheCompletion(PacketPtr pkt
)
393 // Cast to correct packet type
394 CacheReqPacket
* cache_pkt
= dynamic_cast<CacheReqPacket
*>(pkt
);
397 if (cache_pkt
->cacheReq
->isSquashed()) {
398 DPRINTF(InOrderCachePort
,
399 "Ignoring completion of squashed access, [tid:%i] [sn:%i]\n",
400 cache_pkt
->cacheReq
->getInst()->readTid(),
401 cache_pkt
->cacheReq
->getInst()->seqNum
);
403 "Ignoring completion of squashed access, [tid:%i] [sn:%i]\n",
404 cache_pkt
->cacheReq
->getTid(),
405 cache_pkt
->cacheReq
->seqNum
);
407 cache_pkt
->cacheReq
->done();
414 Addr block_addr
= cacheBlockAlign(cache_pkt
->cacheReq
->
415 getInst()->getMemAddr());
417 DPRINTF(InOrderCachePort
,
418 "[tid:%u]: [sn:%i]: Waking from fetch access to addr:%#x(phys:%#x), size:%i\n",
419 cache_pkt
->cacheReq
->getInst()->readTid(),
420 cache_pkt
->cacheReq
->getInst()->seqNum
,
421 block_addr
, cache_pkt
->getAddr(), cache_pkt
->getSize());
423 // Cast to correct request type
424 CacheRequest
*cache_req
= dynamic_cast<CacheReqPtr
>(
425 findRequest(cache_pkt
->cacheReq
->getInst(), cache_pkt
->instIdx
));
428 panic("[tid:%u]: [sn:%i]: Can't find slot for fetch access to "
429 "addr. %08p\n", cache_pkt
->cacheReq
->getInst()->readTid(),
430 cache_pkt
->cacheReq
->getInst()->seqNum
,
434 // Get resource request info
435 unsigned stage_num
= cache_req
->getStageNum();
436 DynInstPtr inst
= cache_req
->inst
;
437 ThreadID tid
= cache_req
->inst
->readTid();
438 short asid
= cpu
->asid
[tid
];
440 assert(!cache_req
->isSquashed());
441 assert(inst
->curSkedEntry
->cmd
== CompleteFetch
);
443 DPRINTF(InOrderCachePort
,
444 "[tid:%u]: [sn:%i]: Processing fetch access for block %#x\n",
445 tid
, inst
->seqNum
, block_addr
);
447 std::list
<FetchBlock
*>::iterator pend_it
= findBlock(pendingFetch
, asid
,
449 assert(pend_it
!= pendingFetch
.end());
451 // Copy Data to pendingFetch queue...
452 (*pend_it
)->block
= new uint8_t[cacheBlkSize
];
453 memcpy((*pend_it
)->block
, cache_pkt
->getPtr
<uint8_t>(), cacheBlkSize
);
454 (*pend_it
)->valid
= true;
456 cache_req
->setMemAccPending(false);
457 cache_req
->setMemAccCompleted();
459 if (cache_req
->isMemStall() &&
460 cpu
->threadModel
== InOrderCPU::SwitchOnCacheMiss
) {
461 DPRINTF(InOrderCachePort
, "[tid:%u] Waking up from Cache Miss.\n",
464 cpu
->activateContext(tid
);
466 DPRINTF(ThreadModel
, "Activating [tid:%i] after return from cache"
470 // Wake up the CPU (if it went to sleep and was waiting on this
471 // completion event).
474 DPRINTF(Activity
, "[tid:%u] Activating %s due to cache completion\n",
475 tid
, cpu
->pipelineStage
[stage_num
]->name());
477 cpu
->switchToActive(stage_num
);
481 FetchUnit::squashCacheRequest(CacheReqPtr req_ptr
)
483 DynInstPtr inst
= req_ptr
->getInst();
484 ThreadID tid
= inst
->readTid();
485 Addr block_addr
= cacheBlockAlign(inst
->getMemAddr());
486 int asid
= cpu
->asid
[tid
];
488 // Check Fetch Buffer (or pending fetch) for this block and
489 // update pending counts
490 std::list
<FetchBlock
*>::iterator buff_it
= findBlock(fetchBuffer
,
493 if (buff_it
!= fetchBuffer
.end()) {
495 DPRINTF(InOrderCachePort
, "[sn:%i] Removing Pending Fetch "
496 "for Buffer block %08p (cnt=%i)\n", inst
->seqNum
,
497 block_addr
, (*buff_it
)->cnt
);
499 std::list
<FetchBlock
*>::iterator block_it
= findBlock(pendingFetch
,
502 if (block_it
!= pendingFetch
.end()) {
504 if ((*block_it
)->cnt
== 0) {
505 DPRINTF(InOrderCachePort
, "[sn:%i] Removing Pending Fetch "
506 "for block %08p (cnt=%i)\n", inst
->seqNum
,
507 block_addr
, (*block_it
)->cnt
);
508 pendingFetch
.erase(block_it
);
513 CacheUnit::squashCacheRequest(req_ptr
);