2021-06-04 09:06:35 +08:00
|
|
|
/***************************************************************************************
|
|
|
|
* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
|
2021-07-24 23:26:38 +08:00
|
|
|
* Copyright (c) 2020-2021 Peng Cheng Laboratory
|
2021-06-04 09:06:35 +08:00
|
|
|
*
|
|
|
|
* XiangShan is licensed under Mulan PSL v2.
|
|
|
|
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
|
|
|
* You may obtain a copy of Mulan PSL v2 at:
|
|
|
|
* http://license.coscl.org.cn/MulanPSL2
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
|
|
|
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
|
|
|
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
|
|
|
*
|
|
|
|
* See the Mulan PSL v2 for more details.
|
|
|
|
***************************************************************************************/
|
|
|
|
|
2020-10-05 20:56:23 +08:00
|
|
|
package xiangshan.mem
|
|
|
|
|
2021-04-19 21:19:20 +08:00
|
|
|
import chipsalliance.rocketchip.config.Parameters
|
2020-10-05 20:56:23 +08:00
|
|
|
import chisel3._
|
|
|
|
import chisel3.util._
|
|
|
|
import xiangshan._
|
|
|
|
import utils._
|
|
|
|
import xiangshan.cache._
|
2021-04-19 21:19:20 +08:00
|
|
|
import difftest._
|
2020-10-05 20:56:23 +08:00
|
|
|
|
2021-03-01 11:58:03 +08:00
|
|
|
class SbufferFlushBundle extends Bundle {
|
|
|
|
val valid = Output(Bool())
|
|
|
|
val empty = Input(Bool())
|
|
|
|
}
|
|
|
|
|
|
|
|
trait HasSbufferConst extends HasXSParameter {
|
2021-10-20 15:48:32 +08:00
|
|
|
val EvictCycles = 1 << 20
|
|
|
|
val SbufferReplayDelayCycles = 16
|
|
|
|
require(isPow2(EvictCycles))
|
|
|
|
val EvictCountBits = log2Up(EvictCycles+1)
|
|
|
|
val MissqReplayCountBits = log2Up(SbufferReplayDelayCycles) + 1
|
2021-01-13 21:13:56 +08:00
|
|
|
|
2020-10-05 20:56:23 +08:00
|
|
|
val SbufferIndexWidth: Int = log2Up(StoreBufferSize)
|
2021-08-03 14:28:43 +08:00
|
|
|
// paddr = ptag + offset
|
2020-10-05 20:56:23 +08:00
|
|
|
val CacheLineBytes: Int = CacheLineSize / 8
|
|
|
|
val CacheLineWords: Int = CacheLineBytes / DataBytes
|
|
|
|
val OffsetWidth: Int = log2Up(CacheLineBytes)
|
2020-12-28 16:35:14 +08:00
|
|
|
val WordsWidth: Int = log2Up(CacheLineWords)
|
2021-08-03 14:28:43 +08:00
|
|
|
val PTagWidth: Int = PAddrBits - OffsetWidth
|
|
|
|
val VTagWidth: Int = VAddrBits - OffsetWidth
|
2021-03-06 15:36:27 +08:00
|
|
|
val WordOffsetWidth: Int = PAddrBits - WordsWidth
|
2020-10-05 20:56:23 +08:00
|
|
|
}
|
|
|
|
|
2021-10-20 15:48:32 +08:00
|
|
|
class SbufferEntryState (implicit p: Parameters) extends SbufferBundle {
|
|
|
|
val state_valid = Bool() // this entry is active
|
|
|
|
val state_inflight = Bool() // sbuffer is trying to write this entry to dcache
|
2021-11-29 11:34:37 +08:00
|
|
|
val w_timeout = Bool() // with timeout resp, waiting for resend store pipeline req timeout
|
|
|
|
val w_sameblock_inflight = Bool() // same cache block dcache req is inflight
|
|
|
|
val s_recheck_inflight = Bool() // recheck if same cache block dcache req is inflight
|
2021-10-20 15:48:32 +08:00
|
|
|
|
|
|
|
def isInvalid(): Bool = !state_valid
|
|
|
|
def isValid(): Bool = state_valid
|
|
|
|
def isActive(): Bool = state_valid && !state_inflight
|
|
|
|
def isInflight(): Bool = state_inflight
|
2021-11-29 11:34:37 +08:00
|
|
|
def isDcacheReqCandidate(): Bool = state_valid && !state_inflight && !w_sameblock_inflight
|
2021-10-20 15:48:32 +08:00
|
|
|
}
|
|
|
|
|
2021-04-19 21:19:20 +08:00
|
|
|
class SbufferBundle(implicit p: Parameters) extends XSBundle with HasSbufferConst
|
2020-10-05 20:56:23 +08:00
|
|
|
|
2021-04-19 21:19:20 +08:00
|
|
|
class DataWriteReq(implicit p: Parameters) extends SbufferBundle {
|
2021-03-06 15:36:27 +08:00
|
|
|
val idx = UInt(SbufferIndexWidth.W)
|
|
|
|
val mask = UInt((DataBits/8).W)
|
|
|
|
val data = UInt(DataBits.W)
|
|
|
|
val wordOffset = UInt(WordOffsetWidth.W)
|
2021-10-20 22:37:06 +08:00
|
|
|
val wline = Bool()
|
2021-03-06 15:36:27 +08:00
|
|
|
}
|
|
|
|
|
2021-04-19 21:19:20 +08:00
|
|
|
class SbufferData(implicit p: Parameters) extends XSModule with HasSbufferConst {
|
2021-03-06 15:36:27 +08:00
|
|
|
val io = IO(new Bundle(){
|
|
|
|
val writeReq = Vec(StorePipelineWidth, Flipped(ValidIO(new DataWriteReq)))
|
|
|
|
val dataOut = Output(Vec(StoreBufferSize, Vec(CacheLineWords, Vec(DataBytes, UInt(8.W)))))
|
|
|
|
})
|
|
|
|
|
|
|
|
val data = Reg(Vec(StoreBufferSize, Vec(CacheLineWords, Vec(DataBytes, UInt(8.W)))))
|
|
|
|
|
|
|
|
val req = io.writeReq
|
2020-10-06 16:23:37 +08:00
|
|
|
|
2021-03-06 15:36:27 +08:00
|
|
|
for(i <- 0 until StorePipelineWidth) {
|
|
|
|
when(req(i).valid){
|
2021-10-20 22:37:06 +08:00
|
|
|
for(word <- 0 until CacheLineWords){
|
|
|
|
for(byte <- 0 until DataBytes){
|
|
|
|
when(
|
|
|
|
req(i).bits.mask(byte) && (req(i).bits.wordOffset(WordsWidth-1, 0) === word.U) ||
|
|
|
|
req(i).bits.wline
|
|
|
|
){
|
|
|
|
data(req(i).bits.idx)(word)(byte) := req(i).bits.data(byte*8+7, byte*8)
|
|
|
|
}
|
2021-03-06 15:36:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-06 16:23:37 +08:00
|
|
|
}
|
2021-03-06 15:36:27 +08:00
|
|
|
|
|
|
|
io.dataOut := data
|
2020-10-05 20:56:23 +08:00
|
|
|
}
|
|
|
|
|
2021-12-10 09:47:25 +08:00
|
|
|
class Sbuffer(implicit p: Parameters) extends DCacheModule with HasSbufferConst with HasPerfEvents {
|
2020-10-05 20:56:23 +08:00
|
|
|
val io = IO(new Bundle() {
|
2021-11-16 16:18:48 +08:00
|
|
|
val hartId = Input(UInt(8.W))
|
2021-09-27 12:17:48 +08:00
|
|
|
val in = Vec(StorePipelineWidth, Flipped(Decoupled(new DCacheWordReqWithVaddr))) //Todo: store logic only support Width == 2 now
|
2021-10-20 15:48:32 +08:00
|
|
|
val dcache = Flipped(new DCacheToSbufferIO)
|
2020-10-05 20:56:23 +08:00
|
|
|
val forward = Vec(LoadPipelineWidth, Flipped(new LoadForwardQueryIO))
|
2021-01-19 00:38:21 +08:00
|
|
|
val sqempty = Input(Bool())
|
2021-03-01 11:58:03 +08:00
|
|
|
val flush = Flipped(new SbufferFlushBundle)
|
2021-03-04 08:59:19 +08:00
|
|
|
val csrCtrl = Flipped(new CustomCSRCtrlIO)
|
2020-10-05 20:56:23 +08:00
|
|
|
})
|
|
|
|
|
2021-03-06 15:36:27 +08:00
|
|
|
val dataModule = Module(new SbufferData)
|
|
|
|
dataModule.io.writeReq <> DontCare
|
|
|
|
val writeReq = dataModule.io.writeReq
|
|
|
|
|
2021-08-03 14:28:43 +08:00
|
|
|
val ptag = Reg(Vec(StoreBufferSize, UInt(PTagWidth.W)))
|
|
|
|
val vtag = Reg(Vec(StoreBufferSize, UInt(VTagWidth.W)))
|
2021-02-02 18:30:29 +08:00
|
|
|
val mask = Reg(Vec(StoreBufferSize, Vec(CacheLineWords, Vec(DataBytes, Bool()))))
|
2021-11-29 11:34:37 +08:00
|
|
|
val waitInflightMask = Reg(Vec(StoreBufferSize, UInt(StoreBufferSize.W)))
|
2021-03-06 15:36:27 +08:00
|
|
|
val data = dataModule.io.dataOut
|
2021-10-20 15:48:32 +08:00
|
|
|
val stateVec = RegInit(VecInit(Seq.fill(StoreBufferSize)(0.U.asTypeOf(new SbufferEntryState))))
|
|
|
|
val cohCount = RegInit(VecInit(Seq.fill(StoreBufferSize)(0.U(EvictCountBits.W))))
|
|
|
|
val missqReplayCount = RegInit(VecInit(Seq.fill(StoreBufferSize)(0.U(MissqReplayCountBits.W))))
|
2021-03-23 23:46:04 +08:00
|
|
|
|
2021-11-15 15:55:13 +08:00
|
|
|
val willSendDcacheReq = Wire(Bool())
|
|
|
|
|
2020-10-05 20:56:23 +08:00
|
|
|
/*
|
2021-08-03 21:41:19 +08:00
|
|
|
idle --[flush] --> drain --[buf empty]--> idle
|
2020-10-05 20:56:23 +08:00
|
|
|
--[buf full]--> replace --[dcache resp]--> idle
|
2021-01-13 21:13:56 +08:00
|
|
|
*/
|
2021-08-03 21:41:19 +08:00
|
|
|
// x_drain_all: drain store queue and sbuffer
|
|
|
|
// x_drain_sbuffer: drain sbuffer only, block store queue to sbuffer write
|
|
|
|
val x_idle :: x_replace :: x_drain_all :: x_drain_sbuffer :: Nil = Enum(4)
|
|
|
|
def needDrain(state: UInt): Bool =
|
|
|
|
state(1)
|
2020-10-05 20:56:23 +08:00
|
|
|
val sbuffer_state = RegInit(x_idle)
|
|
|
|
|
|
|
|
// ---------------------- Store Enq Sbuffer ---------------------
|
|
|
|
|
2021-08-03 14:28:43 +08:00
|
|
|
def getPTag(pa: UInt): UInt =
|
|
|
|
pa(PAddrBits - 1, PAddrBits - PTagWidth)
|
|
|
|
|
|
|
|
def getVTag(va: UInt): UInt =
|
|
|
|
va(VAddrBits - 1, VAddrBits - VTagWidth)
|
2020-10-05 20:56:23 +08:00
|
|
|
|
2021-01-04 19:37:37 +08:00
|
|
|
def getWord(pa: UInt): UInt =
|
|
|
|
pa(PAddrBits-1, 3)
|
|
|
|
|
2020-12-28 16:35:14 +08:00
|
|
|
def getWordOffset(pa: UInt): UInt =
|
|
|
|
pa(OffsetWidth-1, 3)
|
|
|
|
|
2021-08-03 14:28:43 +08:00
|
|
|
def getAddr(ptag: UInt): UInt =
|
|
|
|
Cat(ptag, 0.U((PAddrBits - PTagWidth).W))
|
2020-10-05 20:56:23 +08:00
|
|
|
|
2020-12-28 16:35:14 +08:00
|
|
|
def getByteOffset(offect: UInt): UInt =
|
|
|
|
Cat(offect(OffsetWidth - 1, 3), 0.U(3.W))
|
2020-10-05 20:56:23 +08:00
|
|
|
|
2020-10-07 14:43:14 +08:00
|
|
|
def isOneOf(key: UInt, seq: Seq[UInt]): Bool =
|
|
|
|
if(seq.isEmpty) false.B else Cat(seq.map(_===key)).orR()
|
2020-10-05 20:56:23 +08:00
|
|
|
|
2020-12-16 10:07:15 +08:00
|
|
|
def widthMap[T <: Data](f: Int => T) = (0 until StoreBufferSize) map f
|
2020-10-05 20:56:23 +08:00
|
|
|
|
2020-12-28 16:35:14 +08:00
|
|
|
// sbuffer entry count
|
|
|
|
|
2021-03-03 12:23:09 +08:00
|
|
|
val plru = new PseudoLRU(StoreBufferSize)
|
|
|
|
val accessIdx = Wire(Vec(StorePipelineWidth + 1, Valid(UInt(SbufferIndexWidth.W))))
|
|
|
|
|
|
|
|
val replaceIdx = plru.way
|
|
|
|
plru.access(accessIdx)
|
|
|
|
|
2021-03-04 17:16:47 +08:00
|
|
|
//-------------------------cohCount-----------------------------
|
|
|
|
// insert and merge: cohCount=0
|
|
|
|
// every cycle cohCount+=1
|
2021-10-20 15:48:32 +08:00
|
|
|
// if cohCount(EvictCountBits-1)==1, evict
|
|
|
|
val cohTimeOutMask = VecInit(widthMap(i => cohCount(i)(EvictCountBits - 1) && stateVec(i).isActive()))
|
|
|
|
val (cohTimeOutIdx, cohHasTimeOut) = PriorityEncoderWithFlag(cohTimeOutMask)
|
|
|
|
val missqReplayTimeOutMask = VecInit(widthMap(i => missqReplayCount(i)(MissqReplayCountBits - 1) && stateVec(i).w_timeout))
|
2021-11-15 15:55:13 +08:00
|
|
|
val (missqReplayTimeOutIdx, missqReplayMayHasTimeOut) = PriorityEncoderWithFlag(missqReplayTimeOutMask)
|
|
|
|
val missqReplayHasTimeOut = RegNext(missqReplayMayHasTimeOut) && !RegNext(willSendDcacheReq)
|
|
|
|
val missqReplayTimeOutIdxReg = RegEnable(missqReplayTimeOutIdx, missqReplayMayHasTimeOut)
|
2021-03-04 17:16:47 +08:00
|
|
|
|
2021-10-20 15:48:32 +08:00
|
|
|
val activeMask = VecInit(stateVec.map(s => s.isActive()))
|
|
|
|
val drainIdx = PriorityEncoder(activeMask)
|
2021-01-13 21:13:56 +08:00
|
|
|
|
2021-10-20 15:48:32 +08:00
|
|
|
val inflightMask = VecInit(stateVec.map(s => s.isInflight()))
|
2021-01-04 19:37:37 +08:00
|
|
|
|
2021-08-03 14:28:43 +08:00
|
|
|
val inptags = io.in.map(in => getPTag(in.bits.addr))
|
|
|
|
val invtags = io.in.map(in => getVTag(in.bits.vaddr))
|
|
|
|
val sameTag = inptags(0) === inptags(1)
|
2021-01-04 19:37:37 +08:00
|
|
|
val firstWord = getWord(io.in(0).bits.addr)
|
|
|
|
val secondWord = getWord(io.in(1).bits.addr)
|
2020-12-28 16:35:14 +08:00
|
|
|
val sameWord = firstWord === secondWord
|
|
|
|
|
2021-01-04 19:37:37 +08:00
|
|
|
// merge condition
|
2020-12-28 16:35:14 +08:00
|
|
|
val mergeMask = Wire(Vec(StorePipelineWidth, Vec(StoreBufferSize, Bool())))
|
|
|
|
val mergeIdx = mergeMask.map(PriorityEncoder(_))
|
|
|
|
val canMerge = mergeMask.map(ParallelOR(_))
|
2021-01-04 19:37:37 +08:00
|
|
|
|
2020-12-28 16:35:14 +08:00
|
|
|
for(i <- 0 until StorePipelineWidth){
|
|
|
|
mergeMask(i) := widthMap(j =>
|
2021-10-20 15:48:32 +08:00
|
|
|
inptags(i) === ptag(j) && activeMask(j)
|
2021-02-02 18:30:29 +08:00
|
|
|
)
|
2020-12-28 16:35:14 +08:00
|
|
|
}
|
|
|
|
|
2021-03-04 17:16:47 +08:00
|
|
|
// insert condition
|
2021-01-04 19:37:37 +08:00
|
|
|
// firstInsert: the first invalid entry
|
2021-08-03 14:28:43 +08:00
|
|
|
// if first entry canMerge or second entry has the same ptag with the first entry,
|
2021-03-04 17:16:47 +08:00
|
|
|
// secondInsert equal the first invalid entry, otherwise, the second invalid entry
|
2021-10-20 15:48:32 +08:00
|
|
|
val invalidMask = VecInit(stateVec.map(s => s.isInvalid()))
|
2021-03-03 12:23:09 +08:00
|
|
|
val evenInvalidMask = GetEvenBits(invalidMask.asUInt)
|
|
|
|
val oddInvalidMask = GetOddBits(invalidMask.asUInt)
|
2021-01-28 17:31:09 +08:00
|
|
|
|
|
|
|
val (evenRawInsertIdx, evenCanInsert) = PriorityEncoderWithFlag(evenInvalidMask)
|
|
|
|
val (oddRawInsertIdx, oddCanInsert) = PriorityEncoderWithFlag(oddInvalidMask)
|
|
|
|
val evenInsertIdx = Cat(evenRawInsertIdx, 0.U(1.W))
|
|
|
|
val oddInsertIdx = Cat(oddRawInsertIdx, 1.U(1.W))
|
|
|
|
|
|
|
|
val enbufferSelReg = RegInit(false.B)
|
|
|
|
when(io.in(0).valid) {
|
|
|
|
enbufferSelReg := ~enbufferSelReg
|
2020-12-28 16:35:14 +08:00
|
|
|
}
|
|
|
|
|
2021-01-28 17:31:09 +08:00
|
|
|
val firstInsertIdx = Mux(enbufferSelReg, evenInsertIdx, oddInsertIdx)
|
2021-09-28 09:23:31 +08:00
|
|
|
val secondInsertIdx = Mux(sameTag,
|
2021-01-28 21:34:47 +08:00
|
|
|
firstInsertIdx,
|
|
|
|
Mux(~enbufferSelReg, evenInsertIdx, oddInsertIdx)
|
|
|
|
)
|
2021-08-03 21:41:19 +08:00
|
|
|
val firstCanInsert = sbuffer_state =/= x_drain_sbuffer && Mux(enbufferSelReg, evenCanInsert, oddCanInsert)
|
|
|
|
val secondCanInsert = sbuffer_state =/= x_drain_sbuffer && Mux(sameTag,
|
2021-01-28 21:34:47 +08:00
|
|
|
firstCanInsert,
|
|
|
|
Mux(~enbufferSelReg, evenCanInsert, oddCanInsert)
|
|
|
|
)
|
2021-11-15 15:55:13 +08:00
|
|
|
val forward_need_uarch_drain = WireInit(false.B)
|
|
|
|
val merge_need_uarch_drain = WireInit(false.B)
|
|
|
|
val do_uarch_drain = RegNext(forward_need_uarch_drain) || RegNext(RegNext(merge_need_uarch_drain))
|
2021-08-03 21:41:19 +08:00
|
|
|
XSPerfAccumulate("do_uarch_drain", do_uarch_drain)
|
2020-12-28 16:35:14 +08:00
|
|
|
|
2021-02-02 00:22:00 +08:00
|
|
|
io.in(0).ready := firstCanInsert
|
|
|
|
io.in(1).ready := secondCanInsert && !sameWord && io.in(0).ready
|
2020-12-28 16:35:14 +08:00
|
|
|
|
2021-08-03 14:28:43 +08:00
|
|
|
def wordReqToBufLine(req: DCacheWordReq, reqptag: UInt, reqvtag: UInt, insertIdx: UInt, wordOffset: UInt, flushMask: Bool): Unit = {
|
2021-11-29 11:34:37 +08:00
|
|
|
val sameBlockInflightMask = genSameBlockInflightMask(reqptag)
|
2021-10-20 15:48:32 +08:00
|
|
|
stateVec(insertIdx).state_valid := true.B
|
2021-11-29 11:34:37 +08:00
|
|
|
stateVec(insertIdx).w_sameblock_inflight := sameBlockInflightMask.orR // set w_sameblock_inflight when a line is first allocated
|
|
|
|
when(sameBlockInflightMask.orR){
|
|
|
|
waitInflightMask(insertIdx) := sameBlockInflightMask
|
|
|
|
}
|
2021-01-13 21:13:56 +08:00
|
|
|
cohCount(insertIdx) := 0.U
|
2021-11-15 15:55:13 +08:00
|
|
|
// missqReplayCount(insertIdx) := 0.U
|
2021-08-03 14:28:43 +08:00
|
|
|
ptag(insertIdx) := reqptag
|
|
|
|
vtag(insertIdx) := reqvtag // update vtag iff a new sbuffer line is allocated
|
2020-12-28 16:35:14 +08:00
|
|
|
when(flushMask){
|
|
|
|
for(j <- 0 until CacheLineWords){
|
|
|
|
for(i <- 0 until DataBytes){
|
2021-02-02 18:30:29 +08:00
|
|
|
mask(insertIdx)(j)(i) := false.B
|
2020-12-28 16:35:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-05 20:56:23 +08:00
|
|
|
for(i <- 0 until DataBytes){
|
|
|
|
when(req.mask(i)){
|
2021-02-02 18:30:29 +08:00
|
|
|
mask(insertIdx)(wordOffset)(i) := true.B
|
2021-03-06 15:36:27 +08:00
|
|
|
// data(insertIdx)(wordOffset)(i) := req.data(i*8+7, i*8)
|
2020-10-05 20:56:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-15 15:55:13 +08:00
|
|
|
def mergeWordReq(req: DCacheWordReq, reqptag: UInt, reqvtag: UInt, mergeIdx: UInt, wordOffset: UInt): Unit = {
|
2021-01-13 21:13:56 +08:00
|
|
|
cohCount(mergeIdx) := 0.U
|
2021-11-15 15:55:13 +08:00
|
|
|
// missqReplayCount(mergeIdx) := 0.U
|
2020-10-05 20:56:23 +08:00
|
|
|
for(i <- 0 until DataBytes){
|
|
|
|
when(req.mask(i)){
|
2021-02-02 18:30:29 +08:00
|
|
|
mask(mergeIdx)(wordOffset)(i) := true.B
|
2021-03-06 15:36:27 +08:00
|
|
|
// data(mergeIdx)(wordOffset)(i) := req.data(i*8+7, i*8)
|
2020-10-05 20:56:23 +08:00
|
|
|
}
|
|
|
|
}
|
2021-08-03 21:41:19 +08:00
|
|
|
// check if vtag is the same, if not, trigger sbuffer flush
|
|
|
|
when(reqvtag =/= vtag(mergeIdx)) {
|
2021-09-28 09:23:31 +08:00
|
|
|
XSDebug("reqvtag =/= sbufvtag req(vtag %x ptag %x) sbuffer(vtag %x ptag %x)\n",
|
|
|
|
reqvtag << OffsetWidth,
|
2021-08-03 21:41:19 +08:00
|
|
|
reqptag << OffsetWidth,
|
2021-09-28 09:23:31 +08:00
|
|
|
vtag(mergeIdx) << OffsetWidth,
|
2021-08-03 21:41:19 +08:00
|
|
|
ptag(mergeIdx) << OffsetWidth
|
|
|
|
)
|
2021-11-15 15:55:13 +08:00
|
|
|
merge_need_uarch_drain := true.B
|
2021-08-03 21:41:19 +08:00
|
|
|
}
|
2020-10-05 20:56:23 +08:00
|
|
|
}
|
|
|
|
|
2021-03-04 17:16:47 +08:00
|
|
|
for(((in, wordOffset), i) <- io.in.zip(Seq(firstWord, secondWord)).zipWithIndex){
|
2021-03-06 15:36:27 +08:00
|
|
|
writeReq(i).valid := in.fire()
|
|
|
|
writeReq(i).bits.wordOffset := wordOffset
|
|
|
|
writeReq(i).bits.mask := in.bits.mask
|
|
|
|
writeReq(i).bits.data := in.bits.data
|
2021-10-20 22:37:06 +08:00
|
|
|
writeReq(i).bits.wline := in.bits.wline
|
2021-03-05 17:19:39 +08:00
|
|
|
val insertIdx = if(i == 0) firstInsertIdx else secondInsertIdx
|
|
|
|
val flushMask = if(i == 0) true.B else !sameTag
|
|
|
|
accessIdx(i).valid := RegNext(in.fire())
|
|
|
|
accessIdx(i).bits := RegNext(Mux(canMerge(i), mergeIdx(i), insertIdx))
|
2021-03-04 17:16:47 +08:00
|
|
|
when(in.fire()){
|
|
|
|
when(canMerge(i)){
|
2021-03-06 15:36:27 +08:00
|
|
|
writeReq(i).bits.idx := mergeIdx(i)
|
2021-08-03 21:41:19 +08:00
|
|
|
mergeWordReq(in.bits, inptags(i), invtags(i), mergeIdx(i), wordOffset)
|
2021-03-04 17:16:47 +08:00
|
|
|
XSDebug(p"merge req $i to line [${mergeIdx(i)}]\n")
|
|
|
|
}.otherwise({
|
2021-03-06 15:36:27 +08:00
|
|
|
writeReq(i).bits.idx := insertIdx
|
2021-08-03 14:28:43 +08:00
|
|
|
wordReqToBufLine(in.bits, inptags(i), invtags(i), insertIdx, wordOffset, flushMask)
|
2021-03-04 17:16:47 +08:00
|
|
|
XSDebug(p"insert req $i to line[$insertIdx]\n")
|
|
|
|
})
|
2020-10-05 20:56:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 12:23:09 +08:00
|
|
|
|
2020-10-06 16:23:37 +08:00
|
|
|
for(i <- 0 until StoreBufferSize){
|
2021-10-20 15:48:32 +08:00
|
|
|
XSDebug(stateVec(i).isValid(),
|
|
|
|
p"[$i] timeout:${cohCount(i)(EvictCountBits-1)} state:${stateVec(i)}\n"
|
2020-10-06 21:15:39 +08:00
|
|
|
)
|
2020-10-06 16:23:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for((req, i) <- io.in.zipWithIndex){
|
|
|
|
XSDebug(req.fire(),
|
|
|
|
p"accept req [$i]: " +
|
|
|
|
p"addr:${Hexadecimal(req.bits.addr)} " +
|
2020-12-16 14:44:10 +08:00
|
|
|
p"mask:${Binary(req.bits.mask)} " +
|
|
|
|
p"data:${Hexadecimal(req.bits.data)}\n"
|
2020-10-06 21:15:39 +08:00
|
|
|
)
|
|
|
|
XSDebug(req.valid && !req.ready,
|
|
|
|
p"req [$i] blocked by sbuffer\n"
|
2020-10-06 16:23:37 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-10-05 20:56:23 +08:00
|
|
|
// ---------------------- Send Dcache Req ---------------------
|
|
|
|
|
2021-08-03 21:41:19 +08:00
|
|
|
val sbuffer_empty = Cat(invalidMask).andR()
|
|
|
|
val sq_empty = !Cat(io.in.map(_.valid)).orR()
|
|
|
|
val empty = sbuffer_empty && sq_empty
|
2021-03-05 17:19:39 +08:00
|
|
|
val threshold = RegNext(io.csrCtrl.sbuffer_threshold +& 1.U)
|
2021-10-20 15:48:32 +08:00
|
|
|
val validCount = PopCount(activeMask)
|
2021-09-01 15:04:17 +08:00
|
|
|
val do_eviction = RegNext(validCount >= threshold || validCount === (StoreBufferSize-1).U, init = false.B)
|
|
|
|
require((StoreBufferThreshold + 1) <= StoreBufferSize)
|
2020-12-28 16:35:14 +08:00
|
|
|
|
2021-03-05 17:19:39 +08:00
|
|
|
XSDebug(p"validCount[$validCount]\n")
|
2020-10-06 21:15:39 +08:00
|
|
|
|
2021-02-02 00:20:06 +08:00
|
|
|
io.flush.empty := RegNext(empty && io.sqempty)
|
2021-08-03 21:41:19 +08:00
|
|
|
// lru.io.flush := sbuffer_state === x_drain_all && empty
|
2020-10-05 20:56:23 +08:00
|
|
|
switch(sbuffer_state){
|
|
|
|
is(x_idle){
|
|
|
|
when(io.flush.valid){
|
2021-08-03 21:41:19 +08:00
|
|
|
sbuffer_state := x_drain_all
|
|
|
|
}.elsewhen(do_uarch_drain){
|
2020-10-05 20:56:23 +08:00
|
|
|
sbuffer_state := x_drain_sbuffer
|
2020-10-06 21:15:39 +08:00
|
|
|
}.elsewhen(do_eviction){
|
2020-10-05 20:56:23 +08:00
|
|
|
sbuffer_state := x_replace
|
|
|
|
}
|
|
|
|
}
|
2021-08-03 21:41:19 +08:00
|
|
|
is(x_drain_all){
|
2020-10-05 20:56:23 +08:00
|
|
|
when(empty){
|
|
|
|
sbuffer_state := x_idle
|
|
|
|
}
|
|
|
|
}
|
2021-08-03 21:41:19 +08:00
|
|
|
is(x_drain_sbuffer){
|
2021-11-29 11:34:37 +08:00
|
|
|
when(io.flush.valid){
|
|
|
|
sbuffer_state := x_drain_all
|
|
|
|
}.elsewhen(sbuffer_empty){
|
2021-08-03 21:41:19 +08:00
|
|
|
sbuffer_state := x_idle
|
|
|
|
}
|
|
|
|
}
|
2020-10-05 20:56:23 +08:00
|
|
|
is(x_replace){
|
|
|
|
when(io.flush.valid){
|
2021-08-03 21:41:19 +08:00
|
|
|
sbuffer_state := x_drain_all
|
|
|
|
}.elsewhen(do_uarch_drain){
|
2020-10-05 20:56:23 +08:00
|
|
|
sbuffer_state := x_drain_sbuffer
|
2020-10-06 21:15:39 +08:00
|
|
|
}.elsewhen(!do_eviction){
|
2020-10-05 20:56:23 +08:00
|
|
|
sbuffer_state := x_idle
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-06 21:15:39 +08:00
|
|
|
XSDebug(p"sbuffer state:${sbuffer_state} do eviction:${do_eviction} empty:${empty}\n")
|
2020-10-05 20:56:23 +08:00
|
|
|
|
2020-10-07 14:43:14 +08:00
|
|
|
def noSameBlockInflight(idx: UInt): Bool = {
|
2021-03-04 17:16:47 +08:00
|
|
|
// stateVec(idx) itself must not be s_inflight
|
2021-08-03 14:28:43 +08:00
|
|
|
!Cat(widthMap(i => inflightMask(i) && ptag(idx) === ptag(i))).orR()
|
2020-10-07 14:43:14 +08:00
|
|
|
}
|
|
|
|
|
2021-11-29 11:34:37 +08:00
|
|
|
def genSameBlockInflightMask(ptag_in: UInt): UInt = {
|
|
|
|
val mask = VecInit(widthMap(i => inflightMask(i) && ptag_in === ptag(i))).asUInt // quite slow, use it with care
|
|
|
|
assert(!(PopCount(mask) > 1.U))
|
|
|
|
mask
|
|
|
|
}
|
|
|
|
|
|
|
|
def haveSameBlockInflight(ptag_in: UInt): Bool = {
|
|
|
|
genSameBlockInflightMask(ptag_in).orR
|
|
|
|
}
|
|
|
|
|
2021-08-03 21:41:19 +08:00
|
|
|
val need_drain = needDrain(sbuffer_state)
|
2021-03-04 17:16:47 +08:00
|
|
|
val need_replace = do_eviction || (sbuffer_state === x_replace)
|
2021-10-20 15:48:32 +08:00
|
|
|
val evictionIdx = Mux(missqReplayHasTimeOut,
|
2021-11-15 15:55:13 +08:00
|
|
|
missqReplayTimeOutIdxReg,
|
2021-10-20 15:48:32 +08:00
|
|
|
Mux(need_drain,
|
|
|
|
drainIdx,
|
|
|
|
Mux(cohHasTimeOut, cohTimeOutIdx, replaceIdx)
|
|
|
|
)
|
2021-03-04 17:16:47 +08:00
|
|
|
)
|
2021-10-20 15:48:32 +08:00
|
|
|
|
2020-10-07 14:43:14 +08:00
|
|
|
/*
|
2021-08-03 14:28:43 +08:00
|
|
|
If there is a inflight dcache req which has same ptag with evictionIdx's ptag,
|
2020-10-07 14:43:14 +08:00
|
|
|
current eviction should be blocked.
|
|
|
|
*/
|
2021-10-20 15:48:32 +08:00
|
|
|
val prepareValid = missqReplayHasTimeOut ||
|
2021-11-29 11:34:37 +08:00
|
|
|
stateVec(evictionIdx).isDcacheReqCandidate() && (need_drain || cohHasTimeOut || need_replace)
|
|
|
|
assert(!(stateVec(evictionIdx).isDcacheReqCandidate && !noSameBlockInflight(evictionIdx)))
|
2021-03-04 17:16:47 +08:00
|
|
|
val prepareValidReg = RegInit(false.B)
|
2021-10-20 15:48:32 +08:00
|
|
|
// when canSendDcacheReq, send dcache req stored in pipeline reg to dcache
|
2021-03-04 17:16:47 +08:00
|
|
|
val canSendDcacheReq = io.dcache.req.ready || !prepareValidReg
|
2021-10-20 15:48:32 +08:00
|
|
|
// when willSendDcacheReq, read dcache req data and store them in a pipeline reg
|
2021-11-15 15:55:13 +08:00
|
|
|
willSendDcacheReq := prepareValid && canSendDcacheReq
|
2021-01-05 20:30:25 +08:00
|
|
|
when(io.dcache.req.fire()){
|
2021-03-04 17:16:47 +08:00
|
|
|
prepareValidReg := false.B
|
2021-01-28 14:47:27 +08:00
|
|
|
}
|
2021-03-04 17:16:47 +08:00
|
|
|
when(canSendDcacheReq){
|
|
|
|
prepareValidReg := prepareValid
|
2021-01-05 20:30:25 +08:00
|
|
|
}
|
2021-03-04 17:16:47 +08:00
|
|
|
when(willSendDcacheReq){
|
2021-10-20 15:48:32 +08:00
|
|
|
stateVec(evictionIdx).state_inflight := true.B
|
|
|
|
stateVec(evictionIdx).w_timeout := false.B
|
|
|
|
// stateVec(evictionIdx).s_pipe_req := true.B
|
2021-03-04 17:16:47 +08:00
|
|
|
XSDebug(p"$evictionIdx will be sent to Dcache\n")
|
|
|
|
}
|
2021-10-20 15:48:32 +08:00
|
|
|
XSDebug(p"need drain:$need_drain cohHasTimeOut: $cohHasTimeOut need replace:$need_replace\n")
|
|
|
|
XSDebug(p"drainIdx:$drainIdx tIdx:$cohTimeOutIdx replIdx:$replaceIdx " +
|
|
|
|
p"blocked:${!noSameBlockInflight(evictionIdx)} v:${activeMask(evictionIdx)}\n")
|
2021-03-04 17:16:47 +08:00
|
|
|
XSDebug(p"prepareValid:$prepareValid evictIdx:$evictionIdx dcache ready:${io.dcache.req.ready}\n")
|
|
|
|
// Note: if other dcache req in the same block are inflight,
|
2021-10-20 15:48:32 +08:00
|
|
|
// the lru update may not accurate
|
2021-03-04 17:16:47 +08:00
|
|
|
accessIdx(StorePipelineWidth).valid := invalidMask(replaceIdx) || (
|
2021-10-20 15:48:32 +08:00
|
|
|
need_replace && !need_drain && !cohHasTimeOut && !missqReplayHasTimeOut && canSendDcacheReq && activeMask(replaceIdx))
|
2021-03-04 17:16:47 +08:00
|
|
|
accessIdx(StorePipelineWidth).bits := replaceIdx
|
|
|
|
val evictionIdxReg = RegEnable(evictionIdx, enable = willSendDcacheReq)
|
2021-09-27 12:17:48 +08:00
|
|
|
val evictionPTag = RegEnable(ptag(evictionIdx), enable = willSendDcacheReq)
|
|
|
|
val evictionVTag = RegEnable(vtag(evictionIdx), enable = willSendDcacheReq)
|
2021-03-04 17:16:47 +08:00
|
|
|
|
|
|
|
io.dcache.req.valid := prepareValidReg
|
2021-10-20 15:48:32 +08:00
|
|
|
io.dcache.req.bits := DontCare
|
|
|
|
io.dcache.req.bits.cmd := MemoryOpConstants.M_XWR
|
|
|
|
io.dcache.req.bits.addr := getAddr(evictionPTag)
|
|
|
|
io.dcache.req.bits.vaddr := getAddr(evictionVTag)
|
|
|
|
io.dcache.req.bits.data := data(evictionIdxReg).asUInt
|
|
|
|
io.dcache.req.bits.mask := mask(evictionIdxReg).asUInt
|
2021-03-04 17:16:47 +08:00
|
|
|
io.dcache.req.bits.id := evictionIdxReg
|
2020-10-05 20:56:23 +08:00
|
|
|
|
2021-10-20 15:48:32 +08:00
|
|
|
when (io.dcache.req.fire()) {
|
|
|
|
assert(!(io.dcache.req.bits.vaddr === 0.U))
|
|
|
|
assert(!(io.dcache.req.bits.addr === 0.U))
|
|
|
|
}
|
|
|
|
|
2020-10-06 16:23:37 +08:00
|
|
|
XSDebug(io.dcache.req.fire(),
|
2021-03-04 17:16:47 +08:00
|
|
|
p"send buf [$evictionIdxReg] to Dcache, req fire\n"
|
2020-10-06 16:23:37 +08:00
|
|
|
)
|
|
|
|
|
2021-10-20 15:48:32 +08:00
|
|
|
// update sbuffer status according to dcache resp source
|
|
|
|
|
2021-11-29 11:34:37 +08:00
|
|
|
def id_to_sbuffer_id(id: UInt): UInt = {
|
|
|
|
require(id.getWidth >= log2Up(StoreBufferSize))
|
|
|
|
id(log2Up(StoreBufferSize)-1, 0)
|
|
|
|
}
|
|
|
|
|
2021-10-20 15:48:32 +08:00
|
|
|
// hit resp
|
|
|
|
io.dcache.hit_resps.map(resp => {
|
2021-11-29 11:34:37 +08:00
|
|
|
val dcache_resp_id = resp.bits.id
|
2021-10-20 15:48:32 +08:00
|
|
|
when (resp.fire()) {
|
|
|
|
stateVec(dcache_resp_id).state_inflight := false.B
|
|
|
|
stateVec(dcache_resp_id).state_valid := false.B
|
|
|
|
assert(!resp.bits.replay)
|
|
|
|
assert(!resp.bits.miss) // not need to resp if miss, to be opted
|
|
|
|
assert(stateVec(dcache_resp_id).state_inflight === true.B)
|
|
|
|
}
|
2021-11-29 11:34:37 +08:00
|
|
|
|
|
|
|
// Update w_sameblock_inflight flag is delayed for 1 cycle
|
|
|
|
//
|
|
|
|
// When a new req allocate a new line in sbuffer, sameblock_inflight check will ignore
|
|
|
|
// current dcache.hit_resps. Then, in the next cycle, we have plenty of time to check
|
|
|
|
// if the same block is still inflight
|
|
|
|
(0 until StoreBufferSize).map(i => {
|
|
|
|
when(
|
|
|
|
stateVec(i).w_sameblock_inflight &&
|
|
|
|
stateVec(i).state_valid &&
|
|
|
|
RegNext(resp.fire()) &&
|
|
|
|
waitInflightMask(i) === UIntToOH(RegNext(id_to_sbuffer_id(dcache_resp_id)))
|
|
|
|
){
|
|
|
|
stateVec(i).w_sameblock_inflight := false.B
|
|
|
|
}
|
|
|
|
})
|
2021-10-20 15:48:32 +08:00
|
|
|
})
|
2020-10-05 20:56:23 +08:00
|
|
|
|
2021-11-29 11:34:37 +08:00
|
|
|
|
2021-10-20 15:48:32 +08:00
|
|
|
// replay resp
|
|
|
|
val replay_resp_id = io.dcache.replay_resp.bits.id
|
|
|
|
when (io.dcache.replay_resp.fire()) {
|
|
|
|
missqReplayCount(replay_resp_id) := 0.U
|
|
|
|
stateVec(replay_resp_id).w_timeout := true.B
|
|
|
|
// waiting for timeout
|
|
|
|
assert(io.dcache.replay_resp.bits.replay)
|
|
|
|
assert(stateVec(replay_resp_id).state_inflight === true.B)
|
2021-01-25 16:56:32 +08:00
|
|
|
}
|
2021-10-20 15:48:32 +08:00
|
|
|
|
|
|
|
// TODO: reuse cohCount
|
|
|
|
(0 until StoreBufferSize).map(i => {
|
|
|
|
when(stateVec(i).w_timeout && stateVec(i).state_inflight && !missqReplayCount(i)(MissqReplayCountBits-1)) {
|
|
|
|
missqReplayCount(i) := missqReplayCount(i) + 1.U
|
|
|
|
}
|
|
|
|
when(activeMask(i) && !cohTimeOutMask(i)){
|
2021-01-13 21:13:56 +08:00
|
|
|
cohCount(i) := cohCount(i)+1.U
|
|
|
|
}
|
2021-10-20 15:48:32 +08:00
|
|
|
})
|
|
|
|
|
2021-11-11 10:03:16 +08:00
|
|
|
if (env.EnableDifftest) {
|
2021-10-20 15:48:32 +08:00
|
|
|
// hit resp
|
|
|
|
io.dcache.hit_resps.zipWithIndex.map{case (resp, index) => {
|
|
|
|
val difftest = Module(new DifftestSbufferEvent)
|
|
|
|
val dcache_resp_id = resp.bits.id
|
|
|
|
difftest.io.clock := clock
|
2021-11-16 16:18:48 +08:00
|
|
|
difftest.io.coreid := io.hartId
|
2021-10-20 15:48:32 +08:00
|
|
|
difftest.io.index := index.U
|
|
|
|
difftest.io.sbufferResp := RegNext(resp.fire())
|
|
|
|
difftest.io.sbufferAddr := RegNext(getAddr(ptag(dcache_resp_id)))
|
|
|
|
difftest.io.sbufferData := RegNext(data(dcache_resp_id).asTypeOf(Vec(CacheLineBytes, UInt(8.W))))
|
|
|
|
difftest.io.sbufferMask := RegNext(mask(dcache_resp_id).asUInt)
|
|
|
|
}}
|
2021-01-13 21:13:56 +08:00
|
|
|
}
|
|
|
|
|
2020-10-05 20:56:23 +08:00
|
|
|
// ---------------------- Load Data Forward ---------------------
|
2021-08-03 14:28:43 +08:00
|
|
|
val mismatch = Wire(Vec(LoadPipelineWidth, Bool()))
|
2021-08-03 21:41:19 +08:00
|
|
|
XSPerfAccumulate("vaddr_match_failed", mismatch(0) || mismatch(1))
|
2020-12-16 14:44:10 +08:00
|
|
|
for ((forward, i) <- io.forward.zipWithIndex) {
|
2021-08-03 14:28:43 +08:00
|
|
|
val vtag_matches = VecInit(widthMap(w => vtag(w) === getVTag(forward.vaddr)))
|
|
|
|
val ptag_matches = VecInit(widthMap(w => ptag(w) === getPTag(forward.paddr)))
|
2021-08-03 23:10:27 +08:00
|
|
|
val tag_matches = vtag_matches
|
2021-09-28 09:23:31 +08:00
|
|
|
val tag_mismatch = RegNext(forward.valid) && VecInit(widthMap(w =>
|
2021-10-20 15:48:32 +08:00
|
|
|
RegNext(vtag_matches(w)) =/= RegNext(ptag_matches(w)) && RegNext((activeMask(w) || inflightMask(w)))
|
2021-08-03 14:28:43 +08:00
|
|
|
)).asUInt.orR
|
|
|
|
mismatch(i) := tag_mismatch
|
|
|
|
when (tag_mismatch) {
|
2021-09-28 09:23:31 +08:00
|
|
|
XSDebug("forward tag mismatch: pmatch %x vmatch %x vaddr %x paddr %x\n",
|
|
|
|
RegNext(ptag_matches.asUInt),
|
2021-08-03 21:41:19 +08:00
|
|
|
RegNext(vtag_matches.asUInt),
|
|
|
|
RegNext(forward.vaddr),
|
|
|
|
RegNext(forward.paddr)
|
|
|
|
)
|
2021-11-15 15:55:13 +08:00
|
|
|
forward_need_uarch_drain := true.B
|
2021-08-03 14:28:43 +08:00
|
|
|
}
|
2021-10-20 15:48:32 +08:00
|
|
|
val valid_tag_matches = widthMap(w => tag_matches(w) && activeMask(w))
|
2021-03-04 17:16:47 +08:00
|
|
|
val inflight_tag_matches = widthMap(w => tag_matches(w) && inflightMask(w))
|
2020-12-16 14:44:10 +08:00
|
|
|
val line_offset_mask = UIntToOH(getWordOffset(forward.paddr))
|
2020-10-05 20:56:23 +08:00
|
|
|
|
2020-12-16 14:44:10 +08:00
|
|
|
val valid_tag_match_reg = valid_tag_matches.map(RegNext(_))
|
|
|
|
val inflight_tag_match_reg = inflight_tag_matches.map(RegNext(_))
|
|
|
|
val line_offset_reg = RegNext(line_offset_mask)
|
2021-11-29 11:34:37 +08:00
|
|
|
val forward_mask_candidate_reg = RegEnable(
|
|
|
|
VecInit(mask.map(entry => entry(getWordOffset(forward.paddr)))),
|
|
|
|
forward.valid
|
|
|
|
)
|
2021-11-15 15:55:13 +08:00
|
|
|
val forward_data_candidate_reg = RegEnable(
|
|
|
|
VecInit(data.map(entry => entry(getWordOffset(forward.paddr)))),
|
|
|
|
forward.valid
|
|
|
|
)
|
2020-10-05 20:56:23 +08:00
|
|
|
|
2021-11-29 11:34:37 +08:00
|
|
|
val selectedValidMask = Mux1H(valid_tag_match_reg, forward_mask_candidate_reg)
|
2021-11-15 15:55:13 +08:00
|
|
|
val selectedValidData = Mux1H(valid_tag_match_reg, forward_data_candidate_reg)
|
2021-11-29 11:34:37 +08:00
|
|
|
selectedValidMask.suggestName("selectedValidMask_"+i)
|
2021-11-15 15:55:13 +08:00
|
|
|
selectedValidData.suggestName("selectedValidData_"+i)
|
2020-10-05 20:56:23 +08:00
|
|
|
|
2021-11-29 11:34:37 +08:00
|
|
|
val selectedInflightMask = Mux1H(inflight_tag_match_reg, forward_mask_candidate_reg)
|
2021-11-15 15:55:13 +08:00
|
|
|
val selectedInflightData = Mux1H(inflight_tag_match_reg, forward_data_candidate_reg)
|
2021-11-29 11:34:37 +08:00
|
|
|
selectedInflightMask.suggestName("selectedInflightMask_"+i)
|
2021-11-15 15:55:13 +08:00
|
|
|
selectedInflightData.suggestName("selectedInflightData_"+i)
|
2020-10-06 16:23:37 +08:00
|
|
|
|
2021-11-29 11:34:37 +08:00
|
|
|
// currently not being used
|
2021-08-20 01:27:12 +08:00
|
|
|
val selectedInflightMaskFast = Mux1H(line_offset_mask, Mux1H(inflight_tag_matches, mask).asTypeOf(Vec(CacheLineWords, Vec(DataBytes, Bool()))))
|
|
|
|
val selectedValidMaskFast = Mux1H(line_offset_mask, Mux1H(valid_tag_matches, mask).asTypeOf(Vec(CacheLineWords, Vec(DataBytes, Bool()))))
|
|
|
|
|
2021-04-30 10:40:51 +08:00
|
|
|
forward.dataInvalid := false.B // data in store line merge buffer is always ready
|
2021-08-03 14:28:43 +08:00
|
|
|
forward.matchInvalid := tag_mismatch // paddr / vaddr cam result does not match
|
2020-12-16 14:44:10 +08:00
|
|
|
for (j <- 0 until DataBytes) {
|
|
|
|
forward.forwardMask(j) := false.B
|
|
|
|
forward.forwardData(j) := DontCare
|
2020-10-06 16:23:37 +08:00
|
|
|
|
2020-12-16 14:44:10 +08:00
|
|
|
// valid entries have higher priority than inflight entries
|
2021-01-02 23:56:29 +08:00
|
|
|
when(selectedInflightMask(j)) {
|
2020-12-16 14:44:10 +08:00
|
|
|
forward.forwardMask(j) := true.B
|
|
|
|
forward.forwardData(j) := selectedInflightData(j)
|
|
|
|
}
|
2021-01-02 23:56:29 +08:00
|
|
|
when(selectedValidMask(j)) {
|
2020-12-16 14:44:10 +08:00
|
|
|
forward.forwardMask(j) := true.B
|
|
|
|
forward.forwardData(j) := selectedValidData(j)
|
|
|
|
}
|
2021-08-20 01:27:12 +08:00
|
|
|
|
|
|
|
forward.forwardMaskFast(j) := selectedInflightMaskFast(j) || selectedValidMaskFast(j)
|
2020-12-16 14:44:10 +08:00
|
|
|
}
|
2020-12-28 16:35:14 +08:00
|
|
|
}
|
2021-08-03 14:28:43 +08:00
|
|
|
|
|
|
|
for (i <- 0 until StoreBufferSize) {
|
2021-11-15 15:55:13 +08:00
|
|
|
XSDebug("sbf entry " + i + " : ptag %x vtag %x valid %x active %x inflight %x w_timeout %x\n",
|
2021-08-03 14:28:43 +08:00
|
|
|
ptag(i) << OffsetWidth,
|
|
|
|
vtag(i) << OffsetWidth,
|
2021-10-20 15:48:32 +08:00
|
|
|
stateVec(i).isValid(),
|
|
|
|
activeMask(i),
|
|
|
|
inflightMask(i),
|
|
|
|
stateVec(i).w_timeout
|
2021-08-03 14:28:43 +08:00
|
|
|
)
|
|
|
|
}
|
2021-09-02 13:54:49 +08:00
|
|
|
|
2021-10-20 15:48:32 +08:00
|
|
|
val perf_valid_entry_count = PopCount(VecInit(stateVec.map(s => !s.isInvalid())).asUInt)
|
2021-09-01 14:33:26 +08:00
|
|
|
XSPerfHistogram("util", perf_valid_entry_count, true.B, 0, StoreBufferSize, 1)
|
|
|
|
XSPerfAccumulate("sbuffer_req_valid", PopCount(VecInit(io.in.map(_.valid)).asUInt))
|
|
|
|
XSPerfAccumulate("sbuffer_req_fire", PopCount(VecInit(io.in.map(_.fire())).asUInt))
|
|
|
|
XSPerfAccumulate("sbuffer_merge", PopCount(VecInit(io.in.zipWithIndex.map({case (in, i) => in.fire() && canMerge(i)})).asUInt))
|
|
|
|
XSPerfAccumulate("sbuffer_newline", PopCount(VecInit(io.in.zipWithIndex.map({case (in, i) => in.fire() && !canMerge(i)})).asUInt))
|
|
|
|
XSPerfAccumulate("dcache_req_valid", io.dcache.req.valid)
|
|
|
|
XSPerfAccumulate("dcache_req_fire", io.dcache.req.fire())
|
|
|
|
XSPerfAccumulate("sbuffer_idle", sbuffer_state === x_idle)
|
|
|
|
XSPerfAccumulate("sbuffer_flush", sbuffer_state === x_drain_sbuffer)
|
|
|
|
XSPerfAccumulate("sbuffer_replace", sbuffer_state === x_replace)
|
|
|
|
XSPerfAccumulate("evenCanInsert", evenCanInsert)
|
|
|
|
XSPerfAccumulate("oddCanInsert", oddCanInsert)
|
2021-11-15 15:55:13 +08:00
|
|
|
XSPerfAccumulate("mainpipe_resp_valid", io.dcache.main_pipe_hit_resp.fire())
|
|
|
|
XSPerfAccumulate("refill_resp_valid", io.dcache.refill_hit_resp.fire())
|
|
|
|
XSPerfAccumulate("replay_resp_valid", io.dcache.replay_resp.fire())
|
|
|
|
XSPerfAccumulate("coh_timeout", cohHasTimeOut)
|
|
|
|
|
|
|
|
// val (store_latency_sample, store_latency) = TransactionLatencyCounter(io.lsu.req.fire(), io.lsu.resp.fire())
|
|
|
|
// XSPerfHistogram("store_latency", store_latency, store_latency_sample, 0, 100, 10)
|
|
|
|
// XSPerfAccumulate("store_req", io.lsu.req.fire())
|
2021-10-23 13:38:45 +08:00
|
|
|
|
|
|
|
val perfEvents = Seq(
|
|
|
|
("sbuffer_req_valid ", PopCount(VecInit(io.in.map(_.valid)).asUInt) ),
|
|
|
|
("sbuffer_req_fire ", PopCount(VecInit(io.in.map(_.fire())).asUInt) ),
|
|
|
|
("sbuffer_merge ", PopCount(VecInit(io.in.zipWithIndex.map({case (in, i) => in.fire() && canMerge(i)})).asUInt) ),
|
|
|
|
("sbuffer_newline ", PopCount(VecInit(io.in.zipWithIndex.map({case (in, i) => in.fire() && !canMerge(i)})).asUInt) ),
|
|
|
|
("dcache_req_valid ", io.dcache.req.valid ),
|
|
|
|
("dcache_req_fire ", io.dcache.req.fire() ),
|
2021-11-15 15:55:13 +08:00
|
|
|
("sbuffer_idle ", sbuffer_state === x_idle ),
|
|
|
|
("sbuffer_flush ", sbuffer_state === x_drain_sbuffer ),
|
|
|
|
("sbuffer_replace ", sbuffer_state === x_replace ),
|
|
|
|
("mpipe_resp_valid ", io.dcache.main_pipe_hit_resp.fire() ),
|
|
|
|
("refill_resp_valid ", io.dcache.refill_hit_resp.fire() ),
|
|
|
|
("replay_resp_valid ", io.dcache.replay_resp.fire() ),
|
|
|
|
("coh_timeout ", cohHasTimeOut ),
|
2021-12-10 09:47:25 +08:00
|
|
|
("sbuffer_1_4_valid ", (perf_valid_entry_count < (StoreBufferSize.U/4.U)) ),
|
|
|
|
("sbuffer_2_4_valid ", (perf_valid_entry_count > (StoreBufferSize.U/4.U)) & (perf_valid_entry_count <= (StoreBufferSize.U/2.U)) ),
|
|
|
|
("sbuffer_3_4_valid ", (perf_valid_entry_count > (StoreBufferSize.U/2.U)) & (perf_valid_entry_count <= (StoreBufferSize.U*3.U/4.U))),
|
2021-10-23 13:38:45 +08:00
|
|
|
("sbuffer_full_valid", (perf_valid_entry_count > (StoreBufferSize.U*3.U/4.U)))
|
|
|
|
)
|
2021-12-10 09:47:25 +08:00
|
|
|
generatePerfEvent()
|
2021-10-23 13:38:45 +08:00
|
|
|
|
2020-10-05 20:56:23 +08:00
|
|
|
}
|