add an if for OP_MTMSR and some comments
[soc.git] / src / soc / fu / trap / main_stage.py
1 """Trap Pipeline
2
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/
6 """
7
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.decoder.power_enums import InternalOp
13
14 from soc.decoder.power_fields import DecodeFields
15 from soc.decoder.power_fieldsn import SignalBitRange
16
17 # TODO at some point move these to their own module (for use elsewhere)
18 """
19 -- MSR bit numbers
20 constant MSR_SF : integer := (63 - 0); -- Sixty-Four bit mode
21 constant MSR_EE : integer := (63 - 48); -- External interrupt Enable
22 constant MSR_PR : integer := (63 - 49); -- PRoblem state
23 constant MSR_IR : integer := (63 - 58); -- Instruction Relocation
24 constant MSR_DR : integer := (63 - 59); -- Data Relocation
25 constant MSR_RI : integer := (63 - 62); -- Recoverable Interrupt
26 constant MSR_LE : integer := (63 - 63); -- Little Endian
27 """
28
29 class TrapMainStage(PipeModBase):
30 def __init__(self, pspec):
31 super().__init__(pspec, "main")
32 self.fields = DecodeFields(SignalBitRange, [self.i.ctx.op.insn])
33 self.fields.create_specs()
34
35 def ispec(self):
36 return TrapInputData(self.pspec)
37
38 def ospec(self):
39 return TrapOutputData(self.pspec)
40
41 def elaborate(self, platform):
42 m = Module()
43 comb = m.d.comb
44 op = self.i.ctx.op
45 a_i, b_i = self.i.a, self.i.b
46
47 # take copy of D-Form TO field
48 i_fields = self.fields.FormD
49 to = Signal(i_fields.TO[0:-1].shape())
50 comb += to.eq(i_fields.TO[0:-1])
51
52 # signed/unsigned temporaries for RA and RB
53 a_s = Signal(signed(64), reset_less=True)
54 b_s = Signal(signed(64), reset_less=True)
55
56 a = Signal(64, reset_less=True)
57 b = Signal(64, reset_less=True)
58
59 # set up A and B comparison (truncate/sign-extend if 32 bit)
60 with m.If(op.is_32bit):
61 comb += a_s.eq(exts(a_i, 32, 64))
62 comb += b_s.eq(exts(b_i, 32, 64))
63 comb += a.eq(a_i[0:32])
64 comb += b.eq(b_i[0:32])
65 with m.Else():
66 comb += a_s.eq(a_i)
67 comb += b_s.eq(b_i)
68 comb += a.eq(a_i)
69 comb += b.eq(b_i)
70
71 # establish comparison bits
72 lt_s = Signal(reset_less=True)
73 gt_s = Signal(reset_less=True)
74 lt_u = Signal(reset_less=True)
75 gt_u = Signal(reset_less=True)
76 equal = Signal(reset_less=True)
77
78 comb += lt_s.eq(a_s < b_s)
79 comb += gt_s.eq(a_s > b_s)
80 comb += lt_u.eq(a < b)
81 comb += gt_u.eq(a > b)
82 comb += equal.eq(a == b)
83
84 # They're in reverse bit order because POWER.
85 # Check V3.0B Book 1, Appendix C.6 for chart
86 trap_bits = Signal(5)
87 comb += trap_bits.eq(Cat(gt_u, lt_u, equal, gt_s, lt_s))
88
89 # establish if the trap should go ahead (any tests requested in TO)
90 should_trap = Signal()
91 comb += should_trap.eq((trap_bits & to).any())
92
93 # TODO: some #defines for the bits n stuff.
94 with m.Switch(op):
95 #### trap ####
96 with m.Case(InternalOp.OP_TRAP):
97 """
98 -- trap instructions (tw, twi, td, tdi)
99 if or (trapval and insn_to(e_in.insn)) = '1' then
100 -- generate trap-type program interrupt
101 exception := '1';
102 ctrl_tmp.irq_nia <= std_logic_vector(to_unsigned(16#700#, 64));
103 ctrl_tmp.srr1 <= msr_copy(ctrl.msr);
104 -- set bit 46 to say trap occurred
105 ctrl_tmp.srr1(63 - 46) <= '1';
106 """
107 with m.If(should_trap):
108 comb += self.o.nia.data.eq(0x700) # trap address
109 comb += self.o.nia.ok.eq(1)
110 comb += self.o.srr1.data.eq(self.i.msr) # old MSR
111 comb += self.o.srr1.data[63-46].eq(1) # XXX which bit?
112 comb += self.o.srr1.ok.eq(1)
113 comb += self.o.srr0.data.eq(self.i.cia) # old PC
114 comb += self.o.srr0.ok.eq(1)
115
116 # move to SPR
117 with m.Case(InternalOp.OP_MTMSR):
118 # TODO: some of the bits need zeroing?
119 """
120 if e_in.insn(16) = '1' then
121 -- just update EE and RI
122 ctrl_tmp.msr(MSR_EE) <= c_in(MSR_EE);
123 ctrl_tmp.msr(MSR_RI) <= c_in(MSR_RI);
124 else
125 -- Architecture says to leave out bits 3 (HV), 51 (ME)
126 -- and 63 (LE) (IBM bit numbering)
127 ctrl_tmp.msr(63 downto 61) <= c_in(63 downto 61);
128 ctrl_tmp.msr(59 downto 13) <= c_in(59 downto 13);
129 ctrl_tmp.msr(11 downto 1) <= c_in(11 downto 1);
130 if c_in(MSR_PR) = '1' then
131 ctrl_tmp.msr(MSR_EE) <= '1';
132 ctrl_tmp.msr(MSR_IR) <= '1';
133 ctrl_tmp.msr(MSR_DR) <= '1';
134 """
135 for stt, end in [(1,12), (13, 60), (61, 64)]:
136 comb += self.o.msr.data[stt:end].eq(a[stt:end])
137 with m.If(a[MSR_PR]):
138 self.o.msr[MSR_EE].eq(1)
139 self.o.msr[MSR_IR].eq(1)
140 self.o.msr[MSR_DR].eq(1)
141 comb += self.o.msr.ok.eq(1)
142
143 # move from SPR
144 with m.Case(InternalOp.OP_MFMSR):
145 # TODO: some of the bits need zeroing? apparently not
146 """
147 when OP_MFMSR =>
148 result := ctrl.msr;
149 result_en := '1';
150 """
151 comb += self.o.o.data.eq(self.i.msr)
152 comb += self.o.o.ok.eq(1)
153
154 # TODO
155 with m.Case(InternalOp.OP_RFID):
156 """
157 # XXX f_out.virt_mode <= b_in(MSR_IR) or b_in(MSR_PR);
158 # XXX f_out.priv_mode <= not b_in(MSR_PR);
159 f_out.redirect_nia <= a_in(63 downto 2) & "00"; -- srr0
160 -- Can't use msr_copy here because the partial function MSR
161 -- bits should be left unchanged, not zeroed.
162 ctrl_tmp.msr(63 downto 31) <= b_in(63 downto 31);
163 ctrl_tmp.msr(26 downto 22) <= b_in(26 downto 22);
164 ctrl_tmp.msr(15 downto 0) <= b_in(15 downto 0);
165 if b_in(MSR_PR) = '1' then
166 ctrl_tmp.msr(MSR_EE) <= '1';
167 ctrl_tmp.msr(MSR_IR) <= '1';
168 ctrl_tmp.msr(MSR_DR) <= '1';
169 end if;
170 """
171 comb += self.o.msr.data[:16].eq(b[:16])
172 comb += self.o.msr.data[22:27].eq(b[22:27])
173 comb += self.o.msr.data[31:] # <- oops missed the eq here
174 comb += self.o.msr.ok.eq(1)
175 comb += self.o.msr.data.eq(b)
176 comb += self.o.msr.ok.eq(1)
177
178 # TODO
179 with m.Case(InternalOp.OP_SC):
180 """
181 # TODO: scv must generate illegal instruction. this is
182 # the decoder's job, not ours, here.
183 ctrl_tmp.irq_nia <= std_logic_vector(to_unsigned(16#C00#, 64));
184 ctrl_tmp.srr1 <= msr_copy(ctrl.msr);
185 """
186 comb += self.o.nia.eq(0xC00) # trap address
187 comb += self.o.nia.ok.eq(1)
188 # TODO: srr1 (see 2nd line of vhdl above. remember set ok=1 too)
189
190 #with m.Case(InternalOp.OP_ADDPCIS):
191 # pass
192
193 comb += self.o.ctx.eq(self.i.ctx)
194
195 return m