diff mbox series

[v3,1/4] KVM: x86/hyperv: Calculate APIC bus frequency for hyper-v

Message ID ecd345619fdddfe48f375160c90322754cec9096.1702974319.git.isaku.yamahata@intel.com (mailing list archive)
State New, archived
Headers show
Series KVM: X86: Make bus clock frequency for vapic timer configurable | expand

Commit Message

Isaku Yamahata Dec. 19, 2023, 8:34 a.m. UTC
Remove APIC_BUS_FREUQNCY and calculate it based on APIC bus cycles per NS.
APIC_BUS_FREUQNCY is used only for HV_X64_MSR_APIC_FREQUENCY.  The MSR is
not frequently read, calculate it every time.

In order to make APIC bus frequency configurable, we need to make make two
related constants into variables.  APIC_BUS_FREUQNCY and APIC_BUS_CYCLE_NS.
One can be calculated from the other.
   APIC_BUS_CYCLES_NS = 1000 * 1000 * 1000 / APIC_BUS_FREQUENCY.
By removing APIC_BUS_FREQUENCY, we need to track only single variable
instead of two.

Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>
---
Changes v3:
- Newly added according to Maxim Levistsky suggestion.
---
 arch/x86/kvm/hyperv.c | 2 +-
 arch/x86/kvm/lapic.h  | 1 -
 2 files changed, 1 insertion(+), 2 deletions(-)

Comments

Xiaoyao Li Dec. 21, 2023, 5:26 a.m. UTC | #1
On 12/19/2023 4:34 PM, Isaku Yamahata wrote:
> Remove APIC_BUS_FREUQNCY and calculate it based on APIC bus cycles per NS.
> APIC_BUS_FREUQNCY is used only for HV_X64_MSR_APIC_FREQUENCY.  The MSR is
> not frequently read, calculate it every time.
> 
> In order to make APIC bus frequency configurable, we need to make make two

two 'make', please drop one.

> related constants into variables.  APIC_BUS_FREUQNCY and APIC_BUS_CYCLE_NS.
> One can be calculated from the other.
>     APIC_BUS_CYCLES_NS = 1000 * 1000 * 1000 / APIC_BUS_FREQUENCY.
> By removing APIC_BUS_FREQUENCY, we need to track only single variable
> instead of two.
> 
> Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>
> ---
> Changes v3:
> - Newly added according to Maxim Levistsky suggestion.
> ---
>   arch/x86/kvm/hyperv.c | 2 +-
>   arch/x86/kvm/lapic.h  | 1 -
>   2 files changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c
> index 238afd7335e4..a40ca2fef58c 100644
> --- a/arch/x86/kvm/hyperv.c
> +++ b/arch/x86/kvm/hyperv.c
> @@ -1687,7 +1687,7 @@ static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata,
>   		data = (u64)vcpu->arch.virtual_tsc_khz * 1000;
>   		break;
>   	case HV_X64_MSR_APIC_FREQUENCY:
> -		data = APIC_BUS_FREQUENCY;
> +		data = div64_u64(1000000000ULL, APIC_BUS_CYCLE_NS);
>   		break;
>   	default:
>   		kvm_pr_unimpl_rdmsr(vcpu, msr);
> diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h
> index 0a0ea4b5dd8c..a20cb006b6c8 100644
> --- a/arch/x86/kvm/lapic.h
> +++ b/arch/x86/kvm/lapic.h
> @@ -17,7 +17,6 @@
>   #define APIC_DEST_MASK			0x800
>   
>   #define APIC_BUS_CYCLE_NS       1
> -#define APIC_BUS_FREQUENCY      (1000000000ULL / APIC_BUS_CYCLE_NS)
>   
>   #define APIC_BROADCAST			0xFF
>   #define X2APIC_BROADCAST		0xFFFFFFFFul
Maxim Levitsky Dec. 21, 2023, 5:39 p.m. UTC | #2
On Tue, 2023-12-19 at 00:34 -0800, Isaku Yamahata wrote:
> Remove APIC_BUS_FREUQNCY

> and calculate it based on APIC bus cycles per NS.
> APIC_BUS_FREUQNCY is used only for HV_X64_MSR_APIC_FREQUENCY.  The MSR is
> not frequently read, calculate it every time.
> 
> In order to make APIC bus frequency configurable, we need to make make two
> related constants into variables.  APIC_BUS_FREUQNCY and APIC_BUS_CYCLE_NS.
> One can be calculated from the other.

APIC_BUS_FREUQNCY is a typo.

>    APIC_BUS_CYCLES_NS = 1000 * 1000 * 1000 / APIC_BUS_FREQUENCY.
> By removing APIC_BUS_FREQUENCY, we need to track only single variable
> instead of two.
> 
> Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>
> ---
> Changes v3:
> - Newly added according to Maxim Levistsky suggestion.
Typo: Maxim Levitsky

