3 * https://bugs.libre-soc.org/show_bug.cgi?id=325
4 * https://bugs.libre-soc.org/show_bug.cgi?id=344
5 * https://libre-soc.org/openpower/isa/fixedtrap/
8 from nmigen
import (Module
, Signal
, Cat
, Mux
, Const
, signed
)
9 from nmutil
.pipemodbase
import PipeModBase
10 from nmutil
.extend
import exts
11 from soc
.fu
.trap
.pipe_data
import TrapInputData
, TrapOutputData
12 from soc
.fu
.branch
.main_stage
import br_ext
13 from soc
.decoder
.power_enums
import InternalOp
15 from soc
.decoder
.power_fields
import DecodeFields
16 from soc
.decoder
.power_fieldsn
import SignalBitRange
19 # Listed in V3.0B Book III Chap 4.2.1
21 MSR_SF
= (63 - 0) # Sixty-Four bit mode
22 MSR_HV
= (63 - 3) # Hypervisor state
23 MSR_S
= (63 - 41) # Secure state
24 MSR_EE
= (63 - 48) # External interrupt Enable
25 MSR_PR
= (63 - 49) # PRoblem state
26 MSR_FP
= (63 - 50) # FP available
27 MSR_ME
= (63 - 51) # Machine Check int enable
28 MSR_IR
= (63 - 58) # Instruction Relocation
29 MSR_DR
= (63 - 59) # Data Relocation
30 MSR_PMM
= (63 - 60) # Performance Monitor Mark
31 MSR_RI
= (63 - 62) # Recoverable Interrupt
32 MSR_LE
= (63 - 63) # Little Endian
35 def msr_copy(msr_o
, msr_i
, zero_me
=True):
38 -- Defined MSR bits are classified as either full func-
39 -- tion or partial function. Full function MSR bits are
40 -- saved in SRR1 or HSRR1 when an interrupt other
41 -- than a System Call Vectored interrupt occurs and
42 -- restored by rfscv, rfid, or hrfid, while partial func-
43 -- tion MSR bits are not saved or restored.
44 -- Full function MSR bits lie in the range 0:32, 37:41, and
45 -- 48:63, and partial function MSR bits lie in the range
46 -- 33:36 and 42:47. (Note this is IBM bit numbering).
47 msr_out := (others => '0');
48 msr_out(63 downto 31) := msr(63 downto 31);
49 msr_out(26 downto 22) := msr(26 downto 22);
50 msr_out(15 downto 0) := msr(15 downto 0);
55 for stt
, end
in [(0,16), (22, 27), (31, 64)]:
56 l
.append(msr_o
[stt
:end
].eq(msr_i
[stt
:end
]))
59 class TrapMainStage(PipeModBase
):
60 def __init__(self
, pspec
):
61 super().__init
__(pspec
, "main")
62 self
.fields
= DecodeFields(SignalBitRange
, [self
.i
.ctx
.op
.insn
])
63 self
.fields
.create_specs()
66 return TrapInputData(self
.pspec
)
69 return TrapOutputData(self
.pspec
)
71 def elaborate(self
, platform
):
76 # convenience variables
77 a_i
, b_i
, cia_i
, msr_i
= self
.i
.a
, self
.i
.b
, self
.i
.cia
, self
.i
.msr
78 o
, msr_o
, nia_o
= self
.o
.o
, self
.o
.msr
, self
.o
.nia
79 srr0_o
, srr1_o
= self
.o
.srr0
, self
.o
.srr1
81 # take copy of D-Form TO field
82 i_fields
= self
.fields
.FormD
83 to
= Signal(i_fields
.TO
[0:-1].shape())
84 comb
+= to
.eq(i_fields
.TO
[0:-1])
86 # signed/unsigned temporaries for RA and RB
87 a_s
= Signal(signed(64), reset_less
=True)
88 b_s
= Signal(signed(64), reset_less
=True)
90 a
= Signal(64, reset_less
=True)
91 b
= Signal(64, reset_less
=True)
93 # set up A and B comparison (truncate/sign-extend if 32 bit)
94 with m
.If(op
.is_32bit
):
95 comb
+= a_s
.eq(exts(a_i
, 32, 64))
96 comb
+= b_s
.eq(exts(b_i
, 32, 64))
97 comb
+= a
.eq(a_i
[0:32])
98 comb
+= b
.eq(b_i
[0:32])
105 # establish comparison bits
106 lt_s
= Signal(reset_less
=True)
107 gt_s
= Signal(reset_less
=True)
108 lt_u
= Signal(reset_less
=True)
109 gt_u
= Signal(reset_less
=True)
110 equal
= Signal(reset_less
=True)
112 comb
+= lt_s
.eq(a_s
< b_s
)
113 comb
+= gt_s
.eq(a_s
> b_s
)
114 comb
+= lt_u
.eq(a
< b
)
115 comb
+= gt_u
.eq(a
> b
)
116 comb
+= equal
.eq(a
== b
)
118 # They're in reverse bit order because POWER.
119 # Check V3.0B Book 1, Appendix C.6 for chart
120 trap_bits
= Signal(5)
121 comb
+= trap_bits
.eq(Cat(gt_u
, lt_u
, equal
, gt_s
, lt_s
))
123 # establish if the trap should go ahead (any tests requested in TO)
124 should_trap
= Signal()
125 comb
+= should_trap
.eq((trap_bits
& to
).any())
127 # TODO: some #defines for the bits n stuff.
130 with m
.Case(InternalOp
.OP_TRAP
):
131 # trap instructions (tw, twi, td, tdi)
132 with m
.If(should_trap
):
133 # generate trap-type program interrupt
135 # change the PC to trap address 0x700
136 comb
+= nia_o
.data
.eq(0x700) # trap address
137 comb
+= nia_o
.ok
.eq(1)
139 # take a copy of the current MSR in SRR1
140 comb
+= msr_copy(srr1_o
.data
, msr_i
) # old MSR
141 # set bit 46 to say trap occurred
142 comb
+= srr1_o
.data
[63-46].eq(1) # XXX which bit?
143 comb
+= srr1_o
.ok
.eq(1)
145 # take a copy of the current PC in SRR0
146 comb
+= srr0_o
.data
.eq(cia_i
) # old PC
147 comb
+= srr0_o
.ok
.eq(1)
150 with m
.Case(InternalOp
.OP_MTMSR
):
151 L
= self
.fields
.FormX
.L
[0:-1] # X-Form field L
153 # just update EE and RI
154 comb
+= msr_o
.data
[MSR_EE
].eq(a_i
[MSR_EE
])
155 comb
+= msr_o
.data
[MSR_RI
].eq(a_i
[MSR_RI
])
157 # Architecture says to leave out bits 3 (HV), 51 (ME)
158 # and 63 (LE) (IBM bit numbering)
159 for stt
, end
in [(1,12), (13, 60), (61, 64)]:
160 comb
+= msr_o
.data
[stt
:end
].eq(a_i
[stt
:end
])
161 with m
.If(a_i
[MSR_PR
]):
162 msr_o
.data
[MSR_EE
].eq(1)
163 msr_o
.data
[MSR_IR
].eq(1)
164 msr_o
.data
[MSR_DR
].eq(1)
165 comb
+= msr_o
.ok
.eq(1)
168 with m
.Case(InternalOp
.OP_MFMSR
):
169 # TODO: some of the bits need zeroing? apparently not
170 comb
+= o
.data
.eq(msr_i
)
173 with m
.Case(InternalOp
.OP_RFID
):
174 # XXX f_out.virt_mode <= b_in(MSR_IR) or b_in(MSR_PR);
175 # XXX f_out.priv_mode <= not b_in(MSR_PR);
177 # return addr was in srr0
178 comb
+= nia_o
.data
.eq(br_ext(srr0_i
[2:]))
179 comb
+= nia_o
.ok
.eq(1)
181 comb
+= msr_copy(msr_o
.data
, srr1_i
, zero_me
=False) # don't zero
182 with m
.If(srr1_i
[MSR_PR
]):
186 comb
+= msr_o
.ok
.eq(1)
188 with m
.Case(InternalOp
.OP_SC
):
189 # TODO: scv must generate illegal instruction. this is
190 # the decoder's job, not ours, here.
192 # jump to the trap address
193 comb
+= nia_o
.eq(0xC00) # trap address
194 comb
+= nia_o
.ok
.eq(1)
195 # keep a copy of the MSR in SRR1
196 comb
+= msr_copy(srr1_o
.data
, msr_i
)
197 comb
+= srr1_o
.ok
.eq(1)
198 # and store the (next-after-return) PC in SRR0
199 comb
+= srr0_o
.data
.eq(cia_i
+4) # addr to begin from on return
200 comb
+= srr0_o
.ok
.eq(1)
203 #with m.Case(InternalOp.OP_ADDPCIS):
206 comb
+= self
.o
.ctx
.eq(self
.i
.ctx
)