Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <linux/types.h>
0003 #include <linux/vmalloc.h>
0004 #include <linux/mm.h>
0005 #include <linux/clockchips.h>
0006 #include <linux/acpi.h>
0007 #include <linux/hyperv.h>
0008 #include <linux/slab.h>
0009 #include <linux/cpuhotplug.h>
0010 #include <linux/minmax.h>
0011 #include <asm/hypervisor.h>
0012 #include <asm/mshyperv.h>
0013 #include <asm/apic.h>
0014 
0015 #include <asm/trace/hyperv.h>
0016 
0017 /*
0018  * See struct hv_deposit_memory. The first u64 is partition ID, the rest
0019  * are GPAs.
0020  */
0021 #define HV_DEPOSIT_MAX (HV_HYP_PAGE_SIZE / sizeof(u64) - 1)
0022 
0023 /* Deposits exact number of pages. Must be called with interrupts enabled.  */
0024 int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages)
0025 {
0026     struct page **pages, *page;
0027     int *counts;
0028     int num_allocations;
0029     int i, j, page_count;
0030     int order;
0031     u64 status;
0032     int ret;
0033     u64 base_pfn;
0034     struct hv_deposit_memory *input_page;
0035     unsigned long flags;
0036 
0037     if (num_pages > HV_DEPOSIT_MAX)
0038         return -E2BIG;
0039     if (!num_pages)
0040         return 0;
0041 
0042     /* One buffer for page pointers and counts */
0043     page = alloc_page(GFP_KERNEL);
0044     if (!page)
0045         return -ENOMEM;
0046     pages = page_address(page);
0047 
0048     counts = kcalloc(HV_DEPOSIT_MAX, sizeof(int), GFP_KERNEL);
0049     if (!counts) {
0050         free_page((unsigned long)pages);
0051         return -ENOMEM;
0052     }
0053 
0054     /* Allocate all the pages before disabling interrupts */
0055     i = 0;
0056 
0057     while (num_pages) {
0058         /* Find highest order we can actually allocate */
0059         order = 31 - __builtin_clz(num_pages);
0060 
0061         while (1) {
0062             pages[i] = alloc_pages_node(node, GFP_KERNEL, order);
0063             if (pages[i])
0064                 break;
0065             if (!order) {
0066                 ret = -ENOMEM;
0067                 num_allocations = i;
0068                 goto err_free_allocations;
0069             }
0070             --order;
0071         }
0072 
0073         split_page(pages[i], order);
0074         counts[i] = 1 << order;
0075         num_pages -= counts[i];
0076         i++;
0077     }
0078     num_allocations = i;
0079 
0080     local_irq_save(flags);
0081 
0082     input_page = *this_cpu_ptr(hyperv_pcpu_input_arg);
0083 
0084     input_page->partition_id = partition_id;
0085 
0086     /* Populate gpa_page_list - these will fit on the input page */
0087     for (i = 0, page_count = 0; i < num_allocations; ++i) {
0088         base_pfn = page_to_pfn(pages[i]);
0089         for (j = 0; j < counts[i]; ++j, ++page_count)
0090             input_page->gpa_page_list[page_count] = base_pfn + j;
0091     }
0092     status = hv_do_rep_hypercall(HVCALL_DEPOSIT_MEMORY,
0093                      page_count, 0, input_page, NULL);
0094     local_irq_restore(flags);
0095     if (!hv_result_success(status)) {
0096         pr_err("Failed to deposit pages: %lld\n", status);
0097         ret = hv_result(status);
0098         goto err_free_allocations;
0099     }
0100 
0101     ret = 0;
0102     goto free_buf;
0103 
0104 err_free_allocations:
0105     for (i = 0; i < num_allocations; ++i) {
0106         base_pfn = page_to_pfn(pages[i]);
0107         for (j = 0; j < counts[i]; ++j)
0108             __free_page(pfn_to_page(base_pfn + j));
0109     }
0110 
0111 free_buf:
0112     free_page((unsigned long)pages);
0113     kfree(counts);
0114     return ret;
0115 }
0116 
0117 int hv_call_add_logical_proc(int node, u32 lp_index, u32 apic_id)
0118 {
0119     struct hv_add_logical_processor_in *input;
0120     struct hv_add_logical_processor_out *output;
0121     u64 status;
0122     unsigned long flags;
0123     int ret = HV_STATUS_SUCCESS;
0124     int pxm = node_to_pxm(node);
0125 
0126     /*
0127      * When adding a logical processor, the hypervisor may return
0128      * HV_STATUS_INSUFFICIENT_MEMORY. When that happens, we deposit more
0129      * pages and retry.
0130      */
0131     do {
0132         local_irq_save(flags);
0133 
0134         input = *this_cpu_ptr(hyperv_pcpu_input_arg);
0135         /* We don't do anything with the output right now */
0136         output = *this_cpu_ptr(hyperv_pcpu_output_arg);
0137 
0138         input->lp_index = lp_index;
0139         input->apic_id = apic_id;
0140         input->flags = 0;
0141         input->proximity_domain_info.domain_id = pxm;
0142         input->proximity_domain_info.flags.reserved = 0;
0143         input->proximity_domain_info.flags.proximity_info_valid = 1;
0144         input->proximity_domain_info.flags.proximity_preferred = 1;
0145         status = hv_do_hypercall(HVCALL_ADD_LOGICAL_PROCESSOR,
0146                      input, output);
0147         local_irq_restore(flags);
0148 
0149         if (hv_result(status) != HV_STATUS_INSUFFICIENT_MEMORY) {
0150             if (!hv_result_success(status)) {
0151                 pr_err("%s: cpu %u apic ID %u, %lld\n", __func__,
0152                        lp_index, apic_id, status);
0153                 ret = hv_result(status);
0154             }
0155             break;
0156         }
0157         ret = hv_call_deposit_pages(node, hv_current_partition_id, 1);
0158     } while (!ret);
0159 
0160     return ret;
0161 }
0162 
0163 int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags)
0164 {
0165     struct hv_create_vp *input;
0166     u64 status;
0167     unsigned long irq_flags;
0168     int ret = HV_STATUS_SUCCESS;
0169     int pxm = node_to_pxm(node);
0170 
0171     /* Root VPs don't seem to need pages deposited */
0172     if (partition_id != hv_current_partition_id) {
0173         /* The value 90 is empirically determined. It may change. */
0174         ret = hv_call_deposit_pages(node, partition_id, 90);
0175         if (ret)
0176             return ret;
0177     }
0178 
0179     do {
0180         local_irq_save(irq_flags);
0181 
0182         input = *this_cpu_ptr(hyperv_pcpu_input_arg);
0183 
0184         input->partition_id = partition_id;
0185         input->vp_index = vp_index;
0186         input->flags = flags;
0187         input->subnode_type = HvSubnodeAny;
0188         if (node != NUMA_NO_NODE) {
0189             input->proximity_domain_info.domain_id = pxm;
0190             input->proximity_domain_info.flags.reserved = 0;
0191             input->proximity_domain_info.flags.proximity_info_valid = 1;
0192             input->proximity_domain_info.flags.proximity_preferred = 1;
0193         } else {
0194             input->proximity_domain_info.as_uint64 = 0;
0195         }
0196         status = hv_do_hypercall(HVCALL_CREATE_VP, input, NULL);
0197         local_irq_restore(irq_flags);
0198 
0199         if (hv_result(status) != HV_STATUS_INSUFFICIENT_MEMORY) {
0200             if (!hv_result_success(status)) {
0201                 pr_err("%s: vcpu %u, lp %u, %lld\n", __func__,
0202                        vp_index, flags, status);
0203                 ret = hv_result(status);
0204             }
0205             break;
0206         }
0207         ret = hv_call_deposit_pages(node, partition_id, 1);
0208 
0209     } while (!ret);
0210 
0211     return ret;
0212 }
0213