Skip to content

Commit 94acfbf

Browse files
Merge pull request #915 from openhwgroup/dev
Automatic PR dev->master
2 parents 16a29e0 + 6b34db2 commit 94acfbf

File tree

5 files changed

+64
-7
lines changed

5 files changed

+64
-7
lines changed

bhv/cv32e40p_rvfi.sv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1430,7 +1430,7 @@ insn_trace_t trace_if, trace_id, trace_ex, trace_ex_next, trace_wb;
14301430

14311431
s_new_valid_insn = r_pipe_freeze_trace.id_valid && r_pipe_freeze_trace.is_decoding;// && !r_pipe_freeze_trace.apu_rvalid;
14321432

1433-
s_wb_valid_adjusted = r_pipe_freeze_trace.wb_valid && (s_core_is_decoding || (r_pipe_freeze_trace.ctrl_fsm_cs == FLUSH_EX) || (r_pipe_freeze_trace.ctrl_fsm_cs == DBG_FLUSH) || (r_pipe_freeze_trace.ctrl_fsm_cs == DBG_TAKEN_ID) || (r_pipe_freeze_trace.ctrl_fsm_cs == DBG_TAKEN_IF));// && !r_pipe_freeze_trace.apu_rvalid;;
1433+
s_wb_valid_adjusted = r_pipe_freeze_trace.wb_valid && (s_core_is_decoding || (r_pipe_freeze_trace.ctrl_fsm_cs == FLUSH_EX) || (r_pipe_freeze_trace.ctrl_fsm_cs == FLUSH_WB) || (r_pipe_freeze_trace.ctrl_fsm_cs == DBG_FLUSH) || (r_pipe_freeze_trace.ctrl_fsm_cs == DBG_TAKEN_ID) || (r_pipe_freeze_trace.ctrl_fsm_cs == DBG_TAKEN_IF));// && !r_pipe_freeze_trace.apu_rvalid;;
14341434
s_ex_reg_we_adjusted = r_pipe_freeze_trace.ex_reg_we && r_pipe_freeze_trace.mult_ready && r_pipe_freeze_trace.alu_ready && r_pipe_freeze_trace.lsu_ready_ex && !s_apu_to_alu_port;
14351435
s_rf_we_wb_adjusted = r_pipe_freeze_trace.rf_we_wb && (~r_pipe_freeze_trace.data_misaligned_ex && r_pipe_freeze_trace.wb_ready) && (!s_apu_to_lsu_port || r_pipe_freeze_trace.wb_contention_lsu);
14361436

bhv/pipe_freeze_trace.sv

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,10 @@ function compute_csr_we();
367367
r_pipe_freeze_trace.csr.mstatus_fs_we = 1'b1;
368368
end
369369
CSR_FRM: r_pipe_freeze_trace.csr.frm_we = 1'b1;
370-
CSR_FCSR: r_pipe_freeze_trace.csr.fcsr_we = 1'b1;
370+
CSR_FCSR: begin
371+
r_pipe_freeze_trace.csr.fcsr_we = 1'b1;
372+
r_pipe_freeze_trace.csr.mstatus_fs_we = 1'b1;
373+
end
371374
CSR_DPC: r_pipe_freeze_trace.csr.dpc_we = 1'b1;
372375
CSR_DSCRATCH0: r_pipe_freeze_trace.csr.dscratch0_we = 1'b1;
373376
CSR_DSCRATCH1: r_pipe_freeze_trace.csr.dscratch1_we = 1'b1;

