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._
|
2016-06-28 22:15:39 +02:00
|
|
|
import uncore.agents._
|
|
|
|
import uncore.constants._
|
2012-11-27 05:38:45 +01:00
|
|
|
import Util._
|
2016-08-19 03:40:07 +02:00
|
|
|
import uncore.util._
|
2015-10-22 03:18:32 +02:00
|
|
|
import cde.{Parameters, Field}
|
2011-11-09 23:52:17 +01:00
|
|
|
|
2015-10-06 06:48:05 +02:00
|
|
|
class PTWReq(implicit p: Parameters) extends CoreBundle()(p) {
|
2015-03-28 00:20:59 +01:00
|
|
|
val prv = Bits(width = 2)
|
2016-07-06 04:19:49 +02:00
|
|
|
val pum = Bool()
|
|
|
|
val mxr = Bool()
|
|
|
|
val addr = UInt(width = vpnBits)
|
2015-03-28 00:20:59 +01:00
|
|
|
val store = Bool()
|
|
|
|
val fetch = Bool()
|
2015-03-14 10:49:07 +01:00
|
|
|
}
|
|
|
|
|
2015-10-06 06:48:05 +02:00
|
|
|
class PTWResp(implicit p: Parameters) extends CoreBundle()(p) {
|
2015-03-22 04:12:25 +01:00
|
|
|
val pte = new PTE
|
2013-08-12 19:39:11 +02:00
|
|
|
}
|
|
|
|
|
2015-10-06 06:48:05 +02:00
|
|
|
class TLBPTWIO(implicit p: Parameters) extends CoreBundle()(p) {
|
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
|
2016-06-18 03:29:05 +02:00
|
|
|
val ptbr = new PTBR().asInput
|
2012-11-06 17:13:44 +01:00
|
|
|
val invalidate = Bool(INPUT)
|
2016-06-18 03:29:05 +02:00
|
|
|
val status = new MStatus().asInput
|
2012-11-06 17:13:44 +01:00
|
|
|
}
|
|
|
|
|
2015-10-06 06:48:05 +02:00
|
|
|
class DatapathPTWIO(implicit p: Parameters) extends CoreBundle()(p) {
|
2016-06-18 03:29:05 +02:00
|
|
|
val ptbr = new PTBR().asInput
|
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-10-06 06:48:05 +02:00
|
|
|
class PTE(implicit p: Parameters) extends CoreBundle()(p) {
|
2016-07-06 04:19:49 +02:00
|
|
|
val reserved_for_hardware = Bits(width = 16)
|
|
|
|
val ppn = UInt(width = 38)
|
|
|
|
val reserved_for_software = Bits(width = 2)
|
2015-03-22 04:12:25 +01:00
|
|
|
val d = Bool()
|
2016-07-06 04:19:49 +02:00
|
|
|
val a = Bool()
|
|
|
|
val g = Bool()
|
|
|
|
val u = Bool()
|
|
|
|
val x = Bool()
|
|
|
|
val w = Bool()
|
2015-03-22 04:12:25 +01:00
|
|
|
val r = Bool()
|
2015-05-19 03:23:58 +02:00
|
|
|
val v = Bool()
|
|
|
|
|
2016-07-06 04:19:49 +02:00
|
|
|
def table(dummy: Int = 0) = v && !r && !w && !x
|
|
|
|
def leaf(dummy: Int = 0) = v && (r || (x && !w))
|
|
|
|
def ur(dummy: Int = 0) = sr() && u
|
|
|
|
def uw(dummy: Int = 0) = sw() && u
|
|
|
|
def ux(dummy: Int = 0) = sx() && u
|
|
|
|
def sr(dummy: Int = 0) = leaf() && r
|
|
|
|
def sw(dummy: Int = 0) = leaf() && w
|
|
|
|
def sx(dummy: Int = 0) = leaf() && x
|
|
|
|
|
|
|
|
def access_ok(req: PTWReq) = {
|
|
|
|
val perm_ok = Mux(req.fetch, x, Mux(req.store, w, r || (x && req.mxr)))
|
|
|
|
val priv_ok = Mux(u, !req.pum, req.prv(0))
|
|
|
|
leaf() && priv_ok && perm_ok
|
|
|
|
}
|
2015-03-22 04:12:25 +01:00
|
|
|
}
|
|
|
|
|
2015-10-06 06:48:05 +02:00
|
|
|
class PTW(n: Int)(implicit p: Parameters) extends CoreModule()(p) {
|
2012-11-06 17:13:44 +01:00
|
|
|
val io = new Bundle {
|
2016-01-14 22:57:45 +01:00
|
|
|
val requestor = Vec(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
|
|
|
}
|
2016-06-23 01:09:45 +02:00
|
|
|
|
|
|
|
require(usingAtomics, "PTW requires atomic memory operations")
|
2016-08-02 23:51:11 +02:00
|
|
|
|
|
|
|
val s_ready :: s_req :: s_wait1 :: s_wait2 :: s_set_dirty :: s_wait1_dirty :: s_wait2_dirty :: s_done :: Nil = Enum(UInt(), 8)
|
2013-08-16 00:28:15 +02:00
|
|
|
val state = Reg(init=s_ready)
|
2015-03-28 00:20:59 +01:00
|
|
|
val count = Reg(UInt(width = log2Up(pgLevels)))
|
2016-08-02 23:39:33 +02:00
|
|
|
val s1_kill = Reg(next = Bool(false))
|
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
|
|
|
|
2016-07-06 10:31:56 +02:00
|
|
|
val vpn_idxs = (0 until pgLevels).map(i => (r_req.addr >> (pgLevels-i-1)*pgLevelBits)(pgLevelBits-1,0))
|
|
|
|
val vpn_idx = vpn_idxs(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
|
|
|
|
|
2016-07-30 02:52:56 +02:00
|
|
|
val pte = {
|
|
|
|
val tmp = new PTE().fromBits(io.mem.resp.bits.data)
|
|
|
|
val res = Wire(init = new PTE().fromBits(io.mem.resp.bits.data))
|
|
|
|
res.ppn := tmp.ppn(ppnBits-1, 0)
|
|
|
|
when ((tmp.ppn >> ppnBits) =/= 0) { res.v := false }
|
|
|
|
res
|
|
|
|
}
|
2016-08-02 23:51:11 +02:00
|
|
|
val pte_addr = Cat(r_pte.ppn, vpn_idx) << log2Ceil(xLen/8)
|
2015-03-22 04:12:25 +01:00
|
|
|
|
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
|
2016-06-18 03:29:05 +02:00
|
|
|
r_pte.ppn := io.dpath.ptbr.ppn
|
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) = {
|
2016-07-28 03:40:38 +02:00
|
|
|
val size = 1 << log2Up(pgLevels * 2)
|
2015-03-22 04:12:25 +01:00
|
|
|
val plru = new PseudoLRU(size)
|
2016-07-02 23:34:18 +02:00
|
|
|
val valid = Reg(init = UInt(0, size))
|
|
|
|
val tags = Reg(Vec(size, UInt(width = paddrBits)))
|
|
|
|
val data = Reg(Vec(size, UInt(width = ppnBits)))
|
2015-03-22 04:12:25 +01:00
|
|
|
|
2016-08-01 02:13:52 +02:00
|
|
|
val hits = tags.map(_ === pte_addr).asUInt & valid
|
2015-03-22 04:12:25 +01:00
|
|
|
val hit = hits.orR
|
2015-03-28 00:20:59 +01:00
|
|
|
when (io.mem.resp.valid && pte.table() && !hit) {
|
2016-07-02 23:34:18 +02:00
|
|
|
val r = Mux(valid.andR, plru.replace, PriorityEncoder(~valid))
|
|
|
|
valid := valid | UIntToOH(r)
|
2015-03-22 04:12:25 +01:00
|
|
|
tags(r) := pte_addr
|
2015-03-28 00:20:59 +01:00
|
|
|
data(r) := pte.ppn
|
2015-03-22 04:12:25 +01:00
|
|
|
}
|
|
|
|
when (hit && state === s_req) { plru.access(OHToUInt(hits)) }
|
2016-07-02 23:34:18 +02:00
|
|
|
when (io.dpath.invalidate) { valid := 0 }
|
2015-03-22 04:12:25 +01:00
|
|
|
|
2016-08-02 23:39:33 +02:00
|
|
|
(hit && count < pgLevels-1, Mux1H(hits, data))
|
2011-11-09 23:52:17 +01:00
|
|
|
}
|
2015-03-28 00:20:59 +01:00
|
|
|
|
2015-07-16 05:24:18 +02:00
|
|
|
val pte_wdata = Wire(init=new PTE().fromBits(0))
|
2016-07-06 04:19:49 +02:00
|
|
|
pte_wdata.a := true
|
2015-03-28 00:20:59 +01:00
|
|
|
pte_wdata.d := r_req.store
|
2011-11-09 23:52:17 +01:00
|
|
|
|
2016-08-19 18:46:43 +02:00
|
|
|
io.mem.req.valid := state.isOneOf(s_req, 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)
|
2016-08-02 23:51:11 +02:00
|
|
|
io.mem.req.bits.typ := log2Ceil(xLen/8)
|
2015-03-22 04:12:25 +01:00
|
|
|
io.mem.req.bits.addr := pte_addr
|
2016-08-01 02:13:52 +02:00
|
|
|
io.mem.s1_data := pte_wdata.asUInt
|
2016-08-02 23:39:33 +02:00
|
|
|
io.mem.s1_kill := s1_kill
|
2016-04-27 20:22:04 +02:00
|
|
|
io.mem.invalidate_lr := Bool(false)
|
2011-11-10 06:54:11 +01:00
|
|
|
|
2016-07-30 02:52:56 +02:00
|
|
|
val resp_ppns = (0 until pgLevels-1).map(i => Cat(pte_addr >> (pgIdxBits + pgLevelBits*(pgLevels-i-1)), r_req.addr(pgLevelBits*(pgLevels-i-1)-1,0))) :+ (pte_addr >> pgIdxBits)
|
2012-05-03 11:29:09 +02:00
|
|
|
for (i <- 0 until io.requestor.size) {
|
2016-07-30 02:52:56 +02:00
|
|
|
io.requestor(i).resp.valid := state === s_done && (r_req_dest === i)
|
2015-03-22 04:12:25 +01:00
|
|
|
io.requestor(i).resp.bits.pte := r_pte
|
2016-07-30 02:52:56 +02:00
|
|
|
io.requestor(i).resp.bits.pte.ppn := resp_ppns(count)
|
2016-06-18 03:29:05 +02:00
|
|
|
io.requestor(i).ptbr := io.dpath.ptbr
|
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) {
|
2016-08-02 23:39:33 +02:00
|
|
|
when (pte_cache_hit) {
|
|
|
|
s1_kill := true
|
2015-03-22 04:12:25 +01:00
|
|
|
state := s_req
|
|
|
|
count := count + 1
|
|
|
|
r_pte.ppn := pte_cache_data
|
|
|
|
}.elsewhen (io.mem.req.ready) {
|
2016-08-02 23:51:11 +02:00
|
|
|
state := s_wait1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
is (s_wait1) {
|
|
|
|
state := s_wait2
|
|
|
|
when (io.mem.xcpt.pf.ld) {
|
|
|
|
r_pte.v := false
|
|
|
|
state := s_done
|
2011-11-09 23:52:17 +01:00
|
|
|
}
|
|
|
|
}
|
2016-08-02 23:51:11 +02:00
|
|
|
is (s_wait2) {
|
2016-04-02 04:30:39 +02:00
|
|
|
when (io.mem.s2_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) {
|
2016-03-03 08:29:58 +01:00
|
|
|
state := s_done
|
2016-08-02 23:39:33 +02:00
|
|
|
when (pte.access_ok(r_req) && (!pte.a || (r_req.store && !pte.d))) {
|
2016-03-03 08:29:58 +01:00
|
|
|
state := s_set_dirty
|
2016-08-02 23:39:33 +02:00
|
|
|
}.otherwise {
|
|
|
|
r_pte := pte
|
2016-03-03 08:29:58 +01:00
|
|
|
}
|
2015-03-28 00:20:59 +01:00
|
|
|
when (pte.table() && count < pgLevels-1) {
|
|
|
|
state := s_req
|
|
|
|
count := count + 1
|
|
|
|
}
|
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) {
|
2016-08-02 23:51:11 +02:00
|
|
|
state := s_wait1_dirty
|
|
|
|
}
|
|
|
|
}
|
|
|
|
is (s_wait1_dirty) {
|
|
|
|
state := s_wait2_dirty
|
|
|
|
when (io.mem.xcpt.pf.st) {
|
|
|
|
r_pte.v := false
|
|
|
|
state := s_done
|
2015-03-14 10:49:07 +01:00
|
|
|
}
|
|
|
|
}
|
2016-08-02 23:51:11 +02:00
|
|
|
is (s_wait2_dirty) {
|
2016-04-02 04:30:39 +02:00
|
|
|
when (io.mem.s2_nack) {
|
2015-03-14 10:49:07 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|