system.h 12 KB

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