/* This is called locally and also from flush_fpu_ipi_handler().  */
void arch_flush_local_fpu(void)
/* This is called locally and also from flush_fpu_ipi_handler().  */
void arch_flush_local_fpu(void)
{
	__ASSERT((csr_read(mstatus) & MSTATUS_IEN) == 0,
		 "must be called with IRQs disabled");
	__ASSERT((csr_read(mstatus) & MSTATUS_FS) == 0,
		 "must be called with FPU access disabled");
	struct k_thread *owner = atomic_ptr_get(&_current_cpu->arch.fpu_owner);
	if (owner != NULL) {
		bool dirty = (_current_cpu->arch.fpu_state == MSTATUS_FS_DIRTY);
		if (dirty) {
			csr_set(mstatus, MSTATUS_FS_CLEAN);
			z_riscv_fpu_save(&owner->arch.saved_fp_context);
		}
		owner->arch.fpu_recently_used = dirty;
		csr_clear(mstatus, MSTATUS_FS);
		atomic_ptr_clear(&_current_cpu->arch.fpu_owner);
		DBG("disable", owner);
	}
}