arch_timer.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * arch_timer.c - Tests the riscv64 sstc timer IRQ functionality
  4. *
  5. * The test validates the sstc timer IRQs using vstimecmp registers.
  6. * It's ported from the aarch64 arch_timer test.
  7. *
  8. * Copyright (c) 2024, Intel Corporation.
  9. */
  10. #include "arch_timer.h"
  11. #include "kvm_util.h"
  12. #include "processor.h"
  13. #include "timer_test.h"
  14. #include "ucall_common.h"
  15. static int timer_irq = IRQ_S_TIMER;
  16. static void guest_irq_handler(struct ex_regs *regs)
  17. {
  18. uint64_t xcnt, xcnt_diff_us, cmp;
  19. unsigned int intid = regs->cause & ~CAUSE_IRQ_FLAG;
  20. uint32_t cpu = guest_get_vcpuid();
  21. struct test_vcpu_shared_data *shared_data = &vcpu_shared_data[cpu];
  22. timer_irq_disable();
  23. xcnt = timer_get_cycles();
  24. cmp = timer_get_cmp();
  25. xcnt_diff_us = cycles_to_usec(xcnt - shared_data->xcnt);
  26. /* Make sure we are dealing with the correct timer IRQ */
  27. GUEST_ASSERT_EQ(intid, timer_irq);
  28. __GUEST_ASSERT(xcnt >= cmp,
  29. "xcnt = 0x%"PRIx64", cmp = 0x%"PRIx64", xcnt_diff_us = 0x%" PRIx64,
  30. xcnt, cmp, xcnt_diff_us);
  31. WRITE_ONCE(shared_data->nr_iter, shared_data->nr_iter + 1);
  32. }
  33. static void guest_run(struct test_vcpu_shared_data *shared_data)
  34. {
  35. uint32_t irq_iter, config_iter;
  36. shared_data->nr_iter = 0;
  37. shared_data->guest_stage = 0;
  38. for (config_iter = 0; config_iter < test_args.nr_iter; config_iter++) {
  39. /* Setup the next interrupt */
  40. timer_set_next_cmp_ms(test_args.timer_period_ms);
  41. shared_data->xcnt = timer_get_cycles();
  42. timer_irq_enable();
  43. /* Setup a timeout for the interrupt to arrive */
  44. udelay(msecs_to_usecs(test_args.timer_period_ms) +
  45. test_args.timer_err_margin_us);
  46. irq_iter = READ_ONCE(shared_data->nr_iter);
  47. __GUEST_ASSERT(config_iter + 1 == irq_iter,
  48. "config_iter + 1 = 0x%x, irq_iter = 0x%x.\n"
  49. " Guest timer interrupt was not triggered within the specified\n"
  50. " interval, try to increase the error margin by [-e] option.\n",
  51. config_iter + 1, irq_iter);
  52. }
  53. }
  54. static void guest_code(void)
  55. {
  56. uint32_t cpu = guest_get_vcpuid();
  57. struct test_vcpu_shared_data *shared_data = &vcpu_shared_data[cpu];
  58. timer_irq_disable();
  59. local_irq_enable();
  60. guest_run(shared_data);
  61. GUEST_DONE();
  62. }
  63. struct kvm_vm *test_vm_create(void)
  64. {
  65. struct kvm_vm *vm;
  66. int nr_vcpus = test_args.nr_vcpus;
  67. vm = vm_create_with_vcpus(nr_vcpus, guest_code, vcpus);
  68. __TEST_REQUIRE(__vcpu_has_isa_ext(vcpus[0], KVM_RISCV_ISA_EXT_SSTC),
  69. "SSTC not available, skipping test\n");
  70. vm_init_vector_tables(vm);
  71. vm_install_interrupt_handler(vm, guest_irq_handler);
  72. for (int i = 0; i < nr_vcpus; i++)
  73. vcpu_init_vector_tables(vcpus[i]);
  74. /* Initialize guest timer frequency. */
  75. vcpu_get_reg(vcpus[0], RISCV_TIMER_REG(frequency), &timer_freq);
  76. sync_global_to_guest(vm, timer_freq);
  77. pr_debug("timer_freq: %lu\n", timer_freq);
  78. /* Make all the test's cmdline args visible to the guest */
  79. sync_global_to_guest(vm, test_args);
  80. return vm;
  81. }
  82. void test_vm_cleanup(struct kvm_vm *vm)
  83. {
  84. kvm_vm_free(vm);
  85. }