system.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. #ifndef __ASM_ARM_SYSTEM_H
  2. #define __ASM_ARM_SYSTEM_H
  3. #include <common.h>
  4. #include <linux/compiler.h>
  5. #include <asm/barriers.h>
  6. #ifdef CONFIG_ARM64
  7. /*
  8. * SCTLR_EL1/SCTLR_EL2/SCTLR_EL3 bits definitions
  9. */
  10. #define CR_M (1 << 0) /* MMU enable */
  11. #define CR_A (1 << 1) /* Alignment abort enable */
  12. #define CR_C (1 << 2) /* Dcache enable */
  13. #define CR_SA (1 << 3) /* Stack Alignment Check Enable */
  14. #define CR_I (1 << 12) /* Icache enable */
  15. #define CR_WXN (1 << 19) /* Write Permision Imply XN */
  16. #define CR_EE (1 << 25) /* Exception (Big) Endian */
  17. #ifndef __ASSEMBLY__
  18. u64 get_page_table_size(void);
  19. #define PGTABLE_SIZE get_page_table_size()
  20. /* 2MB granularity */
  21. #define MMU_SECTION_SHIFT 21
  22. #define MMU_SECTION_SIZE (1 << MMU_SECTION_SHIFT)
  23. /* These constants need to be synced to the MT_ types in asm/armv8/mmu.h */
  24. enum dcache_option {
  25. DCACHE_OFF = 0 << 2,
  26. DCACHE_WRITETHROUGH = 3 << 2,
  27. DCACHE_WRITEBACK = 4 << 2,
  28. DCACHE_WRITEALLOC = 4 << 2,
  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 __asm_switch_ttbr(u64 new_ttbr);
  78. void armv8_switch_to_el2(void);
  79. void armv8_switch_to_el1(void);
  80. void gic_init(void);
  81. void gic_send_sgi(unsigned long sgino);
  82. void wait_for_wakeup(void);
  83. void protect_secure_region(void);
  84. void smp_kick_all_cpus(void);
  85. void flush_l3_cache(void);
  86. /*
  87. *Issue a secure monitor call in accordance with ARM "SMC Calling convention",
  88. * DEN0028A
  89. *
  90. * @args: input and output arguments
  91. *
  92. */
  93. void smc_call(struct pt_regs *args);
  94. void __noreturn psci_system_reset(void);
  95. #endif /* __ASSEMBLY__ */
  96. #else /* CONFIG_ARM64 */
  97. #ifdef __KERNEL__
  98. #define CPU_ARCH_UNKNOWN 0
  99. #define CPU_ARCH_ARMv3 1
  100. #define CPU_ARCH_ARMv4 2
  101. #define CPU_ARCH_ARMv4T 3
  102. #define CPU_ARCH_ARMv5 4
  103. #define CPU_ARCH_ARMv5T 5
  104. #define CPU_ARCH_ARMv5TE 6
  105. #define CPU_ARCH_ARMv5TEJ 7
  106. #define CPU_ARCH_ARMv6 8
  107. #define CPU_ARCH_ARMv7 9
  108. /*
  109. * CR1 bits (CP#15 CR1)
  110. */
  111. #define CR_M (1 << 0) /* MMU enable */
  112. #define CR_A (1 << 1) /* Alignment abort enable */
  113. #define CR_C (1 << 2) /* Dcache enable */
  114. #define CR_W (1 << 3) /* Write buffer enable */
  115. #define CR_P (1 << 4) /* 32-bit exception handler */
  116. #define CR_D (1 << 5) /* 32-bit data address range */
  117. #define CR_L (1 << 6) /* Implementation defined */
  118. #define CR_B (1 << 7) /* Big endian */
  119. #define CR_S (1 << 8) /* System MMU protection */
  120. #define CR_R (1 << 9) /* ROM MMU protection */
  121. #define CR_F (1 << 10) /* Implementation defined */
  122. #define CR_Z (1 << 11) /* Implementation defined */
  123. #define CR_I (1 << 12) /* Icache enable */
  124. #define CR_V (1 << 13) /* Vectors relocated to 0xffff0000 */
  125. #define CR_RR (1 << 14) /* Round Robin cache replacement */
  126. #define CR_L4 (1 << 15) /* LDR pc can set T bit */
  127. #define CR_DT (1 << 16)
  128. #define CR_IT (1 << 18)
  129. #define CR_ST (1 << 19)
  130. #define CR_FI (1 << 21) /* Fast interrupt (lower latency mode) */
  131. #define CR_U (1 << 22) /* Unaligned access operation */
  132. #define CR_XP (1 << 23) /* Extended page tables */
  133. #define CR_VE (1 << 24) /* Vectored interrupts */
  134. #define CR_EE (1 << 25) /* Exception (Big) Endian */
  135. #define CR_TRE (1 << 28) /* TEX remap enable */
  136. #define CR_AFE (1 << 29) /* Access flag enable */
  137. #define CR_TE (1 << 30) /* Thumb exception enable */
  138. #if defined(CONFIG_ARMV7_LPAE) && !defined(PGTABLE_SIZE)
  139. #define PGTABLE_SIZE (4096 * 5)
  140. #elif !defined(PGTABLE_SIZE)
  141. #define PGTABLE_SIZE (4096 * 4)
  142. #endif
  143. /*
  144. * This is used to ensure the compiler did actually allocate the register we
  145. * asked it for some inline assembly sequences. Apparently we can't trust
  146. * the compiler from one version to another so a bit of paranoia won't hurt.
  147. * This string is meant to be concatenated with the inline asm string and
  148. * will cause compilation to stop on mismatch.
  149. * (for details, see gcc PR 15089)
  150. */
  151. #define __asmeq(x, y) ".ifnc " x "," y " ; .err ; .endif\n\t"
  152. #ifndef __ASSEMBLY__
  153. /**
  154. * save_boot_params() - Save boot parameters before starting reset sequence
  155. *
  156. * If you provide this function it will be called immediately U-Boot starts,
  157. * both for SPL and U-Boot proper.
  158. *
  159. * All registers are unchanged from U-Boot entry. No registers need be
  160. * preserved.
  161. *
  162. * This is not a normal C function. There is no stack. Return by branching to
  163. * save_boot_params_ret.
  164. *
  165. * void save_boot_params(u32 r0, u32 r1, u32 r2, u32 r3);
  166. */
  167. /**
  168. * save_boot_params_ret() - Return from save_boot_params()
  169. *
  170. * If you provide save_boot_params(), then you should jump back to this
  171. * function when done. Try to preserve all registers.
  172. *
  173. * If your implementation of save_boot_params() is in C then it is acceptable
  174. * to simply call save_boot_params_ret() at the end of your function. Since
  175. * there is no link register set up, you cannot just exit the function. U-Boot
  176. * will return to the (initialised) value of lr, and likely crash/hang.
  177. *
  178. * If your implementation of save_boot_params() is in assembler then you
  179. * should use 'b' or 'bx' to return to save_boot_params_ret.
  180. */
  181. void save_boot_params_ret(void);
  182. #ifdef CONFIG_ARMV7_LPAE
  183. void switch_to_hypervisor_ret(void);
  184. #endif
  185. #define nop() __asm__ __volatile__("mov\tr0,r0\t@ nop\n\t");
  186. #ifdef __ARM_ARCH_7A__
  187. #define wfi() __asm__ __volatile__ ("wfi" : : : "memory")
  188. #else
  189. #define wfi()
  190. #endif
  191. static inline unsigned long get_cpsr(void)
  192. {
  193. unsigned long cpsr;
  194. asm volatile("mrs %0, cpsr" : "=r"(cpsr): );
  195. return cpsr;
  196. }
  197. static inline int is_hyp(void)
  198. {
  199. #ifdef CONFIG_ARMV7_LPAE
  200. /* HYP mode requires LPAE ... */
  201. return ((get_cpsr() & 0x1f) == 0x1a);
  202. #else
  203. /* ... so without LPAE support we can optimize all hyp code away */
  204. return 0;
  205. #endif
  206. }
  207. static inline unsigned int get_cr(void)
  208. {
  209. unsigned int val;
  210. if (is_hyp())
  211. asm volatile("mrc p15, 4, %0, c1, c0, 0 @ get CR" : "=r" (val)
  212. :
  213. : "cc");
  214. else
  215. asm volatile("mrc p15, 0, %0, c1, c0, 0 @ get CR" : "=r" (val)
  216. :
  217. : "cc");
  218. return val;
  219. }
  220. static inline void set_cr(unsigned int val)
  221. {
  222. if (is_hyp())
  223. asm volatile("mcr p15, 4, %0, c1, c0, 0 @ set CR" :
  224. : "r" (val)
  225. : "cc");
  226. else
  227. asm volatile("mcr p15, 0, %0, c1, c0, 0 @ set CR" :
  228. : "r" (val)
  229. : "cc");
  230. isb();
  231. }
  232. static inline unsigned int get_dacr(void)
  233. {
  234. unsigned int val;
  235. asm("mrc p15, 0, %0, c3, c0, 0 @ get DACR" : "=r" (val) : : "cc");
  236. return val;
  237. }
  238. static inline void set_dacr(unsigned int val)
  239. {
  240. asm volatile("mcr p15, 0, %0, c3, c0, 0 @ set DACR"
  241. : : "r" (val) : "cc");
  242. isb();
  243. }
  244. #ifdef CONFIG_ARMV7_LPAE
  245. /* Long-Descriptor Translation Table Level 1/2 Bits */
  246. #define TTB_SECT_XN_MASK (1ULL << 54)
  247. #define TTB_SECT_NG_MASK (1 << 11)
  248. #define TTB_SECT_AF (1 << 10)
  249. #define TTB_SECT_SH_MASK (3 << 8)
  250. #define TTB_SECT_NS_MASK (1 << 5)
  251. #define TTB_SECT_AP (1 << 6)
  252. /* Note: TTB AP bits are set elsewhere */
  253. #define TTB_SECT_MAIR(x) ((x & 0x7) << 2) /* Index into MAIR */
  254. #define TTB_SECT (1 << 0)
  255. #define TTB_PAGETABLE (3 << 0)
  256. /* TTBCR flags */
  257. #define TTBCR_EAE (1 << 31)
  258. #define TTBCR_T0SZ(x) ((x) << 0)
  259. #define TTBCR_T1SZ(x) ((x) << 16)
  260. #define TTBCR_USING_TTBR0 (TTBCR_T0SZ(0) | TTBCR_T1SZ(0))
  261. #define TTBCR_IRGN0_NC (0 << 8)
  262. #define TTBCR_IRGN0_WBWA (1 << 8)
  263. #define TTBCR_IRGN0_WT (2 << 8)
  264. #define TTBCR_IRGN0_WBNWA (3 << 8)
  265. #define TTBCR_IRGN0_MASK (3 << 8)
  266. #define TTBCR_ORGN0_NC (0 << 10)
  267. #define TTBCR_ORGN0_WBWA (1 << 10)
  268. #define TTBCR_ORGN0_WT (2 << 10)
  269. #define TTBCR_ORGN0_WBNWA (3 << 10)
  270. #define TTBCR_ORGN0_MASK (3 << 10)
  271. #define TTBCR_SHARED_NON (0 << 12)
  272. #define TTBCR_SHARED_OUTER (2 << 12)
  273. #define TTBCR_SHARED_INNER (3 << 12)
  274. #define TTBCR_EPD0 (0 << 7)
  275. /*
  276. * Memory types
  277. */
  278. #define MEMORY_ATTRIBUTES ((0x00 << (0 * 8)) | (0x88 << (1 * 8)) | \
  279. (0xcc << (2 * 8)) | (0xff << (3 * 8)))
  280. /* options available for data cache on each page */
  281. enum dcache_option {
  282. DCACHE_OFF = TTB_SECT | TTB_SECT_MAIR(0),
  283. DCACHE_WRITETHROUGH = TTB_SECT | TTB_SECT_MAIR(1),
  284. DCACHE_WRITEBACK = TTB_SECT | TTB_SECT_MAIR(2),
  285. DCACHE_WRITEALLOC = TTB_SECT | TTB_SECT_MAIR(3),
  286. };
  287. #elif defined(CONFIG_CPU_V7)
  288. /* Short-Descriptor Translation Table Level 1 Bits */
  289. #define TTB_SECT_NS_MASK (1 << 19)
  290. #define TTB_SECT_NG_MASK (1 << 17)
  291. #define TTB_SECT_S_MASK (1 << 16)
  292. /* Note: TTB AP bits are set elsewhere */
  293. #define TTB_SECT_AP (3 << 10)
  294. #define TTB_SECT_TEX(x) ((x & 0x7) << 12)
  295. #define TTB_SECT_DOMAIN(x) ((x & 0xf) << 5)
  296. #define TTB_SECT_XN_MASK (1 << 4)
  297. #define TTB_SECT_C_MASK (1 << 3)
  298. #define TTB_SECT_B_MASK (1 << 2)
  299. #define TTB_SECT (2 << 0)
  300. /* options available for data cache on each page */
  301. enum dcache_option {
  302. DCACHE_OFF = TTB_SECT_DOMAIN(0) | TTB_SECT_XN_MASK | TTB_SECT,
  303. DCACHE_WRITETHROUGH = DCACHE_OFF | TTB_SECT_C_MASK,
  304. DCACHE_WRITEBACK = DCACHE_WRITETHROUGH | TTB_SECT_B_MASK,
  305. DCACHE_WRITEALLOC = DCACHE_WRITEBACK | TTB_SECT_TEX(1),
  306. };
  307. #else
  308. #define TTB_SECT_AP (3 << 10)
  309. /* options available for data cache on each page */
  310. enum dcache_option {
  311. DCACHE_OFF = 0x12,
  312. DCACHE_WRITETHROUGH = 0x1a,
  313. DCACHE_WRITEBACK = 0x1e,
  314. DCACHE_WRITEALLOC = 0x16,
  315. };
  316. #endif
  317. /* Size of an MMU section */
  318. enum {
  319. #ifdef CONFIG_ARMV7_LPAE
  320. MMU_SECTION_SHIFT = 21, /* 2MB */
  321. #else
  322. MMU_SECTION_SHIFT = 20, /* 1MB */
  323. #endif
  324. MMU_SECTION_SIZE = 1 << MMU_SECTION_SHIFT,
  325. };
  326. #ifdef CONFIG_CPU_V7
  327. /* TTBR0 bits */
  328. #define TTBR0_BASE_ADDR_MASK 0xFFFFC000
  329. #define TTBR0_RGN_NC (0 << 3)
  330. #define TTBR0_RGN_WBWA (1 << 3)
  331. #define TTBR0_RGN_WT (2 << 3)
  332. #define TTBR0_RGN_WB (3 << 3)
  333. /* TTBR0[6] is IRGN[0] and TTBR[0] is IRGN[1] */
  334. #define TTBR0_IRGN_NC (0 << 0 | 0 << 6)
  335. #define TTBR0_IRGN_WBWA (0 << 0 | 1 << 6)
  336. #define TTBR0_IRGN_WT (1 << 0 | 0 << 6)
  337. #define TTBR0_IRGN_WB (1 << 0 | 1 << 6)
  338. #endif
  339. /**
  340. * Register an update to the page tables, and flush the TLB
  341. *
  342. * \param start start address of update in page table
  343. * \param stop stop address of update in page table
  344. */
  345. void mmu_page_table_flush(unsigned long start, unsigned long stop);
  346. #endif /* __ASSEMBLY__ */
  347. #define arch_align_stack(x) (x)
  348. #endif /* __KERNEL__ */
  349. #endif /* CONFIG_ARM64 */
  350. #ifndef __ASSEMBLY__
  351. /**
  352. * Change the cache settings for a region.
  353. *
  354. * \param start start address of memory region to change
  355. * \param size size of memory region to change
  356. * \param option dcache option to select
  357. */
  358. void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
  359. enum dcache_option option);
  360. #ifdef CONFIG_SYS_NONCACHED_MEMORY
  361. void noncached_init(void);
  362. phys_addr_t noncached_alloc(size_t size, size_t align);
  363. #endif /* CONFIG_SYS_NONCACHED_MEMORY */
  364. #endif /* __ASSEMBLY__ */
  365. #endif