Madhukar Pappireddy | c31708a | 2024-09-25 14:04:03 -0500 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2024 The Hafnium Authors. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style |
| 5 | * license that can be found in the LICENSE file or at |
| 6 | * https://opensource.org/licenses/BSD-3-Clause. |
| 7 | */ |
| 8 | |
| 9 | #include "hf/timer_mgmt.h" |
| 10 | |
| 11 | #include "hf/arch/timer.h" |
| 12 | |
| 13 | #include "hf/api.h" |
| 14 | #include "hf/check.h" |
| 15 | #include "hf/cpu.h" |
| 16 | #include "hf/std.h" |
| 17 | #include "hf/vcpu.h" |
| 18 | |
| 19 | static void timer_list_add_vcpu(struct cpu *cpu, struct vcpu *vcpu) |
| 20 | { |
| 21 | struct timer_pending_vcpu_list *timer_list; |
| 22 | |
| 23 | assert(vcpu != NULL && cpu != NULL); |
| 24 | |
| 25 | timer_list = &cpu->pending_timer_vcpus_list; |
| 26 | sl_lock(&cpu->lock); |
| 27 | |
| 28 | /* Add the vCPU's timer entry if not already part of any list. */ |
| 29 | if (list_empty(&vcpu->timer_node)) { |
| 30 | /* `root_entry` is also the tail of the timer list. */ |
| 31 | list_prepend(&timer_list->root_entry, &vcpu->timer_node); |
| 32 | } |
| 33 | |
| 34 | sl_unlock(&cpu->lock); |
| 35 | } |
| 36 | |
| 37 | static void timer_list_remove_vcpu(struct cpu *cpu, struct vcpu *vcpu) |
| 38 | { |
| 39 | assert(vcpu != NULL && cpu != NULL); |
| 40 | |
| 41 | sl_lock(&cpu->lock); |
| 42 | list_remove(&vcpu->timer_node); |
| 43 | sl_unlock(&cpu->lock); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Depending on the state of the vCPU's arch timer, either track or untrack it |
| 48 | * through the timer list on current CPU. |
| 49 | */ |
| 50 | void timer_vcpu_manage(struct vcpu *vcpu) |
| 51 | { |
| 52 | assert(vcpu != NULL); |
| 53 | |
| 54 | if (arch_timer_enabled(&vcpu->regs)) { |
| 55 | /* |
| 56 | * Add it to the list maintained by partition manager for this |
| 57 | * CPU. |
| 58 | */ |
| 59 | timer_list_add_vcpu(vcpu->cpu, vcpu); |
| 60 | } else { |
| 61 | timer_list_remove_vcpu(vcpu->cpu, vcpu); |
| 62 | } |
| 63 | } |