1
0
rocket-chip/rocket/src/main/scala/tlb.scala

178 lines
5.7 KiB
Scala
Raw Normal View History

2012-10-10 06:35:03 +02:00
package rocket
import Chisel._
import uncore.constants.AddressConstants._
import scala.math._
2012-10-10 06:35:03 +02:00
2013-08-12 19:39:11 +02:00
class CAMIO(entries: Int, addr_bits: Int, tag_bits: Int) extends Bundle {
2012-10-10 06:35:03 +02:00
val clear = Bool(INPUT);
val clear_hit = Bool(INPUT)
val tag = Bits(INPUT, tag_bits);
val hit = Bool(OUTPUT);
2013-08-12 19:39:11 +02:00
val hits = UInt(OUTPUT, entries);
2012-10-10 06:35:03 +02:00
val valid_bits = Bits(OUTPUT, entries);
val write = Bool(INPUT);
val write_tag = Bits(INPUT, tag_bits);
2013-08-12 19:39:11 +02:00
val write_addr = UInt(INPUT, addr_bits);
2012-10-10 06:35:03 +02:00
}
2013-08-12 19:39:11 +02:00
class RocketCAM(entries: Int, tag_bits: Int) extends Module {
2012-10-10 06:35:03 +02:00
val addr_bits = ceil(log(entries)/log(2)).toInt;
2013-08-12 19:39:11 +02:00
val io = new CAMIO(entries, addr_bits, tag_bits);
val cam_tags = Vec.fill(entries){Reg(Bits(width = tag_bits))}
2012-10-10 06:35:03 +02:00
2013-08-16 00:28:15 +02:00
val vb_array = Reg(init=Bits(0, entries))
2012-10-10 06:35:03 +02:00
when (io.write) {
vb_array := vb_array.bitSet(io.write_addr, Bool(true));
cam_tags(io.write_addr) := io.write_tag
}
when (io.clear) {
vb_array := Bits(0, entries);
}
.elsewhen (io.clear_hit) {
vb_array := vb_array & ~io.hits
}
val hits = (0 until entries).map(i => vb_array(i) && cam_tags(i) === io.tag)
io.valid_bits := vb_array;
2013-08-12 19:39:11 +02:00
io.hits := Vec(hits).toBits
2012-10-10 06:35:03 +02:00
io.hit := io.hits.orR
}
class PseudoLRU(n: Int)
{
2013-08-12 19:39:11 +02:00
val state = Reg(Bits(width = n))
def access(way: UInt) = {
2012-10-10 06:35:03 +02:00
var next_state = state
2013-08-12 19:39:11 +02:00
var idx = UInt(1,1)
2012-10-10 06:35:03 +02:00
for (i <- log2Up(n)-1 to 0 by -1) {
val bit = way(i)
2013-08-12 19:39:11 +02:00
val mask = (UInt(1,n) << idx)(n-1,0)
next_state = next_state & ~mask | Mux(bit, UInt(0), mask)
2012-10-10 06:35:03 +02:00
//next_state.bitSet(idx, !bit)
idx = Cat(idx, bit)
}
state := next_state
}
def replace = {
2013-08-12 19:39:11 +02:00
var idx = UInt(1,1)
2012-10-10 06:35:03 +02:00
for (i <- 0 until log2Up(n))
idx = Cat(idx, state(idx))
idx(log2Up(n)-1,0)
}
}
class TLBReq extends Bundle
{
2013-08-12 19:39:11 +02:00
val asid = UInt(width = ASID_BITS)
val vpn = UInt(width = VPN_BITS+1)
val passthrough = Bool()
2012-10-10 06:35:03 +02:00
val instruction = Bool()
}
class TLBResp(entries: Int) extends Bundle
{
// lookup responses
val miss = Bool(OUTPUT)
2013-08-12 19:39:11 +02:00
val hit_idx = UInt(OUTPUT, entries)
val ppn = UInt(OUTPUT, PPN_BITS)
2012-10-10 06:35:03 +02:00
val xcpt_ld = Bool(OUTPUT)
val xcpt_st = Bool(OUTPUT)
val xcpt_if = Bool(OUTPUT)
override def clone = new TLBResp(entries).asInstanceOf[this.type]
}
2013-08-12 19:39:11 +02:00
class TLB(entries: Int) extends Module
2012-10-10 06:35:03 +02:00
{
val io = new Bundle {
2013-08-12 19:39:11 +02:00
val req = Decoupled(new TLBReq).flip
2012-10-10 06:35:03 +02:00
val resp = new TLBResp(entries)
2013-01-07 22:38:59 +01:00
val ptw = new TLBPTWIO
2012-10-10 06:35:03 +02:00
}
2013-08-12 19:39:11 +02:00
val s_ready :: s_request :: s_wait :: s_wait_invalidate :: Nil = Enum(4) { UInt() }
2013-08-16 00:28:15 +02:00
val state = Reg(init=s_ready)
2013-08-12 19:39:11 +02:00
val r_refill_tag = Reg(UInt())
val r_refill_waddr = Reg(UInt())
2012-10-10 06:35:03 +02:00
2013-08-12 19:39:11 +02:00
val tag_cam = Module(new RocketCAM(entries, ASID_BITS+VPN_BITS))
val tag_ram = Vec.fill(entries){Reg(io.ptw.resp.bits.ppn.clone)}
2012-10-10 06:35:03 +02:00
2013-08-12 19:39:11 +02:00
val lookup_tag = Cat(io.req.bits.asid, io.req.bits.vpn).toUInt
tag_cam.io.clear := io.ptw.invalidate
2012-10-10 06:35:03 +02:00
tag_cam.io.clear_hit := io.req.fire() && Mux(io.req.bits.instruction, io.resp.xcpt_if, io.resp.xcpt_ld && io.resp.xcpt_st)
tag_cam.io.tag := lookup_tag
tag_cam.io.write := state === s_wait && io.ptw.resp.valid
tag_cam.io.write_tag := r_refill_tag
tag_cam.io.write_addr := r_refill_waddr
val tag_hit = tag_cam.io.hit
2013-08-12 19:39:11 +02:00
val tag_hit_addr = OHToUInt(tag_cam.io.hits)
2012-10-10 06:35:03 +02:00
// permission bit arrays
2013-08-12 19:39:11 +02:00
val ur_array = Reg(Bits()) // user read permission
val uw_array = Reg(Bits()) // user write permission
val ux_array = Reg(Bits()) // user execute permission
val sr_array = Reg(Bits()) // supervisor read permission
val sw_array = Reg(Bits()) // supervisor write permission
val sx_array = Reg(Bits()) // supervisor execute permission
2012-11-16 10:59:38 +01:00
when (io.ptw.resp.valid) {
tag_ram(r_refill_waddr) := io.ptw.resp.bits.ppn
val perm = (!io.ptw.resp.bits.error).toSInt & io.ptw.resp.bits.perm
2013-08-24 06:16:28 +02:00
ur_array := ur_array.bitSet(r_refill_waddr, perm(0))
2012-10-10 06:35:03 +02:00
uw_array := uw_array.bitSet(r_refill_waddr, perm(1))
2013-08-24 06:16:28 +02:00
ux_array := ux_array.bitSet(r_refill_waddr, perm(2))
sr_array := sr_array.bitSet(r_refill_waddr, perm(3))
2012-10-10 06:35:03 +02:00
sw_array := sw_array.bitSet(r_refill_waddr, perm(4))
2013-08-24 06:16:28 +02:00
sx_array := sx_array.bitSet(r_refill_waddr, perm(5))
2012-10-10 06:35:03 +02:00
}
// high if there are any unused (invalid) entries in the TLB
val has_invalid_entry = !tag_cam.io.valid_bits.andR
val invalid_entry = PriorityEncoder(~tag_cam.io.valid_bits)
val plru = new PseudoLRU(entries)
val repl_waddr = Mux(has_invalid_entry, invalid_entry, plru.replace)
val bad_va = io.req.bits.vpn(VPN_BITS) != io.req.bits.vpn(VPN_BITS-1)
2012-11-27 10:28:06 +01:00
val tlb_hit = io.ptw.status.vm && tag_hit
val tlb_miss = io.ptw.status.vm && !tag_hit && !bad_va
2012-10-10 06:35:03 +02:00
when (io.req.valid && tlb_hit) {
2013-08-12 19:39:11 +02:00
plru.access(OHToUInt(tag_cam.io.hits))
2012-10-10 06:35:03 +02:00
}
io.req.ready := state === s_ready
2012-11-27 10:28:06 +01:00
io.resp.xcpt_ld := bad_va || tlb_hit && !Mux(io.ptw.status.s, (sr_array & tag_cam.io.hits).orR, (ur_array & tag_cam.io.hits).orR)
io.resp.xcpt_st := bad_va || tlb_hit && !Mux(io.ptw.status.s, (sw_array & tag_cam.io.hits).orR, (uw_array & tag_cam.io.hits).orR)
io.resp.xcpt_if := bad_va || tlb_hit && !Mux(io.ptw.status.s, (sx_array & tag_cam.io.hits).orR, (ux_array & tag_cam.io.hits).orR)
2012-10-10 06:35:03 +02:00
io.resp.miss := tlb_miss
2012-11-27 10:28:06 +01:00
io.resp.ppn := Mux(io.ptw.status.vm && !io.req.bits.passthrough, Mux1H(tag_cam.io.hits, tag_ram), io.req.bits.vpn(PPN_BITS-1,0))
2012-10-10 06:35:03 +02:00
io.resp.hit_idx := tag_cam.io.hits
io.ptw.req.valid := state === s_request
io.ptw.req.bits := r_refill_tag
when (io.req.fire() && tlb_miss) {
state := s_request
r_refill_tag := lookup_tag
r_refill_waddr := repl_waddr
}
when (state === s_request) {
when (io.ptw.invalidate) {
2012-10-10 06:35:03 +02:00
state := s_ready
}
when (io.ptw.req.ready) {
state := s_wait
when (io.ptw.invalidate) { state := s_wait_invalidate }
2012-10-10 06:35:03 +02:00
}
}
when (state === s_wait && io.ptw.invalidate) {
2012-10-10 06:35:03 +02:00
state := s_wait_invalidate
}
2012-11-16 10:59:38 +01:00
when (io.ptw.resp.valid) {
2012-10-10 06:35:03 +02:00
state := s_ready
}
}