LCOV - code coverage report
Current view: top level - arch/arm/core/aarch32 - irq_manage.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 10 23 43.5 %
Date: 2022-08-18 11:36:24 Functions: 3 6 50.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 1 4 25.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (c) 2013-2014 Wind River Systems, Inc.
       3                 :            :  *
       4                 :            :  * SPDX-License-Identifier: Apache-2.0
       5                 :            :  */
       6                 :            : 
       7                 :            : /**
       8                 :            :  * @file
       9                 :            :  * @brief ARM Cortex-A, Cortex-M and Cortex-R interrupt management
      10                 :            :  *
      11                 :            :  *
      12                 :            :  * Interrupt management: enabling/disabling and dynamic ISR
      13                 :            :  * connecting/replacing.  SW_ISR_TABLE_DYNAMIC has to be enabled for
      14                 :            :  * connecting ISRs at runtime.
      15                 :            :  */
      16                 :            : 
      17                 :            : #include <kernel.h>
      18                 :            : #include <arch/cpu.h>
      19                 :            : #if defined(CONFIG_CPU_CORTEX_M)
      20                 :            : #include <arch/arm/aarch32/cortex_m/cmsis.h>
      21                 :            : #elif defined(CONFIG_CPU_AARCH32_CORTEX_A) \
      22                 :            :         || defined(CONFIG_CPU_AARCH32_CORTEX_R)
      23                 :            : #include <drivers/interrupt_controller/gic.h>
      24                 :            : #endif
      25                 :            : #include <sys/__assert.h>
      26                 :            : #include <toolchain.h>
      27                 :            : #include <linker/sections.h>
      28                 :            : #include <sw_isr_table.h>
      29                 :            : #include <irq.h>
      30                 :            : #include <tracing/tracing.h>
      31                 :            : #include <pm/pm.h>
      32                 :            : 
      33                 :            : extern void z_arm_reserved(void);
      34                 :            : 
      35                 :            : #if defined(CONFIG_CPU_CORTEX_M)
      36                 :            : #define NUM_IRQS_PER_REG 32
      37                 :            : #define REG_FROM_IRQ(irq) (irq / NUM_IRQS_PER_REG)
      38                 :            : #define BIT_FROM_IRQ(irq) (irq % NUM_IRQS_PER_REG)
      39                 :            : 
      40                 :          4 : void arch_irq_enable(unsigned int irq)
      41                 :            : {
      42                 :          4 :         NVIC_EnableIRQ((IRQn_Type)irq);
      43                 :          4 : }
      44                 :            : 
      45                 :          0 : void arch_irq_disable(unsigned int irq)
      46                 :            : {
      47                 :          0 :         NVIC_DisableIRQ((IRQn_Type)irq);
      48                 :          0 : }
      49                 :            : 
      50                 :          1 : int arch_irq_is_enabled(unsigned int irq)
      51                 :            : {
      52                 :          1 :         return NVIC->ISER[REG_FROM_IRQ(irq)] & BIT(BIT_FROM_IRQ(irq));
      53                 :            : }
      54                 :            : 
      55                 :            : /**
      56                 :            :  * @internal
      57                 :            :  *
      58                 :            :  * @brief Set an interrupt's priority
      59                 :            :  *
      60                 :            :  * The priority is verified if ASSERT_ON is enabled. The maximum number
      61                 :            :  * of priority levels is a little complex, as there are some hardware
      62                 :            :  * priority levels which are reserved.
      63                 :            :  */
      64                 :          4 : void z_arm_irq_priority_set(unsigned int irq, unsigned int prio, uint32_t flags)
      65                 :            : {
      66                 :            :         /* The kernel may reserve some of the highest priority levels.
      67                 :            :          * So we offset the requested priority level with the number
      68                 :            :          * of priority levels reserved by the kernel.
      69                 :            :          */
      70                 :            : 
      71                 :            :         /* If we have zero latency interrupts, those interrupts will
      72                 :            :          * run at a priority level which is not masked by irq_lock().
      73                 :            :          * Our policy is to express priority levels with special properties
      74                 :            :          * via flags
      75                 :            :          */
      76                 :            :         if (IS_ENABLED(CONFIG_ZERO_LATENCY_IRQS) && (flags & IRQ_ZERO_LATENCY)) {
      77                 :            :                 prio = _EXC_ZERO_LATENCY_IRQS_PRIO;
      78                 :            :         } else {
      79                 :          4 :                 prio += _IRQ_PRIO_OFFSET;
      80                 :            :         }
      81                 :            : 
      82                 :            :         /* The last priority level is also used by PendSV exception, but
      83                 :            :          * allow other interrupts to use the same level, even if it ends up
      84                 :            :          * affecting performance (can still be useful on systems with a
      85                 :            :          * reduced set of priorities, like Cortex-M0/M0+).
      86                 :            :          */
      87         [ -  + ]:          4 :         __ASSERT(prio <= (BIT(NUM_IRQ_PRIO_BITS) - 1),
      88                 :            :                  "invalid priority %d for %d irq! values must be less than %lu\n",
      89                 :            :                  prio - _IRQ_PRIO_OFFSET, irq,
      90                 :            :                  BIT(NUM_IRQ_PRIO_BITS) - (_IRQ_PRIO_OFFSET));
      91                 :          4 :         NVIC_SetPriority((IRQn_Type)irq, prio);
      92                 :          4 : }
      93                 :            : 
      94                 :            : #elif defined(CONFIG_CPU_AARCH32_CORTEX_A) \
      95                 :            :         || defined(CONFIG_CPU_AARCH32_CORTEX_R)
      96                 :            : /*
      97                 :            :  * For Cortex-A and Cortex-R cores, the default interrupt controller is the ARM
      98                 :            :  * Generic Interrupt Controller (GIC) and therefore the architecture interrupt
      99                 :            :  * control functions are mapped to the GIC driver interface.
     100                 :            :  *
     101                 :            :  * When a custom interrupt controller is used (i.e.
     102                 :            :  * CONFIG_ARM_CUSTOM_INTERRUPT_CONTROLLER is enabled), the architecture
     103                 :            :  * interrupt control functions are mapped to the SoC layer in
     104                 :            :  * `include/arch/arm/aarch32/irq.h`.
     105                 :            :  */
     106                 :            : 
     107                 :            : #if !defined(CONFIG_ARM_CUSTOM_INTERRUPT_CONTROLLER)
     108                 :            : void arch_irq_enable(unsigned int irq)
     109                 :            : {
     110                 :            :         arm_gic_irq_enable(irq);
     111                 :            : }
     112                 :            : 
     113                 :            : void arch_irq_disable(unsigned int irq)
     114                 :            : {
     115                 :            :         arm_gic_irq_disable(irq);
     116                 :            : }
     117                 :            : 
     118                 :            : int arch_irq_is_enabled(unsigned int irq)
     119                 :            : {
     120                 :            :         return arm_gic_irq_is_enabled(irq);
     121                 :            : }
     122                 :            : 
     123                 :            : /**
     124                 :            :  * @internal
     125                 :            :  *
     126                 :            :  * @brief Set an interrupt's priority
     127                 :            :  *
     128                 :            :  * The priority is verified if ASSERT_ON is enabled. The maximum number
     129                 :            :  * of priority levels is a little complex, as there are some hardware
     130                 :            :  * priority levels which are reserved: three for various types of exceptions,
     131                 :            :  * and possibly one additional to support zero latency interrupts.
     132                 :            :  */
     133                 :            : void z_arm_irq_priority_set(unsigned int irq, unsigned int prio, uint32_t flags)
     134                 :            : {
     135                 :            :         arm_gic_irq_set_priority(irq, prio, flags);
     136                 :            : }
     137                 :            : #endif /* !CONFIG_ARM_CUSTOM_INTERRUPT_CONTROLLER */
     138                 :            : 
     139                 :            : #endif /* CONFIG_CPU_CORTEX_M */
     140                 :            : 
     141                 :            : void z_arm_fatal_error(unsigned int reason, const z_arch_esf_t *esf);
     142                 :            : 
     143                 :            : /**
     144                 :            :  *
     145                 :            :  * @brief Spurious interrupt handler
     146                 :            :  *
     147                 :            :  * Installed in all _sw_isr_table slots at boot time. Throws an error if
     148                 :            :  * called.
     149                 :            :  *
     150                 :            :  */
     151                 :          0 : void z_irq_spurious(const void *unused)
     152                 :            : {
     153                 :            :         ARG_UNUSED(unused);
     154                 :            : 
     155                 :          0 :         z_arm_fatal_error(K_ERR_SPURIOUS_IRQ, NULL);
     156                 :          0 : }
     157                 :            : 
     158                 :            : #ifdef CONFIG_PM
     159                 :          0 : void _arch_isr_direct_pm(void)
     160                 :            : {
     161                 :            : #if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE) \
     162                 :            :         || defined(CONFIG_ARMV7_R) \
     163                 :            :         || defined(CONFIG_AARCH32_ARMV8_R) \
     164                 :            :         || defined(CONFIG_ARMV7_A)
     165                 :            :         unsigned int key;
     166                 :            : 
     167                 :            :         /* irq_lock() does what we want for this CPU */
     168                 :            :         key = irq_lock();
     169                 :            : #elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE)
     170                 :            :         /* Lock all interrupts. irq_lock() will on this CPU only disable those
     171                 :            :          * lower than BASEPRI, which is not what we want. See comments in
     172                 :            :          * arch/arm/core/aarch32/isr_wrapper.S
     173                 :            :          */
     174                 :          0 :         __asm__ volatile("cpsid i" : : : "memory");
     175                 :            : #else
     176                 :            : #error Unknown ARM architecture
     177                 :            : #endif /* CONFIG_ARMV6_M_ARMV8_M_BASELINE */
     178                 :            : 
     179         [ #  # ]:          0 :         if (_kernel.idle) {
     180                 :          0 :                 _kernel.idle = 0;
     181                 :          0 :                 z_pm_save_idle_exit();
     182                 :            :         }
     183                 :            : 
     184                 :            : #if defined(CONFIG_ARMV6_M_ARMV8_M_BASELINE) \
     185                 :            :         || defined(CONFIG_ARMV7_R) \
     186                 :            :         || defined(CONFIG_AARCH32_ARMV8_R) \
     187                 :            :         || defined(CONFIG_ARMV7_A)
     188                 :            :         irq_unlock(key);
     189                 :            : #elif defined(CONFIG_ARMV7_M_ARMV8_M_MAINLINE)
     190                 :          0 :         __asm__ volatile("cpsie i" : : : "memory");
     191                 :            : #else
     192                 :            : #error Unknown ARM architecture
     193                 :            : #endif /* CONFIG_ARMV6_M_ARMV8_M_BASELINE */
     194                 :            : 
     195                 :          0 : }
     196                 :            : #endif
     197                 :            : 
     198                 :            : #if defined(CONFIG_ARM_SECURE_FIRMWARE)
     199                 :            : /**
     200                 :            :  *
     201                 :            :  * @brief Set the target security state for the given IRQ
     202                 :            :  *
     203                 :            :  * Function sets the security state (Secure or Non-Secure) targeted
     204                 :            :  * by the given irq. It requires ARMv8-M MCU.
     205                 :            :  * It is only compiled if ARM_SECURE_FIRMWARE is defined.
     206                 :            :  * It should only be called while in Secure state, otherwise, a write attempt
     207                 :            :  * to NVIC.ITNS register is write-ignored(WI), as the ITNS register is not
     208                 :            :  * banked between security states and, therefore, has no Non-Secure instance.
     209                 :            :  *
     210                 :            :  * It shall return the resulting target state of the given IRQ, indicating
     211                 :            :  * whether the operation has been performed successfully.
     212                 :            :  *
     213                 :            :  * @param irq IRQ line
     214                 :            :  * @param irq_target_state the desired IRQ target state
     215                 :            :  *
     216                 :            :  * @return The resulting target state of the given IRQ
     217                 :            :  */
     218                 :            : irq_target_state_t irq_target_state_set(unsigned int irq,
     219                 :            :         irq_target_state_t irq_target_state)
     220                 :            : {
     221                 :            :         uint32_t result;
     222                 :            : 
     223                 :            :         if (irq_target_state == IRQ_TARGET_STATE_SECURE) {
     224                 :            :                 /* Set target to Secure */
     225                 :            :                 result = NVIC_ClearTargetState(irq);
     226                 :            :         } else {
     227                 :            :                 /* Set target to Non-Secure */
     228                 :            :                 result = NVIC_SetTargetState(irq);
     229                 :            :         }
     230                 :            : 
     231                 :            :         if (result) {
     232                 :            :                 return IRQ_TARGET_STATE_NON_SECURE;
     233                 :            :         } else {
     234                 :            :                 return IRQ_TARGET_STATE_SECURE;
     235                 :            :         }
     236                 :            : }
     237                 :            : 
     238                 :            : /**
     239                 :            :  *
     240                 :            :  * @brief Determine whether the given IRQ targets the Secure state
     241                 :            :  *
     242                 :            :  * Function determines whether the given irq targets the Secure state
     243                 :            :  * or not (i.e. targets the Non-Secure state). It requires ARMv8-M MCU.
     244                 :            :  * It is only compiled if ARM_SECURE_FIRMWARE is defined.
     245                 :            :  * It should only be called while in Secure state, otherwise, a read attempt
     246                 :            :  * to NVIC.ITNS register is read-as-zero(RAZ), as the ITNS register is not
     247                 :            :  * banked between security states and, therefore, has no Non-Secure instance.
     248                 :            :  *
     249                 :            :  * @param irq IRQ line
     250                 :            :  *
     251                 :            :  * @return 1 if target state is Secure, 0 otherwise.
     252                 :            :  */
     253                 :            : int irq_target_state_is_secure(unsigned int irq)
     254                 :            : {
     255                 :            :         return NVIC_GetTargetState(irq) == 0;
     256                 :            : }
     257                 :            : 
     258                 :            : /**
     259                 :            :  *
     260                 :            :  * @brief Disable and set all interrupt lines to target Non-Secure state.
     261                 :            :  *
     262                 :            :  * The function is used to set all HW NVIC interrupt lines to target the
     263                 :            :  * Non-Secure state. The function shall only be called fron Secure state.
     264                 :            :  *
     265                 :            :  * Notes:
     266                 :            :  * - All NVIC interrupts are disabled before being routed to Non-Secure.
     267                 :            :  * - Bits corresponding to un-implemented interrupts are RES0, so writes
     268                 :            :  *   will be ignored.
     269                 :            :  *
     270                 :            : */
     271                 :            : void irq_target_state_set_all_non_secure(void)
     272                 :            : {
     273                 :            :         int i;
     274                 :            : 
     275                 :            :         /* Disable (Clear) all NVIC interrupt lines. */
     276                 :            :         for (i = 0; i < sizeof(NVIC->ICER) / sizeof(NVIC->ICER[0]); i++) {
     277                 :            :                 NVIC->ICER[i] = 0xFFFFFFFF;
     278                 :            :         }
     279                 :            : 
     280                 :            :         __DSB();
     281                 :            :         __ISB();
     282                 :            : 
     283                 :            :         /* Set all NVIC interrupt lines to target Non-Secure */
     284                 :            :         for (i = 0; i < sizeof(NVIC->ITNS) / sizeof(NVIC->ITNS[0]); i++) {
     285                 :            :                 NVIC->ITNS[i] = 0xFFFFFFFF;
     286                 :            :         }
     287                 :            : }
     288                 :            : 
     289                 :            : #endif /* CONFIG_ARM_SECURE_FIRMWARE */
     290                 :            : 
     291                 :            : #ifdef CONFIG_DYNAMIC_INTERRUPTS
     292                 :            : #ifdef CONFIG_GEN_ISR_TABLES
     293                 :            : int arch_irq_connect_dynamic(unsigned int irq, unsigned int priority,
     294                 :            :                              void (*routine)(const void *parameter),
     295                 :            :                              const void *parameter, uint32_t flags)
     296                 :            : {
     297                 :            :         z_isr_install(irq, routine, parameter);
     298                 :            :         z_arm_irq_priority_set(irq, priority, flags);
     299                 :            :         return irq;
     300                 :            : }
     301                 :            : #endif /* CONFIG_GEN_ISR_TABLES */
     302                 :            : 
     303                 :            : #ifdef CONFIG_DYNAMIC_DIRECT_INTERRUPTS
     304                 :            : static inline void z_arm_irq_dynamic_direct_isr_dispatch(void)
     305                 :            : {
     306                 :            :         uint32_t irq = __get_IPSR() - 16;
     307                 :            : 
     308                 :            :         if (irq < IRQ_TABLE_SIZE) {
     309                 :            :                 struct _isr_table_entry *isr_entry = &_sw_isr_table[irq];
     310                 :            : 
     311                 :            :                 isr_entry->isr(isr_entry->arg);
     312                 :            :         }
     313                 :            : }
     314                 :            : 
     315                 :            : ISR_DIRECT_DECLARE(z_arm_irq_direct_dynamic_dispatch_reschedule)
     316                 :            : {
     317                 :            :         z_arm_irq_dynamic_direct_isr_dispatch();
     318                 :            : 
     319                 :            :         return 1;
     320                 :            : }
     321                 :            : 
     322                 :            : ISR_DIRECT_DECLARE(z_arm_irq_direct_dynamic_dispatch_no_reschedule)
     323                 :            : {
     324                 :            :         z_arm_irq_dynamic_direct_isr_dispatch();
     325                 :            : 
     326                 :            :         return 0;
     327                 :            : }
     328                 :            : 
     329                 :            : #endif /* CONFIG_DYNAMIC_DIRECT_INTERRUPTS */
     330                 :            : 
     331                 :            : #endif /* CONFIG_DYNAMIC_INTERRUPTS */

Generated by: LCOV version 1.14