arch_timer.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * The test validates both the virtual and physical timer IRQs using
  4. * CVAL and TVAL registers.
  5. *
  6. * Copyright (c) 2021, Google LLC.
  7. */
  8. #include "arch_timer.h"
  9. #include "delay.h"
  10. #include "gic.h"
  11. #include "processor.h"
  12. #include "timer_test.h"
  13. #include "ucall_common.h"
  14. #include "vgic.h"
  15. enum guest_stage {
  16. GUEST_STAGE_VTIMER_CVAL = 1,
  17. GUEST_STAGE_VTIMER_TVAL,
  18. GUEST_STAGE_PTIMER_CVAL,
  19. GUEST_STAGE_PTIMER_TVAL,
  20. GUEST_STAGE_MAX,
  21. };
  22. static int vtimer_irq, ptimer_irq;
  23. static void
  24. guest_configure_timer_action(struct test_vcpu_shared_data *shared_data)
  25. {
  26. switch (shared_data->guest_stage) {
  27. case GUEST_STAGE_VTIMER_CVAL:
  28. timer_set_next_cval_ms(VIRTUAL, test_args.timer_period_ms);
  29. shared_data->xcnt = timer_get_cntct(VIRTUAL);
  30. timer_set_ctl(VIRTUAL, CTL_ENABLE);
  31. break;
  32. case GUEST_STAGE_VTIMER_TVAL:
  33. timer_set_next_tval_ms(VIRTUAL, test_args.timer_period_ms);
  34. shared_data->xcnt = timer_get_cntct(VIRTUAL);
  35. timer_set_ctl(VIRTUAL, CTL_ENABLE);
  36. break;
  37. case GUEST_STAGE_PTIMER_CVAL:
  38. timer_set_next_cval_ms(PHYSICAL, test_args.timer_period_ms);
  39. shared_data->xcnt = timer_get_cntct(PHYSICAL);
  40. timer_set_ctl(PHYSICAL, CTL_ENABLE);
  41. break;
  42. case GUEST_STAGE_PTIMER_TVAL:
  43. timer_set_next_tval_ms(PHYSICAL, test_args.timer_period_ms);
  44. shared_data->xcnt = timer_get_cntct(PHYSICAL);
  45. timer_set_ctl(PHYSICAL, CTL_ENABLE);
  46. break;
  47. default:
  48. GUEST_ASSERT(0);
  49. }
  50. }
  51. static void guest_validate_irq(unsigned int intid,
  52. struct test_vcpu_shared_data *shared_data)
  53. {
  54. enum guest_stage stage = shared_data->guest_stage;
  55. uint64_t xcnt = 0, xcnt_diff_us, cval = 0;
  56. unsigned long xctl = 0;
  57. unsigned int timer_irq = 0;
  58. unsigned int accessor;
  59. if (intid == IAR_SPURIOUS)
  60. return;
  61. switch (stage) {
  62. case GUEST_STAGE_VTIMER_CVAL:
  63. case GUEST_STAGE_VTIMER_TVAL:
  64. accessor = VIRTUAL;
  65. timer_irq = vtimer_irq;
  66. break;
  67. case GUEST_STAGE_PTIMER_CVAL:
  68. case GUEST_STAGE_PTIMER_TVAL:
  69. accessor = PHYSICAL;
  70. timer_irq = ptimer_irq;
  71. break;
  72. default:
  73. GUEST_ASSERT(0);
  74. return;
  75. }
  76. xctl = timer_get_ctl(accessor);
  77. if ((xctl & CTL_IMASK) || !(xctl & CTL_ENABLE))
  78. return;
  79. timer_set_ctl(accessor, CTL_IMASK);
  80. xcnt = timer_get_cntct(accessor);
  81. cval = timer_get_cval(accessor);
  82. xcnt_diff_us = cycles_to_usec(xcnt - shared_data->xcnt);
  83. /* Make sure we are dealing with the correct timer IRQ */
  84. GUEST_ASSERT_EQ(intid, timer_irq);
  85. /* Basic 'timer condition met' check */
  86. __GUEST_ASSERT(xcnt >= cval,
  87. "xcnt = 0x%lx, cval = 0x%lx, xcnt_diff_us = 0x%lx",
  88. xcnt, cval, xcnt_diff_us);
  89. __GUEST_ASSERT(xctl & CTL_ISTATUS, "xctl = 0x%lx", xctl);
  90. WRITE_ONCE(shared_data->nr_iter, shared_data->nr_iter + 1);
  91. }
  92. static void guest_irq_handler(struct ex_regs *regs)
  93. {
  94. unsigned int intid = gic_get_and_ack_irq();
  95. uint32_t cpu = guest_get_vcpuid();
  96. struct test_vcpu_shared_data *shared_data = &vcpu_shared_data[cpu];
  97. guest_validate_irq(intid, shared_data);
  98. gic_set_eoi(intid);
  99. }
  100. static void guest_run_stage(struct test_vcpu_shared_data *shared_data,
  101. enum guest_stage stage)
  102. {
  103. uint32_t irq_iter, config_iter;
  104. shared_data->guest_stage = stage;
  105. shared_data->nr_iter = 0;
  106. for (config_iter = 0; config_iter < test_args.nr_iter; config_iter++) {
  107. /* Setup the next interrupt */
  108. guest_configure_timer_action(shared_data);
  109. /* Setup a timeout for the interrupt to arrive */
  110. udelay(msecs_to_usecs(test_args.timer_period_ms) +
  111. test_args.timer_err_margin_us);
  112. irq_iter = READ_ONCE(shared_data->nr_iter);
  113. __GUEST_ASSERT(config_iter + 1 == irq_iter,
  114. "config_iter + 1 = 0x%x, irq_iter = 0x%x.\n"
  115. " Guest timer interrupt was not triggered within the specified\n"
  116. " interval, try to increase the error margin by [-e] option.\n",
  117. config_iter + 1, irq_iter);
  118. }
  119. }
  120. static void guest_code(void)
  121. {
  122. uint32_t cpu = guest_get_vcpuid();
  123. struct test_vcpu_shared_data *shared_data = &vcpu_shared_data[cpu];
  124. local_irq_disable();
  125. gic_init(GIC_V3, test_args.nr_vcpus);
  126. timer_set_ctl(VIRTUAL, CTL_IMASK);
  127. timer_set_ctl(PHYSICAL, CTL_IMASK);
  128. gic_irq_enable(vtimer_irq);
  129. gic_irq_enable(ptimer_irq);
  130. local_irq_enable();
  131. guest_run_stage(shared_data, GUEST_STAGE_VTIMER_CVAL);
  132. guest_run_stage(shared_data, GUEST_STAGE_VTIMER_TVAL);
  133. guest_run_stage(shared_data, GUEST_STAGE_PTIMER_CVAL);
  134. guest_run_stage(shared_data, GUEST_STAGE_PTIMER_TVAL);
  135. GUEST_DONE();
  136. }
  137. static void test_init_timer_irq(struct kvm_vm *vm)
  138. {
  139. /* Timer initid should be same for all the vCPUs, so query only vCPU-0 */
  140. ptimer_irq = vcpu_get_ptimer_irq(vcpus[0]);
  141. vtimer_irq = vcpu_get_vtimer_irq(vcpus[0]);
  142. sync_global_to_guest(vm, ptimer_irq);
  143. sync_global_to_guest(vm, vtimer_irq);
  144. pr_debug("ptimer_irq: %d; vtimer_irq: %d\n", ptimer_irq, vtimer_irq);
  145. }
  146. struct kvm_vm *test_vm_create(void)
  147. {
  148. struct kvm_vm *vm;
  149. unsigned int i;
  150. int nr_vcpus = test_args.nr_vcpus;
  151. TEST_REQUIRE(kvm_supports_vgic_v3());
  152. vm = vm_create_with_vcpus(nr_vcpus, guest_code, vcpus);
  153. vm_init_descriptor_tables(vm);
  154. vm_install_exception_handler(vm, VECTOR_IRQ_CURRENT, guest_irq_handler);
  155. if (!test_args.reserved) {
  156. if (kvm_has_cap(KVM_CAP_COUNTER_OFFSET)) {
  157. struct kvm_arm_counter_offset offset = {
  158. .counter_offset = test_args.counter_offset,
  159. .reserved = 0,
  160. };
  161. vm_ioctl(vm, KVM_ARM_SET_COUNTER_OFFSET, &offset);
  162. } else
  163. TEST_FAIL("no support for global offset");
  164. }
  165. for (i = 0; i < nr_vcpus; i++)
  166. vcpu_init_descriptor_tables(vcpus[i]);
  167. test_init_timer_irq(vm);
  168. /* Make all the test's cmdline args visible to the guest */
  169. sync_global_to_guest(vm, test_args);
  170. return vm;
  171. }
  172. void test_vm_cleanup(struct kvm_vm *vm)
  173. {
  174. kvm_vm_free(vm);
  175. }