book3s_hv_builtin.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. #include <linux/cpu.h>
  9. #include <linux/kvm_host.h>
  10. #include <linux/preempt.h>
  11. #include <linux/export.h>
  12. #include <linux/sched.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/init.h>
  15. #include <linux/memblock.h>
  16. #include <linux/sizes.h>
  17. #include <linux/cma.h>
  18. #include <linux/bitops.h>
  19. #include <asm/cputable.h>
  20. #include <asm/kvm_ppc.h>
  21. #include <asm/kvm_book3s.h>
  22. #include <asm/archrandom.h>
  23. #include <asm/xics.h>
  24. #include <asm/xive.h>
  25. #include <asm/dbell.h>
  26. #include <asm/cputhreads.h>
  27. #include <asm/io.h>
  28. #include <asm/opal.h>
  29. #include <asm/smp.h>
  30. #define KVM_CMA_CHUNK_ORDER 18
  31. /*
  32. * Hash page table alignment on newer cpus(CPU_FTR_ARCH_206)
  33. * should be power of 2.
  34. */
  35. #define HPT_ALIGN_PAGES ((1 << 18) >> PAGE_SHIFT) /* 256k */
  36. /*
  37. * By default we reserve 5% of memory for hash pagetable allocation.
  38. */
  39. static unsigned long kvm_cma_resv_ratio = 5;
  40. static struct cma *kvm_cma;
  41. static int __init early_parse_kvm_cma_resv(char *p)
  42. {
  43. pr_debug("%s(%s)\n", __func__, p);
  44. if (!p)
  45. return -EINVAL;
  46. return kstrtoul(p, 0, &kvm_cma_resv_ratio);
  47. }
  48. early_param("kvm_cma_resv_ratio", early_parse_kvm_cma_resv);
  49. struct page *kvm_alloc_hpt_cma(unsigned long nr_pages)
  50. {
  51. VM_BUG_ON(order_base_2(nr_pages) < KVM_CMA_CHUNK_ORDER - PAGE_SHIFT);
  52. return cma_alloc(kvm_cma, nr_pages, order_base_2(HPT_ALIGN_PAGES),
  53. GFP_KERNEL);
  54. }
  55. EXPORT_SYMBOL_GPL(kvm_alloc_hpt_cma);
  56. void kvm_free_hpt_cma(struct page *page, unsigned long nr_pages)
  57. {
  58. cma_release(kvm_cma, page, nr_pages);
  59. }
  60. EXPORT_SYMBOL_GPL(kvm_free_hpt_cma);
  61. /**
  62. * kvm_cma_reserve() - reserve area for kvm hash pagetable
  63. *
  64. * This function reserves memory from early allocator. It should be
  65. * called by arch specific code once the memblock allocator
  66. * has been activated and all other subsystems have already allocated/reserved
  67. * memory.
  68. */
  69. void __init kvm_cma_reserve(void)
  70. {
  71. unsigned long align_size;
  72. struct memblock_region *reg;
  73. phys_addr_t selected_size = 0;
  74. /*
  75. * We need CMA reservation only when we are in HV mode
  76. */
  77. if (!cpu_has_feature(CPU_FTR_HVMODE))
  78. return;
  79. /*
  80. * We cannot use memblock_phys_mem_size() here, because
  81. * memblock_analyze() has not been called yet.
  82. */
  83. for_each_memblock(memory, reg)
  84. selected_size += memblock_region_memory_end_pfn(reg) -
  85. memblock_region_memory_base_pfn(reg);
  86. selected_size = (selected_size * kvm_cma_resv_ratio / 100) << PAGE_SHIFT;
  87. if (selected_size) {
  88. pr_debug("%s: reserving %ld MiB for global area\n", __func__,
  89. (unsigned long)selected_size / SZ_1M);
  90. align_size = HPT_ALIGN_PAGES << PAGE_SHIFT;
  91. cma_declare_contiguous(0, selected_size, 0, align_size,
  92. KVM_CMA_CHUNK_ORDER - PAGE_SHIFT, false, "kvm_cma",
  93. &kvm_cma);
  94. }
  95. }
  96. /*
  97. * Real-mode H_CONFER implementation.
  98. * We check if we are the only vcpu out of this virtual core
  99. * still running in the guest and not ceded. If so, we pop up
  100. * to the virtual-mode implementation; if not, just return to
  101. * the guest.
  102. */
  103. long int kvmppc_rm_h_confer(struct kvm_vcpu *vcpu, int target,
  104. unsigned int yield_count)
  105. {
  106. struct kvmppc_vcore *vc = local_paca->kvm_hstate.kvm_vcore;
  107. int ptid = local_paca->kvm_hstate.ptid;
  108. int threads_running;
  109. int threads_ceded;
  110. int threads_conferring;
  111. u64 stop = get_tb() + 10 * tb_ticks_per_usec;
  112. int rv = H_SUCCESS; /* => don't yield */
  113. set_bit(ptid, &vc->conferring_threads);
  114. while ((get_tb() < stop) && !VCORE_IS_EXITING(vc)) {
  115. threads_running = VCORE_ENTRY_MAP(vc);
  116. threads_ceded = vc->napping_threads;
  117. threads_conferring = vc->conferring_threads;
  118. if ((threads_ceded | threads_conferring) == threads_running) {
  119. rv = H_TOO_HARD; /* => do yield */
  120. break;
  121. }
  122. }
  123. clear_bit(ptid, &vc->conferring_threads);
  124. return rv;
  125. }
  126. /*
  127. * When running HV mode KVM we need to block certain operations while KVM VMs
  128. * exist in the system. We use a counter of VMs to track this.
  129. *
  130. * One of the operations we need to block is onlining of secondaries, so we
  131. * protect hv_vm_count with get/put_online_cpus().
  132. */
  133. static atomic_t hv_vm_count;
  134. void kvm_hv_vm_activated(void)
  135. {
  136. get_online_cpus();
  137. atomic_inc(&hv_vm_count);
  138. put_online_cpus();
  139. }
  140. EXPORT_SYMBOL_GPL(kvm_hv_vm_activated);
  141. void kvm_hv_vm_deactivated(void)
  142. {
  143. get_online_cpus();
  144. atomic_dec(&hv_vm_count);
  145. put_online_cpus();
  146. }
  147. EXPORT_SYMBOL_GPL(kvm_hv_vm_deactivated);
  148. bool kvm_hv_mode_active(void)
  149. {
  150. return atomic_read(&hv_vm_count) != 0;
  151. }
  152. extern int hcall_real_table[], hcall_real_table_end[];
  153. int kvmppc_hcall_impl_hv_realmode(unsigned long cmd)
  154. {
  155. cmd /= 4;
  156. if (cmd < hcall_real_table_end - hcall_real_table &&
  157. hcall_real_table[cmd])
  158. return 1;
  159. return 0;
  160. }
  161. EXPORT_SYMBOL_GPL(kvmppc_hcall_impl_hv_realmode);
  162. int kvmppc_hwrng_present(void)
  163. {
  164. return powernv_hwrng_present();
  165. }
  166. EXPORT_SYMBOL_GPL(kvmppc_hwrng_present);
  167. long kvmppc_h_random(struct kvm_vcpu *vcpu)
  168. {
  169. if (powernv_get_random_real_mode(&vcpu->arch.gpr[4]))
  170. return H_SUCCESS;
  171. return H_HARDWARE;
  172. }
  173. /*
  174. * Send an interrupt or message to another CPU.
  175. * The caller needs to include any barrier needed to order writes
  176. * to memory vs. the IPI/message.
  177. */
  178. void kvmhv_rm_send_ipi(int cpu)
  179. {
  180. void __iomem *xics_phys;
  181. unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
  182. /* On POWER9 we can use msgsnd for any destination cpu. */
  183. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  184. msg |= get_hard_smp_processor_id(cpu);
  185. __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
  186. return;
  187. }
  188. /* On POWER8 for IPIs to threads in the same core, use msgsnd. */
  189. if (cpu_has_feature(CPU_FTR_ARCH_207S) &&
  190. cpu_first_thread_sibling(cpu) ==
  191. cpu_first_thread_sibling(raw_smp_processor_id())) {
  192. msg |= cpu_thread_in_core(cpu);
  193. __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
  194. return;
  195. }
  196. /* We should never reach this */
  197. if (WARN_ON_ONCE(xive_enabled()))
  198. return;
  199. /* Else poke the target with an IPI */
  200. xics_phys = paca[cpu].kvm_hstate.xics_phys;
  201. if (xics_phys)
  202. __raw_rm_writeb(IPI_PRIORITY, xics_phys + XICS_MFRR);
  203. else
  204. opal_int_set_mfrr(get_hard_smp_processor_id(cpu), IPI_PRIORITY);
  205. }
  206. /*
  207. * The following functions are called from the assembly code
  208. * in book3s_hv_rmhandlers.S.
  209. */
  210. static void kvmhv_interrupt_vcore(struct kvmppc_vcore *vc, int active)
  211. {
  212. int cpu = vc->pcpu;
  213. /* Order setting of exit map vs. msgsnd/IPI */
  214. smp_mb();
  215. for (; active; active >>= 1, ++cpu)
  216. if (active & 1)
  217. kvmhv_rm_send_ipi(cpu);
  218. }
  219. void kvmhv_commence_exit(int trap)
  220. {
  221. struct kvmppc_vcore *vc = local_paca->kvm_hstate.kvm_vcore;
  222. int ptid = local_paca->kvm_hstate.ptid;
  223. struct kvm_split_mode *sip = local_paca->kvm_hstate.kvm_split_mode;
  224. int me, ee, i;
  225. /* Set our bit in the threads-exiting-guest map in the 0xff00
  226. bits of vcore->entry_exit_map */
  227. me = 0x100 << ptid;
  228. do {
  229. ee = vc->entry_exit_map;
  230. } while (cmpxchg(&vc->entry_exit_map, ee, ee | me) != ee);
  231. /* Are we the first here? */
  232. if ((ee >> 8) != 0)
  233. return;
  234. /*
  235. * Trigger the other threads in this vcore to exit the guest.
  236. * If this is a hypervisor decrementer interrupt then they
  237. * will be already on their way out of the guest.
  238. */
  239. if (trap != BOOK3S_INTERRUPT_HV_DECREMENTER)
  240. kvmhv_interrupt_vcore(vc, ee & ~(1 << ptid));
  241. /*
  242. * If we are doing dynamic micro-threading, interrupt the other
  243. * subcores to pull them out of their guests too.
  244. */
  245. if (!sip)
  246. return;
  247. for (i = 0; i < MAX_SUBCORES; ++i) {
  248. vc = sip->master_vcs[i];
  249. if (!vc)
  250. break;
  251. do {
  252. ee = vc->entry_exit_map;
  253. /* Already asked to exit? */
  254. if ((ee >> 8) != 0)
  255. break;
  256. } while (cmpxchg(&vc->entry_exit_map, ee,
  257. ee | VCORE_EXIT_REQ) != ee);
  258. if ((ee >> 8) == 0)
  259. kvmhv_interrupt_vcore(vc, ee);
  260. }
  261. }
  262. struct kvmppc_host_rm_ops *kvmppc_host_rm_ops_hv;
  263. EXPORT_SYMBOL_GPL(kvmppc_host_rm_ops_hv);
  264. #ifdef CONFIG_KVM_XICS
  265. static struct kvmppc_irq_map *get_irqmap(struct kvmppc_passthru_irqmap *pimap,
  266. u32 xisr)
  267. {
  268. int i;
  269. /*
  270. * We access the mapped array here without a lock. That
  271. * is safe because we never reduce the number of entries
  272. * in the array and we never change the v_hwirq field of
  273. * an entry once it is set.
  274. *
  275. * We have also carefully ordered the stores in the writer
  276. * and the loads here in the reader, so that if we find a matching
  277. * hwirq here, the associated GSI and irq_desc fields are valid.
  278. */
  279. for (i = 0; i < pimap->n_mapped; i++) {
  280. if (xisr == pimap->mapped[i].r_hwirq) {
  281. /*
  282. * Order subsequent reads in the caller to serialize
  283. * with the writer.
  284. */
  285. smp_rmb();
  286. return &pimap->mapped[i];
  287. }
  288. }
  289. return NULL;
  290. }
  291. /*
  292. * If we have an interrupt that's not an IPI, check if we have a
  293. * passthrough adapter and if so, check if this external interrupt
  294. * is for the adapter.
  295. * We will attempt to deliver the IRQ directly to the target VCPU's
  296. * ICP, the virtual ICP (based on affinity - the xive value in ICS).
  297. *
  298. * If the delivery fails or if this is not for a passthrough adapter,
  299. * return to the host to handle this interrupt. We earlier
  300. * saved a copy of the XIRR in the PACA, it will be picked up by
  301. * the host ICP driver.
  302. */
  303. static int kvmppc_check_passthru(u32 xisr, __be32 xirr, bool *again)
  304. {
  305. struct kvmppc_passthru_irqmap *pimap;
  306. struct kvmppc_irq_map *irq_map;
  307. struct kvm_vcpu *vcpu;
  308. vcpu = local_paca->kvm_hstate.kvm_vcpu;
  309. if (!vcpu)
  310. return 1;
  311. pimap = kvmppc_get_passthru_irqmap(vcpu->kvm);
  312. if (!pimap)
  313. return 1;
  314. irq_map = get_irqmap(pimap, xisr);
  315. if (!irq_map)
  316. return 1;
  317. /* We're handling this interrupt, generic code doesn't need to */
  318. local_paca->kvm_hstate.saved_xirr = 0;
  319. return kvmppc_deliver_irq_passthru(vcpu, xirr, irq_map, pimap, again);
  320. }
  321. #else
  322. static inline int kvmppc_check_passthru(u32 xisr, __be32 xirr, bool *again)
  323. {
  324. return 1;
  325. }
  326. #endif
  327. /*
  328. * Determine what sort of external interrupt is pending (if any).
  329. * Returns:
  330. * 0 if no interrupt is pending
  331. * 1 if an interrupt is pending that needs to be handled by the host
  332. * 2 Passthrough that needs completion in the host
  333. * -1 if there was a guest wakeup IPI (which has now been cleared)
  334. * -2 if there is PCI passthrough external interrupt that was handled
  335. */
  336. static long kvmppc_read_one_intr(bool *again);
  337. long kvmppc_read_intr(void)
  338. {
  339. long ret = 0;
  340. long rc;
  341. bool again;
  342. if (xive_enabled())
  343. return 1;
  344. do {
  345. again = false;
  346. rc = kvmppc_read_one_intr(&again);
  347. if (rc && (ret == 0 || rc > ret))
  348. ret = rc;
  349. } while (again);
  350. return ret;
  351. }
  352. static long kvmppc_read_one_intr(bool *again)
  353. {
  354. void __iomem *xics_phys;
  355. u32 h_xirr;
  356. __be32 xirr;
  357. u32 xisr;
  358. u8 host_ipi;
  359. int64_t rc;
  360. /* see if a host IPI is pending */
  361. host_ipi = local_paca->kvm_hstate.host_ipi;
  362. if (host_ipi)
  363. return 1;
  364. /* Now read the interrupt from the ICP */
  365. xics_phys = local_paca->kvm_hstate.xics_phys;
  366. rc = 0;
  367. if (!xics_phys)
  368. rc = opal_int_get_xirr(&xirr, false);
  369. else
  370. xirr = __raw_rm_readl(xics_phys + XICS_XIRR);
  371. if (rc < 0)
  372. return 1;
  373. /*
  374. * Save XIRR for later. Since we get control in reverse endian
  375. * on LE systems, save it byte reversed and fetch it back in
  376. * host endian. Note that xirr is the value read from the
  377. * XIRR register, while h_xirr is the host endian version.
  378. */
  379. h_xirr = be32_to_cpu(xirr);
  380. local_paca->kvm_hstate.saved_xirr = h_xirr;
  381. xisr = h_xirr & 0xffffff;
  382. /*
  383. * Ensure that the store/load complete to guarantee all side
  384. * effects of loading from XIRR has completed
  385. */
  386. smp_mb();
  387. /* if nothing pending in the ICP */
  388. if (!xisr)
  389. return 0;
  390. /* We found something in the ICP...
  391. *
  392. * If it is an IPI, clear the MFRR and EOI it.
  393. */
  394. if (xisr == XICS_IPI) {
  395. rc = 0;
  396. if (xics_phys) {
  397. __raw_rm_writeb(0xff, xics_phys + XICS_MFRR);
  398. __raw_rm_writel(xirr, xics_phys + XICS_XIRR);
  399. } else {
  400. opal_int_set_mfrr(hard_smp_processor_id(), 0xff);
  401. rc = opal_int_eoi(h_xirr);
  402. }
  403. /* If rc > 0, there is another interrupt pending */
  404. *again = rc > 0;
  405. /*
  406. * Need to ensure side effects of above stores
  407. * complete before proceeding.
  408. */
  409. smp_mb();
  410. /*
  411. * We need to re-check host IPI now in case it got set in the
  412. * meantime. If it's clear, we bounce the interrupt to the
  413. * guest
  414. */
  415. host_ipi = local_paca->kvm_hstate.host_ipi;
  416. if (unlikely(host_ipi != 0)) {
  417. /* We raced with the host,
  418. * we need to resend that IPI, bummer
  419. */
  420. if (xics_phys)
  421. __raw_rm_writeb(IPI_PRIORITY,
  422. xics_phys + XICS_MFRR);
  423. else
  424. opal_int_set_mfrr(hard_smp_processor_id(),
  425. IPI_PRIORITY);
  426. /* Let side effects complete */
  427. smp_mb();
  428. return 1;
  429. }
  430. /* OK, it's an IPI for us */
  431. local_paca->kvm_hstate.saved_xirr = 0;
  432. return -1;
  433. }
  434. return kvmppc_check_passthru(xisr, xirr, again);
  435. }