2022-06-07 02:46:23 -04:00
|
|
|
|
/*
|
2024-01-03 10:48:13 -05:00
|
|
|
|
* SPDX-FileCopyrightText: 2020-2024 Espressif Systems (Shanghai) CO LTD
|
2022-06-07 02:46:23 -04:00
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
|
#include "soc/soc_caps.h"
|
|
|
|
|
#include "soc/assist_debug_reg.h"
|
2022-08-08 09:18:46 -04:00
|
|
|
|
#include "soc/interrupt_reg.h"
|
2022-06-07 02:46:23 -04:00
|
|
|
|
#include "esp_attr.h"
|
|
|
|
|
#include "riscv/csr.h"
|
|
|
|
|
#include "riscv/interrupt.h"
|
2024-04-30 04:37:40 -04:00
|
|
|
|
#include "riscv/csr_pie.h"
|
2022-06-07 02:46:23 -04:00
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-03-14 10:21:29 -04:00
|
|
|
|
#if SOC_CPU_HAS_CSR_PC
|
2022-06-07 02:46:23 -04:00
|
|
|
|
/*performance counter*/
|
|
|
|
|
#define CSR_PCER_MACHINE 0x7e0
|
|
|
|
|
#define CSR_PCMR_MACHINE 0x7e1
|
|
|
|
|
#define CSR_PCCR_MACHINE 0x7e2
|
2024-03-14 10:21:29 -04:00
|
|
|
|
#endif /* SOC_CPU_HAS_CSR_PC */
|
2022-06-07 02:46:23 -04:00
|
|
|
|
|
2023-09-06 07:17:24 -04:00
|
|
|
|
#if SOC_CPU_HAS_FPU
|
|
|
|
|
|
|
|
|
|
/* FPU bits in mstatus start at bit 13 */
|
|
|
|
|
#define CSR_MSTATUS_FPU_SHIFT 13
|
|
|
|
|
/* FPU registers are clean if bits are 0b10 */
|
|
|
|
|
#define CSR_MSTATUS_FPU_CLEAN_STATE 2
|
|
|
|
|
/* FPU status in mstatus are represented with two bits */
|
|
|
|
|
#define CSR_MSTATUS_FPU_MASK 3
|
|
|
|
|
/* FPU is enabled when writing 1 to FPU bits */
|
|
|
|
|
#define CSR_MSTATUS_FPU_ENA BIT(13)
|
|
|
|
|
/* Set FPU registers state to clean (after being dirty) */
|
|
|
|
|
#define CSR_MSTATUS_FPU_CLEAR BIT(13)
|
|
|
|
|
|
|
|
|
|
#endif /* SOC_CPU_HAS_FPU */
|
|
|
|
|
|
2023-07-27 03:05:26 -04:00
|
|
|
|
/* SW defined level which the interrupt module will mask interrupt with priority less than threshold during critical sections
|
|
|
|
|
and spinlocks */
|
2023-07-20 23:36:28 -04:00
|
|
|
|
#define RVHAL_EXCM_LEVEL 4
|
|
|
|
|
|
2022-06-07 02:46:23 -04:00
|
|
|
|
/* --------------------------------------------------- CPU Control -----------------------------------------------------
|
|
|
|
|
*
|
|
|
|
|
* ------------------------------------------------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
|
|
FORCE_INLINE_ATTR void __attribute__((always_inline)) rv_utils_wait_for_intr(void)
|
|
|
|
|
{
|
|
|
|
|
asm volatile ("wfi\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------- CPU Registers ----------------------------------------------------
|
|
|
|
|
*
|
|
|
|
|
* ------------------------------------------------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
|
|
FORCE_INLINE_ATTR __attribute__((pure)) uint32_t rv_utils_get_core_id(void)
|
|
|
|
|
{
|
|
|
|
|
#if SOC_CPU_CORES_NUM == 1
|
|
|
|
|
return 0; // No need to check core ID on single core hardware
|
|
|
|
|
#else
|
|
|
|
|
uint32_t cpuid;
|
|
|
|
|
cpuid = RV_READ_CSR(mhartid);
|
|
|
|
|
return cpuid;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FORCE_INLINE_ATTR void *rv_utils_get_sp(void)
|
|
|
|
|
{
|
|
|
|
|
void *sp;
|
|
|
|
|
asm volatile ("mv %0, sp;" : "=r" (sp));
|
|
|
|
|
return sp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FORCE_INLINE_ATTR uint32_t __attribute__((always_inline)) rv_utils_get_cycle_count(void)
|
|
|
|
|
{
|
2024-03-14 10:21:29 -04:00
|
|
|
|
#if !SOC_CPU_HAS_CSR_PC
|
2023-07-19 04:02:33 -04:00
|
|
|
|
return RV_READ_CSR(mcycle);
|
|
|
|
|
#else
|
2022-06-07 02:46:23 -04:00
|
|
|
|
return RV_READ_CSR(CSR_PCCR_MACHINE);
|
2023-07-19 04:02:33 -04:00
|
|
|
|
#endif
|
2022-06-07 02:46:23 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FORCE_INLINE_ATTR void __attribute__((always_inline)) rv_utils_set_cycle_count(uint32_t ccount)
|
|
|
|
|
{
|
2024-03-14 10:21:29 -04:00
|
|
|
|
#if !SOC_CPU_HAS_CSR_PC
|
2023-07-19 04:02:33 -04:00
|
|
|
|
RV_WRITE_CSR(mcycle, ccount);
|
|
|
|
|
#else
|
2022-06-07 02:46:23 -04:00
|
|
|
|
RV_WRITE_CSR(CSR_PCCR_MACHINE, ccount);
|
2023-07-19 04:02:33 -04:00
|
|
|
|
#endif
|
2022-06-07 02:46:23 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ------------------------------------------------- CPU Interrupts ----------------------------------------------------
|
|
|
|
|
*
|
|
|
|
|
* ------------------------------------------------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
|
|
// --------------- Interrupt Configuration -----------------
|
|
|
|
|
|
|
|
|
|
FORCE_INLINE_ATTR void rv_utils_set_mtvec(uint32_t mtvec_val)
|
|
|
|
|
{
|
2024-02-26 23:29:13 -05:00
|
|
|
|
RV_WRITE_CSR(mtvec, mtvec_val | MTVEC_MODE_CSR);
|
2023-09-22 10:25:56 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-07 02:46:23 -04:00
|
|
|
|
// ------------------ Interrupt Control --------------------
|
|
|
|
|
|
|
|
|
|
FORCE_INLINE_ATTR void rv_utils_intr_enable(uint32_t intr_mask)
|
|
|
|
|
{
|
2022-07-26 10:07:58 -04:00
|
|
|
|
// Disable all interrupts to make updating of the interrupt mask atomic.
|
2022-06-07 02:46:23 -04:00
|
|
|
|
unsigned old_mstatus = RV_CLEAR_CSR(mstatus, MSTATUS_MIE);
|
2023-12-28 21:59:55 -05:00
|
|
|
|
esprv_int_enable(intr_mask);
|
2022-06-07 02:46:23 -04:00
|
|
|
|
RV_SET_CSR(mstatus, old_mstatus & MSTATUS_MIE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FORCE_INLINE_ATTR void rv_utils_intr_disable(uint32_t intr_mask)
|
|
|
|
|
{
|
2022-07-26 10:07:58 -04:00
|
|
|
|
// Disable all interrupts to make updating of the interrupt mask atomic.
|
2022-06-07 02:46:23 -04:00
|
|
|
|
unsigned old_mstatus = RV_CLEAR_CSR(mstatus, MSTATUS_MIE);
|
2023-12-28 21:59:55 -05:00
|
|
|
|
esprv_int_disable(intr_mask);
|
2022-06-07 02:46:23 -04:00
|
|
|
|
RV_SET_CSR(mstatus, old_mstatus & MSTATUS_MIE);
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-26 10:07:58 -04:00
|
|
|
|
FORCE_INLINE_ATTR void rv_utils_intr_global_enable(void)
|
|
|
|
|
{
|
|
|
|
|
RV_SET_CSR(mstatus, MSTATUS_MIE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FORCE_INLINE_ATTR void rv_utils_intr_global_disable(void)
|
|
|
|
|
{
|
|
|
|
|
RV_CLEAR_CSR(mstatus, MSTATUS_MIE);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-26 23:29:13 -05:00
|
|
|
|
/**
|
|
|
|
|
* The other rv_utils functions related to each interrupt controller are defined in `interrupt_clic.h`, `interrupt_plic.h`,
|
|
|
|
|
* and `interrupt_intc.h`.
|
|
|
|
|
*/
|
2023-09-06 07:17:24 -04:00
|
|
|
|
|
|
|
|
|
/* ------------------------------------------------- FPU Related ----------------------------------------------------
|
|
|
|
|
*
|
|
|
|
|
* ------------------------------------------------------------------------------------------------------------------ */
|
|
|
|
|
|
2024-04-15 11:09:11 -04:00
|
|
|
|
#if SOC_CPU_HAS_FPU
|
|
|
|
|
|
2023-09-06 07:17:24 -04:00
|
|
|
|
FORCE_INLINE_ATTR bool rv_utils_enable_fpu(void)
|
|
|
|
|
{
|
|
|
|
|
/* Set mstatus[14:13] to 0b01 to start the floating-point unit initialization */
|
|
|
|
|
RV_SET_CSR(mstatus, CSR_MSTATUS_FPU_ENA);
|
|
|
|
|
/* On the ESP32-P4, the FPU can be used directly after setting `mstatus` bit 13.
|
|
|
|
|
* Since the interrupt handler expects the FPU states to be either 0b10 or 0b11,
|
|
|
|
|
* let's write the FPU CSR and clear the dirty bit afterwards. */
|
|
|
|
|
RV_WRITE_CSR(fcsr, 1);
|
|
|
|
|
RV_CLEAR_CSR(mstatus, CSR_MSTATUS_FPU_CLEAR);
|
|
|
|
|
const uint32_t mstatus = RV_READ_CSR(mstatus);
|
|
|
|
|
/* Make sure the FPU state is 0b10 (clean registers) */
|
|
|
|
|
return ((mstatus >> CSR_MSTATUS_FPU_SHIFT) & CSR_MSTATUS_FPU_MASK) == CSR_MSTATUS_FPU_CLEAN_STATE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FORCE_INLINE_ATTR void rv_utils_disable_fpu(void)
|
|
|
|
|
{
|
|
|
|
|
/* Clear mstatus[14:13] bits to disable the floating-point unit */
|
|
|
|
|
RV_CLEAR_CSR(mstatus, CSR_MSTATUS_FPU_MASK << CSR_MSTATUS_FPU_SHIFT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif /* SOC_CPU_HAS_FPU */
|
|
|
|
|
|
|
|
|
|
|
2024-04-30 04:37:40 -04:00
|
|
|
|
/* ------------------------------------------------- PIE Related ----------------------------------------------------
|
|
|
|
|
*
|
|
|
|
|
* ------------------------------------------------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
|
|
#if SOC_CPU_HAS_PIE
|
|
|
|
|
|
|
|
|
|
FORCE_INLINE_ATTR void rv_utils_enable_pie(void)
|
|
|
|
|
{
|
|
|
|
|
RV_WRITE_CSR(CSR_PIE_STATE_REG, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FORCE_INLINE_ATTR void rv_utils_disable_pie(void)
|
|
|
|
|
{
|
|
|
|
|
RV_WRITE_CSR(CSR_PIE_STATE_REG, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif /* SOC_CPU_HAS_FPU */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-06-07 02:46:23 -04:00
|
|
|
|
/* -------------------------------------------------- Memory Ports -----------------------------------------------------
|
|
|
|
|
*
|
|
|
|
|
* ------------------------------------------------------------------------------------------------------------------ */
|
|
|
|
|
|
2024-04-15 11:09:11 -04:00
|
|
|
|
#if SOC_ASYNCHRONOUS_BUS_ERROR_MODE
|
|
|
|
|
|
|
|
|
|
FORCE_INLINE_ATTR uintptr_t rv_utils_asynchronous_bus_get_error_pc(void)
|
|
|
|
|
{
|
|
|
|
|
uint32_t error_pc;
|
|
|
|
|
uint32_t mcause, mexstatus;
|
|
|
|
|
|
|
|
|
|
mexstatus = RV_READ_CSR(MEXSTATUS);
|
|
|
|
|
/* MEXSTATUS: Bit 8: Indicates that a load/store access fault (MCAUSE=5/7)
|
|
|
|
|
* is due to bus-error exception. If this bit is not cleared before exiting
|
|
|
|
|
* the exception handler, it will trigger a bus error again.
|
|
|
|
|
* Since we have not mechanisms to recover a normal program execution after
|
|
|
|
|
* load/store error appears, do nothing. */
|
|
|
|
|
if ((mexstatus & BIT(8)) == 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
mcause = RV_READ_CSR(mcause) & 0xFF;
|
|
|
|
|
if (mcause == 5) { /* Load access fault */
|
|
|
|
|
/* Get the oldest PC at which the load instruction failed */
|
|
|
|
|
error_pc = RV_READ_CSR(LDPC1);
|
|
|
|
|
if (error_pc == 0) {
|
|
|
|
|
error_pc = RV_READ_CSR(LDPC0);
|
|
|
|
|
}
|
|
|
|
|
} else if (mcause == 7) { /* Store access fault */
|
|
|
|
|
/* Get the oldest PC at which the store instruction failed */
|
|
|
|
|
error_pc = RV_READ_CSR(STPC2);
|
|
|
|
|
if (error_pc == 0) {
|
|
|
|
|
error_pc = RV_READ_CSR(STPC1);
|
|
|
|
|
if (error_pc == 0) {
|
|
|
|
|
error_pc = RV_READ_CSR(STPC0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
/* Bit 0: Valid bit indicating that this CSR holds the PC (program counter).
|
|
|
|
|
* Clear this bit */
|
|
|
|
|
return error_pc & ~(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif // SOC_ASYNCHRONOUS_BUS_ERROR_MODE
|
|
|
|
|
|
2022-06-07 02:46:23 -04:00
|
|
|
|
/* ---------------------------------------------------- Debugging ------------------------------------------------------
|
|
|
|
|
*
|
|
|
|
|
* ------------------------------------------------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
|
|
// --------------- Breakpoints/Watchpoints -----------------
|
|
|
|
|
|
|
|
|
|
FORCE_INLINE_ATTR void rv_utils_set_breakpoint(int bp_num, uint32_t bp_addr)
|
|
|
|
|
{
|
2024-03-14 10:21:29 -04:00
|
|
|
|
/* The code below sets breakpoint which will trigger `Breakpoint` exception
|
|
|
|
|
* instead transferring control to debugger. */
|
2022-06-07 02:46:23 -04:00
|
|
|
|
RV_WRITE_CSR(tselect, bp_num);
|
2023-04-19 13:37:44 -04:00
|
|
|
|
RV_WRITE_CSR(tcontrol, TCONTROL_MPTE | TCONTROL_MTE);
|
|
|
|
|
RV_WRITE_CSR(tdata1, TDATA1_USER | TDATA1_MACHINE | TDATA1_EXECUTE);
|
2022-06-07 02:46:23 -04:00
|
|
|
|
RV_WRITE_CSR(tdata2, bp_addr);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-19 08:18:11 -04:00
|
|
|
|
FORCE_INLINE_ATTR void rv_utils_set_watchpoint(int wp_num,
|
|
|
|
|
uint32_t wp_addr,
|
|
|
|
|
size_t size,
|
|
|
|
|
bool on_read,
|
|
|
|
|
bool on_write)
|
|
|
|
|
{
|
|
|
|
|
RV_WRITE_CSR(tselect, wp_num);
|
2023-04-19 13:37:44 -04:00
|
|
|
|
RV_WRITE_CSR(tcontrol, TCONTROL_MPTE | TCONTROL_MTE);
|
|
|
|
|
RV_WRITE_CSR(tdata1, TDATA1_USER |
|
2023-04-19 08:18:11 -04:00
|
|
|
|
TDATA1_MACHINE |
|
2023-11-15 01:00:09 -05:00
|
|
|
|
((size == 1) ? TDATA1_MATCH_EXACT : TDATA1_MATCH_NAPOT) |
|
2023-04-19 08:18:11 -04:00
|
|
|
|
(on_read ? TDATA1_LOAD : 0) |
|
|
|
|
|
(on_write ? TDATA1_STORE : 0));
|
|
|
|
|
/* From RISC-V Debug Specification:
|
2023-11-15 01:00:09 -05:00
|
|
|
|
* tdata1(mcontrol) match = 0 : Exact byte match
|
|
|
|
|
*
|
|
|
|
|
* tdata1(mcontrol) match = 1 : NAPOT (Naturally Aligned Power-Of-Two):
|
2023-04-19 08:18:11 -04:00
|
|
|
|
* Matches when the top M bits of any compare value match the top M bits of tdata2.
|
|
|
|
|
* M is XLEN − 1 minus the index of the least-significant bit containing 0 in tdata2.
|
2023-11-15 01:00:09 -05:00
|
|
|
|
* Note: Expecting that size is number power of 2 (numbers should be in the range of 1 ~ 31)
|
2023-04-19 08:18:11 -04:00
|
|
|
|
*
|
2023-11-15 01:00:09 -05:00
|
|
|
|
* Examples for understanding how to calculate match pattern to tdata2:
|
2023-04-19 08:18:11 -04:00
|
|
|
|
*
|
2023-11-15 01:00:09 -05:00
|
|
|
|
* nnnn...nnnnn 1-byte Exact byte match
|
2023-04-19 08:18:11 -04:00
|
|
|
|
* nnnn...nnnn0 2-byte NAPOT range
|
|
|
|
|
* nnnn...nnn01 4-byte NAPOT range
|
|
|
|
|
* nnnn...nn011 8-byte NAPOT range
|
|
|
|
|
* nnnn...n0111 16-byte NAPOT range
|
|
|
|
|
* nnnn...01111 32-byte NAPOT range
|
2023-11-15 01:00:09 -05:00
|
|
|
|
* ...
|
|
|
|
|
* n011...11111 2^31 byte NAPOT range
|
2023-04-19 08:18:11 -04:00
|
|
|
|
* * where n are bits from original address
|
|
|
|
|
*/
|
2023-11-15 01:00:09 -05:00
|
|
|
|
uint32_t match_pattern = (wp_addr & ~(size-1)) | ((size-1) >> 1);
|
|
|
|
|
|
|
|
|
|
RV_WRITE_CSR(tdata2, match_pattern);
|
2023-04-19 08:18:11 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-07 02:46:23 -04:00
|
|
|
|
FORCE_INLINE_ATTR void rv_utils_clear_breakpoint(int bp_num)
|
|
|
|
|
{
|
|
|
|
|
RV_WRITE_CSR(tselect, bp_num);
|
2023-04-19 08:18:11 -04:00
|
|
|
|
/* tdata1 is a WARL(write any read legal) register
|
|
|
|
|
* We can just write 0 to it
|
|
|
|
|
*/
|
2023-04-19 13:37:44 -04:00
|
|
|
|
RV_WRITE_CSR(tdata1, 0);
|
2022-06-07 02:46:23 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-19 08:18:11 -04:00
|
|
|
|
FORCE_INLINE_ATTR void rv_utils_clear_watchpoint(int wp_num)
|
2022-06-07 02:46:23 -04:00
|
|
|
|
{
|
2023-04-19 08:18:11 -04:00
|
|
|
|
/* riscv have the same registers for breakpoints and watchpoints */
|
|
|
|
|
rv_utils_clear_breakpoint(wp_num);
|
2022-06-07 02:46:23 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-19 08:18:11 -04:00
|
|
|
|
FORCE_INLINE_ATTR bool rv_utils_is_trigger_fired(int id)
|
2022-06-07 02:46:23 -04:00
|
|
|
|
{
|
2023-04-19 08:18:11 -04:00
|
|
|
|
RV_WRITE_CSR(tselect, id);
|
|
|
|
|
return (RV_READ_CSR(tdata1) >> TDATA1_HIT_S) & 1;
|
2022-06-07 02:46:23 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---------------------- Debugger -------------------------
|
|
|
|
|
|
|
|
|
|
FORCE_INLINE_ATTR bool rv_utils_dbgr_is_attached(void)
|
|
|
|
|
{
|
|
|
|
|
return REG_GET_BIT(ASSIST_DEBUG_CORE_0_DEBUG_MODE_REG, ASSIST_DEBUG_CORE_0_DEBUG_MODULE_ACTIVE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FORCE_INLINE_ATTR void rv_utils_dbgr_break(void)
|
|
|
|
|
{
|
|
|
|
|
asm volatile("ebreak\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ------------------------------------------------------ Misc ---------------------------------------------------------
|
|
|
|
|
*
|
|
|
|
|
* ------------------------------------------------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
|
|
FORCE_INLINE_ATTR bool rv_utils_compare_and_set(volatile uint32_t *addr, uint32_t compare_value, uint32_t new_value)
|
|
|
|
|
{
|
2023-07-27 03:05:26 -04:00
|
|
|
|
#if __riscv_atomic
|
|
|
|
|
uint32_t old_value = 0;
|
|
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
|
|
/* Based on sample code for CAS from RISCV specs v2.2, atomic instructions */
|
|
|
|
|
__asm__ __volatile__(
|
|
|
|
|
"cas: lr.w %0, 0(%2) \n" // load 4 bytes from addr (%2) into old_value (%0)
|
|
|
|
|
" bne %0, %3, fail \n" // fail if old_value if not equal to compare_value (%3)
|
|
|
|
|
" sc.w %1, %4, 0(%2) \n" // store new_value (%4) into addr,
|
|
|
|
|
" bnez %1, cas \n" // if we failed to store the new value then retry the operation
|
|
|
|
|
"fail: \n"
|
|
|
|
|
: "+r" (old_value), "+r" (error) // output parameters
|
|
|
|
|
: "r" (addr), "r" (compare_value), "r" (new_value) // input parameters
|
|
|
|
|
);
|
|
|
|
|
#else
|
|
|
|
|
// For a single core RV target has no atomic CAS instruction, we can achieve atomicity by disabling interrupts
|
2022-06-07 02:46:23 -04:00
|
|
|
|
unsigned old_mstatus;
|
|
|
|
|
old_mstatus = RV_CLEAR_CSR(mstatus, MSTATUS_MIE);
|
|
|
|
|
// Compare and set
|
|
|
|
|
uint32_t old_value;
|
|
|
|
|
old_value = *addr;
|
|
|
|
|
if (old_value == compare_value) {
|
|
|
|
|
*addr = new_value;
|
|
|
|
|
}
|
|
|
|
|
// Restore interrupts
|
|
|
|
|
RV_SET_CSR(mstatus, old_mstatus & MSTATUS_MIE);
|
|
|
|
|
|
2023-07-27 03:05:26 -04:00
|
|
|
|
#endif //__riscv_atomic
|
2022-06-07 02:46:23 -04:00
|
|
|
|
return (old_value == compare_value);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-20 23:51:38 -04:00
|
|
|
|
#if SOC_BRANCH_PREDICTOR_SUPPORTED
|
|
|
|
|
FORCE_INLINE_ATTR void rv_utils_en_branch_predictor(void)
|
|
|
|
|
{
|
|
|
|
|
#define MHCR 0x7c1
|
|
|
|
|
#define MHCR_RS (1<<4) /* R/W, address return stack set bit */
|
|
|
|
|
#define MHCR_BFE (1<<5) /* R/W, allow predictive jump set bit */
|
|
|
|
|
#define MHCR_BTB (1<<12) /* R/W, branch target prediction enable bit */
|
|
|
|
|
RV_SET_CSR(MHCR, MHCR_RS|MHCR_BFE|MHCR_BTB);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-06-07 02:46:23 -04:00
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#endif
|