xref: /XiangShan/src/main/scala/xiangshan/cache/CacheInstruction.scala (revision 0184a80eb583a3e9ddee476464bc331bb2e09785)
1package xiangshan.cache
2
3import chisel3._
4import chisel3.util._
5import xiangshan._
6import xiangshan.frontend.icache._
7import utility._
8import org.chipsalliance.cde.config.Parameters
9import xiangshan.backend.fu.util.HasCSRConst
10
11object CacheOpMap{
12  def apply(opcode: String, optype: String,  name: String ): Map[String, String] = {
13    Map(
14      "opcode" -> opcode,
15      "optype" -> optype,
16      "name"   -> name,
17    )
18  }
19}
20
21object CacheRegMap{
22  def apply(offset: String,  width: String, authority: String, name: String ): (String, Map[String, String]) = {
23    name -> Map(
24      "offset" -> offset,
25      "width"  -> width,
26      "authority" -> authority,
27    )
28  }
29}
30
31trait CacheControlConst{
32  def maxDataRowSupport = 8
33}
34
35abstract class CacheCtrlModule(implicit p: Parameters) extends XSModule with HasCSRConst with CacheControlConst with HasDCacheParameters
36
37object CacheInstrucion{
38  def CacheOperation = List(
39    CacheOpMap("b00000", "CHECK",  "READ_TAG_ECC"),
40    CacheOpMap("b00001", "CHECK",  "READ_DATA_ECC"),
41    CacheOpMap("b00010", "LOAD",   "READ_TAG"),
42    CacheOpMap("b00011", "LOAD",   "READ_DATA"),
43    CacheOpMap("b00100", "STORE",  "WRITE_TAG_ECC"),
44    CacheOpMap("b00101", "STORE",  "WRITE_DATA_ECC"),
45    CacheOpMap("b00110", "STORE",  "WRITE_TAG"),
46    CacheOpMap("b00111", "STORE",  "WRITE_DATA"),
47    CacheOpMap("b01000", "FLUSH",  "FLUSH_BLOCK")
48  )
49
50  def CacheInsRegisterList = Map(
51    //         offset     width    authority  name
52    CacheRegMap("0",      "64",    "RW",      "CACHE_OP"),
53    CacheRegMap("1",      "64",    "RW",      "OP_FINISH"),
54    CacheRegMap("2",      "64",    "RW",      "CACHE_LEVEL"),
55    CacheRegMap("3",      "64",    "RW",      "CACHE_WAY"),
56    CacheRegMap("4",      "64",    "RW",      "CACHE_IDX"),
57    CacheRegMap("5",      "64",    "RW",      "CACHE_BANK_NUM"),
58    CacheRegMap("6",      "64",    "RW",      "CACHE_TAG_ECC"),
59    CacheRegMap("7",      "64",    "RW",      "CACHE_TAG_BITS"), // TODO
60    CacheRegMap("8",      "64",    "RW",      "CACHE_TAG_LOW"),
61    CacheRegMap("9",      "64",    "RW",      "CACHE_TAG_HIGH"), // not used in 64 bit arch
62    CacheRegMap("10",     "64",    "RW",      "CACHE_ECC_WIDTH"), // TODO
63    CacheRegMap("11",     "64",    "RW",      "CACHE_DATA_ECC"),
64    CacheRegMap("12",     "64",    "RW",      "CACHE_DATA_0"),
65    CacheRegMap("13",     "64",    "RW",      "CACHE_DATA_1"),
66    CacheRegMap("14",     "64",    "RW",      "CACHE_DATA_2"),
67    CacheRegMap("15",     "64",    "RW",      "CACHE_DATA_3"),
68    CacheRegMap("16",     "64",    "RW",      "CACHE_DATA_4"),
69    CacheRegMap("17",     "64",    "RW",      "CACHE_DATA_5"),
70    CacheRegMap("18",     "64",    "RW",      "CACHE_DATA_6"),
71    CacheRegMap("19",     "64",    "RW",      "CACHE_DATA_7"),
72    CacheRegMap("20",     "64",    "RW",      "CACHE_ERROR"),
73  )
74
75  // Usage:
76  // val cacheopMapping = CacheInstrucion.CacheInsRegisterList.map{case (name, attribute) => {
77  //   doSthWith(name, attribute("offset"), attribute("width"))
78  // }}
79
80  def COP_CHECK = 0.U
81  def COP_LOAD  = 1.U
82  def COP_STORE = 2.U
83  def COP_FLUSH = 3.U
84
85  def COP_ID_ICACHE = 0
86  def COP_ID_DCACHE = 1
87
88  def COP_RESULT_CODE_IDLE = 0.U
89  def COP_RESULT_CODE_OK = 1.U
90  def COP_RESULT_CODE_ERROR = 2.U
91
92  def isReadTagECC(opcode: UInt)            = opcode === "b00000".U
93  def isReadDataECC(opcode: UInt)           = opcode === "b00001".U
94  def isReadTag(opcode: UInt)               = opcode === "b00010".U
95  def isReadData(opcode: UInt)              = opcode === "b00011".U
96  def isWriteTagECC(opcode: UInt)           = opcode === "b00100".U
97  def isWriteDataECC(opcode: UInt)          = opcode === "b00101".U
98  def isWriteTag(opcode: UInt)              = opcode === "b00110".U
99  def isWriteData(opcode: UInt)             = opcode === "b00111".U
100  def isFlush(opcode: UInt)                 = opcode === "b01000".U
101
102  def isReadOp(opcode: UInt) = isReadTagECC(opcode) ||
103    isReadDataECC(opcode) ||
104    isReadTag(opcode) ||
105    isReadData(opcode)
106}
107
108class CacheCtrlReqInfo(implicit p: Parameters) extends XSBundle with CacheControlConst {
109  val level           = UInt(XLEN.W) // op target id
110  val wayNum          = UInt(XLEN.W)
111  val index           = UInt(XLEN.W)
112  val opCode          = UInt(XLEN.W)
113  val write_tag_high  = UInt(XLEN.W)
114  val write_tag_low   = UInt(XLEN.W)
115  val write_tag_ecc   = UInt(XLEN.W)
116  val write_data_vec  = Vec(maxDataRowSupport, UInt(XLEN.W))
117  val write_data_ecc  = UInt(XLEN.W)
118  val bank_num         = UInt(XLEN.W)
119}
120
121class CacheCtrlRespInfo(implicit p: Parameters) extends XSBundle with HasICacheParameters with CacheControlConst{
122  val read_tag_high  = UInt(XLEN.W)
123  val read_tag_low   = UInt(XLEN.W)
124  val read_tag_ecc   = UInt(XLEN.W)
125  val read_data_vec  = Vec(maxDataRowSupport, UInt(XLEN.W))
126  val read_data_ecc  = UInt(XLEN.W)
127  val bank_num        = UInt(XLEN.W)
128}
129
130class L1CacheToCsrIO(implicit p: Parameters) extends DCacheBundle {
131  val distribute_csr = Flipped(new DistributedCSRIO)
132  val update = new DistributedCSRUpdateReq
133}
134
135class L1CacheInnerOpIO(implicit p: Parameters) extends DCacheBundle {
136  val req  = Valid(new CacheCtrlReqInfo)
137  val resp = Flipped(Valid(new CacheCtrlRespInfo))
138}
139
140class CSRCacheOpDecoder(decoder_name: String, id: Int)(implicit p: Parameters) extends CacheCtrlModule {
141  val io = IO(new Bundle {
142    val csr = new L1CacheToCsrIO
143    val cache = new L1CacheInnerOpIO
144    val cache_req_dup = Vec(DCacheDupNum, Valid(new CacheCtrlReqInfo))
145    val cacheOp_req_bits_opCode_dup = Output(Vec(DCacheDupNum, UInt(XLEN.W)))
146    val error = Flipped(ValidIO(new L1CacheErrorInfo))
147  })
148
149  // CSRCacheOpDecoder state
150  val wait_csr_op_req = RegInit(true.B) // waiting for csr "CACHE_OP" being write
151  val wait_cache_op_resp = RegInit(false.B) // waiting for dcache to finish dcache op
152  val schedule_csr_op_resp_data = RegInit(false.B) // ready to write data readed from cache back to csr
153  val schedule_csr_op_resp_finish = RegInit(false.B) // ready to write "OP_FINISH" csr
154  // val cache_op_resp_timer = RegInit(0.U(4.W))
155  val data_transfer_finished = WireInit(false.B)
156  val data_transfer_cnt = RegInit(0.U(log2Up(maxDataRowSupport).W))
157
158  // Translate CSR write to cache op
159  val translated_cache_req = Reg(new CacheCtrlReqInfo)
160  val translated_cache_req_opCode_dup = Reg(Vec(DCacheDupNum, UInt(XLEN.W)))
161  println("Cache op decoder (" + decoder_name + "):")
162  println("  Id " + id)
163  // CacheInsRegisterList.map{case (name, attribute) => {
164  //   println("  Register CSR mirror " + name)
165  // }}
166
167  def cacheop_csr_is_being_write(csr_name: String): Bool = {
168    io.csr.distribute_csr.w.bits.addr === (CacheInstrucion.CacheInsRegisterList(csr_name)("offset").toInt + Scachebase).U &&
169      io.csr.distribute_csr.w.valid
170  }
171
172  def update_cache_req_when_write(csr_name: String, req_field: Data) = {
173    when(
174      cacheop_csr_is_being_write(csr_name)
175    ){
176      req_field := io.csr.distribute_csr.w.bits.data
177      assert(wait_csr_op_req)
178    }
179  }
180
181  update_cache_req_when_write("CACHE_OP", translated_cache_req.opCode)
182  translated_cache_req_opCode_dup.map(dup => update_cache_req_when_write("CACHE_OP", dup))
183  update_cache_req_when_write("CACHE_LEVEL", translated_cache_req.level)
184  update_cache_req_when_write("CACHE_WAY", translated_cache_req.wayNum)
185  update_cache_req_when_write("CACHE_IDX", translated_cache_req.index)
186  update_cache_req_when_write("CACHE_BANK_NUM", translated_cache_req.bank_num)
187  update_cache_req_when_write("CACHE_TAG_HIGH", translated_cache_req.write_tag_high)
188  update_cache_req_when_write("CACHE_TAG_LOW", translated_cache_req.write_tag_low)
189  update_cache_req_when_write("CACHE_TAG_ECC", translated_cache_req.write_tag_ecc)
190  update_cache_req_when_write("CACHE_DATA_0", translated_cache_req.write_data_vec(0))
191  update_cache_req_when_write("CACHE_DATA_1", translated_cache_req.write_data_vec(1))
192  update_cache_req_when_write("CACHE_DATA_2", translated_cache_req.write_data_vec(2))
193  update_cache_req_when_write("CACHE_DATA_3", translated_cache_req.write_data_vec(3))
194  update_cache_req_when_write("CACHE_DATA_4", translated_cache_req.write_data_vec(4))
195  update_cache_req_when_write("CACHE_DATA_5", translated_cache_req.write_data_vec(5))
196  update_cache_req_when_write("CACHE_DATA_6", translated_cache_req.write_data_vec(6))
197  update_cache_req_when_write("CACHE_DATA_7", translated_cache_req.write_data_vec(7))
198  update_cache_req_when_write("CACHE_DATA_ECC", translated_cache_req.write_data_ecc)
199
200  val cache_op_start = WireInit(cacheop_csr_is_being_write("CACHE_OP") && id.U === translated_cache_req.level)
201  when(cache_op_start) {
202    wait_csr_op_req := false.B
203  }
204
205  // Send cache op to cache
206  io.cache.req.valid := RegNext(cache_op_start)
207  io.cache_req_dup.map( dup => dup.valid := RegNext(cache_op_start) )
208  io.cache.req.bits := translated_cache_req
209  io.cache_req_dup.map( dup => dup.bits := translated_cache_req )
210  when(io.cache.req.fire){
211    wait_cache_op_resp := true.B
212  }
213
214  io.cacheOp_req_bits_opCode_dup.zipWithIndex.map{ case (dup, i) => dup := translated_cache_req_opCode_dup(i) }
215
216  // Receive cache op resp from cache
217  val raw_cache_resp = Reg(new CacheCtrlRespInfo)
218  when(io.cache.resp.fire){
219    wait_cache_op_resp := false.B
220    raw_cache_resp := io.cache.resp.bits
221    when(CacheInstrucion.isReadOp(translated_cache_req.opCode)){
222      schedule_csr_op_resp_data := true.B
223      schedule_csr_op_resp_finish := false.B
224      assert(data_transfer_cnt === 0.U)
225    }.otherwise{
226      schedule_csr_op_resp_data := false.B
227      schedule_csr_op_resp_finish := true.B
228    }
229  }
230
231  // Translate cache op resp to CSR write, send it back to CSR
232  when(io.csr.update.w.fire && schedule_csr_op_resp_data && data_transfer_finished){
233    schedule_csr_op_resp_data := false.B
234    schedule_csr_op_resp_finish := true.B
235  }
236  when(io.csr.update.w.fire && schedule_csr_op_resp_finish){
237    schedule_csr_op_resp_finish := false.B
238    wait_csr_op_req := true.B
239  }
240
241  io.csr.update.w.valid := schedule_csr_op_resp_data || schedule_csr_op_resp_finish
242  io.csr.update.w.bits := DontCare
243
244  val isReadTagECC = WireInit(CacheInstrucion.isReadTagECC(translated_cache_req_opCode_dup(0)))
245  val isReadDataECC = WireInit(CacheInstrucion.isReadDataECC(translated_cache_req_opCode_dup(0)))
246  val isReadTag = WireInit(CacheInstrucion.isReadTag(translated_cache_req.opCode))
247  val isReadData = WireInit(CacheInstrucion.isReadData(translated_cache_req.opCode))
248
249  when(schedule_csr_op_resp_data){
250    io.csr.update.w.bits.addr := Mux1H(List(
251      isReadTagECC -> (CacheInstrucion.CacheInsRegisterList("CACHE_TAG_ECC")("offset").toInt + Scachebase).U,
252      isReadDataECC -> (CacheInstrucion.CacheInsRegisterList("CACHE_DATA_ECC")("offset").toInt + Scachebase).U,
253      isReadTag -> ((CacheInstrucion.CacheInsRegisterList("CACHE_TAG_LOW")("offset").toInt + Scachebase).U + data_transfer_cnt),
254      isReadData -> ((CacheInstrucion.CacheInsRegisterList("CACHE_DATA_0")("offset").toInt + Scachebase).U + data_transfer_cnt),
255    ))
256    io.csr.update.w.bits.data := Mux1H(List(
257      isReadTagECC -> raw_cache_resp.read_tag_ecc,
258      isReadDataECC -> raw_cache_resp.read_data_ecc,
259      isReadTag -> raw_cache_resp.read_tag_low,
260      isReadData -> raw_cache_resp.read_data_vec(data_transfer_cnt),
261    ))
262    data_transfer_finished := Mux(isReadData,
263      data_transfer_cnt === (maxDataRowSupport-1).U,
264      true.B
265    )
266    data_transfer_cnt := data_transfer_cnt + 1.U
267  }
268
269  when(schedule_csr_op_resp_finish){
270    io.csr.update.w.bits.addr := (CacheInstrucion.CacheInsRegisterList("OP_FINISH")("offset").toInt + Scachebase).U
271    io.csr.update.w.bits.data := CacheInstrucion.COP_RESULT_CODE_OK
272    data_transfer_cnt := 0.U
273  }
274
275  val error = DelayNWithValid(io.error, 1)
276  when(error.bits.report_to_beu && error.valid) {
277    io.csr.update.w.bits.addr := (CacheInstrucion.CacheInsRegisterList("CACHE_ERROR")("offset").toInt + Scachebase).U
278    io.csr.update.w.bits.data := error.asUInt
279    io.csr.update.w.valid := true.B
280  }
281}
282
283class CSRCacheErrorDecoder(implicit p: Parameters) extends CacheCtrlModule {
284  val io = IO(new Bundle{
285    val encoded_cache_error = Input(UInt())
286  })
287  val encoded_cache_error = io.encoded_cache_error
288  def print_cache_error_flag(flag: Bool, desc: String) = {
289    when(flag){
290      printf("  " + desc + "\n")
291    }
292  }
293  val decoded_cache_error = WireInit(encoded_cache_error.asTypeOf(ValidIO(new L1CacheErrorInfo)))
294  when(decoded_cache_error.valid && !RegNext(decoded_cache_error.valid)){
295    printf("CACHE_ERROR CSR reported an error:\n")
296    printf("  paddr 0x%x\n", decoded_cache_error.bits.paddr)
297    print_cache_error_flag(decoded_cache_error.bits.report_to_beu, "report to bus error unit")
298    print_cache_error_flag(decoded_cache_error.bits.source.tag, "tag")
299    print_cache_error_flag(decoded_cache_error.bits.source.data, "data")
300    print_cache_error_flag(decoded_cache_error.bits.source.l2, "l2")
301    print_cache_error_flag(decoded_cache_error.bits.opType.fetch, "fetch")
302    print_cache_error_flag(decoded_cache_error.bits.opType.load, "load")
303    print_cache_error_flag(decoded_cache_error.bits.opType.store, "store")
304    print_cache_error_flag(decoded_cache_error.bits.opType.probe, "probe")
305    print_cache_error_flag(decoded_cache_error.bits.opType.release, "release")
306    print_cache_error_flag(decoded_cache_error.bits.opType.atom, "atom")
307    printf("It should not happen in normal execution flow\n")
308  }
309}
310