4010e3775e4a980f19beeb9cd55425f773c50a89
[soc.git] / src / TLB / ariane / tlb_content.py
1 from nmigen import Signal, Module, Cat, Const, Elaboratable
2
3 from TLB.ariane.ptw import TLBUpdate, PTE
4
5
6 class TLBEntry:
7 def __init__(self, asid_width):
8 self.asid = Signal(asid_width,name="ent_asid")
9 # SV48 defines four levels of page tables
10 self.vpn0 = Signal(9,name="ent_vpn0")
11 self.vpn1 = Signal(9,name="ent_vpn1")
12 self.vpn2 = Signal(9,name="ent_vpn2")
13 self.vpn3 = Signal(9,name="ent_vpn3")
14 self.is_2M = Signal(name="ent_is_2M")
15 self.is_1G = Signal(name="ent_is_1G")
16 self.is_512G = Signal(name="ent_is_512G")
17 self.valid = Signal(name="ent_valid")
18
19 def flatten(self):
20 return Cat(*self.ports())
21
22 def eq(self, x):
23 return self.flatten().eq(x.flatten())
24
25 def ports(self):
26 return [self.asid, self.vpn0, self.vpn1, self.vpn2,
27 self.is_2M, self.is_1G, self.valid]
28
29
30 class TLBContent(Elaboratable):
31 def __init__(self, pte_width, asid_width):
32 self.asid_width = asid_width
33 self.pte_width = pte_width
34 self.flush_i = Signal() # Flush signal
35 # Update TLB
36 self.update_i = TLBUpdate(asid_width)
37 self.vpn3 = Signal(9)
38 self.vpn2 = Signal(9)
39 self.vpn1 = Signal(9)
40 self.vpn0 = Signal(9)
41 self.replace_en_i = Signal() # replace the following entry,
42 # set by replacement strategy
43 # Lookup signals
44 self.lu_asid_i = Signal(asid_width)
45 self.lu_content_o = Signal(pte_width)
46 self.lu_is_512G_o = Signal()
47 self.lu_is_2M_o = Signal()
48 self.lu_is_1G_o = Signal()
49 self.lu_hit_o = Signal()
50
51 def elaborate(self, platform):
52 m = Module()
53
54 tags = TLBEntry(self.asid_width)
55 #TODO signal names, RecordObject
56 print("TAGS:")
57 #print(tags)
58
59
60 content = Signal(self.pte_width)
61
62 m.d.comb += [self.lu_hit_o.eq(0),
63 self.lu_is_2M_o.eq(0),
64 self.lu_is_1G_o.eq(0)]
65
66 # temporaries for 1st level match
67 asid_ok = Signal(reset_less=True)
68 vpn2_ok = Signal(reset_less=True)
69 tags_ok = Signal(reset_less=True)
70 vpn2_hit = Signal(reset_less=True)
71 m.d.comb += [tags_ok.eq(tags.valid),
72 asid_ok.eq(tags.asid == self.lu_asid_i),
73 vpn2_ok.eq(tags.vpn2 == self.vpn2),
74 vpn2_hit.eq(tags_ok & asid_ok & vpn2_ok)]
75 # temporaries for 2nd level match
76 vpn1_ok = Signal(reset_less=True)
77 tags_2M = Signal(reset_less=True)
78 vpn0_ok = Signal(reset_less=True)
79 vpn0_or_2M = Signal(reset_less=True)
80 m.d.comb += [vpn1_ok.eq(self.vpn1 == tags.vpn1),
81 tags_2M.eq(tags.is_2M),
82 vpn0_ok.eq(self.vpn0 == tags.vpn0),
83 vpn0_or_2M.eq(tags_2M | vpn0_ok)]
84 # TODO temporaries for 3rd level match
85
86 # first level match, this may be a giga page,
87 # check the ASID flags as well
88 with m.If(vpn2_hit):
89 # second level
90 with m.If (tags.is_1G):
91 m.d.comb += [ self.lu_content_o.eq(content),
92 self.lu_is_1G_o.eq(1),
93 self.lu_hit_o.eq(1),
94 ]
95 # not a giga page hit so check further
96 with m.Elif(vpn1_ok):
97 # this could be a 2 mega page hit or a 4 kB hit
98 # output accordingly
99 with m.If(vpn0_or_2M):
100 m.d.comb += [ self.lu_content_o.eq(content),
101 self.lu_is_2M_o.eq(tags.is_2M),
102 self.lu_hit_o.eq(1),
103 ]
104 # ------------------
105 # Update or Flush
106 # ------------------
107
108 # temporaries
109 replace_valid = Signal(reset_less=True)
110 m.d.comb += replace_valid.eq(self.update_i.valid & self.replace_en_i)
111
112 # flush
113 with m.If (self.flush_i):
114 # invalidate (flush) conditions: all if zero or just this ASID
115 with m.If (self.lu_asid_i == Const(0, self.asid_width) |
116 (self.lu_asid_i == tags.asid)):
117 m.d.sync += tags.valid.eq(0)
118
119 # normal replacement
120 with m.Elif(replace_valid):
121 m.d.sync += [ # update tag array
122 tags.asid.eq(self.update_i.asid),
123 tags.vpn3.eq(self.update_i.vpn[27:36]),
124 tags.vpn2.eq(self.update_i.vpn[18:27]),
125 tags.vpn1.eq(self.update_i.vpn[9:18]),
126 tags.vpn0.eq(self.update_i.vpn[0:9]),
127 tags.is_512G.eq(self.update_i.is_512G),
128 tags.is_1G.eq(self.update_i.is_1G),
129 tags.is_2M.eq(self.update_i.is_2M),
130 tags.valid.eq(1),
131 # and content as well
132 content.eq(self.update_i.content.flatten())
133 ]
134 return m
135
136 def ports(self):
137 return [self.flush_i,
138 self.lu_asid_i,
139 self.lu_is_2M_o, self.lu_is_1G_o,self.lu_is_512G_o, self.lu_hit_o,
140 ] + self.update_i.content.ports() + self.update_i.ports()