docs/source/corev_hw_loop.rst

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,9 @@ is that it greatly simplifies compiler optimization (relative to basic blocks ma
9090
In order to use hardware loops, the compiler needs to setup the loops beforehand with cv.start/i, cv.end/i, cv.count/i or cv.setup/i instructions.
9191
The compiler will use HWLoop automatically whenever possible without the need of assembly.
9292

93-
For debugging and context switches, the hardware loop registers are mapped into the CSR custom read-only address space.
93+
For debugging, interrupts and context switches, the hardware loop registers are mapped into the CSR custom read-only address space.
9494
To read them csrr instructions should be used and to write them register flavour of hardware loop instructions should be used.
9595
Using csrw instructions to write hardware loop registers will generate an illegal instruction exception.
96-
97-
Since hardware loop feature could be used in interrupt routine/handler, the registers have
98-
to be saved (resp. restored) at the beginning (resp. end) of the interrupt routine together with the general purpose registers.
9996
The CSR HWLoop registers are described in the :ref:`cs-registers` section.
10097

10198
Below an assembly code example of a nested HWLoop that computes a matrix addition.
@@ -138,3 +135,50 @@ it is executed 10x10 times. Whereas the outermost loop, from startO to (endO - 4
138135
executes 10 times the innermost loop and adds 2 to the register %[j].
139136
At the end of the loop, the register %[i] contains 300 and the register %[j] contains 20.
140137

138+
.. _hwloop-exceptions_handlers:
139+
140+
Hardware loops impact on application, exceptions handlers and debugger
141+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
142+
143+
Application and ebreak/ecall exception handlers
144+
-----------------------------------------------
145+
146+
When an ebreak or an ecall instruction is used in an application, special care should be given for those instruction handlers in case they are placed as the last instruction of an HWLoop.
147+
Those handlers should manage MEPC and lpcountX CSRs updates because an hw loop early-exit could happen if not done.
148+
149+
At the end of the handlers after restoring the context/CSRs, a piece of smart code should be added with following highest to lowest order of priority:
150+
151+
1. if MEPC = lpend0 - 4 and lpcount0 > 1 then MPEC should be set to lpstart0 and lpcount0 should be decremented by 1,
152+
2. else if MEPC = lpend0 - 4 and lpcount0 = 1 then MPEC should be incremented by 4 and lpcount0 should be decremented by 1,
153+
3. else if MEPC = lpend1 - 4 and lpcount1 > 1 then MPEC should be set to lpstart1 and lpcount1 should be decremented by 1,
154+
4. else if MEPC = lpend1 - 4 and lpcount1 = 1 then MPEC should be incremented by 4 and lpcount1 should be decremented by 1,
155+
5. else if (lpstart0 <= MEPC < lpend0 - 4) or (lpstart1 <= MEPC < lpend1 - 4) then MPEC should be incremented by 4,
156+
6. else if instruction at MEPC location is either ecall or ebreak then MPEC should be incremented by 4,
157+
7. else if instruction at MEPC location location is c.ebreak then MPEC should be incremented by 2.
158+
159+
The 2 last cases are the standard ones when ebreak/ecall are not inside an HWLopp.
160+
161+
Interrupt handlers
162+
------------------
163+
164+
When an interrupt is happening on the last HWLoop instruction, its execution is cancelled, its address is saved in MEPC and its execution will be resumed when returning from interrupt handler.
165+
There is nothing special to be done in those interrupt handlers with respect to MEPC and lpcountX updates, they will be correctly managed by design when executing this last HWLoop instruction after interrupt handler execution.
166+
167+
Moreover since hardware loop could be used in interrupt routine, the registers have to be saved (resp. restored) at the beginning (resp. end) of the interrupt routine together with the general purpose registers.
168+
169+
Illegal instruction exception handler
170+
-------------------------------------
171+
172+
Depending if an application is going to resume or not after Illegal instruction exception handler, same MEPC/HWLoops CSRs management than ebreak/ecall could be necessary.
173+
174+
Debugger
175+
--------
176+
177+
If ebreak is used to enter in Debug Mode (:ref:`ebreak_scenario_2`) and put at the last instruction location of an HWLoop (not very likely to happen), same management than above should be done but on DPC rather than on MEPC.
178+
179+
When ebreak instruction is used as Software Breakpoint by a debugger when in debug mode and is placed at the last instruction location of an HWLoop in instruction memory, no special management is foreseen.
180+
When executing the Software Breakpoint/ebreak instruction, control is given back to the debugger which will manage the different cases.
181+
For instance in Single-Step case, original instruction is put back in instruction memory, a Single-Step command is executed on this last instruction (with desgin updating PC and lpcountX to correct values) and Software Breakpoint/ebreak is put back by the debugger in memory.
182+
183+
When ecall instruction is used by a debugger to execute System Calls and is placed at the last instruction location of an HWLoop in instruction memory, debugger ecall handler in debug rom should do the same than described above for application case.
184+

docs/source/debug.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ The EBREAK instruction description is distributed across several RISC-V specific
162162
`RISC-V Priveleged Specification <https://github.com/riscv/riscv-isa-manual/releases/tag/Ratified-IMFDQC-and-Priv-v1.11>`_,
163163
`RISC-V ISA <https://github.com/riscv/riscv-isa-manual/releases/tag/Ratified-IMAFDQC>`_. The following is a summary of the behavior for three common scenarios.
164164

165+
.. _ebreak_scenario_1:
166+
165167
Scenario 1 : Enter Exception
166168
""""""""""""""""""""""""""""
167169

@@ -173,10 +175,14 @@ Executing the EBREAK instruction when the core is **not** in Debug Mode and the
173175
To properly return from the exception, the ebreak handler will need to increment the MEPC to the next instruction.
174176
This requires querying the size of the ebreak instruction that was used to enter the exception (16 bit c.ebreak or 32 bit ebreak).
175177

178+
As mentioned in :ref:`hwloop-exceptions_handlers`, some additional cases exist for MEPC update when ebreak is the last instruction of an Hardware Loop.
179+
176180
.. note::
177181

178182
The CV32E40P does not support MTVAL CSR register which would have saved the value of the instruction for exceptions. This may be supported on a future core.
179183

184+
.. _ebreak_scenario_2:
185+
180186
Scenario 2 : Enter Debug Mode
181187
"""""""""""""""""""""""""""""
182188

@@ -187,11 +193,15 @@ Executing the EBREAK instruction when the core is **not** in Debug Mode and the
187193

188194
Similar to the exception scenario above, the debugger will need to increment the DPC to the next instruction before returning from Debug Mode.
189195

196+
There is no forseseen situtation where it would be needed to enter in Debug Mode only on the last instruction of an Hardware Loop but just in case this is mentioned in :ref:`hwloop-exceptions_handlers` as well.
197+
190198
.. note::
191199

192200
The default value of DCSR.EBREAKM is 0 and the DCSR is only accessible in Debug Mode. To enter Debug Mode from EBREAK,
193201
the user will first need to enter Debug Mode through some other means, such as from the external ``debug_req_i``, and set DCSR.EBREAKM.
194202

203+
.. _ebreak_scenario_3:
204+
195205
Scenario 3 : Exit Program Buffer & Restart Debug Code
196206
"""""""""""""""""""""""""""""""""""""""""""""""""""""
197207

docs/source/pipeline.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ The cycle counts assume zero stall on the instruction-side interface and zero st
150150
| Comparison, Conversion | | If there are enough instructions between FPU one and |
151151
| or Classify | | the instruction using the result then cycle number is 1. |
152152
+------------------------+--------------------------------------+ "Enough instruction" number is either FPU_ADDMUL_LAT, |
153-
| Single Precision | 1..12 | FPU_OTHERS_LAT or 11. |
153+
| Single Precision | 1..19 | FPU_OTHERS_LAT or 11. |
154154
| Floating-Point | | If there are no instruction in between then cycle number is |
155155
| Division and | | the maximum value for each category. |
156156
| Square-Root | | |

0 commit comments

Comments
 (0)