system.h 8.7 KB

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