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.frontend.icache 18 19import chisel3._ 20import chisel3.util._ 21import utils._ 22import chipsalliance.rocketchip.config.Parameters 23import freechips.rocketchip.diplomacy.{IdRange, LazyModule, LazyModuleImp, TransferSizes} 24import freechips.rocketchip.tilelink.{TLArbiter, TLBundleA, TLBundleD, TLClientNode, TLEdgeOut, TLMasterParameters, TLMasterPortParameters} 25import xiangshan._ 26import xiangshan.frontend._ 27 28class InsUncacheReq(implicit p: Parameters) extends ICacheBundle 29{ 30 val addr = UInt(PAddrBits.W) 31} 32 33class InsUncacheResp(implicit p: Parameters) extends ICacheBundle 34{ 35 val data = UInt(maxInstrLen.W) 36} 37 38// One miss entry deals with one mmio request 39class InstrMMIOEntry(edge: TLEdgeOut)(implicit p: Parameters) extends XSModule with HasICacheParameters with HasIFUConst 40{ 41 val io = IO(new Bundle { 42 val id = Input(UInt(log2Up(cacheParams.nMMIOs).W)) 43 // client requests 44 val req = Flipped(DecoupledIO(new InsUncacheReq)) 45 val resp = DecoupledIO(new InsUncacheResp) 46 47 val mmio_acquire = DecoupledIO(new TLBundleA(edge.bundle)) 48 val mmio_grant = Flipped(DecoupledIO(new TLBundleD(edge.bundle))) 49 50 val flush = Input(Bool()) 51 }) 52 53 54 val s_invalid :: s_refill_req :: s_refill_resp :: s_send_resp :: Nil = Enum(4) 55 56 val state = RegInit(s_invalid) 57 58 val req = Reg(new InsUncacheReq ) 59 val respDataReg = Reg(UInt(mmioBusWidth.W)) 60 61 // assign default values to output signals 62 io.req.ready := false.B 63 io.resp.valid := false.B 64 io.resp.bits := DontCare 65 66 io.mmio_acquire.valid := false.B 67 io.mmio_acquire.bits := DontCare 68 69 io.mmio_grant.ready := false.B 70 71 val needFlush = RegInit(false.B) 72 73 when(io.flush && (state =/= s_invalid) && (state =/= s_send_resp)){ needFlush := true.B } 74 .elsewhen((state=== s_send_resp) && needFlush){ needFlush := false.B } 75 76 // -------------------------------------------- 77 // s_invalid: receive requests 78 when (state === s_invalid) { 79 io.req.ready := true.B 80 81 when (io.req.fire()) { 82 req := io.req.bits 83 state := s_refill_req 84 } 85 } 86 87 88 when (state === s_refill_req) { 89 val address_aligned = req.addr(req.addr.getWidth - 1, log2Ceil(mmioBusBytes)) 90 io.mmio_acquire.valid := true.B 91 io.mmio_acquire.bits := edge.Get( 92 fromSource = io.id, 93 toAddress = Cat(address_aligned, 0.U(log2Ceil(mmioBusBytes).W)), 94 lgSize = log2Ceil(mmioBusBytes).U 95 )._2 96 97 when (io.mmio_acquire.fire()) { 98 state := s_refill_resp 99 } 100 } 101 102 val (_, _, refill_done, _) = edge.addr_inc(io.mmio_grant) 103 104 when (state === s_refill_resp) { 105 io.mmio_grant.ready := true.B 106 107 when (io.mmio_grant.fire()) { 108 respDataReg := io.mmio_grant.bits.data 109 state := s_send_resp 110 } 111 } 112 113 def getDataFromBus(pc: UInt) = { 114 val respData = Wire(UInt(maxInstrLen.W)) 115 respData := Mux(pc(2,1) === "b00".U, respDataReg(31,0), 116 Mux(pc(2,1) === "b01".U, respDataReg(47,16), 117 Mux(pc(2,1) === "b10".U, respDataReg(63,32), 118 Cat(0.U, respDataReg(63,48)) 119 ) 120 ) 121 ) 122 respData 123 } 124 125 when (state === s_send_resp) { 126 io.resp.valid := !needFlush 127 io.resp.bits.data := getDataFromBus(req.addr) 128 // meta data should go with the response 129 when (io.resp.fire() || needFlush) { 130 state := s_invalid 131 } 132 } 133} 134 135class InstrUncacheIO(implicit p: Parameters) extends ICacheBundle { 136 val req = Flipped(DecoupledIO(new InsUncacheReq )) 137 val resp = DecoupledIO(new InsUncacheResp) 138 val flush = Input(Bool()) 139} 140 141class InstrUncache()(implicit p: Parameters) extends LazyModule with HasICacheParameters { 142 143 val clientParameters = TLMasterPortParameters.v1( 144 clients = Seq(TLMasterParameters.v1( 145 "InstrUncache", 146 sourceId = IdRange(0, cacheParams.nMMIOs) 147 )) 148 ) 149 val clientNode = TLClientNode(Seq(clientParameters)) 150 151 lazy val module = new InstrUncacheImp(this) 152 153} 154 155class InstrUncacheImp(outer: InstrUncache) 156 extends LazyModuleImp(outer) 157 with HasICacheParameters 158 with HasTLDump 159{ 160 val io = IO(new InstrUncacheIO) 161 162 val (bus, edge) = outer.clientNode.out.head 163 164 val resp_arb = Module(new Arbiter(new InsUncacheResp, cacheParams.nMMIOs)) 165 166 val req = io.req 167 val resp = io.resp 168 val mmio_acquire = bus.a 169 val mmio_grant = bus.d 170 171 val entry_alloc_idx = Wire(UInt()) 172 val req_ready = WireInit(false.B) 173 174 // assign default values to output signals 175 bus.b.ready := false.B 176 bus.c.valid := false.B 177 bus.c.bits := DontCare 178 bus.d.ready := false.B 179 bus.e.valid := false.B 180 bus.e.bits := DontCare 181 182 val entries = (0 until cacheParams.nMMIOs) map { i => 183 val entry = Module(new InstrMMIOEntry(edge)) 184 185 entry.io.id := i.U(log2Up(cacheParams.nMMIOs).W) 186 entry.io.flush := io.flush 187 188 // entry req 189 entry.io.req.valid := (i.U === entry_alloc_idx) && req.valid 190 entry.io.req.bits := req.bits 191 when (i.U === entry_alloc_idx) { 192 req_ready := entry.io.req.ready 193 } 194 195 // entry resp 196 resp_arb.io.in(i) <> entry.io.resp 197 198 entry.io.mmio_grant.valid := false.B 199 entry.io.mmio_grant.bits := DontCare 200 when (mmio_grant.bits.source === i.U) { 201 entry.io.mmio_grant <> mmio_grant 202 } 203 entry 204 } 205 206 entry_alloc_idx := PriorityEncoder(entries.map(m=>m.io.req.ready)) 207 208 req.ready := req_ready 209 resp <> resp_arb.io.out 210 TLArbiter.lowestFromSeq(edge, mmio_acquire, entries.map(_.io.mmio_acquire)) 211 212} 213