2016-11-28 01:16:37 +01:00
|
|
|
// See LICENSE.Berkeley for license details.
|
|
|
|
// See LICENSE.SiFive for license details.
|
|
|
|
|
2012-02-26 02:09:26 +01:00
|
|
|
package rocket
|
2011-10-26 08:02:47 +02:00
|
|
|
|
2012-10-08 05:15:54 +02:00
|
|
|
import Chisel._
|
2016-12-13 02:38:55 +01:00
|
|
|
import config._
|
|
|
|
import diplomacy._
|
2017-02-09 22:59:09 +01:00
|
|
|
import tile._
|
2016-12-13 02:38:55 +01:00
|
|
|
import uncore.tilelink2._
|
2017-02-09 22:59:09 +01:00
|
|
|
import uncore.util.Code
|
2016-09-28 06:27:07 +02:00
|
|
|
import util._
|
2016-09-29 01:10:32 +02:00
|
|
|
import Chisel.ImplicitConversions._
|
2011-10-26 08:02:47 +02:00
|
|
|
|
2017-02-09 22:59:09 +01:00
|
|
|
case class ICacheParams(
|
|
|
|
nSets: Int = 64,
|
|
|
|
nWays: Int = 4,
|
|
|
|
rowBits: Int = 128,
|
2017-03-20 09:29:26 +01:00
|
|
|
nTLBEntries: Int = 32,
|
2017-02-09 22:59:09 +01:00
|
|
|
cacheIdBits: Int = 0,
|
2017-03-07 06:35:45 +01:00
|
|
|
ecc: Option[Code] = None,
|
|
|
|
blockBytes: Int = 64) extends L1CacheParams {
|
2017-02-09 22:59:09 +01:00
|
|
|
def replacement = new RandomReplacement(nWays)
|
2014-09-01 22:28:58 +02:00
|
|
|
}
|
2014-08-12 03:36:23 +02:00
|
|
|
|
2017-02-09 22:59:09 +01:00
|
|
|
trait HasL1ICacheParameters extends HasL1CacheParameters with HasCoreParameters {
|
|
|
|
val cacheParams = tileParams.icache.get
|
|
|
|
}
|
|
|
|
|
|
|
|
class ICacheReq(implicit p: Parameters) extends CoreBundle()(p) with HasL1ICacheParameters {
|
2016-05-24 02:51:08 +02:00
|
|
|
val addr = UInt(width = vaddrBits)
|
2013-08-12 19:39:11 +02:00
|
|
|
}
|
|
|
|
|
2017-02-09 22:59:09 +01:00
|
|
|
class ICacheResp(implicit p: Parameters) extends CoreBundle()(p) with HasL1ICacheParameters {
|
2014-09-01 22:28:58 +02:00
|
|
|
val data = Bits(width = coreInstBits)
|
2014-08-12 03:36:23 +02:00
|
|
|
val datablock = Bits(width = rowBits)
|
2013-08-12 19:39:11 +02:00
|
|
|
}
|
|
|
|
|
2016-12-13 02:38:55 +01:00
|
|
|
class ICache(val latency: Int)(implicit p: Parameters) extends LazyModule {
|
|
|
|
lazy val module = new ICacheModule(this)
|
|
|
|
val node = TLClientNode(TLClientParameters(sourceId = IdRange(0,1)))
|
|
|
|
}
|
|
|
|
|
|
|
|
class ICacheBundle(outer: ICache) extends CoreBundle()(outer.p) {
|
|
|
|
val req = Valid(new ICacheReq).flip
|
2017-03-06 06:43:20 +01:00
|
|
|
val s1_paddr = UInt(INPUT, paddrBits) // delayed one cycle w.r.t. req
|
2016-12-13 02:38:55 +01:00
|
|
|
val s1_kill = Bool(INPUT) // delayed one cycle w.r.t. req
|
|
|
|
val s2_kill = Bool(INPUT) // delayed two cycles; prevents I$ miss emission
|
|
|
|
|
|
|
|
val resp = Decoupled(new ICacheResp)
|
|
|
|
val invalidate = Bool(INPUT)
|
|
|
|
val mem = outer.node.bundleOut
|
|
|
|
}
|
|
|
|
|
|
|
|
class ICacheModule(outer: ICache) extends LazyModuleImp(outer)
|
2017-02-09 22:59:09 +01:00
|
|
|
with HasL1ICacheParameters {
|
2016-12-13 02:38:55 +01:00
|
|
|
val io = new ICacheBundle(outer)
|
|
|
|
val edge = outer.node.edgesOut(0)
|
|
|
|
val tl_out = io.mem(0)
|
2016-04-02 04:30:39 +02:00
|
|
|
|
2014-08-08 21:23:02 +02:00
|
|
|
require(isPow2(nSets) && isPow2(nWays))
|
2014-09-01 22:28:58 +02:00
|
|
|
require(isPow2(coreInstBytes))
|
2016-05-24 02:51:08 +02:00
|
|
|
require(!usingVM || pgIdxBits >= untagBits)
|
2012-01-25 00:13:49 +01:00
|
|
|
|
2013-09-10 19:51:35 +02:00
|
|
|
val s_ready :: s_request :: s_refill_wait :: s_refill :: Nil = Enum(UInt(), 4)
|
2013-08-16 00:28:15 +02:00
|
|
|
val state = Reg(init=s_ready)
|
2013-08-12 19:39:11 +02:00
|
|
|
val invalidated = Reg(Bool())
|
2012-10-10 06:35:03 +02:00
|
|
|
val stall = !io.resp.ready
|
|
|
|
|
2015-04-22 20:26:03 +02:00
|
|
|
val refill_addr = Reg(UInt(width = paddrBits))
|
2016-01-12 21:42:57 +01:00
|
|
|
val s1_any_tag_hit = Wire(Bool())
|
2011-10-26 08:02:47 +02:00
|
|
|
|
2013-08-16 00:28:15 +02:00
|
|
|
val s1_valid = Reg(init=Bool(false))
|
2016-04-02 04:30:39 +02:00
|
|
|
val out_valid = s1_valid && !io.s1_kill && state === s_ready
|
2017-03-06 06:43:20 +01:00
|
|
|
val s1_idx = io.s1_paddr(untagBits-1,blockOffBits)
|
|
|
|
val s1_tag = io.s1_paddr(tagBits+untagBits-1,untagBits)
|
2015-04-22 20:26:03 +02:00
|
|
|
val s1_hit = out_valid && s1_any_tag_hit
|
|
|
|
val s1_miss = out_valid && !s1_any_tag_hit
|
2017-03-06 06:43:20 +01:00
|
|
|
|
|
|
|
val s0_valid = io.req.valid && state === s_ready && !(out_valid && stall)
|
|
|
|
val s0_vaddr = io.req.bits.addr
|
|
|
|
|
|
|
|
s1_valid := s0_valid || out_valid && stall
|
2012-10-10 06:35:03 +02:00
|
|
|
|
2016-07-09 10:08:52 +02:00
|
|
|
when (s1_miss && state === s_ready) {
|
2017-03-06 06:43:20 +01:00
|
|
|
refill_addr := io.s1_paddr
|
2015-04-22 20:26:03 +02:00
|
|
|
}
|
|
|
|
val refill_tag = refill_addr(tagBits+untagBits-1,untagBits)
|
2017-03-06 06:43:20 +01:00
|
|
|
val refill_idx = refill_addr(untagBits-1,blockOffBits)
|
2016-12-13 02:38:55 +01:00
|
|
|
val (_, _, refill_done, refill_cnt) = edge.count(tl_out.d)
|
2017-02-25 06:01:56 +01:00
|
|
|
tl_out.d.ready := Bool(true)
|
|
|
|
require (edge.manager.minLatency > 0)
|
2012-10-10 06:35:03 +02:00
|
|
|
|
2015-04-22 20:26:03 +02:00
|
|
|
val repl_way = if (isDM) UInt(0) else LFSR16(s1_miss)(log2Up(nWays)-1,0)
|
2014-08-12 03:36:23 +02:00
|
|
|
val entagbits = code.width(tagBits)
|
2016-01-12 21:42:57 +01:00
|
|
|
val tag_array = SeqMem(nSets, Vec(nWays, Bits(width = entagbits)))
|
2016-05-24 02:51:08 +02:00
|
|
|
val tag_rdata = tag_array.read(s0_vaddr(untagBits-1,blockOffBits), !refill_done && s0_valid)
|
2012-10-10 06:35:03 +02:00
|
|
|
when (refill_done) {
|
2016-08-01 02:13:52 +02:00
|
|
|
val tag = code.encode(refill_tag)
|
2017-03-06 06:43:20 +01:00
|
|
|
tag_array.write(refill_idx, Vec.fill(nWays)(tag), Vec.tabulate(nWays)(repl_way === _))
|
2012-11-05 01:39:25 +01:00
|
|
|
}
|
2012-07-12 23:50:12 +02:00
|
|
|
|
2014-08-08 21:23:02 +02:00
|
|
|
val vb_array = Reg(init=Bits(0, nSets*nWays))
|
2012-10-10 06:35:03 +02:00
|
|
|
when (refill_done && !invalidated) {
|
2017-03-06 06:43:20 +01:00
|
|
|
vb_array := vb_array.bitSet(Cat(repl_way, refill_idx), Bool(true))
|
2012-10-10 06:35:03 +02:00
|
|
|
}
|
2012-11-16 10:55:45 +01:00
|
|
|
when (io.invalidate) {
|
2012-07-12 23:50:12 +02:00
|
|
|
vb_array := Bits(0)
|
2012-10-10 06:35:03 +02:00
|
|
|
invalidated := Bool(true)
|
2012-07-12 23:50:12 +02:00
|
|
|
}
|
2016-01-12 21:42:57 +01:00
|
|
|
val s1_disparity = Wire(Vec(nWays, Bool()))
|
2014-08-08 21:23:02 +02:00
|
|
|
for (i <- 0 until nWays)
|
2015-04-22 20:26:03 +02:00
|
|
|
when (s1_valid && s1_disparity(i)) { vb_array := vb_array.bitSet(Cat(UInt(i), s1_idx), Bool(false)) }
|
2012-10-10 06:35:03 +02:00
|
|
|
|
2016-01-12 21:42:57 +01:00
|
|
|
val s1_tag_match = Wire(Vec(nWays, Bool()))
|
|
|
|
val s1_tag_hit = Wire(Vec(nWays, Bool()))
|
|
|
|
val s1_dout = Wire(Vec(nWays, Bits(width = rowBits)))
|
2017-03-06 06:43:20 +01:00
|
|
|
val s1_dout_valid = RegNext(s0_valid)
|
2012-11-25 07:00:43 +01:00
|
|
|
|
2014-08-08 21:23:02 +02:00
|
|
|
for (i <- 0 until nWays) {
|
2017-03-06 06:43:20 +01:00
|
|
|
val s1_vb = !io.invalidate && vb_array(Cat(UInt(i), io.s1_paddr(untagBits-1,blockOffBits))).toBool
|
2015-09-12 00:43:07 +02:00
|
|
|
val tag_out = tag_rdata(i)
|
2017-03-06 06:43:20 +01:00
|
|
|
val s1_tag_disparity = code.decode(tag_out).error holdUnless s1_dout_valid
|
|
|
|
s1_tag_match(i) := (tag_out(tagBits-1,0) === s1_tag) holdUnless s1_dout_valid
|
2015-04-22 20:26:03 +02:00
|
|
|
s1_tag_hit(i) := s1_vb && s1_tag_match(i)
|
|
|
|
s1_disparity(i) := s1_vb && (s1_tag_disparity || code.decode(s1_dout(i)).error)
|
2012-10-10 06:35:03 +02:00
|
|
|
}
|
2015-04-22 20:26:03 +02:00
|
|
|
s1_any_tag_hit := s1_tag_hit.reduceLeft(_||_) && !s1_disparity.reduceLeft(_||_)
|
2012-10-10 06:35:03 +02:00
|
|
|
|
2014-08-08 21:23:02 +02:00
|
|
|
for (i <- 0 until nWays) {
|
2016-01-12 21:42:57 +01:00
|
|
|
val data_array = SeqMem(nSets * refillCycles, Bits(width = code.width(rowBits)))
|
2016-12-13 02:38:55 +01:00
|
|
|
val wen = tl_out.d.valid && repl_way === UInt(i)
|
2015-07-11 22:32:45 +02:00
|
|
|
when (wen) {
|
2016-12-13 02:38:55 +01:00
|
|
|
val e_d = code.encode(tl_out.d.bits.data)
|
2017-03-06 06:43:20 +01:00
|
|
|
data_array.write((refill_idx << log2Ceil(refillCycles)) | refill_cnt, e_d)
|
2012-10-10 06:35:03 +02:00
|
|
|
}
|
2016-07-15 06:42:12 +02:00
|
|
|
val s0_raddr = s0_vaddr(untagBits-1,blockOffBits-log2Ceil(refillCycles))
|
2017-03-06 06:43:20 +01:00
|
|
|
s1_dout(i) := data_array.read(s0_raddr, !wen && s0_valid) holdUnless s1_dout_valid
|
2012-01-25 01:51:30 +01:00
|
|
|
}
|
2011-12-04 04:41:15 +01:00
|
|
|
|
2011-11-07 09:58:25 +01:00
|
|
|
// output signals
|
2016-12-13 02:38:55 +01:00
|
|
|
outer.latency match {
|
2016-07-14 21:38:54 +02:00
|
|
|
case 1 =>
|
|
|
|
io.resp.bits.datablock := Mux1H(s1_tag_hit, s1_dout)
|
|
|
|
io.resp.valid := s1_hit
|
|
|
|
case 2 =>
|
2016-10-18 03:33:03 +02:00
|
|
|
val s2_hit = RegEnable(s1_hit, Bool(false), !stall)
|
2016-07-14 21:38:54 +02:00
|
|
|
val s2_tag_hit = RegEnable(s1_tag_hit, !stall)
|
|
|
|
val s2_dout = RegEnable(s1_dout, !stall)
|
|
|
|
io.resp.bits.datablock := Mux1H(s2_tag_hit, s2_dout)
|
|
|
|
io.resp.valid := s2_hit
|
2015-12-03 02:17:49 +01:00
|
|
|
}
|
2016-12-13 02:38:55 +01:00
|
|
|
tl_out.a.valid := state === s_request && !io.s2_kill
|
|
|
|
tl_out.a.bits := edge.Get(
|
|
|
|
fromSource = UInt(0),
|
|
|
|
toAddress = (refill_addr >> blockOffBits) << blockOffBits,
|
|
|
|
lgSize = lgCacheBlockBytes)._2
|
2017-03-20 01:18:50 +01:00
|
|
|
tl_out.b.ready := Bool(true)
|
2016-12-13 02:38:55 +01:00
|
|
|
tl_out.c.valid := Bool(false)
|
|
|
|
tl_out.e.valid := Bool(false)
|
2011-10-26 08:02:47 +02:00
|
|
|
|
|
|
|
// control state machine
|
|
|
|
switch (state) {
|
|
|
|
is (s_ready) {
|
2015-04-22 20:26:03 +02:00
|
|
|
when (s1_miss) { state := s_request }
|
2012-03-06 09:31:44 +01:00
|
|
|
invalidated := Bool(false)
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
2012-10-10 06:35:03 +02:00
|
|
|
is (s_request) {
|
2016-12-13 02:38:55 +01:00
|
|
|
when (tl_out.a.ready) { state := s_refill_wait }
|
2016-07-09 10:08:52 +02:00
|
|
|
when (io.s2_kill) { state := s_ready }
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
|
|
|
is (s_refill_wait) {
|
2016-12-13 02:38:55 +01:00
|
|
|
when (tl_out.d.valid) { state := s_refill }
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
|
|
|
is (s_refill) {
|
2012-10-10 06:35:03 +02:00
|
|
|
when (refill_done) { state := s_ready }
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
2012-10-10 06:35:03 +02:00
|
|
|
}
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|