asm_inline_gcc.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* ARM AArch32 GCC specific public inline assembler functions and macros */
  2. /*
  3. * Copyright (c) 2015, Wind River Systems, Inc.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. */
  7. /* Either public functions or macros or invoked by public functions */
  8. #ifndef ZEPHYR_INCLUDE_ARCH_ARM_AARCH32_ASM_INLINE_GCC_H_
  9. #define ZEPHYR_INCLUDE_ARCH_ARM_AARCH32_ASM_INLINE_GCC_H_
  10. /*
  11. * The file must not be included directly
  12. * Include arch/cpu.h instead
  13. */
  14. #ifndef _ASMLANGUAGE
  15. #include <zephyr/types.h>
  16. #include <arch/arm/aarch32/exc.h>
  17. #include <irq.h>
  18. #if defined(CONFIG_CPU_CORTEX_R)
  19. #include <arch/arm/aarch32/cortex_a_r/cpu.h>
  20. #endif
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /* On ARMv7-M and ARMv8-M Mainline CPUs, this function prevents regular
  25. * exceptions (i.e. with interrupt priority lower than or equal to
  26. * _EXC_IRQ_DEFAULT_PRIO) from interrupting the CPU. NMI, Faults, SVC,
  27. * and Zero Latency IRQs (if supported) may still interrupt the CPU.
  28. *
  29. * On ARMv6-M and ARMv8-M Baseline CPUs, this function reads the value of
  30. * PRIMASK which shows if interrupts are enabled, then disables all interrupts
  31. * except NMI.
  32. */
  33. #ifndef CONFIG_DISABLE_IRQ_STAT
  34. static ALWAYS_INLINE unsigned int arch_irq_lock(void)
  35. {
  36. unsigned int key;
  37. #if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE)
  38. __asm__ volatile("mrs %0, PRIMASK;"
  39. "cpsid i"
  40. : "=r" (key)
  41. :
  42. : "memory");
  43. #elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE)
  44. unsigned int tmp;
  45. __asm__ volatile(
  46. "mov %1, %2;"
  47. "mrs %0, BASEPRI;"
  48. "msr BASEPRI_MAX, %1;"
  49. "isb;"
  50. : "=r"(key), "=r"(tmp)
  51. : "i"(_EXC_IRQ_DEFAULT_PRIO)
  52. : "memory");
  53. #elif defined(CONFIG_ARMV7_R)
  54. __asm__ volatile(
  55. "mrs %0, cpsr;"
  56. "and %0, #" TOSTR(I_BIT) ";"
  57. "cpsid i;"
  58. : "=r" (key)
  59. :
  60. : "memory", "cc");
  61. #else
  62. #error Unknown ARM architecture
  63. #endif /* CONFIG_ARMV6_M_ARMV8_M_BASELINE */
  64. return key;
  65. }
  66. /* On Cortex-M0/M0+, this enables all interrupts if they were not
  67. * previously disabled.
  68. */
  69. static ALWAYS_INLINE void arch_irq_unlock(unsigned int key)
  70. {
  71. #if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE)
  72. if (key != 0U) {
  73. return;
  74. }
  75. __asm__ volatile(
  76. "cpsie i;"
  77. "isb"
  78. : : : "memory");
  79. #elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE)
  80. __asm__ volatile(
  81. "msr BASEPRI, %0;"
  82. "isb;"
  83. : : "r"(key) : "memory");
  84. #elif defined(CONFIG_ARMV7_R)
  85. if (key != 0U) {
  86. return;
  87. }
  88. __asm__ volatile(
  89. "cpsie i;"
  90. : : : "memory", "cc");
  91. #else
  92. #error Unknown ARM architecture
  93. #endif /* CONFIG_ARMV6_M_ARMV8_M_BASELINE */
  94. }
  95. #endif
  96. static ALWAYS_INLINE bool arch_irq_unlocked(unsigned int key)
  97. {
  98. /* This convention works for both PRIMASK and BASEPRI */
  99. return key == 0U;
  100. }
  101. #ifdef __cplusplus
  102. }
  103. #endif
  104. #endif /* _ASMLANGUAGE */
  105. #endif /* ZEPHYR_INCLUDE_ARCH_ARM_AARCH32_ASM_INLINE_GCC_H_ */