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