1package xiangshan.mem.prefetch 2 3import org.chipsalliance.cde.config.Parameters 4import chisel3._ 5import chisel3.util._ 6import xiangshan._ 7import utils._ 8import utility._ 9import xiangshan.cache.HasDCacheParameters 10import xiangshan.cache.mmu._ 11import xiangshan.mem.{L1PrefetchReq, LdPrefetchTrainBundle} 12import xiangshan.mem.trace._ 13import xiangshan.mem.L1PrefetchSource 14 15trait HasL1PrefetchHelper extends HasCircularQueuePtrHelper with HasDCacheParameters { 16 // region related 17 val REGION_SIZE = 1024 18 val PAGE_OFFSET = 12 19 val BLOCK_OFFSET = log2Up(dcacheParameters.blockBytes) 20 val BIT_VEC_WITDH = REGION_SIZE / dcacheParameters.blockBytes 21 val REGION_BITS = log2Up(BIT_VEC_WITDH) 22 val REGION_TAG_OFFSET = BLOCK_OFFSET + REGION_BITS 23 val REGION_TAG_BITS = VAddrBits - BLOCK_OFFSET - REGION_BITS 24 25 // hash related 26 val VADDR_HASH_WIDTH = 5 27 val BLK_ADDR_RAW_WIDTH = 10 28 val HASH_TAG_WIDTH = VADDR_HASH_WIDTH + BLK_ADDR_RAW_WIDTH 29 30 // capacity related 31 val MLP_SIZE = 16 32 33 // prefetch sink related 34 val SINK_BITS = 2 35 def SINK_L1 = "b00".U 36 def SINK_L2 = "b01".U 37 def SINK_L3 = "b10".U 38 39 // vaddr: | region tag | region bits | block offset | 40 def get_region_tag(vaddr: UInt) = { 41 require(vaddr.getWidth == VAddrBits) 42 vaddr(vaddr.getWidth - 1, REGION_TAG_OFFSET) 43 } 44 45 def get_region_bits(vaddr: UInt) = { 46 require(vaddr.getWidth == VAddrBits) 47 vaddr(REGION_TAG_OFFSET - 1, BLOCK_OFFSET) 48 } 49 50 def block_addr(x: UInt): UInt = { 51 x(x.getWidth - 1, BLOCK_OFFSET) 52 } 53 54 def vaddr_hash(x: UInt): UInt = { 55 val width = VADDR_HASH_WIDTH 56 val low = x(width - 1, 0) 57 val mid = x(2 * width - 1, width) 58 val high = x(3 * width - 1, 2 * width) 59 low ^ mid ^ high 60 } 61 62 def pc_hash_tag(x: UInt): UInt = { 63 val low = x(BLK_ADDR_RAW_WIDTH - 1, 0) 64 val high = x(BLK_ADDR_RAW_WIDTH - 1 + 3 * VADDR_HASH_WIDTH, BLK_ADDR_RAW_WIDTH) 65 val high_hash = vaddr_hash(high) 66 Cat(high_hash, low) 67 } 68 69 def block_hash_tag(x: UInt): UInt = { 70 val blk_addr = block_addr(x) 71 val low = blk_addr(BLK_ADDR_RAW_WIDTH - 1, 0) 72 val high = blk_addr(BLK_ADDR_RAW_WIDTH - 1 + 3 * VADDR_HASH_WIDTH, BLK_ADDR_RAW_WIDTH) 73 val high_hash = vaddr_hash(high) 74 Cat(high_hash, low) 75 } 76 77 def region_hash_tag(region_tag: UInt): UInt = { 78 val low = region_tag(BLK_ADDR_RAW_WIDTH - 1, 0) 79 val high = region_tag(BLK_ADDR_RAW_WIDTH - 1 + 3 * VADDR_HASH_WIDTH, BLK_ADDR_RAW_WIDTH) 80 val high_hash = vaddr_hash(high) 81 Cat(high_hash, low) 82 } 83 84 def region_to_block_addr(region_tag: UInt, region_bits: UInt): UInt = { 85 Cat(region_tag, region_bits) 86 } 87 88 def get_candidate_oh(x: UInt): UInt = { 89 require(x.getWidth == PAddrBits) 90 UIntToOH(x(REGION_BITS + BLOCK_OFFSET - 1, BLOCK_OFFSET)) 91 } 92 93 def toBinary(n: Int): String = n match { 94 case 0|1 => s"$n" 95 case _ => s"${toBinary(n/2)}${n%2}" 96 } 97} 98 99trait HasTrainFilterHelper extends HasCircularQueuePtrHelper { 100 def reorder[T <: LdPrefetchTrainBundle](source: Vec[ValidIO[T]]): Vec[ValidIO[T]] = { 101 if(source.length == 1) { 102 source 103 }else if(source.length == 2) { 104 val source_v = source.map(_.valid) 105 val res = Wire(source.cloneType) 106 // source 1 is older than source 0 107 val source_1_older = isBefore(source(1).bits.uop.robIdx, source(0).bits.uop.robIdx) 108 when(source_1_older) { 109 res(0) := source(1) 110 res(1) := source(0) 111 }.otherwise { 112 res := source 113 } 114 115 res 116 }else if(source.length == 3) { 117 // TODO: generalize 118 val res_0_1 = Wire(source.cloneType) 119 val res_1_2 = Wire(source.cloneType) 120 val res = Wire(source.cloneType) 121 122 val tmp = reorder(VecInit(source.slice(0, 2))) 123 res_0_1(0) := tmp(0) 124 res_0_1(1) := tmp(1) 125 res_0_1(2) := source(2) 126 val tmp_1 = reorder(VecInit(res_0_1.slice(1, 3))) 127 res_1_2(0) := res_0_1(0) 128 res_1_2(1) := tmp_1(0) 129 res_1_2(2) := tmp_1(1) 130 val tmp_2 = reorder(VecInit(res_1_2.slice(0, 2))) 131 res(0) := tmp_2(0) 132 res(1) := tmp_2(1) 133 res(2) := res_1_2(2) 134 135 res 136 }else { 137 require(false, "for now, 4 or more sources are invalid") 138 source 139 } 140 } 141} 142 143// get prefetch train reqs from `exuParameters.LduCnt` load pipelines (up to `exuParameters.LduCnt`/cycle) 144// filter by cache line address, send out train req to stride (up to 1 req/cycle) 145class TrainFilter(size: Int, name: String)(implicit p: Parameters) extends XSModule with HasL1PrefetchHelper with HasTrainFilterHelper { 146 val io = IO(new Bundle() { 147 val enable = Input(Bool()) 148 val flush = Input(Bool()) 149 // train input, only from load for now 150 val ld_in = Flipped(Vec(exuParameters.LduCnt, ValidIO(new LdPrefetchTrainBundle()))) 151 // filter out 152 val train_req = DecoupledIO(new PrefetchReqBundle()) 153 }) 154 155 class Ptr(implicit p: Parameters) extends CircularQueuePtr[Ptr]( p => size ){} 156 object Ptr { 157 def apply(f: Bool, v: UInt)(implicit p: Parameters): Ptr = { 158 val ptr = Wire(new Ptr) 159 ptr.flag := f 160 ptr.value := v 161 ptr 162 } 163 } 164 165 val entries = RegInit(VecInit(Seq.fill(size){ (0.U.asTypeOf(new PrefetchReqBundle())) })) 166 val valids = RegInit(VecInit(Seq.fill(size){ (false.B) })) 167 168 // enq 169 val enqLen = exuParameters.LduCnt 170 val enqPtrExt = RegInit(VecInit((0 until enqLen).map(_.U.asTypeOf(new Ptr)))) 171 val deqPtrExt = RegInit(0.U.asTypeOf(new Ptr)) 172 173 val deqPtr = WireInit(deqPtrExt.value) 174 175 require(size >= enqLen) 176 177 val ld_in_reordered = reorder(io.ld_in) 178 val reqs_l = ld_in_reordered.map(_.bits.asPrefetchReqBundle()) 179 val reqs_vl = ld_in_reordered.map(_.valid) 180 val needAlloc = Wire(Vec(enqLen, Bool())) 181 val canAlloc = Wire(Vec(enqLen, Bool())) 182 183 for(i <- (0 until enqLen)) { 184 val req = reqs_l(i) 185 val req_v = reqs_vl(i) 186 val index = PopCount(needAlloc.take(i)) 187 val allocPtr = enqPtrExt(index) 188 val entry_match = Cat(entries.zip(valids).map { 189 case(e, v) => v && block_hash_tag(e.vaddr) === block_hash_tag(req.vaddr) 190 }).orR 191 val prev_enq_match = if(i == 0) false.B else Cat(reqs_l.zip(reqs_vl).take(i).map { 192 case(pre, pre_v) => pre_v && block_hash_tag(pre.vaddr) === block_hash_tag(req.vaddr) 193 }).orR 194 195 needAlloc(i) := req_v && !entry_match && !prev_enq_match 196 canAlloc(i) := needAlloc(i) && allocPtr >= deqPtrExt && io.enable 197 198 when(canAlloc(i)) { 199 valids(allocPtr.value) := true.B 200 entries(allocPtr.value) := req 201 } 202 } 203 val allocNum = PopCount(canAlloc) 204 205 enqPtrExt.foreach{case x => x := x + allocNum} 206 207 // deq 208 io.train_req.valid := false.B 209 io.train_req.bits := DontCare 210 valids.zip(entries).zipWithIndex.foreach { 211 case((valid, entry), i) => { 212 when(deqPtr === i.U) { 213 io.train_req.valid := valid && io.enable 214 io.train_req.bits := entry 215 } 216 } 217 } 218 219 when(io.train_req.fire) { 220 valids(deqPtr) := false.B 221 deqPtrExt := deqPtrExt + 1.U 222 } 223 224 when(RegNext(io.flush)) { 225 valids.foreach {case valid => valid := false.B} 226 (0 until enqLen).map {case i => enqPtrExt(i) := i.U.asTypeOf(new Ptr)} 227 deqPtrExt := 0.U.asTypeOf(new Ptr) 228 } 229 230 XSPerfAccumulate(s"${name}_train_filter_full", PopCount(valids) === size.U) 231 XSPerfAccumulate(s"${name}_train_filter_half", PopCount(valids) >= (size / 2).U) 232 XSPerfAccumulate(s"${name}_train_filter_empty", PopCount(valids) === 0.U) 233 234 val raw_enq_pattern = Cat(reqs_vl) 235 val filtered_enq_pattern = Cat(needAlloc) 236 val actual_enq_pattern = Cat(canAlloc) 237 XSPerfAccumulate(s"${name}_train_filter_enq", allocNum > 0.U) 238 XSPerfAccumulate(s"${name}_train_filter_deq", io.train_req.fire) 239 for(i <- 0 until (1 << enqLen)) { 240 XSPerfAccumulate(s"${name}_train_filter_raw_enq_pattern_${toBinary(i)}", raw_enq_pattern === i.U) 241 XSPerfAccumulate(s"${name}_train_filter_filtered_enq_pattern_${toBinary(i)}", filtered_enq_pattern === i.U) 242 XSPerfAccumulate(s"${name}_train_filter_actual_enq_pattern_${toBinary(i)}", actual_enq_pattern === i.U) 243 } 244} 245 246class MLPReqFilterBundle(implicit p: Parameters) extends XSBundle with HasL1PrefetchHelper { 247 val tag = UInt(HASH_TAG_WIDTH.W) 248 val region = UInt(REGION_TAG_BITS.W) 249 val bit_vec = UInt(BIT_VEC_WITDH.W) 250 // NOTE: l1 will not use sent_vec, for making more prefetch reqs to l1 dcache 251 val sent_vec = UInt(BIT_VEC_WITDH.W) 252 val sink = UInt(SINK_BITS.W) 253 val alias = UInt(2.W) 254 val is_vaddr = Bool() 255 val source = new L1PrefetchSource() 256 257 def reset(index: Int) = { 258 tag := region_hash_tag(index.U) 259 region := index.U 260 bit_vec := 0.U 261 sent_vec := 0.U 262 sink := SINK_L1 263 alias := 0.U 264 is_vaddr := false.B 265 source.value := L1_HW_PREFETCH_NULL 266 } 267 268 def tag_match(new_tag: UInt): Bool = { 269 require(new_tag.getWidth == HASH_TAG_WIDTH) 270 tag === new_tag 271 } 272 273 def update(update_bit_vec: UInt, update_sink: UInt) = { 274 bit_vec := bit_vec | update_bit_vec 275 when(update_sink < sink) { 276 bit_vec := (bit_vec & ~sent_vec) | update_bit_vec 277 sink := update_sink 278 } 279 280 assert(PopCount(update_bit_vec) >= 1.U, "valid bits in update vector should greater than one") 281 } 282 283 def can_send_pf(): Bool = { 284 Mux( 285 sink === SINK_L1, 286 !is_vaddr && bit_vec.orR, 287 !is_vaddr && (bit_vec & ~sent_vec).orR 288 ) 289 } 290 291 def get_pf_addr(): UInt = { 292 require(PAddrBits <= VAddrBits) 293 require((region.getWidth + REGION_BITS + BLOCK_OFFSET) == VAddrBits) 294 295 val candidate = Mux( 296 sink === SINK_L1, 297 PriorityEncoder(bit_vec).asTypeOf(UInt(REGION_BITS.W)), 298 PriorityEncoder(bit_vec & ~sent_vec).asTypeOf(UInt(REGION_BITS.W)) 299 ) 300 Cat(region, candidate, 0.U(BLOCK_OFFSET.W)) 301 } 302 303 def get_tlb_va(): UInt = { 304 require((region.getWidth + REGION_TAG_OFFSET) == VAddrBits) 305 Cat(region, 0.U(REGION_TAG_OFFSET.W)) 306 } 307 308 def fromStreamPrefetchReqBundle(x : StreamPrefetchReqBundle): MLPReqFilterBundle = { 309 require(PAGE_OFFSET >= REGION_TAG_OFFSET, "region is greater than 4k, alias bit may be incorrect") 310 311 val res = Wire(new MLPReqFilterBundle) 312 res.tag := region_hash_tag(x.region) 313 res.region := x.region 314 res.bit_vec := x.bit_vec 315 res.sent_vec := 0.U 316 res.sink := x.sink 317 res.is_vaddr := true.B 318 res.source := x.source 319 res.alias := x.region(PAGE_OFFSET - REGION_TAG_OFFSET + 1, PAGE_OFFSET - REGION_TAG_OFFSET) 320 321 res 322 } 323 324 def invalidate() = { 325 // disable sending pf req 326 when(sink === SINK_L1) { 327 bit_vec := 0.U(BIT_VEC_WITDH.W) 328 }.otherwise { 329 sent_vec := ~(0.U(BIT_VEC_WITDH.W)) 330 } 331 // disable sending tlb req 332 is_vaddr := false.B 333 } 334} 335 336// there are 5 independent pipelines inside 337// 1. prefetch enqueue 338// 2. tlb request 339// 3. actual l1 prefetch 340// 4. actual l2 prefetch 341// 5. actual l3 prefetch 342class MutiLevelPrefetchFilter(implicit p: Parameters) extends XSModule with HasL1PrefetchHelper { 343 val io = IO(new XSBundle { 344 val enable = Input(Bool()) 345 val flush = Input(Bool()) 346 val prefetch_req = Flipped(ValidIO(new StreamPrefetchReqBundle)) 347 val tlb_req = new TlbRequestIO(nRespDups = 2) 348 val l1_req = DecoupledIO(new L1PrefetchReq()) 349 val l2_pf_addr = ValidIO(new L2PrefetchReq()) 350 val l3_pf_addr = ValidIO(UInt(PAddrBits.W)) // TODO: l3 pf source 351 val confidence = Input(UInt(1.W)) 352 val l2PfqBusy = Input(Bool()) 353 }) 354 355 val array = Reg(Vec(MLP_SIZE, new MLPReqFilterBundle)) 356 val replacement = ReplacementPolicy.fromString("plru", MLP_SIZE) 357 val tlb_req_arb = Module(new RRArbiterInit(new TlbReq, MLP_SIZE)) 358 val l1_pf_req_arb = Module(new RRArbiterInit(new L1PrefetchReq, MLP_SIZE)) 359 val l2_pf_req_arb = Module(new RRArbiterInit(new L2PrefetchReq, MLP_SIZE)) 360 val l3_pf_req_arb = Module(new RRArbiterInit(UInt(PAddrBits.W), MLP_SIZE)) 361 362 // enq 363 // s0: hash tag match 364 val s0_can_accept = Wire(Bool()) 365 val s0_valid = io.prefetch_req.valid && s0_can_accept 366 val s0_region = io.prefetch_req.bits.region 367 val s0_region_hash = region_hash_tag(s0_region) 368 val s0_match_vec = array.map(_.tag_match(s0_region_hash)) 369 val s0_hit = VecInit(s0_match_vec).asUInt.orR 370 val s0_index = Mux(s0_hit, OHToUInt(VecInit(s0_match_vec).asUInt), replacement.way) 371 val s0_prefetch_req = (new MLPReqFilterBundle).fromStreamPrefetchReqBundle(io.prefetch_req.bits) 372 373 when(s0_valid) { 374 replacement.access(s0_index) 375 } 376 377 assert(!s0_valid || PopCount(VecInit(s0_match_vec)) <= 1.U, "req region should match no more than 1 entry") 378 assert(!(s0_valid && RegNext(s0_valid) && !s0_hit && !RegNext(s0_hit) && replacement.way === RegNext(replacement.way)), "replacement error") 379 380 XSPerfAccumulate("s0_enq_fire", s0_valid) 381 XSPerfAccumulate("s0_enq_valid", io.prefetch_req.valid) 382 XSPerfAccumulate("s0_cannot_enq", io.prefetch_req.valid && !s0_can_accept) 383 384 // s1: alloc or update 385 val s1_valid = RegNext(s0_valid) 386 val s1_region = RegEnable(s0_region, s0_valid) 387 val s1_region_hash = RegEnable(s0_region_hash, s0_valid) 388 val s1_hit = RegEnable(s0_hit, s0_valid) 389 val s1_index = RegEnable(s0_index, s0_valid) 390 val s1_prefetch_req = RegEnable(s0_prefetch_req, s0_valid) 391 val s1_alloc = s1_valid && !s1_hit 392 val s1_update = s1_valid && s1_hit 393 s0_can_accept := !(s1_valid && s1_alloc && (s0_region_hash === s1_region_hash)) 394 395 when(s1_alloc) { 396 array(s1_index) := s1_prefetch_req 397 }.elsewhen(s1_update) { 398 array(s1_index).update( 399 update_bit_vec = s1_prefetch_req.bit_vec, 400 update_sink = s1_prefetch_req.sink 401 ) 402 } 403 404 // TODO: set this constraint looser to enable more kinds of depth 405 // assert(!(s0_valid && s1_valid && s0_region === s1_region), "s0 and s1 must have different region") 406 407 XSPerfAccumulate("s1_enq_valid", s1_valid) 408 XSPerfAccumulate("s1_enq_alloc", s1_alloc) 409 XSPerfAccumulate("s1_enq_update", s1_update) 410 XSPerfAccumulate("hash_conflict", s0_valid && RegNext(s1_valid) && (s0_region =/= RegNext(s1_region)) && (s0_region_hash === RegNext(s1_region_hash))) 411 412 // tlb req 413 // s0: arb all tlb reqs 414 val s0_tlb_fire_vec = VecInit((0 until MLP_SIZE).map{case i => tlb_req_arb.io.in(i).fire}) 415 val s1_tlb_fire_vec = RegNext(s0_tlb_fire_vec) 416 val s2_tlb_fire_vec = RegNext(s1_tlb_fire_vec) 417 418 for(i <- 0 until MLP_SIZE) { 419 val evict = s1_alloc && (s1_index === i.U) 420 tlb_req_arb.io.in(i).valid := array(i).is_vaddr && !s1_tlb_fire_vec(i) && !s2_tlb_fire_vec(i) && !evict 421 tlb_req_arb.io.in(i).bits.vaddr := array(i).get_tlb_va() 422 tlb_req_arb.io.in(i).bits.cmd := TlbCmd.read 423 tlb_req_arb.io.in(i).bits.size := 3.U 424 tlb_req_arb.io.in(i).bits.kill := false.B 425 tlb_req_arb.io.in(i).bits.no_translate := false.B 426 tlb_req_arb.io.in(i).bits.memidx := DontCare 427 tlb_req_arb.io.in(i).bits.debug := DontCare 428 } 429 430 assert(PopCount(s0_tlb_fire_vec) <= 1.U, "s0_tlb_fire_vec should be one-hot or empty") 431 432 // s1: send out the req 433 val s1_tlb_req_valid = RegNext(tlb_req_arb.io.out.valid) 434 val s1_tlb_req_bits = RegEnable(tlb_req_arb.io.out.bits, tlb_req_arb.io.out.valid) 435 val s1_tlb_req_index = RegEnable(OHToUInt(s0_tlb_fire_vec.asUInt), tlb_req_arb.io.out.valid) 436 val s1_tlb_evict = s1_alloc && (s1_index === s1_tlb_req_index) 437 io.tlb_req.req.valid := s1_tlb_req_valid && !s1_tlb_evict 438 io.tlb_req.req.bits := s1_tlb_req_bits 439 io.tlb_req.req_kill := false.B 440 tlb_req_arb.io.out.ready := true.B 441 442 XSPerfAccumulate("s1_tlb_req_sent", io.tlb_req.req.valid) 443 XSPerfAccumulate("s1_tlb_req_evict", s1_tlb_req_valid && s1_tlb_evict) 444 445 // s2: get response from tlb 446 val s2_tlb_resp = io.tlb_req.resp 447 val s2_tlb_update_index = RegEnable(s1_tlb_req_index, s1_tlb_req_valid) 448 val s2_tlb_evict = s1_alloc && (s1_index === s2_tlb_update_index) 449 when(s2_tlb_resp.valid && !s2_tlb_evict) { 450 array(s2_tlb_update_index).is_vaddr := s2_tlb_resp.bits.miss 451 452 when(!s2_tlb_resp.bits.miss) { 453 array(s2_tlb_update_index).region := Cat(0.U((VAddrBits - PAddrBits).W), s2_tlb_resp.bits.paddr.head(s2_tlb_resp.bits.paddr.head.getWidth - 1, REGION_TAG_OFFSET)) 454 when(s2_tlb_resp.bits.excp.head.pf.ld || s2_tlb_resp.bits.excp.head.af.ld) { 455 array(s2_tlb_update_index).invalidate() 456 } 457 } 458 } 459 s2_tlb_resp.ready := true.B 460 461 XSPerfAccumulate("s2_tlb_resp_valid", s2_tlb_resp.valid) 462 XSPerfAccumulate("s2_tlb_resp_evict", s2_tlb_resp.valid && s2_tlb_evict) 463 XSPerfAccumulate("s2_tlb_resp_miss", s2_tlb_resp.valid && !s2_tlb_evict && s2_tlb_resp.bits.miss) 464 XSPerfAccumulate("s2_tlb_resp_updated", s2_tlb_resp.valid && !s2_tlb_evict && !s2_tlb_resp.bits.miss) 465 XSPerfAccumulate("s2_tlb_resp_page_fault", s2_tlb_resp.valid && !s2_tlb_evict && !s2_tlb_resp.bits.miss && s2_tlb_resp.bits.excp.head.pf.ld) 466 XSPerfAccumulate("s2_tlb_resp_access_fault", s2_tlb_resp.valid && !s2_tlb_evict && !s2_tlb_resp.bits.miss && s2_tlb_resp.bits.excp.head.af.ld) 467 468 // l1 pf 469 // s0: generate prefetch req paddr per entry, arb them 470 val s0_pf_fire_vec = VecInit((0 until MLP_SIZE).map{case i => l1_pf_req_arb.io.in(i).fire}) 471 val s1_pf_fire_vec = RegNext(s0_pf_fire_vec) 472 473 val s0_pf_fire = l1_pf_req_arb.io.out.fire 474 val s0_pf_index = l1_pf_req_arb.io.chosen 475 val s0_pf_candidate_oh = get_candidate_oh(l1_pf_req_arb.io.out.bits.paddr) 476 477 for(i <- 0 until MLP_SIZE) { 478 val evict = s1_alloc && (s1_index === i.U) 479 l1_pf_req_arb.io.in(i).valid := array(i).can_send_pf() && (array(i).sink === SINK_L1) && !evict 480 l1_pf_req_arb.io.in(i).bits.paddr := array(i).get_pf_addr() 481 l1_pf_req_arb.io.in(i).bits.alias := array(i).alias 482 l1_pf_req_arb.io.in(i).bits.confidence := io.confidence 483 l1_pf_req_arb.io.in(i).bits.is_store := false.B 484 l1_pf_req_arb.io.in(i).bits.pf_source := array(i).source 485 } 486 487 when(s0_pf_fire) { 488 array(s0_pf_index).sent_vec := array(s0_pf_index).sent_vec | s0_pf_candidate_oh 489 } 490 491 assert(PopCount(s0_pf_fire_vec) <= 1.U, "s0_pf_fire_vec should be one-hot or empty") 492 493 // s1: send out to dcache 494 val s1_pf_valid = Reg(Bool()) 495 val s1_pf_bits = RegEnable(l1_pf_req_arb.io.out.bits, l1_pf_req_arb.io.out.fire) 496 val s1_pf_index = RegEnable(s0_pf_index, l1_pf_req_arb.io.out.fire) 497 val s1_pf_candidate_oh = RegEnable(s0_pf_candidate_oh, l1_pf_req_arb.io.out.fire) 498 val s1_pf_evict = s1_alloc && (s1_index === s1_pf_index) 499 val s1_pf_update = s1_update && (s1_index === s1_pf_index) 500 val s1_pf_can_go = io.l1_req.ready && !s1_pf_evict && !s1_pf_update 501 val s1_pf_fire = s1_pf_valid && s1_pf_can_go 502 503 when(s1_pf_can_go) { 504 s1_pf_valid := false.B 505 } 506 507 when(l1_pf_req_arb.io.out.fire) { 508 s1_pf_valid := true.B 509 } 510 511 when(s1_pf_fire) { 512 array(s1_pf_index).bit_vec := array(s1_pf_index).bit_vec & ~s1_pf_candidate_oh 513 } 514 515 io.l1_req.valid := s1_pf_valid && !s1_pf_evict && !s1_pf_update && (s1_pf_bits.paddr >= 0x80000000L.U) && io.enable 516 io.l1_req.bits := s1_pf_bits 517 518 l1_pf_req_arb.io.out.ready := s1_pf_can_go || !s1_pf_valid 519 520 assert(!((s1_alloc || s1_update) && s1_pf_fire && (s1_index === s1_pf_index)), "pf pipeline & enq pipeline bit_vec harzard!") 521 522 XSPerfAccumulate("s1_pf_valid", s1_pf_valid) 523 XSPerfAccumulate("s1_pf_block_by_pipe_unready", s1_pf_valid && !io.l1_req.ready) 524 XSPerfAccumulate("s1_pf_block_by_enq_alloc_harzard", s1_pf_valid && s1_pf_evict) 525 XSPerfAccumulate("s1_pf_block_by_enq_update_harzard", s1_pf_valid && s1_pf_update) 526 XSPerfAccumulate("s1_pf_fire", s1_pf_fire) 527 528 // l2 pf 529 // s0: generate prefetch req paddr per entry, arb them, sent out 530 io.l2_pf_addr.valid := l2_pf_req_arb.io.out.valid 531 io.l2_pf_addr.bits := l2_pf_req_arb.io.out.bits 532 533 l2_pf_req_arb.io.out.ready := true.B 534 535 for(i <- 0 until MLP_SIZE) { 536 val evict = s1_alloc && (s1_index === i.U) 537 l2_pf_req_arb.io.in(i).valid := array(i).can_send_pf() && (array(i).sink === SINK_L2) && !evict 538 l2_pf_req_arb.io.in(i).bits.addr := array(i).get_pf_addr() 539 l2_pf_req_arb.io.in(i).bits.source := MuxLookup(array(i).source.value, MemReqSource.Prefetch2L2Unknown.id.U, Seq( 540 L1_HW_PREFETCH_STRIDE -> MemReqSource.Prefetch2L2Stride.id.U, 541 L1_HW_PREFETCH_STREAM -> MemReqSource.Prefetch2L2Stream.id.U 542 )) 543 } 544 545 when(l2_pf_req_arb.io.out.valid) { 546 array(l2_pf_req_arb.io.chosen).sent_vec := array(l2_pf_req_arb.io.chosen).sent_vec | get_candidate_oh(l2_pf_req_arb.io.out.bits.addr) 547 } 548 549 // last level cache pf 550 // s0: generate prefetch req paddr per entry, arb them, sent out 551 io.l3_pf_addr.valid := l3_pf_req_arb.io.out.valid 552 io.l3_pf_addr.bits := l3_pf_req_arb.io.out.bits 553 554 l3_pf_req_arb.io.out.ready := true.B 555 556 for(i <- 0 until MLP_SIZE) { 557 val evict = s1_alloc && (s1_index === i.U) 558 l3_pf_req_arb.io.in(i).valid := array(i).can_send_pf() && (array(i).sink === SINK_L3) && !evict 559 l3_pf_req_arb.io.in(i).bits := array(i).get_pf_addr() 560 } 561 562 when(l3_pf_req_arb.io.out.valid) { 563 array(l3_pf_req_arb.io.chosen).sent_vec := array(l3_pf_req_arb.io.chosen).sent_vec | get_candidate_oh(l3_pf_req_arb.io.out.bits) 564 } 565 566 // reset meta to avoid muti-hit problem 567 for(i <- 0 until MLP_SIZE) { 568 when(reset.asBool || RegNext(io.flush)) { 569 array(i).reset(i) 570 } 571 } 572 573 XSPerfAccumulate("l2_prefetche_queue_busby", io.l2PfqBusy) 574 XSPerfHistogram("filter_active", PopCount(VecInit(array.map(_.can_send_pf())).asUInt), true.B, 0, MLP_SIZE, 1) 575 XSPerfHistogram("l1_filter_active", PopCount(VecInit(array.map(x => x.can_send_pf() && (x.sink === SINK_L1))).asUInt), true.B, 0, MLP_SIZE, 1) 576 XSPerfHistogram("l2_filter_active", PopCount(VecInit(array.map(x => x.can_send_pf() && (x.sink === SINK_L2))).asUInt), true.B, 0, MLP_SIZE, 1) 577 XSPerfHistogram("l3_filter_active", PopCount(VecInit(array.map(x => x.can_send_pf() && (x.sink === SINK_L3))).asUInt), true.B, 0, MLP_SIZE, 1) 578} 579 580class L1Prefetcher(implicit p: Parameters) extends BasePrefecher with HasStreamPrefetchHelper with HasStridePrefetchHelper { 581 val pf_ctrl = IO(Input(new PrefetchControlBundle)) 582 val stride_train = IO(Flipped(Vec(exuParameters.LduCnt, ValidIO(new LdPrefetchTrainBundle())))) 583 val l2PfqBusy = IO(Input(Bool())) 584 585 val stride_train_filter = Module(new TrainFilter(STRIDE_FILTER_SIZE, "stride")) 586 val stride_meta_array = Module(new StrideMetaArray) 587 val stream_train_filter = Module(new TrainFilter(STREAM_FILTER_SIZE, "stream")) 588 val stream_bit_vec_array = Module(new StreamBitVectorArray) 589 val pf_queue_filter = Module(new MutiLevelPrefetchFilter) 590 591 // for now, if the stream is disabled, train and prefetch process will continue, without sending out and reqs 592 val enable = io.enable 593 val flush = pf_ctrl.flush 594 595 stream_train_filter.io.ld_in.zipWithIndex.foreach { 596 case (ld_in, i) => { 597 ld_in.valid := io.ld_in(i).valid && enable 598 ld_in.bits := io.ld_in(i).bits 599 } 600 } 601 stream_train_filter.io.enable := enable 602 stream_train_filter.io.flush := flush 603 604 stride_train_filter.io.ld_in.zipWithIndex.foreach { 605 case (ld_in, i) => { 606 ld_in.valid := stride_train(i).valid && enable 607 ld_in.bits := stride_train(i).bits 608 } 609 } 610 stride_train_filter.io.enable := enable 611 stride_train_filter.io.flush := flush 612 613 stream_bit_vec_array.io.enable := enable 614 stream_bit_vec_array.io.flush := flush 615 stream_bit_vec_array.io.dynamic_depth := pf_ctrl.dynamic_depth 616 stream_bit_vec_array.io.train_req <> stream_train_filter.io.train_req 617 618 stride_meta_array.io.enable := enable 619 stride_meta_array.io.flush := flush 620 stride_meta_array.io.dynamic_depth := 0.U 621 stride_meta_array.io.train_req <> stride_train_filter.io.train_req 622 stride_meta_array.io.stream_lookup_req <> stream_bit_vec_array.io.stream_lookup_req 623 stride_meta_array.io.stream_lookup_resp <> stream_bit_vec_array.io.stream_lookup_resp 624 625 // stream has higher priority than stride 626 pf_queue_filter.io.prefetch_req.valid := stream_bit_vec_array.io.prefetch_req.valid || stride_meta_array.io.prefetch_req.valid 627 pf_queue_filter.io.prefetch_req.bits := Mux( 628 stream_bit_vec_array.io.prefetch_req.valid, 629 stream_bit_vec_array.io.prefetch_req.bits, 630 stride_meta_array.io.prefetch_req.bits 631 ) 632 633 io.l1_req.valid := pf_queue_filter.io.l1_req.valid && enable && pf_ctrl.enable 634 io.l1_req.bits := pf_queue_filter.io.l1_req.bits 635 636 pf_queue_filter.io.l1_req.ready := Mux(pf_ctrl.enable, io.l1_req.ready, true.B) 637 pf_queue_filter.io.tlb_req <> io.tlb_req 638 pf_queue_filter.io.enable := enable 639 pf_queue_filter.io.flush := flush 640 pf_queue_filter.io.confidence := pf_ctrl.confidence 641 pf_queue_filter.io.l2PfqBusy := l2PfqBusy 642 643 io.l2_req.valid := pf_queue_filter.io.l2_pf_addr.valid && pf_queue_filter.io.l2_pf_addr.bits.addr > 0x80000000L.U && enable && pf_ctrl.enable 644 io.l2_req.bits := pf_queue_filter.io.l2_pf_addr.bits 645 646 io.l3_req.valid := pf_queue_filter.io.l3_pf_addr.valid && pf_queue_filter.io.l3_pf_addr.bits > 0x80000000L.U && enable && pf_ctrl.enable 647 io.l3_req.bits := pf_queue_filter.io.l3_pf_addr.bits 648}