ParaNut SystemC Model
A SystemC Model of the ParaNut architecture
ptw.h
Go to the documentation of this file.
1 /*************************************************************************
2 
3  This file is part of the ParaNut project.
4 
5  Copyright (C) 2020-2022 Christian H. Meyer <christian.meyer@hs-augsburg.de>
6  Hochschule Augsburg, University of Applied Sciences
7 
8  Description:
9  This is a SystemC model of the Page Table Walker (PTW) of the ParaNut,
10  which is is closely coupled to the MBusIf.
11  At first, it does a lookup in the Translation Lookaside Buffer. If the virtual
12  address is not found, it walks the page table.
13 
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  GNU General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <http://www.gnu.org/licenses/>.
26 
27  *************************************************************************/
28 
29 #pragma once
30 
31 #include "paranut-config.h"
32 #include "base.h"
33 
34 #define MAX_ADR_LENGTH 20U
35 
36 #define PHYSADR_SP_LENGTH 10U
37 #define PHYSADR_LENGTH 20U
38 
39 #define VIRTADR_SP_LENGTH 10U
40 #define VIRTADR_LENGTH 20U
41 
42 #define TAG_SP_LENGTH (PHYSADR_SP_LENGTH - CFG_MMU_TLB_WAYS_LD)
43 #define TAG_LENGTH (PHYSADR_LENGTH - CFG_MMU_TLB_WAYS_LD)
44 
45 #define CFG_MMU_TLB_CNTR_LENGTH (CFG_MMU_TLB_REPLACE_LRU ? (6 + CFG_MMU_SP_TLB_WAYS_LD) : (6 + CFG_MMU_TLB_WAYS_LD))
46 #define CFG_MMU_SP_TLB_CNTR_LENGTH (CFG_MMU_SP_TLB_REPLACE_LRU ? (6 + CFG_MMU_TLB_WAYS_LD) : (6 + CFG_MMU_SP_TLB_WAYS_LD))
47 
48 #define CNTR_LENGTH MAX(CFG_MMU_TLB_CNTR_LENGTH, CFG_MMU_SP_TLB_CNTR_LENGTH)
49 
50 
51 typedef enum {
52  MmuIdle = 0,
63 } EPtwState;
64 
65 struct SVirtAdr
66 {
67  sc_uint<10> vpn1;
68  sc_uint<10> vpn0;
69  sc_uint<12> page_offset;
70 
71  sc_uint<20> vpn () { return (this->vpn1, this->vpn0); }
72  sc_uint<22> superpage_offset () { return (this->vpn0, this->page_offset); }
73  sc_uint<12> page_table_offset_vpn1 () { return (this->vpn1, sc_uint<2>(0)); }
74  sc_uint<12> page_table_offset_vpn0 () { return (this->vpn0, sc_uint<2>(0)); }
75 
76  operator const sc_uint<32> () { return (vpn1, vpn0, page_offset); };
77  SVirtAdr operator= (const sc_uint<32> &data)
78  {
79  vpn1 = data.range (31, 22);
80  vpn0 = data.range (21, 12);
81  page_offset = data.range (11, 0);
82 
83  return *this;
84  };
85 
86  bool operator==(const SVirtAdr &t) const
87  {
88  return C(vpn1) && C(vpn0) && C(page_offset);
89  }
90 
91  friend ostream &operator<<(ostream &os, const SVirtAdr &t)
92  {
93  os << "vpn1=" << t.vpn1 << "vpn0=" << t.vpn0 << "page_offset=" << t.page_offset;
94  return os;
95  }
96 
97 #ifndef __SYNTHESIS__
98  // Trace...
99  friend void sc_trace(sc_trace_file *tf, const SVirtAdr &t, const std::string &name)
100  {
101  PN_TRACE_R (tf, t, vpn1, name);
102  PN_TRACE_R (tf, t, vpn0, name);
103  PN_TRACE_R (tf, t, page_offset, name);
104  }
105 #endif
106 };
107 
108 // Page Table Entry
109 struct SPte
110 {
111  sc_uint<12> ppn1;
112  sc_uint<10> ppn0;
114  bool v;
115  bool g;
116  bool r;
117  bool w;
118  bool u;
119  bool x;
120  bool a;
121  bool d;
122 
123  sc_uint<32> page_frame() { return (this->ppn1.range(9, 0), this->ppn0, sc_uint<12>(0)); }
124  sc_uint<20> page_frame_number() { return (this->ppn1.range(9, 0), this->ppn0); }
125  sc_uint<32> table_pointer() { return (this->ppn1.range(9, 0), this->ppn0, sc_uint<12>(0)); }
126 
127  operator sc_uint<32>()
128  {
129  return (ppn1, ppn0, reserved_for_software, d, a, g, u, x, w, r, v);
130  }
131 
132  bool is_invalid() { return !this->v || (!this->r && this->w); };
133  bool is_misaligned_superpage() { return !this->is_aligned_superpage(); };
134  bool is_aligned_superpage() { return this->ppn0 == 0; }
135  bool is_leaf_pte() { return this->r || this->x; };
136 
137  SPte operator=(const sc_uint<32> &data)
138  {
139  ppn1 = data.range (31, 20);
140  ppn0 = data.range (19, 10);
141  reserved_for_software = data.range (9, 8);
142  d = data[7];
143  a = data[6];
144  g = data[5];
145  u = data[4];
146  x = data[3];
147  w = data[2];
148  r = data[1];
149  v = data[0];
150 
151  return *this;
152  }
153 
154  bool operator==(const SPte &t) const
155  {
156  return C(ppn1) && C(ppn0) && C(reserved_for_software) && C(v) && C(r) && C(w) && C(x) && C(u) && C(g) && C(a) && C(d);
157  }
158 
159  friend ostream &operator<<(ostream &os, const SPte &t)
160  {
161  os << "ppn1=" << t.ppn1 << ",ppn0=" << t.ppn0
162  << ",reserved_for_software=" << t.reserved_for_software
163  << "v=" << t.v << ",g=" << t.g << "," << ",d:" << t.d << ",a:" << t.a << ",g:" << t.g
164  << ",u:" << t.u << ",x:" << t.x << ",w:" << t.w << ",r:" << t.r << ",v:" << t.v;
165  return os;
166  }
167 
168 #ifndef __SYNTHESIS__
169  // Trace...
170  friend void sc_trace(sc_trace_file *tf, const SPte &t, const std::string &name)
171  {
172  PN_TRACE_R (tf, t, ppn1, name);
173  PN_TRACE_R (tf, t, ppn0, name);
174  PN_TRACE_R (tf, t, reserved_for_software, name);
175  PN_TRACE_R (tf, t, g, name);
176  PN_TRACE_R (tf, t, v, name);
177  PN_TRACE_R (tf, t, d, name);
178  PN_TRACE_R (tf, t, a, name);
179  PN_TRACE_R (tf, t, g, name);
180  PN_TRACE_R (tf, t, u, name);
181  PN_TRACE_R (tf, t, r, name);
182  PN_TRACE_R (tf, t, w, name);
183  PN_TRACE_R (tf, t, x, name);
184  }
185 #endif
186 };
187 
188 SC_MODULE(MPtw)
189 {
190 public:
191  sc_in<bool> clk;
192  sc_in<bool> reset;
193 
194  // Wishbone ports
195  sc_out<bool> wb_cyc_o; // cycle valid output
196  sc_out<bool> wb_stb_o; // strobe output
197  sc_out<bool> wb_we_o; // indicates write transfer
198  sc_out<sc_uint<3> > wb_cti_o; // cycle type identifier
199  sc_out<sc_uint<2> > wb_bte_o; // burst type extension
200  sc_out<sc_uint<CFG_MEMU_BUSIF_WIDTH/8> > wb_sel_o; // byte select outputs
201  sc_out<sc_uint<32> > wb_adr_o; // address bus outputs
202  sc_out<sc_uint<CFG_MEMU_BUSIF_WIDTH> > wb_dat_o; // output data bus
203 
204  sc_in<bool> wb_ack_i; // normal termination
205  sc_in<sc_uint<CFG_MEMU_BUSIF_WIDTH> > wb_dat_i; // input data bus
206 
207 
208  // To MemU
209  sc_in<sc_uint<20> > memu_root_ppn;
210 
211  // To BusIf
212  sc_in<bool> busif_req;
213  sc_out<bool> busif_ack;
214  sc_in<sc_uint<32> > busif_virt_adr;
215  sc_out<sc_uint<32> > busif_phys_adr;
216  sc_out<bool> busif_ac_r;
217  sc_out<bool> busif_ac_w;
218  sc_out<bool> busif_ac_x;
219  sc_out<bool> busif_ac_u;
220  sc_out<bool> busif_ac_d;
221  sc_out<bool> busif_ac_a;
222 
223  // To TLB
224  sc_out<bool> tlb_req, tlb_wr;
225  sc_out<sc_uint<20> > tlb_va_o;
226  sc_out<sc_uint<20> > tlb_pa_o;
227  sc_out<bool> tlb_superpage_o;
228  sc_out<bool> tlb_ac_r_o;
229  sc_out<bool> tlb_ac_w_o;
230  sc_out<bool> tlb_ac_x_o;
231  sc_out<bool> tlb_ac_u_o;
232  sc_out<bool> tlb_ac_d_o;
233  sc_out<bool> tlb_ac_a_o;
234 
235  sc_in<bool> tlb_superpage_i;
236  sc_in<sc_uint<20> > tlb_adr_i;
237  sc_in<bool> tlb_hit;
238  sc_in<bool> tlb_miss;
239 
240  sc_in<bool> tlb_ac_r_i;
241  sc_in<bool> tlb_ac_w_i;
242  sc_in<bool> tlb_ac_x_i;
243  sc_in<bool> tlb_ac_u_i;
244  sc_in<bool> tlb_ac_d_i;
245  sc_in<bool> tlb_ac_a_i;
246 
247  SC_HAS_PROCESS(MPtw);
248  MPtw(sc_module_name name)
249  // : sc_module(name), tlb("TLB")
250  {
251  SC_METHOD(OutputMethod);
252  sensitive << pte << state << virtadr;
253  SC_METHOD(TransitionMethod);
254  sensitive << clk.pos ();
255  };
256 
257  void Trace(sc_trace_file * tf, int level = 1);
258 
259 protected:
260  void OutputMethod();
261  void TransitionMethod();
262 
263  sc_signal<sc_uint<32> > pte;
264  sc_signal<sc_uint<32> > virtadr;
265  sc_signal<sc_uint<4> > state;
266 
267  sc_signal<int> state_trace;
268 };
269 
270 
271 
272 
Helpers, Makros and performance measuring Classes used in most ParaNut files.
sc_signal< bool > reset
Definition: dm_tb.cpp:54
sc_signal< bool > clk
Definition: dm_tb.cpp:54
#define CFG_MEMU_BUSIF_WIDTH
Busif Data Width.
Definition: paranut-config.h:228
#define C(MEMBER)
Convenient macro to compare a member variable with the member of input t.
Definition: base.h:158
#define PN_TRACE_R(TF, OBJ, MEMBER, STR)
Helper macro for recursively calling sc_trace in own types/structs.
Definition: base.h:255
Configuration Makros used in most ParaNut files.
EPtwState
Definition: ptw.h:51
@ MmuSuccessPage
Definition: ptw.h:62
@ MmuTlbLookup
Definition: ptw.h:53
@ MmuReqPte2
Definition: ptw.h:58
@ MmuValidate2
Definition: ptw.h:59
@ MmuTlbHit
Definition: ptw.h:54
@ MmuTlbHitSuperpage
Definition: ptw.h:55
@ MmuReqPte1
Definition: ptw.h:56
@ MmuSuccessSuperpage
Definition: ptw.h:61
@ MmuFault
Definition: ptw.h:60
@ MmuIdle
Definition: ptw.h:52
@ MmuValidate1
Definition: ptw.h:57
Definition: ptw.h:110
SPte operator=(const sc_uint< 32 > &data)
Definition: ptw.h:137
bool r
Definition: ptw.h:116
bool v
Definition: ptw.h:114
bool d
Definition: ptw.h:121
sc_uint< 20 > page_frame_number()
Definition: ptw.h:124
bool g
Definition: ptw.h:115
bool x
Definition: ptw.h:119
friend ostream & operator<<(ostream &os, const SPte &t)
Definition: ptw.h:159
sc_uint< 32 > table_pointer()
Definition: ptw.h:125
friend void sc_trace(sc_trace_file *tf, const SPte &t, const std::string &name)
Definition: ptw.h:170
bool is_misaligned_superpage()
Definition: ptw.h:133
sc_uint< 2 > reserved_for_software
Definition: ptw.h:113
sc_uint< 10 > ppn0
Definition: ptw.h:112
bool w
Definition: ptw.h:117
bool u
Definition: ptw.h:118
bool is_aligned_superpage()
Definition: ptw.h:134
bool operator==(const SPte &t) const
Definition: ptw.h:154
bool is_leaf_pte()
Definition: ptw.h:135
sc_uint< 12 > ppn1
Definition: ptw.h:111
bool a
Definition: ptw.h:120
sc_uint< 32 > page_frame()
Definition: ptw.h:123
bool is_invalid()
Definition: ptw.h:132
Definition: ptw.h:66
sc_uint< 12 > page_table_offset_vpn0()
Definition: ptw.h:74
sc_uint< 20 > vpn()
Definition: ptw.h:71
sc_uint< 10 > vpn0
Definition: ptw.h:68
sc_uint< 22 > superpage_offset()
Definition: ptw.h:72
sc_uint< 10 > vpn1
Definition: ptw.h:67
sc_uint< 12 > page_offset
Definition: ptw.h:69
friend ostream & operator<<(ostream &os, const SVirtAdr &t)
Definition: ptw.h:91
friend void sc_trace(sc_trace_file *tf, const SVirtAdr &t, const std::string &name)
Definition: ptw.h:99
bool operator==(const SVirtAdr &t) const
Definition: ptw.h:86
sc_uint< 12 > page_table_offset_vpn1()
Definition: ptw.h:73
SVirtAdr operator=(const sc_uint< 32 > &data)
Definition: ptw.h:77
sc_trace_file * tf
Definition: tlb_tb.cpp:94