forked from Mirrors/opensbi
		
	lib: Add csr detect support
As RISC-V ISA allows many CSRs such as pmp, s/mcounteren to be optional in hardware, OpenSBI should provide an option to dynamically detect these csr access capability at run time. Implement a csr read/write access check helper macros. Signed-off-by: Atish Patra <atish.patra@wdc.com> Tested-by: Jonathan Balkind <jbalkind@cs.princeton.edu> Reviewed-by: Anup Patel <anup.patel@wdc.com>
This commit is contained in:
		
							
								
								
									
										50
									
								
								include/sbi/sbi_csr_detect.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								include/sbi/sbi_csr_detect.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,50 @@
 | 
			
		||||
/*
 | 
			
		||||
 * SPDX-License-Identifier: BSD-2-Clause
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2020 Western Digital Corporation or its affiliates.
 | 
			
		||||
 *
 | 
			
		||||
 * Authors:
 | 
			
		||||
 *   Atish Patra <atish.patra@wdc.com>
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef __SBI_CSR_DETECT__H
 | 
			
		||||
#define __SBI_CSR_DETECT__H
 | 
			
		||||
 | 
			
		||||
#include <sbi/riscv_encoding.h>
 | 
			
		||||
#include <sbi/sbi_hart.h>
 | 
			
		||||
 | 
			
		||||
#define csr_read_allowed(csr_num, trap)					\
 | 
			
		||||
	({								\
 | 
			
		||||
	register ulong tinfo asm("a3") = (ulong)trap;			\
 | 
			
		||||
	register ulong ttmp asm("a4");					\
 | 
			
		||||
	register ulong mtvec = sbi_hart_expected_trap_addr();		\
 | 
			
		||||
	register ulong ret = 0;						\
 | 
			
		||||
	asm volatile(							\
 | 
			
		||||
		"add %[ttmp], %[tinfo], zero\n"				\
 | 
			
		||||
		"csrrw %[mtvec], " STR(CSR_MTVEC) ", %[mtvec]\n"	\
 | 
			
		||||
		"csrr %[ret], %[csr]\n"					\
 | 
			
		||||
		"csrw " STR(CSR_MTVEC) ", %[mtvec]"			\
 | 
			
		||||
	    : [mtvec] "+&r"(mtvec), [tinfo] "+&r"(tinfo),		\
 | 
			
		||||
	      [ttmp] "+&r"(ttmp), [ret] "=&r" (ret)			\
 | 
			
		||||
	    : [csr] "i" (csr_num)					\
 | 
			
		||||
	    : "memory");						\
 | 
			
		||||
	ret;								\
 | 
			
		||||
	})								\
 | 
			
		||||
 | 
			
		||||
#define csr_write_allowed(csr_num, trap, value)				\
 | 
			
		||||
	({								\
 | 
			
		||||
	register ulong tinfo asm("a3") = (ulong)trap;			\
 | 
			
		||||
	register ulong ttmp asm("a4");					\
 | 
			
		||||
	register ulong mtvec = sbi_hart_expected_trap_addr();		\
 | 
			
		||||
	asm volatile(							\
 | 
			
		||||
		"add %[ttmp], %[tinfo], zero\n"				\
 | 
			
		||||
		"csrrw %[mtvec], " STR(CSR_MTVEC) ", %[mtvec]\n"	\
 | 
			
		||||
		"csrw %[csr], %[val]\n"					\
 | 
			
		||||
		"csrw " STR(CSR_MTVEC) ", %[mtvec]"			\
 | 
			
		||||
	    : [mtvec] "+&r"(mtvec),					\
 | 
			
		||||
	      [tinfo] "+&r"(tinfo), [ttmp] "+&r"(ttmp)			\
 | 
			
		||||
	    : [csr] "i" (csr_num), [val] "r" (value)			\
 | 
			
		||||
	    : "memory");						\
 | 
			
		||||
	})								\
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
		Reference in New Issue
	
	Block a user