XiangShan/src/main/scala/xiangshan/mem/pipeline/LoadUnit.scala

543 lines
23 KiB
Scala
Raw Normal View History

/***************************************************************************************
* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
* Copyright (c) 2020-2021 Peng Cheng Laboratory
*
* 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-08-06 16:58:13 +08:00
package xiangshan.mem
import chipsalliance.rocketchip.config.Parameters
2020-08-06 16:58:13 +08:00
import chisel3._
import chisel3.util._
import utils._
import xiangshan._
import xiangshan.backend.decode.ImmUnion
import xiangshan.backend.fu.PMPRespBundle
import xiangshan.cache._
import xiangshan.cache.mmu.{TLB, TlbCmd, TlbPtwIO, TlbReq, TlbRequestIO, TlbResp}
2020-08-06 16:58:13 +08:00
class LoadToLsqIO(implicit p: Parameters) extends XSBundle {
2020-08-06 16:58:13 +08:00
val loadIn = ValidIO(new LsPipelineBundle)
val ldout = Flipped(DecoupledIO(new ExuOutput))
val loadDataForwarded = Output(Bool())
2021-02-05 23:10:00 +08:00
val needReplayFromRS = Output(Bool())
val forward = new PipeLoadForwardQueryIO
2020-08-06 16:58:13 +08:00
}
class LoadToLoadIO(implicit p: Parameters) extends XSBundle {
// load to load fast path is limited to ld (64 bit) used as vaddr src1 only
val data = UInt(XLEN.W)
val valid = Bool()
}
2020-10-17 21:05:46 +08:00
// Load Pipeline Stage 0
// Generate addr, use addr to query DCache and DTLB
class LoadUnit_S0(implicit p: Parameters) extends XSModule with HasDCacheParameters{
2020-08-06 16:58:13 +08:00
val io = IO(new Bundle() {
2020-10-17 21:05:46 +08:00
val in = Flipped(Decoupled(new ExuInput))
val out = Decoupled(new LsPipelineBundle)
val fastpath = Input(Vec(LoadPipelineWidth, new LoadToLoadIO))
val dtlbReq = DecoupledIO(new TlbReq)
val dcacheReq = DecoupledIO(new DCacheWordReq)
val rsIdx = Input(UInt(log2Up(IssQueSize).W))
val isFirstIssue = Input(Bool())
val loadFastMatch = Input(UInt(exuParameters.LduCnt.W))
2020-08-06 16:58:13 +08:00
})
require(LoadPipelineWidth == exuParameters.LduCnt)
2020-08-06 16:58:13 +08:00
2020-10-17 21:05:46 +08:00
val s0_uop = io.in.bits.uop
val imm12 = WireInit(s0_uop.ctrl.imm(11,0))
// slow vaddr from non-load insts
val slowpath_vaddr = io.in.bits.src(0) + SignExt(s0_uop.ctrl.imm(11,0), VAddrBits)
val slowpath_mask = genWmask(slowpath_vaddr, s0_uop.ctrl.fuOpType(1,0))
// fast vaddr from load insts
val fastpath_vaddrs = WireInit(VecInit(List.tabulate(LoadPipelineWidth)(i => {
io.fastpath(i).data + SignExt(s0_uop.ctrl.imm(11,0), VAddrBits)
})))
val fastpath_masks = WireInit(VecInit(List.tabulate(LoadPipelineWidth)(i => {
genWmask(fastpath_vaddrs(i), s0_uop.ctrl.fuOpType(1,0))
})))
val fastpath_vaddr = Mux1H(io.loadFastMatch, fastpath_vaddrs)
val fastpath_mask = Mux1H(io.loadFastMatch, fastpath_masks)
// select vaddr from 2 alus
val s0_vaddr = Mux(io.loadFastMatch.orR, fastpath_vaddr, slowpath_vaddr)
val s0_mask = Mux(io.loadFastMatch.orR, fastpath_mask, slowpath_mask)
XSPerfAccumulate("load_to_load_forward", io.loadFastMatch.orR && io.in.fire())
2020-10-17 21:05:46 +08:00
val isSoftPrefetch = Wire(Bool())
isSoftPrefetch := s0_uop.ctrl.isORI //it's a ORI but it exists in ldu, which means it's a softprefecth
val isSoftPrefetchRead = Wire(Bool())
val isSoftPrefetchWrite = Wire(Bool())
isSoftPrefetchRead := s0_uop.ctrl.isSoftPrefetchRead
isSoftPrefetchWrite := s0_uop.ctrl.isSoftPrefetchWrite
2020-10-17 21:05:46 +08:00
// query DTLB
2020-12-11 19:59:25 +08:00
io.dtlbReq.valid := io.in.valid
io.dtlbReq.bits.vaddr := s0_vaddr
io.dtlbReq.bits.cmd := TlbCmd.read
io.dtlbReq.bits.size := LSUOpType.size(io.in.bits.uop.ctrl.fuOpType)
io.dtlbReq.bits.robIdx := s0_uop.robIdx
io.dtlbReq.bits.debug.pc := s0_uop.cf.pc
io.dtlbReq.bits.debug.isFirstIssue := io.isFirstIssue
2020-10-17 21:05:46 +08:00
// query DCache
2020-12-11 19:59:25 +08:00
io.dcacheReq.valid := io.in.valid
when (isSoftPrefetchRead) {
io.dcacheReq.bits.cmd := MemoryOpConstants.M_PFR
}.elsewhen (isSoftPrefetchWrite) {
io.dcacheReq.bits.cmd := MemoryOpConstants.M_PFW
}.otherwise {
io.dcacheReq.bits.cmd := MemoryOpConstants.M_XRD
}
io.dcacheReq.bits.addr := s0_vaddr
io.dcacheReq.bits.mask := s0_mask
io.dcacheReq.bits.data := DontCare
when(isSoftPrefetch) {
io.dcacheReq.bits.instrtype := SOFT_PREFETCH.U
}.otherwise {
io.dcacheReq.bits.instrtype := LOAD_SOURCE.U
}
// TODO: update cache meta
io.dcacheReq.bits.id := DontCare
2020-10-17 21:05:46 +08:00
val addrAligned = LookupTree(s0_uop.ctrl.fuOpType(1, 0), List(
"b00".U -> true.B, //b
"b01".U -> (s0_vaddr(0) === 0.U), //h
"b10".U -> (s0_vaddr(1, 0) === 0.U), //w
"b11".U -> (s0_vaddr(2, 0) === 0.U) //d
))
2020-10-17 21:05:46 +08:00
io.out.valid := io.in.valid && io.dcacheReq.ready
2020-12-11 19:59:25 +08:00
2020-10-17 21:05:46 +08:00
io.out.bits := DontCare
io.out.bits.vaddr := s0_vaddr
io.out.bits.mask := s0_mask
io.out.bits.uop := s0_uop
io.out.bits.uop.cf.exceptionVec(loadAddrMisaligned) := !addrAligned
io.out.bits.rsIdx := io.rsIdx
io.out.bits.isFirstIssue := io.isFirstIssue
io.out.bits.isSoftPrefetch := isSoftPrefetch
2020-10-17 21:05:46 +08:00
2020-12-11 19:59:25 +08:00
io.in.ready := !io.in.valid || (io.out.ready && io.dcacheReq.ready)
2020-10-27 18:11:11 +08:00
2020-12-11 19:59:25 +08:00
XSDebug(io.dcacheReq.fire(),
2020-12-13 21:31:00 +08:00
p"[DCACHE LOAD REQ] pc ${Hexadecimal(s0_uop.cf.pc)}, vaddr ${Hexadecimal(s0_vaddr)}\n"
2020-10-27 18:11:11 +08:00
)
XSPerfAccumulate("in_valid", io.in.valid)
XSPerfAccumulate("in_fire", io.in.fire)
XSPerfAccumulate("in_fire_first_issue", io.in.valid && io.isFirstIssue)
XSPerfAccumulate("stall_out", io.out.valid && !io.out.ready && io.dcacheReq.ready)
XSPerfAccumulate("stall_dcache", io.out.valid && io.out.ready && !io.dcacheReq.ready)
XSPerfAccumulate("addr_spec_success", io.out.fire() && s0_vaddr(VAddrBits-1, 12) === io.in.bits.src(0)(VAddrBits-1, 12))
XSPerfAccumulate("addr_spec_failed", io.out.fire() && s0_vaddr(VAddrBits-1, 12) =/= io.in.bits.src(0)(VAddrBits-1, 12))
XSPerfAccumulate("addr_spec_success_once", io.out.fire() && s0_vaddr(VAddrBits-1, 12) === io.in.bits.src(0)(VAddrBits-1, 12) && io.isFirstIssue)
XSPerfAccumulate("addr_spec_failed_once", io.out.fire() && s0_vaddr(VAddrBits-1, 12) =/= io.in.bits.src(0)(VAddrBits-1, 12) && io.isFirstIssue)
2020-10-17 21:05:46 +08:00
}
// Load Pipeline Stage 1
// TLB resp (send paddr to dcache)
class LoadUnit_S1(implicit p: Parameters) extends XSModule {
2020-10-17 21:05:46 +08:00
val io = IO(new Bundle() {
val in = Flipped(Decoupled(new LsPipelineBundle))
val out = Decoupled(new LsPipelineBundle)
2020-12-13 21:31:00 +08:00
val dtlbResp = Flipped(DecoupledIO(new TlbResp))
val dcachePAddr = Output(UInt(PAddrBits.W))
val dcacheKill = Output(Bool())
val dcacheBankConflict = Input(Bool())
val fullForwardFast = Output(Bool())
2020-11-02 19:23:04 +08:00
val sbuffer = new LoadForwardQueryIO
val lsq = new PipeLoadForwardQueryIO
val rsFeedback = ValidIO(new RSFeedback)
2020-08-06 16:58:13 +08:00
})
2020-08-16 15:59:15 +08:00
val isSoftPrefetch = io.in.bits.isSoftPrefetch
val actually_execpt = io.dtlbResp.bits.excp.pf.ld || io.dtlbResp.bits.excp.af.ld || io.out.bits.uop.cf.exceptionVec(loadAddrMisaligned)
val actually_mmio = !io.dtlbResp.bits.miss && io.dtlbResp.bits.mmio
val softprefecth_mmio = isSoftPrefetch && actually_mmio //TODO, fix it
val softprefecth_excep = isSoftPrefetch && actually_execpt //TODO, fix it
2020-10-17 21:05:46 +08:00
val s1_uop = io.in.bits.uop
2020-12-13 21:31:00 +08:00
val s1_paddr = io.dtlbResp.bits.paddr
val s1_exception = selectLoad(io.out.bits.uop.cf.exceptionVec, false).asUInt.orR // af & pf exception were modified below.
2020-12-13 21:31:00 +08:00
val s1_tlb_miss = io.dtlbResp.bits.miss
//val s1_mmio = !s1_tlb_miss && io.dtlbResp.bits.mmio
val s1_mmio = !isSoftPrefetch && actually_mmio
2020-11-02 19:23:04 +08:00
val s1_mask = io.in.bits.mask
val s1_bank_conflict = io.dcacheBankConflict
2020-11-18 20:47:14 +08:00
2020-11-02 19:23:04 +08:00
io.out.bits := io.in.bits // forwardXX field will be updated in s1
2020-12-13 21:31:00 +08:00
io.dtlbResp.ready := true.B
2021-01-10 12:20:47 +08:00
// TOOD: PMA check
2020-12-13 21:31:00 +08:00
io.dcachePAddr := s1_paddr
//io.dcacheKill := s1_tlb_miss || s1_exception || s1_mmio
io.dcacheKill := s1_tlb_miss || actually_mmio || actually_execpt
2020-11-02 19:23:04 +08:00
// load forward query datapath
io.sbuffer.valid := io.in.valid && !(s1_exception || s1_tlb_miss)
io.sbuffer.vaddr := io.in.bits.vaddr
2020-11-02 19:23:04 +08:00
io.sbuffer.paddr := s1_paddr
io.sbuffer.uop := s1_uop
io.sbuffer.sqIdx := s1_uop.sqIdx
io.sbuffer.mask := s1_mask
io.sbuffer.pc := s1_uop.cf.pc // FIXME: remove it
2020-11-18 20:47:14 +08:00
io.lsq.valid := io.in.valid && !(s1_exception || s1_tlb_miss)
io.lsq.vaddr := io.in.bits.vaddr
2020-11-18 20:47:14 +08:00
io.lsq.paddr := s1_paddr
io.lsq.uop := s1_uop
io.lsq.sqIdx := s1_uop.sqIdx
io.lsq.sqIdxMask := DontCare // will be overwritten by sqIdxMask pre-generated in s0
2020-11-18 20:47:14 +08:00
io.lsq.mask := s1_mask
io.lsq.pc := s1_uop.cf.pc // FIXME: remove it
2020-11-02 19:23:04 +08:00
// Generate forwardMaskFast to wake up insts earlier
val forwardMaskFast = io.lsq.forwardMaskFast.asUInt | io.sbuffer.forwardMaskFast.asUInt
io.fullForwardFast := (~forwardMaskFast & s1_mask) === 0.U
// Generate feedback signal caused by dcache bank conflict
io.rsFeedback.valid := io.in.valid && s1_bank_conflict
io.rsFeedback.bits.hit := false.B // we have found s1_bank_conflict
io.rsFeedback.bits.rsIdx := io.in.bits.rsIdx
io.rsFeedback.bits.flushState := io.in.bits.ptwBack
io.rsFeedback.bits.sourceType := RSFeedbackType.bankConflict
io.rsFeedback.bits.dataInvalidSqIdx := DontCare
io.out.valid := io.in.valid && !s1_bank_conflict // if bank conflict, load inst will be canceled immediately
2020-10-17 21:05:46 +08:00
io.out.bits.paddr := s1_paddr
2021-01-10 12:20:47 +08:00
io.out.bits.mmio := s1_mmio && !s1_exception
io.out.bits.tlbMiss := s1_tlb_miss
// current ori test will cause the case of ldest == 0, below will be modifeid in the future.
// af & pf exception were modified
io.out.bits.uop.cf.exceptionVec(loadPageFault) := !isSoftPrefetch && io.dtlbResp.bits.excp.pf.ld
io.out.bits.uop.cf.exceptionVec(loadAccessFault) := !isSoftPrefetch && io.dtlbResp.bits.excp.af.ld
io.out.bits.ptwBack := io.dtlbResp.bits.ptwBack
io.out.bits.rsIdx := io.in.bits.rsIdx
2020-08-06 16:58:13 +08:00
// soft prefetch stuff
io.out.bits.isSoftPrefetch := io.in.bits.isSoftPrefetch
io.out.bits.isSoftPreExcept := softprefecth_excep
io.out.bits.isSoftPremmio := softprefecth_mmio
2020-12-11 19:59:25 +08:00
io.in.ready := !io.in.valid || io.out.ready
XSPerfAccumulate("in_valid", io.in.valid)
XSPerfAccumulate("in_fire", io.in.fire)
XSPerfAccumulate("in_fire_first_issue", io.in.fire && io.in.bits.isFirstIssue)
XSPerfAccumulate("tlb_miss", io.in.fire && s1_tlb_miss)
XSPerfAccumulate("tlb_miss_first_issue", io.in.fire && s1_tlb_miss && io.in.bits.isFirstIssue)
XSPerfAccumulate("stall_out", io.out.valid && !io.out.ready)
2020-10-17 21:05:46 +08:00
}
2020-10-17 21:05:46 +08:00
// Load Pipeline Stage 2
// DCache resp
class LoadUnit_S2(implicit p: Parameters) extends XSModule with HasLoadHelper {
2020-10-17 21:05:46 +08:00
val io = IO(new Bundle() {
val in = Flipped(Decoupled(new LsPipelineBundle))
val out = Decoupled(new LsPipelineBundle)
val rsFeedback = ValidIO(new RSFeedback)
val dcacheResp = Flipped(DecoupledIO(new DCacheWordResp))
val pmpResp = Input(new PMPRespBundle())
val lsq = new LoadForwardQueryIO
val dataInvalidSqIdx = Input(UInt())
val sbuffer = new LoadForwardQueryIO
val dataForwarded = Output(Bool())
2021-02-05 23:10:00 +08:00
val needReplayFromRS = Output(Bool())
val fastpath = Output(new LoadToLoadIO)
val dcache_kill = Output(Bool())
2020-10-17 21:05:46 +08:00
})
val excep = WireInit(io.in.bits.uop.cf.exceptionVec)
excep(loadAccessFault) := io.in.bits.uop.cf.exceptionVec(loadAccessFault) || io.pmpResp.ld
val s2_exception = selectLoad(excep, false).asUInt.orR
2020-10-17 21:05:46 +08:00
val s2_uop = io.in.bits.uop
val s2_mask = io.in.bits.mask
val s2_paddr = io.in.bits.paddr
val s2_tlb_miss = io.in.bits.tlbMiss
val s2_data_invalid = io.lsq.dataInvalid
val s2_mmio = io.in.bits.mmio && !s2_exception
val s2_cache_miss = io.dcacheResp.bits.miss
val s2_cache_replay = io.dcacheResp.bits.replay
val s2_cache_miss_enter = io.dcacheResp.bits.miss_enter //missReq enter the mshr successfully
val isSoftPreExcept = io.in.bits.isSoftPreExcept
val isSoftPremmio = io.in.bits.isSoftPremmio
// val cnt = RegInit(127.U)
// cnt := cnt + io.in.valid.asUInt
// val s2_forward_fail = io.lsq.matchInvalid || io.sbuffer.matchInvalid || cnt === 0.U
val s2_forward_fail = io.lsq.matchInvalid || io.sbuffer.matchInvalid
2021-08-16 15:23:32 +08:00
// assert(!s2_forward_fail)
io.dcache_kill := io.in.valid && io.pmpResp.ld
io.dcacheResp.ready := true.B
2021-01-10 12:20:47 +08:00
val dcacheShouldResp = !(s2_tlb_miss || s2_exception || s2_mmio)
assert(!(io.in.valid && (dcacheShouldResp && !io.dcacheResp.valid) && (!isSoftPreExcept) && (!isSoftPremmio)), "DCache response got lost")
2020-08-06 16:58:13 +08:00
// merge forward result
// lsq has higher priority than sbuffer
val forwardMask = Wire(Vec(8, Bool()))
val forwardData = Wire(Vec(8, UInt(8.W)))
val fullForward = (~forwardMask.asUInt & s2_mask) === 0.U && !io.lsq.dataInvalid
io.lsq := DontCare
io.sbuffer := DontCare
// generate XLEN/8 Muxs
for (i <- 0 until XLEN / 8) {
forwardMask(i) := io.lsq.forwardMask(i) || io.sbuffer.forwardMask(i)
forwardData(i) := Mux(io.lsq.forwardMask(i), io.lsq.forwardData(i), io.sbuffer.forwardData(i))
}
2020-08-06 16:58:13 +08:00
XSDebug(io.out.fire(), "[FWD LOAD RESP] pc %x fwd %x(%b) + %x(%b)\n",
s2_uop.cf.pc,
io.lsq.forwardData.asUInt, io.lsq.forwardMask.asUInt,
io.in.bits.forwardData.asUInt, io.in.bits.forwardMask.asUInt
)
2020-08-06 16:58:13 +08:00
// data merge
val rdataVec = VecInit((0 until XLEN / 8).map(j =>
Mux(forwardMask(j), forwardData(j), io.dcacheResp.bits.data(8*(j+1)-1, 8*j))))
val rdata = rdataVec.asUInt
2020-10-17 21:05:46 +08:00
val rdataSel = LookupTree(s2_paddr(2, 0), List(
2020-08-06 16:58:13 +08:00
"b000".U -> rdata(63, 0),
"b001".U -> rdata(63, 8),
"b010".U -> rdata(63, 16),
"b011".U -> rdata(63, 24),
"b100".U -> rdata(63, 32),
"b101".U -> rdata(63, 40),
"b110".U -> rdata(63, 48),
"b111".U -> rdata(63, 56)
))
2020-12-12 23:48:12 +08:00
val rdataPartialLoad = rdataHelper(s2_uop, rdataSel)
2020-08-06 16:58:13 +08:00
2021-08-17 20:59:08 +08:00
io.out.valid := io.in.valid && !s2_tlb_miss && !s2_data_invalid
2020-11-18 20:47:14 +08:00
// Inst will be canceled in store queue / lsq,
// so we do not need to care about flush in load / store unit's out.valid
2020-10-17 21:05:46 +08:00
io.out.bits := io.in.bits
io.out.bits.data := rdataPartialLoad
// when exception occurs, set it to not miss and let it write back to rob (via int port)
if (EnableFastForward) {
when(io.in.bits.isSoftPrefetch) {
io.out.bits.miss := s2_cache_miss && !s2_exception && !s2_forward_fail && !fullForward && !s2_cache_miss_enter && !isSoftPreExcept && !isSoftPremmio
}.otherwise {
io.out.bits.miss := s2_cache_miss && !s2_exception && !s2_forward_fail && !fullForward
}
} else {
when(io.in.bits.isSoftPrefetch) {
io.out.bits.miss := s2_cache_miss && !s2_exception && !s2_forward_fail && !s2_cache_miss_enter && !isSoftPreExcept && !isSoftPremmio
}.otherwise {
io.out.bits.miss := s2_cache_miss && !s2_exception && !s2_forward_fail
}
}
io.out.bits.uop.ctrl.fpWen := io.in.bits.uop.ctrl.fpWen && !s2_exception
// if forward fail, replay this inst
io.out.bits.uop.ctrl.replayInst := s2_forward_fail && !s2_mmio
io.out.bits.mmio := s2_mmio
io.out.bits.uop.cf.exceptionVec := excep
// For timing reasons, sometimes we can not let
// io.out.bits.miss := s2_cache_miss && !s2_exception && !fullForward
// We use io.dataForwarded instead. It means forward logic have prepared all data needed,
// and dcache query is no longer needed.
// Such inst will be writebacked from load queue.
io.dataForwarded := s2_cache_miss && fullForward && !s2_exception && !s2_forward_fail
// io.out.bits.forwardX will be send to lq
io.out.bits.forwardMask := forwardMask
// data retbrived from dcache is also included in io.out.bits.forwardData
io.out.bits.forwardData := rdataVec
2020-10-17 21:05:46 +08:00
io.in.ready := io.out.ready || !io.in.valid
2021-08-20 18:17:28 +08:00
// feedback tlb result to RS
io.rsFeedback.valid := io.in.valid
when (io.in.bits.isSoftPrefetch) {
io.rsFeedback.bits.hit := (!s2_tlb_miss && (!s2_cache_replay || s2_mmio || s2_exception || fullForward) && !s2_data_invalid) || s2_cache_miss_enter || isSoftPreExcept || isSoftPremmio
}.otherwise {
io.rsFeedback.bits.hit := !s2_tlb_miss && (!s2_cache_replay || s2_mmio || s2_exception || fullForward) && !s2_data_invalid
}
2021-08-20 18:17:28 +08:00
io.rsFeedback.bits.rsIdx := io.in.bits.rsIdx
io.rsFeedback.bits.flushState := io.in.bits.ptwBack
io.rsFeedback.bits.sourceType := Mux(s2_tlb_miss, RSFeedbackType.tlbMiss,
Mux(io.lsq.dataInvalid,
RSFeedbackType.dataInvalid,
RSFeedbackType.mshrFull
)
)
io.rsFeedback.bits.dataInvalidSqIdx.value := io.dataInvalidSqIdx
io.rsFeedback.bits.dataInvalidSqIdx.flag := DontCare
2021-08-20 18:17:28 +08:00
// s2_cache_replay is quite slow to generate, send it separately to LQ
io.needReplayFromRS := s2_cache_replay && !fullForward
2021-08-20 18:17:28 +08:00
// fast load to load forward
io.fastpath.valid := io.in.valid // for debug only
io.fastpath.data := rdata // raw data
2021-09-02 13:54:49 +08:00
2020-11-18 20:47:14 +08:00
XSDebug(io.out.fire(), "[DCACHE LOAD RESP] pc %x rdata %x <- D$ %x + fwd %x(%b)\n",
s2_uop.cf.pc, rdataPartialLoad, io.dcacheResp.bits.data,
forwardData.asUInt, forwardMask.asUInt
2020-08-06 16:58:13 +08:00
)
2021-03-08 22:31:59 +08:00
XSPerfAccumulate("in_valid", io.in.valid)
XSPerfAccumulate("in_fire", io.in.fire)
XSPerfAccumulate("in_fire_first_issue", io.in.fire && io.in.bits.isFirstIssue)
XSPerfAccumulate("dcache_miss", io.in.fire && s2_cache_miss)
XSPerfAccumulate("dcache_miss_first_issue", io.in.fire && s2_cache_miss && io.in.bits.isFirstIssue)
XSPerfAccumulate("full_forward", io.in.valid && fullForward)
XSPerfAccumulate("dcache_miss_full_forward", io.in.valid && s2_cache_miss && fullForward)
XSPerfAccumulate("replay", io.rsFeedback.valid && !io.rsFeedback.bits.hit)
XSPerfAccumulate("replay_tlb_miss", io.rsFeedback.valid && !io.rsFeedback.bits.hit && s2_tlb_miss)
XSPerfAccumulate("replay_cache", io.rsFeedback.valid && !io.rsFeedback.bits.hit && !s2_tlb_miss && s2_cache_replay)
XSPerfAccumulate("stall_out", io.out.valid && !io.out.ready)
2020-10-17 21:05:46 +08:00
}
2020-08-06 16:58:13 +08:00
class LoadUnit(implicit p: Parameters) extends XSModule with HasLoadHelper {
2020-10-17 21:05:46 +08:00
val io = IO(new Bundle() {
val ldin = Flipped(Decoupled(new ExuInput))
val ldout = Decoupled(new ExuOutput)
val redirect = Flipped(ValidIO(new Redirect))
val feedbackSlow = ValidIO(new RSFeedback)
val feedbackFast = ValidIO(new RSFeedback)
val rsIdx = Input(UInt(log2Up(IssQueSize).W))
val isFirstIssue = Input(Bool())
val dcache = new DCacheLoadIO
2020-10-17 21:05:46 +08:00
val sbuffer = new LoadForwardQueryIO
2020-11-18 20:47:14 +08:00
val lsq = new LoadToLsqIO
val fastUop = ValidIO(new MicroOp) // early wakeup signal generated in load_s1
l0tlb: add a new level tlb, a load tlb and a store tlb (#961) * Revert "Revert "l0tlb: add a new level tlb to each mem pipeline (#936)" (#945)" This reverts commit b052b97230d6fdeedaf4e4905092adef6e768b4f. * fu: remove unused import * mmu.tlb: 2 load/store pipeline has 1 dtlb * mmu: remove btlb, the l1-tlb * mmu: set split-tlb to 32 to check perf effect * mmu: wrap tlb's param with TLBParameters * mmu: add params 'useBTlb' dtlb size is small: normal 8, super 2 * mmu.tlb: add Bundle TlbEntry, simplify tlb hit logic(coding) * mmu.tlb: seperate tlb's storage, relative hit/sfence logic tlb now supports full-associate, set-associate, directive-associate. more: change tlb's parameter usage, change util.Random to support case that mod is 1. * mmu.tlb: support normalAsVictim, super(fa) -> normal(sa/da) be carefull to use tlb's parameter, only a part of param combination is supported * mmu.tlb: fix bug of hit method and victim write * mmu.tlb: add tlb storage's perf counter * mmu.tlb: rewrite replace part, support set or non-set * mmu.tlb: add param outReplace to receive out replace index * mmu.tlb: change param superSize to superNWays add param superNSets, which should always be 1 * mmu.tlb: change some perf counter's name and change some params * mmu.tlb: fix bug of replace io bundle * mmu.tlb: remove unused signal wayIdx in tlbstorageio * mmu.tlb: separate tlb_ld/st into two 'same' tlb * mmu.tlb: when nWays is 1, replace returns 0.U before, replace will return 1.U, no influence for refill but bad for perf counter * mmu.tlb: give tlb_ld and tlb_st a name (in waveform)
2021-09-02 22:53:18 +08:00
val tlb = new TlbRequestIO
val pmp = Input(new PMPRespBundle()) // arrive same to tlb now
val fastpathOut = Output(new LoadToLoadIO)
val fastpathIn = Input(Vec(LoadPipelineWidth, new LoadToLoadIO))
val loadFastMatch = Input(UInt(exuParameters.LduCnt.W))
2020-10-17 21:05:46 +08:00
})
val load_s0 = Module(new LoadUnit_S0)
val load_s1 = Module(new LoadUnit_S1)
val load_s2 = Module(new LoadUnit_S2)
load_s0.io.in <> io.ldin
l0tlb: add a new level tlb, a load tlb and a store tlb (#961) * Revert "Revert "l0tlb: add a new level tlb to each mem pipeline (#936)" (#945)" This reverts commit b052b97230d6fdeedaf4e4905092adef6e768b4f. * fu: remove unused import * mmu.tlb: 2 load/store pipeline has 1 dtlb * mmu: remove btlb, the l1-tlb * mmu: set split-tlb to 32 to check perf effect * mmu: wrap tlb's param with TLBParameters * mmu: add params 'useBTlb' dtlb size is small: normal 8, super 2 * mmu.tlb: add Bundle TlbEntry, simplify tlb hit logic(coding) * mmu.tlb: seperate tlb's storage, relative hit/sfence logic tlb now supports full-associate, set-associate, directive-associate. more: change tlb's parameter usage, change util.Random to support case that mod is 1. * mmu.tlb: support normalAsVictim, super(fa) -> normal(sa/da) be carefull to use tlb's parameter, only a part of param combination is supported * mmu.tlb: fix bug of hit method and victim write * mmu.tlb: add tlb storage's perf counter * mmu.tlb: rewrite replace part, support set or non-set * mmu.tlb: add param outReplace to receive out replace index * mmu.tlb: change param superSize to superNWays add param superNSets, which should always be 1 * mmu.tlb: change some perf counter's name and change some params * mmu.tlb: fix bug of replace io bundle * mmu.tlb: remove unused signal wayIdx in tlbstorageio * mmu.tlb: separate tlb_ld/st into two 'same' tlb * mmu.tlb: when nWays is 1, replace returns 0.U before, replace will return 1.U, no influence for refill but bad for perf counter * mmu.tlb: give tlb_ld and tlb_st a name (in waveform)
2021-09-02 22:53:18 +08:00
load_s0.io.dtlbReq <> io.tlb.req
load_s0.io.dcacheReq <> io.dcache.req
load_s0.io.rsIdx := io.rsIdx
load_s0.io.isFirstIssue := io.isFirstIssue
load_s0.io.fastpath := io.fastpathIn
load_s0.io.loadFastMatch := io.loadFastMatch
2020-10-17 21:05:46 +08:00
PipelineConnect(load_s0.io.out, load_s1.io.in, true.B, load_s0.io.out.bits.uop.robIdx.needFlush(io.redirect))
2020-10-17 21:05:46 +08:00
l0tlb: add a new level tlb, a load tlb and a store tlb (#961) * Revert "Revert "l0tlb: add a new level tlb to each mem pipeline (#936)" (#945)" This reverts commit b052b97230d6fdeedaf4e4905092adef6e768b4f. * fu: remove unused import * mmu.tlb: 2 load/store pipeline has 1 dtlb * mmu: remove btlb, the l1-tlb * mmu: set split-tlb to 32 to check perf effect * mmu: wrap tlb's param with TLBParameters * mmu: add params 'useBTlb' dtlb size is small: normal 8, super 2 * mmu.tlb: add Bundle TlbEntry, simplify tlb hit logic(coding) * mmu.tlb: seperate tlb's storage, relative hit/sfence logic tlb now supports full-associate, set-associate, directive-associate. more: change tlb's parameter usage, change util.Random to support case that mod is 1. * mmu.tlb: support normalAsVictim, super(fa) -> normal(sa/da) be carefull to use tlb's parameter, only a part of param combination is supported * mmu.tlb: fix bug of hit method and victim write * mmu.tlb: add tlb storage's perf counter * mmu.tlb: rewrite replace part, support set or non-set * mmu.tlb: add param outReplace to receive out replace index * mmu.tlb: change param superSize to superNWays add param superNSets, which should always be 1 * mmu.tlb: change some perf counter's name and change some params * mmu.tlb: fix bug of replace io bundle * mmu.tlb: remove unused signal wayIdx in tlbstorageio * mmu.tlb: separate tlb_ld/st into two 'same' tlb * mmu.tlb: when nWays is 1, replace returns 0.U before, replace will return 1.U, no influence for refill but bad for perf counter * mmu.tlb: give tlb_ld and tlb_st a name (in waveform)
2021-09-02 22:53:18 +08:00
load_s1.io.dtlbResp <> io.tlb.resp
2020-12-13 21:31:00 +08:00
io.dcache.s1_paddr <> load_s1.io.dcachePAddr
io.dcache.s1_kill <> load_s1.io.dcacheKill
2020-12-11 19:59:25 +08:00
load_s1.io.sbuffer <> io.sbuffer
load_s1.io.lsq <> io.lsq.forward
load_s1.io.dcacheBankConflict <> io.dcache.s1_bank_conflict
2020-10-17 21:05:46 +08:00
PipelineConnect(load_s1.io.out, load_s2.io.in, true.B, load_s1.io.out.bits.uop.robIdx.needFlush(io.redirect))
2020-10-17 21:05:46 +08:00
io.dcache.s2_kill <> load_s2.io.dcache_kill
load_s2.io.dcacheResp <> io.dcache.resp
load_s2.io.pmpResp <> io.pmp
2020-12-13 21:31:00 +08:00
load_s2.io.lsq.forwardData <> io.lsq.forward.forwardData
load_s2.io.lsq.forwardMask <> io.lsq.forward.forwardMask
load_s2.io.lsq.forwardMaskFast <> io.lsq.forward.forwardMaskFast // should not be used in load_s2
load_s2.io.lsq.dataInvalid <> io.lsq.forward.dataInvalid
load_s2.io.lsq.matchInvalid <> io.lsq.forward.matchInvalid
load_s2.io.sbuffer.forwardData <> io.sbuffer.forwardData
load_s2.io.sbuffer.forwardMask <> io.sbuffer.forwardMask
load_s2.io.sbuffer.forwardMaskFast <> io.sbuffer.forwardMaskFast // should not be used in load_s2
load_s2.io.sbuffer.dataInvalid <> io.sbuffer.dataInvalid // always false
load_s2.io.sbuffer.matchInvalid <> io.sbuffer.matchInvalid
load_s2.io.dataForwarded <> io.lsq.loadDataForwarded
load_s2.io.fastpath <> io.fastpathOut
load_s2.io.dataInvalidSqIdx := io.lsq.forward.dataInvalidSqIdx // provide dataInvalidSqIdx to make wakeup faster
2021-02-08 20:48:27 +08:00
io.lsq.needReplayFromRS := load_s2.io.needReplayFromRS
2020-10-17 21:05:46 +08:00
// feedback tlb miss / dcache miss queue full
io.feedbackSlow.bits := RegNext(load_s2.io.rsFeedback.bits)
io.feedbackSlow.valid := RegNext(load_s2.io.rsFeedback.valid && !load_s2.io.out.bits.uop.robIdx.needFlush(io.redirect))
// feedback bank conflict to rs
io.feedbackFast.bits := load_s1.io.rsFeedback.bits
io.feedbackFast.valid := load_s1.io.rsFeedback.valid
assert(!(RegNext(RegNext(io.feedbackFast.valid)) && io.feedbackSlow.valid))
// pre-calcuate sqIdx mask in s0, then send it to lsq in s1 for forwarding
val sqIdxMaskReg = RegNext(UIntToMask(load_s0.io.in.bits.uop.sqIdx.value, StoreQueueSize))
io.lsq.forward.sqIdxMask := sqIdxMaskReg
2020-10-17 21:05:46 +08:00
// // use s2_hit_way to select data received in s1
// load_s2.io.dcacheResp.bits.data := Mux1H(RegNext(io.dcache.s1_hit_way), RegNext(io.dcache.s1_data))
// assert(load_s2.io.dcacheResp.bits.data === io.dcache.resp.bits.data)
2021-08-20 18:17:28 +08:00
io.fastUop.valid := io.dcache.s1_hit_way.orR && // dcache hit
!io.dcache.s1_disable_fast_wakeup && // load fast wakeup should be disabled when dcache data read is not ready
load_s1.io.in.valid && // valid laod request
!load_s1.io.dcacheKill && // not mmio or tlb miss
!io.lsq.forward.dataInvalidFast // forward failed
io.fastUop.bits := load_s1.io.out.bits.uop
2020-10-17 21:05:46 +08:00
XSDebug(load_s0.io.out.valid,
p"S0: pc ${Hexadecimal(load_s0.io.out.bits.uop.cf.pc)}, lId ${Hexadecimal(load_s0.io.out.bits.uop.lqIdx.asUInt)}, " +
2020-10-17 21:05:46 +08:00
p"vaddr ${Hexadecimal(load_s0.io.out.bits.vaddr)}, mask ${Hexadecimal(load_s0.io.out.bits.mask)}\n")
2020-11-18 20:47:14 +08:00
XSDebug(load_s1.io.out.valid,
l0tlb: add a new level tlb, a load tlb and a store tlb (#961) * Revert "Revert "l0tlb: add a new level tlb to each mem pipeline (#936)" (#945)" This reverts commit b052b97230d6fdeedaf4e4905092adef6e768b4f. * fu: remove unused import * mmu.tlb: 2 load/store pipeline has 1 dtlb * mmu: remove btlb, the l1-tlb * mmu: set split-tlb to 32 to check perf effect * mmu: wrap tlb's param with TLBParameters * mmu: add params 'useBTlb' dtlb size is small: normal 8, super 2 * mmu.tlb: add Bundle TlbEntry, simplify tlb hit logic(coding) * mmu.tlb: seperate tlb's storage, relative hit/sfence logic tlb now supports full-associate, set-associate, directive-associate. more: change tlb's parameter usage, change util.Random to support case that mod is 1. * mmu.tlb: support normalAsVictim, super(fa) -> normal(sa/da) be carefull to use tlb's parameter, only a part of param combination is supported * mmu.tlb: fix bug of hit method and victim write * mmu.tlb: add tlb storage's perf counter * mmu.tlb: rewrite replace part, support set or non-set * mmu.tlb: add param outReplace to receive out replace index * mmu.tlb: change param superSize to superNWays add param superNSets, which should always be 1 * mmu.tlb: change some perf counter's name and change some params * mmu.tlb: fix bug of replace io bundle * mmu.tlb: remove unused signal wayIdx in tlbstorageio * mmu.tlb: separate tlb_ld/st into two 'same' tlb * mmu.tlb: when nWays is 1, replace returns 0.U before, replace will return 1.U, no influence for refill but bad for perf counter * mmu.tlb: give tlb_ld and tlb_st a name (in waveform)
2021-09-02 22:53:18 +08:00
p"S1: pc ${Hexadecimal(load_s1.io.out.bits.uop.cf.pc)}, lId ${Hexadecimal(load_s1.io.out.bits.uop.lqIdx.asUInt)}, tlb_miss ${io.tlb.resp.bits.miss}, " +
p"paddr ${Hexadecimal(load_s1.io.out.bits.paddr)}, mmio ${load_s1.io.out.bits.mmio}\n")
2020-08-06 16:58:13 +08:00
2020-11-18 20:47:14 +08:00
// writeback to LSQ
2020-08-06 16:58:13 +08:00
// Current dcache use MSHR
// Load queue will be updated at s2 for both hit/miss int/fp load
2020-11-18 20:47:14 +08:00
io.lsq.loadIn.valid := load_s2.io.out.valid
io.lsq.loadIn.bits := load_s2.io.out.bits
// write to rob and writeback bus
val s2_wb_valid = load_s2.io.out.valid && !load_s2.io.out.bits.miss && !load_s2.io.out.bits.mmio
2020-08-06 16:58:13 +08:00
// Int load, if hit, will be writebacked at s2
val hitLoadOut = Wire(Valid(new ExuOutput))
hitLoadOut.valid := s2_wb_valid
hitLoadOut.bits.uop := load_s2.io.out.bits.uop
hitLoadOut.bits.data := load_s2.io.out.bits.data
hitLoadOut.bits.redirectValid := false.B
hitLoadOut.bits.redirect := DontCare
hitLoadOut.bits.debug.isMMIO := load_s2.io.out.bits.mmio
hitLoadOut.bits.debug.isPerfCnt := false.B
hitLoadOut.bits.debug.paddr := load_s2.io.out.bits.paddr
hitLoadOut.bits.fflags := DontCare
2020-08-06 16:58:13 +08:00
2020-10-17 21:05:46 +08:00
load_s2.io.out.ready := true.B
io.ldout.bits := Mux(hitLoadOut.valid, hitLoadOut.bits, io.lsq.ldout.bits)
io.ldout.valid := hitLoadOut.valid || io.lsq.ldout.valid
io.lsq.ldout.ready := !hitLoadOut.valid
2020-08-16 15:59:15 +08:00
when(io.ldout.fire()){
XSDebug("ldout %x\n", io.ldout.bits.uop.cf.pc)
}
}