> ---
>  arch/x86/kvm/hyperv.c | 2 +-
>  arch/x86/kvm/lapic.h  | 1 -
>  2 files changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c
> index 238afd7335e4..a40ca2fef58c 100644
> --- a/arch/x86/kvm/hyperv.c
> +++ b/arch/x86/kvm/hyperv.c
> @@ -1687,7 +1687,7 @@ static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata,
>  		data = (u64)vcpu->arch.virtual_tsc_khz * 1000;
>  		break;
>  	case HV_X64_MSR_APIC_FREQUENCY:
> -		data = APIC_BUS_FREQUENCY;
> +		data = div64_u64(1000000000ULL, APIC_BUS_CYCLE_NS);
>  		break;
>  	default:
>  		kvm_pr_unimpl_rdmsr(vcpu, msr);
> diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h
> index 0a0ea4b5dd8c..a20cb006b6c8 100644
> --- a/arch/x86/kvm/lapic.h
> +++ b/arch/x86/kvm/lapic.h
> @@ -17,7 +17,6 @@
>  #define APIC_DEST_MASK			0x800
>  
>  #define APIC_BUS_CYCLE_NS       1
> -#define APIC_BUS_FREQUENCY      (1000000000ULL / APIC_BUS_CYCLE_NS)
>  
>  #define APIC_BROADCAST			0xFF
>  #define X2APIC_BROADCAST		0xFFFFFFFFul


Other than typos:

Reviewed-by: Maxim Levitsky <maximlevitsky@gmail.com>

Best regards,
	Maxim Levitsky
Xiaoyao Li Dec. 29, 2023, 4:20 a.m. UTC | #3
On 12/21/2023 1:26 PM, Xiaoyao Li wrote:
> On 12/19/2023 4:34 PM, Isaku Yamahata wrote:
>> Remove APIC_BUS_FREUQNCY and calculate it based on APIC bus cycles per 
>> NS.
>> APIC_BUS_FREUQNCY is used only for HV_X64_MSR_APIC_FREQUENCY.  The MSR is
>> not frequently read, calculate it every time.
>>
>> In order to make APIC bus frequency configurable, we need to make make 
>> two
> 
> two 'make', please drop one.

With this and other typos pointed by Maxim fixed.

Reviewed-by: Xiaoyao Li <xiaoyao.li@intel.com>

>> related constants into variables.  APIC_BUS_FREUQNCY and 
>> APIC_BUS_CYCLE_NS.
>> One can be calculated from the other.
>>     APIC_BUS_CYCLES_NS = 1000 * 1000 * 1000 / APIC_BUS_FREQUENCY.
>> By removing APIC_BUS_FREQUENCY, we need to track only single variable
>> instead of two.
>>
>> Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>
>> ---
>> Changes v3:
>> - Newly added according to Maxim Levistsky suggestion.
>> ---
>>   arch/x86/kvm/hyperv.c | 2 +-
>>   arch/x86/kvm/lapic.h  | 1 -
>>   2 files changed, 1 insertion(+), 2 deletions(-)
>>
>> diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c
>> index 238afd7335e4..a40ca2fef58c 100644
>> --- a/arch/x86/kvm/hyperv.c
>> +++ b/arch/x86/kvm/hyperv.c
>> @@ -1687,7 +1687,7 @@ static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, 
>> u32 msr, u64 *pdata,
>>           data = (u64)vcpu->arch.virtual_tsc_khz * 1000;
>>           break;
>>       case HV_X64_MSR_APIC_FREQUENCY:
>> -        data = APIC_BUS_FREQUENCY;
>> +        data = div64_u64(1000000000ULL, APIC_BUS_CYCLE_NS);
>>           break;
>>       default:
>>           kvm_pr_unimpl_rdmsr(vcpu, msr);
>> diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h
>> index 0a0ea4b5dd8c..a20cb006b6c8 100644
>> --- a/arch/x86/kvm/lapic.h
>> +++ b/arch/x86/kvm/lapic.h
>> @@ -17,7 +17,6 @@
>>   #define APIC_DEST_MASK            0x800
>>   #define APIC_BUS_CYCLE_NS       1
>> -#define APIC_BUS_FREQUENCY      (1000000000ULL / APIC_BUS_CYCLE_NS)
>>   #define APIC_BROADCAST            0xFF
>>   #define X2APIC_BROADCAST        0xFFFFFFFFul
> 
>
diff mbox series

Patch

diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c
index 238afd7335e4..a40ca2fef58c 100644
--- a/arch/x86/kvm/hyperv.c
+++ b/arch/x86/kvm/hyperv.c
@@ -1687,7 +1687,7 @@  static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata,
 		data = (u64)vcpu->arch.virtual_tsc_khz * 1000;
 		break;
 	case HV_X64_MSR_APIC_FREQUENCY:
-		data = APIC_BUS_FREQUENCY;
+		data = div64_u64(1000000000ULL, APIC_BUS_CYCLE_NS);
 		break;
 	default:
 		kvm_pr_unimpl_rdmsr(vcpu, msr);
diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h
index 0a0ea4b5dd8c..a20cb006b6c8 100644
--- a/arch/x86/kvm/lapic.h
+++ b/arch/x86/kvm/lapic.h
@@ -17,7 +17,6 @@ 
 #define APIC_DEST_MASK			0x800
 
 #define APIC_BUS_CYCLE_NS       1
-#define APIC_BUS_FREQUENCY      (1000000000ULL / APIC_BUS_CYCLE_NS)
 
 #define APIC_BROADCAST			0xFF
 #define X2APIC_BROADCAST		0xFFFFFFFFul