spapr.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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/smp.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/init.h>
  15. #include <linux/of.h>
  16. #include <linux/slab.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/cpumask.h>
  19. #include <linux/mm.h>
  20. #include <asm/prom.h>
  21. #include <asm/io.h>
  22. #include <asm/smp.h>
  23. #include <asm/irq.h>
  24. #include <asm/errno.h>
  25. #include <asm/xive.h>
  26. #include <asm/xive-regs.h>
  27. #include <asm/hvcall.h>
  28. #include "xive-internal.h"
  29. static u32 xive_queue_shift;
  30. struct xive_irq_bitmap {
  31. unsigned long *bitmap;
  32. unsigned int base;
  33. unsigned int count;
  34. spinlock_t lock;
  35. struct list_head list;
  36. };
  37. static LIST_HEAD(xive_irq_bitmaps);
  38. static int xive_irq_bitmap_add(int base, int count)
  39. {
  40. struct xive_irq_bitmap *xibm;
  41. xibm = kzalloc(sizeof(*xibm), GFP_ATOMIC);
  42. if (!xibm)
  43. return -ENOMEM;
  44. spin_lock_init(&xibm->lock);
  45. xibm->base = base;
  46. xibm->count = count;
  47. xibm->bitmap = kzalloc(xibm->count, GFP_KERNEL);
  48. list_add(&xibm->list, &xive_irq_bitmaps);
  49. pr_info("Using IRQ range [%x-%x]", xibm->base,
  50. xibm->base + xibm->count - 1);
  51. return 0;
  52. }
  53. static int __xive_irq_bitmap_alloc(struct xive_irq_bitmap *xibm)
  54. {
  55. int irq;
  56. irq = find_first_zero_bit(xibm->bitmap, xibm->count);
  57. if (irq != xibm->count) {
  58. set_bit(irq, xibm->bitmap);
  59. irq += xibm->base;
  60. } else {
  61. irq = -ENOMEM;
  62. }
  63. return irq;
  64. }
  65. static int xive_irq_bitmap_alloc(void)
  66. {
  67. struct xive_irq_bitmap *xibm;
  68. unsigned long flags;
  69. int irq = -ENOENT;
  70. list_for_each_entry(xibm, &xive_irq_bitmaps, list) {
  71. spin_lock_irqsave(&xibm->lock, flags);
  72. irq = __xive_irq_bitmap_alloc(xibm);
  73. spin_unlock_irqrestore(&xibm->lock, flags);
  74. if (irq >= 0)
  75. break;
  76. }
  77. return irq;
  78. }
  79. static void xive_irq_bitmap_free(int irq)
  80. {
  81. unsigned long flags;
  82. struct xive_irq_bitmap *xibm;
  83. list_for_each_entry(xibm, &xive_irq_bitmaps, list) {
  84. if ((irq >= xibm->base) && (irq < xibm->base + xibm->count)) {
  85. spin_lock_irqsave(&xibm->lock, flags);
  86. clear_bit(irq - xibm->base, xibm->bitmap);
  87. spin_unlock_irqrestore(&xibm->lock, flags);
  88. break;
  89. }
  90. }
  91. }
  92. static long plpar_int_get_source_info(unsigned long flags,
  93. unsigned long lisn,
  94. unsigned long *src_flags,
  95. unsigned long *eoi_page,
  96. unsigned long *trig_page,
  97. unsigned long *esb_shift)
  98. {
  99. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  100. long rc;
  101. rc = plpar_hcall(H_INT_GET_SOURCE_INFO, retbuf, flags, lisn);
  102. if (rc) {
  103. pr_err("H_INT_GET_SOURCE_INFO lisn=%ld failed %ld\n", lisn, rc);
  104. return rc;
  105. }
  106. *src_flags = retbuf[0];
  107. *eoi_page = retbuf[1];
  108. *trig_page = retbuf[2];
  109. *esb_shift = retbuf[3];
  110. pr_devel("H_INT_GET_SOURCE_INFO flags=%lx eoi=%lx trig=%lx shift=%lx\n",
  111. retbuf[0], retbuf[1], retbuf[2], retbuf[3]);
  112. return 0;
  113. }
  114. #define XIVE_SRC_SET_EISN (1ull << (63 - 62))
  115. #define XIVE_SRC_MASK (1ull << (63 - 63)) /* unused */
  116. static long plpar_int_set_source_config(unsigned long flags,
  117. unsigned long lisn,
  118. unsigned long target,
  119. unsigned long prio,
  120. unsigned long sw_irq)
  121. {
  122. long rc;
  123. pr_devel("H_INT_SET_SOURCE_CONFIG flags=%lx lisn=%lx target=%lx prio=%lx sw_irq=%lx\n",
  124. flags, lisn, target, prio, sw_irq);
  125. rc = plpar_hcall_norets(H_INT_SET_SOURCE_CONFIG, flags, lisn,
  126. target, prio, sw_irq);
  127. if (rc) {
  128. pr_err("H_INT_SET_SOURCE_CONFIG lisn=%ld target=%lx prio=%lx failed %ld\n",
  129. lisn, target, prio, rc);
  130. return rc;
  131. }
  132. return 0;
  133. }
  134. static long plpar_int_get_queue_info(unsigned long flags,
  135. unsigned long target,
  136. unsigned long priority,
  137. unsigned long *esn_page,
  138. unsigned long *esn_size)
  139. {
  140. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  141. long rc;
  142. rc = plpar_hcall(H_INT_GET_QUEUE_INFO, retbuf, flags, target, priority);
  143. if (rc) {
  144. pr_err("H_INT_GET_QUEUE_INFO cpu=%ld prio=%ld failed %ld\n",
  145. target, priority, rc);
  146. return rc;
  147. }
  148. *esn_page = retbuf[0];
  149. *esn_size = retbuf[1];
  150. pr_devel("H_INT_GET_QUEUE_INFO page=%lx size=%lx\n",
  151. retbuf[0], retbuf[1]);
  152. return 0;
  153. }
  154. #define XIVE_EQ_ALWAYS_NOTIFY (1ull << (63 - 63))
  155. static long plpar_int_set_queue_config(unsigned long flags,
  156. unsigned long target,
  157. unsigned long priority,
  158. unsigned long qpage,
  159. unsigned long qsize)
  160. {
  161. long rc;
  162. pr_devel("H_INT_SET_QUEUE_CONFIG flags=%lx target=%lx priority=%lx qpage=%lx qsize=%lx\n",
  163. flags, target, priority, qpage, qsize);
  164. rc = plpar_hcall_norets(H_INT_SET_QUEUE_CONFIG, flags, target,
  165. priority, qpage, qsize);
  166. if (rc) {
  167. pr_err("H_INT_SET_QUEUE_CONFIG cpu=%ld prio=%ld qpage=%lx returned %ld\n",
  168. target, priority, qpage, rc);
  169. return rc;
  170. }
  171. return 0;
  172. }
  173. static long plpar_int_sync(unsigned long flags, unsigned long lisn)
  174. {
  175. long rc;
  176. rc = plpar_hcall_norets(H_INT_SYNC, flags, lisn);
  177. if (rc) {
  178. pr_err("H_INT_SYNC lisn=%ld returned %ld\n", lisn, rc);
  179. return rc;
  180. }
  181. return 0;
  182. }
  183. #define XIVE_ESB_FLAG_STORE (1ull << (63 - 63))
  184. static long plpar_int_esb(unsigned long flags,
  185. unsigned long lisn,
  186. unsigned long offset,
  187. unsigned long in_data,
  188. unsigned long *out_data)
  189. {
  190. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  191. long rc;
  192. pr_devel("H_INT_ESB flags=%lx lisn=%lx offset=%lx in=%lx\n",
  193. flags, lisn, offset, in_data);
  194. rc = plpar_hcall(H_INT_ESB, retbuf, flags, lisn, offset, in_data);
  195. if (rc) {
  196. pr_err("H_INT_ESB lisn=%ld offset=%ld returned %ld\n",
  197. lisn, offset, rc);
  198. return rc;
  199. }
  200. *out_data = retbuf[0];
  201. return 0;
  202. }
  203. static u64 xive_spapr_esb_rw(u32 lisn, u32 offset, u64 data, bool write)
  204. {
  205. unsigned long read_data;
  206. long rc;
  207. rc = plpar_int_esb(write ? XIVE_ESB_FLAG_STORE : 0,
  208. lisn, offset, data, &read_data);
  209. if (rc)
  210. return -1;
  211. return write ? 0 : read_data;
  212. }
  213. #define XIVE_SRC_H_INT_ESB (1ull << (63 - 60))
  214. #define XIVE_SRC_LSI (1ull << (63 - 61))
  215. #define XIVE_SRC_TRIGGER (1ull << (63 - 62))
  216. #define XIVE_SRC_STORE_EOI (1ull << (63 - 63))
  217. static int xive_spapr_populate_irq_data(u32 hw_irq, struct xive_irq_data *data)
  218. {
  219. long rc;
  220. unsigned long flags;
  221. unsigned long eoi_page;
  222. unsigned long trig_page;
  223. unsigned long esb_shift;
  224. memset(data, 0, sizeof(*data));
  225. rc = plpar_int_get_source_info(0, hw_irq, &flags, &eoi_page, &trig_page,
  226. &esb_shift);
  227. if (rc)
  228. return -EINVAL;
  229. if (flags & XIVE_SRC_H_INT_ESB)
  230. data->flags |= XIVE_IRQ_FLAG_H_INT_ESB;
  231. if (flags & XIVE_SRC_STORE_EOI)
  232. data->flags |= XIVE_IRQ_FLAG_STORE_EOI;
  233. if (flags & XIVE_SRC_LSI)
  234. data->flags |= XIVE_IRQ_FLAG_LSI;
  235. data->eoi_page = eoi_page;
  236. data->esb_shift = esb_shift;
  237. data->trig_page = trig_page;
  238. /*
  239. * No chip-id for the sPAPR backend. This has an impact how we
  240. * pick a target. See xive_pick_irq_target().
  241. */
  242. data->src_chip = XIVE_INVALID_CHIP_ID;
  243. data->eoi_mmio = ioremap(data->eoi_page, 1u << data->esb_shift);
  244. if (!data->eoi_mmio) {
  245. pr_err("Failed to map EOI page for irq 0x%x\n", hw_irq);
  246. return -ENOMEM;
  247. }
  248. data->hw_irq = hw_irq;
  249. /* Full function page supports trigger */
  250. if (flags & XIVE_SRC_TRIGGER) {
  251. data->trig_mmio = data->eoi_mmio;
  252. return 0;
  253. }
  254. data->trig_mmio = ioremap(data->trig_page, 1u << data->esb_shift);
  255. if (!data->trig_mmio) {
  256. pr_err("Failed to map trigger page for irq 0x%x\n", hw_irq);
  257. return -ENOMEM;
  258. }
  259. return 0;
  260. }
  261. static int xive_spapr_configure_irq(u32 hw_irq, u32 target, u8 prio, u32 sw_irq)
  262. {
  263. long rc;
  264. rc = plpar_int_set_source_config(XIVE_SRC_SET_EISN, hw_irq, target,
  265. prio, sw_irq);
  266. return rc == 0 ? 0 : -ENXIO;
  267. }
  268. /* This can be called multiple time to change a queue configuration */
  269. static int xive_spapr_configure_queue(u32 target, struct xive_q *q, u8 prio,
  270. __be32 *qpage, u32 order)
  271. {
  272. s64 rc = 0;
  273. unsigned long esn_page;
  274. unsigned long esn_size;
  275. u64 flags, qpage_phys;
  276. /* If there's an actual queue page, clean it */
  277. if (order) {
  278. if (WARN_ON(!qpage))
  279. return -EINVAL;
  280. qpage_phys = __pa(qpage);
  281. } else {
  282. qpage_phys = 0;
  283. }
  284. /* Initialize the rest of the fields */
  285. q->msk = order ? ((1u << (order - 2)) - 1) : 0;
  286. q->idx = 0;
  287. q->toggle = 0;
  288. rc = plpar_int_get_queue_info(0, target, prio, &esn_page, &esn_size);
  289. if (rc) {
  290. pr_err("Error %lld getting queue info prio %d\n", rc, prio);
  291. rc = -EIO;
  292. goto fail;
  293. }
  294. /* TODO: add support for the notification page */
  295. q->eoi_phys = esn_page;
  296. /* Default is to always notify */
  297. flags = XIVE_EQ_ALWAYS_NOTIFY;
  298. /* Configure and enable the queue in HW */
  299. rc = plpar_int_set_queue_config(flags, target, prio, qpage_phys, order);
  300. if (rc) {
  301. pr_err("Error %lld setting queue for prio %d\n", rc, prio);
  302. rc = -EIO;
  303. } else {
  304. q->qpage = qpage;
  305. }
  306. fail:
  307. return rc;
  308. }
  309. static int xive_spapr_setup_queue(unsigned int cpu, struct xive_cpu *xc,
  310. u8 prio)
  311. {
  312. struct xive_q *q = &xc->queue[prio];
  313. __be32 *qpage;
  314. qpage = xive_queue_page_alloc(cpu, xive_queue_shift);
  315. if (IS_ERR(qpage))
  316. return PTR_ERR(qpage);
  317. return xive_spapr_configure_queue(cpu, q, prio, qpage,
  318. xive_queue_shift);
  319. }
  320. static void xive_spapr_cleanup_queue(unsigned int cpu, struct xive_cpu *xc,
  321. u8 prio)
  322. {
  323. struct xive_q *q = &xc->queue[prio];
  324. unsigned int alloc_order;
  325. long rc;
  326. rc = plpar_int_set_queue_config(0, cpu, prio, 0, 0);
  327. if (rc)
  328. pr_err("Error %ld setting queue for prio %d\n", rc, prio);
  329. alloc_order = xive_alloc_order(xive_queue_shift);
  330. free_pages((unsigned long)q->qpage, alloc_order);
  331. q->qpage = NULL;
  332. }
  333. static bool xive_spapr_match(struct device_node *node)
  334. {
  335. /* Ignore cascaded controllers for the moment */
  336. return 1;
  337. }
  338. #ifdef CONFIG_SMP
  339. static int xive_spapr_get_ipi(unsigned int cpu, struct xive_cpu *xc)
  340. {
  341. int irq = xive_irq_bitmap_alloc();
  342. if (irq < 0) {
  343. pr_err("Failed to allocate IPI on CPU %d\n", cpu);
  344. return -ENXIO;
  345. }
  346. xc->hw_ipi = irq;
  347. return 0;
  348. }
  349. static void xive_spapr_put_ipi(unsigned int cpu, struct xive_cpu *xc)
  350. {
  351. xive_irq_bitmap_free(xc->hw_ipi);
  352. }
  353. #endif /* CONFIG_SMP */
  354. static void xive_spapr_shutdown(void)
  355. {
  356. long rc;
  357. rc = plpar_hcall_norets(H_INT_RESET, 0);
  358. if (rc)
  359. pr_err("H_INT_RESET failed %ld\n", rc);
  360. }
  361. /*
  362. * Perform an "ack" cycle on the current thread. Grab the pending
  363. * active priorities and update the CPPR to the most favored one.
  364. */
  365. static void xive_spapr_update_pending(struct xive_cpu *xc)
  366. {
  367. u8 nsr, cppr;
  368. u16 ack;
  369. /*
  370. * Perform the "Acknowledge O/S to Register" cycle.
  371. *
  372. * Let's speedup the access to the TIMA using the raw I/O
  373. * accessor as we don't need the synchronisation routine of
  374. * the higher level ones
  375. */
  376. ack = be16_to_cpu(__raw_readw(xive_tima + TM_SPC_ACK_OS_REG));
  377. /* Synchronize subsequent queue accesses */
  378. mb();
  379. /*
  380. * Grab the CPPR and the "NSR" field which indicates the source
  381. * of the interrupt (if any)
  382. */
  383. cppr = ack & 0xff;
  384. nsr = ack >> 8;
  385. if (nsr & TM_QW1_NSR_EO) {
  386. if (cppr == 0xff)
  387. return;
  388. /* Mark the priority pending */
  389. xc->pending_prio |= 1 << cppr;
  390. /*
  391. * A new interrupt should never have a CPPR less favored
  392. * than our current one.
  393. */
  394. if (cppr >= xc->cppr)
  395. pr_err("CPU %d odd ack CPPR, got %d at %d\n",
  396. smp_processor_id(), cppr, xc->cppr);
  397. /* Update our idea of what the CPPR is */
  398. xc->cppr = cppr;
  399. }
  400. }
  401. static void xive_spapr_eoi(u32 hw_irq)
  402. {
  403. /* Not used */;
  404. }
  405. static void xive_spapr_setup_cpu(unsigned int cpu, struct xive_cpu *xc)
  406. {
  407. /* Only some debug on the TIMA settings */
  408. pr_debug("(HW value: %08x %08x %08x)\n",
  409. in_be32(xive_tima + TM_QW1_OS + TM_WORD0),
  410. in_be32(xive_tima + TM_QW1_OS + TM_WORD1),
  411. in_be32(xive_tima + TM_QW1_OS + TM_WORD2));
  412. }
  413. static void xive_spapr_teardown_cpu(unsigned int cpu, struct xive_cpu *xc)
  414. {
  415. /* Nothing to do */;
  416. }
  417. static void xive_spapr_sync_source(u32 hw_irq)
  418. {
  419. /* Specs are unclear on what this is doing */
  420. plpar_int_sync(0, hw_irq);
  421. }
  422. static const struct xive_ops xive_spapr_ops = {
  423. .populate_irq_data = xive_spapr_populate_irq_data,
  424. .configure_irq = xive_spapr_configure_irq,
  425. .setup_queue = xive_spapr_setup_queue,
  426. .cleanup_queue = xive_spapr_cleanup_queue,
  427. .match = xive_spapr_match,
  428. .shutdown = xive_spapr_shutdown,
  429. .update_pending = xive_spapr_update_pending,
  430. .eoi = xive_spapr_eoi,
  431. .setup_cpu = xive_spapr_setup_cpu,
  432. .teardown_cpu = xive_spapr_teardown_cpu,
  433. .sync_source = xive_spapr_sync_source,
  434. .esb_rw = xive_spapr_esb_rw,
  435. #ifdef CONFIG_SMP
  436. .get_ipi = xive_spapr_get_ipi,
  437. .put_ipi = xive_spapr_put_ipi,
  438. #endif /* CONFIG_SMP */
  439. .name = "spapr",
  440. };
  441. /*
  442. * get max priority from "/ibm,plat-res-int-priorities"
  443. */
  444. static bool xive_get_max_prio(u8 *max_prio)
  445. {
  446. struct device_node *rootdn;
  447. const __be32 *reg;
  448. u32 len;
  449. int prio, found;
  450. rootdn = of_find_node_by_path("/");
  451. if (!rootdn) {
  452. pr_err("not root node found !\n");
  453. return false;
  454. }
  455. reg = of_get_property(rootdn, "ibm,plat-res-int-priorities", &len);
  456. if (!reg) {
  457. pr_err("Failed to read 'ibm,plat-res-int-priorities' property\n");
  458. return false;
  459. }
  460. if (len % (2 * sizeof(u32)) != 0) {
  461. pr_err("invalid 'ibm,plat-res-int-priorities' property\n");
  462. return false;
  463. }
  464. /* HW supports priorities in the range [0-7] and 0xFF is a
  465. * wildcard priority used to mask. We scan the ranges reserved
  466. * by the hypervisor to find the lowest priority we can use.
  467. */
  468. found = 0xFF;
  469. for (prio = 0; prio < 8; prio++) {
  470. int reserved = 0;
  471. int i;
  472. for (i = 0; i < len / (2 * sizeof(u32)); i++) {
  473. int base = be32_to_cpu(reg[2 * i]);
  474. int range = be32_to_cpu(reg[2 * i + 1]);
  475. if (prio >= base && prio < base + range)
  476. reserved++;
  477. }
  478. if (!reserved)
  479. found = prio;
  480. }
  481. if (found == 0xFF) {
  482. pr_err("no valid priority found in 'ibm,plat-res-int-priorities'\n");
  483. return false;
  484. }
  485. *max_prio = found;
  486. return true;
  487. }
  488. bool __init xive_spapr_init(void)
  489. {
  490. struct device_node *np;
  491. struct resource r;
  492. void __iomem *tima;
  493. struct property *prop;
  494. u8 max_prio;
  495. u32 val;
  496. u32 len;
  497. const __be32 *reg;
  498. int i;
  499. if (xive_cmdline_disabled)
  500. return false;
  501. pr_devel("%s()\n", __func__);
  502. np = of_find_compatible_node(NULL, NULL, "ibm,power-ivpe");
  503. if (!np) {
  504. pr_devel("not found !\n");
  505. return false;
  506. }
  507. pr_devel("Found %s\n", np->full_name);
  508. /* Resource 1 is the OS ring TIMA */
  509. if (of_address_to_resource(np, 1, &r)) {
  510. pr_err("Failed to get thread mgmnt area resource\n");
  511. return false;
  512. }
  513. tima = ioremap(r.start, resource_size(&r));
  514. if (!tima) {
  515. pr_err("Failed to map thread mgmnt area\n");
  516. return false;
  517. }
  518. if (!xive_get_max_prio(&max_prio))
  519. return false;
  520. /* Feed the IRQ number allocator with the ranges given in the DT */
  521. reg = of_get_property(np, "ibm,xive-lisn-ranges", &len);
  522. if (!reg) {
  523. pr_err("Failed to read 'ibm,xive-lisn-ranges' property\n");
  524. return false;
  525. }
  526. if (len % (2 * sizeof(u32)) != 0) {
  527. pr_err("invalid 'ibm,xive-lisn-ranges' property\n");
  528. return false;
  529. }
  530. for (i = 0; i < len / (2 * sizeof(u32)); i++, reg += 2)
  531. xive_irq_bitmap_add(be32_to_cpu(reg[0]),
  532. be32_to_cpu(reg[1]));
  533. /* Iterate the EQ sizes and pick one */
  534. of_property_for_each_u32(np, "ibm,xive-eq-sizes", prop, reg, val) {
  535. xive_queue_shift = val;
  536. if (val == PAGE_SHIFT)
  537. break;
  538. }
  539. /* Initialize XIVE core with our backend */
  540. if (!xive_core_init(&xive_spapr_ops, tima, TM_QW1_OS, max_prio))
  541. return false;
  542. pr_info("Using %dkB queues\n", 1 << (xive_queue_shift - 10));
  543. return true;
  544. }