| /* |
| * Copyright 2023 Google LLC |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| |
| #include "hw/top_matcha/sw/autogen/top_matcha.h" |
| #include "sw/device/lib/arch/device.h" |
| #include "sw/device/lib/dif/dif_rv_plic.h" |
| #include "sw/device/lib/dif/dif_smc_ctrl.h" |
| #include "sw/device/lib/dif/dif_tlul_mailbox.h" |
| #include "sw/device/lib/dif/dif_uart.h" |
| #include "sw/device/lib/runtime/hart.h" |
| #include "sw/device/lib/runtime/log.h" |
| #include "sw/device/lib/runtime/print.h" |
| #include "sw/device/lib/testing/test_framework/check.h" |
| #include "sw/device/lib/testing/test_framework/ottf_test_config.h" |
| #include "sw/device/lib/testing/test_framework/status.h" |
| #include "sw/device/lib/testing/test_framework/test_util.h" |
| |
| OTTF_DEFINE_TEST_CONFIG(); |
| |
| static dif_uart_t uart; |
| static dif_tlul_mailbox_t tlul_mailbox; |
| static dif_smc_ctrl_t smc_ctrl; |
| static dif_rv_plic_t plic_sec; |
| |
| void ottf_external_isr(void) { |
| uint32_t rx; |
| dif_rv_plic_irq_id_t plic_irq_id; |
| |
| LOG_INFO("Woken!"); |
| |
| // Clear IRQ from the tlul_mailbox and claim it IRQ from the PLIC. |
| CHECK_DIF_OK( |
| dif_tlul_mailbox_irq_acknowledge(&tlul_mailbox, kDifTlulMailboxIrqRtirq)); |
| CHECK_DIF_OK(dif_rv_plic_irq_claim(&plic_sec, kTopMatchaPlicTargetIbex0, |
| &plic_irq_id)); |
| |
| CHECK_DIF_OK(dif_tlul_mailbox_read_message(&tlul_mailbox, &rx)); |
| LOG_INFO("Message: %x", rx); |
| CHECK(rx == 0xBEEFB0BA, |
| "Message from SMC incorrect - expected: 0xBEEFB0BA | actual: %x", rx); |
| |
| CHECK_DIF_OK(dif_rv_plic_irq_complete(&plic_sec, kTopMatchaPlicTargetIbex0, |
| plic_irq_id)); |
| |
| // End the test in the ISR since we return to wfi afterwards. |
| test_status_set(kTestStatusPassed); |
| } |
| |
| void _ottf_main(void) { |
| // Initialize the UART to enable logging for non-DV simulation platforms. |
| if (kDeviceType != kDeviceSimDV) { |
| init_uart(TOP_MATCHA_UART0_BASE_ADDR, &uart); |
| } |
| |
| LOG_INFO("Hello Shodan!"); |
| |
| test_status_set(kTestStatusInTest); |
| |
| uint32_t buf1 = 0xCAFEB0BA; |
| uint32_t buf2 = 0xDEADBEEF; |
| |
| CHECK_DIF_OK(dif_tlul_mailbox_init( |
| mmio_region_from_addr(TOP_MATCHA_TLUL_MAILBOX_SEC_BASE_ADDR), |
| &tlul_mailbox)); |
| CHECK_DIF_OK(dif_tlul_mailbox_irq_set_enabled( |
| &tlul_mailbox, kDifTlulMailboxIrqRtirq, kDifToggleEnabled)); |
| CHECK_DIF_OK(dif_tlul_mailbox_irq_set_enabled( |
| &tlul_mailbox, kDifTlulMailboxIrqWtirq, kDifToggleEnabled)); |
| CHECK_DIF_OK(dif_tlul_mailbox_irq_set_enabled( |
| &tlul_mailbox, kDifTlulMailboxIrqEirq, kDifToggleEnabled)); |
| |
| CHECK_DIF_OK(dif_smc_ctrl_init( |
| mmio_region_from_addr(TOP_MATCHA_SMC_CTRL_BASE_ADDR), &smc_ctrl)); |
| |
| CHECK_DIF_OK(dif_smc_ctrl_set_en(&smc_ctrl)); |
| LOG_INFO("Set SMC CTRL_EN."); |
| |
| CHECK_DIF_OK(dif_rv_plic_init( |
| mmio_region_from_addr(TOP_MATCHA_RV_PLIC_BASE_ADDR), &plic_sec)); |
| CHECK_DIF_OK(dif_rv_plic_irq_set_enabled( |
| &plic_sec, kTopMatchaPlicIrqIdTlulMailboxSecRtirq, |
| kTopMatchaPlicTargetIbex0, kDifToggleEnabled)); |
| CHECK_DIF_OK(dif_rv_plic_irq_set_priority( |
| &plic_sec, kTopMatchaPlicIrqIdTlulMailboxSecRtirq, 1)); |
| |
| LOG_INFO("Sending..."); |
| CHECK_DIF_OK(dif_tlul_mailbox_send_message(&tlul_mailbox, &buf1)); |
| CHECK_DIF_OK(dif_tlul_mailbox_send_message(&tlul_mailbox, &buf2)); |
| |
| LOG_INFO("Enable interrupts and sleep."); |
| asm volatile("csrs mstatus, %0\n" : : "r"(0x8)); |
| asm volatile("csrs mie, %0\n" : : "r"(0x800)); |
| |
| asm volatile("wfi"); |
| } |