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.mem 18 19import org.chipsalliance.cde.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import xiangshan._ 23import utils._ 24import utility._ 25import xiangshan.cache._ 26 27trait HasStorePrefetchHelper extends HasCircularQueuePtrHelper with HasDCacheParameters { 28 // common 29 val PAGEOFFSET = 12 // page offset 4096 Bytes 30 val BLOCKOFFSET = log2Up(dcacheParameters.blockBytes) // cache block offset 64 Bytes 31 32 // spb parameters 33 val ENABLE_SPB = EnableStorePrefetchSPB 34 val ONLY_ON_MEMSET = false 35 val SATURATE_COUNTER_BITS = 7 36 val BURST_ENGINE_SIZE = 2 37 val SPB_N = 48 38 39 // serializer parameters 40 val SERIALIZER_SIZE = 12 41 42 def block_addr(x: UInt): UInt = { 43 val offset = log2Up(dcacheParameters.blockBytes) 44 x(x.getWidth - 1, offset) 45 } 46 47 // filter logic (granularity: a page) 48 def same_page_addr(addr0: UInt, addr1: UInt): Bool = { 49 addr0(addr0.getWidth - 1, PAGEOFFSET) === addr1(addr1.getWidth - 1, PAGEOFFSET) 50 } 51 52 def filter_by_page_addr(valid_vec: Vec[Bool], data_vec: Vec[UInt], incoming_vaddr: UInt) : Bool = { 53 val match_vec = (valid_vec zip data_vec).map{ 54 case(v, e_vaddr) => v && same_page_addr(e_vaddr, incoming_vaddr) 55 } 56 VecInit(match_vec).asUInt.orR 57 } 58 59 def cache_block_addr_difference(req_addr: UInt, last_addr: UInt): UInt = { 60 (block_addr(req_addr).asSInt - block_addr(last_addr).asSInt)(SATURATE_COUNTER_BITS - 1, 0) 61 } 62 63 def get_store_count_divided_by_8(st_count: UInt): UInt = { 64 st_count(st_count.getWidth - 1, 3) 65 } 66 67 def trigger_check(st_count: UInt, N: UInt): Bool = { 68 st_count > N 69 } 70 71 def can_burst(st_count: UInt, N: UInt, sa_count: SInt): Bool = { 72 // 1.counter overflows 73 // 2.counter / 8 == saturate counter 74 // 3.saturate counter is not negtive 75 trigger_check(st_count, N) && get_store_count_divided_by_8(st_count) === sa_count.asUInt && sa_count(sa_count.getWidth - 1) === false.B 76 } 77} 78 79// L1 Store prefetch component 80 81// an prefetch request generator used by spb to burst some prefetch request to L1 Dcache 82class PrefetchBurstGenerator(is_store: Boolean)(implicit p: Parameters) extends DCacheModule with HasStorePrefetchHelper { 83 val io = IO(new DCacheBundle { 84 val alloc = Input(Bool()) 85 val vaddr = Input(UInt(VAddrBits.W)) 86 val prefetch_req = Vec(StorePipelineWidth, DecoupledIO(new StorePrefetchReq)) 87 }) 88 89 require(StorePipelineWidth >= 2) 90 91 val SIZE = BURST_ENGINE_SIZE 92 93 val valids = RegInit(VecInit(List.tabulate(SIZE){_ => false.B})) 94 val datas = RegInit(VecInit(List.tabulate(SIZE){_ => 0.U.asTypeOf(io.vaddr)})) 95 val pagebits = RegInit(VecInit(List.tabulate(SIZE){_ => 0.U(1.W)})) 96 97 // enq 98 val enq_valids = ~(valids.asUInt) 99 val full = !(enq_valids.orR) 100 val enq_idx = PriorityEncoder(enq_valids) 101 val enq_filter = filter_by_page_addr(valids, datas, io.vaddr) 102 103 when(io.alloc && !full && !enq_filter) { 104 valids(enq_idx) := true.B 105 datas(enq_idx) := io.vaddr 106 pagebits(enq_idx) := io.vaddr(PAGEOFFSET) 107 } 108 109 XSPerfAccumulate("burst_generator_alloc_success", io.alloc && !full && !enq_filter) 110 XSPerfAccumulate("burst_generator_alloc_fail", io.alloc && full && !enq_filter) 111 XSPerfAccumulate("burst_generator_full", full) 112 113 // next prefetch address 114 val datas_next = Wire(Vec(SIZE, chiselTypeOf(datas(0)))) 115 datas_next := datas.map(_ + Cat(1.U(1.W), 0.U(BLOCKOFFSET.W))) 116 // double next prefetch address 117 val datas_next_next = Wire(Vec(SIZE, chiselTypeOf(datas(0)))) 118 datas_next_next := datas.map(_ + Cat(2.U(2.W), 0.U(BLOCKOFFSET.W))) 119 120 // deq 121 // val deq_valids = (valids zip datas zip pagebits).map{case (v, vaddr, pgbit) => v && vaddr(PAGEOFFSET) === pagebits} 122 val deq_valids = valids 123 val deq_decoupled = Wire(Vec(SIZE, Vec(StorePipelineWidth, Decoupled(new StorePrefetchReq)))) 124 125 (deq_valids zip deq_decoupled zip datas zip datas_next zip datas_next_next zip pagebits zip valids).foreach{case ((((((deq_valid, out_decouple), data), data_next), data_next_next), pg_bit), v) => { 126 out_decouple(0).valid := deq_valid 127 out_decouple(0).bits := DontCare 128 out_decouple(0).bits.vaddr := data 129 out_decouple(1).valid := deq_valid && data_next(PAGEOFFSET) === pg_bit && out_decouple(0).fire 130 out_decouple(1).bits := DontCare 131 out_decouple(1).bits.vaddr := data_next 132 out_decouple.drop(2).foreach { out => out.valid := false.B; out.bits := DontCare } 133 when(out_decouple(1).fire) { 134 // fired 2 prefetch reqs 135 data := data_next_next 136 when(data_next_next(PAGEOFFSET) =/= pg_bit) { 137 // cross page, invalid this entry 138 v := false.B 139 } 140 }.elsewhen(out_decouple(0).fire) { 141 // fired 1 prefetch req 142 data := data_next 143 when(data_next(PAGEOFFSET) =/= pg_bit) { 144 // cross page, invalid this entry 145 v := false.B 146 } 147 } 148 }} 149 for (i <- 0 until StorePipelineWidth) { 150 arbiter(deq_decoupled.map(_(i)), io.prefetch_req(i), Some(s"spb_deq_arb${i}")) 151 } 152 153 XSPerfAccumulate("burst_valid_num", PopCount(valids)) 154 XSPerfAccumulate("prefetch_req_fire_by_generator", PopCount(VecInit(io.prefetch_req.map(_.fire)))) 155} 156 157class StorePrefetchBursts(implicit p: Parameters) extends DCacheModule with HasStorePrefetchHelper { 158 val io = IO(new DCacheBundle { 159 val enable = Input(Bool()) 160 val memSetPattenDetected = Input(Bool()) 161 val sbuffer_enq = Flipped(Valid(new DCacheWordReqWithVaddr)) 162 val prefetch_req = Vec(StorePipelineWidth, DecoupledIO(new StorePrefetchReq)) 163 }) 164 require(EnsbufferWidth == 2) 165 166 // meta for SPB 167 val N = SPB_N 168 val last_st_block_addr = RegInit(0.U(VAddrBits.W)) 169 val saturate_counter = RegInit(0.S(SATURATE_COUNTER_BITS.W)) 170 val store_count = RegInit(0.U((log2Up(N) + 1).W)) 171 val burst_engine = Module(new PrefetchBurstGenerator(is_store = true)) 172 173 val sbuffer_fire = io.sbuffer_enq.valid 174 val sbuffer_vaddr = io.sbuffer_enq.bits.vaddr 175 176 val next_store_count = store_count + Mux(sbuffer_fire, 1.U, 0.U) 177 val next_saturate_count = (saturate_counter + Mux(sbuffer_fire, cache_block_addr_difference(sbuffer_vaddr, last_st_block_addr).asSInt, 0.S)).asSInt 178 179 when(sbuffer_fire) { 180 last_st_block_addr := sbuffer_vaddr 181 } 182 183 val check = trigger_check(next_store_count, N.U) 184 val burst = can_burst(next_store_count, N.U, next_saturate_count) 185 186 store_count := Mux(burst || check, 0.U, next_store_count) 187 saturate_counter := Mux(burst || check, 0.S, next_saturate_count) 188 189 if(ONLY_ON_MEMSET) { 190 // very strict: only burst on memset 191 burst_engine.io.alloc := burst && io.enable && io.memSetPattenDetected 192 }else { 193 burst_engine.io.alloc := burst && io.enable 194 } 195 burst_engine.io.vaddr := get_block_addr(io.sbuffer_enq.bits.vaddr) 196 burst_engine.io.prefetch_req <> io.prefetch_req 197 198 // perf 199 XSPerfAccumulate("trigger_burst", burst && io.enable) 200 XSPerfAccumulate("trigger_check", check && io.enable) 201} 202 203// L2 Store prefetch component 204 205// Serializer: FIFO queue, recieve EnsbufferWidth requests sent from sq to sbuffer 206// save them to a FIFO queue, pop them in order 207class Serializer(implicit p: Parameters) extends DCacheModule with HasStorePrefetchHelper { 208 val io = IO(new DCacheBundle { 209 val sbuffer_enq = Vec(EnsbufferWidth, Flipped(Valid(new DCacheWordReqWithVaddr))) 210 val prefetch_train = DecoupledIO(new DCacheWordReqWithVaddr) 211 }) 212 val QueueSize = SERIALIZER_SIZE 213 214 class SerializerPtr(implicit p: Parameters) extends CircularQueuePtr[SerializerPtr](p => QueueSize){} 215 216 object SerializerPtr { 217 def apply(f: Bool, v: UInt)(implicit p: Parameters): SerializerPtr = { 218 val ptr = Wire(new SerializerPtr) 219 ptr.flag := f 220 ptr.value := v 221 ptr 222 } 223 } 224 225 val enqPtrExt = RegInit(VecInit((0 until EnsbufferWidth).map(_.U.asTypeOf(new SerializerPtr)))) 226 val deqPtrExt = RegInit(0.U.asTypeOf(new SerializerPtr)) 227 228 val deqPtr = deqPtrExt.value 229 230 val reqs = RegInit(VecInit((0 until QueueSize).map(_.U.asTypeOf(Valid(new DCacheWordReqWithVaddr))))) 231 232 // deq 233 io.prefetch_train.valid := reqs(deqPtr).valid 234 io.prefetch_train.bits := reqs(deqPtr).bits 235 236 when(io.prefetch_train.fire) { 237 deqPtrExt := deqPtrExt + 1.U 238 reqs(deqPtr).valid := false.B 239 } 240 241 // enq 242 val count_vsreq = PopCount(io.sbuffer_enq.map(_.valid)) 243 val canEnqueue = (distanceBetween(enqPtrExt(0), deqPtrExt) + count_vsreq) <= QueueSize.U 244 245 when(canEnqueue) { 246 for(i <- 0 until EnsbufferWidth) { 247 when(io.sbuffer_enq(i).valid) { 248 reqs(enqPtrExt(i).value) := io.sbuffer_enq(i) 249 } 250 } 251 enqPtrExt.map(ptr => ptr := ptr + count_vsreq) 252 } 253 254 XSPerfAccumulate("canNotEnqueue", !canEnqueue) 255 XSPerfAccumulate("prefetch_train_fire", io.prefetch_train.fire) 256 XSPerfAccumulate("full", PopCount(reqs.map(_.valid)) === QueueSize.U) 257} 258 259class StorePfWrapper()(implicit p: Parameters) extends DCacheModule with HasStorePrefetchHelper { 260 val io = IO(new DCacheBundle { 261 val sbuffer_enq = Vec(EnsbufferWidth, Flipped(Valid(new DCacheWordReqWithVaddr))) 262 val prefetch_req = Vec(StorePipelineWidth, DecoupledIO(new StorePrefetchReq)) 263 val memSetPattenDetected = Input(Bool()) 264 }) 265 266 // TODO: remove serializer, use a ptr in sq 267 val serializer = Module(new Serializer()) 268 val spb = Module(new StorePrefetchBursts()) 269 270 // give mutiple reqs to serializer, serializer will give out one req per cycle 271 for(i <- 0 until EnsbufferWidth) { 272 serializer.io.sbuffer_enq(i).valid := io.sbuffer_enq(i).valid && ENABLE_SPB.B 273 serializer.io.sbuffer_enq(i).bits := io.sbuffer_enq(i).bits 274 } 275 276 // train spb 277 spb.io.enable := ENABLE_SPB.B 278 spb.io.memSetPattenDetected := io.memSetPattenDetected 279 spb.io.sbuffer_enq.valid := serializer.io.prefetch_train.valid 280 spb.io.sbuffer_enq.bits := serializer.io.prefetch_train.bits 281 // spb will always recieve train req 282 serializer.io.prefetch_train.ready := true.B 283 284 // fire a prefetch req 285 io.prefetch_req <> spb.io.prefetch_req 286}