system.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #ifndef __ASM_ARM_SYSTEM_H
  2. #define __ASM_ARM_SYSTEM_H
  3. #ifdef CONFIG_ARM64
  4. /*
  5. * SCTLR_EL1/SCTLR_EL2/SCTLR_EL3 bits definitions
  6. */
  7. #define CR_M (1 << 0) /* MMU enable */
  8. #define CR_A (1 << 1) /* Alignment abort enable */
  9. #define CR_C (1 << 2) /* Dcache enable */
  10. #define CR_SA (1 << 3) /* Stack Alignment Check Enable */
  11. #define CR_I (1 << 12) /* Icache enable */
  12. #define CR_WXN (1 << 19) /* Write Permision Imply XN */
  13. #define CR_EE (1 << 25) /* Exception (Big) Endian */
  14. #define PGTABLE_SIZE (0x10000)
  15. #ifndef __ASSEMBLY__
  16. #define isb() \
  17. ({asm volatile( \
  18. "isb" : : : "memory"); \
  19. })
  20. #define wfi() \
  21. ({asm volatile( \
  22. "wfi" : : : "memory"); \
  23. })
  24. static inline unsigned int current_el(void)
  25. {
  26. unsigned int el;
  27. asm volatile("mrs %0, CurrentEL" : "=r" (el) : : "cc");
  28. return el >> 2;
  29. }
  30. static inline unsigned int get_sctlr(void)
  31. {
  32. unsigned int el, val;
  33. el = current_el();
  34. if (el == 1)
  35. asm volatile("mrs %0, sctlr_el1" : "=r" (val) : : "cc");
  36. else if (el == 2)
  37. asm volatile("mrs %0, sctlr_el2" : "=r" (val) : : "cc");
  38. else
  39. asm volatile("mrs %0, sctlr_el3" : "=r" (val) : : "cc");
  40. return val;
  41. }
  42. static inline void set_sctlr(unsigned int val)
  43. {
  44. unsigned int el;
  45. el = current_el();
  46. if (el == 1)
  47. asm volatile("msr sctlr_el1, %0" : : "r" (val) : "cc");
  48. else if (el == 2)
  49. asm volatile("msr sctlr_el2, %0" : : "r" (val) : "cc");
  50. else
  51. asm volatile("msr sctlr_el3, %0" : : "r" (val) : "cc");
  52. asm volatile("isb");
  53. }
  54. void __asm_flush_dcache_all(void);
  55. void __asm_invalidate_dcache_all(void);
  56. void __asm_flush_dcache_range(u64 start, u64 end);
  57. void __asm_invalidate_tlb_all(void);
  58. void __asm_invalidate_icache_all(void);
  59. int __asm_flush_l3_cache(void);
  60. void armv8_switch_to_el2(void);
  61. void armv8_switch_to_el1(void);
  62. void gic_init(void);
  63. void gic_send_sgi(unsigned long sgino);
  64. void wait_for_wakeup(void);
  65. void protect_secure_region(void);
  66. void smp_kick_all_cpus(void);
  67. void flush_l3_cache(void);
  68. #endif /* __ASSEMBLY__ */
  69. #else /* CONFIG_ARM64 */
  70. #ifdef __KERNEL__
  71. #define CPU_ARCH_UNKNOWN 0
  72. #define CPU_ARCH_ARMv3 1
  73. #define CPU_ARCH_ARMv4 2
  74. #define CPU_ARCH_ARMv4T 3
  75. #define CPU_ARCH_ARMv5 4
  76. #define CPU_ARCH_ARMv5T 5
  77. #define CPU_ARCH_ARMv5TE 6
  78. #define CPU_ARCH_ARMv5TEJ 7
  79. #define CPU_ARCH_ARMv6 8
  80. #define CPU_ARCH_ARMv7 9
  81. /*
  82. * CR1 bits (CP#15 CR1)
  83. */
  84. #define CR_M (1 << 0) /* MMU enable */
  85. #define CR_A (1 << 1) /* Alignment abort enable */
  86. #define CR_C (1 << 2) /* Dcache enable */
  87. #define CR_W (1 << 3) /* Write buffer enable */
  88. #define CR_P (1 << 4) /* 32-bit exception handler */
  89. #define CR_D (1 << 5) /* 32-bit data address range */
  90. #define CR_L (1 << 6) /* Implementation defined */
  91. #define CR_B (1 << 7) /* Big endian */
  92. #define CR_S (1 << 8) /* System MMU protection */
  93. #define CR_R (1 << 9) /* ROM MMU protection */
  94. #define CR_F (1 << 10) /* Implementation defined */
  95. #define CR_Z (1 << 11) /* Implementation defined */
  96. #define CR_I (1 << 12) /* Icache enable */
  97. #define CR_V (1 << 13) /* Vectors relocated to 0xffff0000 */
  98. #define CR_RR (1 << 14) /* Round Robin cache replacement */
  99. #define CR_L4 (1 << 15) /* LDR pc can set T bit */
  100. #define CR_DT (1 << 16)
  101. #define CR_IT (1 << 18)
  102. #define CR_ST (1 << 19)
  103. #define CR_FI (1 << 21) /* Fast interrupt (lower latency mode) */
  104. #define CR_U (1 << 22) /* Unaligned access operation */
  105. #define CR_XP (1 << 23) /* Extended page tables */
  106. #define CR_VE (1 << 24) /* Vectored interrupts */
  107. #define CR_EE (1 << 25) /* Exception (Big) Endian */
  108. #define CR_TRE (1 << 28) /* TEX remap enable */
  109. #define CR_AFE (1 << 29) /* Access flag enable */
  110. #define CR_TE (1 << 30) /* Thumb exception enable */
  111. #define PGTABLE_SIZE (4096 * 4)
  112. /*
  113. * This is used to ensure the compiler did actually allocate the register we
  114. * asked it for some inline assembly sequences. Apparently we can't trust
  115. * the compiler from one version to another so a bit of paranoia won't hurt.
  116. * This string is meant to be concatenated with the inline asm string and
  117. * will cause compilation to stop on mismatch.
  118. * (for details, see gcc PR 15089)
  119. */
  120. #define __asmeq(x, y) ".ifnc " x "," y " ; .err ; .endif\n\t"
  121. #ifndef __ASSEMBLY__
  122. /**
  123. * save_boot_params() - Save boot parameters before starting reset sequence
  124. *
  125. * If you provide this function it will be called immediately U-Boot starts,
  126. * both for SPL and U-Boot proper.
  127. *
  128. * All registers are unchanged from U-Boot entry. No registers need be
  129. * preserved.
  130. *
  131. * This is not a normal C function. There is no stack. Return by branching to
  132. * save_boot_params_ret.
  133. *
  134. * void save_boot_params(u32 r0, u32 r1, u32 r2, u32 r3);
  135. */
  136. #define isb() __asm__ __volatile__ ("" : : : "memory")
  137. #define nop() __asm__ __volatile__("mov\tr0,r0\t@ nop\n\t");
  138. #ifdef __ARM_ARCH_7A__
  139. #define wfi() __asm__ __volatile__ ("wfi" : : : "memory")
  140. #else
  141. #define wfi()
  142. #endif
  143. static inline unsigned int get_cr(void)
  144. {
  145. unsigned int val;
  146. asm("mrc p15, 0, %0, c1, c0, 0 @ get CR" : "=r" (val) : : "cc");
  147. return val;
  148. }
  149. static inline void set_cr(unsigned int val)
  150. {
  151. asm volatile("mcr p15, 0, %0, c1, c0, 0 @ set CR"
  152. : : "r" (val) : "cc");
  153. isb();
  154. }
  155. static inline unsigned int get_dacr(void)
  156. {
  157. unsigned int val;
  158. asm("mrc p15, 0, %0, c3, c0, 0 @ get DACR" : "=r" (val) : : "cc");
  159. return val;
  160. }
  161. static inline void set_dacr(unsigned int val)
  162. {
  163. asm volatile("mcr p15, 0, %0, c3, c0, 0 @ set DACR"
  164. : : "r" (val) : "cc");
  165. isb();
  166. }
  167. #ifdef CONFIG_ARMV7
  168. /* Short-Descriptor Translation Table Level 1 Bits */
  169. #define TTB_SECT_NS_MASK (1 << 19)
  170. #define TTB_SECT_NG_MASK (1 << 17)
  171. #define TTB_SECT_S_MASK (1 << 16)
  172. /* Note: TTB AP bits are set elsewhere */
  173. #define TTB_SECT_TEX(x) ((x & 0x7) << 12)
  174. #define TTB_SECT_DOMAIN(x) ((x & 0xf) << 5)
  175. #define TTB_SECT_XN_MASK (1 << 4)
  176. #define TTB_SECT_C_MASK (1 << 3)
  177. #define TTB_SECT_B_MASK (1 << 2)
  178. #define TTB_SECT (2 << 0)
  179. /* options available for data cache on each page */
  180. enum dcache_option {
  181. DCACHE_OFF = TTB_SECT_S_MASK | TTB_SECT_DOMAIN(0) |
  182. TTB_SECT_XN_MASK | TTB_SECT,
  183. DCACHE_WRITETHROUGH = DCACHE_OFF | TTB_SECT_C_MASK,
  184. DCACHE_WRITEBACK = DCACHE_WRITETHROUGH | TTB_SECT_B_MASK,
  185. DCACHE_WRITEALLOC = DCACHE_WRITEBACK | TTB_SECT_TEX(1),
  186. };
  187. #else
  188. /* options available for data cache on each page */
  189. enum dcache_option {
  190. DCACHE_OFF = 0x12,
  191. DCACHE_WRITETHROUGH = 0x1a,
  192. DCACHE_WRITEBACK = 0x1e,
  193. DCACHE_WRITEALLOC = 0x16,
  194. };
  195. #endif
  196. /* Size of an MMU section */
  197. enum {
  198. MMU_SECTION_SHIFT = 20,
  199. MMU_SECTION_SIZE = 1 << MMU_SECTION_SHIFT,
  200. };
  201. #ifdef CONFIG_ARMV7
  202. /* TTBR0 bits */
  203. #define TTBR0_BASE_ADDR_MASK 0xFFFFC000
  204. #define TTBR0_RGN_NC (0 << 3)
  205. #define TTBR0_RGN_WBWA (1 << 3)
  206. #define TTBR0_RGN_WT (2 << 3)
  207. #define TTBR0_RGN_WB (3 << 3)
  208. /* TTBR0[6] is IRGN[0] and TTBR[0] is IRGN[1] */
  209. #define TTBR0_IRGN_NC (0 << 0 | 0 << 6)
  210. #define TTBR0_IRGN_WBWA (0 << 0 | 1 << 6)
  211. #define TTBR0_IRGN_WT (1 << 0 | 0 << 6)
  212. #define TTBR0_IRGN_WB (1 << 0 | 1 << 6)
  213. #endif
  214. /**
  215. * Change the cache settings for a region.
  216. *
  217. * \param start start address of memory region to change
  218. * \param size size of memory region to change
  219. * \param option dcache option to select
  220. */
  221. void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
  222. enum dcache_option option);
  223. /**
  224. * Register an update to the page tables, and flush the TLB
  225. *
  226. * \param start start address of update in page table
  227. * \param stop stop address of update in page table
  228. */
  229. void mmu_page_table_flush(unsigned long start, unsigned long stop);
  230. #ifdef CONFIG_SYS_NONCACHED_MEMORY
  231. void noncached_init(void);
  232. phys_addr_t noncached_alloc(size_t size, size_t align);
  233. #endif /* CONFIG_SYS_NONCACHED_MEMORY */
  234. #endif /* __ASSEMBLY__ */
  235. #define arch_align_stack(x) (x)
  236. #endif /* __KERNEL__ */
  237. #endif /* CONFIG_ARM64 */
  238. #endif