]>
Commit | Line | Data |
---|---|---|
aa04b4cc PM |
1 | /* |
2 | * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License, version 2, as | |
6 | * published by the Free Software Foundation. | |
7 | */ | |
8 | ||
441c19c8 | 9 | #include <linux/cpu.h> |
aa04b4cc PM |
10 | #include <linux/kvm_host.h> |
11 | #include <linux/preempt.h> | |
66b15db6 | 12 | #include <linux/export.h> |
aa04b4cc PM |
13 | #include <linux/sched.h> |
14 | #include <linux/spinlock.h> | |
aa04b4cc | 15 | #include <linux/init.h> |
fa61a4e3 AK |
16 | #include <linux/memblock.h> |
17 | #include <linux/sizes.h> | |
fc95ca72 | 18 | #include <linux/cma.h> |
90fd09f8 | 19 | #include <linux/bitops.h> |
aa04b4cc PM |
20 | |
21 | #include <asm/cputable.h> | |
22 | #include <asm/kvm_ppc.h> | |
23 | #include <asm/kvm_book3s.h> | |
e928e9cb | 24 | #include <asm/archrandom.h> |
eddb60fb | 25 | #include <asm/xics.h> |
aa04b4cc | 26 | |
fc95ca72 JK |
27 | #define KVM_CMA_CHUNK_ORDER 18 |
28 | ||
fa61a4e3 AK |
29 | /* |
30 | * Hash page table alignment on newer cpus(CPU_FTR_ARCH_206) | |
31 | * should be power of 2. | |
32 | */ | |
33 | #define HPT_ALIGN_PAGES ((1 << 18) >> PAGE_SHIFT) /* 256k */ | |
34 | /* | |
35 | * By default we reserve 5% of memory for hash pagetable allocation. | |
36 | */ | |
37 | static unsigned long kvm_cma_resv_ratio = 5; | |
aa04b4cc | 38 | |
fc95ca72 JK |
39 | static struct cma *kvm_cma; |
40 | ||
fa61a4e3 | 41 | static int __init early_parse_kvm_cma_resv(char *p) |
d2a1b483 | 42 | { |
fa61a4e3 | 43 | pr_debug("%s(%s)\n", __func__, p); |
d2a1b483 | 44 | if (!p) |
fa61a4e3 AK |
45 | return -EINVAL; |
46 | return kstrtoul(p, 0, &kvm_cma_resv_ratio); | |
d2a1b483 | 47 | } |
fa61a4e3 | 48 | early_param("kvm_cma_resv_ratio", early_parse_kvm_cma_resv); |
d2a1b483 | 49 | |
fa61a4e3 | 50 | struct page *kvm_alloc_hpt(unsigned long nr_pages) |
d2a1b483 | 51 | { |
c04fa583 | 52 | VM_BUG_ON(order_base_2(nr_pages) < KVM_CMA_CHUNK_ORDER - PAGE_SHIFT); |
fc95ca72 | 53 | |
c17b98cf | 54 | return cma_alloc(kvm_cma, nr_pages, order_base_2(HPT_ALIGN_PAGES)); |
d2a1b483 AG |
55 | } |
56 | EXPORT_SYMBOL_GPL(kvm_alloc_hpt); | |
57 | ||
fa61a4e3 | 58 | void kvm_release_hpt(struct page *page, unsigned long nr_pages) |
d2a1b483 | 59 | { |
fc95ca72 | 60 | cma_release(kvm_cma, page, nr_pages); |
d2a1b483 AG |
61 | } |
62 | EXPORT_SYMBOL_GPL(kvm_release_hpt); | |
63 | ||
fa61a4e3 AK |
64 | /** |
65 | * kvm_cma_reserve() - reserve area for kvm hash pagetable | |
66 | * | |
67 | * This function reserves memory from early allocator. It should be | |
14ed7409 | 68 | * called by arch specific code once the memblock allocator |
fa61a4e3 AK |
69 | * has been activated and all other subsystems have already allocated/reserved |
70 | * memory. | |
71 | */ | |
72 | void __init kvm_cma_reserve(void) | |
73 | { | |
74 | unsigned long align_size; | |
75 | struct memblock_region *reg; | |
76 | phys_addr_t selected_size = 0; | |
cec26bc3 AK |
77 | |
78 | /* | |
79 | * We need CMA reservation only when we are in HV mode | |
80 | */ | |
81 | if (!cpu_has_feature(CPU_FTR_HVMODE)) | |
82 | return; | |
fa61a4e3 AK |
83 | /* |
84 | * We cannot use memblock_phys_mem_size() here, because | |
85 | * memblock_analyze() has not been called yet. | |
86 | */ | |
87 | for_each_memblock(memory, reg) | |
88 | selected_size += memblock_region_memory_end_pfn(reg) - | |
89 | memblock_region_memory_base_pfn(reg); | |
90 | ||
91 | selected_size = (selected_size * kvm_cma_resv_ratio / 100) << PAGE_SHIFT; | |
92 | if (selected_size) { | |
93 | pr_debug("%s: reserving %ld MiB for global area\n", __func__, | |
94 | (unsigned long)selected_size / SZ_1M); | |
c17b98cf | 95 | align_size = HPT_ALIGN_PAGES << PAGE_SHIFT; |
c1f733aa JK |
96 | cma_declare_contiguous(0, selected_size, 0, align_size, |
97 | KVM_CMA_CHUNK_ORDER - PAGE_SHIFT, false, &kvm_cma); | |
fa61a4e3 AK |
98 | } |
99 | } | |
441c19c8 | 100 | |
90fd09f8 SB |
101 | /* |
102 | * Real-mode H_CONFER implementation. | |
103 | * We check if we are the only vcpu out of this virtual core | |
104 | * still running in the guest and not ceded. If so, we pop up | |
105 | * to the virtual-mode implementation; if not, just return to | |
106 | * the guest. | |
107 | */ | |
108 | long int kvmppc_rm_h_confer(struct kvm_vcpu *vcpu, int target, | |
109 | unsigned int yield_count) | |
110 | { | |
111 | struct kvmppc_vcore *vc = vcpu->arch.vcore; | |
112 | int threads_running; | |
113 | int threads_ceded; | |
114 | int threads_conferring; | |
115 | u64 stop = get_tb() + 10 * tb_ticks_per_usec; | |
116 | int rv = H_SUCCESS; /* => don't yield */ | |
117 | ||
118 | set_bit(vcpu->arch.ptid, &vc->conferring_threads); | |
7d6c40da PM |
119 | while ((get_tb() < stop) && !VCORE_IS_EXITING(vc)) { |
120 | threads_running = VCORE_ENTRY_MAP(vc); | |
121 | threads_ceded = vc->napping_threads; | |
122 | threads_conferring = vc->conferring_threads; | |
123 | if ((threads_ceded | threads_conferring) == threads_running) { | |
90fd09f8 SB |
124 | rv = H_TOO_HARD; /* => do yield */ |
125 | break; | |
126 | } | |
127 | } | |
128 | clear_bit(vcpu->arch.ptid, &vc->conferring_threads); | |
129 | return rv; | |
130 | } | |
131 | ||
441c19c8 ME |
132 | /* |
133 | * When running HV mode KVM we need to block certain operations while KVM VMs | |
134 | * exist in the system. We use a counter of VMs to track this. | |
135 | * | |
136 | * One of the operations we need to block is onlining of secondaries, so we | |
137 | * protect hv_vm_count with get/put_online_cpus(). | |
138 | */ | |
139 | static atomic_t hv_vm_count; | |
140 | ||
141 | void kvm_hv_vm_activated(void) | |
142 | { | |
143 | get_online_cpus(); | |
144 | atomic_inc(&hv_vm_count); | |
145 | put_online_cpus(); | |
146 | } | |
147 | EXPORT_SYMBOL_GPL(kvm_hv_vm_activated); | |
148 | ||
149 | void kvm_hv_vm_deactivated(void) | |
150 | { | |
151 | get_online_cpus(); | |
152 | atomic_dec(&hv_vm_count); | |
153 | put_online_cpus(); | |
154 | } | |
155 | EXPORT_SYMBOL_GPL(kvm_hv_vm_deactivated); | |
156 | ||
157 | bool kvm_hv_mode_active(void) | |
158 | { | |
159 | return atomic_read(&hv_vm_count) != 0; | |
160 | } | |
ae2113a4 PM |
161 | |
162 | extern int hcall_real_table[], hcall_real_table_end[]; | |
163 | ||
164 | int kvmppc_hcall_impl_hv_realmode(unsigned long cmd) | |
165 | { | |
166 | cmd /= 4; | |
167 | if (cmd < hcall_real_table_end - hcall_real_table && | |
168 | hcall_real_table[cmd]) | |
169 | return 1; | |
170 | ||
171 | return 0; | |
172 | } | |
173 | EXPORT_SYMBOL_GPL(kvmppc_hcall_impl_hv_realmode); | |
e928e9cb ME |
174 | |
175 | int kvmppc_hwrng_present(void) | |
176 | { | |
177 | return powernv_hwrng_present(); | |
178 | } | |
179 | EXPORT_SYMBOL_GPL(kvmppc_hwrng_present); | |
180 | ||
181 | long kvmppc_h_random(struct kvm_vcpu *vcpu) | |
182 | { | |
183 | if (powernv_get_random_real_mode(&vcpu->arch.gpr[4])) | |
184 | return H_SUCCESS; | |
185 | ||
186 | return H_HARDWARE; | |
187 | } | |
eddb60fb PM |
188 | |
189 | static inline void rm_writeb(unsigned long paddr, u8 val) | |
190 | { | |
191 | __asm__ __volatile__("stbcix %0,0,%1" | |
192 | : : "r" (val), "r" (paddr) : "memory"); | |
193 | } | |
194 | ||
195 | /* | |
196 | * Send an interrupt to another CPU. | |
197 | * This can only be called in real mode. | |
198 | * The caller needs to include any barrier needed to order writes | |
199 | * to memory vs. the IPI/message. | |
200 | */ | |
201 | void kvmhv_rm_send_ipi(int cpu) | |
202 | { | |
203 | unsigned long xics_phys; | |
204 | ||
205 | /* Poke the target */ | |
206 | xics_phys = paca[cpu].kvm_hstate.xics_phys; | |
207 | rm_writeb(xics_phys + XICS_MFRR, IPI_PRIORITY); | |
208 | } | |
209 | ||
210 | /* | |
211 | * The following functions are called from the assembly code | |
212 | * in book3s_hv_rmhandlers.S. | |
213 | */ | |
214 | static void kvmhv_interrupt_vcore(struct kvmppc_vcore *vc, int active) | |
215 | { | |
216 | int cpu = vc->pcpu; | |
217 | ||
218 | /* Order setting of exit map vs. msgsnd/IPI */ | |
219 | smp_mb(); | |
220 | for (; active; active >>= 1, ++cpu) | |
221 | if (active & 1) | |
222 | kvmhv_rm_send_ipi(cpu); | |
223 | } | |
224 | ||
225 | void kvmhv_commence_exit(int trap) | |
226 | { | |
227 | struct kvmppc_vcore *vc = local_paca->kvm_hstate.kvm_vcore; | |
228 | int ptid = local_paca->kvm_hstate.ptid; | |
229 | int me, ee; | |
230 | ||
231 | /* Set our bit in the threads-exiting-guest map in the 0xff00 | |
232 | bits of vcore->entry_exit_map */ | |
233 | me = 0x100 << ptid; | |
234 | do { | |
235 | ee = vc->entry_exit_map; | |
236 | } while (cmpxchg(&vc->entry_exit_map, ee, ee | me) != ee); | |
237 | ||
238 | /* Are we the first here? */ | |
239 | if ((ee >> 8) != 0) | |
240 | return; | |
241 | ||
242 | /* | |
243 | * Trigger the other threads in this vcore to exit the guest. | |
244 | * If this is a hypervisor decrementer interrupt then they | |
245 | * will be already on their way out of the guest. | |
246 | */ | |
247 | if (trap != BOOK3S_INTERRUPT_HV_DECREMENTER) | |
248 | kvmhv_interrupt_vcore(vc, ee & ~(1 << ptid)); | |
249 | } |