native.c 17 KB

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