hujun260 opened a new pull request, #21662:
URL: https://github.com/apache/nuttx/pull/21662

   # NuttX PR Description
   
   ## Title
   
   arm64/timer: add secure and non-secure physical timer support
   
   ## Description
   
   This PR adds TrustZone-aware ARM64 architectural timer support. The timer
   implementation now selects the appropriate timer registers and interrupt 
based
   on whether NuttX runs in the secure world, the non-secure world, or plain 
EL1.
   
   ## Changes
   
   - Select the timer implementation according to the TrustZone configuration:
     - `CONFIG_ARCH_TRUSTZONE_SECURE`: Secure Physical Timer using `cntps_*`,
       PPI 13, and `GIC_IRQ_STM`.
     - `CONFIG_ARCH_TRUSTZONE_NONSECURE`: Non-Secure Physical Timer using
       `cntp_*`, PPI 14, and `GIC_IRQ_PTM`.
     - Plain EL1: Virtual Timer using `cntv_*`, PPI 11, and `GIC_IRQ_VTM`.
   - Replace the four unused `CONFIG_ARM_TIMER_*_IRQ` macros with the single
     `ARM_ARCH_TIMER_IRQ` selection.
   - Add timer-specific inline accessors for:
     - Compare value programming.
     - Relative timeout programming.
     - Counter enable.
     - Interrupt masking.
     - Counter reading.
   - Preserve the existing virtual-timer accessors for the plain-EL1 default.
   - Fix per-CPU one-shot timer initialization ordering:
     1. Mask the timer interrupt.
     2. Set CVAL to the maximum value.
     3. Enable the timer counter.
     4. Enable the GIC interrupt line.
     5. Unmask the timer only after `oneshot_count_init()` completes.
   - Prevent spurious timer interrupts during initialization.
   - In the secure world, delegate the non-secure timer PPI to the normal world
     with `up_secure_irq(GIC_IRQ_PTM, false)` on every CPU.
   
   ## Motivation
   
   Ensure that ARM64 timer interrupts use the correct security state and timer
   register interface under Trusted Firmware-A and TrustZone deployments. The
   updated initialization sequence also avoids early or spurious timer 
interrupts
   before the one-shot timer state is fully initialized.
   
   ## Runtime Testing
   
   The following runtime log was captured from the Vela QEMU ARM64 TrustZone
   environment using the `qemu-arm64-v8a-ap` configuration with four Cortex-A53
   CPUs, 1057 MiB of RAM, a 4 MiB ivshmem region, and a GDB server on TCP port
   `7775`.
   
   ### Trusted Firmware-A and world initialization
   
   ```text
   NOTICE:  Booting Trusted Firmware
   NOTICE:  BL1: v2.14.0(debug):fb1381d84
   INFO:    BL2: Loading image id 4
   INFO:    Handoff to BL32
   NOTICE:  BL1: Booting BL31
   INFO:    NUTTXSPD: BL32 ep=0xe100000
   INFO:    NUTTXSPD: BL32 CPU0 entry done
   INFO:    NUTTXSPD: BL32 CPU1 entry done
   INFO:    NUTTXSPD: BL32 CPU2 entry done
   INFO:    NUTTXSPD: BL32 CPU3 entry done
   INFO:    Entry point address = 0x40280000
   ```
   
   ### AP timer and shared-memory initialization
   
   ```text
   0.382308  8 I 0 ap pci_sdhci_probe: SDHCI controller at 10001000 irq=37
   0.383143  8 I 0 ap pci_ivshmem_probe: shmem addr=0x8000400000 size=4194304 
reg=0x10001100
   0.393617  8 I 0 ap board_init_emmc: GPT partitions registered
   NuttX  12.7.2 49d23ab02201-dirty Nov 10 2026 13:39:07 arm64 ap
   ```
   
   ### TEE initialization
   
   ```text
   21 I 0 tee D/TC:0 21 call_initcalls:40 level 1 register_time_source()
   21 I 0 tee D/TC:0 21 call_initcalls:40 level 3 tee_cryp_init()
   21 I 0 tee D/TC:0 21 call_initcalls:40 level 4 host_fs_init()
   21 I 0 tee D/TC:0 21 host_fs_init:162 host fs init success
   21 I 0 tee D/TC:0 21 call_initcalls:40 level 6 default_mobj_init()
   ```
   
   ### AP-side `ostest`
   
   ```text
   qemu-armv8a-ap> ostest
   ...
   user_main: smp call test
   cpu_call_test: Test start
   cpu_call_test: Call cpu 0, wait
   cpu_call_test: Call cpu 1, wait
   cpu_call_test: Call cpu 2, wait
   cpu_call_test: Call cpu 3, wait
   cpu_call_test: Call multi cpu, wait
   cpu_call_test: Test success
   
   user_main: Exiting
   ostest_main: Exiting with status 0
   ```
   
   ### TEE-side `ostest`
   
   ```text
   tee> ostest
   ...
   user_main: smp call test
   cpu_call_test: Test start
   cpu_call_test: Call cpu 0, wait
   cpu_call_test: Call cpu 1, wait
   cpu_call_test: Call cpu 2, wait
   cpu_call_test: Call cpu 3, wait
   cpu_call_test: Call multi cpu, wait
   cpu_call_test: Test success
   
   user_main: Exiting
   ostest_main: Exiting with status 0
   ```
   
   ## Testing Status
   
   - [x] Compile-tested with secure-world timer configuration
   - [x] Compile-tested with non-secure-world timer configuration
   - [x] Runtime-tested on Vela QEMU ARM64
   - [x] AP-side `ostest` completed with status 0
   - [x] TEE-side `ostest` completed with status 0
   - [x ] Verified timer interrupt routing separately for secure and non-secure
     physical timer PPIs
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to