native.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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. static bool xive_has_single_esc;
  40. int xive_native_populate_irq_data(u32 hw_irq, struct xive_irq_data *data)
  41. {
  42. __be64 flags, eoi_page, trig_page;
  43. __be32 esb_shift, src_chip;
  44. u64 opal_flags;
  45. s64 rc;
  46. memset(data, 0, sizeof(*data));
  47. rc = opal_xive_get_irq_info(hw_irq, &flags, &eoi_page, &trig_page,
  48. &esb_shift, &src_chip);
  49. if (rc) {
  50. pr_err("opal_xive_get_irq_info(0x%x) returned %lld\n",
  51. hw_irq, rc);
  52. return -EINVAL;
  53. }
  54. opal_flags = be64_to_cpu(flags);
  55. if (opal_flags & OPAL_XIVE_IRQ_STORE_EOI)
  56. data->flags |= XIVE_IRQ_FLAG_STORE_EOI;
  57. if (opal_flags & OPAL_XIVE_IRQ_LSI)
  58. data->flags |= XIVE_IRQ_FLAG_LSI;
  59. if (opal_flags & OPAL_XIVE_IRQ_SHIFT_BUG)
  60. data->flags |= XIVE_IRQ_FLAG_SHIFT_BUG;
  61. if (opal_flags & OPAL_XIVE_IRQ_MASK_VIA_FW)
  62. data->flags |= XIVE_IRQ_FLAG_MASK_FW;
  63. if (opal_flags & OPAL_XIVE_IRQ_EOI_VIA_FW)
  64. data->flags |= XIVE_IRQ_FLAG_EOI_FW;
  65. data->eoi_page = be64_to_cpu(eoi_page);
  66. data->trig_page = be64_to_cpu(trig_page);
  67. data->esb_shift = be32_to_cpu(esb_shift);
  68. data->src_chip = be32_to_cpu(src_chip);
  69. data->eoi_mmio = ioremap(data->eoi_page, 1u << data->esb_shift);
  70. if (!data->eoi_mmio) {
  71. pr_err("Failed to map EOI page for irq 0x%x\n", hw_irq);
  72. return -ENOMEM;
  73. }
  74. data->hw_irq = hw_irq;
  75. if (!data->trig_page)
  76. return 0;
  77. if (data->trig_page == data->eoi_page) {
  78. data->trig_mmio = data->eoi_mmio;
  79. return 0;
  80. }
  81. data->trig_mmio = ioremap(data->trig_page, 1u << data->esb_shift);
  82. if (!data->trig_mmio) {
  83. pr_err("Failed to map trigger page for irq 0x%x\n", hw_irq);
  84. return -ENOMEM;
  85. }
  86. return 0;
  87. }
  88. EXPORT_SYMBOL_GPL(xive_native_populate_irq_data);
  89. int xive_native_configure_irq(u32 hw_irq, u32 target, u8 prio, u32 sw_irq)
  90. {
  91. s64 rc;
  92. for (;;) {
  93. rc = opal_xive_set_irq_config(hw_irq, target, prio, sw_irq);
  94. if (rc != OPAL_BUSY)
  95. break;
  96. msleep(OPAL_BUSY_DELAY_MS);
  97. }
  98. return rc == 0 ? 0 : -ENXIO;
  99. }
  100. EXPORT_SYMBOL_GPL(xive_native_configure_irq);
  101. /* This can be called multiple time to change a queue configuration */
  102. int xive_native_configure_queue(u32 vp_id, struct xive_q *q, u8 prio,
  103. __be32 *qpage, u32 order, bool can_escalate)
  104. {
  105. s64 rc = 0;
  106. __be64 qeoi_page_be;
  107. __be32 esc_irq_be;
  108. u64 flags, qpage_phys;
  109. /* If there's an actual queue page, clean it */
  110. if (order) {
  111. if (WARN_ON(!qpage))
  112. return -EINVAL;
  113. qpage_phys = __pa(qpage);
  114. } else
  115. qpage_phys = 0;
  116. /* Initialize the rest of the fields */
  117. q->msk = order ? ((1u << (order - 2)) - 1) : 0;
  118. q->idx = 0;
  119. q->toggle = 0;
  120. rc = opal_xive_get_queue_info(vp_id, prio, NULL, NULL,
  121. &qeoi_page_be,
  122. &esc_irq_be,
  123. NULL);
  124. if (rc) {
  125. pr_err("Error %lld getting queue info prio %d\n", rc, prio);
  126. rc = -EIO;
  127. goto fail;
  128. }
  129. q->eoi_phys = be64_to_cpu(qeoi_page_be);
  130. /* Default flags */
  131. flags = OPAL_XIVE_EQ_ALWAYS_NOTIFY | OPAL_XIVE_EQ_ENABLED;
  132. /* Escalation needed ? */
  133. if (can_escalate) {
  134. q->esc_irq = be32_to_cpu(esc_irq_be);
  135. flags |= OPAL_XIVE_EQ_ESCALATE;
  136. }
  137. /* Configure and enable the queue in HW */
  138. for (;;) {
  139. rc = opal_xive_set_queue_info(vp_id, prio, qpage_phys, order, flags);
  140. if (rc != OPAL_BUSY)
  141. break;
  142. msleep(OPAL_BUSY_DELAY_MS);
  143. }
  144. if (rc) {
  145. pr_err("Error %lld setting queue for prio %d\n", rc, prio);
  146. rc = -EIO;
  147. } else {
  148. /*
  149. * KVM code requires all of the above to be visible before
  150. * q->qpage is set due to how it manages IPI EOIs
  151. */
  152. wmb();
  153. q->qpage = qpage;
  154. }
  155. fail:
  156. return rc;
  157. }
  158. EXPORT_SYMBOL_GPL(xive_native_configure_queue);
  159. static void __xive_native_disable_queue(u32 vp_id, struct xive_q *q, u8 prio)
  160. {
  161. s64 rc;
  162. /* Disable the queue in HW */
  163. for (;;) {
  164. rc = opal_xive_set_queue_info(vp_id, prio, 0, 0, 0);
  165. if (rc != OPAL_BUSY)
  166. break;
  167. msleep(OPAL_BUSY_DELAY_MS);
  168. }
  169. if (rc)
  170. pr_err("Error %lld disabling queue for prio %d\n", rc, prio);
  171. }
  172. void xive_native_disable_queue(u32 vp_id, struct xive_q *q, u8 prio)
  173. {
  174. __xive_native_disable_queue(vp_id, q, prio);
  175. }
  176. EXPORT_SYMBOL_GPL(xive_native_disable_queue);
  177. static int xive_native_setup_queue(unsigned int cpu, struct xive_cpu *xc, u8 prio)
  178. {
  179. struct xive_q *q = &xc->queue[prio];
  180. __be32 *qpage;
  181. qpage = xive_queue_page_alloc(cpu, xive_queue_shift);
  182. if (IS_ERR(qpage))
  183. return PTR_ERR(qpage);
  184. return xive_native_configure_queue(get_hard_smp_processor_id(cpu),
  185. q, prio, qpage, xive_queue_shift, false);
  186. }
  187. static void xive_native_cleanup_queue(unsigned int cpu, struct xive_cpu *xc, u8 prio)
  188. {
  189. struct xive_q *q = &xc->queue[prio];
  190. unsigned int alloc_order;
  191. /*
  192. * We use the variant with no iounmap as this is called on exec
  193. * from an IPI and iounmap isn't safe
  194. */
  195. __xive_native_disable_queue(get_hard_smp_processor_id(cpu), q, prio);
  196. alloc_order = xive_alloc_order(xive_queue_shift);
  197. free_pages((unsigned long)q->qpage, alloc_order);
  198. q->qpage = NULL;
  199. }
  200. static bool xive_native_match(struct device_node *node)
  201. {
  202. return of_device_is_compatible(node, "ibm,opal-xive-vc");
  203. }
  204. static s64 opal_xive_allocate_irq(u32 chip_id)
  205. {
  206. s64 irq = opal_xive_allocate_irq_raw(chip_id);
  207. /*
  208. * Old versions of skiboot can incorrectly return 0xffffffff to
  209. * indicate no space, fix it up here.
  210. */
  211. return irq == 0xffffffff ? OPAL_RESOURCE : irq;
  212. }
  213. #ifdef CONFIG_SMP
  214. static int xive_native_get_ipi(unsigned int cpu, struct xive_cpu *xc)
  215. {
  216. struct device_node *np;
  217. unsigned int chip_id;
  218. s64 irq;
  219. /* Find the chip ID */
  220. np = of_get_cpu_node(cpu, NULL);
  221. if (np) {
  222. if (of_property_read_u32(np, "ibm,chip-id", &chip_id) < 0)
  223. chip_id = 0;
  224. }
  225. /* Allocate an IPI and populate info about it */
  226. for (;;) {
  227. irq = opal_xive_allocate_irq(chip_id);
  228. if (irq == OPAL_BUSY) {
  229. msleep(OPAL_BUSY_DELAY_MS);
  230. continue;
  231. }
  232. if (irq < 0) {
  233. pr_err("Failed to allocate IPI on CPU %d\n", cpu);
  234. return -ENXIO;
  235. }
  236. xc->hw_ipi = irq;
  237. break;
  238. }
  239. return 0;
  240. }
  241. #endif /* CONFIG_SMP */
  242. u32 xive_native_alloc_irq(void)
  243. {
  244. s64 rc;
  245. for (;;) {
  246. rc = opal_xive_allocate_irq(OPAL_XIVE_ANY_CHIP);
  247. if (rc != OPAL_BUSY)
  248. break;
  249. msleep(OPAL_BUSY_DELAY_MS);
  250. }
  251. if (rc < 0)
  252. return 0;
  253. return rc;
  254. }
  255. EXPORT_SYMBOL_GPL(xive_native_alloc_irq);
  256. void xive_native_free_irq(u32 irq)
  257. {
  258. for (;;) {
  259. s64 rc = opal_xive_free_irq(irq);
  260. if (rc != OPAL_BUSY)
  261. break;
  262. msleep(OPAL_BUSY_DELAY_MS);
  263. }
  264. }
  265. EXPORT_SYMBOL_GPL(xive_native_free_irq);
  266. #ifdef CONFIG_SMP
  267. static void xive_native_put_ipi(unsigned int cpu, struct xive_cpu *xc)
  268. {
  269. s64 rc;
  270. /* Free the IPI */
  271. if (!xc->hw_ipi)
  272. return;
  273. for (;;) {
  274. rc = opal_xive_free_irq(xc->hw_ipi);
  275. if (rc == OPAL_BUSY) {
  276. msleep(OPAL_BUSY_DELAY_MS);
  277. continue;
  278. }
  279. xc->hw_ipi = 0;
  280. break;
  281. }
  282. }
  283. #endif /* CONFIG_SMP */
  284. static void xive_native_shutdown(void)
  285. {
  286. /* Switch the XIVE to emulation mode */
  287. opal_xive_reset(OPAL_XIVE_MODE_EMU);
  288. }
  289. /*
  290. * Perform an "ack" cycle on the current thread, thus
  291. * grabbing the pending active priorities and updating
  292. * the CPPR to the most favored one.
  293. */
  294. static void xive_native_update_pending(struct xive_cpu *xc)
  295. {
  296. u8 he, cppr;
  297. u16 ack;
  298. /* Perform the acknowledge hypervisor to register cycle */
  299. ack = be16_to_cpu(__raw_readw(xive_tima + TM_SPC_ACK_HV_REG));
  300. /* Synchronize subsequent queue accesses */
  301. mb();
  302. /*
  303. * Grab the CPPR and the "HE" field which indicates the source
  304. * of the hypervisor interrupt (if any)
  305. */
  306. cppr = ack & 0xff;
  307. he = (ack >> 8) >> 6;
  308. switch(he) {
  309. case TM_QW3_NSR_HE_NONE: /* Nothing to see here */
  310. break;
  311. case TM_QW3_NSR_HE_PHYS: /* Physical thread interrupt */
  312. if (cppr == 0xff)
  313. return;
  314. /* Mark the priority pending */
  315. xc->pending_prio |= 1 << cppr;
  316. /*
  317. * A new interrupt should never have a CPPR less favored
  318. * than our current one.
  319. */
  320. if (cppr >= xc->cppr)
  321. pr_err("CPU %d odd ack CPPR, got %d at %d\n",
  322. smp_processor_id(), cppr, xc->cppr);
  323. /* Update our idea of what the CPPR is */
  324. xc->cppr = cppr;
  325. break;
  326. case TM_QW3_NSR_HE_POOL: /* HV Pool interrupt (unused) */
  327. case TM_QW3_NSR_HE_LSI: /* Legacy FW LSI (unused) */
  328. pr_err("CPU %d got unexpected interrupt type HE=%d\n",
  329. smp_processor_id(), he);
  330. return;
  331. }
  332. }
  333. static void xive_native_eoi(u32 hw_irq)
  334. {
  335. /*
  336. * Not normally used except if specific interrupts need
  337. * a workaround on EOI.
  338. */
  339. opal_int_eoi(hw_irq);
  340. }
  341. static void xive_native_setup_cpu(unsigned int cpu, struct xive_cpu *xc)
  342. {
  343. s64 rc;
  344. u32 vp;
  345. __be64 vp_cam_be;
  346. u64 vp_cam;
  347. if (xive_pool_vps == XIVE_INVALID_VP)
  348. return;
  349. /* Check if pool VP already active, if it is, pull it */
  350. if (in_be32(xive_tima + TM_QW2_HV_POOL + TM_WORD2) & TM_QW2W2_VP)
  351. in_be64(xive_tima + TM_SPC_PULL_POOL_CTX);
  352. /* Enable the pool VP */
  353. vp = xive_pool_vps + cpu;
  354. for (;;) {
  355. rc = opal_xive_set_vp_info(vp, OPAL_XIVE_VP_ENABLED, 0);
  356. if (rc != OPAL_BUSY)
  357. break;
  358. msleep(OPAL_BUSY_DELAY_MS);
  359. }
  360. if (rc) {
  361. pr_err("Failed to enable pool VP on CPU %d\n", cpu);
  362. return;
  363. }
  364. /* Grab it's CAM value */
  365. rc = opal_xive_get_vp_info(vp, NULL, &vp_cam_be, NULL, NULL);
  366. if (rc) {
  367. pr_err("Failed to get pool VP info CPU %d\n", cpu);
  368. return;
  369. }
  370. vp_cam = be64_to_cpu(vp_cam_be);
  371. /* Push it on the CPU (set LSMFB to 0xff to skip backlog scan) */
  372. out_be32(xive_tima + TM_QW2_HV_POOL + TM_WORD0, 0xff);
  373. out_be32(xive_tima + TM_QW2_HV_POOL + TM_WORD2, TM_QW2W2_VP | vp_cam);
  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(OPAL_BUSY_DELAY_MS);
  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 = kcalloc(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 %u\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 %u 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 __init 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 %pOF\n", np);
  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. /* Do we support single escalation */
  504. if (of_get_property(np, "single-escalation-support", NULL) != NULL)
  505. xive_has_single_esc = true;
  506. /* Configure Thread Management areas for KVM */
  507. for_each_possible_cpu(cpu)
  508. kvmppc_set_xive_tima(cpu, r.start, tima);
  509. /* Grab size of provisionning pages */
  510. xive_parse_provisioning(np);
  511. /* Switch the XIVE to exploitation mode */
  512. rc = opal_xive_reset(OPAL_XIVE_MODE_EXPL);
  513. if (rc) {
  514. pr_err("Switch to exploitation mode failed with error %lld\n", rc);
  515. return false;
  516. }
  517. /* Setup some dummy HV pool VPs */
  518. xive_native_setup_pools();
  519. /* Initialize XIVE core with our backend */
  520. if (!xive_core_init(&xive_native_ops, tima, TM_QW3_HV_PHYS,
  521. max_prio)) {
  522. opal_xive_reset(OPAL_XIVE_MODE_EMU);
  523. return false;
  524. }
  525. pr_info("Using %dkB queues\n", 1 << (xive_queue_shift - 10));
  526. return true;
  527. }
  528. static bool xive_native_provision_pages(void)
  529. {
  530. u32 i;
  531. void *p;
  532. for (i = 0; i < xive_provision_chip_count; i++) {
  533. u32 chip = xive_provision_chips[i];
  534. /*
  535. * XXX TODO: Try to make the allocation local to the node where
  536. * the chip resides.
  537. */
  538. p = kmem_cache_alloc(xive_provision_cache, GFP_KERNEL);
  539. if (!p) {
  540. pr_err("Failed to allocate provisioning page\n");
  541. return false;
  542. }
  543. opal_xive_donate_page(chip, __pa(p));
  544. }
  545. return true;
  546. }
  547. u32 xive_native_alloc_vp_block(u32 max_vcpus)
  548. {
  549. s64 rc;
  550. u32 order;
  551. order = fls(max_vcpus) - 1;
  552. if (max_vcpus > (1 << order))
  553. order++;
  554. pr_debug("VP block alloc, for max VCPUs %d use order %d\n",
  555. max_vcpus, order);
  556. for (;;) {
  557. rc = opal_xive_alloc_vp_block(order);
  558. switch (rc) {
  559. case OPAL_BUSY:
  560. msleep(OPAL_BUSY_DELAY_MS);
  561. break;
  562. case OPAL_XIVE_PROVISIONING:
  563. if (!xive_native_provision_pages())
  564. return XIVE_INVALID_VP;
  565. break;
  566. default:
  567. if (rc < 0) {
  568. pr_err("OPAL failed to allocate VCPUs order %d, err %lld\n",
  569. order, rc);
  570. return XIVE_INVALID_VP;
  571. }
  572. return rc;
  573. }
  574. }
  575. }
  576. EXPORT_SYMBOL_GPL(xive_native_alloc_vp_block);
  577. void xive_native_free_vp_block(u32 vp_base)
  578. {
  579. s64 rc;
  580. if (vp_base == XIVE_INVALID_VP)
  581. return;
  582. rc = opal_xive_free_vp_block(vp_base);
  583. if (rc < 0)
  584. pr_warn("OPAL error %lld freeing VP block\n", rc);
  585. }
  586. EXPORT_SYMBOL_GPL(xive_native_free_vp_block);
  587. int xive_native_enable_vp(u32 vp_id, bool single_escalation)
  588. {
  589. s64 rc;
  590. u64 flags = OPAL_XIVE_VP_ENABLED;
  591. if (single_escalation)
  592. flags |= OPAL_XIVE_VP_SINGLE_ESCALATION;
  593. for (;;) {
  594. rc = opal_xive_set_vp_info(vp_id, flags, 0);
  595. if (rc != OPAL_BUSY)
  596. break;
  597. msleep(OPAL_BUSY_DELAY_MS);
  598. }
  599. return rc ? -EIO : 0;
  600. }
  601. EXPORT_SYMBOL_GPL(xive_native_enable_vp);
  602. int xive_native_disable_vp(u32 vp_id)
  603. {
  604. s64 rc;
  605. for (;;) {
  606. rc = opal_xive_set_vp_info(vp_id, 0, 0);
  607. if (rc != OPAL_BUSY)
  608. break;
  609. msleep(OPAL_BUSY_DELAY_MS);
  610. }
  611. return rc ? -EIO : 0;
  612. }
  613. EXPORT_SYMBOL_GPL(xive_native_disable_vp);
  614. int xive_native_get_vp_info(u32 vp_id, u32 *out_cam_id, u32 *out_chip_id)
  615. {
  616. __be64 vp_cam_be;
  617. __be32 vp_chip_id_be;
  618. s64 rc;
  619. rc = opal_xive_get_vp_info(vp_id, NULL, &vp_cam_be, NULL, &vp_chip_id_be);
  620. if (rc)
  621. return -EIO;
  622. *out_cam_id = be64_to_cpu(vp_cam_be) & 0xffffffffu;
  623. *out_chip_id = be32_to_cpu(vp_chip_id_be);
  624. return 0;
  625. }
  626. EXPORT_SYMBOL_GPL(xive_native_get_vp_info);
  627. bool xive_native_has_single_escalation(void)
  628. {
  629. return xive_has_single_esc;
  630. }
  631. EXPORT_SYMBOL_GPL(xive_native_has_single_escalation);