move MSR_PR checking to separate functiong
[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.fu.branch.main_stage import br_ext
13 from soc.decoder.power_enums import InternalOp
14
15 from soc.decoder.power_fields import DecodeFields
16 from soc.decoder.power_fieldsn import SignalBitRange
17
18
19 # Listed in V3.0B Book III Chap 4.2.1
20 # MSR bit numbers
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
33
34
35 def msr_copy(msr_o, msr_i, zero_me=True):
36 """
37 -- ISA says this:
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);
51 """
52 l = []
53 if zero_me:
54 l.append(msr_o.eq(0))
55 for stt, end in [(0,16), (22, 27), (31, 64)]:
56 l.append(msr_o[stt:end].eq(msr_i[stt:end]))
57 return l
58
59
60 def msr_check_pr(m, msr_o):
61 """msr_check_pr: checks "problem state"
62 """
63 comb = m.d.comb
64 with m.If(msrdata[MSR_PR]):
65 comb += msr[MSR_EE].eq(1) # set external interrupt bit
66 comb += msr[MSR_IR].eq(1) # set instruction relocation bit
67 comb += msr[MSR_DR].eq(1) # set data relocation bit
68
69
70 class TrapMainStage(PipeModBase):
71 def __init__(self, pspec):
72 super().__init__(pspec, "main")
73 self.fields = DecodeFields(SignalBitRange, [self.i.ctx.op.insn])
74 self.fields.create_specs()
75
76 def ispec(self):
77 return TrapInputData(self.pspec)
78
79 def ospec(self):
80 return TrapOutputData(self.pspec)
81
82 def elaborate(self, platform):
83 m = Module()
84 comb = m.d.comb
85 op = self.i.ctx.op
86
87 # convenience variables
88 a_i, b_i, cia_i, msr_i = self.i.a, self.i.b, self.i.cia, self.i.msr
89 o, msr_o, nia_o = self.o.o, self.o.msr, self.o.nia
90 srr0_o, srr1_o = self.o.srr0, self.o.srr1
91
92 # take copy of D-Form TO field
93 i_fields = self.fields.FormD
94 to = Signal(i_fields.TO[0:-1].shape())
95 comb += to.eq(i_fields.TO[0:-1])
96
97 # signed/unsigned temporaries for RA and RB
98 a_s = Signal(signed(64), reset_less=True)
99 b_s = Signal(signed(64), reset_less=True)
100
101 a = Signal(64, reset_less=True)
102 b = Signal(64, reset_less=True)
103
104 # set up A and B comparison (truncate/sign-extend if 32 bit)
105 with m.If(op.is_32bit):
106 comb += a_s.eq(exts(a_i, 32, 64))
107 comb += b_s.eq(exts(b_i, 32, 64))
108 comb += a.eq(a_i[0:32])
109 comb += b.eq(b_i[0:32])
110 with m.Else():
111 comb += a_s.eq(a_i)
112 comb += b_s.eq(b_i)
113 comb += a.eq(a_i)
114 comb += b.eq(b_i)
115
116 # establish comparison bits
117 lt_s = Signal(reset_less=True)
118 gt_s = Signal(reset_less=True)
119 lt_u = Signal(reset_less=True)
120 gt_u = Signal(reset_less=True)
121 equal = Signal(reset_less=True)
122
123 comb += lt_s.eq(a_s < b_s)
124 comb += gt_s.eq(a_s > b_s)
125 comb += lt_u.eq(a < b)
126 comb += gt_u.eq(a > b)
127 comb += equal.eq(a == b)
128
129 # They're in reverse bit order because POWER.
130 # Check V3.0B Book 1, Appendix C.6 for chart
131 trap_bits = Signal(5)
132 comb += trap_bits.eq(Cat(gt_u, lt_u, equal, gt_s, lt_s))
133
134 # establish if the trap should go ahead (any tests requested in TO)
135 should_trap = Signal()
136 comb += should_trap.eq((trap_bits & to).any())
137
138 # TODO: some #defines for the bits n stuff.
139 with m.Switch(op):
140 #### trap ####
141 with m.Case(InternalOp.OP_TRAP):
142 # trap instructions (tw, twi, td, tdi)
143 with m.If(should_trap):
144 # generate trap-type program interrupt
145
146 # change the PC to trap address 0x700
147 comb += nia_o.data.eq(0x700) # trap address
148 comb += nia_o.ok.eq(1)
149
150 # take a copy of the current MSR in SRR1
151 comb += msr_copy(srr1_o.data, msr_i) # old MSR
152 # set bit 46 to say trap occurred
153 comb += srr1_o.data[63-46].eq(1) # XXX which bit?
154 comb += srr1_o.ok.eq(1)
155
156 # take a copy of the current PC in SRR0
157 comb += srr0_o.data.eq(cia_i) # old PC
158 comb += srr0_o.ok.eq(1)
159
160 # move to MSR
161 with m.Case(InternalOp.OP_MTMSR):
162 L = self.fields.FormX.L[0:-1] # X-Form field L
163 with m.If(L):
164 # just update EE and RI
165 comb += msr_o.data[MSR_EE].eq(a_i[MSR_EE])
166 comb += msr_o.data[MSR_RI].eq(a_i[MSR_RI])
167 with m.Else():
168 # Architecture says to leave out bits 3 (HV), 51 (ME)
169 # and 63 (LE) (IBM bit numbering)
170 for stt, end in [(1,12), (13, 60), (61, 64)]:
171 comb += msr_o.data[stt:end].eq(a_i[stt:end])
172 msr_check_pr(m, msr_o.data)
173 comb += msr_o.ok.eq(1)
174
175 # move from MSR
176 with m.Case(InternalOp.OP_MFMSR):
177 # TODO: some of the bits need zeroing? apparently not
178 comb += o.data.eq(msr_i)
179 comb += o.ok.eq(1)
180
181 with m.Case(InternalOp.OP_RFID):
182 # XXX f_out.virt_mode <= b_in(MSR_IR) or b_in(MSR_PR);
183 # XXX f_out.priv_mode <= not b_in(MSR_PR);
184
185 # return addr was in srr0
186 comb += nia_o.data.eq(br_ext(srr0_i[2:]))
187 comb += nia_o.ok.eq(1)
188 # MSR was in srr1
189 comb += msr_copy(msr_o.data, srr1_i, zero_me=False) # don't zero
190 msr_check_pr(m, msr_o.data)
191 comb += msr_o.ok.eq(1)
192
193 with m.Case(InternalOp.OP_SC):
194 # TODO: scv must generate illegal instruction. this is
195 # the decoder's job, not ours, here.
196
197 # jump to the trap address
198 comb += nia_o.eq(0xC00) # trap address
199 comb += nia_o.ok.eq(1)
200 # keep a copy of the MSR in SRR1
201 comb += msr_copy(srr1_o.data, msr_i)
202 comb += srr1_o.ok.eq(1)
203 # and store the (next-after-return) PC in SRR0
204 comb += srr0_o.data.eq(cia_i+4) # addr to begin from on return
205 comb += srr0_o.ok.eq(1)
206
207 # TODO (later)
208 #with m.Case(InternalOp.OP_ADDPCIS):
209 # pass
210
211 comb += self.o.ctx.eq(self.i.ctx)
212
213 return m