xref: /XiangShan/src/main/scala/xiangshan/backend/fu/PMP.scala (revision f57f7f2aa52bf8c9d7952402ff7d36066bf8e1b3)
1/***************************************************************************************
2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3* Copyright (c) 2020-2021 Peng Cheng Laboratory
4*
5* XiangShan is licensed under Mulan PSL v2.
6* You can use this software according to the terms and conditions of the Mulan PSL v2.
7* You may obtain a copy of Mulan PSL v2 at:
8*          http://license.coscl.org.cn/MulanPSL2
9*
10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13*
14* See the Mulan PSL v2 for more details.
15***************************************************************************************/
16
17// See LICENSE.SiFive for license details.
18
19package xiangshan.backend.fu
20
21import org.chipsalliance.cde.config.Parameters
22import chisel3._
23import chisel3.util._
24import utility.MaskedRegMap.WritableMask
25import xiangshan._
26import xiangshan.backend.fu.util.HasCSRConst
27import utils._
28import utility._
29import xiangshan.cache.mmu.{TlbCmd, TlbExceptionBundle}
30
31trait PMPConst extends HasPMParameters {
32  val PMPOffBits = 2 // minimal 4bytes
33  val CoarserGrain: Boolean = PlatformGrain > PMPOffBits
34}
35
36abstract class PMPBundle(implicit val p: Parameters) extends Bundle with PMPConst
37abstract class PMPModule(implicit val p: Parameters) extends Module with PMPConst
38abstract class PMPXSModule(implicit p: Parameters) extends XSModule with PMPConst
39
40class PMPConfig(implicit p: Parameters) extends PMPBundle {
41  val l = Bool()
42  val c = Bool() // res(1), unuse in pmp
43  val atomic = Bool() // res(0), unuse in pmp
44  val a = UInt(2.W)
45  val x = Bool()
46  val w = Bool()
47  val r = Bool()
48
49  def res: UInt = Cat(c, atomic) // in pmp, unused
50  def off = a === 0.U
51  def tor = a === 1.U
52  def na4 = { if (CoarserGrain) false.B else a === 2.U }
53  def napot = { if (CoarserGrain) a(1).asBool else a === 3.U }
54  def off_tor = !a(1)
55  def na4_napot = a(1)
56
57  def locked = l
58  def addr_locked: Bool = locked
59  def addr_locked(next: PMPConfig): Bool = locked || (next.locked && next.tor)
60}
61
62object PMPConfigUInt {
63  def apply(
64    l: Boolean = false,
65    c: Boolean = false,
66    atomic: Boolean = false,
67    a: Int = 0,
68    x: Boolean = false,
69    w: Boolean = false,
70    r: Boolean = false)(implicit p: Parameters): UInt = {
71    var config = 0
72    if (l) { config += (1 << 7) }
73    if (c) { config += (1 << 6) }
74    if (atomic) { config += (1 << 5) }
75    if (a > 0) { config += (a << 3) }
76    if (x) { config += (1 << 2) }
77    if (w) { config += (1 << 1) }
78    if (r) { config += (1 << 0) }
79    config.U(8.W)
80  }
81}
82trait PMPReadWriteMethodBare extends PMPConst {
83  def match_mask(cfg: PMPConfig, paddr: UInt) = {
84    val match_mask_c_addr = Cat(paddr, cfg.a(0)) | (((1 << PlatformGrain) - 1) >> PMPOffBits).U((paddr.getWidth + 1).W)
85    Cat(match_mask_c_addr & ~(match_mask_c_addr + 1.U), ((1 << PMPOffBits) - 1).U(PMPOffBits.W))
86  }
87
88  def write_cfg_vec(mask: Vec[UInt], addr: Vec[UInt], index: Int, oldcfg: UInt)(cfgs: UInt): UInt = {
89    val cfgVec = Wire(Vec(cfgs.getWidth/8, new PMPConfig))
90    for (i <- cfgVec.indices) {
91      val cfg_w_m_tmp = cfgs((i+1)*8-1, i*8).asUInt.asTypeOf(new PMPConfig)
92      val cfg_old_tmp = oldcfg((i+1)*8-1, i*8).asUInt.asTypeOf(new PMPConfig)
93      cfgVec(i) := cfg_old_tmp
94      when (!cfg_old_tmp.l) {
95        cfgVec(i) := cfg_w_m_tmp
96        cfgVec(i).w := cfg_w_m_tmp.w && cfg_w_m_tmp.r
97        if (CoarserGrain) { cfgVec(i).a := Cat(cfg_w_m_tmp.a(1), cfg_w_m_tmp.a.orR) }
98        when (cfgVec(i).na4_napot) {
99          mask(index + i) := match_mask(cfgVec(i), addr(index + i))
100        }
101      }
102    }
103    cfgVec.asUInt
104  }
105
106  def read_addr(cfg: PMPConfig)(addr: UInt): UInt = {
107    val G = PlatformGrain - PMPOffBits
108    require(G >= 0)
109    if (G == 0) {
110      addr
111    } else if (G >= 2) {
112      Mux(cfg.na4_napot, set_low_bits(addr, G-1), clear_low_bits(addr, G))
113    } else { // G is 1
114      Mux(cfg.off_tor, clear_low_bits(addr, G), addr)
115    }
116  }
117
118  def write_addr(next: PMPConfig, mask: UInt)(paddr: UInt, cfg: PMPConfig, addr: UInt): UInt = {
119    val locked = cfg.addr_locked(next)
120    mask := Mux(!locked, match_mask(cfg, paddr), mask)
121    Mux(!locked, paddr, addr)
122  }
123
124  def set_low_bits(data: UInt, num: Int): UInt = {
125    require(num >= 0)
126    data | ((1 << num)-1).U
127  }
128
129  /** mask the data's low num bits (lsb) */
130  def clear_low_bits(data: UInt, num: Int): UInt = {
131    require(num >= 0)
132    // use Cat instead of & with mask to avoid "Signal Width" problem
133    if (num == 0) { data }
134    else { Cat(data(data.getWidth-1, num), 0.U(num.W)) }
135  }
136}
137
138trait PMPReadWriteMethod extends PMPReadWriteMethodBare  { this: PMPBase =>
139  def write_cfg_vec(oldcfg: UInt)(cfgs: UInt): UInt = {
140    val cfgVec = Wire(Vec(cfgs.getWidth/8, new PMPConfig))
141    for (i <- cfgVec.indices) {
142      val cfg_w_tmp = cfgs((i+1)*8-1, i*8).asUInt.asTypeOf(new PMPConfig)
143      val cfg_old_tmp = oldcfg((i+1)*8-1, i*8).asUInt.asTypeOf(new PMPConfig)
144      cfgVec(i) := cfg_old_tmp
145      when (!cfg_old_tmp.l) {
146        cfgVec(i) := cfg_w_tmp
147        cfgVec(i).w := cfg_w_tmp.w && cfg_w_tmp.r
148        if (CoarserGrain) { cfgVec(i).a := Cat(cfg_w_tmp.a(1), cfg_w_tmp.a.orR) }
149      }
150    }
151    cfgVec.asUInt
152  }
153
154  /** In general, the PMP grain is 2**{G+2} bytes. when G >= 1, na4 is not selectable.
155   * When G >= 2 and cfg.a(1) is set(then the mode is napot), the bits addr(G-2, 0) read as zeros.
156   * When G >= 1 and cfg.a(1) is clear(the mode is off or tor), the addr(G-1, 0) read as zeros.
157   * The low OffBits is dropped
158   */
159  def read_addr(): UInt = {
160    read_addr(cfg)(addr)
161  }
162
163  /** addr for inside addr, drop OffBits with.
164   * compare_addr for inside addr for comparing.
165   * paddr for outside addr.
166   */
167  def write_addr(next: PMPConfig)(paddr: UInt): UInt = {
168    Mux(!cfg.addr_locked(next), paddr, addr)
169  }
170  def write_addr(paddr: UInt): UInt = {
171    Mux(!cfg.addr_locked, paddr, addr)
172  }
173}
174
175/** PMPBase for CSR unit
176  * with only read and write logic
177  */
178class PMPBase(implicit p: Parameters) extends PMPBundle with PMPReadWriteMethod {
179  val cfg = new PMPConfig
180  val addr = UInt((PMPAddrBits - PMPOffBits).W)
181
182  def gen(cfg: PMPConfig, addr: UInt) = {
183    require(addr.getWidth == this.addr.getWidth)
184    this.cfg := cfg
185    this.addr := addr
186  }
187}
188
189trait PMPMatchMethod extends PMPConst { this: PMPEntry =>
190  /** compare_addr is used to compare with input addr */
191  def compare_addr: UInt = ((addr << PMPOffBits) & ~(((1 << PlatformGrain) - 1).U(PMPAddrBits.W))).asUInt
192
193  /** size and maxSize are all log2 Size
194   * for dtlb, the maxSize is bPMXLEN which is 8
195   * for itlb and ptw, the maxSize is log2(512) ?
196   * but we may only need the 64 bytes? how to prevent the bugs?
197   * TODO: handle the special case that itlb & ptw & dcache access wider size than PMXLEN
198   */
199  def is_match(paddr: UInt, lgSize: UInt, lgMaxSize: Int, last_pmp: PMPEntry): Bool = {
200    Mux(cfg.na4_napot, napotMatch(paddr, lgSize, lgMaxSize),
201      Mux(cfg.tor, torMatch(paddr, lgSize, lgMaxSize, last_pmp), false.B))
202  }
203
204  /** generate match mask to help match in napot mode */
205  def match_mask(paddr: UInt): UInt = {
206    match_mask(cfg, paddr)
207  }
208
209  def boundMatch(paddr: UInt, lgSize: UInt, lgMaxSize: Int): Bool = {
210    if (lgMaxSize <= PlatformGrain) {
211      (paddr < compare_addr)
212    } else {
213      val highLess = (paddr >> lgMaxSize) < (compare_addr >> lgMaxSize)
214      val highEqual = (paddr >> lgMaxSize) === (compare_addr >> lgMaxSize)
215      val lowLess = (paddr(lgMaxSize-1, 0) | OneHot.UIntToOH1(lgSize, lgMaxSize))  < compare_addr(lgMaxSize-1, 0)
216      highLess || (highEqual && lowLess)
217    }
218  }
219
220  def lowerBoundMatch(paddr: UInt, lgSize: UInt, lgMaxSize: Int): Bool = {
221    !boundMatch(paddr, lgSize, lgMaxSize)
222  }
223
224  def higherBoundMatch(paddr: UInt, lgMaxSize: Int) = {
225    boundMatch(paddr, 0.U, lgMaxSize)
226  }
227
228  def torMatch(paddr: UInt, lgSize: UInt, lgMaxSize: Int, last_pmp: PMPEntry): Bool = {
229    last_pmp.lowerBoundMatch(paddr, lgSize, lgMaxSize) && higherBoundMatch(paddr, lgMaxSize)
230  }
231
232  def unmaskEqual(a: UInt, b: UInt, m: UInt) = {
233    (a & ~m) === (b & ~m)
234  }
235
236  def napotMatch(paddr: UInt, lgSize: UInt, lgMaxSize: Int) = {
237    if (lgMaxSize <= PlatformGrain) {
238      unmaskEqual(paddr, compare_addr, mask)
239    } else {
240      val lowMask = mask | OneHot.UIntToOH1(lgSize, lgMaxSize)
241      val highMatch = unmaskEqual(paddr >> lgMaxSize, compare_addr >> lgMaxSize, mask >> lgMaxSize)
242      val lowMatch = unmaskEqual(paddr(lgMaxSize-1, 0), compare_addr(lgMaxSize-1, 0), lowMask(lgMaxSize-1, 0))
243      highMatch && lowMatch
244    }
245  }
246
247  def aligned(paddr: UInt, lgSize: UInt, lgMaxSize: Int, last: PMPEntry) = {
248    if (lgMaxSize <= PlatformGrain) {
249      true.B
250    } else {
251      val lowBitsMask = OneHot.UIntToOH1(lgSize, lgMaxSize)
252      val lowerBound = ((paddr >> lgMaxSize) === (last.compare_addr >> lgMaxSize)) &&
253        ((~paddr(lgMaxSize-1, 0) & last.compare_addr(lgMaxSize-1, 0)) =/= 0.U)
254      val upperBound = ((paddr >> lgMaxSize) === (compare_addr >> lgMaxSize)) &&
255        ((compare_addr(lgMaxSize-1, 0) & (paddr(lgMaxSize-1, 0) | lowBitsMask)) =/= 0.U)
256      val torAligned = !(lowerBound || upperBound)
257      val napotAligned = (lowBitsMask & ~mask(lgMaxSize-1, 0)) === 0.U
258      Mux(cfg.na4_napot, napotAligned, torAligned)
259    }
260  }
261}
262
263/** PMPEntry for outside pmp copies
264  * with one more elements mask to help napot match
265  * TODO: make mask an element, not an method, for timing opt
266  */
267class PMPEntry(implicit p: Parameters) extends PMPBase with PMPMatchMethod {
268  val mask = UInt(PMPAddrBits.W) // help to match in napot
269
270  def write_addr(next: PMPConfig, mask: UInt)(paddr: UInt) = {
271    mask := Mux(!cfg.addr_locked(next), match_mask(paddr), mask)
272    Mux(!cfg.addr_locked(next), paddr, addr)
273  }
274
275  def write_addr(mask: UInt)(paddr: UInt) = {
276    mask := Mux(!cfg.addr_locked, match_mask(paddr), mask)
277    Mux(!cfg.addr_locked, paddr, addr)
278  }
279
280  def gen(cfg: PMPConfig, addr: UInt, mask: UInt) = {
281    require(addr.getWidth == this.addr.getWidth)
282    this.cfg := cfg
283    this.addr := addr
284    this.mask := mask
285  }
286}
287
288trait PMPMethod extends PMPConst {
289  def pmp_init() : (Vec[UInt], Vec[UInt], Vec[UInt])= {
290    val cfg = WireInit(0.U.asTypeOf(Vec(NumPMP/8, UInt(PMXLEN.W))))
291    // val addr = Wire(Vec(NumPMP, UInt((PMPAddrBits-PMPOffBits).W)))
292    // val mask = Wire(Vec(NumPMP, UInt(PMPAddrBits.W)))
293    // addr := DontCare
294    // mask := DontCare
295    // INFO: these CSRs could be uninitialized, but for difftesting with NEMU, we opt to initialize them.
296    val addr = WireInit(0.U.asTypeOf(Vec(NumPMP, UInt((PMPAddrBits-PMPOffBits).W))))
297    val mask = WireInit(0.U.asTypeOf(Vec(NumPMP, UInt(PMPAddrBits.W))))
298    (cfg, addr, mask)
299  }
300
301  def pmp_gen_mapping
302  (
303    init: () => (Vec[UInt], Vec[UInt], Vec[UInt]),
304    num: Int = 16,
305    cfgBase: Int,
306    addrBase: Int,
307    entries: Vec[PMPEntry]
308  ) = {
309    val pmpCfgPerCSR = PMXLEN / new PMPConfig().getWidth
310    def pmpCfgIndex(i: Int) = (PMXLEN / 32) * (i / pmpCfgPerCSR)
311    val init_value = init()
312    /** to fit MaskedRegMap's write, declare cfgs as Merged CSRs and split them into each pmp */
313    val cfgMerged = RegInit(init_value._1) //(Vec(num / pmpCfgPerCSR, UInt(PMXLEN.W))) // RegInit(VecInit(Seq.fill(num / pmpCfgPerCSR)(0.U(PMXLEN.W))))
314    val cfgs = WireInit(cfgMerged).asTypeOf(Vec(num, new PMPConfig()))
315    val addr = RegInit(init_value._2) // (Vec(num, UInt((PMPAddrBits-PMPOffBits).W)))
316    val mask = RegInit(init_value._3) // (Vec(num, UInt(PMPAddrBits.W)))
317
318    for (i <- entries.indices) {
319      entries(i).gen(cfgs(i), addr(i), mask(i))
320    }
321
322    val cfg_mapping = (0 until num by pmpCfgPerCSR).map(i => {Map(
323      MaskedRegMap(
324        addr = cfgBase + pmpCfgIndex(i),
325        reg = cfgMerged(i/pmpCfgPerCSR),
326        wmask = WritableMask,
327        wfn = new PMPBase().write_cfg_vec(mask, addr, i, cfgMerged(i/pmpCfgPerCSR))
328      ))
329    }).fold(Map())((a, b) => a ++ b) // ugly code, hit me if u have better codes
330
331    val addr_mapping = (0 until num).map(i => {Map(
332      MaskedRegMap(
333        addr = addrBase + i,
334        reg = addr(i),
335        wmask = WritableMask,
336        wfn = { if (i != num-1) entries(i).write_addr(entries(i+1).cfg, mask(i)) else entries(i).write_addr(mask(i)) },
337        rmask = WritableMask,
338        rfn = new PMPBase().read_addr(entries(i).cfg)
339      ))
340    }).fold(Map())((a, b) => a ++ b) // ugly code, hit me if u have better codes.
341
342    cfg_mapping ++ addr_mapping
343  }
344}
345
346class PMP(implicit p: Parameters) extends PMPXSModule with HasXSParameter with PMPMethod with PMAMethod with HasCSRConst {
347  val io = IO(new Bundle {
348    val distribute_csr = Flipped(new DistributedCSRIO())
349    val pmp = Output(Vec(NumPMP, new PMPEntry()))
350    val pma = Output(Vec(NumPMA, new PMPEntry()))
351  })
352
353  val w = io.distribute_csr.w
354
355  val pmp = Wire(Vec(NumPMP, new PMPEntry()))
356  val pma = Wire(Vec(NumPMA, new PMPEntry()))
357
358  val pmpMapping = pmp_gen_mapping(pmp_init, NumPMP, PmpcfgBase, PmpaddrBase, pmp)
359  val pmaMapping = pmp_gen_mapping(pma_init, NumPMA, PmacfgBase, PmaaddrBase, pma)
360  val mapping = pmpMapping ++ pmaMapping
361
362  val rdata = Wire(UInt(PMXLEN.W))
363  MaskedRegMap.generate(mapping, w.bits.addr, rdata, w.valid, w.bits.data)
364
365  io.pmp := pmp
366  io.pma := pma
367}
368
369class PMPReqBundle(lgMaxSize: Int = 3)(implicit p: Parameters) extends PMPBundle {
370  val addr = Output(UInt(PMPAddrBits.W))
371  val size = Output(UInt(log2Ceil(lgMaxSize+1).W))
372  val cmd = Output(TlbCmd())
373
374  def apply(addr: UInt, size: UInt, cmd: UInt) {
375    this.addr := addr
376    this.size := size
377    this.cmd := cmd
378  }
379
380  def apply(addr: UInt) { // req minimal permission and req align size
381    apply(addr, lgMaxSize.U, TlbCmd.read)
382  }
383
384}
385
386class PMPRespBundle(implicit p: Parameters) extends PMPBundle {
387  val ld = Output(Bool())
388  val st = Output(Bool())
389  val instr = Output(Bool())
390  val mmio = Output(Bool())
391  val atomic = Output(Bool())
392
393  def |(resp: PMPRespBundle): PMPRespBundle = {
394    val res = Wire(new PMPRespBundle())
395    res.ld := this.ld || resp.ld
396    res.st := this.st || resp.st
397    res.instr := this.instr || resp.instr
398    res.mmio := this.mmio || resp.mmio
399    res.atomic := this.atomic || resp.atomic
400    res
401  }
402}
403
404trait PMPCheckMethod extends PMPConst {
405  def pmp_check(cmd: UInt, cfg: PMPConfig) = {
406    val resp = Wire(new PMPRespBundle)
407    resp.ld := TlbCmd.isRead(cmd) && !TlbCmd.isAmo(cmd) && !cfg.r
408    resp.st := (TlbCmd.isWrite(cmd) || TlbCmd.isAmo(cmd)) && !cfg.w
409    resp.instr := TlbCmd.isExec(cmd) && !cfg.x
410    resp.mmio := false.B
411    resp.atomic := false.B
412    resp
413  }
414
415  def pmp_match_res(leaveHitMux: Boolean = false, valid: Bool = true.B)(
416    addr: UInt,
417    size: UInt,
418    pmpEntries: Vec[PMPEntry],
419    mode: UInt,
420    lgMaxSize: Int
421  ) = {
422    val num = pmpEntries.size
423    require(num == NumPMP)
424
425    val passThrough = if (pmpEntries.isEmpty) true.B else (mode > 1.U)
426    val pmpDefault = WireInit(0.U.asTypeOf(new PMPEntry()))
427    pmpDefault.cfg.r := passThrough
428    pmpDefault.cfg.w := passThrough
429    pmpDefault.cfg.x := passThrough
430
431    val match_vec = Wire(Vec(num+1, Bool()))
432    val cfg_vec = Wire(Vec(num+1, new PMPEntry()))
433
434    pmpEntries.zip(pmpDefault +: pmpEntries.take(num-1)).zipWithIndex.foreach{ case ((pmp, last_pmp), i) =>
435      val is_match = pmp.is_match(addr, size, lgMaxSize, last_pmp)
436      val ignore = passThrough && !pmp.cfg.l
437      val aligned = pmp.aligned(addr, size, lgMaxSize, last_pmp)
438
439      val cur = WireInit(pmp)
440      cur.cfg.r := aligned && (pmp.cfg.r || ignore)
441      cur.cfg.w := aligned && (pmp.cfg.w || ignore)
442      cur.cfg.x := aligned && (pmp.cfg.x || ignore)
443
444//      Mux(is_match, cur, prev)
445      match_vec(i) := is_match
446      cfg_vec(i) := cur
447    }
448
449    // default value
450    match_vec(num) := true.B
451    cfg_vec(num) := pmpDefault
452
453    if (leaveHitMux) {
454      ParallelPriorityMux(match_vec.map(RegEnable(_, false.B, valid)), RegEnable(cfg_vec, valid))
455    } else {
456      ParallelPriorityMux(match_vec, cfg_vec)
457    }
458  }
459}
460
461class PMPCheckerEnv(implicit p: Parameters) extends PMPBundle {
462  val mode = UInt(2.W)
463  val pmp = Vec(NumPMP, new PMPEntry())
464  val pma = Vec(NumPMA, new PMPEntry())
465
466  def apply(mode: UInt, pmp: Vec[PMPEntry], pma: Vec[PMPEntry]): Unit = {
467    this.mode := mode
468    this.pmp := pmp
469    this.pma := pma
470  }
471}
472
473class PMPCheckIO(lgMaxSize: Int)(implicit p: Parameters) extends PMPBundle {
474  val check_env = Input(new PMPCheckerEnv())
475  val req = Flipped(Valid(new PMPReqBundle(lgMaxSize))) // usage: assign the valid to fire signal
476  val resp = new PMPRespBundle()
477
478  def apply(mode: UInt, pmp: Vec[PMPEntry], pma: Vec[PMPEntry], req: Valid[PMPReqBundle]) = {
479    check_env.apply(mode, pmp, pma)
480    this.req := req
481    resp
482  }
483
484  def req_apply(valid: Bool, addr: UInt): Unit = {
485    this.req.valid := valid
486    this.req.bits.apply(addr)
487  }
488
489  def apply(mode: UInt, pmp: Vec[PMPEntry], pma: Vec[PMPEntry], valid: Bool, addr: UInt) = {
490    check_env.apply(mode, pmp, pma)
491    req_apply(valid, addr)
492    resp
493  }
494}
495
496class PMPCheckv2IO(lgMaxSize: Int)(implicit p: Parameters) extends PMPBundle {
497  val check_env = Input(new PMPCheckerEnv())
498  val req = Flipped(Valid(new PMPReqBundle(lgMaxSize))) // usage: assign the valid to fire signal
499  val resp = Output(new PMPConfig())
500
501  def apply(mode: UInt, pmp: Vec[PMPEntry], pma: Vec[PMPEntry], req: Valid[PMPReqBundle]) = {
502    check_env.apply(mode, pmp, pma)
503    this.req := req
504    resp
505  }
506
507  def req_apply(valid: Bool, addr: UInt): Unit = {
508    this.req.valid := valid
509    this.req.bits.apply(addr)
510  }
511
512  def apply(mode: UInt, pmp: Vec[PMPEntry], pma: Vec[PMPEntry], valid: Bool, addr: UInt) = {
513    check_env.apply(mode, pmp, pma)
514    req_apply(valid, addr)
515    resp
516  }
517}
518
519class PMPChecker
520(
521  lgMaxSize: Int = 3,
522  sameCycle: Boolean = false,
523  leaveHitMux: Boolean = false,
524  pmpUsed: Boolean = true
525)(implicit p: Parameters) extends PMPModule
526  with PMPCheckMethod
527  with PMACheckMethod
528{
529  require(!(leaveHitMux && sameCycle))
530  val io = IO(new PMPCheckIO(lgMaxSize))
531
532  val req = io.req.bits
533
534  val res_pmp = pmp_match_res(leaveHitMux, io.req.valid)(req.addr, req.size, io.check_env.pmp, io.check_env.mode, lgMaxSize)
535  val res_pma = pma_match_res(leaveHitMux, io.req.valid)(req.addr, req.size, io.check_env.pma, io.check_env.mode, lgMaxSize)
536
537  val resp_pmp = pmp_check(req.cmd, res_pmp.cfg)
538  val resp_pma = pma_check(req.cmd, res_pma.cfg)
539  val resp = if (pmpUsed) (resp_pmp | resp_pma) else resp_pma
540
541  if (sameCycle || leaveHitMux) {
542    io.resp := resp
543  } else {
544    io.resp := RegEnable(resp, io.req.valid)
545  }
546}
547
548/* get config with check */
549class PMPCheckerv2
550(
551  lgMaxSize: Int = 3,
552  sameCycle: Boolean = false,
553  leaveHitMux: Boolean = false
554)(implicit p: Parameters) extends PMPModule
555  with PMPCheckMethod
556  with PMACheckMethod
557{
558  require(!(leaveHitMux && sameCycle))
559  val io = IO(new PMPCheckv2IO(lgMaxSize))
560
561  val req = io.req.bits
562
563  val res_pmp = pmp_match_res(leaveHitMux, io.req.valid)(req.addr, req.size, io.check_env.pmp, io.check_env.mode, lgMaxSize)
564  val res_pma = pma_match_res(leaveHitMux, io.req.valid)(req.addr, req.size, io.check_env.pma, io.check_env.mode, lgMaxSize)
565
566  val resp = and(res_pmp, res_pma)
567
568  if (sameCycle || leaveHitMux) {
569    io.resp := resp
570  } else {
571    io.resp := RegEnable(resp, io.req.valid)
572  }
573
574  def and(pmp: PMPEntry, pma: PMPEntry): PMPConfig = {
575    val tmp_res = Wire(new PMPConfig)
576    tmp_res.l := DontCare
577    tmp_res.a := DontCare
578    tmp_res.r := pmp.cfg.r && pma.cfg.r
579    tmp_res.w := pmp.cfg.w && pma.cfg.w
580    tmp_res.x := pmp.cfg.x && pma.cfg.x
581    tmp_res.c := pma.cfg.c
582    tmp_res.atomic := pma.cfg.atomic
583    tmp_res
584  }
585}
586