2 * Copyright (c) 2007 MIPS Technologies, Inc.
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/cpu.hh"
42 #include "cpu/inorder/pipeline_traits.hh"
43 #include "cpu/inorder/resource_pool.hh"
44 #include "debug/Activity.hh"
45 #include "debug/AddrDep.hh"
46 #include "debug/InOrderCachePort.hh"
47 #include "debug/InOrderStall.hh"
48 #include "debug/InOrderTLB.hh"
49 #include "debug/LLSC.hh"
50 #include "debug/RefCount.hh"
51 #include "debug/ThreadModel.hh"
52 #include "mem/request.hh"
55 using namespace TheISA
;
56 using namespace ThePipeline
;
60 printMemData(uint8_t *data
, unsigned size
)
62 std::stringstream dataStr
;
63 for (unsigned pos
= 0; pos
< size
; pos
++) {
64 ccprintf(dataStr
, "%02x", data
[pos
]);
71 CacheUnit::CachePort::recvAtomic(PacketPtr pkt
)
73 panic("CacheUnit::CachePort doesn't expect recvAtomic callback!");
78 CacheUnit::CachePort::recvFunctional(PacketPtr pkt
)
80 panic("CacheUnit::CachePort doesn't expect recvFunctional callback!");
84 CacheUnit::CachePort::recvStatusChange(Status status
)
86 if (status
== RangeChange
)
89 panic("CacheUnit::CachePort doesn't expect recvStatusChange callback!");
93 CacheUnit::CachePort::recvTiming(Packet
*pkt
)
95 cachePortUnit
->processCacheCompletion(pkt
);
100 CacheUnit::CachePort::recvRetry()
102 cachePortUnit
->recvRetry();
105 CacheUnit::CacheUnit(string res_name
, int res_id
, int res_width
,
106 int res_latency
, InOrderCPU
*_cpu
, ThePipeline::Params
*params
)
107 : Resource(res_name
, res_id
, res_width
, res_latency
, _cpu
),
108 cachePortBlocked(false)
110 cachePort
= new CachePort(this);
112 // Hard-Code Selection For Now
113 if (res_name
== "icache_port")
115 else if (res_name
== "dcache_port")
118 fatal("Unrecognized TLB name passed by user");
120 for (int i
=0; i
< MaxThreads
; i
++) {
121 tlbBlocked
[i
] = false;
133 CacheUnit::getPort(const string
&if_name
, int idx
)
135 if (if_name
== resName
)
144 for (int i
= 0; i
< width
; i
++) {
145 reqs
[i
] = new CacheRequest(this);
148 // Currently Used to Model TLB Latency. Eventually
149 // Switch to Timing TLB translations.
150 resourceEvent
= new CacheUnitEvent
[width
];
152 cacheBlkSize
= this->cachePort
->peerBlockSize();
153 cacheBlkMask
= cacheBlkSize
- 1;
159 CacheUnit::getSlot(DynInstPtr inst
)
161 ThreadID tid
= inst
->readTid();
162 if (tlbBlocked
[tid
]) {
166 // For a Split-Load, the instruction would have processed once already
167 // causing the address to be unset.
168 if (!inst
->validMemAddr() && !inst
->splitInst
) {
169 panic("[tid:%i][sn:%i] Mem. Addr. must be set before requesting "
170 "cache access\n", inst
->readTid(), inst
->seqNum
);
173 int new_slot
= Resource::getSlot(inst
);
174 inst
->memTime
= curTick();
175 //@note: add back in if you want speculative loads/store capability
176 //setAddrDependency(inst);
181 CacheUnit::setAddrDependency(DynInstPtr inst
)
183 Addr req_addr
= inst
->getMemAddr();
184 ThreadID tid
= inst
->readTid();
186 addrList
[tid
].push_back(req_addr
);
187 addrMap
[tid
][req_addr
] = inst
->seqNum
;
190 "[tid:%i]: [sn:%i]: Address %08p added to dependency list (size=%i)\n",
191 inst
->readTid(), inst
->seqNum
, req_addr
, addrList
[tid
].size());
193 //@NOTE: 10 is an arbitrarily "high" number, but to be exact
194 // we would need to know the # of outstanding accesses
195 // a priori. Information like fetch width, stage width,
196 // fetch buffer, and the branch resolution stage would be
197 // useful for the icache_port. For the dcache port, the #
198 // of outstanding cache accesses (mshrs) would be a good
199 // sanity check here.
200 //assert(addrList[tid].size() < 10);
204 CacheUnit::removeAddrDependency(DynInstPtr inst
)
206 ThreadID tid
= inst
->readTid();
208 Addr mem_addr
= inst
->getMemAddr();
210 inst
->unsetMemAddr();
212 // Erase from Address List
213 std::list
<Addr
>::iterator list_it
= find(addrList
[tid
].begin(),
216 assert(list_it
!= addrList
[tid
].end() || inst
->splitInst
);
218 if (list_it
!= addrList
[tid
].end()) {
220 "[tid:%i]: [sn:%i] Address %08p removed from dependency "
221 "list\n", inst
->readTid(), inst
->seqNum
, (*list_it
));
223 addrList
[tid
].erase(list_it
);
225 // Erase From Address Map (Used for Debugging)
226 addrMap
[tid
].erase(addrMap
[tid
].find(mem_addr
));
233 CacheUnit::findRequest(DynInstPtr inst
)
235 for (int i
= 0; i
< width
; i
++) {
236 CacheRequest
* cache_req
=
237 dynamic_cast<CacheRequest
*>(reqs
[i
]);
240 if (cache_req
->valid
&&
241 cache_req
->getInst() == inst
&&
242 cache_req
->instIdx
== inst
->curSkedEntry
->idx
) {
251 CacheUnit::findRequest(DynInstPtr inst
, int idx
)
253 for (int i
= 0; i
< width
; i
++) {
254 CacheRequest
* cache_req
=
255 dynamic_cast<CacheRequest
*>(reqs
[i
]);
258 if (cache_req
->valid
&&
259 cache_req
->getInst() == inst
&&
260 cache_req
->instIdx
== idx
) {
270 CacheUnit::getRequest(DynInstPtr inst
, int stage_num
, int res_idx
,
271 int slot_num
, unsigned cmd
)
273 ScheduleEntry
* sched_entry
= *inst
->curSkedEntry
;
274 CacheRequest
* cache_req
= dynamic_cast<CacheRequest
*>(reqs
[slot_num
]);
276 if (!inst
->validMemAddr()) {
277 panic("Mem. Addr. must be set before requesting cache access\n");
280 MemCmd::Command pkt_cmd
;
282 switch (sched_entry
->cmd
)
284 case InitSecondSplitRead
:
285 pkt_cmd
= MemCmd::ReadReq
;
287 DPRINTF(InOrderCachePort
,
288 "[tid:%i]: Read request from [sn:%i] for addr %08p\n",
289 inst
->readTid(), inst
->seqNum
, inst
->split2ndAddr
);
292 case InitiateReadData
:
293 pkt_cmd
= MemCmd::ReadReq
;
295 DPRINTF(InOrderCachePort
,
296 "[tid:%i]: Read request from [sn:%i] for addr %08p\n",
297 inst
->readTid(), inst
->seqNum
, inst
->getMemAddr());
300 case InitSecondSplitWrite
:
301 pkt_cmd
= MemCmd::WriteReq
;
303 DPRINTF(InOrderCachePort
,
304 "[tid:%i]: Write request from [sn:%i] for addr %08p\n",
305 inst
->readTid(), inst
->seqNum
, inst
->split2ndAddr
);
308 case InitiateWriteData
:
309 pkt_cmd
= MemCmd::WriteReq
;
311 DPRINTF(InOrderCachePort
,
312 "[tid:%i]: Write request from [sn:%i] for addr %08p\n",
313 inst
->readTid(), inst
->seqNum
, inst
->getMemAddr());
317 panic("%i: Unexpected request type (%i) to %s", curTick(),
318 sched_entry
->cmd
, name());
321 cache_req
->setRequest(inst
, stage_num
, id
, slot_num
,
322 sched_entry
->cmd
, pkt_cmd
,
323 inst
->curSkedEntry
->idx
);
328 CacheUnit::requestAgain(DynInstPtr inst
, bool &service_request
)
330 CacheReqPtr cache_req
= dynamic_cast<CacheReqPtr
>(findRequest(inst
));
333 // Check to see if this instruction is requesting the same command
334 // or a different one
335 if (cache_req
->cmd
!= inst
->curSkedEntry
->cmd
&&
336 cache_req
->instIdx
== inst
->curSkedEntry
->idx
) {
337 // If different, then update command in the request
338 cache_req
->cmd
= inst
->curSkedEntry
->cmd
;
339 DPRINTF(InOrderCachePort
,
340 "[tid:%i]: [sn:%i]: Updating the command for this "
341 "instruction\n", inst
->readTid(), inst
->seqNum
);
343 service_request
= true;
344 } else if (inst
->curSkedEntry
->idx
!= CacheUnit::InitSecondSplitRead
&&
345 inst
->curSkedEntry
->idx
!= CacheUnit::InitSecondSplitWrite
) {
346 // If same command, just check to see if memory access was completed
347 // but dont try to re-execute
348 DPRINTF(InOrderCachePort
,
349 "[tid:%i]: [sn:%i]: requesting this resource again\n",
350 inst
->readTid(), inst
->seqNum
);
352 service_request
= true;
357 CacheUnit::setupMemRequest(DynInstPtr inst
, CacheReqPtr cache_req
,
358 int acc_size
, int flags
)
360 ThreadID tid
= inst
->readTid();
361 Addr aligned_addr
= inst
->getMemAddr();
363 if (!cache_req
->is2ndSplit()) {
364 if (cache_req
->memReq
== NULL
) {
366 new Request(cpu
->asid
[tid
], aligned_addr
, acc_size
, flags
,
367 inst
->instAddr(), cpu
->readCpuId(),
369 DPRINTF(InOrderCachePort
, "[sn:%i] Created memReq @%x, ->%x\n",
370 inst
->seqNum
, &cache_req
->memReq
, cache_req
->memReq
);
373 assert(inst
->splitInst
);
375 if (inst
->splitMemReq
== NULL
) {
376 inst
->splitMemReq
= new Request(cpu
->asid
[tid
],
385 cache_req
->memReq
= inst
->splitMemReq
;
390 CacheUnit::doTLBAccess(DynInstPtr inst
, CacheReqPtr cache_req
, int acc_size
,
391 int flags
, TheISA::TLB::Mode tlb_mode
)
393 ThreadID tid
= inst
->readTid();
394 //Addr aligned_addr = inst->getMemAddr();
395 unsigned stage_num
= cache_req
->getStageNum();
396 unsigned slot_idx
= cache_req
->getSlot();
398 setupMemRequest(inst
, cache_req
, acc_size
, flags
);
401 _tlb
->translateAtomic(cache_req
->memReq
,
402 cpu
->thread
[tid
]->getTC(), tlb_mode
);
404 if (inst
->fault
!= NoFault
) {
405 DPRINTF(InOrderTLB
, "[tid:%i]: %s encountered while translating "
406 "addr:%08p for [sn:%i].\n", tid
, inst
->fault
->name(),
407 cache_req
->memReq
->getVaddr(), inst
->seqNum
);
409 cpu
->pipelineStage
[stage_num
]->setResStall(cache_req
, tid
);
411 tlbBlocked
[tid
] = true;
413 cache_req
->tlbStall
= true;
415 // schedule a time to process the tlb miss.
416 // latency hardcoded to 1 (for now), but will be updated
417 // when timing translation gets added in
418 scheduleEvent(slot_idx
, 1);
420 DPRINTF(InOrderTLB
, "[tid:%i]: [sn:%i] virt. addr %08p translated "
421 "to phys. addr:%08p.\n", tid
, inst
->seqNum
,
422 cache_req
->memReq
->getVaddr(),
423 cache_req
->memReq
->getPaddr());
429 CacheUnit::read(DynInstPtr inst
, Addr addr
,
430 uint8_t *data
, unsigned size
, unsigned flags
)
432 CacheReqPtr cache_req
= dynamic_cast<CacheReqPtr
>(findRequest(inst
));
433 assert(cache_req
&& "Can't Find Instruction for Read!");
435 // The block size of our peer
436 unsigned blockSize
= this->cachePort
->peerBlockSize();
438 //The size of the data we're trying to read.
440 inst
->totalSize
= size
;
442 if (inst
->traceData
) {
443 inst
->traceData
->setAddr(addr
);
446 if (inst
->split2ndAccess
) {
447 size
= inst
->split2ndSize
;
448 cache_req
->splitAccess
= true;
449 cache_req
->split2ndAccess
= true;
451 DPRINTF(InOrderCachePort
, "[sn:%i] Split Read Access (2 of 2) for "
452 "(%#x, %#x).\n", inst
->seqNum
, inst
->getMemAddr(),
457 //The address of the second part of this access if it needs to be split
458 //across a cache line boundary.
459 Addr secondAddr
= roundDown(addr
+ size
- 1, blockSize
);
462 if (secondAddr
> addr
&& !inst
->split2ndAccess
) {
464 if (!inst
->splitInst
) {
465 DPRINTF(InOrderCachePort
, "%i: sn[%i] Split Read Access (1 of 2) for "
466 "(%#x, %#x).\n", curTick(), inst
->seqNum
, addr
, secondAddr
);
468 unsigned stage_num
= cache_req
->getStageNum();
469 unsigned cmd
= inst
->curSkedEntry
->cmd
;
471 // 1. Make A New Inst. Schedule w/Split Read/Complete Entered on
473 // ==============================
474 // 2. Reassign curSkedPtr to current command (InitiateRead) on new
476 // ==============================
477 inst
->splitInst
= true;
478 inst
->setBackSked(cpu
->createBackEndSked(inst
));
479 inst
->curSkedEntry
= inst
->backSked
->find(stage_num
, cmd
);
481 DPRINTF(InOrderCachePort
, "[tid:%i] [sn:%i] Retrying Split Read "
482 "Access (1 of 2) for (%#x, %#x).\n", inst
->readTid(),
483 inst
->seqNum
, addr
, secondAddr
);
486 // Save All "Total" Split Information
487 // ==============================
488 inst
->splitMemData
= new uint8_t[size
];
490 // Split Information for First Access
491 // ==============================
492 size
= secondAddr
- addr
;
493 cache_req
->splitAccess
= true;
495 // Split Information for Second Access
496 // ==============================
497 inst
->split2ndSize
= addr
+ fullSize
- secondAddr
;
498 inst
->split2ndAddr
= secondAddr
;
499 inst
->split2ndDataPtr
= inst
->splitMemData
+ size
;
500 inst
->split2ndFlags
= flags
;
503 doTLBAccess(inst
, cache_req
, size
, flags
, TheISA::TLB::Read
);
505 if (inst
->fault
== NoFault
) {
506 if (!cache_req
->splitAccess
) {
507 cache_req
->reqData
= new uint8_t[size
];
508 doCacheAccess(inst
, NULL
);
510 if (!inst
->split2ndAccess
) {
511 cache_req
->reqData
= inst
->splitMemData
;
513 cache_req
->reqData
= inst
->split2ndDataPtr
;
516 doCacheAccess(inst
, NULL
, cache_req
);
524 CacheUnit::write(DynInstPtr inst
, uint8_t *data
, unsigned size
,
525 Addr addr
, unsigned flags
, uint64_t *write_res
)
527 CacheReqPtr cache_req
= dynamic_cast<CacheReqPtr
>(findRequest(inst
));
528 assert(cache_req
&& "Can't Find Instruction for Write!");
530 // The block size of our peer
531 unsigned blockSize
= this->cachePort
->peerBlockSize();
533 //The size of the data we're trying to write.
535 inst
->totalSize
= size
;
537 if (inst
->traceData
) {
538 inst
->traceData
->setAddr(addr
);
541 if (inst
->split2ndAccess
) {
542 size
= inst
->split2ndSize
;
543 cache_req
->splitAccess
= true;
544 cache_req
->split2ndAccess
= true;
546 DPRINTF(InOrderCachePort
, "[sn:%i] Split Write Access (2 of 2) for "
547 "(%#x, %#x).\n", inst
->seqNum
, inst
->getMemAddr(),
551 //The address of the second part of this access if it needs to be split
552 //across a cache line boundary.
553 Addr secondAddr
= roundDown(addr
+ size
- 1, blockSize
);
555 if (secondAddr
> addr
&& !inst
->split2ndAccess
) {
557 DPRINTF(InOrderCachePort
, "[sn:%i] Split Write Access (1 of 2) for "
558 "(%#x, %#x).\n", inst
->seqNum
, addr
, secondAddr
);
560 // Save All "Total" Split Information
561 // ==============================
562 inst
->splitInst
= true;
564 if (!inst
->splitInstSked
) {
565 assert(0 && "Split Requests Not Supported for Now...");
567 // Schedule Split Read/Complete for Instruction
568 // ==============================
569 int stage_num
= cache_req
->getStageNum();
570 RSkedPtr inst_sked
= (stage_num
>= ThePipeline::BackEndStartStage
) ?
571 inst
->backSked
: inst
->frontSked
;
573 // this is just an arbitrarily high priority to ensure that this
574 // gets pushed to the back of the list
577 int isplit_cmd
= CacheUnit::InitSecondSplitWrite
;
579 ScheduleEntry(stage_num
,
581 cpu
->resPool
->getResIdx(DCache
),
585 int csplit_cmd
= CacheUnit::CompleteSecondSplitWrite
;
587 ScheduleEntry(stage_num
+ 1,
589 cpu
->resPool
->getResIdx(DCache
),
592 inst
->splitInstSked
= true;
594 DPRINTF(InOrderCachePort
, "[tid:%i] sn:%i] Retrying Split Read "
595 "Access (1 of 2) for (%#x, %#x).\n",
596 inst
->readTid(), inst
->seqNum
, addr
, secondAddr
);
601 // Split Information for First Access
602 // ==============================
603 size
= secondAddr
- addr
;
604 cache_req
->splitAccess
= true;
606 // Split Information for Second Access
607 // ==============================
608 inst
->split2ndSize
= addr
+ fullSize
- secondAddr
;
609 inst
->split2ndAddr
= secondAddr
;
610 inst
->split2ndFlags
= flags
;
611 inst
->splitInstSked
= true;
614 doTLBAccess(inst
, cache_req
, size
, flags
, TheISA::TLB::Write
);
616 if (inst
->fault
== NoFault
) {
617 if (!cache_req
->splitAccess
) {
618 cache_req
->reqData
= new uint8_t[size
];
619 memcpy(cache_req
->reqData
, data
, size
);
621 //inst->split2ndStoreDataPtr = cache_req->reqData;
622 //inst->split2ndStoreDataPtr += size;
624 doCacheAccess(inst
, write_res
);
626 doCacheAccess(inst
, write_res
, cache_req
);
636 CacheUnit::execute(int slot_num
)
638 CacheReqPtr cache_req
= dynamic_cast<CacheReqPtr
>(reqs
[slot_num
]);
641 if (cachePortBlocked
&&
642 (cache_req
->cmd
== InitiateReadData
||
643 cache_req
->cmd
== InitiateWriteData
||
644 cache_req
->cmd
== InitSecondSplitRead
||
645 cache_req
->cmd
== InitSecondSplitWrite
)) {
646 DPRINTF(InOrderCachePort
, "Cache Port Blocked. Cannot Access\n");
647 cache_req
->done(false);
651 DynInstPtr inst
= cache_req
->inst
;
653 ThreadID tid
= inst
->readTid();
654 std::string acc_type
= "write";
657 inst
->fault
= NoFault
;
659 switch (cache_req
->cmd
)
662 case InitiateReadData
:
666 case InitiateWriteData
:
667 if (cachePortBlocked
) {
668 DPRINTF(InOrderCachePort
, "Cache Port Blocked. Cannot Access\n");
669 cache_req
->done(false);
673 DPRINTF(InOrderCachePort
,
674 "[tid:%u]: [sn:%i] Initiating data %s access to %s for "
675 "addr. %08p\n", tid
, inst
->seqNum
, acc_type
, name(),
676 cache_req
->inst
->getMemAddr());
678 inst
->setCurResSlot(slot_num
);
680 if (inst
->isDataPrefetch() || inst
->isInstPrefetch()) {
688 case InitSecondSplitRead
:
689 DPRINTF(InOrderCachePort
,
690 "[tid:%u]: [sn:%i] Initiating split data read access to %s "
691 "for addr. %08p\n", tid
, inst
->seqNum
, name(),
692 cache_req
->inst
->split2ndAddr
);
693 inst
->split2ndAccess
= true;
694 assert(inst
->split2ndAddr
!= 0);
695 read(inst
, inst
->split2ndAddr
, &inst
->split2ndData
,
696 inst
->totalSize
, inst
->split2ndFlags
);
699 case InitSecondSplitWrite
:
700 DPRINTF(InOrderCachePort
,
701 "[tid:%u]: [sn:%i] Initiating split data write access to %s "
702 "for addr. %08p\n", tid
, inst
->seqNum
, name(),
703 cache_req
->inst
->getMemAddr());
705 inst
->split2ndAccess
= true;
706 assert(inst
->split2ndAddr
!= 0);
707 write(inst
, &inst
->split2ndData
, inst
->totalSize
,
708 inst
->split2ndAddr
, inst
->split2ndFlags
, NULL
);
711 case CompleteReadData
:
712 DPRINTF(InOrderCachePort
,
713 "[tid:%i]: [sn:%i]: Trying to Complete Data Read Access\n",
715 //@todo: timing translations need to check here...
716 assert(!inst
->isInstPrefetch() && "Can't Handle Inst. Prefecthes");
717 if (cache_req
->isMemAccComplete() || inst
->isDataPrefetch()) {
718 finishCacheUnitReq(inst
, cache_req
);
720 DPRINTF(InOrderStall
, "STALL: [tid:%i]: Data miss from %08p\n",
721 tid
, cache_req
->inst
->getMemAddr());
722 cache_req
->setCompleted(false);
723 cache_req
->setMemStall(true);
727 case CompleteWriteData
:
729 DPRINTF(InOrderCachePort
,
730 "[tid:%i]: [sn:%i]: Trying to Complete Data Write Access\n",
733 //@todo: check that timing translation is finished here
734 RequestPtr mem_req
= cache_req
->memReq
;
735 DPRINTF(InOrderCachePort
,
736 "[tid:%i]: [sn:%i]: cSwap:%i LLSC:%i isSwap:%i isCond:%i\n",
738 mem_req
->isCondSwap(),
741 inst
->isStoreConditional());
743 if (mem_req
->isCondSwap() || mem_req
->isLLSC() || mem_req
->isSwap()) {
744 DPRINTF(InOrderCachePort
, "Detected Conditional Store Inst.\n");
746 if (!cache_req
->isMemAccComplete()) {
747 DPRINTF(InOrderStall
, "STALL: [tid:%i]: Data miss from %08p\n",
748 tid
, cache_req
->inst
->getMemAddr());
749 cache_req
->setCompleted(false);
750 cache_req
->setMemStall(true);
753 DPRINTF(InOrderStall
, "Mem Acc Completed\n");
757 if (cache_req
->isMemAccPending()) {
758 DPRINTF(InOrderCachePort
, "Store Instruction Pending Completion.\n");
759 cache_req
->dataPkt
->reqData
= cache_req
->reqData
;
760 cache_req
->dataPkt
->memReq
= cache_req
->memReq
;
762 DPRINTF(InOrderCachePort
, "Store Instruction Finished Completion.\n");
764 //@todo: if split inst save data
766 finishCacheUnitReq(inst
, cache_req
);
770 case CompleteSecondSplitRead
:
771 DPRINTF(InOrderCachePort
,
772 "[tid:%i]: [sn:%i]: Trying to Complete Split Data Read "
773 "Access\n", tid
, inst
->seqNum
);
775 //@todo: check that timing translation is finished here
776 assert(!inst
->isInstPrefetch() && "Can't Handle Inst. Prefecthes");
777 if (cache_req
->isMemAccComplete() || inst
->isDataPrefetch()) {
778 finishCacheUnitReq(inst
, cache_req
);
780 DPRINTF(InOrderStall
, "STALL: [tid:%i]: Data miss from %08p\n",
781 tid
, cache_req
->inst
->split2ndAddr
);
782 cache_req
->setCompleted(false);
783 cache_req
->setMemStall(true);
787 case CompleteSecondSplitWrite
:
788 DPRINTF(InOrderCachePort
,
789 "[tid:%i]: [sn:%i]: Trying to Complete Split Data Write "
790 "Access\n", tid
, inst
->seqNum
);
791 //@todo: illegal to have a unaligned cond.swap or llsc?
792 assert(!cache_req
->memReq
->isSwap() && !cache_req
->memReq
->isCondSwap() && !cache_req
->memReq
->isLLSC());
794 if (cache_req
->isMemAccPending()) {
795 cache_req
->dataPkt
->reqData
= cache_req
->reqData
;
796 cache_req
->dataPkt
->memReq
= cache_req
->memReq
;
799 //@todo: check that timing translation is finished here
800 finishCacheUnitReq(inst
, cache_req
);
804 fatal("Unrecognized command to %s", resName
);
809 CacheUnit::finishCacheUnitReq(DynInstPtr inst
, CacheRequest
*cache_req
)
811 //@note: add back in for speculative load/store capability
812 //removeAddrDependency(inst);
813 cache_req
->setMemStall(false);
818 CacheUnit::buildDataPacket(CacheRequest
*cache_req
)
820 // Check for LL/SC and if so change command
821 if (cache_req
->memReq
->isLLSC() && cache_req
->pktCmd
== MemCmd::ReadReq
) {
822 cache_req
->pktCmd
= MemCmd::LoadLockedReq
;
825 if (cache_req
->pktCmd
== MemCmd::WriteReq
) {
827 cache_req
->memReq
->isSwap() ? MemCmd::SwapReq
:
828 (cache_req
->memReq
->isLLSC() ? MemCmd::StoreCondReq
832 cache_req
->dataPkt
= new CacheReqPacket(cache_req
,
836 DPRINTF(InOrderCachePort
, "[slot:%i]: Slot marked for %x [pkt:%x->%x]\n",
837 cache_req
->getSlot(),
838 cache_req
->dataPkt
->getAddr(),
842 cache_req
->dataPkt
->hasSlot
= true;
843 cache_req
->dataPkt
->dataStatic(cache_req
->reqData
);
847 CacheUnit::doCacheAccess(DynInstPtr inst
, uint64_t *write_res
,
848 CacheReqPtr split_req
)
850 Fault fault
= NoFault
;
852 ThreadID tid
= inst
->readTid();
854 bool do_access
= true; // flag to suppress cache access
856 // Special Handling if this is a split request
857 CacheReqPtr cache_req
;
858 if (split_req
== NULL
)
859 cache_req
= dynamic_cast<CacheReqPtr
>(reqs
[inst
->getCurResSlot()]);
861 cache_req
= split_req
;
865 // Make a new packet inside the CacheRequest object
867 buildDataPacket(cache_req
);
869 // Special Handling for LL/SC or Compare/Swap
870 bool is_write
= cache_req
->dataPkt
->isWrite();
871 RequestPtr mem_req
= cache_req
->dataPkt
->req
;
873 DPRINTF(InOrderCachePort
,
874 "[tid:%u]: [sn:%i]: Storing data: %s\n",
876 printMemData(cache_req
->dataPkt
->getPtr
<uint8_t>(),
877 cache_req
->dataPkt
->getSize()));
879 if (mem_req
->isCondSwap()) {
881 cache_req
->memReq
->setExtraData(*write_res
);
883 if (mem_req
->isLLSC()) {
884 assert(cache_req
->inst
->isStoreConditional());
885 DPRINTF(InOrderCachePort
, "Evaluating Store Conditional access\n");
886 do_access
= TheISA::handleLockedWrite(cpu
, mem_req
);
890 // Finally, go ahead and make the access if we can...
891 DPRINTF(InOrderCachePort
,
892 "[tid:%i] [sn:%i] attempting to access cache for addr %08p\n",
893 tid
, inst
->seqNum
, cache_req
->dataPkt
->getAddr());
896 if (!cachePort
->sendTiming(cache_req
->dataPkt
)) {
897 DPRINTF(InOrderCachePort
,
898 "[tid:%i] [sn:%i] cannot access cache, because port "
899 "is blocked. now waiting to retry request\n", tid
,
901 delete cache_req
->dataPkt
;
902 cache_req
->dataPkt
= NULL
;
904 delete cache_req
->memReq
;
905 cache_req
->memReq
= NULL
;
907 cache_req
->done(false);
908 cachePortBlocked
= true;
910 DPRINTF(InOrderCachePort
,
911 "[tid:%i] [sn:%i] is now waiting for cache response\n",
913 cache_req
->setCompleted();
914 cache_req
->setMemAccPending();
915 cachePortBlocked
= false;
917 } else if (mem_req
->isLLSC()){
918 // Store-Conditional instructions complete even if they "failed"
919 assert(cache_req
->inst
->isStoreConditional());
920 cache_req
->setCompleted(true);
923 "[tid:%i]: T%i Ignoring Failed Store Conditional Access\n",
926 processCacheCompletion(cache_req
->dataPkt
);
928 delete cache_req
->dataPkt
;
929 cache_req
->dataPkt
= NULL
;
931 delete cache_req
->memReq
;
932 cache_req
->memReq
= NULL
;
934 // Make cache request again since access due to
935 // inability to access
936 DPRINTF(InOrderStall
, "STALL: \n");
937 cache_req
->done(false);
943 CacheUnit::processSquash(CacheReqPacket
*cache_pkt
)
945 // The resource may no longer be actively servicing this
946 // packet. Scenarios like a store that has been sent to the
947 // memory system or access that's been squashed. If that's
948 // the case, we can't access the request slot because it
949 // will be either invalid or servicing another request.
950 if (!cache_pkt
->hasSlot
) {
951 DPRINTF(InOrderCachePort
,
952 "%x does not have a slot in unit, ignoring.\n",
953 cache_pkt
->getAddr());
955 if (cache_pkt
->reqData
) {
956 delete [] cache_pkt
->reqData
;
957 cache_pkt
->reqData
= NULL
;
960 if (cache_pkt
->memReq
) {
961 delete cache_pkt
->memReq
;
962 cache_pkt
->memReq
= NULL
;
970 DPRINTF(InOrderCachePort
, "%x has slot %i\n",
971 cache_pkt
->getAddr(), cache_pkt
->cacheReq
->getSlot());
975 // It's possible that the request is squashed but the
976 // packet is still acknowledged by the resource. Squashes
977 // should happen at the end of the cycles and trigger the
978 // code above, but if not, this would handle any timing
979 // variations due to diff. user parameters.
980 if (cache_pkt
->cacheReq
->isSquashed()) {
981 DPRINTF(InOrderCachePort
,
982 "Ignoring completion of squashed access, [tid:%i] [sn:%i]\n",
983 cache_pkt
->cacheReq
->getInst()->readTid(),
984 cache_pkt
->cacheReq
->getInst()->seqNum
);
986 cache_pkt
->cacheReq
->setMemAccPending(false);
987 cache_pkt
->cacheReq
->freeSlot();
999 CacheUnit::processCacheCompletion(PacketPtr pkt
)
1001 CacheReqPacket
* cache_pkt
= dynamic_cast<CacheReqPacket
*>(pkt
);
1004 DPRINTF(InOrderCachePort
, "Finished request for %x [pkt:%x->%x]\n",
1005 pkt
->getAddr(), &cache_pkt
, cache_pkt
);
1007 //@todo: process Squashed Completion
1008 if (processSquash(cache_pkt
))
1011 CacheRequest
*cache_req
= dynamic_cast<CacheReqPtr
>(
1012 findRequest(cache_pkt
->cacheReq
->getInst(), cache_pkt
->instIdx
));
1015 panic("[tid:%u]: [sn:%i]: Can't find slot for cache access to "
1016 "addr. %08p\n", cache_pkt
->cacheReq
->getInst()->readTid(),
1017 cache_pkt
->cacheReq
->getInst()->seqNum
,
1018 cache_pkt
->cacheReq
->getInst()->getMemAddr());
1022 assert(cache_req
== cache_pkt
->cacheReq
);
1024 DPRINTF(InOrderCachePort
,
1025 "[tid:%u]: [sn:%i]: [slot:%i] Waking from cache access (vaddr.%08p, paddr:%08p)\n",
1026 cache_pkt
->cacheReq
->getInst()->readTid(),
1027 cache_pkt
->cacheReq
->getInst()->seqNum
,
1028 cache_req
->getSlot(),
1029 cache_pkt
->req
->getVaddr(),
1030 cache_pkt
->req
->getPaddr());
1032 // Get resource request info
1033 unsigned stage_num
= cache_req
->getStageNum();
1034 DynInstPtr inst
= cache_req
->inst
;
1035 ThreadID tid
= cache_req
->inst
->readTid();
1037 assert(!cache_req
->isSquashed());
1038 assert(inst
->staticInst
&& inst
->isMemRef());
1041 DPRINTF(InOrderCachePort
,
1042 "[tid:%u]: [sn:%i]: Processing cache access\n",
1045 PacketPtr split_pkt
= NULL
;
1046 if (inst
->splitInst
) {
1047 inst
->splitFinishCnt
++;
1049 if (inst
->splitFinishCnt
== 2) {
1050 cache_req
->memReq
->setVirt(0/*inst->tid*/,
1056 split_pkt
= new Packet(cache_req
->memReq
, cache_req
->pktCmd
,
1058 split_pkt
->dataStatic(inst
->splitMemData
);
1060 DPRINTF(InOrderCachePort
, "Completing Split Access.\n");
1061 inst
->completeAcc(split_pkt
);
1064 inst
->completeAcc(cache_pkt
);
1067 inst
->setExecuted();
1069 if (inst
->isLoad()) {
1070 assert(cache_pkt
->isRead());
1072 if (cache_pkt
->req
->isLLSC()) {
1073 DPRINTF(InOrderCachePort
,
1074 "[tid:%u]: Handling Load-Linked for [sn:%u]\n",
1076 TheISA::handleLockedRead(cpu
, cache_pkt
->req
);
1079 DPRINTF(InOrderCachePort
,
1080 "[tid:%u]: [sn:%i]: Bytes loaded were: %s\n",
1082 (split_pkt
) ? printMemData(split_pkt
->getPtr
<uint8_t>(),
1083 split_pkt
->getSize()) :
1084 printMemData(cache_pkt
->getPtr
<uint8_t>(),
1085 cache_pkt
->getSize()));
1086 } else if(inst
->isStore()) {
1087 assert(cache_pkt
->isWrite());
1089 DPRINTF(InOrderCachePort
,
1090 "[tid:%u]: [sn:%i]: Bytes stored were: %s\n",
1092 (split_pkt
) ? printMemData(split_pkt
->getPtr
<uint8_t>(),
1093 split_pkt
->getSize()) :
1094 printMemData(cache_pkt
->getPtr
<uint8_t>(),
1095 cache_pkt
->getSize()));
1098 DPRINTF(InOrderCachePort
, "Deleting packets %x (%x).\n",
1099 cache_pkt
, cache_req
->dataPkt
);
1106 cache_req
->setMemAccPending(false);
1107 cache_req
->setMemAccCompleted();
1109 if (cache_req
->isMemStall() &&
1110 cpu
->threadModel
== InOrderCPU::SwitchOnCacheMiss
) {
1111 DPRINTF(InOrderCachePort
, "[tid:%u] Waking up from Cache Miss.\n",
1114 cpu
->activateContext(tid
);
1116 DPRINTF(ThreadModel
, "Activating [tid:%i] after return from cache"
1120 // Wake up the CPU (if it went to sleep and was waiting on this
1121 // completion event).
1124 DPRINTF(Activity
, "[tid:%u] Activating %s due to cache completion\n",
1125 tid
, cpu
->pipelineStage
[stage_num
]->name());
1127 cpu
->switchToActive(stage_num
);
1131 CacheUnit::recvRetry()
1133 DPRINTF(InOrderCachePort
, "Unblocking Cache Port. \n");
1135 assert(cachePortBlocked
);
1137 // Clear the cache port for use again
1138 cachePortBlocked
= false;
1143 CacheUnitEvent::CacheUnitEvent()
1148 CacheUnitEvent::process()
1150 DynInstPtr inst
= resource
->reqs
[slotIdx
]->inst
;
1151 int stage_num
= resource
->reqs
[slotIdx
]->getStageNum();
1152 ThreadID tid
= inst
->threadNumber
;
1153 CacheReqPtr req_ptr
= dynamic_cast<CacheReqPtr
>(resource
->reqs
[slotIdx
]);
1155 DPRINTF(InOrderTLB
, "Waking up from TLB Miss caused by [sn:%i].\n",
1158 CacheUnit
* tlb_res
= dynamic_cast<CacheUnit
*>(resource
);
1161 //@todo: eventually, we should do a timing translation w/
1162 // hw page table walk on tlb miss
1163 DPRINTF(Fault
, "Handling Fault %s : [sn:%i] %x\n", inst
->fault
->name(), inst
->seqNum
, inst
->getMemAddr());
1164 inst
->fault
->invoke(tlb_res
->cpu
->tcBase(tid
), inst
->staticInst
);
1166 tlb_res
->tlbBlocked
[tid
] = false;
1168 tlb_res
->cpu
->pipelineStage
[stage_num
]->
1169 unsetResStall(tlb_res
->reqs
[slotIdx
], tid
);
1171 req_ptr
->tlbStall
= false;
1173 //@todo: timing translation needs to have some type of independent
1174 // info regarding if it's squashed or not so we can
1175 // free up the resource if a request gets squashed in the middle
1177 if (req_ptr
->isSquashed()) {
1178 req_ptr
->freeSlot();
1181 tlb_res
->cpu
->wakeCPU();
1185 CacheUnit::squashDueToMemStall(DynInstPtr inst
, int stage_num
,
1186 InstSeqNum squash_seq_num
, ThreadID tid
)
1188 // If squashing due to memory stall, then we do NOT want to
1189 // squash the instruction that caused the stall so we
1190 // increment the sequence number here to prevent that.
1192 // NOTE: This is only for the SwitchOnCacheMiss Model
1193 // NOTE: If you have multiple outstanding misses from the same
1194 // thread then you need to reevaluate this code
1195 // NOTE: squash should originate from
1196 // pipeline_stage.cc:processInstSchedule
1197 DPRINTF(InOrderCachePort
, "Squashing above [sn:%u]\n",
1198 squash_seq_num
+ 1);
1200 squash(inst
, stage_num
, squash_seq_num
+ 1, tid
);
1204 CacheUnit::squashCacheRequest(CacheReqPtr req_ptr
)
1206 DynInstPtr inst
= req_ptr
->getInst();
1207 req_ptr
->setSquashed();
1208 inst
->setSquashed();
1210 //@note: add back in for speculative load/store capability
1211 /*if (inst->validMemAddr()) {
1212 DPRINTF(AddrDep, "Squash of [tid:%i] [sn:%i], attempting to "
1213 "remove addr. %08p dependencies.\n",
1216 inst->getMemAddr());
1218 removeAddrDependency(inst);
1224 CacheUnit::squash(DynInstPtr inst
, int stage_num
,
1225 InstSeqNum squash_seq_num
, ThreadID tid
)
1227 for (int i
= 0; i
< width
; i
++) {
1228 ResReqPtr req_ptr
= reqs
[i
];
1230 if (req_ptr
->valid
&&
1231 req_ptr
->getInst()->readTid() == tid
&&
1232 req_ptr
->getInst()->seqNum
> squash_seq_num
) {
1234 DPRINTF(InOrderCachePort
,
1235 "[tid:%i] Squashing request from [sn:%i]\n",
1236 req_ptr
->getInst()->readTid(), req_ptr
->getInst()->seqNum
);
1238 if (req_ptr
->isSquashed()) {
1239 DPRINTF(AddrDep
, "Request for [tid:%i] [sn:%i] already "
1240 "squashed, ignoring squash process.\n",
1241 req_ptr
->getInst()->readTid(),
1242 req_ptr
->getInst()->seqNum
);
1246 CacheReqPtr cache_req
= dynamic_cast<CacheReqPtr
>(req_ptr
);
1249 squashCacheRequest(cache_req
);
1251 int req_slot_num
= req_ptr
->getSlot();
1253 if (cache_req
->tlbStall
) {
1254 tlbBlocked
[tid
] = false;
1256 int stall_stage
= reqs
[req_slot_num
]->getStageNum();
1258 cpu
->pipelineStage
[stall_stage
]->
1259 unsetResStall(reqs
[req_slot_num
], tid
);
1262 if (cache_req
->isMemAccPending()) {
1263 cache_req
->dataPkt
->reqData
= cache_req
->reqData
;
1264 cache_req
->dataPkt
->memReq
= cache_req
->memReq
;
1267 if (!cache_req
->tlbStall
)
1268 freeSlot(req_slot_num
);
1275 CacheRequest::clearRequest()
1277 if (!memAccPending
) {
1278 if (reqData
&& !splitAccess
)
1282 DPRINTF(InOrderCachePort
, "Clearing request for %x...%x\n",
1283 memReq
->getVaddr(), (memReq
->hasPaddr()) ? memReq
->getPaddr() : 0);
1291 dataPkt
->hasSlot
= false;
1292 DPRINTF(InOrderCachePort
, "[slot:%i]: Slot unmarked for %x for [pkt:%x->%x]\n",
1293 getSlot(), dataPkt
->getAddr(), &dataPkt
, dataPkt
);
1300 memAccComplete
= false;
1301 memAccPending
= false;
1303 splitAccess
= false;
1304 splitAccessNum
= -1;
1305 split2ndAccess
= false;
1307 fetchBufferFill
= false;
1309 ResourceRequest::clearRequest();