| Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 1 | /* |
| Boyan Karatotev | 794b0ac | 2025-06-20 13:13:29 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2025, Arm Limited. All rights reserved. |
| Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <arch_helpers.h> |
| Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 8 | #include <debug.h> |
| Antonio Nino Diaz | 09a00ef | 2019-01-11 13:12:58 +0000 | [diff] [blame] | 9 | #include <drivers/arm/arm_gic.h> |
| Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 10 | #include <irq.h> |
| 11 | #include <platform.h> |
| Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 12 | #include <tftf_lib.h> |
| 13 | |
| 14 | /* |
| 15 | * The 2 following global variables are used by the SGI handler to return |
| 16 | * information to the main test function. |
| 17 | */ |
| Boyan Karatotev | 794b0ac | 2025-06-20 13:13:29 +0100 | [diff] [blame] | 18 | static unsigned int sgi_data; |
| Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 19 | |
| 20 | /* Flag to indicate whether the SGI has been handled */ |
| 21 | static volatile unsigned int sgi_handled; |
| 22 | |
| 23 | static int sgi_handler(void *data) |
| 24 | { |
| 25 | /* Save SGI data */ |
| Boyan Karatotev | 794b0ac | 2025-06-20 13:13:29 +0100 | [diff] [blame] | 26 | sgi_data = *(unsigned int *) data; |
| Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 27 | sgi_handled = 1; |
| 28 | |
| 29 | /* Return value doesn't matter */ |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | /* |
| 34 | * @Test_Aim@ Test SGI support on lead CPU |
| 35 | * |
| 36 | * 1) Register a local IRQ handler for SGI 0. |
| 37 | * 2) Send SGI 0 to the calling core, i.e. the lead CPU. |
| 38 | * 3) Check the correctness of the data received in the IRQ handler. |
| 39 | * |
| 40 | * TODO: Improve this test by sending SGIs to all cores in the system. |
| 41 | * This will ensure that IRQs are correctly configured on all cores. |
| 42 | */ |
| 43 | test_result_t test_validation_sgi(void) |
| 44 | { |
| 45 | unsigned int mpid = read_mpidr_el1(); |
| 46 | unsigned int core_pos = platform_get_core_pos(mpid); |
| 47 | const unsigned int sgi_id = IRQ_NS_SGI_0; |
| Boyan Karatotev | 6d144db | 2025-06-23 15:04:53 +0100 | [diff] [blame^] | 48 | const unsigned int sgi_irq = tftf_irq_get_my_sgi_num(sgi_id); |
| Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 49 | test_result_t test_res = TEST_RESULT_SUCCESS; |
| 50 | int ret; |
| 51 | |
| 52 | /* Register the local IRQ handler for the SGI */ |
| Boyan Karatotev | 6d144db | 2025-06-23 15:04:53 +0100 | [diff] [blame^] | 53 | ret = tftf_irq_register_handler_sgi(sgi_id, sgi_handler); |
| Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 54 | if (ret != 0) { |
| 55 | tftf_testcase_printf("Failed to register IRQ %u (%d)", |
| 56 | sgi_id, ret); |
| 57 | return TEST_RESULT_FAIL; |
| 58 | } |
| Boyan Karatotev | 6d144db | 2025-06-23 15:04:53 +0100 | [diff] [blame^] | 59 | tftf_irq_enable_sgi(sgi_id, GIC_HIGHEST_NS_PRIORITY); |
| Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 60 | |
| 61 | /* Send the SGI to the lead CPU */ |
| 62 | tftf_send_sgi(sgi_id, core_pos); |
| 63 | |
| 64 | /* |
| 65 | * Wait for the SGI to be handled. |
| 66 | * The SGI handler will update a global variable to reflect that. |
| 67 | */ |
| 68 | while (sgi_handled == 0) |
| 69 | continue; |
| 70 | |
| 71 | /* Verify the data received in the SGI handler */ |
| Boyan Karatotev | 6d144db | 2025-06-23 15:04:53 +0100 | [diff] [blame^] | 72 | if (sgi_data != sgi_irq) { |
| Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 73 | tftf_testcase_printf("Wrong IRQ ID, expected %u, got %u\n", |
| Boyan Karatotev | 6d144db | 2025-06-23 15:04:53 +0100 | [diff] [blame^] | 74 | sgi_irq, sgi_data); |
| Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 75 | test_res = TEST_RESULT_FAIL; |
| 76 | } |
| 77 | |
| Boyan Karatotev | 6d144db | 2025-06-23 15:04:53 +0100 | [diff] [blame^] | 78 | tftf_irq_disable_sgi(sgi_id); |
| 79 | tftf_irq_unregister_handler_sgi(sgi_id); |
| Sandrine Bailleux | 3cd87d7 | 2018-10-09 11:12:55 +0200 | [diff] [blame] | 80 | |
| 81 | return test_res; |
| 82 | } |