forked from Mirrors/opensbi
		
	include: sbi: Optimize reads of mhartid and mscratch
csr_read() is marked as volatile and clobbering memory, which is generally the safe thing to do. However, these two CSRs do not have any side effects, and the values returned do not change between calls. The compiler can generate better code if we allow it to reorder calls to these functions and cache the return value. Introduce csr_read_relaxed() for this use case. Signed-off-by: Samuel Holland <samuel.holland@sifive.com> Reviewed-by: Anup Patel <anup@brainfault.org>
This commit is contained in:
		
				
					committed by
					
						
						Anup Patel
					
				
			
			
				
	
			
			
			
						parent
						
							3e0c170397
						
					
				
				
					commit
					62447cd7aa
				
			@@ -101,6 +101,14 @@
 | 
			
		||||
		__v;                                            \
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
/* Variant of csr_read() that allows the compiler to cache the value. */
 | 
			
		||||
#define csr_read_relaxed(csr)                                     \
 | 
			
		||||
	({                                                        \
 | 
			
		||||
		register unsigned long __v;                       \
 | 
			
		||||
		__asm__ ("csrr %0, " __ASM_STR(csr) : "=r"(__v)); \
 | 
			
		||||
		__v;                                              \
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
#define csr_write(csr, val)                                        \
 | 
			
		||||
	({                                                         \
 | 
			
		||||
		unsigned long __v = (unsigned long)(val);          \
 | 
			
		||||
@@ -163,7 +171,7 @@ void csr_write_num(int csr_num, unsigned long val);
 | 
			
		||||
	} while (0)
 | 
			
		||||
 | 
			
		||||
/* Get current HART id */
 | 
			
		||||
#define current_hartid()	((unsigned int)csr_read(CSR_MHARTID))
 | 
			
		||||
#define current_hartid()	((unsigned int)csr_read_relaxed(CSR_MHARTID))
 | 
			
		||||
 | 
			
		||||
/* determine CPU extension, return non-zero support */
 | 
			
		||||
int misa_extension_imp(char ext);
 | 
			
		||||
 
 | 
			
		||||
@@ -159,7 +159,7 @@ enum sbi_scratch_options {
 | 
			
		||||
 | 
			
		||||
/** Get pointer to sbi_scratch for current HART */
 | 
			
		||||
#define sbi_scratch_thishart_ptr() \
 | 
			
		||||
	((struct sbi_scratch *)csr_read(CSR_MSCRATCH))
 | 
			
		||||
	((struct sbi_scratch *)csr_read_relaxed(CSR_MSCRATCH))
 | 
			
		||||
 | 
			
		||||
/** Get Arg1 of next booting stage for current HART */
 | 
			
		||||
#define sbi_scratch_thishart_arg1_ptr() \
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user