xref: /openbmc/u-boot/arch/arm/include/asm/system.h (revision 0c4b382f9041f9f2f00246c8a0ece90dae5451be)
1819833afSPeter Tyser #ifndef __ASM_ARM_SYSTEM_H
2819833afSPeter Tyser #define __ASM_ARM_SYSTEM_H
3819833afSPeter Tyser 
4a5b9fa30SSergey Temerkhanov #include <common.h>
5a5b9fa30SSergey Temerkhanov #include <linux/compiler.h>
6a78cd861STom Rini #include <asm/barriers.h>
7a5b9fa30SSergey Temerkhanov 
80ae76531SDavid Feng #ifdef CONFIG_ARM64
90ae76531SDavid Feng 
100ae76531SDavid Feng /*
110ae76531SDavid Feng  * SCTLR_EL1/SCTLR_EL2/SCTLR_EL3 bits definitions
120ae76531SDavid Feng  */
130ae76531SDavid Feng #define CR_M		(1 << 0)	/* MMU enable			*/
140ae76531SDavid Feng #define CR_A		(1 << 1)	/* Alignment abort enable	*/
150ae76531SDavid Feng #define CR_C		(1 << 2)	/* Dcache enable		*/
160ae76531SDavid Feng #define CR_SA		(1 << 3)	/* Stack Alignment Check Enable	*/
170ae76531SDavid Feng #define CR_I		(1 << 12)	/* Icache enable		*/
180ae76531SDavid Feng #define CR_WXN		(1 << 19)	/* Write Permision Imply XN	*/
190ae76531SDavid Feng #define CR_EE		(1 << 25)	/* Exception (Big) Endian	*/
200ae76531SDavid Feng 
21ec6617c3SAlison Wang #define ES_TO_AARCH64		1
22ec6617c3SAlison Wang #define ES_TO_AARCH32		0
23ec6617c3SAlison Wang 
24ec6617c3SAlison Wang /*
25ec6617c3SAlison Wang  * SCR_EL3 bits definitions
26ec6617c3SAlison Wang  */
27ec6617c3SAlison Wang #define SCR_EL3_RW_AARCH64	(1 << 10) /* Next lower level is AArch64     */
28ec6617c3SAlison Wang #define SCR_EL3_RW_AARCH32	(0 << 10) /* Lower lowers level are AArch32  */
29ec6617c3SAlison Wang #define SCR_EL3_HCE_EN		(1 << 8)  /* Hypervisor Call enable          */
30ec6617c3SAlison Wang #define SCR_EL3_SMD_DIS		(1 << 7)  /* Secure Monitor Call disable     */
31ec6617c3SAlison Wang #define SCR_EL3_RES1		(3 << 4)  /* Reserved, RES1                  */
32*a7aab5bcSChee Hong Ang #define SCR_EL3_EA_EN		(1 << 3)  /* External aborts taken to EL3    */
33ec6617c3SAlison Wang #define SCR_EL3_NS_EN		(1 << 0)  /* EL0 and EL1 in Non-scure state  */
34ec6617c3SAlison Wang 
35ec6617c3SAlison Wang /*
36ec6617c3SAlison Wang  * SPSR_EL3/SPSR_EL2 bits definitions
37ec6617c3SAlison Wang  */
38ec6617c3SAlison Wang #define SPSR_EL_END_LE		(0 << 9)  /* Exception Little-endian          */
39ec6617c3SAlison Wang #define SPSR_EL_DEBUG_MASK	(1 << 9)  /* Debug exception masked           */
40ec6617c3SAlison Wang #define SPSR_EL_ASYN_MASK	(1 << 8)  /* Asynchronous data abort masked   */
41ec6617c3SAlison Wang #define SPSR_EL_SERR_MASK	(1 << 8)  /* System Error exception masked    */
42ec6617c3SAlison Wang #define SPSR_EL_IRQ_MASK	(1 << 7)  /* IRQ exception masked             */
43ec6617c3SAlison Wang #define SPSR_EL_FIQ_MASK	(1 << 6)  /* FIQ exception masked             */
44ec6617c3SAlison Wang #define SPSR_EL_T_A32		(0 << 5)  /* AArch32 instruction set A32      */
45ec6617c3SAlison Wang #define SPSR_EL_M_AARCH64	(0 << 4)  /* Exception taken from AArch64     */
46ec6617c3SAlison Wang #define SPSR_EL_M_AARCH32	(1 << 4)  /* Exception taken from AArch32     */
47ec6617c3SAlison Wang #define SPSR_EL_M_SVC		(0x3)     /* Exception taken from SVC mode    */
48ec6617c3SAlison Wang #define SPSR_EL_M_HYP		(0xa)     /* Exception taken from HYP mode    */
49ec6617c3SAlison Wang #define SPSR_EL_M_EL1H		(5)       /* Exception taken from EL1h mode   */
50ec6617c3SAlison Wang #define SPSR_EL_M_EL2H		(9)       /* Exception taken from EL2h mode   */
51ec6617c3SAlison Wang 
52ec6617c3SAlison Wang /*
53ec6617c3SAlison Wang  * CPTR_EL2 bits definitions
54ec6617c3SAlison Wang  */
55ec6617c3SAlison Wang #define CPTR_EL2_RES1		(3 << 12 | 0x3ff)           /* Reserved, RES1 */
56ec6617c3SAlison Wang 
57ec6617c3SAlison Wang /*
58ec6617c3SAlison Wang  * SCTLR_EL2 bits definitions
59ec6617c3SAlison Wang  */
60ec6617c3SAlison Wang #define SCTLR_EL2_RES1		(3 << 28 | 3 << 22 | 1 << 18 | 1 << 16 |\
61ec6617c3SAlison Wang 				 1 << 11 | 3 << 4)	    /* Reserved, RES1 */
62ec6617c3SAlison Wang #define SCTLR_EL2_EE_LE		(0 << 25) /* Exception Little-endian          */
63ec6617c3SAlison Wang #define SCTLR_EL2_WXN_DIS	(0 << 19) /* Write permission is not XN       */
64ec6617c3SAlison Wang #define SCTLR_EL2_ICACHE_DIS	(0 << 12) /* Instruction cache disabled       */
65ec6617c3SAlison Wang #define SCTLR_EL2_SA_DIS	(0 << 3)  /* Stack Alignment Check disabled   */
66ec6617c3SAlison Wang #define SCTLR_EL2_DCACHE_DIS	(0 << 2)  /* Data cache disabled              */
67ec6617c3SAlison Wang #define SCTLR_EL2_ALIGN_DIS	(0 << 1)  /* Alignment check disabled         */
68ec6617c3SAlison Wang #define SCTLR_EL2_MMU_DIS	(0)       /* MMU disabled                     */
69ec6617c3SAlison Wang 
70ec6617c3SAlison Wang /*
71ec6617c3SAlison Wang  * CNTHCTL_EL2 bits definitions
72ec6617c3SAlison Wang  */
73ec6617c3SAlison Wang #define CNTHCTL_EL2_EL1PCEN_EN	(1 << 1)  /* Physical timer regs accessible   */
74ec6617c3SAlison Wang #define CNTHCTL_EL2_EL1PCTEN_EN	(1 << 0)  /* Physical counter accessible      */
75ec6617c3SAlison Wang 
76ec6617c3SAlison Wang /*
77ec6617c3SAlison Wang  * HCR_EL2 bits definitions
78ec6617c3SAlison Wang  */
79ec6617c3SAlison Wang #define HCR_EL2_RW_AARCH64	(1 << 31) /* EL1 is AArch64                   */
80ec6617c3SAlison Wang #define HCR_EL2_RW_AARCH32	(0 << 31) /* Lower levels are AArch32         */
81ec6617c3SAlison Wang #define HCR_EL2_HCD_DIS		(1 << 29) /* Hypervisor Call disabled         */
82ec6617c3SAlison Wang 
83ec6617c3SAlison Wang /*
84ec6617c3SAlison Wang  * CPACR_EL1 bits definitions
85ec6617c3SAlison Wang  */
86ec6617c3SAlison Wang #define CPACR_EL1_FPEN_EN	(3 << 20) /* SIMD and FP instruction enabled  */
87ec6617c3SAlison Wang 
88ec6617c3SAlison Wang /*
89ec6617c3SAlison Wang  * SCTLR_EL1 bits definitions
90ec6617c3SAlison Wang  */
91ec6617c3SAlison Wang #define SCTLR_EL1_RES1		(3 << 28 | 3 << 22 | 1 << 20 |\
92ec6617c3SAlison Wang 				 1 << 11) /* Reserved, RES1                   */
93ec6617c3SAlison Wang #define SCTLR_EL1_UCI_DIS	(0 << 26) /* Cache instruction disabled       */
94ec6617c3SAlison Wang #define SCTLR_EL1_EE_LE		(0 << 25) /* Exception Little-endian          */
95ec6617c3SAlison Wang #define SCTLR_EL1_WXN_DIS	(0 << 19) /* Write permission is not XN       */
96ec6617c3SAlison Wang #define SCTLR_EL1_NTWE_DIS	(0 << 18) /* WFE instruction disabled         */
97ec6617c3SAlison Wang #define SCTLR_EL1_NTWI_DIS	(0 << 16) /* WFI instruction disabled         */
98ec6617c3SAlison Wang #define SCTLR_EL1_UCT_DIS	(0 << 15) /* CTR_EL0 access disabled          */
99ec6617c3SAlison Wang #define SCTLR_EL1_DZE_DIS	(0 << 14) /* DC ZVA instruction disabled      */
100ec6617c3SAlison Wang #define SCTLR_EL1_ICACHE_DIS	(0 << 12) /* Instruction cache disabled       */
101ec6617c3SAlison Wang #define SCTLR_EL1_UMA_DIS	(0 << 9)  /* User Mask Access disabled        */
102ec6617c3SAlison Wang #define SCTLR_EL1_SED_EN	(0 << 8)  /* SETEND instruction enabled       */
103ec6617c3SAlison Wang #define SCTLR_EL1_ITD_EN	(0 << 7)  /* IT instruction enabled           */
104ec6617c3SAlison Wang #define SCTLR_EL1_CP15BEN_DIS	(0 << 5)  /* CP15 barrier operation disabled  */
105ec6617c3SAlison Wang #define SCTLR_EL1_SA0_DIS	(0 << 4)  /* Stack Alignment EL0 disabled     */
106ec6617c3SAlison Wang #define SCTLR_EL1_SA_DIS	(0 << 3)  /* Stack Alignment EL1 disabled     */
107ec6617c3SAlison Wang #define SCTLR_EL1_DCACHE_DIS	(0 << 2)  /* Data cache disabled              */
108ec6617c3SAlison Wang #define SCTLR_EL1_ALIGN_DIS	(0 << 1)  /* Alignment check disabled         */
109ec6617c3SAlison Wang #define SCTLR_EL1_MMU_DIS	(0)       /* MMU disabled                     */
110ec6617c3SAlison Wang 
1117985cdf7SAlexander Graf #ifndef __ASSEMBLY__
1127985cdf7SAlexander Graf 
1137985cdf7SAlexander Graf u64 get_page_table_size(void);
1147985cdf7SAlexander Graf #define PGTABLE_SIZE	get_page_table_size()
1157985cdf7SAlexander Graf 
116dad17fd5SSiva Durga Prasad Paladugu /* 2MB granularity */
117dad17fd5SSiva Durga Prasad Paladugu #define MMU_SECTION_SHIFT	21
11888f965d7SStephen Warren #define MMU_SECTION_SIZE	(1 << MMU_SECTION_SHIFT)
1190ae76531SDavid Feng 
12053eb45efSAlexander Graf /* These constants need to be synced to the MT_ types in asm/armv8/mmu.h */
121dad17fd5SSiva Durga Prasad Paladugu enum dcache_option {
12253eb45efSAlexander Graf 	DCACHE_OFF = 0 << 2,
12353eb45efSAlexander Graf 	DCACHE_WRITETHROUGH = 3 << 2,
12453eb45efSAlexander Graf 	DCACHE_WRITEBACK = 4 << 2,
12553eb45efSAlexander Graf 	DCACHE_WRITEALLOC = 4 << 2,
126dad17fd5SSiva Durga Prasad Paladugu };
127dad17fd5SSiva Durga Prasad Paladugu 
1280ae76531SDavid Feng #define wfi()				\
1290ae76531SDavid Feng 	({asm volatile(			\
1300ae76531SDavid Feng 	"wfi" : : : "memory");		\
1310ae76531SDavid Feng 	})
1320ae76531SDavid Feng 
current_el(void)1330ae76531SDavid Feng static inline unsigned int current_el(void)
1340ae76531SDavid Feng {
1350ae76531SDavid Feng 	unsigned int el;
1360ae76531SDavid Feng 	asm volatile("mrs %0, CurrentEL" : "=r" (el) : : "cc");
1370ae76531SDavid Feng 	return el >> 2;
1380ae76531SDavid Feng }
1390ae76531SDavid Feng 
get_sctlr(void)1400ae76531SDavid Feng static inline unsigned int get_sctlr(void)
1410ae76531SDavid Feng {
1420ae76531SDavid Feng 	unsigned int el, val;
1430ae76531SDavid Feng 
1440ae76531SDavid Feng 	el = current_el();
1450ae76531SDavid Feng 	if (el == 1)
1460ae76531SDavid Feng 		asm volatile("mrs %0, sctlr_el1" : "=r" (val) : : "cc");
1470ae76531SDavid Feng 	else if (el == 2)
1480ae76531SDavid Feng 		asm volatile("mrs %0, sctlr_el2" : "=r" (val) : : "cc");
1490ae76531SDavid Feng 	else
1500ae76531SDavid Feng 		asm volatile("mrs %0, sctlr_el3" : "=r" (val) : : "cc");
1510ae76531SDavid Feng 
1520ae76531SDavid Feng 	return val;
1530ae76531SDavid Feng }
1540ae76531SDavid Feng 
set_sctlr(unsigned int val)1550ae76531SDavid Feng static inline void set_sctlr(unsigned int val)
1560ae76531SDavid Feng {
1570ae76531SDavid Feng 	unsigned int el;
1580ae76531SDavid Feng 
1590ae76531SDavid Feng 	el = current_el();
1600ae76531SDavid Feng 	if (el == 1)
1610ae76531SDavid Feng 		asm volatile("msr sctlr_el1, %0" : : "r" (val) : "cc");
1620ae76531SDavid Feng 	else if (el == 2)
1630ae76531SDavid Feng 		asm volatile("msr sctlr_el2, %0" : : "r" (val) : "cc");
1640ae76531SDavid Feng 	else
1650ae76531SDavid Feng 		asm volatile("msr sctlr_el3, %0" : : "r" (val) : "cc");
1660ae76531SDavid Feng 
1670ae76531SDavid Feng 	asm volatile("isb");
1680ae76531SDavid Feng }
1690ae76531SDavid Feng 
read_mpidr(void)170ba5648cdSSergey Temerkhanov static inline unsigned long read_mpidr(void)
171ba5648cdSSergey Temerkhanov {
172ba5648cdSSergey Temerkhanov 	unsigned long val;
173ba5648cdSSergey Temerkhanov 
174ba5648cdSSergey Temerkhanov 	asm volatile("mrs %0, mpidr_el1" : "=r" (val));
175ba5648cdSSergey Temerkhanov 
176ba5648cdSSergey Temerkhanov 	return val;
177ba5648cdSSergey Temerkhanov }
178ba5648cdSSergey Temerkhanov 
179ba5648cdSSergey Temerkhanov #define BSP_COREID	0
180ba5648cdSSergey Temerkhanov 
1810ae76531SDavid Feng void __asm_flush_dcache_all(void);
1821e6ad55cSYork Sun void __asm_invalidate_dcache_all(void);
1830ae76531SDavid Feng void __asm_flush_dcache_range(u64 start, u64 end);
1846775a820SSimon Glass 
1856775a820SSimon Glass /**
1866775a820SSimon Glass  * __asm_invalidate_dcache_range() - Invalidate a range of virtual addresses
1876775a820SSimon Glass  *
1886775a820SSimon Glass  * This performance an invalidate from @start to @end - 1. Both addresses
1896775a820SSimon Glass  * should be cache-aligned, otherwise this function will align the start
1906775a820SSimon Glass  * address and may continue past the end address.
1916775a820SSimon Glass  *
1926775a820SSimon Glass  * Data in the address range is evicted from the cache and is not written back
1936775a820SSimon Glass  * to memory.
1946775a820SSimon Glass  *
1956775a820SSimon Glass  * @start: Start address to invalidate
1966775a820SSimon Glass  * @end: End address to invalidate up to (exclusive)
1976775a820SSimon Glass  */
1986775a820SSimon Glass void __asm_invalidate_dcache_range(u64 start, u64 end);
1990ae76531SDavid Feng void __asm_invalidate_tlb_all(void);
2000ae76531SDavid Feng void __asm_invalidate_icache_all(void);
2011ab557a0SStephen Warren int __asm_invalidate_l3_dcache(void);
2021ab557a0SStephen Warren int __asm_flush_l3_dcache(void);
2031ab557a0SStephen Warren int __asm_invalidate_l3_icache(void);
2045e2ec773SAlexander Graf void __asm_switch_ttbr(u64 new_ttbr);
2050ae76531SDavid Feng 
206ec6617c3SAlison Wang /*
207ec6617c3SAlison Wang  * Switch from EL3 to EL2 for ARMv8
208ec6617c3SAlison Wang  *
209ec6617c3SAlison Wang  * @args:        For loading 64-bit OS, fdt address.
210ec6617c3SAlison Wang  *               For loading 32-bit OS, zero.
211ec6617c3SAlison Wang  * @mach_nr:     For loading 64-bit OS, zero.
212ec6617c3SAlison Wang  *               For loading 32-bit OS, machine nr
213ec6617c3SAlison Wang  * @fdt_addr:    For loading 64-bit OS, zero.
214ec6617c3SAlison Wang  *               For loading 32-bit OS, fdt address.
2157c5e1febSAlison Wang  * @arg4:	 Input argument.
216ec6617c3SAlison Wang  * @entry_point: kernel entry point
217ec6617c3SAlison Wang  * @es_flag:     execution state flag, ES_TO_AARCH64 or ES_TO_AARCH32
218ec6617c3SAlison Wang  */
219fb97b862SYork Sun void __noreturn armv8_switch_to_el2(u64 args, u64 mach_nr, u64 fdt_addr,
2207c5e1febSAlison Wang 				    u64 arg4, u64 entry_point, u64 es_flag);
221ec6617c3SAlison Wang /*
222ec6617c3SAlison Wang  * Switch from EL2 to EL1 for ARMv8
223ec6617c3SAlison Wang  *
224ec6617c3SAlison Wang  * @args:        For loading 64-bit OS, fdt address.
225ec6617c3SAlison Wang  *               For loading 32-bit OS, zero.
226ec6617c3SAlison Wang  * @mach_nr:     For loading 64-bit OS, zero.
227ec6617c3SAlison Wang  *               For loading 32-bit OS, machine nr
228ec6617c3SAlison Wang  * @fdt_addr:    For loading 64-bit OS, zero.
229ec6617c3SAlison Wang  *               For loading 32-bit OS, fdt address.
2307c5e1febSAlison Wang  * @arg4:	 Input argument.
231ec6617c3SAlison Wang  * @entry_point: kernel entry point
232ec6617c3SAlison Wang  * @es_flag:     execution state flag, ES_TO_AARCH64 or ES_TO_AARCH32
233ec6617c3SAlison Wang  */
234ec6617c3SAlison Wang void armv8_switch_to_el1(u64 args, u64 mach_nr, u64 fdt_addr,
2357c5e1febSAlison Wang 			 u64 arg4, u64 entry_point, u64 es_flag);
2363db86f4bSAlison Wang void armv8_el2_to_aarch32(u64 args, u64 mach_nr, u64 fdt_addr,
2377c5e1febSAlison Wang 			  u64 arg4, u64 entry_point);
2380ae76531SDavid Feng void gic_init(void);
2390ae76531SDavid Feng void gic_send_sgi(unsigned long sgino);
2400ae76531SDavid Feng void wait_for_wakeup(void);
24173169874SIan Campbell void protect_secure_region(void);
2420ae76531SDavid Feng void smp_kick_all_cpus(void);
2430ae76531SDavid Feng 
2442f78eae5SYork Sun void flush_l3_cache(void);
2457f9b9f31SYork Sun void mmu_change_region_attr(phys_addr_t start, size_t size, u64 attrs);
2462f78eae5SYork Sun 
247a5b9fa30SSergey Temerkhanov /*
248a5b9fa30SSergey Temerkhanov  *Issue a secure monitor call in accordance with ARM "SMC Calling convention",
249a5b9fa30SSergey Temerkhanov  * DEN0028A
250a5b9fa30SSergey Temerkhanov  *
251a5b9fa30SSergey Temerkhanov  * @args: input and output arguments
252a5b9fa30SSergey Temerkhanov  *
253a5b9fa30SSergey Temerkhanov  */
254a5b9fa30SSergey Temerkhanov void smc_call(struct pt_regs *args);
255a5b9fa30SSergey Temerkhanov 
25651bfb5b6SAlexander Graf void __noreturn psci_system_reset(void);
2573ee655edSAlexander Graf void __noreturn psci_system_off(void);
2585a07abb3SBeniamino Galvani 
2599a561753Smacro.wave.z@gmail.com #ifdef CONFIG_ARMV8_PSCI
2609a561753Smacro.wave.z@gmail.com extern char __secure_start[];
2619a561753Smacro.wave.z@gmail.com extern char __secure_end[];
2629a561753Smacro.wave.z@gmail.com extern char __secure_stack_start[];
2639a561753Smacro.wave.z@gmail.com extern char __secure_stack_end[];
2649a561753Smacro.wave.z@gmail.com 
2659a561753Smacro.wave.z@gmail.com void armv8_setup_psci(void);
2669a561753Smacro.wave.z@gmail.com void psci_setup_vectors(void);
2679a561753Smacro.wave.z@gmail.com void psci_arch_init(void);
2689a561753Smacro.wave.z@gmail.com #endif
2699a561753Smacro.wave.z@gmail.com 
2700ae76531SDavid Feng #endif	/* __ASSEMBLY__ */
2710ae76531SDavid Feng 
2720ae76531SDavid Feng #else /* CONFIG_ARM64 */
2730ae76531SDavid Feng 
274819833afSPeter Tyser #ifdef __KERNEL__
275819833afSPeter Tyser 
276819833afSPeter Tyser #define CPU_ARCH_UNKNOWN	0
277819833afSPeter Tyser #define CPU_ARCH_ARMv3		1
278819833afSPeter Tyser #define CPU_ARCH_ARMv4		2
279819833afSPeter Tyser #define CPU_ARCH_ARMv4T		3
280819833afSPeter Tyser #define CPU_ARCH_ARMv5		4
281819833afSPeter Tyser #define CPU_ARCH_ARMv5T		5
282819833afSPeter Tyser #define CPU_ARCH_ARMv5TE	6
283819833afSPeter Tyser #define CPU_ARCH_ARMv5TEJ	7
284819833afSPeter Tyser #define CPU_ARCH_ARMv6		8
285819833afSPeter Tyser #define CPU_ARCH_ARMv7		9
286819833afSPeter Tyser 
287819833afSPeter Tyser /*
288819833afSPeter Tyser  * CR1 bits (CP#15 CR1)
289819833afSPeter Tyser  */
290819833afSPeter Tyser #define CR_M	(1 << 0)	/* MMU enable				*/
291819833afSPeter Tyser #define CR_A	(1 << 1)	/* Alignment abort enable		*/
292819833afSPeter Tyser #define CR_C	(1 << 2)	/* Dcache enable			*/
293819833afSPeter Tyser #define CR_W	(1 << 3)	/* Write buffer enable			*/
294819833afSPeter Tyser #define CR_P	(1 << 4)	/* 32-bit exception handler		*/
295819833afSPeter Tyser #define CR_D	(1 << 5)	/* 32-bit data address range		*/
296819833afSPeter Tyser #define CR_L	(1 << 6)	/* Implementation defined		*/
297819833afSPeter Tyser #define CR_B	(1 << 7)	/* Big endian				*/
298819833afSPeter Tyser #define CR_S	(1 << 8)	/* System MMU protection		*/
299819833afSPeter Tyser #define CR_R	(1 << 9)	/* ROM MMU protection			*/
300819833afSPeter Tyser #define CR_F	(1 << 10)	/* Implementation defined		*/
301819833afSPeter Tyser #define CR_Z	(1 << 11)	/* Implementation defined		*/
302819833afSPeter Tyser #define CR_I	(1 << 12)	/* Icache enable			*/
303819833afSPeter Tyser #define CR_V	(1 << 13)	/* Vectors relocated to 0xffff0000	*/
304819833afSPeter Tyser #define CR_RR	(1 << 14)	/* Round Robin cache replacement	*/
305819833afSPeter Tyser #define CR_L4	(1 << 15)	/* LDR pc can set T bit			*/
306819833afSPeter Tyser #define CR_DT	(1 << 16)
307819833afSPeter Tyser #define CR_IT	(1 << 18)
308819833afSPeter Tyser #define CR_ST	(1 << 19)
309819833afSPeter Tyser #define CR_FI	(1 << 21)	/* Fast interrupt (lower latency mode)	*/
310819833afSPeter Tyser #define CR_U	(1 << 22)	/* Unaligned access operation		*/
311819833afSPeter Tyser #define CR_XP	(1 << 23)	/* Extended page tables			*/
312819833afSPeter Tyser #define CR_VE	(1 << 24)	/* Vectored interrupts			*/
313819833afSPeter Tyser #define CR_EE	(1 << 25)	/* Exception (Big) Endian		*/
314819833afSPeter Tyser #define CR_TRE	(1 << 28)	/* TEX remap enable			*/
315819833afSPeter Tyser #define CR_AFE	(1 << 29)	/* Access flag enable			*/
316819833afSPeter Tyser #define CR_TE	(1 << 30)	/* Thumb exception enable		*/
317819833afSPeter Tyser 
318d990f5c8SAlexander Graf #if defined(CONFIG_ARMV7_LPAE) && !defined(PGTABLE_SIZE)
319d990f5c8SAlexander Graf #define PGTABLE_SIZE		(4096 * 5)
320d990f5c8SAlexander Graf #elif !defined(PGTABLE_SIZE)
3210ae76531SDavid Feng #define PGTABLE_SIZE		(4096 * 4)
32294f7ff36SSergey Temerkhanov #endif
3230ae76531SDavid Feng 
324819833afSPeter Tyser /*
325819833afSPeter Tyser  * This is used to ensure the compiler did actually allocate the register we
326819833afSPeter Tyser  * asked it for some inline assembly sequences.  Apparently we can't trust
327819833afSPeter Tyser  * the compiler from one version to another so a bit of paranoia won't hurt.
328819833afSPeter Tyser  * This string is meant to be concatenated with the inline asm string and
329819833afSPeter Tyser  * will cause compilation to stop on mismatch.
330819833afSPeter Tyser  * (for details, see gcc PR 15089)
331819833afSPeter Tyser  */
332819833afSPeter Tyser #define __asmeq(x, y)  ".ifnc " x "," y " ; .err ; .endif\n\t"
333819833afSPeter Tyser 
334819833afSPeter Tyser #ifndef __ASSEMBLY__
335819833afSPeter Tyser 
336d31d4a2dSKeerthy #ifdef CONFIG_ARMV7_LPAE
337d31d4a2dSKeerthy void switch_to_hypervisor_ret(void);
338d31d4a2dSKeerthy #endif
339d31d4a2dSKeerthy 
340819833afSPeter Tyser #define nop() __asm__ __volatile__("mov\tr0,r0\t@ nop\n\t");
341819833afSPeter Tyser 
3422ff467c0SRob Herring #ifdef __ARM_ARCH_7A__
3432ff467c0SRob Herring #define wfi() __asm__ __volatile__ ("wfi" : : : "memory")
3442ff467c0SRob Herring #else
3452ff467c0SRob Herring #define wfi()
3462ff467c0SRob Herring #endif
3472ff467c0SRob Herring 
get_cpsr(void)348d990f5c8SAlexander Graf static inline unsigned long get_cpsr(void)
349d990f5c8SAlexander Graf {
350d990f5c8SAlexander Graf 	unsigned long cpsr;
351d990f5c8SAlexander Graf 
352d990f5c8SAlexander Graf 	asm volatile("mrs %0, cpsr" : "=r"(cpsr): );
353d990f5c8SAlexander Graf 	return cpsr;
354d990f5c8SAlexander Graf }
355d990f5c8SAlexander Graf 
is_hyp(void)356d990f5c8SAlexander Graf static inline int is_hyp(void)
357d990f5c8SAlexander Graf {
358d990f5c8SAlexander Graf #ifdef CONFIG_ARMV7_LPAE
359d990f5c8SAlexander Graf 	/* HYP mode requires LPAE ... */
360d990f5c8SAlexander Graf 	return ((get_cpsr() & 0x1f) == 0x1a);
361d990f5c8SAlexander Graf #else
362d990f5c8SAlexander Graf 	/* ... so without LPAE support we can optimize all hyp code away */
363d990f5c8SAlexander Graf 	return 0;
364d990f5c8SAlexander Graf #endif
365d990f5c8SAlexander Graf }
366d990f5c8SAlexander Graf 
get_cr(void)367819833afSPeter Tyser static inline unsigned int get_cr(void)
368819833afSPeter Tyser {
369819833afSPeter Tyser 	unsigned int val;
370d990f5c8SAlexander Graf 
371d990f5c8SAlexander Graf 	if (is_hyp())
372d990f5c8SAlexander Graf 		asm volatile("mrc p15, 4, %0, c1, c0, 0	@ get CR" : "=r" (val)
373d990f5c8SAlexander Graf 								  :
374d990f5c8SAlexander Graf 								  : "cc");
375d990f5c8SAlexander Graf 	else
376d990f5c8SAlexander Graf 		asm volatile("mrc p15, 0, %0, c1, c0, 0	@ get CR" : "=r" (val)
377d990f5c8SAlexander Graf 								  :
378d990f5c8SAlexander Graf 								  : "cc");
379819833afSPeter Tyser 	return val;
380819833afSPeter Tyser }
381819833afSPeter Tyser 
set_cr(unsigned int val)382819833afSPeter Tyser static inline void set_cr(unsigned int val)
383819833afSPeter Tyser {
384d990f5c8SAlexander Graf 	if (is_hyp())
385d990f5c8SAlexander Graf 		asm volatile("mcr p15, 4, %0, c1, c0, 0	@ set CR" :
386d990f5c8SAlexander Graf 								  : "r" (val)
387d990f5c8SAlexander Graf 								  : "cc");
388d990f5c8SAlexander Graf 	else
389d990f5c8SAlexander Graf 		asm volatile("mcr p15, 0, %0, c1, c0, 0	@ set CR" :
390d990f5c8SAlexander Graf 								  : "r" (val)
391d990f5c8SAlexander Graf 								  : "cc");
392819833afSPeter Tyser 	isb();
393819833afSPeter Tyser }
394819833afSPeter Tyser 
get_dacr(void)395de63ac27SR Sricharan static inline unsigned int get_dacr(void)
396de63ac27SR Sricharan {
397de63ac27SR Sricharan 	unsigned int val;
398de63ac27SR Sricharan 	asm("mrc p15, 0, %0, c3, c0, 0	@ get DACR" : "=r" (val) : : "cc");
399de63ac27SR Sricharan 	return val;
400de63ac27SR Sricharan }
401de63ac27SR Sricharan 
set_dacr(unsigned int val)402de63ac27SR Sricharan static inline void set_dacr(unsigned int val)
403de63ac27SR Sricharan {
404de63ac27SR Sricharan 	asm volatile("mcr p15, 0, %0, c3, c0, 0	@ set DACR"
405de63ac27SR Sricharan 	  : : "r" (val) : "cc");
406de63ac27SR Sricharan 	isb();
407de63ac27SR Sricharan }
408de63ac27SR Sricharan 
409d990f5c8SAlexander Graf #ifdef CONFIG_ARMV7_LPAE
410d990f5c8SAlexander Graf /* Long-Descriptor Translation Table Level 1/2 Bits */
411d990f5c8SAlexander Graf #define TTB_SECT_XN_MASK	(1ULL << 54)
412d990f5c8SAlexander Graf #define TTB_SECT_NG_MASK	(1 << 11)
413d990f5c8SAlexander Graf #define TTB_SECT_AF		(1 << 10)
414d990f5c8SAlexander Graf #define TTB_SECT_SH_MASK	(3 << 8)
415d990f5c8SAlexander Graf #define TTB_SECT_NS_MASK	(1 << 5)
416d990f5c8SAlexander Graf #define TTB_SECT_AP		(1 << 6)
417d990f5c8SAlexander Graf /* Note: TTB AP bits are set elsewhere */
418d990f5c8SAlexander Graf #define TTB_SECT_MAIR(x)	((x & 0x7) << 2) /* Index into MAIR */
419d990f5c8SAlexander Graf #define TTB_SECT		(1 << 0)
420d990f5c8SAlexander Graf #define TTB_PAGETABLE		(3 << 0)
421d990f5c8SAlexander Graf 
422d990f5c8SAlexander Graf /* TTBCR flags */
423d990f5c8SAlexander Graf #define TTBCR_EAE		(1 << 31)
424d990f5c8SAlexander Graf #define TTBCR_T0SZ(x)		((x) << 0)
425d990f5c8SAlexander Graf #define TTBCR_T1SZ(x)		((x) << 16)
426d990f5c8SAlexander Graf #define TTBCR_USING_TTBR0	(TTBCR_T0SZ(0) | TTBCR_T1SZ(0))
427d990f5c8SAlexander Graf #define TTBCR_IRGN0_NC		(0 << 8)
428d990f5c8SAlexander Graf #define TTBCR_IRGN0_WBWA	(1 << 8)
429d990f5c8SAlexander Graf #define TTBCR_IRGN0_WT		(2 << 8)
430d990f5c8SAlexander Graf #define TTBCR_IRGN0_WBNWA	(3 << 8)
431d990f5c8SAlexander Graf #define TTBCR_IRGN0_MASK	(3 << 8)
432d990f5c8SAlexander Graf #define TTBCR_ORGN0_NC		(0 << 10)
433d990f5c8SAlexander Graf #define TTBCR_ORGN0_WBWA	(1 << 10)
434d990f5c8SAlexander Graf #define TTBCR_ORGN0_WT		(2 << 10)
435d990f5c8SAlexander Graf #define TTBCR_ORGN0_WBNWA	(3 << 10)
436d990f5c8SAlexander Graf #define TTBCR_ORGN0_MASK	(3 << 10)
437d990f5c8SAlexander Graf #define TTBCR_SHARED_NON	(0 << 12)
438d990f5c8SAlexander Graf #define TTBCR_SHARED_OUTER	(2 << 12)
439d990f5c8SAlexander Graf #define TTBCR_SHARED_INNER	(3 << 12)
440d990f5c8SAlexander Graf #define TTBCR_EPD0		(0 << 7)
441d990f5c8SAlexander Graf 
442d990f5c8SAlexander Graf /*
443d990f5c8SAlexander Graf  * Memory types
444d990f5c8SAlexander Graf  */
445d990f5c8SAlexander Graf #define MEMORY_ATTRIBUTES	((0x00 << (0 * 8)) | (0x88 << (1 * 8)) | \
446d990f5c8SAlexander Graf 				 (0xcc << (2 * 8)) | (0xff << (3 * 8)))
447d990f5c8SAlexander Graf 
448d990f5c8SAlexander Graf /* options available for data cache on each page */
449d990f5c8SAlexander Graf enum dcache_option {
45006d43c80SKeerthy 	DCACHE_OFF = TTB_SECT | TTB_SECT_MAIR(0) | TTB_SECT_XN_MASK,
451d990f5c8SAlexander Graf 	DCACHE_WRITETHROUGH = TTB_SECT | TTB_SECT_MAIR(1),
452d990f5c8SAlexander Graf 	DCACHE_WRITEBACK = TTB_SECT | TTB_SECT_MAIR(2),
453d990f5c8SAlexander Graf 	DCACHE_WRITEALLOC = TTB_SECT | TTB_SECT_MAIR(3),
454d990f5c8SAlexander Graf };
455acf15001SLokesh Vutla #elif defined(CONFIG_CPU_V7A)
45697840b5dSBryan Brinsko /* Short-Descriptor Translation Table Level 1 Bits */
45797840b5dSBryan Brinsko #define TTB_SECT_NS_MASK	(1 << 19)
45897840b5dSBryan Brinsko #define TTB_SECT_NG_MASK	(1 << 17)
45997840b5dSBryan Brinsko #define TTB_SECT_S_MASK		(1 << 16)
46097840b5dSBryan Brinsko /* Note: TTB AP bits are set elsewhere */
461d990f5c8SAlexander Graf #define TTB_SECT_AP		(3 << 10)
46297840b5dSBryan Brinsko #define TTB_SECT_TEX(x)		((x & 0x7) << 12)
46397840b5dSBryan Brinsko #define TTB_SECT_DOMAIN(x)	((x & 0xf) << 5)
46497840b5dSBryan Brinsko #define TTB_SECT_XN_MASK	(1 << 4)
46597840b5dSBryan Brinsko #define TTB_SECT_C_MASK		(1 << 3)
46697840b5dSBryan Brinsko #define TTB_SECT_B_MASK		(1 << 2)
46797840b5dSBryan Brinsko #define TTB_SECT			(2 << 0)
46897840b5dSBryan Brinsko 
46997840b5dSBryan Brinsko /* options available for data cache on each page */
47097840b5dSBryan Brinsko enum dcache_option {
4718890c2fbSMarek Vasut 	DCACHE_OFF = TTB_SECT_DOMAIN(0) | TTB_SECT_XN_MASK | TTB_SECT,
47297840b5dSBryan Brinsko 	DCACHE_WRITETHROUGH = DCACHE_OFF | TTB_SECT_C_MASK,
47397840b5dSBryan Brinsko 	DCACHE_WRITEBACK = DCACHE_WRITETHROUGH | TTB_SECT_B_MASK,
47497840b5dSBryan Brinsko 	DCACHE_WRITEALLOC = DCACHE_WRITEBACK | TTB_SECT_TEX(1),
47597840b5dSBryan Brinsko };
47697840b5dSBryan Brinsko #else
477d990f5c8SAlexander Graf #define TTB_SECT_AP		(3 << 10)
4780dde7f53SSimon Glass /* options available for data cache on each page */
4790dde7f53SSimon Glass enum dcache_option {
4800dde7f53SSimon Glass 	DCACHE_OFF = 0x12,
4810dde7f53SSimon Glass 	DCACHE_WRITETHROUGH = 0x1a,
4820dde7f53SSimon Glass 	DCACHE_WRITEBACK = 0x1e,
483ff7e9700SMarek Vasut 	DCACHE_WRITEALLOC = 0x16,
4840dde7f53SSimon Glass };
48597840b5dSBryan Brinsko #endif
4860dde7f53SSimon Glass 
4870dde7f53SSimon Glass /* Size of an MMU section */
4880dde7f53SSimon Glass enum {
489d990f5c8SAlexander Graf #ifdef CONFIG_ARMV7_LPAE
490d990f5c8SAlexander Graf 	MMU_SECTION_SHIFT	= 21, /* 2MB */
491d990f5c8SAlexander Graf #else
492d990f5c8SAlexander Graf 	MMU_SECTION_SHIFT	= 20, /* 1MB */
493d990f5c8SAlexander Graf #endif
4940dde7f53SSimon Glass 	MMU_SECTION_SIZE	= 1 << MMU_SECTION_SHIFT,
4950dde7f53SSimon Glass };
4960dde7f53SSimon Glass 
497acf15001SLokesh Vutla #ifdef CONFIG_CPU_V7A
49897840b5dSBryan Brinsko /* TTBR0 bits */
49997840b5dSBryan Brinsko #define TTBR0_BASE_ADDR_MASK	0xFFFFC000
50097840b5dSBryan Brinsko #define TTBR0_RGN_NC			(0 << 3)
50197840b5dSBryan Brinsko #define TTBR0_RGN_WBWA			(1 << 3)
50297840b5dSBryan Brinsko #define TTBR0_RGN_WT			(2 << 3)
50397840b5dSBryan Brinsko #define TTBR0_RGN_WB			(3 << 3)
50497840b5dSBryan Brinsko /* TTBR0[6] is IRGN[0] and TTBR[0] is IRGN[1] */
50597840b5dSBryan Brinsko #define TTBR0_IRGN_NC			(0 << 0 | 0 << 6)
50697840b5dSBryan Brinsko #define TTBR0_IRGN_WBWA			(0 << 0 | 1 << 6)
50797840b5dSBryan Brinsko #define TTBR0_IRGN_WT			(1 << 0 | 0 << 6)
50897840b5dSBryan Brinsko #define TTBR0_IRGN_WB			(1 << 0 | 1 << 6)
50997840b5dSBryan Brinsko #endif
51097840b5dSBryan Brinsko 
5110dde7f53SSimon Glass /**
5120dde7f53SSimon Glass  * Register an update to the page tables, and flush the TLB
5130dde7f53SSimon Glass  *
5140dde7f53SSimon Glass  * \param start		start address of update in page table
5150dde7f53SSimon Glass  * \param stop		stop address of update in page table
5160dde7f53SSimon Glass  */
5170dde7f53SSimon Glass void mmu_page_table_flush(unsigned long start, unsigned long stop);
5180dde7f53SSimon Glass 
519819833afSPeter Tyser #endif /* __ASSEMBLY__ */
520819833afSPeter Tyser 
521819833afSPeter Tyser #define arch_align_stack(x) (x)
522819833afSPeter Tyser 
523819833afSPeter Tyser #endif /* __KERNEL__ */
524819833afSPeter Tyser 
5250ae76531SDavid Feng #endif /* CONFIG_ARM64 */
5260ae76531SDavid Feng 
527dad17fd5SSiva Durga Prasad Paladugu #ifndef __ASSEMBLY__
528dad17fd5SSiva Durga Prasad Paladugu /**
5297776cc01SPhilipp Tomsich  * save_boot_params() - Save boot parameters before starting reset sequence
5307776cc01SPhilipp Tomsich  *
5317776cc01SPhilipp Tomsich  * If you provide this function it will be called immediately U-Boot starts,
5327776cc01SPhilipp Tomsich  * both for SPL and U-Boot proper.
5337776cc01SPhilipp Tomsich  *
5347776cc01SPhilipp Tomsich  * All registers are unchanged from U-Boot entry. No registers need be
5357776cc01SPhilipp Tomsich  * preserved.
5367776cc01SPhilipp Tomsich  *
5377776cc01SPhilipp Tomsich  * This is not a normal C function. There is no stack. Return by branching to
5387776cc01SPhilipp Tomsich  * save_boot_params_ret.
5397776cc01SPhilipp Tomsich  *
5407776cc01SPhilipp Tomsich  * void save_boot_params(u32 r0, u32 r1, u32 r2, u32 r3);
5417776cc01SPhilipp Tomsich  */
5427776cc01SPhilipp Tomsich 
5437776cc01SPhilipp Tomsich /**
5447776cc01SPhilipp Tomsich  * save_boot_params_ret() - Return from save_boot_params()
5457776cc01SPhilipp Tomsich  *
5467776cc01SPhilipp Tomsich  * If you provide save_boot_params(), then you should jump back to this
5477776cc01SPhilipp Tomsich  * function when done. Try to preserve all registers.
5487776cc01SPhilipp Tomsich  *
5497776cc01SPhilipp Tomsich  * If your implementation of save_boot_params() is in C then it is acceptable
5507776cc01SPhilipp Tomsich  * to simply call save_boot_params_ret() at the end of your function. Since
5517776cc01SPhilipp Tomsich  * there is no link register set up, you cannot just exit the function. U-Boot
5527776cc01SPhilipp Tomsich  * will return to the (initialised) value of lr, and likely crash/hang.
5537776cc01SPhilipp Tomsich  *
5547776cc01SPhilipp Tomsich  * If your implementation of save_boot_params() is in assembler then you
5557776cc01SPhilipp Tomsich  * should use 'b' or 'bx' to return to save_boot_params_ret.
5567776cc01SPhilipp Tomsich  */
5577776cc01SPhilipp Tomsich void save_boot_params_ret(void);
5587776cc01SPhilipp Tomsich 
5597776cc01SPhilipp Tomsich /**
560dad17fd5SSiva Durga Prasad Paladugu  * Change the cache settings for a region.
561dad17fd5SSiva Durga Prasad Paladugu  *
562dad17fd5SSiva Durga Prasad Paladugu  * \param start		start address of memory region to change
563dad17fd5SSiva Durga Prasad Paladugu  * \param size		size of memory region to change
564dad17fd5SSiva Durga Prasad Paladugu  * \param option	dcache option to select
565dad17fd5SSiva Durga Prasad Paladugu  */
566dad17fd5SSiva Durga Prasad Paladugu void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
567dad17fd5SSiva Durga Prasad Paladugu 				     enum dcache_option option);
568dad17fd5SSiva Durga Prasad Paladugu 
56988f965d7SStephen Warren #ifdef CONFIG_SYS_NONCACHED_MEMORY
57088f965d7SStephen Warren void noncached_init(void);
57188f965d7SStephen Warren phys_addr_t noncached_alloc(size_t size, size_t align);
57288f965d7SStephen Warren #endif /* CONFIG_SYS_NONCACHED_MEMORY */
57388f965d7SStephen Warren 
574dad17fd5SSiva Durga Prasad Paladugu #endif /* __ASSEMBLY__ */
575dad17fd5SSiva Durga Prasad Paladugu 
576819833afSPeter Tyser #endif
577