xref: /XiangShan/src/main/scala/xiangshan/backend/fu/PMP.scala (revision 45f43e6e5f88874a7573ff096d1e5c2855bd16c7)
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    (cfg, addr, mask)
296  }
297
298  def pmp_gen_mapping
299  (
300    init: () => (Vec[UInt], Vec[UInt], Vec[UInt]),
301    num: Int = 16,
302    cfgBase: Int,
303    addrBase: Int,
304    entries: Vec[PMPEntry]
305  ) = {
306    val pmpCfgPerCSR = PMXLEN / new PMPConfig().getWidth
307    def pmpCfgIndex(i: Int) = (PMXLEN / 32) * (i / pmpCfgPerCSR)
308    val init_value = init()
309    /** to fit MaskedRegMap's write, declare cfgs as Merged CSRs and split them into each pmp */
310    val cfgMerged = RegInit(init_value._1) //(Vec(num / pmpCfgPerCSR, UInt(PMXLEN.W))) // RegInit(VecInit(Seq.fill(num / pmpCfgPerCSR)(0.U(PMXLEN.W))))
311    val cfgs = WireInit(cfgMerged).asTypeOf(Vec(num, new PMPConfig()))
312    val addr = RegInit(init_value._2) // (Vec(num, UInt((PMPAddrBits-PMPOffBits).W)))
313    val mask = RegInit(init_value._3) // (Vec(num, UInt(PMPAddrBits.W)))
314
315    for (i <- entries.indices) {
316      entries(i).gen(cfgs(i), addr(i), mask(i))
317    }
318
319    val cfg_mapping = (0 until num by pmpCfgPerCSR).map(i => {Map(
320      MaskedRegMap(
321        addr = cfgBase + pmpCfgIndex(i),
322        reg = cfgMerged(i/pmpCfgPerCSR),
323        wmask = WritableMask,
324        wfn = new PMPBase().write_cfg_vec(mask, addr, i, cfgMerged(i/pmpCfgPerCSR))
325      ))
326    }).fold(Map())((a, b) => a ++ b) // ugly code, hit me if u have better codes
327
328    val addr_mapping = (0 until num).map(i => {Map(
329      MaskedRegMap(
330        addr = addrBase + i,
331        reg = addr(i),
332        wmask = WritableMask,
333        wfn = { if (i != num-1) entries(i).write_addr(entries(i+1).cfg, mask(i)) else entries(i).write_addr(mask(i)) },
334        rmask = WritableMask,
335        rfn = new PMPBase().read_addr(entries(i).cfg)
336      ))
337    }).fold(Map())((a, b) => a ++ b) // ugly code, hit me if u have better codes.
338
339    cfg_mapping ++ addr_mapping
340  }
341}
342
343class PMP(implicit p: Parameters) extends PMPXSModule with HasXSParameter with PMPMethod with PMAMethod with HasCSRConst {
344  val io = IO(new Bundle {
345    val distribute_csr = Flipped(new DistributedCSRIO())
346    val pmp = Output(Vec(NumPMP, new PMPEntry()))
347    val pma = Output(Vec(NumPMA, new PMPEntry()))
348  })
349
350  val w = io.distribute_csr.w
351
352  val pmp = Wire(Vec(NumPMP, new PMPEntry()))
353  val pma = Wire(Vec(NumPMA, new PMPEntry()))
354
355  val pmpMapping = pmp_gen_mapping(pmp_init, NumPMP, PmpcfgBase, PmpaddrBase, pmp)
356  val pmaMapping = pmp_gen_mapping(pma_init, NumPMA, PmacfgBase, PmaaddrBase, pma)
357  val mapping = pmpMapping ++ pmaMapping
358
359  val rdata = Wire(UInt(PMXLEN.W))
360  MaskedRegMap.generate(mapping, w.bits.addr, rdata, w.valid, w.bits.data)
361
362  io.pmp := pmp
363  io.pma := pma
364}
365
366class PMPReqBundle(lgMaxSize: Int = 3)(implicit p: Parameters) extends PMPBundle {
367  val addr = Output(UInt(PMPAddrBits.W))
368  val size = Output(UInt(log2Ceil(lgMaxSize+1).W))
369  val cmd = Output(TlbCmd())
370
371  def apply(addr: UInt, size: UInt, cmd: UInt) {
372    this.addr := addr
373    this.size := size
374    this.cmd := cmd
375  }
376
377  def apply(addr: UInt) { // req minimal permission and req align size
378    apply(addr, lgMaxSize.U, TlbCmd.read)
379  }
380
381}
382
383class PMPRespBundle(implicit p: Parameters) extends PMPBundle {
384  val ld = Output(Bool())
385  val st = Output(Bool())
386  val instr = Output(Bool())
387  val mmio = Output(Bool())
388  val atomic = Output(Bool())
389
390  def |(resp: PMPRespBundle): PMPRespBundle = {
391    val res = Wire(new PMPRespBundle())
392    res.ld := this.ld || resp.ld
393    res.st := this.st || resp.st
394    res.instr := this.instr || resp.instr
395    res.mmio := this.mmio || resp.mmio
396    res.atomic := this.atomic || resp.atomic
397    res
398  }
399}
400
401trait PMPCheckMethod extends PMPConst {
402  def pmp_check(cmd: UInt, cfg: PMPConfig) = {
403    val resp = Wire(new PMPRespBundle)
404    resp.ld := TlbCmd.isRead(cmd) && !TlbCmd.isAmo(cmd) && !cfg.r
405    resp.st := (TlbCmd.isWrite(cmd) || TlbCmd.isAmo(cmd)) && !cfg.w
406    resp.instr := TlbCmd.isExec(cmd) && !cfg.x
407    resp.mmio := false.B
408    resp.atomic := false.B
409    resp
410  }
411
412  def pmp_match_res(leaveHitMux: Boolean = false, valid: Bool = true.B)(
413    addr: UInt,
414    size: UInt,
415    pmpEntries: Vec[PMPEntry],
416    mode: UInt,
417    lgMaxSize: Int
418  ) = {
419    val num = pmpEntries.size
420    require(num == NumPMP)
421
422    val passThrough = if (pmpEntries.isEmpty) true.B else (mode > 1.U)
423    val pmpDefault = WireInit(0.U.asTypeOf(new PMPEntry()))
424    pmpDefault.cfg.r := passThrough
425    pmpDefault.cfg.w := passThrough
426    pmpDefault.cfg.x := passThrough
427
428    val match_vec = Wire(Vec(num+1, Bool()))
429    val cfg_vec = Wire(Vec(num+1, new PMPEntry()))
430
431    pmpEntries.zip(pmpDefault +: pmpEntries.take(num-1)).zipWithIndex.foreach{ case ((pmp, last_pmp), i) =>
432      val is_match = pmp.is_match(addr, size, lgMaxSize, last_pmp)
433      val ignore = passThrough && !pmp.cfg.l
434      val aligned = pmp.aligned(addr, size, lgMaxSize, last_pmp)
435
436      val cur = WireInit(pmp)
437      cur.cfg.r := aligned && (pmp.cfg.r || ignore)
438      cur.cfg.w := aligned && (pmp.cfg.w || ignore)
439      cur.cfg.x := aligned && (pmp.cfg.x || ignore)
440
441//      Mux(is_match, cur, prev)
442      match_vec(i) := is_match
443      cfg_vec(i) := cur
444    }
445
446    // default value
447    match_vec(num) := true.B
448    cfg_vec(num) := pmpDefault
449
450    if (leaveHitMux) {
451      ParallelPriorityMux(match_vec.map(RegEnable(_, false.B, valid)), RegEnable(cfg_vec, valid))
452    } else {
453      ParallelPriorityMux(match_vec, cfg_vec)
454    }
455  }
456}
457
458class PMPCheckerEnv(implicit p: Parameters) extends PMPBundle {
459  val mode = UInt(2.W)
460  val pmp = Vec(NumPMP, new PMPEntry())
461  val pma = Vec(NumPMA, new PMPEntry())
462
463  def apply(mode: UInt, pmp: Vec[PMPEntry], pma: Vec[PMPEntry]): Unit = {
464    this.mode := mode
465    this.pmp := pmp
466    this.pma := pma
467  }
468}
469
470class PMPCheckIO(lgMaxSize: Int)(implicit p: Parameters) extends PMPBundle {
471  val check_env = Input(new PMPCheckerEnv())
472  val req = Flipped(Valid(new PMPReqBundle(lgMaxSize))) // usage: assign the valid to fire signal
473  val resp = new PMPRespBundle()
474
475  def apply(mode: UInt, pmp: Vec[PMPEntry], pma: Vec[PMPEntry], req: Valid[PMPReqBundle]) = {
476    check_env.apply(mode, pmp, pma)
477    this.req := req
478    resp
479  }
480
481  def req_apply(valid: Bool, addr: UInt): Unit = {
482    this.req.valid := valid
483    this.req.bits.apply(addr)
484  }
485
486  def apply(mode: UInt, pmp: Vec[PMPEntry], pma: Vec[PMPEntry], valid: Bool, addr: UInt) = {
487    check_env.apply(mode, pmp, pma)
488    req_apply(valid, addr)
489    resp
490  }
491}
492
493class PMPCheckv2IO(lgMaxSize: Int)(implicit p: Parameters) extends PMPBundle {
494  val check_env = Input(new PMPCheckerEnv())
495  val req = Flipped(Valid(new PMPReqBundle(lgMaxSize))) // usage: assign the valid to fire signal
496  val resp = Output(new PMPConfig())
497
498  def apply(mode: UInt, pmp: Vec[PMPEntry], pma: Vec[PMPEntry], req: Valid[PMPReqBundle]) = {
499    check_env.apply(mode, pmp, pma)
500    this.req := req
501    resp
502  }
503
504  def req_apply(valid: Bool, addr: UInt): Unit = {
505    this.req.valid := valid
506    this.req.bits.apply(addr)
507  }
508
509  def apply(mode: UInt, pmp: Vec[PMPEntry], pma: Vec[PMPEntry], valid: Bool, addr: UInt) = {
510    check_env.apply(mode, pmp, pma)
511    req_apply(valid, addr)
512    resp
513  }
514}
515
516class PMPChecker
517(
518  lgMaxSize: Int = 3,
519  sameCycle: Boolean = false,
520  leaveHitMux: Boolean = false,
521  pmpUsed: Boolean = true
522)(implicit p: Parameters) extends PMPModule
523  with PMPCheckMethod
524  with PMACheckMethod
525{
526  require(!(leaveHitMux && sameCycle))
527  val io = IO(new PMPCheckIO(lgMaxSize))
528
529  val req = io.req.bits
530
531  val res_pmp = pmp_match_res(leaveHitMux, io.req.valid)(req.addr, req.size, io.check_env.pmp, io.check_env.mode, lgMaxSize)
532  val res_pma = pma_match_res(leaveHitMux, io.req.valid)(req.addr, req.size, io.check_env.pma, io.check_env.mode, lgMaxSize)
533
534  val resp_pmp = pmp_check(req.cmd, res_pmp.cfg)
535  val resp_pma = pma_check(req.cmd, res_pma.cfg)
536  val resp = if (pmpUsed) (resp_pmp | resp_pma) else resp_pma
537
538  if (sameCycle || leaveHitMux) {
539    io.resp := resp
540  } else {
541    io.resp := RegEnable(resp, io.req.valid)
542  }
543}
544
545/* get config with check */
546class PMPCheckerv2
547(
548  lgMaxSize: Int = 3,
549  sameCycle: Boolean = false,
550  leaveHitMux: Boolean = false
551)(implicit p: Parameters) extends PMPModule
552  with PMPCheckMethod
553  with PMACheckMethod
554{
555  require(!(leaveHitMux && sameCycle))
556  val io = IO(new PMPCheckv2IO(lgMaxSize))
557
558  val req = io.req.bits
559
560  val res_pmp = pmp_match_res(leaveHitMux, io.req.valid)(req.addr, req.size, io.check_env.pmp, io.check_env.mode, lgMaxSize)
561  val res_pma = pma_match_res(leaveHitMux, io.req.valid)(req.addr, req.size, io.check_env.pma, io.check_env.mode, lgMaxSize)
562
563  val resp = and(res_pmp, res_pma)
564
565  if (sameCycle || leaveHitMux) {
566    io.resp := resp
567  } else {
568    io.resp := RegEnable(resp, io.req.valid)
569  }
570
571  def and(pmp: PMPEntry, pma: PMPEntry): PMPConfig = {
572    val tmp_res = Wire(new PMPConfig)
573    tmp_res.l := DontCare
574    tmp_res.a := DontCare
575    tmp_res.r := pmp.cfg.r && pma.cfg.r
576    tmp_res.w := pmp.cfg.w && pma.cfg.w
577    tmp_res.x := pmp.cfg.x && pma.cfg.x
578    tmp_res.c := pma.cfg.c
579    tmp_res.atomic := pma.cfg.atomic
580    tmp_res
581  }
582}
583