1 /******************************************************************************
2  * arch/x86/guest/hyperv/util.c
3  *
4  * Hyper-V utility functions
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; If not, see <http://www.gnu.org/licenses/>.
18  *
19  * Copyright (c) 2020 Microsoft.
20  */
21 
22 #include <xen/cpu.h>
23 #include <xen/cpumask.h>
24 #include <xen/errno.h>
25 
26 #include <asm/guest/hyperv.h>
27 #include <asm/guest/hyperv-tlfs.h>
28 
29 #include "private.h"
30 
cpumask_to_vpset(struct hv_vpset * vpset,const cpumask_t * mask)31 int cpumask_to_vpset(struct hv_vpset *vpset,
32                      const cpumask_t *mask)
33 {
34     int nr = 1;
35     unsigned int cpu, vcpu_bank, vcpu_offset;
36     unsigned int max_banks = hv_max_vp_index / 64;
37 
38     /* Up to 64 banks can be represented by valid_bank_mask */
39     if ( max_banks > 64 )
40         return -E2BIG;
41 
42     /* Clear all banks to avoid flushing unwanted CPUs */
43     for ( vcpu_bank = 0; vcpu_bank < max_banks; vcpu_bank++ )
44         vpset->bank_contents[vcpu_bank] = 0;
45 
46     vpset->format = HV_GENERIC_SET_SPARSE_4K;
47 
48     for_each_cpu ( cpu, mask )
49     {
50         unsigned int vcpu = hv_vp_index(cpu);
51 
52         vcpu_bank = vcpu / 64;
53         vcpu_offset = vcpu % 64;
54 
55         __set_bit(vcpu_offset, &vpset->bank_contents[vcpu_bank]);
56 
57         if ( vcpu_bank >= nr )
58             nr = vcpu_bank + 1;
59     }
60 
61     /* Some banks may be empty but that's ok */
62     vpset->valid_bank_mask = ~0ULL >> (64 - nr);
63 
64     return nr;
65 }
66 
67 /*
68  * Local variables:
69  * mode: C
70  * c-file-style: "BSD"
71  * c-basic-offset: 4
72  * tab-width: 4
73  * indent-tabs-mode: nil
74  * End:
75  */
76