system.h 8.4 KB

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