1/*************************************************************************************** 2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3* Copyright (c) 2020-2021 Peng Cheng Laboratory 4* 5* XiangShan is licensed under Mulan PSL v2. 6* You can use this software according to the terms and conditions of the Mulan PSL v2. 7* You may obtain a copy of Mulan PSL v2 at: 8* http://license.coscl.org.cn/MulanPSL2 9* 10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13* 14* See the Mulan PSL v2 for more details. 15***************************************************************************************/ 16 17package xiangshan.backend.fu 18 19import chisel3._ 20import chisel3.util._ 21import org.chipsalliance.cde.config.Parameters 22import utility.XSDebug 23import xiangshan.ExceptionNO.{illegalInstr, virtualInstr} 24import xiangshan._ 25 26class FenceIO(implicit p: Parameters) extends XSBundle { 27 val sfence = Output(new SfenceBundle) 28 val fencei = Output(Bool()) 29 val sbuffer = new FenceToSbuffer 30} 31 32class FenceToSbuffer extends Bundle { 33 val flushSb = Output(Bool()) 34 val sbIsEmpty = Input(Bool()) 35} 36 37class Fence(cfg: FuConfig)(implicit p: Parameters) extends FuncUnit(cfg) { 38 39 val sfence = io.fenceio.get.sfence 40 val fencei = io.fenceio.get.fencei 41 val toSbuffer = io.fenceio.get.sbuffer 42 val (valid, src1) = ( 43 io.in.valid, 44 io.in.bits.data.src(0) 45 ) 46 47 val s_idle :: s_wait :: s_tlb :: s_icache :: s_fence :: s_nofence :: Nil = Enum(6) 48 49 val state = RegInit(s_idle) 50 /* fsm 51 * s_idle : init state, send sbflush 52 * s_wait : send sbflush, wait for sbEmpty 53 * s_tlb : flush tlb, just hold one cycle 54 * s_icache: flush icache, just hold one cycle 55 * s_fence : do nothing, for timing optimiaztion 56 * s_nofence: do nothing , for Svinval extension 57 */ 58 59 val sbuffer = toSbuffer.flushSb 60 val sbEmpty = toSbuffer.sbIsEmpty 61 val uop = RegEnable(io.in.bits, io.in.fire) 62 val func = uop.ctrl.fuOpType 63 64 // NOTE: icache & tlb & sbuffer must receive flush signal at any time 65 sbuffer := state === s_wait 66 fencei := state === s_icache 67 sfence.valid := state === s_tlb && (func === FenceOpType.sfence || func === FenceOpType.hfence_v || func === FenceOpType.hfence_g) 68 sfence.bits.rs1 := uop.data.imm(4, 0) === 0.U 69 sfence.bits.rs2 := uop.data.imm(9, 5) === 0.U 70 sfence.bits.flushPipe := uop.ctrl.flushPipe.get 71 sfence.bits.hv := func === FenceOpType.hfence_v 72 sfence.bits.hg := func === FenceOpType.hfence_g 73 sfence.bits.addr := RegEnable(io.in.bits.data.src(0), io.in.fire) 74 sfence.bits.id := RegEnable(io.in.bits.data.src(1), io.in.fire) 75 76 when (state === s_idle && io.in.valid) { state := s_wait } 77 when (state === s_wait && func === FenceOpType.fencei && sbEmpty) { state := s_icache } 78 when (state === s_wait && ((func === FenceOpType.sfence || func === FenceOpType.hfence_g || func === FenceOpType.hfence_v) && sbEmpty)) { state := s_tlb } 79 when (state === s_wait && func === FenceOpType.fence && sbEmpty) { state := s_fence } 80 when (state === s_wait && func === FenceOpType.nofence && sbEmpty) { state := s_nofence } 81 when (state =/= s_idle && state =/= s_wait) { state := s_idle } 82 83 io.in.ready := state === s_idle 84 io.out.valid := state =/= s_idle && state =/= s_wait 85 io.out.bits.res.data := 0.U 86 io.out.bits.ctrl.robIdx := uop.ctrl.robIdx 87 io.out.bits.res.pc.get := uop.data.pc.get 88 io.out.bits.ctrl.pdest := uop.ctrl.pdest 89 io.out.bits.ctrl.flushPipe.get := uop.ctrl.flushPipe.get 90 io.out.bits.ctrl.exceptionVec.get := 0.U.asTypeOf(io.out.bits.ctrl.exceptionVec.get) 91 io.out.bits.perfDebugInfo := io.in.bits.perfDebugInfo 92 93 XSDebug(io.in.valid, p"In(${io.in.valid} ${io.in.ready}) state:${state} Inpc:0x${Hexadecimal(io.in.bits.data.pc.get)} InrobIdx:${io.in.bits.ctrl.robIdx}\n") 94 XSDebug(state =/= s_idle, p"state:${state} sbuffer(flush:${sbuffer} empty:${sbEmpty}) fencei:${fencei} sfence:${sfence}\n") 95 XSDebug(io.out.valid, p" Out(${io.out.valid} ${io.out.ready}) state:${state} Outpc:0x${Hexadecimal(io.out.bits.res.pc.get)} OutrobIdx:${io.out.bits.ctrl.robIdx}\n") 96 97 assert(!io.out.valid || io.out.ready, "when fence is out valid, out ready should always be true") 98} 99