native.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /*
  2. * Copyright 2016,2017 IBM Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #define pr_fmt(fmt) "xive: " fmt
  10. #include <linux/types.h>
  11. #include <linux/irq.h>
  12. #include <linux/debugfs.h>
  13. #include <linux/smp.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/seq_file.h>
  16. #include <linux/init.h>
  17. #include <linux/of.h>
  18. #include <linux/slab.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/delay.h>
  21. #include <linux/cpumask.h>
  22. #include <linux/mm.h>
  23. #include <asm/prom.h>
  24. #include <asm/io.h>
  25. #include <asm/smp.h>
  26. #include <asm/irq.h>
  27. #include <asm/errno.h>
  28. #include <asm/xive.h>
  29. #include <asm/xive-regs.h>
  30. #include <asm/opal.h>
  31. #include <asm/kvm_ppc.h>
  32. #include "xive-internal.h"
  33. static u32 xive_provision_size;
  34. static u32 *xive_provision_chips;
  35. static u32 xive_provision_chip_count;
  36. static u32 xive_queue_shift;
  37. static u32 xive_pool_vps = XIVE_INVALID_VP;
  38. static struct kmem_cache *xive_provision_cache;
  39. int xive_native_populate_irq_data(u32 hw_irq, struct xive_irq_data *data)
  40. {
  41. __be64 flags, eoi_page, trig_page;
  42. __be32 esb_shift, src_chip;
  43. u64 opal_flags;
  44. s64 rc;
  45. memset(data, 0, sizeof(*data));
  46. rc = opal_xive_get_irq_info(hw_irq, &flags, &eoi_page, &trig_page,
  47. &esb_shift, &src_chip);
  48. if (rc) {
  49. pr_err("opal_xive_get_irq_info(0x%x) returned %lld\n",
  50. hw_irq, rc);
  51. return -EINVAL;
  52. }
  53. opal_flags = be64_to_cpu(flags);
  54. if (opal_flags & OPAL_XIVE_IRQ_STORE_EOI)
  55. data->flags |= XIVE_IRQ_FLAG_STORE_EOI;
  56. if (opal_flags & OPAL_XIVE_IRQ_LSI)
  57. data->flags |= XIVE_IRQ_FLAG_LSI;
  58. if (opal_flags & OPAL_XIVE_IRQ_SHIFT_BUG)
  59. data->flags |= XIVE_IRQ_FLAG_SHIFT_BUG;
  60. if (opal_flags & OPAL_XIVE_IRQ_MASK_VIA_FW)
  61. data->flags |= XIVE_IRQ_FLAG_MASK_FW;
  62. if (opal_flags & OPAL_XIVE_IRQ_EOI_VIA_FW)
  63. data->flags |= XIVE_IRQ_FLAG_EOI_FW;
  64. data->eoi_page = be64_to_cpu(eoi_page);
  65. data->trig_page = be64_to_cpu(trig_page);
  66. data->esb_shift = be32_to_cpu(esb_shift);
  67. data->src_chip = be32_to_cpu(src_chip);
  68. data->eoi_mmio = ioremap(data->eoi_page, 1u << data->esb_shift);
  69. if (!data->eoi_mmio) {
  70. pr_err("Failed to map EOI page for irq 0x%x\n", hw_irq);
  71. return -ENOMEM;
  72. }
  73. if (!data->trig_page)
  74. return 0;
  75. if (data->trig_page == data->eoi_page) {
  76. data->trig_mmio = data->eoi_mmio;
  77. return 0;
  78. }
  79. data->trig_mmio = ioremap(data->trig_page, 1u << data->esb_shift);
  80. if (!data->trig_mmio) {
  81. pr_err("Failed to map trigger page for irq 0x%x\n", hw_irq);
  82. return -ENOMEM;
  83. }
  84. return 0;
  85. }
  86. EXPORT_SYMBOL_GPL(xive_native_populate_irq_data);
  87. int xive_native_configure_irq(u32 hw_irq, u32 target, u8 prio, u32 sw_irq)
  88. {
  89. s64 rc;
  90. for (;;) {
  91. rc = opal_xive_set_irq_config(hw_irq, target, prio, sw_irq);
  92. if (rc != OPAL_BUSY)
  93. break;
  94. msleep(1);
  95. }
  96. return rc == 0 ? 0 : -ENXIO;
  97. }
  98. EXPORT_SYMBOL_GPL(xive_native_configure_irq);
  99. /* This can be called multiple time to change a queue configuration */
  100. int xive_native_configure_queue(u32 vp_id, struct xive_q *q, u8 prio,
  101. __be32 *qpage, u32 order, bool can_escalate)
  102. {
  103. s64 rc = 0;
  104. __be64 qeoi_page_be;
  105. __be32 esc_irq_be;
  106. u64 flags, qpage_phys;
  107. /* If there's an actual queue page, clean it */
  108. if (order) {
  109. if (WARN_ON(!qpage))
  110. return -EINVAL;
  111. qpage_phys = __pa(qpage);
  112. } else
  113. qpage_phys = 0;
  114. /* Initialize the rest of the fields */
  115. q->msk = order ? ((1u << (order - 2)) - 1) : 0;
  116. q->idx = 0;
  117. q->toggle = 0;
  118. rc = opal_xive_get_queue_info(vp_id, prio, NULL, NULL,
  119. &qeoi_page_be,
  120. &esc_irq_be,
  121. NULL);
  122. if (rc) {
  123. pr_err("Error %lld getting queue info prio %d\n", rc, prio);
  124. rc = -EIO;
  125. goto fail;
  126. }
  127. q->eoi_phys = be64_to_cpu(qeoi_page_be);
  128. /* Default flags */
  129. flags = OPAL_XIVE_EQ_ALWAYS_NOTIFY | OPAL_XIVE_EQ_ENABLED;
  130. /* Escalation needed ? */
  131. if (can_escalate) {
  132. q->esc_irq = be32_to_cpu(esc_irq_be);
  133. flags |= OPAL_XIVE_EQ_ESCALATE;
  134. }
  135. /* Configure and enable the queue in HW */
  136. for (;;) {
  137. rc = opal_xive_set_queue_info(vp_id, prio, qpage_phys, order, flags);
  138. if (rc != OPAL_BUSY)
  139. break;
  140. msleep(1);
  141. }
  142. if (rc) {
  143. pr_err("Error %lld setting queue for prio %d\n", rc, prio);
  144. rc = -EIO;
  145. } else {
  146. /*
  147. * KVM code requires all of the above to be visible before
  148. * q->qpage is set due to how it manages IPI EOIs
  149. */
  150. wmb();
  151. q->qpage = qpage;
  152. }
  153. fail:
  154. return rc;
  155. }
  156. EXPORT_SYMBOL_GPL(xive_native_configure_queue);
  157. static void __xive_native_disable_queue(u32 vp_id, struct xive_q *q, u8 prio)
  158. {
  159. s64 rc;
  160. /* Disable the queue in HW */
  161. for (;;) {
  162. rc = opal_xive_set_queue_info(vp_id, prio, 0, 0, 0);
  163. if (rc != OPAL_BUSY)
  164. break;
  165. msleep(1);
  166. }
  167. if (rc)
  168. pr_err("Error %lld disabling queue for prio %d\n", rc, prio);
  169. }
  170. void xive_native_disable_queue(u32 vp_id, struct xive_q *q, u8 prio)
  171. {
  172. __xive_native_disable_queue(vp_id, q, prio);
  173. }
  174. EXPORT_SYMBOL_GPL(xive_native_disable_queue);
  175. static int xive_native_setup_queue(unsigned int cpu, struct xive_cpu *xc, u8 prio)
  176. {
  177. struct xive_q *q = &xc->queue[prio];
  178. unsigned int alloc_order;
  179. struct page *pages;
  180. __be32 *qpage;
  181. alloc_order = (xive_queue_shift > PAGE_SHIFT) ?
  182. (xive_queue_shift - PAGE_SHIFT) : 0;
  183. pages = alloc_pages_node(cpu_to_node(cpu), GFP_KERNEL, alloc_order);
  184. if (!pages)
  185. return -ENOMEM;
  186. qpage = (__be32 *)page_address(pages);
  187. memset(qpage, 0, 1 << xive_queue_shift);
  188. return xive_native_configure_queue(get_hard_smp_processor_id(cpu),
  189. q, prio, qpage, xive_queue_shift, false);
  190. }
  191. static void xive_native_cleanup_queue(unsigned int cpu, struct xive_cpu *xc, u8 prio)
  192. {
  193. struct xive_q *q = &xc->queue[prio];
  194. unsigned int alloc_order;
  195. /*
  196. * We use the variant with no iounmap as this is called on exec
  197. * from an IPI and iounmap isn't safe
  198. */
  199. __xive_native_disable_queue(get_hard_smp_processor_id(cpu), q, prio);
  200. alloc_order = (xive_queue_shift > PAGE_SHIFT) ?
  201. (xive_queue_shift - PAGE_SHIFT) : 0;
  202. free_pages((unsigned long)q->qpage, alloc_order);
  203. q->qpage = NULL;
  204. }
  205. static bool xive_native_match(struct device_node *node)
  206. {
  207. return of_device_is_compatible(node, "ibm,opal-xive-vc");
  208. }
  209. #ifdef CONFIG_SMP
  210. static int xive_native_get_ipi(unsigned int cpu, struct xive_cpu *xc)
  211. {
  212. struct device_node *np;
  213. unsigned int chip_id;
  214. s64 irq;
  215. /* Find the chip ID */
  216. np = of_get_cpu_node(cpu, NULL);
  217. if (np) {
  218. if (of_property_read_u32(np, "ibm,chip-id", &chip_id) < 0)
  219. chip_id = 0;
  220. }
  221. /* Allocate an IPI and populate info about it */
  222. for (;;) {
  223. irq = opal_xive_allocate_irq(chip_id);
  224. if (irq == OPAL_BUSY) {
  225. msleep(1);
  226. continue;
  227. }
  228. if (irq < 0) {
  229. pr_err("Failed to allocate IPI on CPU %d\n", cpu);
  230. return -ENXIO;
  231. }
  232. xc->hw_ipi = irq;
  233. break;
  234. }
  235. return 0;
  236. }
  237. #endif /* CONFIG_SMP */
  238. u32 xive_native_alloc_irq(void)
  239. {
  240. s64 rc;
  241. for (;;) {
  242. rc = opal_xive_allocate_irq(OPAL_XIVE_ANY_CHIP);
  243. if (rc != OPAL_BUSY)
  244. break;
  245. msleep(1);
  246. }
  247. if (rc < 0)
  248. return 0;
  249. return rc;
  250. }
  251. EXPORT_SYMBOL_GPL(xive_native_alloc_irq);
  252. void xive_native_free_irq(u32 irq)
  253. {
  254. for (;;) {
  255. s64 rc = opal_xive_free_irq(irq);
  256. if (rc != OPAL_BUSY)
  257. break;
  258. msleep(1);
  259. }
  260. }
  261. EXPORT_SYMBOL_GPL(xive_native_free_irq);
  262. #ifdef CONFIG_SMP
  263. static void xive_native_put_ipi(unsigned int cpu, struct xive_cpu *xc)
  264. {
  265. s64 rc;
  266. /* Free the IPI */
  267. if (!xc->hw_ipi)
  268. return;
  269. for (;;) {
  270. rc = opal_xive_free_irq(xc->hw_ipi);
  271. if (rc == OPAL_BUSY) {
  272. msleep(1);
  273. continue;
  274. }
  275. xc->hw_ipi = 0;
  276. break;
  277. }
  278. }
  279. #endif /* CONFIG_SMP */
  280. static void xive_native_shutdown(void)
  281. {
  282. /* Switch the XIVE to emulation mode */
  283. opal_xive_reset(OPAL_XIVE_MODE_EMU);
  284. }
  285. /*
  286. * Perform an "ack" cycle on the current thread, thus
  287. * grabbing the pending active priorities and updating
  288. * the CPPR to the most favored one.
  289. */
  290. static void xive_native_update_pending(struct xive_cpu *xc)
  291. {
  292. u8 he, cppr;
  293. u16 ack;
  294. /* Perform the acknowledge hypervisor to register cycle */
  295. ack = be16_to_cpu(__raw_readw(xive_tima + TM_SPC_ACK_HV_REG));
  296. /* Synchronize subsequent queue accesses */
  297. mb();
  298. /*
  299. * Grab the CPPR and the "HE" field which indicates the source
  300. * of the hypervisor interrupt (if any)
  301. */
  302. cppr = ack & 0xff;
  303. he = GETFIELD(TM_QW3_NSR_HE, (ack >> 8));
  304. switch(he) {
  305. case TM_QW3_NSR_HE_NONE: /* Nothing to see here */
  306. break;
  307. case TM_QW3_NSR_HE_PHYS: /* Physical thread interrupt */
  308. if (cppr == 0xff)
  309. return;
  310. /* Mark the priority pending */
  311. xc->pending_prio |= 1 << cppr;
  312. /*
  313. * A new interrupt should never have a CPPR less favored
  314. * than our current one.
  315. */
  316. if (cppr >= xc->cppr)
  317. pr_err("CPU %d odd ack CPPR, got %d at %d\n",
  318. smp_processor_id(), cppr, xc->cppr);
  319. /* Update our idea of what the CPPR is */
  320. xc->cppr = cppr;
  321. break;
  322. case TM_QW3_NSR_HE_POOL: /* HV Pool interrupt (unused) */
  323. case TM_QW3_NSR_HE_LSI: /* Legacy FW LSI (unused) */
  324. pr_err("CPU %d got unexpected interrupt type HE=%d\n",
  325. smp_processor_id(), he);
  326. return;
  327. }
  328. }
  329. static void xive_native_eoi(u32 hw_irq)
  330. {
  331. /*
  332. * Not normally used except if specific interrupts need
  333. * a workaround on EOI.
  334. */
  335. opal_int_eoi(hw_irq);
  336. }
  337. static void xive_native_setup_cpu(unsigned int cpu, struct xive_cpu *xc)
  338. {
  339. s64 rc;
  340. u32 vp;
  341. __be64 vp_cam_be;
  342. u64 vp_cam;
  343. if (xive_pool_vps == XIVE_INVALID_VP)
  344. return;
  345. /* Enable the pool VP */
  346. vp = xive_pool_vps + cpu;
  347. pr_debug("CPU %d setting up pool VP 0x%x\n", cpu, vp);
  348. for (;;) {
  349. rc = opal_xive_set_vp_info(vp, OPAL_XIVE_VP_ENABLED, 0);
  350. if (rc != OPAL_BUSY)
  351. break;
  352. msleep(1);
  353. }
  354. if (rc) {
  355. pr_err("Failed to enable pool VP on CPU %d\n", cpu);
  356. return;
  357. }
  358. /* Grab it's CAM value */
  359. rc = opal_xive_get_vp_info(vp, NULL, &vp_cam_be, NULL, NULL);
  360. if (rc) {
  361. pr_err("Failed to get pool VP info CPU %d\n", cpu);
  362. return;
  363. }
  364. vp_cam = be64_to_cpu(vp_cam_be);
  365. pr_debug("VP CAM = %llx\n", vp_cam);
  366. /* Push it on the CPU (set LSMFB to 0xff to skip backlog scan) */
  367. pr_debug("(Old HW value: %08x)\n",
  368. in_be32(xive_tima + TM_QW2_HV_POOL + TM_WORD2));
  369. out_be32(xive_tima + TM_QW2_HV_POOL + TM_WORD0, 0xff);
  370. out_be32(xive_tima + TM_QW2_HV_POOL + TM_WORD2,
  371. TM_QW2W2_VP | vp_cam);
  372. pr_debug("(New HW value: %08x)\n",
  373. in_be32(xive_tima + TM_QW2_HV_POOL + TM_WORD2));
  374. }
  375. static void xive_native_teardown_cpu(unsigned int cpu, struct xive_cpu *xc)
  376. {
  377. s64 rc;
  378. u32 vp;
  379. if (xive_pool_vps == XIVE_INVALID_VP)
  380. return;
  381. /* Pull the pool VP from the CPU */
  382. in_be64(xive_tima + TM_SPC_PULL_POOL_CTX);
  383. /* Disable it */
  384. vp = xive_pool_vps + cpu;
  385. for (;;) {
  386. rc = opal_xive_set_vp_info(vp, 0, 0);
  387. if (rc != OPAL_BUSY)
  388. break;
  389. msleep(1);
  390. }
  391. }
  392. void xive_native_sync_source(u32 hw_irq)
  393. {
  394. opal_xive_sync(XIVE_SYNC_EAS, hw_irq);
  395. }
  396. EXPORT_SYMBOL_GPL(xive_native_sync_source);
  397. static const struct xive_ops xive_native_ops = {
  398. .populate_irq_data = xive_native_populate_irq_data,
  399. .configure_irq = xive_native_configure_irq,
  400. .setup_queue = xive_native_setup_queue,
  401. .cleanup_queue = xive_native_cleanup_queue,
  402. .match = xive_native_match,
  403. .shutdown = xive_native_shutdown,
  404. .update_pending = xive_native_update_pending,
  405. .eoi = xive_native_eoi,
  406. .setup_cpu = xive_native_setup_cpu,
  407. .teardown_cpu = xive_native_teardown_cpu,
  408. .sync_source = xive_native_sync_source,
  409. #ifdef CONFIG_SMP
  410. .get_ipi = xive_native_get_ipi,
  411. .put_ipi = xive_native_put_ipi,
  412. #endif /* CONFIG_SMP */
  413. .name = "native",
  414. };
  415. static bool xive_parse_provisioning(struct device_node *np)
  416. {
  417. int rc;
  418. if (of_property_read_u32(np, "ibm,xive-provision-page-size",
  419. &xive_provision_size) < 0)
  420. return true;
  421. rc = of_property_count_elems_of_size(np, "ibm,xive-provision-chips", 4);
  422. if (rc < 0) {
  423. pr_err("Error %d getting provision chips array\n", rc);
  424. return false;
  425. }
  426. xive_provision_chip_count = rc;
  427. if (rc == 0)
  428. return true;
  429. xive_provision_chips = kzalloc(4 * xive_provision_chip_count,
  430. GFP_KERNEL);
  431. if (WARN_ON(!xive_provision_chips))
  432. return false;
  433. rc = of_property_read_u32_array(np, "ibm,xive-provision-chips",
  434. xive_provision_chips,
  435. xive_provision_chip_count);
  436. if (rc < 0) {
  437. pr_err("Error %d reading provision chips array\n", rc);
  438. return false;
  439. }
  440. xive_provision_cache = kmem_cache_create("xive-provision",
  441. xive_provision_size,
  442. xive_provision_size,
  443. 0, NULL);
  444. if (!xive_provision_cache) {
  445. pr_err("Failed to allocate provision cache\n");
  446. return false;
  447. }
  448. return true;
  449. }
  450. static void xive_native_setup_pools(void)
  451. {
  452. /* Allocate a pool big enough */
  453. pr_debug("XIVE: Allocating VP block for pool size %d\n", nr_cpu_ids);
  454. xive_pool_vps = xive_native_alloc_vp_block(nr_cpu_ids);
  455. if (WARN_ON(xive_pool_vps == XIVE_INVALID_VP))
  456. pr_err("XIVE: Failed to allocate pool VP, KVM might not function\n");
  457. pr_debug("XIVE: Pool VPs allocated at 0x%x for %d max CPUs\n",
  458. xive_pool_vps, nr_cpu_ids);
  459. }
  460. u32 xive_native_default_eq_shift(void)
  461. {
  462. return xive_queue_shift;
  463. }
  464. EXPORT_SYMBOL_GPL(xive_native_default_eq_shift);
  465. bool xive_native_init(void)
  466. {
  467. struct device_node *np;
  468. struct resource r;
  469. void __iomem *tima;
  470. struct property *prop;
  471. u8 max_prio = 7;
  472. const __be32 *p;
  473. u32 val, cpu;
  474. s64 rc;
  475. if (xive_cmdline_disabled)
  476. return false;
  477. pr_devel("xive_native_init()\n");
  478. np = of_find_compatible_node(NULL, NULL, "ibm,opal-xive-pe");
  479. if (!np) {
  480. pr_devel("not found !\n");
  481. return false;
  482. }
  483. pr_devel("Found %s\n", np->full_name);
  484. /* Resource 1 is HV window */
  485. if (of_address_to_resource(np, 1, &r)) {
  486. pr_err("Failed to get thread mgmnt area resource\n");
  487. return false;
  488. }
  489. tima = ioremap(r.start, resource_size(&r));
  490. if (!tima) {
  491. pr_err("Failed to map thread mgmnt area\n");
  492. return false;
  493. }
  494. /* Read number of priorities */
  495. if (of_property_read_u32(np, "ibm,xive-#priorities", &val) == 0)
  496. max_prio = val - 1;
  497. /* Iterate the EQ sizes and pick one */
  498. of_property_for_each_u32(np, "ibm,xive-eq-sizes", prop, p, val) {
  499. xive_queue_shift = val;
  500. if (val == PAGE_SHIFT)
  501. break;
  502. }
  503. /* Configure Thread Management areas for KVM */
  504. for_each_possible_cpu(cpu)
  505. kvmppc_set_xive_tima(cpu, r.start, tima);
  506. /* Grab size of provisionning pages */
  507. xive_parse_provisioning(np);
  508. /* Switch the XIVE to exploitation mode */
  509. rc = opal_xive_reset(OPAL_XIVE_MODE_EXPL);
  510. if (rc) {
  511. pr_err("Switch to exploitation mode failed with error %lld\n", rc);
  512. return false;
  513. }
  514. /* Setup some dummy HV pool VPs */
  515. xive_native_setup_pools();
  516. /* Initialize XIVE core with our backend */
  517. if (!xive_core_init(&xive_native_ops, tima, TM_QW3_HV_PHYS,
  518. max_prio)) {
  519. opal_xive_reset(OPAL_XIVE_MODE_EMU);
  520. return false;
  521. }
  522. pr_info("Using %dkB queues\n", 1 << (xive_queue_shift - 10));
  523. return true;
  524. }
  525. static bool xive_native_provision_pages(void)
  526. {
  527. u32 i;
  528. void *p;
  529. for (i = 0; i < xive_provision_chip_count; i++) {
  530. u32 chip = xive_provision_chips[i];
  531. /*
  532. * XXX TODO: Try to make the allocation local to the node where
  533. * the chip resides.
  534. */
  535. p = kmem_cache_alloc(xive_provision_cache, GFP_KERNEL);
  536. if (!p) {
  537. pr_err("Failed to allocate provisioning page\n");
  538. return false;
  539. }
  540. opal_xive_donate_page(chip, __pa(p));
  541. }
  542. return true;
  543. }
  544. u32 xive_native_alloc_vp_block(u32 max_vcpus)
  545. {
  546. s64 rc;
  547. u32 order;
  548. order = fls(max_vcpus) - 1;
  549. if (max_vcpus > (1 << order))
  550. order++;
  551. pr_info("VP block alloc, for max VCPUs %d use order %d\n",
  552. max_vcpus, order);
  553. for (;;) {
  554. rc = opal_xive_alloc_vp_block(order);
  555. switch (rc) {
  556. case OPAL_BUSY:
  557. msleep(1);
  558. break;
  559. case OPAL_XIVE_PROVISIONING:
  560. if (!xive_native_provision_pages())
  561. return XIVE_INVALID_VP;
  562. break;
  563. default:
  564. if (rc < 0) {
  565. pr_err("OPAL failed to allocate VCPUs order %d, err %lld\n",
  566. order, rc);
  567. return XIVE_INVALID_VP;
  568. }
  569. return rc;
  570. }
  571. }
  572. }
  573. EXPORT_SYMBOL_GPL(xive_native_alloc_vp_block);
  574. void xive_native_free_vp_block(u32 vp_base)
  575. {
  576. s64 rc;
  577. if (vp_base == XIVE_INVALID_VP)
  578. return;
  579. rc = opal_xive_free_vp_block(vp_base);
  580. if (rc < 0)
  581. pr_warn("OPAL error %lld freeing VP block\n", rc);
  582. }
  583. EXPORT_SYMBOL_GPL(xive_native_free_vp_block);
  584. int xive_native_enable_vp(u32 vp_id)
  585. {
  586. s64 rc;
  587. for (;;) {
  588. rc = opal_xive_set_vp_info(vp_id, OPAL_XIVE_VP_ENABLED, 0);
  589. if (rc != OPAL_BUSY)
  590. break;
  591. msleep(1);
  592. }
  593. return rc ? -EIO : 0;
  594. }
  595. EXPORT_SYMBOL_GPL(xive_native_enable_vp);
  596. int xive_native_disable_vp(u32 vp_id)
  597. {
  598. s64 rc;
  599. for (;;) {
  600. rc = opal_xive_set_vp_info(vp_id, 0, 0);
  601. if (rc != OPAL_BUSY)
  602. break;
  603. msleep(1);
  604. }
  605. return rc ? -EIO : 0;
  606. }
  607. EXPORT_SYMBOL_GPL(xive_native_disable_vp);
  608. int xive_native_get_vp_info(u32 vp_id, u32 *out_cam_id, u32 *out_chip_id)
  609. {
  610. __be64 vp_cam_be;
  611. __be32 vp_chip_id_be;
  612. s64 rc;
  613. rc = opal_xive_get_vp_info(vp_id, NULL, &vp_cam_be, NULL, &vp_chip_id_be);
  614. if (rc)
  615. return -EIO;
  616. *out_cam_id = be64_to_cpu(vp_cam_be) & 0xffffffffu;
  617. *out_chip_id = be32_to_cpu(vp_chip_id_be);
  618. return 0;
  619. }
  620. EXPORT_SYMBOL_GPL(xive_native_get_vp_info);