spapr_irq.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /*
  2. * QEMU PowerPC sPAPR IRQ interface
  3. *
  4. * Copyright (c) 2018, IBM Corporation.
  5. *
  6. * This code is licensed under the GPL version 2 or later. See the
  7. * COPYING file in the top-level directory.
  8. */
  9. #include "qemu/osdep.h"
  10. #include "qemu/log.h"
  11. #include "qemu/error-report.h"
  12. #include "qapi/error.h"
  13. #include "hw/irq.h"
  14. #include "hw/ppc/spapr.h"
  15. #include "hw/ppc/spapr_cpu_core.h"
  16. #include "hw/ppc/spapr_xive.h"
  17. #include "hw/ppc/xics.h"
  18. #include "hw/ppc/xics_spapr.h"
  19. #include "hw/qdev-properties.h"
  20. #include "cpu-models.h"
  21. #include "sysemu/kvm.h"
  22. #include "trace.h"
  23. static const TypeInfo spapr_intc_info = {
  24. .name = TYPE_SPAPR_INTC,
  25. .parent = TYPE_INTERFACE,
  26. .class_size = sizeof(SpaprInterruptControllerClass),
  27. };
  28. static void spapr_irq_msi_init(SpaprMachineState *spapr)
  29. {
  30. if (SPAPR_MACHINE_GET_CLASS(spapr)->legacy_irq_allocation) {
  31. /* Legacy mode doesn't use this allocator */
  32. return;
  33. }
  34. spapr->irq_map_nr = spapr_irq_nr_msis(spapr);
  35. spapr->irq_map = bitmap_new(spapr->irq_map_nr);
  36. }
  37. int spapr_irq_msi_alloc(SpaprMachineState *spapr, uint32_t num, bool align,
  38. Error **errp)
  39. {
  40. int irq;
  41. /*
  42. * The 'align_mask' parameter of bitmap_find_next_zero_area()
  43. * should be one less than a power of 2; 0 means no
  44. * alignment. Adapt the 'align' value of the former allocator
  45. * to fit the requirements of bitmap_find_next_zero_area()
  46. */
  47. align -= 1;
  48. irq = bitmap_find_next_zero_area(spapr->irq_map, spapr->irq_map_nr, 0, num,
  49. align);
  50. if (irq == spapr->irq_map_nr) {
  51. error_setg(errp, "can't find a free %d-IRQ block", num);
  52. return -1;
  53. }
  54. bitmap_set(spapr->irq_map, irq, num);
  55. return irq + SPAPR_IRQ_MSI;
  56. }
  57. void spapr_irq_msi_free(SpaprMachineState *spapr, int irq, uint32_t num)
  58. {
  59. bitmap_clear(spapr->irq_map, irq - SPAPR_IRQ_MSI, num);
  60. }
  61. int spapr_irq_init_kvm(int (*fn)(SpaprInterruptController *, Error **),
  62. SpaprInterruptController *intc,
  63. Error **errp)
  64. {
  65. MachineState *machine = MACHINE(qdev_get_machine());
  66. Error *local_err = NULL;
  67. if (kvm_enabled() && machine_kernel_irqchip_allowed(machine)) {
  68. if (fn(intc, &local_err) < 0) {
  69. if (machine_kernel_irqchip_required(machine)) {
  70. error_prepend(&local_err,
  71. "kernel_irqchip requested but unavailable: ");
  72. error_propagate(errp, local_err);
  73. return -1;
  74. }
  75. /*
  76. * We failed to initialize the KVM device, fallback to
  77. * emulated mode
  78. */
  79. error_prepend(&local_err,
  80. "kernel_irqchip allowed but unavailable: ");
  81. error_append_hint(&local_err,
  82. "Falling back to kernel-irqchip=off\n");
  83. warn_report_err(local_err);
  84. }
  85. }
  86. return 0;
  87. }
  88. /*
  89. * XICS IRQ backend.
  90. */
  91. SpaprIrq spapr_irq_xics = {
  92. .xics = true,
  93. .xive = false,
  94. };
  95. /*
  96. * XIVE IRQ backend.
  97. */
  98. SpaprIrq spapr_irq_xive = {
  99. .xics = false,
  100. .xive = true,
  101. };
  102. /*
  103. * Dual XIVE and XICS IRQ backend.
  104. *
  105. * Both interrupt mode, XIVE and XICS, objects are created but the
  106. * machine starts in legacy interrupt mode (XICS). It can be changed
  107. * by the CAS negotiation process and, in that case, the new mode is
  108. * activated after an extra machine reset.
  109. */
  110. /*
  111. * Define values in sync with the XIVE and XICS backend
  112. */
  113. SpaprIrq spapr_irq_dual = {
  114. .xics = true,
  115. .xive = true,
  116. };
  117. static int spapr_irq_check(SpaprMachineState *spapr, Error **errp)
  118. {
  119. MachineState *machine = MACHINE(spapr);
  120. /*
  121. * Sanity checks on non-P9 machines. On these, XIVE is not
  122. * advertised, see spapr_dt_ov5_platform_support()
  123. */
  124. if (!ppc_type_check_compat(machine->cpu_type, CPU_POWERPC_LOGICAL_3_00,
  125. 0, spapr->max_compat_pvr)) {
  126. /*
  127. * If the 'dual' interrupt mode is selected, force XICS as CAS
  128. * negotiation is useless.
  129. */
  130. if (spapr->irq == &spapr_irq_dual) {
  131. spapr->irq = &spapr_irq_xics;
  132. return 0;
  133. }
  134. /*
  135. * Non-P9 machines using only XIVE is a bogus setup. We have two
  136. * scenarios to take into account because of the compat mode:
  137. *
  138. * 1. POWER7/8 machines should fail to init later on when creating
  139. * the XIVE interrupt presenters because a POWER9 exception
  140. * model is required.
  141. * 2. POWER9 machines using the POWER8 compat mode won't fail and
  142. * will let the OS boot with a partial XIVE setup : DT
  143. * properties but no hcalls.
  144. *
  145. * To cover both and not confuse the OS, add an early failure in
  146. * QEMU.
  147. */
  148. if (spapr->irq == &spapr_irq_xive) {
  149. error_setg(errp, "XIVE-only machines require a POWER9 CPU");
  150. return -1;
  151. }
  152. }
  153. /*
  154. * On a POWER9 host, some older KVM XICS devices cannot be destroyed and
  155. * re-created. Detect that early to avoid QEMU to exit later when the
  156. * guest reboots.
  157. */
  158. if (kvm_enabled() &&
  159. spapr->irq == &spapr_irq_dual &&
  160. machine_kernel_irqchip_required(machine) &&
  161. xics_kvm_has_broken_disconnect(spapr)) {
  162. error_setg(errp, "KVM is too old to support ic-mode=dual,kernel-irqchip=on");
  163. return -1;
  164. }
  165. return 0;
  166. }
  167. /*
  168. * sPAPR IRQ frontend routines for devices
  169. */
  170. #define ALL_INTCS(spapr_) \
  171. { SPAPR_INTC((spapr_)->ics), SPAPR_INTC((spapr_)->xive), }
  172. int spapr_irq_cpu_intc_create(SpaprMachineState *spapr,
  173. PowerPCCPU *cpu, Error **errp)
  174. {
  175. SpaprInterruptController *intcs[] = ALL_INTCS(spapr);
  176. int i;
  177. int rc;
  178. for (i = 0; i < ARRAY_SIZE(intcs); i++) {
  179. SpaprInterruptController *intc = intcs[i];
  180. if (intc) {
  181. SpaprInterruptControllerClass *sicc = SPAPR_INTC_GET_CLASS(intc);
  182. rc = sicc->cpu_intc_create(intc, cpu, errp);
  183. if (rc < 0) {
  184. return rc;
  185. }
  186. }
  187. }
  188. return 0;
  189. }
  190. void spapr_irq_cpu_intc_reset(SpaprMachineState *spapr, PowerPCCPU *cpu)
  191. {
  192. SpaprInterruptController *intcs[] = ALL_INTCS(spapr);
  193. int i;
  194. for (i = 0; i < ARRAY_SIZE(intcs); i++) {
  195. SpaprInterruptController *intc = intcs[i];
  196. if (intc) {
  197. SpaprInterruptControllerClass *sicc = SPAPR_INTC_GET_CLASS(intc);
  198. sicc->cpu_intc_reset(intc, cpu);
  199. }
  200. }
  201. }
  202. void spapr_irq_cpu_intc_destroy(SpaprMachineState *spapr, PowerPCCPU *cpu)
  203. {
  204. SpaprInterruptController *intcs[] = ALL_INTCS(spapr);
  205. int i;
  206. for (i = 0; i < ARRAY_SIZE(intcs); i++) {
  207. SpaprInterruptController *intc = intcs[i];
  208. if (intc) {
  209. SpaprInterruptControllerClass *sicc = SPAPR_INTC_GET_CLASS(intc);
  210. sicc->cpu_intc_destroy(intc, cpu);
  211. }
  212. }
  213. }
  214. static void spapr_set_irq(void *opaque, int irq, int level)
  215. {
  216. SpaprMachineState *spapr = SPAPR_MACHINE(opaque);
  217. SpaprInterruptControllerClass *sicc
  218. = SPAPR_INTC_GET_CLASS(spapr->active_intc);
  219. sicc->set_irq(spapr->active_intc, irq, level);
  220. }
  221. void spapr_irq_print_info(SpaprMachineState *spapr, Monitor *mon)
  222. {
  223. SpaprInterruptControllerClass *sicc
  224. = SPAPR_INTC_GET_CLASS(spapr->active_intc);
  225. sicc->print_info(spapr->active_intc, mon);
  226. }
  227. void spapr_irq_dt(SpaprMachineState *spapr, uint32_t nr_servers,
  228. void *fdt, uint32_t phandle)
  229. {
  230. SpaprInterruptControllerClass *sicc
  231. = SPAPR_INTC_GET_CLASS(spapr->active_intc);
  232. sicc->dt(spapr->active_intc, nr_servers, fdt, phandle);
  233. }
  234. uint32_t spapr_irq_nr_msis(SpaprMachineState *spapr)
  235. {
  236. SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
  237. if (smc->legacy_irq_allocation) {
  238. return smc->nr_xirqs;
  239. } else {
  240. return SPAPR_XIRQ_BASE + smc->nr_xirqs - SPAPR_IRQ_MSI;
  241. }
  242. }
  243. void spapr_irq_init(SpaprMachineState *spapr, Error **errp)
  244. {
  245. MachineState *machine = MACHINE(spapr);
  246. SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
  247. if (machine_kernel_irqchip_split(machine)) {
  248. error_setg(errp, "kernel_irqchip split mode not supported on pseries");
  249. return;
  250. }
  251. if (!kvm_enabled() && machine_kernel_irqchip_required(machine)) {
  252. error_setg(errp,
  253. "kernel_irqchip requested but only available with KVM");
  254. return;
  255. }
  256. if (spapr_irq_check(spapr, errp) < 0) {
  257. return;
  258. }
  259. /* Initialize the MSI IRQ allocator. */
  260. spapr_irq_msi_init(spapr);
  261. if (spapr->irq->xics) {
  262. Error *local_err = NULL;
  263. Object *obj;
  264. obj = object_new(TYPE_ICS_SPAPR);
  265. object_property_add_child(OBJECT(spapr), "ics", obj, &local_err);
  266. if (local_err) {
  267. error_propagate(errp, local_err);
  268. return;
  269. }
  270. object_property_add_const_link(obj, ICS_PROP_XICS, OBJECT(spapr),
  271. &local_err);
  272. if (local_err) {
  273. error_propagate(errp, local_err);
  274. return;
  275. }
  276. object_property_set_int(obj, smc->nr_xirqs, "nr-irqs", &local_err);
  277. if (local_err) {
  278. error_propagate(errp, local_err);
  279. return;
  280. }
  281. object_property_set_bool(obj, true, "realized", &local_err);
  282. if (local_err) {
  283. error_propagate(errp, local_err);
  284. return;
  285. }
  286. spapr->ics = ICS_SPAPR(obj);
  287. }
  288. if (spapr->irq->xive) {
  289. uint32_t nr_servers = spapr_max_server_number(spapr);
  290. DeviceState *dev;
  291. int i;
  292. dev = qdev_create(NULL, TYPE_SPAPR_XIVE);
  293. qdev_prop_set_uint32(dev, "nr-irqs", smc->nr_xirqs + SPAPR_XIRQ_BASE);
  294. /*
  295. * 8 XIVE END structures per CPU. One for each available
  296. * priority
  297. */
  298. qdev_prop_set_uint32(dev, "nr-ends", nr_servers << 3);
  299. qdev_init_nofail(dev);
  300. spapr->xive = SPAPR_XIVE(dev);
  301. /* Enable the CPU IPIs */
  302. for (i = 0; i < nr_servers; ++i) {
  303. SpaprInterruptControllerClass *sicc
  304. = SPAPR_INTC_GET_CLASS(spapr->xive);
  305. if (sicc->claim_irq(SPAPR_INTC(spapr->xive), SPAPR_IRQ_IPI + i,
  306. false, errp) < 0) {
  307. return;
  308. }
  309. }
  310. spapr_xive_hcall_init(spapr);
  311. }
  312. spapr->qirqs = qemu_allocate_irqs(spapr_set_irq, spapr,
  313. smc->nr_xirqs + SPAPR_XIRQ_BASE);
  314. /*
  315. * Mostly we don't actually need this until reset, except that not
  316. * having this set up can cause VFIO devices to issue a
  317. * false-positive warning during realize(), because they don't yet
  318. * have an in-kernel irq chip.
  319. */
  320. spapr_irq_update_active_intc(spapr);
  321. }
  322. int spapr_irq_claim(SpaprMachineState *spapr, int irq, bool lsi, Error **errp)
  323. {
  324. SpaprInterruptController *intcs[] = ALL_INTCS(spapr);
  325. int i;
  326. SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
  327. int rc;
  328. assert(irq >= SPAPR_XIRQ_BASE);
  329. assert(irq < (smc->nr_xirqs + SPAPR_XIRQ_BASE));
  330. for (i = 0; i < ARRAY_SIZE(intcs); i++) {
  331. SpaprInterruptController *intc = intcs[i];
  332. if (intc) {
  333. SpaprInterruptControllerClass *sicc = SPAPR_INTC_GET_CLASS(intc);
  334. rc = sicc->claim_irq(intc, irq, lsi, errp);
  335. if (rc < 0) {
  336. return rc;
  337. }
  338. }
  339. }
  340. return 0;
  341. }
  342. void spapr_irq_free(SpaprMachineState *spapr, int irq, int num)
  343. {
  344. SpaprInterruptController *intcs[] = ALL_INTCS(spapr);
  345. int i, j;
  346. SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
  347. assert(irq >= SPAPR_XIRQ_BASE);
  348. assert((irq + num) <= (smc->nr_xirqs + SPAPR_XIRQ_BASE));
  349. for (i = irq; i < (irq + num); i++) {
  350. for (j = 0; j < ARRAY_SIZE(intcs); j++) {
  351. SpaprInterruptController *intc = intcs[j];
  352. if (intc) {
  353. SpaprInterruptControllerClass *sicc
  354. = SPAPR_INTC_GET_CLASS(intc);
  355. sicc->free_irq(intc, i);
  356. }
  357. }
  358. }
  359. }
  360. qemu_irq spapr_qirq(SpaprMachineState *spapr, int irq)
  361. {
  362. SpaprMachineClass *smc = SPAPR_MACHINE_GET_CLASS(spapr);
  363. /*
  364. * This interface is basically for VIO and PHB devices to find the
  365. * right qemu_irq to manipulate, so we only allow access to the
  366. * external irqs for now. Currently anything which needs to
  367. * access the IPIs most naturally gets there via the guest side
  368. * interfaces, we can change this if we need to in future.
  369. */
  370. assert(irq >= SPAPR_XIRQ_BASE);
  371. assert(irq < (smc->nr_xirqs + SPAPR_XIRQ_BASE));
  372. if (spapr->ics) {
  373. assert(ics_valid_irq(spapr->ics, irq));
  374. }
  375. if (spapr->xive) {
  376. assert(irq < spapr->xive->nr_irqs);
  377. assert(xive_eas_is_valid(&spapr->xive->eat[irq]));
  378. }
  379. return spapr->qirqs[irq];
  380. }
  381. int spapr_irq_post_load(SpaprMachineState *spapr, int version_id)
  382. {
  383. SpaprInterruptControllerClass *sicc;
  384. spapr_irq_update_active_intc(spapr);
  385. sicc = SPAPR_INTC_GET_CLASS(spapr->active_intc);
  386. return sicc->post_load(spapr->active_intc, version_id);
  387. }
  388. void spapr_irq_reset(SpaprMachineState *spapr, Error **errp)
  389. {
  390. assert(!spapr->irq_map || bitmap_empty(spapr->irq_map, spapr->irq_map_nr));
  391. spapr_irq_update_active_intc(spapr);
  392. }
  393. int spapr_irq_get_phandle(SpaprMachineState *spapr, void *fdt, Error **errp)
  394. {
  395. const char *nodename = "interrupt-controller";
  396. int offset, phandle;
  397. offset = fdt_subnode_offset(fdt, 0, nodename);
  398. if (offset < 0) {
  399. error_setg(errp, "Can't find node \"%s\": %s",
  400. nodename, fdt_strerror(offset));
  401. return -1;
  402. }
  403. phandle = fdt_get_phandle(fdt, offset);
  404. if (!phandle) {
  405. error_setg(errp, "Can't get phandle of node \"%s\"", nodename);
  406. return -1;
  407. }
  408. return phandle;
  409. }
  410. static void set_active_intc(SpaprMachineState *spapr,
  411. SpaprInterruptController *new_intc)
  412. {
  413. SpaprInterruptControllerClass *sicc;
  414. assert(new_intc);
  415. if (new_intc == spapr->active_intc) {
  416. /* Nothing to do */
  417. return;
  418. }
  419. if (spapr->active_intc) {
  420. sicc = SPAPR_INTC_GET_CLASS(spapr->active_intc);
  421. if (sicc->deactivate) {
  422. sicc->deactivate(spapr->active_intc);
  423. }
  424. }
  425. sicc = SPAPR_INTC_GET_CLASS(new_intc);
  426. if (sicc->activate) {
  427. sicc->activate(new_intc, &error_fatal);
  428. }
  429. spapr->active_intc = new_intc;
  430. /*
  431. * We've changed the kernel irqchip, let VFIO devices know they
  432. * need to readjust.
  433. */
  434. kvm_irqchip_change_notify();
  435. }
  436. void spapr_irq_update_active_intc(SpaprMachineState *spapr)
  437. {
  438. SpaprInterruptController *new_intc;
  439. if (!spapr->ics) {
  440. /*
  441. * XXX before we run CAS, ov5_cas is initialized empty, which
  442. * indicates XICS, even if we have ic-mode=xive. TODO: clean
  443. * up the CAS path so that we have a clearer way of handling
  444. * this.
  445. */
  446. new_intc = SPAPR_INTC(spapr->xive);
  447. } else if (spapr->ov5_cas
  448. && spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
  449. new_intc = SPAPR_INTC(spapr->xive);
  450. } else {
  451. new_intc = SPAPR_INTC(spapr->ics);
  452. }
  453. set_active_intc(spapr, new_intc);
  454. }
  455. /*
  456. * XICS legacy routines - to deprecate one day
  457. */
  458. static int ics_find_free_block(ICSState *ics, int num, int alignnum)
  459. {
  460. int first, i;
  461. for (first = 0; first < ics->nr_irqs; first += alignnum) {
  462. if (num > (ics->nr_irqs - first)) {
  463. return -1;
  464. }
  465. for (i = first; i < first + num; ++i) {
  466. if (!ics_irq_free(ics, i)) {
  467. break;
  468. }
  469. }
  470. if (i == (first + num)) {
  471. return first;
  472. }
  473. }
  474. return -1;
  475. }
  476. int spapr_irq_find(SpaprMachineState *spapr, int num, bool align, Error **errp)
  477. {
  478. ICSState *ics = spapr->ics;
  479. int first = -1;
  480. assert(ics);
  481. /*
  482. * MSIMesage::data is used for storing VIRQ so
  483. * it has to be aligned to num to support multiple
  484. * MSI vectors. MSI-X is not affected by this.
  485. * The hint is used for the first IRQ, the rest should
  486. * be allocated continuously.
  487. */
  488. if (align) {
  489. assert((num == 1) || (num == 2) || (num == 4) ||
  490. (num == 8) || (num == 16) || (num == 32));
  491. first = ics_find_free_block(ics, num, num);
  492. } else {
  493. first = ics_find_free_block(ics, num, 1);
  494. }
  495. if (first < 0) {
  496. error_setg(errp, "can't find a free %d-IRQ block", num);
  497. return -1;
  498. }
  499. return first + ics->offset;
  500. }
  501. SpaprIrq spapr_irq_xics_legacy = {
  502. .xics = true,
  503. .xive = false,
  504. };
  505. static void spapr_irq_register_types(void)
  506. {
  507. type_register_static(&spapr_intc_info);
  508. }
  509. type_init(spapr_irq_register_types)