native.c 20 KB

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