2014-09-13 03:06:41 +02:00
|
|
|
// See LICENSE for license details.
|
|
|
|
|
2012-02-26 02:09:26 +01:00
|
|
|
package rocket
|
2011-11-09 23:52:17 +01:00
|
|
|
|
2012-10-08 05:15:54 +02:00
|
|
|
import Chisel._
|
2014-04-02 02:15:46 +02:00
|
|
|
import uncore._
|
2012-11-27 05:38:45 +01:00
|
|
|
import Util._
|
2011-11-09 23:52:17 +01:00
|
|
|
|
2015-03-14 10:49:07 +01:00
|
|
|
class PTWReq extends CoreBundle {
|
|
|
|
val addr = UInt(width = vpnBits)
|
|
|
|
val perm = Bits(width = permBits)
|
|
|
|
}
|
|
|
|
|
2015-02-02 05:04:13 +01:00
|
|
|
class PTWResp extends CoreBundle {
|
2014-04-02 02:15:46 +02:00
|
|
|
val error = Bool()
|
2015-03-22 04:12:25 +01:00
|
|
|
val pte = new PTE
|
2013-08-12 19:39:11 +02:00
|
|
|
}
|
|
|
|
|
2015-02-02 05:04:13 +01:00
|
|
|
class TLBPTWIO extends CoreBundle {
|
2015-03-14 10:49:07 +01:00
|
|
|
val req = Decoupled(new PTWReq)
|
2013-08-12 19:39:11 +02:00
|
|
|
val resp = Valid(new PTWResp).flip
|
2015-03-14 10:49:07 +01:00
|
|
|
val status = new MStatus().asInput
|
2012-11-06 17:13:44 +01:00
|
|
|
val invalidate = Bool(INPUT)
|
|
|
|
}
|
|
|
|
|
2015-02-02 05:04:13 +01:00
|
|
|
class DatapathPTWIO extends CoreBundle {
|
|
|
|
val ptbr = UInt(INPUT, paddrBits)
|
2012-11-06 17:13:44 +01:00
|
|
|
val invalidate = Bool(INPUT)
|
2015-03-14 10:49:07 +01:00
|
|
|
val status = new MStatus().asInput
|
2011-11-09 23:52:17 +01:00
|
|
|
}
|
|
|
|
|
2015-03-22 04:12:25 +01:00
|
|
|
class PTE extends CoreBundle {
|
|
|
|
val ppn = Bits(width = ppnBits)
|
|
|
|
val reserved = Bits(width = 2)
|
|
|
|
val d = Bool()
|
|
|
|
val r = Bool()
|
|
|
|
val perm = Bits(width = 6)
|
|
|
|
val g = Bool()
|
|
|
|
val t = Bool()
|
|
|
|
val v = Bool()
|
|
|
|
}
|
|
|
|
|
2015-02-02 05:04:13 +01:00
|
|
|
class PTW(n: Int) extends CoreModule
|
2011-11-09 23:52:17 +01:00
|
|
|
{
|
2012-11-06 17:13:44 +01:00
|
|
|
val io = new Bundle {
|
2013-08-12 19:39:11 +02:00
|
|
|
val requestor = Vec.fill(n){new TLBPTWIO}.flip
|
2014-08-08 21:23:02 +02:00
|
|
|
val mem = new HellaCacheIO
|
2013-01-07 22:38:59 +01:00
|
|
|
val dpath = new DatapathPTWIO
|
2012-11-06 17:13:44 +01:00
|
|
|
}
|
2011-11-09 23:52:17 +01:00
|
|
|
|
2012-05-01 10:24:36 +02:00
|
|
|
val levels = 3
|
2015-02-02 05:04:13 +01:00
|
|
|
val bitsPerLevel = vpnBits/levels
|
|
|
|
require(vpnBits == levels * bitsPerLevel)
|
2012-05-01 10:24:36 +02:00
|
|
|
|
2015-03-14 10:49:07 +01:00
|
|
|
val s_ready :: s_req :: s_wait :: s_set_dirty :: s_wait_dirty :: s_done :: s_error :: Nil = Enum(UInt(), 7)
|
2013-08-16 00:28:15 +02:00
|
|
|
val state = Reg(init=s_ready)
|
2013-08-12 19:39:11 +02:00
|
|
|
val count = Reg(UInt(width = log2Up(levels)))
|
2012-11-27 05:38:45 +01:00
|
|
|
|
2015-03-14 10:49:07 +01:00
|
|
|
val r_req = Reg(new PTWReq)
|
2013-08-12 19:39:11 +02:00
|
|
|
val r_req_dest = Reg(Bits())
|
2015-03-22 04:12:25 +01:00
|
|
|
val r_pte = Reg(new PTE)
|
2011-11-09 23:52:17 +01:00
|
|
|
|
2015-03-14 10:49:07 +01:00
|
|
|
val vpn_idx = Vec((0 until levels).map(i => (r_req.addr >> (levels-i-1)*bitsPerLevel)(bitsPerLevel-1,0)))(count)
|
2012-03-18 07:00:51 +01:00
|
|
|
|
2015-03-14 10:49:07 +01:00
|
|
|
val arb = Module(new RRArbiter(new PTWReq, n))
|
2012-10-10 06:35:03 +02:00
|
|
|
arb.io.in <> io.requestor.map(_.req)
|
|
|
|
arb.io.out.ready := state === s_ready
|
|
|
|
|
2015-03-22 04:12:25 +01:00
|
|
|
val pte = new PTE().fromBits(io.mem.resp.bits.data)
|
|
|
|
val pte_addr = Cat(r_pte.ppn, vpn_idx).toUInt << log2Up(xLen/8)
|
|
|
|
|
2012-10-10 06:35:03 +02:00
|
|
|
when (arb.io.out.fire()) {
|
2015-03-14 10:49:07 +01:00
|
|
|
r_req := arb.io.out.bits
|
2012-10-10 06:35:03 +02:00
|
|
|
r_req_dest := arb.io.chosen
|
2015-03-22 04:12:25 +01:00
|
|
|
r_pte.ppn := io.dpath.ptbr(paddrBits-1,pgIdxBits)
|
2011-11-10 09:23:29 +01:00
|
|
|
}
|
2012-03-17 01:14:43 +01:00
|
|
|
|
2015-03-22 04:12:25 +01:00
|
|
|
val (pte_cache_hit, pte_cache_data) = {
|
|
|
|
val size = log2Up(levels * 2)
|
|
|
|
val plru = new PseudoLRU(size)
|
|
|
|
val valid = Reg(init = Bits(0, size))
|
|
|
|
val tags = Mem(UInt(width = paddrBits), size)
|
|
|
|
val data = Mem(UInt(width = paddrBits - pgIdxBits), size)
|
|
|
|
|
|
|
|
val hits = Vec(tags.map(_ === pte_addr)).toBits & valid
|
|
|
|
val hit = hits.orR
|
|
|
|
when (io.mem.resp.valid && io.mem.resp.bits.data(1,0).andR && !hit) {
|
|
|
|
val r = Mux(valid.andR, plru.replace, PriorityEncoder(~valid))
|
|
|
|
valid(r) := true
|
|
|
|
tags(r) := pte_addr
|
|
|
|
data(r) := io.mem.resp.bits.data(paddrBits-1,pgIdxBits)
|
|
|
|
}
|
|
|
|
when (hit && state === s_req) { plru.access(OHToUInt(hits)) }
|
|
|
|
when (io.dpath.invalidate) { valid := 0 }
|
|
|
|
|
|
|
|
(hit, Mux1H(hits, data))
|
|
|
|
}
|
|
|
|
|
|
|
|
val perm_ok = (pte.perm & r_req.perm).orR
|
2015-03-14 10:49:07 +01:00
|
|
|
val is_store = r_req.perm(1) || r_req.perm(4)
|
2015-03-22 04:12:25 +01:00
|
|
|
val set_dirty_bit = perm_ok && !pte.t && (!pte.r || (is_store && !pte.d))
|
2015-03-14 10:49:07 +01:00
|
|
|
when (io.mem.resp.valid && state === s_wait && !set_dirty_bit) {
|
|
|
|
r_pte := pte
|
2011-11-09 23:52:17 +01:00
|
|
|
}
|
|
|
|
|
2015-03-14 10:49:07 +01:00
|
|
|
io.mem.req.valid := state === s_req || state === s_set_dirty
|
2012-11-06 17:13:44 +01:00
|
|
|
io.mem.req.bits.phys := Bool(true)
|
2015-03-14 10:49:07 +01:00
|
|
|
io.mem.req.bits.cmd := Mux(state === s_set_dirty, M_XA_OR, M_XRD)
|
2012-05-02 03:23:04 +02:00
|
|
|
io.mem.req.bits.typ := MT_D
|
2015-03-22 04:12:25 +01:00
|
|
|
io.mem.req.bits.addr := pte_addr
|
2012-05-02 03:23:04 +02:00
|
|
|
io.mem.req.bits.kill := Bool(false)
|
2015-03-14 10:49:07 +01:00
|
|
|
io.mem.req.bits.data := UInt(1<<9) | Mux(is_store, UInt(1<<10), UInt(0))
|
2011-11-10 06:54:11 +01:00
|
|
|
|
2015-03-14 10:49:07 +01:00
|
|
|
val resp_err = state === s_error
|
|
|
|
val resp_val = state === s_done || resp_err
|
2012-11-27 05:38:45 +01:00
|
|
|
|
2015-02-02 05:04:13 +01:00
|
|
|
val r_resp_ppn = io.mem.req.bits.addr >> UInt(pgIdxBits)
|
2015-03-14 10:49:07 +01:00
|
|
|
val resp_ppn = Vec((0 until levels-1).map(i => Cat(r_resp_ppn >> bitsPerLevel*(levels-i-1), r_req.addr(bitsPerLevel*(levels-i-1)-1,0))) :+ r_resp_ppn)(count)
|
2012-05-03 11:29:09 +02:00
|
|
|
|
|
|
|
for (i <- 0 until io.requestor.size) {
|
2013-08-12 19:39:11 +02:00
|
|
|
val me = r_req_dest === UInt(i)
|
2012-10-10 06:35:03 +02:00
|
|
|
io.requestor(i).resp.valid := resp_val && me
|
|
|
|
io.requestor(i).resp.bits.error := resp_err
|
2015-03-22 04:12:25 +01:00
|
|
|
io.requestor(i).resp.bits.pte := r_pte
|
|
|
|
io.requestor(i).resp.bits.pte.ppn := resp_ppn
|
2012-11-06 17:13:44 +01:00
|
|
|
io.requestor(i).invalidate := io.dpath.invalidate
|
|
|
|
io.requestor(i).status := io.dpath.status
|
2012-05-03 11:29:09 +02:00
|
|
|
}
|
2011-11-09 23:52:17 +01:00
|
|
|
|
|
|
|
// control state machine
|
|
|
|
switch (state) {
|
|
|
|
is (s_ready) {
|
2012-10-10 06:35:03 +02:00
|
|
|
when (arb.io.out.valid) {
|
2014-01-14 06:43:56 +01:00
|
|
|
state := s_req
|
2011-12-10 09:42:09 +01:00
|
|
|
}
|
2013-08-12 19:39:11 +02:00
|
|
|
count := UInt(0)
|
2011-11-09 23:52:17 +01:00
|
|
|
}
|
2012-05-01 10:24:36 +02:00
|
|
|
is (s_req) {
|
2015-03-22 04:12:25 +01:00
|
|
|
when (pte_cache_hit && count < levels-1) {
|
|
|
|
io.mem.req.valid := false
|
|
|
|
state := s_req
|
|
|
|
count := count + 1
|
|
|
|
r_pte.ppn := pte_cache_data
|
|
|
|
}.elsewhen (io.mem.req.ready) {
|
2014-01-14 06:43:56 +01:00
|
|
|
state := s_wait
|
2011-11-09 23:52:17 +01:00
|
|
|
}
|
|
|
|
}
|
2012-05-01 10:24:36 +02:00
|
|
|
is (s_wait) {
|
2012-05-02 03:23:04 +02:00
|
|
|
when (io.mem.resp.bits.nack) {
|
2012-05-01 10:24:36 +02:00
|
|
|
state := s_req
|
2011-12-10 09:42:09 +01:00
|
|
|
}
|
2012-11-16 11:39:33 +01:00
|
|
|
when (io.mem.resp.valid) {
|
2013-08-24 06:16:28 +02:00
|
|
|
state := s_error
|
2015-03-22 04:12:25 +01:00
|
|
|
when (pte.v) {
|
2015-03-14 10:49:07 +01:00
|
|
|
when (set_dirty_bit) {
|
|
|
|
state := s_set_dirty
|
2015-03-22 04:12:25 +01:00
|
|
|
}.elsewhen (!pte.t) {
|
2013-08-24 06:16:28 +02:00
|
|
|
state := s_done
|
|
|
|
}.elsewhen (count < levels-1) {
|
2012-05-01 10:24:36 +02:00
|
|
|
state := s_req
|
2013-08-24 06:16:28 +02:00
|
|
|
count := count + 1
|
2012-05-01 10:24:36 +02:00
|
|
|
}
|
2011-11-09 23:52:17 +01:00
|
|
|
}
|
|
|
|
}
|
2012-05-01 10:24:36 +02:00
|
|
|
}
|
2015-03-14 10:49:07 +01:00
|
|
|
is (s_set_dirty) {
|
|
|
|
when (io.mem.req.ready) {
|
|
|
|
state := s_wait_dirty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
is (s_wait_dirty) {
|
|
|
|
when (io.mem.resp.bits.nack) {
|
|
|
|
state := s_set_dirty
|
|
|
|
}
|
|
|
|
when (io.mem.resp.valid) {
|
|
|
|
state := s_req
|
|
|
|
}
|
|
|
|
}
|
2011-11-09 23:52:17 +01:00
|
|
|
is (s_done) {
|
2014-01-14 06:43:56 +01:00
|
|
|
state := s_ready
|
2011-11-09 23:52:17 +01:00
|
|
|
}
|
|
|
|
is (s_error) {
|
2014-01-14 06:43:56 +01:00
|
|
|
state := s_ready
|
2011-11-09 23:52:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|