blob: 9b329ace1ce83c87367f45f41cc3404e4f0228a0 [file] [log] [blame]
/*
* 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_timer.h"
#include "sw/device/lib/dif/dif_smc_ctrl.h"
#include "sw/device/lib/dif/dif_uart.h"
#include "sw/device/lib/runtime/hart.h"
#include "sw/device/lib/runtime/irq.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/test_util.h"
#include "sw/device/tests/smc/smc_rv_timer_test_fpga_nexus_bin_c.h"
OTTF_DEFINE_TEST_CONFIG();
static dif_rv_timer_t rv_timer;
static dif_smc_ctrl_t smc_ctrl;
static dif_uart_t uart;
static volatile bool timer_isr_seen = false;
void ottf_timer_isr(void) {
CHECK(!timer_isr_seen);
timer_isr_seen = true;
CHECK_DIF_OK(
dif_rv_timer_counter_set_enabled(&rv_timer, 0, kDifToggleDisabled));
CHECK_DIF_OK(dif_rv_timer_irq_acknowledge(
&rv_timer, kDifRvTimerIrqTimerExpiredHart0Timer0));
}
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("Begin RV_TIMER test.");
test_status_set(kTestStatusInTest);
irq_timer_ctrl(true);
irq_global_ctrl(true);
dif_rv_timer_tick_params_t tick_params;
CHECK_DIF_OK(dif_rv_timer_approximate_tick_params(kClockFreqPeripheralHz,
1000000, &tick_params));
CHECK_DIF_OK(dif_rv_timer_init(
mmio_region_from_addr(TOP_MATCHA_RV_TIMER_BASE_ADDR), &rv_timer));
CHECK_DIF_OK(dif_rv_timer_set_tick_params(&rv_timer, 0, tick_params));
CHECK_DIF_OK(dif_rv_timer_irq_set_enabled(
&rv_timer, kDifRvTimerIrqTimerExpiredHart0Timer0, kDifToggleEnabled));
uint64_t counter_start, counter_end;
CHECK_DIF_OK(dif_rv_timer_counter_read(&rv_timer, 0, &counter_start));
CHECK_DIF_OK(dif_rv_timer_arm(&rv_timer, 0, 0, counter_start + 1000));
CHECK_DIF_OK(
dif_rv_timer_counter_set_enabled(&rv_timer, 0, kDifToggleEnabled));
while (true) {
wait_for_interrupt();
if (timer_isr_seen) {
break;
}
}
CHECK_DIF_OK(dif_rv_timer_counter_read(&rv_timer, 0, &counter_end));
CHECK((counter_end - counter_start) >= 1000);
CHECK((counter_end - counter_start) < 2000);
LOG_INFO("Begin RV_TIMER test on SMC.");
// Copy embedded binary to SMC RAM.
if (kDeviceType == kDeviceFpgaNexus) {
memcpy((void *)TOP_MATCHA_RAM_SMC_BASE_ADDR, smc_bin, smc_bin_len);
}
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));
// Note: the SMC must end the simulation through testing or otherwise, as the
// secure core indefinitely sleeps.
asm volatile("wfi");
__builtin_unreachable();
}