book3s_hv_builtin.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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/asm-prototypes.h>
  20. #include <asm/cputable.h>
  21. #include <asm/kvm_ppc.h>
  22. #include <asm/kvm_book3s.h>
  23. #include <asm/archrandom.h>
  24. #include <asm/xics.h>
  25. #include <asm/xive.h>
  26. #include <asm/dbell.h>
  27. #include <asm/cputhreads.h>
  28. #include <asm/io.h>
  29. #include <asm/opal.h>
  30. #include <asm/smp.h>
  31. #define KVM_CMA_CHUNK_ORDER 18
  32. #include "book3s_xics.h"
  33. #include "book3s_xive.h"
  34. /*
  35. * The XIVE module will populate these when it loads
  36. */
  37. unsigned long (*__xive_vm_h_xirr)(struct kvm_vcpu *vcpu);
  38. unsigned long (*__xive_vm_h_ipoll)(struct kvm_vcpu *vcpu, unsigned long server);
  39. int (*__xive_vm_h_ipi)(struct kvm_vcpu *vcpu, unsigned long server,
  40. unsigned long mfrr);
  41. int (*__xive_vm_h_cppr)(struct kvm_vcpu *vcpu, unsigned long cppr);
  42. int (*__xive_vm_h_eoi)(struct kvm_vcpu *vcpu, unsigned long xirr);
  43. EXPORT_SYMBOL_GPL(__xive_vm_h_xirr);
  44. EXPORT_SYMBOL_GPL(__xive_vm_h_ipoll);
  45. EXPORT_SYMBOL_GPL(__xive_vm_h_ipi);
  46. EXPORT_SYMBOL_GPL(__xive_vm_h_cppr);
  47. EXPORT_SYMBOL_GPL(__xive_vm_h_eoi);
  48. /*
  49. * Hash page table alignment on newer cpus(CPU_FTR_ARCH_206)
  50. * should be power of 2.
  51. */
  52. #define HPT_ALIGN_PAGES ((1 << 18) >> PAGE_SHIFT) /* 256k */
  53. /*
  54. * By default we reserve 5% of memory for hash pagetable allocation.
  55. */
  56. static unsigned long kvm_cma_resv_ratio = 5;
  57. static struct cma *kvm_cma;
  58. static int __init early_parse_kvm_cma_resv(char *p)
  59. {
  60. pr_debug("%s(%s)\n", __func__, p);
  61. if (!p)
  62. return -EINVAL;
  63. return kstrtoul(p, 0, &kvm_cma_resv_ratio);
  64. }
  65. early_param("kvm_cma_resv_ratio", early_parse_kvm_cma_resv);
  66. struct page *kvm_alloc_hpt_cma(unsigned long nr_pages)
  67. {
  68. VM_BUG_ON(order_base_2(nr_pages) < KVM_CMA_CHUNK_ORDER - PAGE_SHIFT);
  69. return cma_alloc(kvm_cma, nr_pages, order_base_2(HPT_ALIGN_PAGES),
  70. false);
  71. }
  72. EXPORT_SYMBOL_GPL(kvm_alloc_hpt_cma);
  73. void kvm_free_hpt_cma(struct page *page, unsigned long nr_pages)
  74. {
  75. cma_release(kvm_cma, page, nr_pages);
  76. }
  77. EXPORT_SYMBOL_GPL(kvm_free_hpt_cma);
  78. /**
  79. * kvm_cma_reserve() - reserve area for kvm hash pagetable
  80. *
  81. * This function reserves memory from early allocator. It should be
  82. * called by arch specific code once the memblock allocator
  83. * has been activated and all other subsystems have already allocated/reserved
  84. * memory.
  85. */
  86. void __init kvm_cma_reserve(void)
  87. {
  88. unsigned long align_size;
  89. struct memblock_region *reg;
  90. phys_addr_t selected_size = 0;
  91. /*
  92. * We need CMA reservation only when we are in HV mode
  93. */
  94. if (!cpu_has_feature(CPU_FTR_HVMODE))
  95. return;
  96. /*
  97. * We cannot use memblock_phys_mem_size() here, because
  98. * memblock_analyze() has not been called yet.
  99. */
  100. for_each_memblock(memory, reg)
  101. selected_size += memblock_region_memory_end_pfn(reg) -
  102. memblock_region_memory_base_pfn(reg);
  103. selected_size = (selected_size * kvm_cma_resv_ratio / 100) << PAGE_SHIFT;
  104. if (selected_size) {
  105. pr_debug("%s: reserving %ld MiB for global area\n", __func__,
  106. (unsigned long)selected_size / SZ_1M);
  107. align_size = HPT_ALIGN_PAGES << PAGE_SHIFT;
  108. cma_declare_contiguous(0, selected_size, 0, align_size,
  109. KVM_CMA_CHUNK_ORDER - PAGE_SHIFT, false, "kvm_cma",
  110. &kvm_cma);
  111. }
  112. }
  113. /*
  114. * Real-mode H_CONFER implementation.
  115. * We check if we are the only vcpu out of this virtual core
  116. * still running in the guest and not ceded. If so, we pop up
  117. * to the virtual-mode implementation; if not, just return to
  118. * the guest.
  119. */
  120. long int kvmppc_rm_h_confer(struct kvm_vcpu *vcpu, int target,
  121. unsigned int yield_count)
  122. {
  123. struct kvmppc_vcore *vc = local_paca->kvm_hstate.kvm_vcore;
  124. int ptid = local_paca->kvm_hstate.ptid;
  125. int threads_running;
  126. int threads_ceded;
  127. int threads_conferring;
  128. u64 stop = get_tb() + 10 * tb_ticks_per_usec;
  129. int rv = H_SUCCESS; /* => don't yield */
  130. set_bit(ptid, &vc->conferring_threads);
  131. while ((get_tb() < stop) && !VCORE_IS_EXITING(vc)) {
  132. threads_running = VCORE_ENTRY_MAP(vc);
  133. threads_ceded = vc->napping_threads;
  134. threads_conferring = vc->conferring_threads;
  135. if ((threads_ceded | threads_conferring) == threads_running) {
  136. rv = H_TOO_HARD; /* => do yield */
  137. break;
  138. }
  139. }
  140. clear_bit(ptid, &vc->conferring_threads);
  141. return rv;
  142. }
  143. /*
  144. * When running HV mode KVM we need to block certain operations while KVM VMs
  145. * exist in the system. We use a counter of VMs to track this.
  146. *
  147. * One of the operations we need to block is onlining of secondaries, so we
  148. * protect hv_vm_count with get/put_online_cpus().
  149. */
  150. static atomic_t hv_vm_count;
  151. void kvm_hv_vm_activated(void)
  152. {
  153. get_online_cpus();
  154. atomic_inc(&hv_vm_count);
  155. put_online_cpus();
  156. }
  157. EXPORT_SYMBOL_GPL(kvm_hv_vm_activated);
  158. void kvm_hv_vm_deactivated(void)
  159. {
  160. get_online_cpus();
  161. atomic_dec(&hv_vm_count);
  162. put_online_cpus();
  163. }
  164. EXPORT_SYMBOL_GPL(kvm_hv_vm_deactivated);
  165. bool kvm_hv_mode_active(void)
  166. {
  167. return atomic_read(&hv_vm_count) != 0;
  168. }
  169. extern int hcall_real_table[], hcall_real_table_end[];
  170. int kvmppc_hcall_impl_hv_realmode(unsigned long cmd)
  171. {
  172. cmd /= 4;
  173. if (cmd < hcall_real_table_end - hcall_real_table &&
  174. hcall_real_table[cmd])
  175. return 1;
  176. return 0;
  177. }
  178. EXPORT_SYMBOL_GPL(kvmppc_hcall_impl_hv_realmode);
  179. int kvmppc_hwrng_present(void)
  180. {
  181. return powernv_hwrng_present();
  182. }
  183. EXPORT_SYMBOL_GPL(kvmppc_hwrng_present);
  184. long kvmppc_h_random(struct kvm_vcpu *vcpu)
  185. {
  186. int r;
  187. /* Only need to do the expensive mfmsr() on radix */
  188. if (kvm_is_radix(vcpu->kvm) && (mfmsr() & MSR_IR))
  189. r = powernv_get_random_long(&vcpu->arch.regs.gpr[4]);
  190. else
  191. r = powernv_get_random_real_mode(&vcpu->arch.regs.gpr[4]);
  192. if (r)
  193. return H_SUCCESS;
  194. return H_HARDWARE;
  195. }
  196. /*
  197. * Send an interrupt or message to another CPU.
  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. void __iomem *xics_phys;
  204. unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
  205. /* On POWER9 we can use msgsnd for any destination cpu. */
  206. if (cpu_has_feature(CPU_FTR_ARCH_300)) {
  207. msg |= get_hard_smp_processor_id(cpu);
  208. __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
  209. return;
  210. }
  211. /* On POWER8 for IPIs to threads in the same core, use msgsnd. */
  212. if (cpu_has_feature(CPU_FTR_ARCH_207S) &&
  213. cpu_first_thread_sibling(cpu) ==
  214. cpu_first_thread_sibling(raw_smp_processor_id())) {
  215. msg |= cpu_thread_in_core(cpu);
  216. __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
  217. return;
  218. }
  219. /* We should never reach this */
  220. if (WARN_ON_ONCE(xive_enabled()))
  221. return;
  222. /* Else poke the target with an IPI */
  223. xics_phys = paca_ptrs[cpu]->kvm_hstate.xics_phys;
  224. if (xics_phys)
  225. __raw_rm_writeb(IPI_PRIORITY, xics_phys + XICS_MFRR);
  226. else
  227. opal_int_set_mfrr(get_hard_smp_processor_id(cpu), IPI_PRIORITY);
  228. }
  229. /*
  230. * The following functions are called from the assembly code
  231. * in book3s_hv_rmhandlers.S.
  232. */
  233. static void kvmhv_interrupt_vcore(struct kvmppc_vcore *vc, int active)
  234. {
  235. int cpu = vc->pcpu;
  236. /* Order setting of exit map vs. msgsnd/IPI */
  237. smp_mb();
  238. for (; active; active >>= 1, ++cpu)
  239. if (active & 1)
  240. kvmhv_rm_send_ipi(cpu);
  241. }
  242. void kvmhv_commence_exit(int trap)
  243. {
  244. struct kvmppc_vcore *vc = local_paca->kvm_hstate.kvm_vcore;
  245. int ptid = local_paca->kvm_hstate.ptid;
  246. struct kvm_split_mode *sip = local_paca->kvm_hstate.kvm_split_mode;
  247. int me, ee, i, t;
  248. int cpu0;
  249. /* Set our bit in the threads-exiting-guest map in the 0xff00
  250. bits of vcore->entry_exit_map */
  251. me = 0x100 << ptid;
  252. do {
  253. ee = vc->entry_exit_map;
  254. } while (cmpxchg(&vc->entry_exit_map, ee, ee | me) != ee);
  255. /* Are we the first here? */
  256. if ((ee >> 8) != 0)
  257. return;
  258. /*
  259. * Trigger the other threads in this vcore to exit the guest.
  260. * If this is a hypervisor decrementer interrupt then they
  261. * will be already on their way out of the guest.
  262. */
  263. if (trap != BOOK3S_INTERRUPT_HV_DECREMENTER)
  264. kvmhv_interrupt_vcore(vc, ee & ~(1 << ptid));
  265. /*
  266. * If we are doing dynamic micro-threading, interrupt the other
  267. * subcores to pull them out of their guests too.
  268. */
  269. if (!sip)
  270. return;
  271. for (i = 0; i < MAX_SUBCORES; ++i) {
  272. vc = sip->vc[i];
  273. if (!vc)
  274. break;
  275. do {
  276. ee = vc->entry_exit_map;
  277. /* Already asked to exit? */
  278. if ((ee >> 8) != 0)
  279. break;
  280. } while (cmpxchg(&vc->entry_exit_map, ee,
  281. ee | VCORE_EXIT_REQ) != ee);
  282. if ((ee >> 8) == 0)
  283. kvmhv_interrupt_vcore(vc, ee);
  284. }
  285. /*
  286. * On POWER9 when running a HPT guest on a radix host (sip != NULL),
  287. * we have to interrupt inactive CPU threads to get them to
  288. * restore the host LPCR value.
  289. */
  290. if (sip->lpcr_req) {
  291. if (cmpxchg(&sip->do_restore, 0, 1) == 0) {
  292. vc = local_paca->kvm_hstate.kvm_vcore;
  293. cpu0 = vc->pcpu + ptid - local_paca->kvm_hstate.tid;
  294. for (t = 1; t < threads_per_core; ++t) {
  295. if (sip->napped[t])
  296. kvmhv_rm_send_ipi(cpu0 + t);
  297. }
  298. }
  299. }
  300. }
  301. struct kvmppc_host_rm_ops *kvmppc_host_rm_ops_hv;
  302. EXPORT_SYMBOL_GPL(kvmppc_host_rm_ops_hv);
  303. #ifdef CONFIG_KVM_XICS
  304. static struct kvmppc_irq_map *get_irqmap(struct kvmppc_passthru_irqmap *pimap,
  305. u32 xisr)
  306. {
  307. int i;
  308. /*
  309. * We access the mapped array here without a lock. That
  310. * is safe because we never reduce the number of entries
  311. * in the array and we never change the v_hwirq field of
  312. * an entry once it is set.
  313. *
  314. * We have also carefully ordered the stores in the writer
  315. * and the loads here in the reader, so that if we find a matching
  316. * hwirq here, the associated GSI and irq_desc fields are valid.
  317. */
  318. for (i = 0; i < pimap->n_mapped; i++) {
  319. if (xisr == pimap->mapped[i].r_hwirq) {
  320. /*
  321. * Order subsequent reads in the caller to serialize
  322. * with the writer.
  323. */
  324. smp_rmb();
  325. return &pimap->mapped[i];
  326. }
  327. }
  328. return NULL;
  329. }
  330. /*
  331. * If we have an interrupt that's not an IPI, check if we have a
  332. * passthrough adapter and if so, check if this external interrupt
  333. * is for the adapter.
  334. * We will attempt to deliver the IRQ directly to the target VCPU's
  335. * ICP, the virtual ICP (based on affinity - the xive value in ICS).
  336. *
  337. * If the delivery fails or if this is not for a passthrough adapter,
  338. * return to the host to handle this interrupt. We earlier
  339. * saved a copy of the XIRR in the PACA, it will be picked up by
  340. * the host ICP driver.
  341. */
  342. static int kvmppc_check_passthru(u32 xisr, __be32 xirr, bool *again)
  343. {
  344. struct kvmppc_passthru_irqmap *pimap;
  345. struct kvmppc_irq_map *irq_map;
  346. struct kvm_vcpu *vcpu;
  347. vcpu = local_paca->kvm_hstate.kvm_vcpu;
  348. if (!vcpu)
  349. return 1;
  350. pimap = kvmppc_get_passthru_irqmap(vcpu->kvm);
  351. if (!pimap)
  352. return 1;
  353. irq_map = get_irqmap(pimap, xisr);
  354. if (!irq_map)
  355. return 1;
  356. /* We're handling this interrupt, generic code doesn't need to */
  357. local_paca->kvm_hstate.saved_xirr = 0;
  358. return kvmppc_deliver_irq_passthru(vcpu, xirr, irq_map, pimap, again);
  359. }
  360. #else
  361. static inline int kvmppc_check_passthru(u32 xisr, __be32 xirr, bool *again)
  362. {
  363. return 1;
  364. }
  365. #endif
  366. /*
  367. * Determine what sort of external interrupt is pending (if any).
  368. * Returns:
  369. * 0 if no interrupt is pending
  370. * 1 if an interrupt is pending that needs to be handled by the host
  371. * 2 Passthrough that needs completion in the host
  372. * -1 if there was a guest wakeup IPI (which has now been cleared)
  373. * -2 if there is PCI passthrough external interrupt that was handled
  374. */
  375. static long kvmppc_read_one_intr(bool *again);
  376. long kvmppc_read_intr(void)
  377. {
  378. long ret = 0;
  379. long rc;
  380. bool again;
  381. if (xive_enabled())
  382. return 1;
  383. do {
  384. again = false;
  385. rc = kvmppc_read_one_intr(&again);
  386. if (rc && (ret == 0 || rc > ret))
  387. ret = rc;
  388. } while (again);
  389. return ret;
  390. }
  391. static long kvmppc_read_one_intr(bool *again)
  392. {
  393. void __iomem *xics_phys;
  394. u32 h_xirr;
  395. __be32 xirr;
  396. u32 xisr;
  397. u8 host_ipi;
  398. int64_t rc;
  399. if (xive_enabled())
  400. return 1;
  401. /* see if a host IPI is pending */
  402. host_ipi = local_paca->kvm_hstate.host_ipi;
  403. if (host_ipi)
  404. return 1;
  405. /* Now read the interrupt from the ICP */
  406. xics_phys = local_paca->kvm_hstate.xics_phys;
  407. rc = 0;
  408. if (!xics_phys)
  409. rc = opal_int_get_xirr(&xirr, false);
  410. else
  411. xirr = __raw_rm_readl(xics_phys + XICS_XIRR);
  412. if (rc < 0)
  413. return 1;
  414. /*
  415. * Save XIRR for later. Since we get control in reverse endian
  416. * on LE systems, save it byte reversed and fetch it back in
  417. * host endian. Note that xirr is the value read from the
  418. * XIRR register, while h_xirr is the host endian version.
  419. */
  420. h_xirr = be32_to_cpu(xirr);
  421. local_paca->kvm_hstate.saved_xirr = h_xirr;
  422. xisr = h_xirr & 0xffffff;
  423. /*
  424. * Ensure that the store/load complete to guarantee all side
  425. * effects of loading from XIRR has completed
  426. */
  427. smp_mb();
  428. /* if nothing pending in the ICP */
  429. if (!xisr)
  430. return 0;
  431. /* We found something in the ICP...
  432. *
  433. * If it is an IPI, clear the MFRR and EOI it.
  434. */
  435. if (xisr == XICS_IPI) {
  436. rc = 0;
  437. if (xics_phys) {
  438. __raw_rm_writeb(0xff, xics_phys + XICS_MFRR);
  439. __raw_rm_writel(xirr, xics_phys + XICS_XIRR);
  440. } else {
  441. opal_int_set_mfrr(hard_smp_processor_id(), 0xff);
  442. rc = opal_int_eoi(h_xirr);
  443. }
  444. /* If rc > 0, there is another interrupt pending */
  445. *again = rc > 0;
  446. /*
  447. * Need to ensure side effects of above stores
  448. * complete before proceeding.
  449. */
  450. smp_mb();
  451. /*
  452. * We need to re-check host IPI now in case it got set in the
  453. * meantime. If it's clear, we bounce the interrupt to the
  454. * guest
  455. */
  456. host_ipi = local_paca->kvm_hstate.host_ipi;
  457. if (unlikely(host_ipi != 0)) {
  458. /* We raced with the host,
  459. * we need to resend that IPI, bummer
  460. */
  461. if (xics_phys)
  462. __raw_rm_writeb(IPI_PRIORITY,
  463. xics_phys + XICS_MFRR);
  464. else
  465. opal_int_set_mfrr(hard_smp_processor_id(),
  466. IPI_PRIORITY);
  467. /* Let side effects complete */
  468. smp_mb();
  469. return 1;
  470. }
  471. /* OK, it's an IPI for us */
  472. local_paca->kvm_hstate.saved_xirr = 0;
  473. return -1;
  474. }
  475. return kvmppc_check_passthru(xisr, xirr, again);
  476. }
  477. #ifdef CONFIG_KVM_XICS
  478. static inline bool is_rm(void)
  479. {
  480. return !(mfmsr() & MSR_DR);
  481. }
  482. unsigned long kvmppc_rm_h_xirr(struct kvm_vcpu *vcpu)
  483. {
  484. if (!kvmppc_xics_enabled(vcpu))
  485. return H_TOO_HARD;
  486. if (xive_enabled()) {
  487. if (is_rm())
  488. return xive_rm_h_xirr(vcpu);
  489. if (unlikely(!__xive_vm_h_xirr))
  490. return H_NOT_AVAILABLE;
  491. return __xive_vm_h_xirr(vcpu);
  492. } else
  493. return xics_rm_h_xirr(vcpu);
  494. }
  495. unsigned long kvmppc_rm_h_xirr_x(struct kvm_vcpu *vcpu)
  496. {
  497. if (!kvmppc_xics_enabled(vcpu))
  498. return H_TOO_HARD;
  499. vcpu->arch.regs.gpr[5] = get_tb();
  500. if (xive_enabled()) {
  501. if (is_rm())
  502. return xive_rm_h_xirr(vcpu);
  503. if (unlikely(!__xive_vm_h_xirr))
  504. return H_NOT_AVAILABLE;
  505. return __xive_vm_h_xirr(vcpu);
  506. } else
  507. return xics_rm_h_xirr(vcpu);
  508. }
  509. unsigned long kvmppc_rm_h_ipoll(struct kvm_vcpu *vcpu, unsigned long server)
  510. {
  511. if (!kvmppc_xics_enabled(vcpu))
  512. return H_TOO_HARD;
  513. if (xive_enabled()) {
  514. if (is_rm())
  515. return xive_rm_h_ipoll(vcpu, server);
  516. if (unlikely(!__xive_vm_h_ipoll))
  517. return H_NOT_AVAILABLE;
  518. return __xive_vm_h_ipoll(vcpu, server);
  519. } else
  520. return H_TOO_HARD;
  521. }
  522. int kvmppc_rm_h_ipi(struct kvm_vcpu *vcpu, unsigned long server,
  523. unsigned long mfrr)
  524. {
  525. if (!kvmppc_xics_enabled(vcpu))
  526. return H_TOO_HARD;
  527. if (xive_enabled()) {
  528. if (is_rm())
  529. return xive_rm_h_ipi(vcpu, server, mfrr);
  530. if (unlikely(!__xive_vm_h_ipi))
  531. return H_NOT_AVAILABLE;
  532. return __xive_vm_h_ipi(vcpu, server, mfrr);
  533. } else
  534. return xics_rm_h_ipi(vcpu, server, mfrr);
  535. }
  536. int kvmppc_rm_h_cppr(struct kvm_vcpu *vcpu, unsigned long cppr)
  537. {
  538. if (!kvmppc_xics_enabled(vcpu))
  539. return H_TOO_HARD;
  540. if (xive_enabled()) {
  541. if (is_rm())
  542. return xive_rm_h_cppr(vcpu, cppr);
  543. if (unlikely(!__xive_vm_h_cppr))
  544. return H_NOT_AVAILABLE;
  545. return __xive_vm_h_cppr(vcpu, cppr);
  546. } else
  547. return xics_rm_h_cppr(vcpu, cppr);
  548. }
  549. int kvmppc_rm_h_eoi(struct kvm_vcpu *vcpu, unsigned long xirr)
  550. {
  551. if (!kvmppc_xics_enabled(vcpu))
  552. return H_TOO_HARD;
  553. if (xive_enabled()) {
  554. if (is_rm())
  555. return xive_rm_h_eoi(vcpu, xirr);
  556. if (unlikely(!__xive_vm_h_eoi))
  557. return H_NOT_AVAILABLE;
  558. return __xive_vm_h_eoi(vcpu, xirr);
  559. } else
  560. return xics_rm_h_eoi(vcpu, xirr);
  561. }
  562. #endif /* CONFIG_KVM_XICS */
  563. void kvmppc_bad_interrupt(struct pt_regs *regs)
  564. {
  565. /*
  566. * 100 could happen at any time, 200 can happen due to invalid real
  567. * address access for example (or any time due to a hardware problem).
  568. */
  569. if (TRAP(regs) == 0x100) {
  570. get_paca()->in_nmi++;
  571. system_reset_exception(regs);
  572. get_paca()->in_nmi--;
  573. } else if (TRAP(regs) == 0x200) {
  574. machine_check_exception(regs);
  575. } else {
  576. die("Bad interrupt in KVM entry/exit code", regs, SIGABRT);
  577. }
  578. panic("Bad KVM trap");
  579. }
  580. /*
  581. * Functions used to switch LPCR HR and UPRT bits on all threads
  582. * when entering and exiting HPT guests on a radix host.
  583. */
  584. #define PHASE_REALMODE 1 /* in real mode */
  585. #define PHASE_SET_LPCR 2 /* have set LPCR */
  586. #define PHASE_OUT_OF_GUEST 4 /* have finished executing in guest */
  587. #define PHASE_RESET_LPCR 8 /* have reset LPCR to host value */
  588. #define ALL(p) (((p) << 24) | ((p) << 16) | ((p) << 8) | (p))
  589. static void wait_for_sync(struct kvm_split_mode *sip, int phase)
  590. {
  591. int thr = local_paca->kvm_hstate.tid;
  592. sip->lpcr_sync.phase[thr] |= phase;
  593. phase = ALL(phase);
  594. while ((sip->lpcr_sync.allphases & phase) != phase) {
  595. HMT_low();
  596. barrier();
  597. }
  598. HMT_medium();
  599. }
  600. void kvmhv_p9_set_lpcr(struct kvm_split_mode *sip)
  601. {
  602. unsigned long rb, set;
  603. /* wait for every other thread to get to real mode */
  604. wait_for_sync(sip, PHASE_REALMODE);
  605. /* Set LPCR and LPIDR */
  606. mtspr(SPRN_LPCR, sip->lpcr_req);
  607. mtspr(SPRN_LPID, sip->lpidr_req);
  608. isync();
  609. /* Invalidate the TLB on thread 0 */
  610. if (local_paca->kvm_hstate.tid == 0) {
  611. sip->do_set = 0;
  612. asm volatile("ptesync" : : : "memory");
  613. for (set = 0; set < POWER9_TLB_SETS_RADIX; ++set) {
  614. rb = TLBIEL_INVAL_SET_LPID +
  615. (set << TLBIEL_INVAL_SET_SHIFT);
  616. asm volatile(PPC_TLBIEL(%0, %1, 0, 0, 0) : :
  617. "r" (rb), "r" (0));
  618. }
  619. asm volatile("ptesync" : : : "memory");
  620. }
  621. /* indicate that we have done so and wait for others */
  622. wait_for_sync(sip, PHASE_SET_LPCR);
  623. /* order read of sip->lpcr_sync.allphases vs. sip->do_set */
  624. smp_rmb();
  625. }
  626. /*
  627. * Called when a thread that has been in the guest needs
  628. * to reload the host LPCR value - but only on POWER9 when
  629. * running a HPT guest on a radix host.
  630. */
  631. void kvmhv_p9_restore_lpcr(struct kvm_split_mode *sip)
  632. {
  633. /* we're out of the guest... */
  634. wait_for_sync(sip, PHASE_OUT_OF_GUEST);
  635. mtspr(SPRN_LPID, 0);
  636. mtspr(SPRN_LPCR, sip->host_lpcr);
  637. isync();
  638. if (local_paca->kvm_hstate.tid == 0) {
  639. sip->do_restore = 0;
  640. smp_wmb(); /* order store of do_restore vs. phase */
  641. }
  642. wait_for_sync(sip, PHASE_RESET_LPCR);
  643. smp_mb();
  644. local_paca->kvm_hstate.kvm_split_mode = NULL;
  645. }