spapr.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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/smp.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/init.h>
  11. #include <linux/of.h>
  12. #include <linux/of_address.h>
  13. #include <linux/of_fdt.h>
  14. #include <linux/slab.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/bitmap.h>
  17. #include <linux/cpumask.h>
  18. #include <linux/mm.h>
  19. #include <linux/delay.h>
  20. #include <linux/libfdt.h>
  21. #include <asm/machdep.h>
  22. #include <asm/prom.h>
  23. #include <asm/io.h>
  24. #include <asm/smp.h>
  25. #include <asm/irq.h>
  26. #include <asm/errno.h>
  27. #include <asm/xive.h>
  28. #include <asm/xive-regs.h>
  29. #include <asm/hvcall.h>
  30. #include <asm/svm.h>
  31. #include <asm/ultravisor.h>
  32. #include "xive-internal.h"
  33. static u32 xive_queue_shift;
  34. struct xive_irq_bitmap {
  35. unsigned long *bitmap;
  36. unsigned int base;
  37. unsigned int count;
  38. spinlock_t lock;
  39. struct list_head list;
  40. };
  41. static LIST_HEAD(xive_irq_bitmaps);
  42. static int __init xive_irq_bitmap_add(int base, int count)
  43. {
  44. struct xive_irq_bitmap *xibm;
  45. xibm = kzalloc(sizeof(*xibm), GFP_KERNEL);
  46. if (!xibm)
  47. return -ENOMEM;
  48. spin_lock_init(&xibm->lock);
  49. xibm->base = base;
  50. xibm->count = count;
  51. xibm->bitmap = bitmap_zalloc(xibm->count, GFP_KERNEL);
  52. if (!xibm->bitmap) {
  53. kfree(xibm);
  54. return -ENOMEM;
  55. }
  56. list_add(&xibm->list, &xive_irq_bitmaps);
  57. pr_info("Using IRQ range [%x-%x]", xibm->base,
  58. xibm->base + xibm->count - 1);
  59. return 0;
  60. }
  61. static void xive_irq_bitmap_remove_all(void)
  62. {
  63. struct xive_irq_bitmap *xibm, *tmp;
  64. list_for_each_entry_safe(xibm, tmp, &xive_irq_bitmaps, list) {
  65. list_del(&xibm->list);
  66. bitmap_free(xibm->bitmap);
  67. kfree(xibm);
  68. }
  69. }
  70. static int __xive_irq_bitmap_alloc(struct xive_irq_bitmap *xibm)
  71. {
  72. int irq;
  73. irq = find_first_zero_bit(xibm->bitmap, xibm->count);
  74. if (irq != xibm->count) {
  75. set_bit(irq, xibm->bitmap);
  76. irq += xibm->base;
  77. } else {
  78. irq = -ENOMEM;
  79. }
  80. return irq;
  81. }
  82. static int xive_irq_bitmap_alloc(void)
  83. {
  84. struct xive_irq_bitmap *xibm;
  85. unsigned long flags;
  86. int irq = -ENOENT;
  87. list_for_each_entry(xibm, &xive_irq_bitmaps, list) {
  88. spin_lock_irqsave(&xibm->lock, flags);
  89. irq = __xive_irq_bitmap_alloc(xibm);
  90. spin_unlock_irqrestore(&xibm->lock, flags);
  91. if (irq >= 0)
  92. break;
  93. }
  94. return irq;
  95. }
  96. static void xive_irq_bitmap_free(int irq)
  97. {
  98. unsigned long flags;
  99. struct xive_irq_bitmap *xibm;
  100. list_for_each_entry(xibm, &xive_irq_bitmaps, list) {
  101. if ((irq >= xibm->base) && (irq < xibm->base + xibm->count)) {
  102. spin_lock_irqsave(&xibm->lock, flags);
  103. clear_bit(irq - xibm->base, xibm->bitmap);
  104. spin_unlock_irqrestore(&xibm->lock, flags);
  105. break;
  106. }
  107. }
  108. }
  109. /* Based on the similar routines in RTAS */
  110. static unsigned int plpar_busy_delay_time(long rc)
  111. {
  112. unsigned int ms = 0;
  113. if (H_IS_LONG_BUSY(rc)) {
  114. ms = get_longbusy_msecs(rc);
  115. } else if (rc == H_BUSY) {
  116. ms = 10; /* seems appropriate for XIVE hcalls */
  117. }
  118. return ms;
  119. }
  120. static unsigned int plpar_busy_delay(int rc)
  121. {
  122. unsigned int ms;
  123. ms = plpar_busy_delay_time(rc);
  124. if (ms)
  125. mdelay(ms);
  126. return ms;
  127. }
  128. /*
  129. * Note: this call has a partition wide scope and can take a while to
  130. * complete. If it returns H_LONG_BUSY_* it should be retried
  131. * periodically.
  132. */
  133. static long plpar_int_reset(unsigned long flags)
  134. {
  135. long rc;
  136. do {
  137. rc = plpar_hcall_norets(H_INT_RESET, flags);
  138. } while (plpar_busy_delay(rc));
  139. if (rc)
  140. pr_err("H_INT_RESET failed %ld\n", rc);
  141. return rc;
  142. }
  143. static long plpar_int_get_source_info(unsigned long flags,
  144. unsigned long lisn,
  145. unsigned long *src_flags,
  146. unsigned long *eoi_page,
  147. unsigned long *trig_page,
  148. unsigned long *esb_shift)
  149. {
  150. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  151. long rc;
  152. do {
  153. rc = plpar_hcall(H_INT_GET_SOURCE_INFO, retbuf, flags, lisn);
  154. } while (plpar_busy_delay(rc));
  155. if (rc) {
  156. pr_err("H_INT_GET_SOURCE_INFO lisn=0x%lx failed %ld\n", lisn, rc);
  157. return rc;
  158. }
  159. *src_flags = retbuf[0];
  160. *eoi_page = retbuf[1];
  161. *trig_page = retbuf[2];
  162. *esb_shift = retbuf[3];
  163. pr_debug("H_INT_GET_SOURCE_INFO lisn=0x%lx flags=0x%lx eoi=0x%lx trig=0x%lx shift=0x%lx\n",
  164. lisn, retbuf[0], retbuf[1], retbuf[2], retbuf[3]);
  165. return 0;
  166. }
  167. #define XIVE_SRC_SET_EISN (1ull << (63 - 62))
  168. #define XIVE_SRC_MASK (1ull << (63 - 63)) /* unused */
  169. static long plpar_int_set_source_config(unsigned long flags,
  170. unsigned long lisn,
  171. unsigned long target,
  172. unsigned long prio,
  173. unsigned long sw_irq)
  174. {
  175. long rc;
  176. pr_debug("H_INT_SET_SOURCE_CONFIG flags=0x%lx lisn=0x%lx target=%ld prio=%ld sw_irq=%ld\n",
  177. flags, lisn, target, prio, sw_irq);
  178. do {
  179. rc = plpar_hcall_norets(H_INT_SET_SOURCE_CONFIG, flags, lisn,
  180. target, prio, sw_irq);
  181. } while (plpar_busy_delay(rc));
  182. if (rc) {
  183. pr_err("H_INT_SET_SOURCE_CONFIG lisn=0x%lx target=%ld prio=%ld failed %ld\n",
  184. lisn, target, prio, rc);
  185. return rc;
  186. }
  187. return 0;
  188. }
  189. static long plpar_int_get_source_config(unsigned long flags,
  190. unsigned long lisn,
  191. unsigned long *target,
  192. unsigned long *prio,
  193. unsigned long *sw_irq)
  194. {
  195. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  196. long rc;
  197. pr_debug("H_INT_GET_SOURCE_CONFIG flags=0x%lx lisn=0x%lx\n", flags, lisn);
  198. do {
  199. rc = plpar_hcall(H_INT_GET_SOURCE_CONFIG, retbuf, flags, lisn,
  200. target, prio, sw_irq);
  201. } while (plpar_busy_delay(rc));
  202. if (rc) {
  203. pr_err("H_INT_GET_SOURCE_CONFIG lisn=0x%lx failed %ld\n",
  204. lisn, rc);
  205. return rc;
  206. }
  207. *target = retbuf[0];
  208. *prio = retbuf[1];
  209. *sw_irq = retbuf[2];
  210. pr_debug("H_INT_GET_SOURCE_CONFIG target=%ld prio=%ld sw_irq=%ld\n",
  211. retbuf[0], retbuf[1], retbuf[2]);
  212. return 0;
  213. }
  214. static long plpar_int_get_queue_info(unsigned long flags,
  215. unsigned long target,
  216. unsigned long priority,
  217. unsigned long *esn_page,
  218. unsigned long *esn_size)
  219. {
  220. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  221. long rc;
  222. do {
  223. rc = plpar_hcall(H_INT_GET_QUEUE_INFO, retbuf, flags, target,
  224. priority);
  225. } while (plpar_busy_delay(rc));
  226. if (rc) {
  227. pr_err("H_INT_GET_QUEUE_INFO cpu=%ld prio=%ld failed %ld\n",
  228. target, priority, rc);
  229. return rc;
  230. }
  231. *esn_page = retbuf[0];
  232. *esn_size = retbuf[1];
  233. pr_debug("H_INT_GET_QUEUE_INFO cpu=%ld prio=%ld page=0x%lx size=0x%lx\n",
  234. target, priority, retbuf[0], retbuf[1]);
  235. return 0;
  236. }
  237. #define XIVE_EQ_ALWAYS_NOTIFY (1ull << (63 - 63))
  238. static long plpar_int_set_queue_config(unsigned long flags,
  239. unsigned long target,
  240. unsigned long priority,
  241. unsigned long qpage,
  242. unsigned long qsize)
  243. {
  244. long rc;
  245. pr_debug("H_INT_SET_QUEUE_CONFIG flags=0x%lx target=%ld priority=0x%lx qpage=0x%lx qsize=0x%lx\n",
  246. flags, target, priority, qpage, qsize);
  247. do {
  248. rc = plpar_hcall_norets(H_INT_SET_QUEUE_CONFIG, flags, target,
  249. priority, qpage, qsize);
  250. } while (plpar_busy_delay(rc));
  251. if (rc) {
  252. pr_err("H_INT_SET_QUEUE_CONFIG cpu=%ld prio=%ld qpage=0x%lx returned %ld\n",
  253. target, priority, qpage, rc);
  254. return rc;
  255. }
  256. return 0;
  257. }
  258. static long plpar_int_sync(unsigned long flags, unsigned long lisn)
  259. {
  260. long rc;
  261. do {
  262. rc = plpar_hcall_norets(H_INT_SYNC, flags, lisn);
  263. } while (plpar_busy_delay(rc));
  264. if (rc) {
  265. pr_err("H_INT_SYNC lisn=0x%lx returned %ld\n", lisn, rc);
  266. return rc;
  267. }
  268. return 0;
  269. }
  270. #define XIVE_ESB_FLAG_STORE (1ull << (63 - 63))
  271. static long plpar_int_esb(unsigned long flags,
  272. unsigned long lisn,
  273. unsigned long offset,
  274. unsigned long in_data,
  275. unsigned long *out_data)
  276. {
  277. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  278. long rc;
  279. pr_debug("H_INT_ESB flags=0x%lx lisn=0x%lx offset=0x%lx in=0x%lx\n",
  280. flags, lisn, offset, in_data);
  281. do {
  282. rc = plpar_hcall(H_INT_ESB, retbuf, flags, lisn, offset,
  283. in_data);
  284. } while (plpar_busy_delay(rc));
  285. if (rc) {
  286. pr_err("H_INT_ESB lisn=0x%lx offset=0x%lx returned %ld\n",
  287. lisn, offset, rc);
  288. return rc;
  289. }
  290. *out_data = retbuf[0];
  291. return 0;
  292. }
  293. static u64 xive_spapr_esb_rw(u32 lisn, u32 offset, u64 data, bool write)
  294. {
  295. unsigned long read_data;
  296. long rc;
  297. rc = plpar_int_esb(write ? XIVE_ESB_FLAG_STORE : 0,
  298. lisn, offset, data, &read_data);
  299. if (rc)
  300. return -1;
  301. return write ? 0 : read_data;
  302. }
  303. #define XIVE_SRC_H_INT_ESB (1ull << (63 - 60))
  304. #define XIVE_SRC_LSI (1ull << (63 - 61))
  305. #define XIVE_SRC_TRIGGER (1ull << (63 - 62))
  306. #define XIVE_SRC_STORE_EOI (1ull << (63 - 63))
  307. static int xive_spapr_populate_irq_data(u32 hw_irq, struct xive_irq_data *data)
  308. {
  309. long rc;
  310. unsigned long flags;
  311. unsigned long eoi_page;
  312. unsigned long trig_page;
  313. unsigned long esb_shift;
  314. memset(data, 0, sizeof(*data));
  315. rc = plpar_int_get_source_info(0, hw_irq, &flags, &eoi_page, &trig_page,
  316. &esb_shift);
  317. if (rc)
  318. return -EINVAL;
  319. if (flags & XIVE_SRC_H_INT_ESB)
  320. data->flags |= XIVE_IRQ_FLAG_H_INT_ESB;
  321. if (flags & XIVE_SRC_STORE_EOI)
  322. data->flags |= XIVE_IRQ_FLAG_STORE_EOI;
  323. if (flags & XIVE_SRC_LSI)
  324. data->flags |= XIVE_IRQ_FLAG_LSI;
  325. data->eoi_page = eoi_page;
  326. data->esb_shift = esb_shift;
  327. data->trig_page = trig_page;
  328. data->hw_irq = hw_irq;
  329. /*
  330. * No chip-id for the sPAPR backend. This has an impact how we
  331. * pick a target. See xive_pick_irq_target().
  332. */
  333. data->src_chip = XIVE_INVALID_CHIP_ID;
  334. /*
  335. * When the H_INT_ESB flag is set, the H_INT_ESB hcall should
  336. * be used for interrupt management. Skip the remapping of the
  337. * ESB pages which are not available.
  338. */
  339. if (data->flags & XIVE_IRQ_FLAG_H_INT_ESB)
  340. return 0;
  341. data->eoi_mmio = ioremap(data->eoi_page, 1u << data->esb_shift);
  342. if (!data->eoi_mmio) {
  343. pr_err("Failed to map EOI page for irq 0x%x\n", hw_irq);
  344. return -ENOMEM;
  345. }
  346. /* Full function page supports trigger */
  347. if (flags & XIVE_SRC_TRIGGER) {
  348. data->trig_mmio = data->eoi_mmio;
  349. return 0;
  350. }
  351. data->trig_mmio = ioremap(data->trig_page, 1u << data->esb_shift);
  352. if (!data->trig_mmio) {
  353. iounmap(data->eoi_mmio);
  354. pr_err("Failed to map trigger page for irq 0x%x\n", hw_irq);
  355. return -ENOMEM;
  356. }
  357. return 0;
  358. }
  359. static int xive_spapr_configure_irq(u32 hw_irq, u32 target, u8 prio, u32 sw_irq)
  360. {
  361. long rc;
  362. rc = plpar_int_set_source_config(XIVE_SRC_SET_EISN, hw_irq, target,
  363. prio, sw_irq);
  364. return rc == 0 ? 0 : -ENXIO;
  365. }
  366. static int xive_spapr_get_irq_config(u32 hw_irq, u32 *target, u8 *prio,
  367. u32 *sw_irq)
  368. {
  369. long rc;
  370. unsigned long h_target;
  371. unsigned long h_prio;
  372. unsigned long h_sw_irq;
  373. rc = plpar_int_get_source_config(0, hw_irq, &h_target, &h_prio,
  374. &h_sw_irq);
  375. *target = h_target;
  376. *prio = h_prio;
  377. *sw_irq = h_sw_irq;
  378. return rc == 0 ? 0 : -ENXIO;
  379. }
  380. /* This can be called multiple time to change a queue configuration */
  381. static int xive_spapr_configure_queue(u32 target, struct xive_q *q, u8 prio,
  382. __be32 *qpage, u32 order)
  383. {
  384. s64 rc = 0;
  385. unsigned long esn_page;
  386. unsigned long esn_size;
  387. u64 flags, qpage_phys;
  388. /* If there's an actual queue page, clean it */
  389. if (order) {
  390. if (WARN_ON(!qpage))
  391. return -EINVAL;
  392. qpage_phys = __pa(qpage);
  393. } else {
  394. qpage_phys = 0;
  395. }
  396. /* Initialize the rest of the fields */
  397. q->msk = order ? ((1u << (order - 2)) - 1) : 0;
  398. q->idx = 0;
  399. q->toggle = 0;
  400. rc = plpar_int_get_queue_info(0, target, prio, &esn_page, &esn_size);
  401. if (rc) {
  402. pr_err("Error %lld getting queue info CPU %d prio %d\n", rc,
  403. target, prio);
  404. rc = -EIO;
  405. goto fail;
  406. }
  407. /* TODO: add support for the notification page */
  408. q->eoi_phys = esn_page;
  409. /* Default is to always notify */
  410. flags = XIVE_EQ_ALWAYS_NOTIFY;
  411. /* Configure and enable the queue in HW */
  412. rc = plpar_int_set_queue_config(flags, target, prio, qpage_phys, order);
  413. if (rc) {
  414. pr_err("Error %lld setting queue for CPU %d prio %d\n", rc,
  415. target, prio);
  416. rc = -EIO;
  417. } else {
  418. q->qpage = qpage;
  419. if (is_secure_guest())
  420. uv_share_page(PHYS_PFN(qpage_phys),
  421. 1 << xive_alloc_order(order));
  422. }
  423. fail:
  424. return rc;
  425. }
  426. static int xive_spapr_setup_queue(unsigned int cpu, struct xive_cpu *xc,
  427. u8 prio)
  428. {
  429. struct xive_q *q = &xc->queue[prio];
  430. __be32 *qpage;
  431. qpage = xive_queue_page_alloc(cpu, xive_queue_shift);
  432. if (IS_ERR(qpage))
  433. return PTR_ERR(qpage);
  434. return xive_spapr_configure_queue(get_hard_smp_processor_id(cpu),
  435. q, prio, qpage, xive_queue_shift);
  436. }
  437. static void xive_spapr_cleanup_queue(unsigned int cpu, struct xive_cpu *xc,
  438. u8 prio)
  439. {
  440. struct xive_q *q = &xc->queue[prio];
  441. unsigned int alloc_order;
  442. long rc;
  443. int hw_cpu = get_hard_smp_processor_id(cpu);
  444. rc = plpar_int_set_queue_config(0, hw_cpu, prio, 0, 0);
  445. if (rc)
  446. pr_err("Error %ld setting queue for CPU %d prio %d\n", rc,
  447. hw_cpu, prio);
  448. alloc_order = xive_alloc_order(xive_queue_shift);
  449. if (is_secure_guest())
  450. uv_unshare_page(PHYS_PFN(__pa(q->qpage)), 1 << alloc_order);
  451. free_pages((unsigned long)q->qpage, alloc_order);
  452. q->qpage = NULL;
  453. }
  454. static bool xive_spapr_match(struct device_node *node)
  455. {
  456. /* Ignore cascaded controllers for the moment */
  457. return true;
  458. }
  459. #ifdef CONFIG_SMP
  460. static int xive_spapr_get_ipi(unsigned int cpu, struct xive_cpu *xc)
  461. {
  462. int irq = xive_irq_bitmap_alloc();
  463. if (irq < 0) {
  464. pr_err("Failed to allocate IPI on CPU %d\n", cpu);
  465. return -ENXIO;
  466. }
  467. xc->hw_ipi = irq;
  468. return 0;
  469. }
  470. static void xive_spapr_put_ipi(unsigned int cpu, struct xive_cpu *xc)
  471. {
  472. if (xc->hw_ipi == XIVE_BAD_IRQ)
  473. return;
  474. xive_irq_bitmap_free(xc->hw_ipi);
  475. xc->hw_ipi = XIVE_BAD_IRQ;
  476. }
  477. #endif /* CONFIG_SMP */
  478. static void xive_spapr_shutdown(void)
  479. {
  480. plpar_int_reset(0);
  481. }
  482. /*
  483. * Perform an "ack" cycle on the current thread. Grab the pending
  484. * active priorities and update the CPPR to the most favored one.
  485. */
  486. static void xive_spapr_update_pending(struct xive_cpu *xc)
  487. {
  488. u8 nsr, cppr;
  489. u16 ack;
  490. /*
  491. * Perform the "Acknowledge O/S to Register" cycle.
  492. *
  493. * Let's speedup the access to the TIMA using the raw I/O
  494. * accessor as we don't need the synchronisation routine of
  495. * the higher level ones
  496. */
  497. ack = be16_to_cpu(__raw_readw(xive_tima + TM_SPC_ACK_OS_REG));
  498. /* Synchronize subsequent queue accesses */
  499. mb();
  500. /*
  501. * Grab the CPPR and the "NSR" field which indicates the source
  502. * of the interrupt (if any)
  503. */
  504. cppr = ack & 0xff;
  505. nsr = ack >> 8;
  506. if (nsr & TM_QW1_NSR_EO) {
  507. if (cppr == 0xff)
  508. return;
  509. /* Mark the priority pending */
  510. xc->pending_prio |= 1 << cppr;
  511. /*
  512. * A new interrupt should never have a CPPR less favored
  513. * than our current one.
  514. */
  515. if (cppr >= xc->cppr)
  516. pr_err("CPU %d odd ack CPPR, got %d at %d\n",
  517. smp_processor_id(), cppr, xc->cppr);
  518. /* Update our idea of what the CPPR is */
  519. xc->cppr = cppr;
  520. }
  521. }
  522. static void xive_spapr_setup_cpu(unsigned int cpu, struct xive_cpu *xc)
  523. {
  524. /* Only some debug on the TIMA settings */
  525. pr_debug("(HW value: %08x %08x %08x)\n",
  526. in_be32(xive_tima + TM_QW1_OS + TM_WORD0),
  527. in_be32(xive_tima + TM_QW1_OS + TM_WORD1),
  528. in_be32(xive_tima + TM_QW1_OS + TM_WORD2));
  529. }
  530. static void xive_spapr_teardown_cpu(unsigned int cpu, struct xive_cpu *xc)
  531. {
  532. /* Nothing to do */;
  533. }
  534. static void xive_spapr_sync_source(u32 hw_irq)
  535. {
  536. /* Specs are unclear on what this is doing */
  537. plpar_int_sync(0, hw_irq);
  538. }
  539. static int xive_spapr_debug_show(struct seq_file *m, void *private)
  540. {
  541. struct xive_irq_bitmap *xibm;
  542. char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  543. if (!buf)
  544. return -ENOMEM;
  545. list_for_each_entry(xibm, &xive_irq_bitmaps, list) {
  546. memset(buf, 0, PAGE_SIZE);
  547. bitmap_print_to_pagebuf(true, buf, xibm->bitmap, xibm->count);
  548. seq_printf(m, "bitmap #%d: %s", xibm->count, buf);
  549. }
  550. kfree(buf);
  551. return 0;
  552. }
  553. static const struct xive_ops xive_spapr_ops = {
  554. .populate_irq_data = xive_spapr_populate_irq_data,
  555. .configure_irq = xive_spapr_configure_irq,
  556. .get_irq_config = xive_spapr_get_irq_config,
  557. .setup_queue = xive_spapr_setup_queue,
  558. .cleanup_queue = xive_spapr_cleanup_queue,
  559. .match = xive_spapr_match,
  560. .shutdown = xive_spapr_shutdown,
  561. .update_pending = xive_spapr_update_pending,
  562. .setup_cpu = xive_spapr_setup_cpu,
  563. .teardown_cpu = xive_spapr_teardown_cpu,
  564. .sync_source = xive_spapr_sync_source,
  565. .esb_rw = xive_spapr_esb_rw,
  566. #ifdef CONFIG_SMP
  567. .get_ipi = xive_spapr_get_ipi,
  568. .put_ipi = xive_spapr_put_ipi,
  569. .debug_show = xive_spapr_debug_show,
  570. #endif /* CONFIG_SMP */
  571. .name = "spapr",
  572. };
  573. /*
  574. * get max priority from "/ibm,plat-res-int-priorities"
  575. */
  576. static bool __init xive_get_max_prio(u8 *max_prio)
  577. {
  578. struct device_node *rootdn;
  579. const __be32 *reg;
  580. u32 len;
  581. int prio, found;
  582. rootdn = of_find_node_by_path("/");
  583. if (!rootdn) {
  584. pr_err("not root node found !\n");
  585. return false;
  586. }
  587. reg = of_get_property(rootdn, "ibm,plat-res-int-priorities", &len);
  588. of_node_put(rootdn);
  589. if (!reg) {
  590. pr_err("Failed to read 'ibm,plat-res-int-priorities' property\n");
  591. return false;
  592. }
  593. if (len % (2 * sizeof(u32)) != 0) {
  594. pr_err("invalid 'ibm,plat-res-int-priorities' property\n");
  595. return false;
  596. }
  597. /* HW supports priorities in the range [0-7] and 0xFF is a
  598. * wildcard priority used to mask. We scan the ranges reserved
  599. * by the hypervisor to find the lowest priority we can use.
  600. */
  601. found = 0xFF;
  602. for (prio = 0; prio < 8; prio++) {
  603. int reserved = 0;
  604. int i;
  605. for (i = 0; i < len / (2 * sizeof(u32)); i++) {
  606. int base = be32_to_cpu(reg[2 * i]);
  607. int range = be32_to_cpu(reg[2 * i + 1]);
  608. if (prio >= base && prio < base + range)
  609. reserved++;
  610. }
  611. if (!reserved)
  612. found = prio;
  613. }
  614. if (found == 0xFF) {
  615. pr_err("no valid priority found in 'ibm,plat-res-int-priorities'\n");
  616. return false;
  617. }
  618. *max_prio = found;
  619. return true;
  620. }
  621. static const u8 *__init get_vec5_feature(unsigned int index)
  622. {
  623. unsigned long root, chosen;
  624. int size;
  625. const u8 *vec5;
  626. root = of_get_flat_dt_root();
  627. chosen = of_get_flat_dt_subnode_by_name(root, "chosen");
  628. if (chosen == -FDT_ERR_NOTFOUND)
  629. return NULL;
  630. vec5 = of_get_flat_dt_prop(chosen, "ibm,architecture-vec-5", &size);
  631. if (!vec5)
  632. return NULL;
  633. if (size <= index)
  634. return NULL;
  635. return vec5 + index;
  636. }
  637. static bool __init xive_spapr_disabled(void)
  638. {
  639. const u8 *vec5_xive;
  640. vec5_xive = get_vec5_feature(OV5_INDX(OV5_XIVE_SUPPORT));
  641. if (vec5_xive) {
  642. u8 val;
  643. val = *vec5_xive & OV5_FEAT(OV5_XIVE_SUPPORT);
  644. switch (val) {
  645. case OV5_FEAT(OV5_XIVE_EITHER):
  646. case OV5_FEAT(OV5_XIVE_LEGACY):
  647. break;
  648. case OV5_FEAT(OV5_XIVE_EXPLOIT):
  649. /* Hypervisor only supports XIVE */
  650. if (xive_cmdline_disabled)
  651. pr_warn("WARNING: Ignoring cmdline option xive=off\n");
  652. return false;
  653. default:
  654. pr_warn("%s: Unknown xive support option: 0x%x\n",
  655. __func__, val);
  656. break;
  657. }
  658. }
  659. return xive_cmdline_disabled;
  660. }
  661. bool __init xive_spapr_init(void)
  662. {
  663. struct device_node *np;
  664. struct resource r;
  665. void __iomem *tima;
  666. struct property *prop;
  667. u8 max_prio;
  668. u32 val;
  669. u32 len;
  670. const __be32 *reg;
  671. int i, err;
  672. if (xive_spapr_disabled())
  673. return false;
  674. pr_devel("%s()\n", __func__);
  675. np = of_find_compatible_node(NULL, NULL, "ibm,power-ivpe");
  676. if (!np) {
  677. pr_devel("not found !\n");
  678. return false;
  679. }
  680. pr_devel("Found %s\n", np->full_name);
  681. /* Resource 1 is the OS ring TIMA */
  682. if (of_address_to_resource(np, 1, &r)) {
  683. pr_err("Failed to get thread mgmnt area resource\n");
  684. goto err_put;
  685. }
  686. tima = ioremap(r.start, resource_size(&r));
  687. if (!tima) {
  688. pr_err("Failed to map thread mgmnt area\n");
  689. goto err_put;
  690. }
  691. if (!xive_get_max_prio(&max_prio))
  692. goto err_unmap;
  693. /* Feed the IRQ number allocator with the ranges given in the DT */
  694. reg = of_get_property(np, "ibm,xive-lisn-ranges", &len);
  695. if (!reg) {
  696. pr_err("Failed to read 'ibm,xive-lisn-ranges' property\n");
  697. goto err_unmap;
  698. }
  699. if (len % (2 * sizeof(u32)) != 0) {
  700. pr_err("invalid 'ibm,xive-lisn-ranges' property\n");
  701. goto err_unmap;
  702. }
  703. for (i = 0; i < len / (2 * sizeof(u32)); i++, reg += 2) {
  704. err = xive_irq_bitmap_add(be32_to_cpu(reg[0]),
  705. be32_to_cpu(reg[1]));
  706. if (err < 0)
  707. goto err_mem_free;
  708. }
  709. /* Iterate the EQ sizes and pick one */
  710. of_property_for_each_u32(np, "ibm,xive-eq-sizes", prop, reg, val) {
  711. xive_queue_shift = val;
  712. if (val == PAGE_SHIFT)
  713. break;
  714. }
  715. /* Initialize XIVE core with our backend */
  716. if (!xive_core_init(np, &xive_spapr_ops, tima, TM_QW1_OS, max_prio))
  717. goto err_mem_free;
  718. of_node_put(np);
  719. pr_info("Using %dkB queues\n", 1 << (xive_queue_shift - 10));
  720. return true;
  721. err_mem_free:
  722. xive_irq_bitmap_remove_all();
  723. err_unmap:
  724. iounmap(tima);
  725. err_put:
  726. of_node_put(np);
  727. return false;
  728. }
  729. machine_arch_initcall(pseries, xive_core_debug_init);