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
):
132 -- trap instructions (tw, twi, td, tdi)
133 if or (trapval and insn_to(e_in.insn)) = '1' then
134 -- generate trap-type program interrupt
136 ctrl_tmp.irq_nia <= std_logic_vector(to_unsigned(16#700#, 64));
137 ctrl_tmp.srr1 <= msr_copy(ctrl.msr);
138 -- set bit 46 to say trap occurred
139 ctrl_tmp.srr1(63 - 46) <= '1';
141 with m
.If(should_trap
):
142 comb
+= nia_o
.data
.eq(0x700) # trap address
143 comb
+= nia_o
.ok
.eq(1)
144 comb
+= msr_copy(srr1_o
.data
, msr_i
) # old MSR
145 comb
+= srr1_o
.data
[63-46].eq(1) # XXX which bit?
146 comb
+= srr1_o
.ok
.eq(1)
147 comb
+= srr0_o
.data
.eq(cia_i
) # old PC
148 comb
+= srr0_o
.ok
.eq(1)
151 with m
.Case(InternalOp
.OP_MTMSR
):
152 # TODO: some of the bits need zeroing?
154 if e_in.insn(16) = '1' then <-- this is X-form field "L".
155 -- just update EE and RI
156 ctrl_tmp.msr(MSR_EE) <= c_in(MSR_EE);
157 ctrl_tmp.msr(MSR_RI) <= c_in(MSR_RI);
159 -- Architecture says to leave out bits 3 (HV), 51 (ME)
160 -- and 63 (LE) (IBM bit numbering)
161 ctrl_tmp.msr(63 downto 61) <= c_in(63 downto 61);
162 ctrl_tmp.msr(59 downto 13) <= c_in(59 downto 13);
163 ctrl_tmp.msr(11 downto 1) <= c_in(11 downto 1);
164 if c_in(MSR_PR) = '1' then
165 ctrl_tmp.msr(MSR_EE) <= '1';
166 ctrl_tmp.msr(MSR_IR) <= '1';
167 ctrl_tmp.msr(MSR_DR) <= '1';
170 L = self.fields.FormXL.L[0:-1]
171 if e_in.insn(16) = '1' then <-- this is X-form field "L".
172 -- just update EE and RI
173 ctrl_tmp.msr(MSR_EE) <= c_in(MSR_EE);
174 ctrl_tmp.msr(MSR_RI) <= c_in(MSR_RI);
176 L
= self
.fields
.FormX
.L
[0:-1]
178 comb
+= msr_o
[MSR_EE
].eq(msr_i
[MSR_EE
])
179 comb
+= msr_o
[MSR_RI
].eq(msr_i
[MSR_RI
])
182 for stt
, end
in [(1,12), (13, 60), (61, 64)]:
183 comb
+= msr_o
.data
[stt
:end
].eq(a_i
[stt
:end
])
184 with m
.If(a
[MSR_PR
]):
188 comb
+= msr_o
.ok
.eq(1)
191 with m
.Case(InternalOp
.OP_MFMSR
):
192 # TODO: some of the bits need zeroing? apparently not
198 comb
+= o
.data
.eq(msr_i
)
201 with m
.Case(InternalOp
.OP_RFID
):
203 # XXX f_out.virt_mode <= b_in(MSR_IR) or b_in(MSR_PR);
204 # XXX f_out.priv_mode <= not b_in(MSR_PR);
205 f_out.redirect_nia <= a_in(63 downto 2) & "00"; -- srr0
206 -- Can't use msr_copy here because the partial function MSR
207 -- bits should be left unchanged, not zeroed.
208 ctrl_tmp.msr(63 downto 31) <= b_in(63 downto 31);
209 ctrl_tmp.msr(26 downto 22) <= b_in(26 downto 22);
210 ctrl_tmp.msr(15 downto 0) <= b_in(15 downto 0);
211 if b_in(MSR_PR) = '1' then
212 ctrl_tmp.msr(MSR_EE) <= '1';
213 ctrl_tmp.msr(MSR_IR) <= '1';
214 ctrl_tmp.msr(MSR_DR) <= '1';
217 comb
+= nia_o
.data
.eq(br_ext(a_i
[2:]))
218 comb
+= nia_o
.ok
.eq(1)
219 comb
+= msr_copy(msr_o
.data
, srr1_i
, zero_me
=False) # don't zero
220 with m
.If(a
[MSR_PR
]):
224 comb
+= msr_o
.ok
.eq(1)
226 with m
.Case(InternalOp
.OP_SC
):
228 # TODO: scv must generate illegal instruction. this is
229 # the decoder's job, not ours, here.
230 ctrl_tmp.irq_nia <= std_logic_vector(to_unsigned(16#C00#, 64));
231 ctrl_tmp.srr1 <= msr_copy(ctrl.msr);
233 comb
+= nia_o
.eq(0xC00) # trap address
234 comb
+= nia_o
.ok
.eq(1)
235 comb
+= msr_copy(srr1_o
.data
, msr_i
) # old msr
236 comb
+= srr1_o
.ok
.eq(1)
237 comb
+= srr0_o
.data
.eq(cia_i
+4) # addr to begin from on return
238 comb
+= srr0_o
.ok
.eq(1)
241 #with m.Case(InternalOp.OP_ADDPCIS):
244 comb
+= self
.o
.ctx
.eq(self
.i
.ctx
)