1aa04b4ccSPaul Mackerras /* 2aa04b4ccSPaul Mackerras * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com> 3aa04b4ccSPaul Mackerras * 4aa04b4ccSPaul Mackerras * This program is free software; you can redistribute it and/or modify 5aa04b4ccSPaul Mackerras * it under the terms of the GNU General Public License, version 2, as 6aa04b4ccSPaul Mackerras * published by the Free Software Foundation. 7aa04b4ccSPaul Mackerras */ 8aa04b4ccSPaul Mackerras 9441c19c8SMichael Ellerman #include <linux/cpu.h> 10aa04b4ccSPaul Mackerras #include <linux/kvm_host.h> 11aa04b4ccSPaul Mackerras #include <linux/preempt.h> 1266b15db6SPaul Gortmaker #include <linux/export.h> 13aa04b4ccSPaul Mackerras #include <linux/sched.h> 14aa04b4ccSPaul Mackerras #include <linux/spinlock.h> 15aa04b4ccSPaul Mackerras #include <linux/bootmem.h> 16aa04b4ccSPaul Mackerras #include <linux/init.h> 17fa61a4e3SAneesh Kumar K.V #include <linux/memblock.h> 18fa61a4e3SAneesh Kumar K.V #include <linux/sizes.h> 19fc95ca72SJoonsoo Kim #include <linux/cma.h> 2090fd09f8SSam Bobroff #include <linux/bitops.h> 21aa04b4ccSPaul Mackerras 22aa04b4ccSPaul Mackerras #include <asm/cputable.h> 23aa04b4ccSPaul Mackerras #include <asm/kvm_ppc.h> 24aa04b4ccSPaul Mackerras #include <asm/kvm_book3s.h> 25aa04b4ccSPaul Mackerras 26fc95ca72SJoonsoo Kim #define KVM_CMA_CHUNK_ORDER 18 27fc95ca72SJoonsoo Kim 28fa61a4e3SAneesh Kumar K.V /* 29fa61a4e3SAneesh Kumar K.V * Hash page table alignment on newer cpus(CPU_FTR_ARCH_206) 30fa61a4e3SAneesh Kumar K.V * should be power of 2. 31fa61a4e3SAneesh Kumar K.V */ 32fa61a4e3SAneesh Kumar K.V #define HPT_ALIGN_PAGES ((1 << 18) >> PAGE_SHIFT) /* 256k */ 33fa61a4e3SAneesh Kumar K.V /* 34fa61a4e3SAneesh Kumar K.V * By default we reserve 5% of memory for hash pagetable allocation. 35fa61a4e3SAneesh Kumar K.V */ 36fa61a4e3SAneesh Kumar K.V static unsigned long kvm_cma_resv_ratio = 5; 37aa04b4ccSPaul Mackerras 38fc95ca72SJoonsoo Kim static struct cma *kvm_cma; 39fc95ca72SJoonsoo Kim 40fa61a4e3SAneesh Kumar K.V static int __init early_parse_kvm_cma_resv(char *p) 41d2a1b483SAlexander Graf { 42fa61a4e3SAneesh Kumar K.V pr_debug("%s(%s)\n", __func__, p); 43d2a1b483SAlexander Graf if (!p) 44fa61a4e3SAneesh Kumar K.V return -EINVAL; 45fa61a4e3SAneesh Kumar K.V return kstrtoul(p, 0, &kvm_cma_resv_ratio); 46d2a1b483SAlexander Graf } 47fa61a4e3SAneesh Kumar K.V early_param("kvm_cma_resv_ratio", early_parse_kvm_cma_resv); 48d2a1b483SAlexander Graf 49fa61a4e3SAneesh Kumar K.V struct page *kvm_alloc_hpt(unsigned long nr_pages) 50d2a1b483SAlexander Graf { 51c04fa583SAlexey Kardashevskiy VM_BUG_ON(order_base_2(nr_pages) < KVM_CMA_CHUNK_ORDER - PAGE_SHIFT); 52fc95ca72SJoonsoo Kim 53c17b98cfSPaul Mackerras return cma_alloc(kvm_cma, nr_pages, order_base_2(HPT_ALIGN_PAGES)); 54d2a1b483SAlexander Graf } 55d2a1b483SAlexander Graf EXPORT_SYMBOL_GPL(kvm_alloc_hpt); 56d2a1b483SAlexander Graf 57fa61a4e3SAneesh Kumar K.V void kvm_release_hpt(struct page *page, unsigned long nr_pages) 58d2a1b483SAlexander Graf { 59fc95ca72SJoonsoo Kim cma_release(kvm_cma, page, nr_pages); 60d2a1b483SAlexander Graf } 61d2a1b483SAlexander Graf EXPORT_SYMBOL_GPL(kvm_release_hpt); 62d2a1b483SAlexander Graf 63fa61a4e3SAneesh Kumar K.V /** 64fa61a4e3SAneesh Kumar K.V * kvm_cma_reserve() - reserve area for kvm hash pagetable 65fa61a4e3SAneesh Kumar K.V * 66fa61a4e3SAneesh Kumar K.V * This function reserves memory from early allocator. It should be 67fa61a4e3SAneesh Kumar K.V * called by arch specific code once the early allocator (memblock or bootmem) 68fa61a4e3SAneesh Kumar K.V * has been activated and all other subsystems have already allocated/reserved 69fa61a4e3SAneesh Kumar K.V * memory. 70fa61a4e3SAneesh Kumar K.V */ 71fa61a4e3SAneesh Kumar K.V void __init kvm_cma_reserve(void) 72fa61a4e3SAneesh Kumar K.V { 73fa61a4e3SAneesh Kumar K.V unsigned long align_size; 74fa61a4e3SAneesh Kumar K.V struct memblock_region *reg; 75fa61a4e3SAneesh Kumar K.V phys_addr_t selected_size = 0; 76cec26bc3SAneesh Kumar K.V 77cec26bc3SAneesh Kumar K.V /* 78cec26bc3SAneesh Kumar K.V * We need CMA reservation only when we are in HV mode 79cec26bc3SAneesh Kumar K.V */ 80cec26bc3SAneesh Kumar K.V if (!cpu_has_feature(CPU_FTR_HVMODE)) 81cec26bc3SAneesh Kumar K.V return; 82fa61a4e3SAneesh Kumar K.V /* 83fa61a4e3SAneesh Kumar K.V * We cannot use memblock_phys_mem_size() here, because 84fa61a4e3SAneesh Kumar K.V * memblock_analyze() has not been called yet. 85fa61a4e3SAneesh Kumar K.V */ 86fa61a4e3SAneesh Kumar K.V for_each_memblock(memory, reg) 87fa61a4e3SAneesh Kumar K.V selected_size += memblock_region_memory_end_pfn(reg) - 88fa61a4e3SAneesh Kumar K.V memblock_region_memory_base_pfn(reg); 89fa61a4e3SAneesh Kumar K.V 90fa61a4e3SAneesh Kumar K.V selected_size = (selected_size * kvm_cma_resv_ratio / 100) << PAGE_SHIFT; 91fa61a4e3SAneesh Kumar K.V if (selected_size) { 92fa61a4e3SAneesh Kumar K.V pr_debug("%s: reserving %ld MiB for global area\n", __func__, 93fa61a4e3SAneesh Kumar K.V (unsigned long)selected_size / SZ_1M); 94fa61a4e3SAneesh Kumar K.V align_size = HPT_ALIGN_PAGES << PAGE_SHIFT; 95c1f733aaSJoonsoo Kim cma_declare_contiguous(0, selected_size, 0, align_size, 96c1f733aaSJoonsoo Kim KVM_CMA_CHUNK_ORDER - PAGE_SHIFT, false, &kvm_cma); 97fa61a4e3SAneesh Kumar K.V } 98fa61a4e3SAneesh Kumar K.V } 99441c19c8SMichael Ellerman 100441c19c8SMichael Ellerman /* 10190fd09f8SSam Bobroff * Real-mode H_CONFER implementation. 10290fd09f8SSam Bobroff * We check if we are the only vcpu out of this virtual core 10390fd09f8SSam Bobroff * still running in the guest and not ceded. If so, we pop up 10490fd09f8SSam Bobroff * to the virtual-mode implementation; if not, just return to 10590fd09f8SSam Bobroff * the guest. 10690fd09f8SSam Bobroff */ 10790fd09f8SSam Bobroff long int kvmppc_rm_h_confer(struct kvm_vcpu *vcpu, int target, 10890fd09f8SSam Bobroff unsigned int yield_count) 10990fd09f8SSam Bobroff { 11090fd09f8SSam Bobroff struct kvmppc_vcore *vc = vcpu->arch.vcore; 11190fd09f8SSam Bobroff int threads_running; 11290fd09f8SSam Bobroff int threads_ceded; 11390fd09f8SSam Bobroff int threads_conferring; 11490fd09f8SSam Bobroff u64 stop = get_tb() + 10 * tb_ticks_per_usec; 11590fd09f8SSam Bobroff int rv = H_SUCCESS; /* => don't yield */ 11690fd09f8SSam Bobroff 11790fd09f8SSam Bobroff set_bit(vcpu->arch.ptid, &vc->conferring_threads); 11890fd09f8SSam Bobroff while ((get_tb() < stop) && (VCORE_EXIT_COUNT(vc) == 0)) { 11990fd09f8SSam Bobroff threads_running = VCORE_ENTRY_COUNT(vc); 12090fd09f8SSam Bobroff threads_ceded = hweight32(vc->napping_threads); 12190fd09f8SSam Bobroff threads_conferring = hweight32(vc->conferring_threads); 12290fd09f8SSam Bobroff if (threads_ceded + threads_conferring >= threads_running) { 12390fd09f8SSam Bobroff rv = H_TOO_HARD; /* => do yield */ 12490fd09f8SSam Bobroff break; 12590fd09f8SSam Bobroff } 12690fd09f8SSam Bobroff } 12790fd09f8SSam Bobroff clear_bit(vcpu->arch.ptid, &vc->conferring_threads); 12890fd09f8SSam Bobroff return rv; 12990fd09f8SSam Bobroff } 13090fd09f8SSam Bobroff 13190fd09f8SSam Bobroff /* 132441c19c8SMichael Ellerman * When running HV mode KVM we need to block certain operations while KVM VMs 133441c19c8SMichael Ellerman * exist in the system. We use a counter of VMs to track this. 134441c19c8SMichael Ellerman * 135441c19c8SMichael Ellerman * One of the operations we need to block is onlining of secondaries, so we 136441c19c8SMichael Ellerman * protect hv_vm_count with get/put_online_cpus(). 137441c19c8SMichael Ellerman */ 138441c19c8SMichael Ellerman static atomic_t hv_vm_count; 139441c19c8SMichael Ellerman 140441c19c8SMichael Ellerman void kvm_hv_vm_activated(void) 141441c19c8SMichael Ellerman { 142441c19c8SMichael Ellerman get_online_cpus(); 143441c19c8SMichael Ellerman atomic_inc(&hv_vm_count); 144441c19c8SMichael Ellerman put_online_cpus(); 145441c19c8SMichael Ellerman } 146441c19c8SMichael Ellerman EXPORT_SYMBOL_GPL(kvm_hv_vm_activated); 147441c19c8SMichael Ellerman 148441c19c8SMichael Ellerman void kvm_hv_vm_deactivated(void) 149441c19c8SMichael Ellerman { 150441c19c8SMichael Ellerman get_online_cpus(); 151441c19c8SMichael Ellerman atomic_dec(&hv_vm_count); 152441c19c8SMichael Ellerman put_online_cpus(); 153441c19c8SMichael Ellerman } 154441c19c8SMichael Ellerman EXPORT_SYMBOL_GPL(kvm_hv_vm_deactivated); 155441c19c8SMichael Ellerman 156441c19c8SMichael Ellerman bool kvm_hv_mode_active(void) 157441c19c8SMichael Ellerman { 158441c19c8SMichael Ellerman return atomic_read(&hv_vm_count) != 0; 159441c19c8SMichael Ellerman } 160ae2113a4SPaul Mackerras 161ae2113a4SPaul Mackerras extern int hcall_real_table[], hcall_real_table_end[]; 162ae2113a4SPaul Mackerras 163ae2113a4SPaul Mackerras int kvmppc_hcall_impl_hv_realmode(unsigned long cmd) 164ae2113a4SPaul Mackerras { 165ae2113a4SPaul Mackerras cmd /= 4; 166ae2113a4SPaul Mackerras if (cmd < hcall_real_table_end - hcall_real_table && 167ae2113a4SPaul Mackerras hcall_real_table[cmd]) 168ae2113a4SPaul Mackerras return 1; 169ae2113a4SPaul Mackerras 170ae2113a4SPaul Mackerras return 0; 171ae2113a4SPaul Mackerras } 172ae2113a4SPaul Mackerras EXPORT_SYMBOL_GPL(kvmppc_hcall_impl_hv_realmode); 173