a879787041af8b5c7e91b731788fe2cc32fa006d
1 # SPDX-License-Identifier: LGPLv3+
2 # Copyright (C) 2020, 2021 Luke Kenneth Casson Leighton <lkcl@lkcl.net>
3 # Copyright (C) 2021 Tobias Platen
4 # Funded by NLnet http://nlnet.nl
5 """core of the python-based POWER9 simulator
7 this is part of a cycle-accurate POWER9 simulator. its primary purpose is
8 not speed, it is for both learning and educational purposes, as well as
9 a method of verifying the HDL.
13 * https://bugs.libre-soc.org/show_bug.cgi?id=604
16 from nmigen
.back
.pysim
import Settle
18 from soc
.decoder
.selectable_int
import (FieldSelectableInt
, SelectableInt
,
20 from soc
.decoder
.helpers
import exts
, gtu
, ltu
, undefined
21 from soc
.decoder
.isa
.mem
import Mem
26 # very quick, TODO move to SelectableInt utils later
27 def genmask(shift
, size
):
28 res
= SelectableInt(0, size
)
31 res
[size
-1-i
] = SelectableInt(1, 1)
37 //Accessing 2nd double word of partition table (pate1)
38 //Ref: Power ISA Manual v3.0B, Book-III, section 5.7.6.1
40 // ====================================================
41 // -----------------------------------------------
42 // | /// | PATB | /// | PATS |
43 // -----------------------------------------------
45 // PATB[4:51] holds the base address of the Partition Table,
46 // right shifted by 12 bits.
47 // This is because the address of the Partition base is
48 // 4k aligned. Hence, the lower 12bits, which are always
49 // 0 are ommitted from the PTCR.
51 // Thus, The Partition Table Base is obtained by (PATB << 12)
53 // PATS represents the partition table size right-shifted by 12 bits.
54 // The minimal size of the partition table is 4k.
55 // Thus partition table size = (1 << PATS + 12).
58 // ====================================================
59 // 0 PATE0 63 PATE1 127
60 // |----------------------|----------------------|
62 // |----------------------|----------------------|
64 // |----------------------|----------------------|
66 // |----------------------|----------------------|
70 // |----------------------|----------------------|
72 // |----------------------|----------------------|
74 // The effective LPID forms the index into the Partition Table.
76 // Each entry in the partition table contains 2 double words, PATE0, PATE1,
77 // corresponding to that partition.
79 // In case of Radix, The structure of PATE0 and PATE1 is as follows.
82 // -----------------------------------------------
83 // |1|RTS1|/| RPDB | RTS2 | RPDS |
84 // -----------------------------------------------
85 // 0 1 2 3 4 55 56 58 59 63
87 // HR[0] : For Radix Page table, first bit should be 1.
88 // RTS1[1:2] : Gives one fragment of the Radix treesize
89 // RTS2[56:58] : Gives the second fragment of the Radix Tree size.
90 // RTS = (RTS1 << 3 + RTS2) + 31.
92 // RPDB[4:55] = Root Page Directory Base.
93 // RPDS = Logarithm of Root Page Directory Size right shifted by 3.
94 // Thus, Root page directory size = 1 << (RPDS + 3).
98 // -----------------------------------------------
99 // |///| PRTB | // | PRTS |
100 // -----------------------------------------------
101 // 0 3 4 51 52 58 59 63
103 // PRTB[4:51] = Process Table Base. This is aligned to size.
104 // PRTS[59: 63] = Process Table Size right shifted by 12.
105 // Minimal size of the process table is 4k.
106 // Process Table Size = (1 << PRTS + 12).
109 // Computing the size aligned Process Table Base:
110 // table_base = (PRTB & ~((1 << PRTS) - 1)) << 12
111 // Thus, the lower 12+PRTS bits of table_base will
115 //Ref: Power ISA Manual v3.0B, Book-III, section 5.7.6.2
118 // ==========================
119 // 0 PRTE0 63 PRTE1 127
120 // |----------------------|----------------------|
122 // |----------------------|----------------------|
124 // |----------------------|----------------------|
126 // |----------------------|----------------------|
130 // |----------------------|----------------------|
132 // |----------------------|----------------------|
134 // The effective Process id (PID) forms the index into the Process Table.
136 // Each entry in the partition table contains 2 double words, PRTE0, PRTE1,
137 // corresponding to that process
139 // In case of Radix, The structure of PRTE0 and PRTE1 is as follows.
142 // -----------------------------------------------
143 // |/|RTS1|/| RPDB | RTS2 | RPDS |
144 // -----------------------------------------------
145 // 0 1 2 3 4 55 56 58 59 63
147 // RTS1[1:2] : Gives one fragment of the Radix treesize
148 // RTS2[56:58] : Gives the second fragment of the Radix Tree size.
149 // RTS = (RTS1 << 3 + RTS2) << 31,
150 // since minimal Radix Tree size is 4G.
152 // RPDB = Root Page Directory Base.
153 // RPDS = Root Page Directory Size right shifted by 3.
154 // Thus, Root page directory size = RPDS << 3.
158 // -----------------------------------------------
160 // -----------------------------------------------
162 // All bits are reserved.
167 # see qemu/target/ppc/mmu-radix64.c for reference
169 def __init__(self
, mem
, caller
):
172 self
.dsisr
= self
.caller
.spr
["DSISR"]
173 self
.dar
= self
.caller
.spr
["DAR"]
174 self
.pidr
= self
.caller
.spr
["PIDR"]
175 self
.prtbl
= self
.caller
.spr
["PRTBL"]
177 # cached page table stuff
179 self
.pt0_valid
= False
181 self
.pt3_valid
= False
183 def __call__(self
,*args
, **kwargs
):
184 print("TODO: implement RADIX.__call__()")
189 def ld(self
, address
, width
=8, swap
=True, check_in_mem
=False):
190 print("RADIX: ld from addr 0x%x width %d" % (address
, width
))
192 pte
= self
._walk
_tree
()
193 # use pte to caclculate phys address
194 return self
.mem
.ld(address
, width
, swap
, check_in_mem
)
196 # XXX set SPRs on error
199 def st(self
, addr
, v
, width
=8, swap
=True):
200 print("RADIX: st to addr 0x%x width %d data %x" % (addr
, width
, v
))
202 # use pte to caclculate phys address (addr)
203 return self
.mem
.st(addr
, v
, width
, swap
)
205 # XXX set SPRs on error
207 def memassign(self
, addr
, sz
, val
):
208 print("memassign", addr
, sz
, val
)
209 self
.st(addr
.value
, val
.value
, sz
, swap
=False)
211 def _next_level(self
):
216 ## Prepare for next iteration
218 def _walk_tree(self
):
222 // vaddr |-----------------------------------------------------|
224 // |-----------|-----------------------------------------|
225 // | 0000000 | usefulBits = X bits (typically 52) |
226 // |-----------|-----------------------------------------|
227 // | |<--Cursize---->| |
231 // |-----------------------------------------------------|
234 // PDE |---------------------------| |
235 // |V|L|//| NLB |///|NLS| |
236 // |---------------------------| |
237 // PDE = Page Directory Entry |
238 // [0] = V = Valid Bit |
239 // [1] = L = Leaf bit. If 0, then |
240 // [4:55] = NLB = Next Level Base |
241 // right shifted by 8 |
242 // [59:63] = NLS = Next Level Size |
245 // | |--------------------------|
246 // | | usfulBits = X-Cursize |
247 // | |--------------------------|
248 // |---------------------><--NLS-->| |
252 // |--------------------------|
254 // If the next PDE obtained by |
255 // (NLB << 8 + 8 * index) is a |
256 // nonleaf, then repeat the above. |
258 // If the next PDE is a leaf, |
259 // then Leaf PDE structure is as |
264 // |------------------------------| |----------------|
265 // |V|L|sw|//|RPN|sw|R|C|/|ATT|EAA| | usefulBits |
266 // |------------------------------| |----------------|
267 // [0] = V = Valid Bit |
268 // [1] = L = Leaf Bit = 1 if leaf |
270 // [2] = Sw = Sw bit 0. |
271 // [7:51] = RPN = Real Page Number, V
272 // real_page = RPN << 12 -------------> Logical OR
273 // [52:54] = Sw Bits 1:3 |
274 // [55] = R = Reference |
275 // [56] = C = Change V
276 // [58:59] = Att = Physical Address
277 // 0b00 = Normal Memory
279 // 0b10 = Non Idenmpotent
280 // 0b11 = Tolerant I/O
281 // [60:63] = Encoded Access
285 # walk tree starts on prtbl
287 ret
= self
._next
_level
()
290 def _decode_prte(self
, data
):
292 -----------------------------------------------
293 |/|RTS1|/| RPDB | RTS2 | RPDS |
294 -----------------------------------------------
295 0 1 2 3 4 55 56 58 59 63
297 # note that SelectableInt does big-endian! so the indices
298 # below *directly* match the spec, unlike microwatt which
299 # has to turn them around (to LE)
300 zero
= SelectableInt(0, 1)
301 rts
= selectconcat(zero
,
305 masksize
= data
[59:64] # RPDS
306 mbits
= selectconcat(zero
, masksize
)
307 pgbase
= selectconcat(data
[8:56], # part of RPDB
308 SelectableInt(0, 16),)
310 return (rts
, mbits
, pgbase
)
312 def _segment_check(self
, addr
, mbits
, shift
):
313 """checks segment valid
314 mbits := '0' & r.mask_size;
315 v.shift := r.shift + (31 - 12) - mbits;
316 nonzero := or(r.addr(61 downto 31) and not finalmask(30 downto 0));
317 if r.addr(63) /= r.addr(62) or nonzero = '1' then
318 v.state := RADIX_FINISH;
320 elsif mbits < 5 or mbits > 16 or mbits > (r.shift + (31 - 12)) then
321 v.state := RADIX_FINISH;
324 v.state := RADIX_LOOKUP;
326 # note that SelectableInt does big-endian! so the indices
327 # below *directly* match the spec, unlike microwatt which
328 # has to turn them around (to LE)
329 mask
= genmask(shift
, 44)
330 nonzero
= addr
[1:32] & mask
[13:44] # mask 31 LSBs (BE numbered 13:44)
331 print ("RADIX _segment_check nonzero", bin(nonzero
.value
))
332 print ("RADIX _segment_check addr[0-1]", addr
[0].value
, addr
[1].value
)
333 if addr
[0] != addr
[1] or nonzero
== 1:
335 limit
= shift
+ (31 - 12)
336 if mbits
< 5 or mbits
> 16 or mbits
> limit
:
338 new_shift
= shift
+ (31 - 12) - mbits
341 def _check_perms(self
):
342 """check page permissions
344 if data(62) = '1' then
345 -- check permissions and RC bits
347 if r.priv = '1' or data(3) = '0' then
348 if r.iside = '0' then
349 perm_ok := data(1) or (data(2) and not r.store);
351 -- no IAMR, so no KUEP support for now
352 -- deny execute permission if cache inhibited
353 perm_ok := data(0) and not data(5);
356 rc_ok := data(8) and (data(7) or not r.store);
357 if perm_ok = '1' and rc_ok = '1' then
358 v.state := RADIX_LOAD_TLB;
360 v.state := RADIX_FINISH;
361 v.perm_err := not perm_ok;
362 -- permission error takes precedence over RC error
363 v.rc_error := perm_ok;
367 def _get_prtable_addr(self
, shift
, prtbl
, addr
, pid
):
369 if r.addr(63) = '1' then
370 effpid := x"00000000";
374 x"00" & r.prtbl(55 downto 36) &
375 ((r.prtbl(35 downto 12) and not finalmask(23 downto 0)) or
376 (effpid(31 downto 8) and finalmask(23 downto 0))) &
377 effpid(7 downto 0) & "0000";
379 finalmask
= genmask(shift
, 44)
380 finalmask24
= finalmask
[20:44]
381 if addr
[0].value
== 1:
382 effpid
= SelectableInt(0, 32)
384 effpid
= self
.pid
[32:64] # TODO, check on this
385 zero16
= SelectableInt(0, 16)
386 zero4
= SelectableInt(0, 4)
387 res
= selectconcat(zero16
,
389 (prtbl
[28:52] & ~finalmask24
) |
#
390 (effpid
[0:24] & finalmask24
), #
396 def _get_pgtable_addr(self
, mask_size
, pgbase
, addrsh
):
398 x"00" & r.pgbase(55 downto 19) &
399 ((r.pgbase(18 downto 3) and not mask) or (addrsh and mask)) &
402 mask16
= genmask(mask_size
+5, 16)
403 zero8
= SelectableInt(0, 8)
404 zero3
= SelectableInt(0, 3)
405 res
= selectconcat(zero8
,
407 (prtbl
[45:61] & ~mask16
) |
#
413 def _get_pte(self
, shift
, addr
, pde
):
416 ((r.pde(55 downto 12) and not finalmask) or
417 (r.addr(55 downto 12) and finalmask))
418 & r.pde(11 downto 0);
420 finalmask
= genmask(shift
, 44)
421 zero8
= SelectableInt(0, 8)
422 res
= selectconcat(zero8
,
423 (pde
[8:52] & ~finalmask
) |
#
424 (addr
[8:52] & finalmask
), #
430 # very quick test of maskgen function (TODO, move to util later)
431 if __name__
== '__main__':
432 # set up dummy minimal ISACaller
433 spr
= {'DSISR': SelectableInt(0, 64),
434 'DAR': SelectableInt(0, 64),
435 'PIDR': SelectableInt(0, 64),
436 'PRTBL': SelectableInt(0, 64)
438 class ISACaller
: pass
442 shift
= SelectableInt(5, 6)
443 mask
= genmask(shift
, 43)
444 print (" mask", bin(mask
.value
))
446 mem
= Mem(row_bytes
=8)
447 mem
= RADIX(mem
, caller
)
448 # -----------------------------------------------
449 # |/|RTS1|/| RPDB | RTS2 | RPDS |
450 # -----------------------------------------------
451 # |0|1 2|3|4 55|56 58|59 63|
452 data
= SelectableInt(0, 64)
455 data
[59:64] = 0b01101 # mask
457 (rts
, mbits
, pgbase
) = mem
._decode
_prte
(data
)
458 print (" rts", bin(rts
.value
), rts
.bits
)
459 print (" mbits", bin(mbits
.value
), mbits
.bits
)
460 print (" pgbase", hex(pgbase
.value
), pgbase
.bits
)
461 addr
= SelectableInt(0x1000, 64)
462 check
= mem
._segment
_check
(addr
, mbits
, shift
)
463 print (" segment check", check
)