spapr_xive.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710
  1. /*
  2. * QEMU PowerPC sPAPR XIVE interrupt controller model
  3. *
  4. * Copyright (c) 2017-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/module.h"
  12. #include "qapi/error.h"
  13. #include "qemu/error-report.h"
  14. #include "target/ppc/cpu.h"
  15. #include "sysemu/cpus.h"
  16. #include "sysemu/reset.h"
  17. #include "migration/vmstate.h"
  18. #include "monitor/monitor.h"
  19. #include "hw/ppc/fdt.h"
  20. #include "hw/ppc/spapr.h"
  21. #include "hw/ppc/spapr_cpu_core.h"
  22. #include "hw/ppc/spapr_xive.h"
  23. #include "hw/ppc/xive.h"
  24. #include "hw/ppc/xive_regs.h"
  25. #include "hw/qdev-properties.h"
  26. /*
  27. * XIVE Virtualization Controller BAR and Thread Managment BAR that we
  28. * use for the ESB pages and the TIMA pages
  29. */
  30. #define SPAPR_XIVE_VC_BASE 0x0006010000000000ull
  31. #define SPAPR_XIVE_TM_BASE 0x0006030203180000ull
  32. /*
  33. * The allocation of VP blocks is a complex operation in OPAL and the
  34. * VP identifiers have a relation with the number of HW chips, the
  35. * size of the VP blocks, VP grouping, etc. The QEMU sPAPR XIVE
  36. * controller model does not have the same constraints and can use a
  37. * simple mapping scheme of the CPU vcpu_id
  38. *
  39. * These identifiers are never returned to the OS.
  40. */
  41. #define SPAPR_XIVE_NVT_BASE 0x400
  42. /*
  43. * sPAPR NVT and END indexing helpers
  44. */
  45. static uint32_t spapr_xive_nvt_to_target(uint8_t nvt_blk, uint32_t nvt_idx)
  46. {
  47. return nvt_idx - SPAPR_XIVE_NVT_BASE;
  48. }
  49. static void spapr_xive_cpu_to_nvt(PowerPCCPU *cpu,
  50. uint8_t *out_nvt_blk, uint32_t *out_nvt_idx)
  51. {
  52. assert(cpu);
  53. if (out_nvt_blk) {
  54. *out_nvt_blk = SPAPR_XIVE_BLOCK_ID;
  55. }
  56. if (out_nvt_blk) {
  57. *out_nvt_idx = SPAPR_XIVE_NVT_BASE + cpu->vcpu_id;
  58. }
  59. }
  60. static int spapr_xive_target_to_nvt(uint32_t target,
  61. uint8_t *out_nvt_blk, uint32_t *out_nvt_idx)
  62. {
  63. PowerPCCPU *cpu = spapr_find_cpu(target);
  64. if (!cpu) {
  65. return -1;
  66. }
  67. spapr_xive_cpu_to_nvt(cpu, out_nvt_blk, out_nvt_idx);
  68. return 0;
  69. }
  70. /*
  71. * sPAPR END indexing uses a simple mapping of the CPU vcpu_id, 8
  72. * priorities per CPU
  73. */
  74. int spapr_xive_end_to_target(uint8_t end_blk, uint32_t end_idx,
  75. uint32_t *out_server, uint8_t *out_prio)
  76. {
  77. assert(end_blk == SPAPR_XIVE_BLOCK_ID);
  78. if (out_server) {
  79. *out_server = end_idx >> 3;
  80. }
  81. if (out_prio) {
  82. *out_prio = end_idx & 0x7;
  83. }
  84. return 0;
  85. }
  86. static void spapr_xive_cpu_to_end(PowerPCCPU *cpu, uint8_t prio,
  87. uint8_t *out_end_blk, uint32_t *out_end_idx)
  88. {
  89. assert(cpu);
  90. if (out_end_blk) {
  91. *out_end_blk = SPAPR_XIVE_BLOCK_ID;
  92. }
  93. if (out_end_idx) {
  94. *out_end_idx = (cpu->vcpu_id << 3) + prio;
  95. }
  96. }
  97. static int spapr_xive_target_to_end(uint32_t target, uint8_t prio,
  98. uint8_t *out_end_blk, uint32_t *out_end_idx)
  99. {
  100. PowerPCCPU *cpu = spapr_find_cpu(target);
  101. if (!cpu) {
  102. return -1;
  103. }
  104. spapr_xive_cpu_to_end(cpu, prio, out_end_blk, out_end_idx);
  105. return 0;
  106. }
  107. /*
  108. * On sPAPR machines, use a simplified output for the XIVE END
  109. * structure dumping only the information related to the OS EQ.
  110. */
  111. static void spapr_xive_end_pic_print_info(SpaprXive *xive, XiveEND *end,
  112. Monitor *mon)
  113. {
  114. uint64_t qaddr_base = xive_end_qaddr(end);
  115. uint32_t qindex = xive_get_field32(END_W1_PAGE_OFF, end->w1);
  116. uint32_t qgen = xive_get_field32(END_W1_GENERATION, end->w1);
  117. uint32_t qsize = xive_get_field32(END_W0_QSIZE, end->w0);
  118. uint32_t qentries = 1 << (qsize + 10);
  119. uint32_t nvt = xive_get_field32(END_W6_NVT_INDEX, end->w6);
  120. uint8_t priority = xive_get_field32(END_W7_F0_PRIORITY, end->w7);
  121. monitor_printf(mon, "%3d/%d % 6d/%5d @%"PRIx64" ^%d",
  122. spapr_xive_nvt_to_target(0, nvt),
  123. priority, qindex, qentries, qaddr_base, qgen);
  124. xive_end_queue_pic_print_info(end, 6, mon);
  125. }
  126. void spapr_xive_pic_print_info(SpaprXive *xive, Monitor *mon)
  127. {
  128. XiveSource *xsrc = &xive->source;
  129. int i;
  130. if (kvm_irqchip_in_kernel()) {
  131. Error *local_err = NULL;
  132. kvmppc_xive_synchronize_state(xive, &local_err);
  133. if (local_err) {
  134. error_report_err(local_err);
  135. return;
  136. }
  137. }
  138. monitor_printf(mon, " LISN PQ EISN CPU/PRIO EQ\n");
  139. for (i = 0; i < xive->nr_irqs; i++) {
  140. uint8_t pq = xive_source_esb_get(xsrc, i);
  141. XiveEAS *eas = &xive->eat[i];
  142. if (!xive_eas_is_valid(eas)) {
  143. continue;
  144. }
  145. monitor_printf(mon, " %08x %s %c%c%c %s %08x ", i,
  146. xive_source_irq_is_lsi(xsrc, i) ? "LSI" : "MSI",
  147. pq & XIVE_ESB_VAL_P ? 'P' : '-',
  148. pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
  149. xsrc->status[i] & XIVE_STATUS_ASSERTED ? 'A' : ' ',
  150. xive_eas_is_masked(eas) ? "M" : " ",
  151. (int) xive_get_field64(EAS_END_DATA, eas->w));
  152. if (!xive_eas_is_masked(eas)) {
  153. uint32_t end_idx = xive_get_field64(EAS_END_INDEX, eas->w);
  154. XiveEND *end;
  155. assert(end_idx < xive->nr_ends);
  156. end = &xive->endt[end_idx];
  157. if (xive_end_is_valid(end)) {
  158. spapr_xive_end_pic_print_info(xive, end, mon);
  159. }
  160. }
  161. monitor_printf(mon, "\n");
  162. }
  163. }
  164. void spapr_xive_mmio_set_enabled(SpaprXive *xive, bool enable)
  165. {
  166. memory_region_set_enabled(&xive->source.esb_mmio, enable);
  167. memory_region_set_enabled(&xive->tm_mmio, enable);
  168. /* Disable the END ESBs until a guest OS makes use of them */
  169. memory_region_set_enabled(&xive->end_source.esb_mmio, false);
  170. }
  171. static void spapr_xive_end_reset(XiveEND *end)
  172. {
  173. memset(end, 0, sizeof(*end));
  174. /* switch off the escalation and notification ESBs */
  175. end->w1 = cpu_to_be32(END_W1_ESe_Q | END_W1_ESn_Q);
  176. }
  177. static void spapr_xive_reset(void *dev)
  178. {
  179. SpaprXive *xive = SPAPR_XIVE(dev);
  180. int i;
  181. /*
  182. * The XiveSource has its own reset handler, which mask off all
  183. * IRQs (!P|Q)
  184. */
  185. /* Mask all valid EASs in the IRQ number space. */
  186. for (i = 0; i < xive->nr_irqs; i++) {
  187. XiveEAS *eas = &xive->eat[i];
  188. if (xive_eas_is_valid(eas)) {
  189. eas->w = cpu_to_be64(EAS_VALID | EAS_MASKED);
  190. } else {
  191. eas->w = 0;
  192. }
  193. }
  194. /* Clear all ENDs */
  195. for (i = 0; i < xive->nr_ends; i++) {
  196. spapr_xive_end_reset(&xive->endt[i]);
  197. }
  198. }
  199. static void spapr_xive_instance_init(Object *obj)
  200. {
  201. SpaprXive *xive = SPAPR_XIVE(obj);
  202. object_initialize_child(obj, "source", &xive->source, sizeof(xive->source),
  203. TYPE_XIVE_SOURCE, &error_abort, NULL);
  204. object_initialize_child(obj, "end_source", &xive->end_source,
  205. sizeof(xive->end_source), TYPE_XIVE_END_SOURCE,
  206. &error_abort, NULL);
  207. /* Not connected to the KVM XIVE device */
  208. xive->fd = -1;
  209. }
  210. static void spapr_xive_realize(DeviceState *dev, Error **errp)
  211. {
  212. SpaprXive *xive = SPAPR_XIVE(dev);
  213. XiveSource *xsrc = &xive->source;
  214. XiveENDSource *end_xsrc = &xive->end_source;
  215. Error *local_err = NULL;
  216. if (!xive->nr_irqs) {
  217. error_setg(errp, "Number of interrupt needs to be greater 0");
  218. return;
  219. }
  220. if (!xive->nr_ends) {
  221. error_setg(errp, "Number of interrupt needs to be greater 0");
  222. return;
  223. }
  224. /*
  225. * Initialize the internal sources, for IPIs and virtual devices.
  226. */
  227. object_property_set_int(OBJECT(xsrc), xive->nr_irqs, "nr-irqs",
  228. &error_fatal);
  229. object_property_add_const_link(OBJECT(xsrc), "xive", OBJECT(xive),
  230. &error_fatal);
  231. object_property_set_bool(OBJECT(xsrc), true, "realized", &local_err);
  232. if (local_err) {
  233. error_propagate(errp, local_err);
  234. return;
  235. }
  236. sysbus_init_mmio(SYS_BUS_DEVICE(xive), &xsrc->esb_mmio);
  237. /*
  238. * Initialize the END ESB source
  239. */
  240. object_property_set_int(OBJECT(end_xsrc), xive->nr_irqs, "nr-ends",
  241. &error_fatal);
  242. object_property_add_const_link(OBJECT(end_xsrc), "xive", OBJECT(xive),
  243. &error_fatal);
  244. object_property_set_bool(OBJECT(end_xsrc), true, "realized", &local_err);
  245. if (local_err) {
  246. error_propagate(errp, local_err);
  247. return;
  248. }
  249. sysbus_init_mmio(SYS_BUS_DEVICE(xive), &end_xsrc->esb_mmio);
  250. /* Set the mapping address of the END ESB pages after the source ESBs */
  251. xive->end_base = xive->vc_base + (1ull << xsrc->esb_shift) * xsrc->nr_irqs;
  252. /*
  253. * Allocate the routing tables
  254. */
  255. xive->eat = g_new0(XiveEAS, xive->nr_irqs);
  256. xive->endt = g_new0(XiveEND, xive->nr_ends);
  257. xive->nodename = g_strdup_printf("interrupt-controller@%" PRIx64,
  258. xive->tm_base + XIVE_TM_USER_PAGE * (1 << TM_SHIFT));
  259. qemu_register_reset(spapr_xive_reset, dev);
  260. /* TIMA initialization */
  261. memory_region_init_io(&xive->tm_mmio, OBJECT(xive), &xive_tm_ops, xive,
  262. "xive.tima", 4ull << TM_SHIFT);
  263. sysbus_init_mmio(SYS_BUS_DEVICE(xive), &xive->tm_mmio);
  264. /*
  265. * Map all regions. These will be enabled or disabled at reset and
  266. * can also be overridden by KVM memory regions if active
  267. */
  268. sysbus_mmio_map(SYS_BUS_DEVICE(xive), 0, xive->vc_base);
  269. sysbus_mmio_map(SYS_BUS_DEVICE(xive), 1, xive->end_base);
  270. sysbus_mmio_map(SYS_BUS_DEVICE(xive), 2, xive->tm_base);
  271. }
  272. static int spapr_xive_get_eas(XiveRouter *xrtr, uint8_t eas_blk,
  273. uint32_t eas_idx, XiveEAS *eas)
  274. {
  275. SpaprXive *xive = SPAPR_XIVE(xrtr);
  276. if (eas_idx >= xive->nr_irqs) {
  277. return -1;
  278. }
  279. *eas = xive->eat[eas_idx];
  280. return 0;
  281. }
  282. static int spapr_xive_get_end(XiveRouter *xrtr,
  283. uint8_t end_blk, uint32_t end_idx, XiveEND *end)
  284. {
  285. SpaprXive *xive = SPAPR_XIVE(xrtr);
  286. if (end_idx >= xive->nr_ends) {
  287. return -1;
  288. }
  289. memcpy(end, &xive->endt[end_idx], sizeof(XiveEND));
  290. return 0;
  291. }
  292. static int spapr_xive_write_end(XiveRouter *xrtr, uint8_t end_blk,
  293. uint32_t end_idx, XiveEND *end,
  294. uint8_t word_number)
  295. {
  296. SpaprXive *xive = SPAPR_XIVE(xrtr);
  297. if (end_idx >= xive->nr_ends) {
  298. return -1;
  299. }
  300. memcpy(&xive->endt[end_idx], end, sizeof(XiveEND));
  301. return 0;
  302. }
  303. static int spapr_xive_get_nvt(XiveRouter *xrtr,
  304. uint8_t nvt_blk, uint32_t nvt_idx, XiveNVT *nvt)
  305. {
  306. uint32_t vcpu_id = spapr_xive_nvt_to_target(nvt_blk, nvt_idx);
  307. PowerPCCPU *cpu = spapr_find_cpu(vcpu_id);
  308. if (!cpu) {
  309. /* TODO: should we assert() if we can find a NVT ? */
  310. return -1;
  311. }
  312. /*
  313. * sPAPR does not maintain a NVT table. Return that the NVT is
  314. * valid if we have found a matching CPU
  315. */
  316. nvt->w0 = cpu_to_be32(NVT_W0_VALID);
  317. return 0;
  318. }
  319. static int spapr_xive_write_nvt(XiveRouter *xrtr, uint8_t nvt_blk,
  320. uint32_t nvt_idx, XiveNVT *nvt,
  321. uint8_t word_number)
  322. {
  323. /*
  324. * We don't need to write back to the NVTs because the sPAPR
  325. * machine should never hit a non-scheduled NVT. It should never
  326. * get called.
  327. */
  328. g_assert_not_reached();
  329. }
  330. static XiveTCTX *spapr_xive_get_tctx(XiveRouter *xrtr, CPUState *cs)
  331. {
  332. PowerPCCPU *cpu = POWERPC_CPU(cs);
  333. return spapr_cpu_state(cpu)->tctx;
  334. }
  335. static const VMStateDescription vmstate_spapr_xive_end = {
  336. .name = TYPE_SPAPR_XIVE "/end",
  337. .version_id = 1,
  338. .minimum_version_id = 1,
  339. .fields = (VMStateField []) {
  340. VMSTATE_UINT32(w0, XiveEND),
  341. VMSTATE_UINT32(w1, XiveEND),
  342. VMSTATE_UINT32(w2, XiveEND),
  343. VMSTATE_UINT32(w3, XiveEND),
  344. VMSTATE_UINT32(w4, XiveEND),
  345. VMSTATE_UINT32(w5, XiveEND),
  346. VMSTATE_UINT32(w6, XiveEND),
  347. VMSTATE_UINT32(w7, XiveEND),
  348. VMSTATE_END_OF_LIST()
  349. },
  350. };
  351. static const VMStateDescription vmstate_spapr_xive_eas = {
  352. .name = TYPE_SPAPR_XIVE "/eas",
  353. .version_id = 1,
  354. .minimum_version_id = 1,
  355. .fields = (VMStateField []) {
  356. VMSTATE_UINT64(w, XiveEAS),
  357. VMSTATE_END_OF_LIST()
  358. },
  359. };
  360. static int vmstate_spapr_xive_pre_save(void *opaque)
  361. {
  362. if (kvm_irqchip_in_kernel()) {
  363. return kvmppc_xive_pre_save(SPAPR_XIVE(opaque));
  364. }
  365. return 0;
  366. }
  367. /*
  368. * Called by the sPAPR IRQ backend 'post_load' method at the machine
  369. * level.
  370. */
  371. static int spapr_xive_post_load(SpaprInterruptController *intc, int version_id)
  372. {
  373. if (kvm_irqchip_in_kernel()) {
  374. return kvmppc_xive_post_load(SPAPR_XIVE(intc), version_id);
  375. }
  376. return 0;
  377. }
  378. static const VMStateDescription vmstate_spapr_xive = {
  379. .name = TYPE_SPAPR_XIVE,
  380. .version_id = 1,
  381. .minimum_version_id = 1,
  382. .pre_save = vmstate_spapr_xive_pre_save,
  383. .post_load = NULL, /* handled at the machine level */
  384. .fields = (VMStateField[]) {
  385. VMSTATE_UINT32_EQUAL(nr_irqs, SpaprXive, NULL),
  386. VMSTATE_STRUCT_VARRAY_POINTER_UINT32(eat, SpaprXive, nr_irqs,
  387. vmstate_spapr_xive_eas, XiveEAS),
  388. VMSTATE_STRUCT_VARRAY_POINTER_UINT32(endt, SpaprXive, nr_ends,
  389. vmstate_spapr_xive_end, XiveEND),
  390. VMSTATE_END_OF_LIST()
  391. },
  392. };
  393. static int spapr_xive_claim_irq(SpaprInterruptController *intc, int lisn,
  394. bool lsi, Error **errp)
  395. {
  396. SpaprXive *xive = SPAPR_XIVE(intc);
  397. XiveSource *xsrc = &xive->source;
  398. assert(lisn < xive->nr_irqs);
  399. if (xive_eas_is_valid(&xive->eat[lisn])) {
  400. error_setg(errp, "IRQ %d is not free", lisn);
  401. return -EBUSY;
  402. }
  403. /*
  404. * Set default values when allocating an IRQ number
  405. */
  406. xive->eat[lisn].w |= cpu_to_be64(EAS_VALID | EAS_MASKED);
  407. if (lsi) {
  408. xive_source_irq_set_lsi(xsrc, lisn);
  409. }
  410. if (kvm_irqchip_in_kernel()) {
  411. return kvmppc_xive_source_reset_one(xsrc, lisn, errp);
  412. }
  413. return 0;
  414. }
  415. static void spapr_xive_free_irq(SpaprInterruptController *intc, int lisn)
  416. {
  417. SpaprXive *xive = SPAPR_XIVE(intc);
  418. assert(lisn < xive->nr_irqs);
  419. xive->eat[lisn].w &= cpu_to_be64(~EAS_VALID);
  420. }
  421. static Property spapr_xive_properties[] = {
  422. DEFINE_PROP_UINT32("nr-irqs", SpaprXive, nr_irqs, 0),
  423. DEFINE_PROP_UINT32("nr-ends", SpaprXive, nr_ends, 0),
  424. DEFINE_PROP_UINT64("vc-base", SpaprXive, vc_base, SPAPR_XIVE_VC_BASE),
  425. DEFINE_PROP_UINT64("tm-base", SpaprXive, tm_base, SPAPR_XIVE_TM_BASE),
  426. DEFINE_PROP_END_OF_LIST(),
  427. };
  428. static int spapr_xive_cpu_intc_create(SpaprInterruptController *intc,
  429. PowerPCCPU *cpu, Error **errp)
  430. {
  431. SpaprXive *xive = SPAPR_XIVE(intc);
  432. Object *obj;
  433. SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
  434. obj = xive_tctx_create(OBJECT(cpu), XIVE_ROUTER(xive), errp);
  435. if (!obj) {
  436. return -1;
  437. }
  438. spapr_cpu->tctx = XIVE_TCTX(obj);
  439. return 0;
  440. }
  441. static void xive_tctx_set_os_cam(XiveTCTX *tctx, uint32_t os_cam)
  442. {
  443. uint32_t qw1w2 = cpu_to_be32(TM_QW1W2_VO | os_cam);
  444. memcpy(&tctx->regs[TM_QW1_OS + TM_WORD2], &qw1w2, 4);
  445. }
  446. static void spapr_xive_cpu_intc_reset(SpaprInterruptController *intc,
  447. PowerPCCPU *cpu)
  448. {
  449. XiveTCTX *tctx = spapr_cpu_state(cpu)->tctx;
  450. uint8_t nvt_blk;
  451. uint32_t nvt_idx;
  452. xive_tctx_reset(tctx);
  453. /*
  454. * When a Virtual Processor is scheduled to run on a HW thread,
  455. * the hypervisor pushes its identifier in the OS CAM line.
  456. * Emulate the same behavior under QEMU.
  457. */
  458. spapr_xive_cpu_to_nvt(cpu, &nvt_blk, &nvt_idx);
  459. xive_tctx_set_os_cam(tctx, xive_nvt_cam_line(nvt_blk, nvt_idx));
  460. }
  461. static void spapr_xive_cpu_intc_destroy(SpaprInterruptController *intc,
  462. PowerPCCPU *cpu)
  463. {
  464. SpaprCpuState *spapr_cpu = spapr_cpu_state(cpu);
  465. xive_tctx_destroy(spapr_cpu->tctx);
  466. spapr_cpu->tctx = NULL;
  467. }
  468. static void spapr_xive_set_irq(SpaprInterruptController *intc, int irq, int val)
  469. {
  470. SpaprXive *xive = SPAPR_XIVE(intc);
  471. if (kvm_irqchip_in_kernel()) {
  472. kvmppc_xive_source_set_irq(&xive->source, irq, val);
  473. } else {
  474. xive_source_set_irq(&xive->source, irq, val);
  475. }
  476. }
  477. static void spapr_xive_print_info(SpaprInterruptController *intc, Monitor *mon)
  478. {
  479. SpaprXive *xive = SPAPR_XIVE(intc);
  480. CPUState *cs;
  481. CPU_FOREACH(cs) {
  482. PowerPCCPU *cpu = POWERPC_CPU(cs);
  483. xive_tctx_pic_print_info(spapr_cpu_state(cpu)->tctx, mon);
  484. }
  485. spapr_xive_pic_print_info(xive, mon);
  486. }
  487. static void spapr_xive_dt(SpaprInterruptController *intc, uint32_t nr_servers,
  488. void *fdt, uint32_t phandle)
  489. {
  490. SpaprXive *xive = SPAPR_XIVE(intc);
  491. int node;
  492. uint64_t timas[2 * 2];
  493. /* Interrupt number ranges for the IPIs */
  494. uint32_t lisn_ranges[] = {
  495. cpu_to_be32(0),
  496. cpu_to_be32(nr_servers),
  497. };
  498. /*
  499. * EQ size - the sizes of pages supported by the system 4K, 64K,
  500. * 2M, 16M. We only advertise 64K for the moment.
  501. */
  502. uint32_t eq_sizes[] = {
  503. cpu_to_be32(16), /* 64K */
  504. };
  505. /*
  506. * The following array is in sync with the reserved priorities
  507. * defined by the 'spapr_xive_priority_is_reserved' routine.
  508. */
  509. uint32_t plat_res_int_priorities[] = {
  510. cpu_to_be32(7), /* start */
  511. cpu_to_be32(0xf8), /* count */
  512. };
  513. /* Thread Interrupt Management Area : User (ring 3) and OS (ring 2) */
  514. timas[0] = cpu_to_be64(xive->tm_base +
  515. XIVE_TM_USER_PAGE * (1ull << TM_SHIFT));
  516. timas[1] = cpu_to_be64(1ull << TM_SHIFT);
  517. timas[2] = cpu_to_be64(xive->tm_base +
  518. XIVE_TM_OS_PAGE * (1ull << TM_SHIFT));
  519. timas[3] = cpu_to_be64(1ull << TM_SHIFT);
  520. _FDT(node = fdt_add_subnode(fdt, 0, xive->nodename));
  521. _FDT(fdt_setprop_string(fdt, node, "device_type", "power-ivpe"));
  522. _FDT(fdt_setprop(fdt, node, "reg", timas, sizeof(timas)));
  523. _FDT(fdt_setprop_string(fdt, node, "compatible", "ibm,power-ivpe"));
  524. _FDT(fdt_setprop(fdt, node, "ibm,xive-eq-sizes", eq_sizes,
  525. sizeof(eq_sizes)));
  526. _FDT(fdt_setprop(fdt, node, "ibm,xive-lisn-ranges", lisn_ranges,
  527. sizeof(lisn_ranges)));
  528. /* For Linux to link the LSIs to the interrupt controller. */
  529. _FDT(fdt_setprop(fdt, node, "interrupt-controller", NULL, 0));
  530. _FDT(fdt_setprop_cell(fdt, node, "#interrupt-cells", 2));
  531. /* For SLOF */
  532. _FDT(fdt_setprop_cell(fdt, node, "linux,phandle", phandle));
  533. _FDT(fdt_setprop_cell(fdt, node, "phandle", phandle));
  534. /*
  535. * The "ibm,plat-res-int-priorities" property defines the priority
  536. * ranges reserved by the hypervisor
  537. */
  538. _FDT(fdt_setprop(fdt, 0, "ibm,plat-res-int-priorities",
  539. plat_res_int_priorities, sizeof(plat_res_int_priorities)));
  540. }
  541. static int spapr_xive_activate(SpaprInterruptController *intc, Error **errp)
  542. {
  543. SpaprXive *xive = SPAPR_XIVE(intc);
  544. if (kvm_enabled()) {
  545. int rc = spapr_irq_init_kvm(kvmppc_xive_connect, intc, errp);
  546. if (rc < 0) {
  547. return rc;
  548. }
  549. }
  550. /* Activate the XIVE MMIOs */
  551. spapr_xive_mmio_set_enabled(xive, true);
  552. return 0;
  553. }
  554. static void spapr_xive_deactivate(SpaprInterruptController *intc)
  555. {
  556. SpaprXive *xive = SPAPR_XIVE(intc);
  557. spapr_xive_mmio_set_enabled(xive, false);
  558. if (kvm_irqchip_in_kernel()) {
  559. kvmppc_xive_disconnect(intc);
  560. }
  561. }
  562. static void spapr_xive_class_init(ObjectClass *klass, void *data)
  563. {
  564. DeviceClass *dc = DEVICE_CLASS(klass);
  565. XiveRouterClass *xrc = XIVE_ROUTER_CLASS(klass);
  566. SpaprInterruptControllerClass *sicc = SPAPR_INTC_CLASS(klass);
  567. dc->desc = "sPAPR XIVE Interrupt Controller";
  568. dc->props = spapr_xive_properties;
  569. dc->realize = spapr_xive_realize;
  570. dc->vmsd = &vmstate_spapr_xive;
  571. xrc->get_eas = spapr_xive_get_eas;
  572. xrc->get_end = spapr_xive_get_end;
  573. xrc->write_end = spapr_xive_write_end;
  574. xrc->get_nvt = spapr_xive_get_nvt;
  575. xrc->write_nvt = spapr_xive_write_nvt;
  576. xrc->get_tctx = spapr_xive_get_tctx;
  577. sicc->activate = spapr_xive_activate;
  578. sicc->deactivate = spapr_xive_deactivate;
  579. sicc->cpu_intc_create = spapr_xive_cpu_intc_create;
  580. sicc->cpu_intc_reset = spapr_xive_cpu_intc_reset;
  581. sicc->cpu_intc_destroy = spapr_xive_cpu_intc_destroy;
  582. sicc->claim_irq = spapr_xive_claim_irq;
  583. sicc->free_irq = spapr_xive_free_irq;
  584. sicc->set_irq = spapr_xive_set_irq;
  585. sicc->print_info = spapr_xive_print_info;
  586. sicc->dt = spapr_xive_dt;
  587. sicc->post_load = spapr_xive_post_load;
  588. }
  589. static const TypeInfo spapr_xive_info = {
  590. .name = TYPE_SPAPR_XIVE,
  591. .parent = TYPE_XIVE_ROUTER,
  592. .instance_init = spapr_xive_instance_init,
  593. .instance_size = sizeof(SpaprXive),
  594. .class_init = spapr_xive_class_init,
  595. .interfaces = (InterfaceInfo[]) {
  596. { TYPE_SPAPR_INTC },
  597. { }
  598. },
  599. };
  600. static void spapr_xive_register_types(void)
  601. {
  602. type_register_static(&spapr_xive_info);
  603. }
  604. type_init(spapr_xive_register_types)
  605. /*
  606. * XIVE hcalls
  607. *
  608. * The terminology used by the XIVE hcalls is the following :
  609. *
  610. * TARGET vCPU number
  611. * EQ Event Queue assigned by OS to receive event data
  612. * ESB page for source interrupt management
  613. * LISN Logical Interrupt Source Number identifying a source in the
  614. * machine
  615. * EISN Effective Interrupt Source Number used by guest OS to
  616. * identify source in the guest
  617. *
  618. * The EAS, END, NVT structures are not exposed.
  619. */
  620. /*
  621. * Linux hosts under OPAL reserve priority 7 for their own escalation
  622. * interrupts (DD2.X POWER9). So we only allow the guest to use
  623. * priorities [0..6].
  624. */
  625. static bool spapr_xive_priority_is_reserved(uint8_t priority)
  626. {
  627. switch (priority) {
  628. case 0 ... 6:
  629. return false;
  630. case 7: /* OPAL escalation queue */
  631. default:
  632. return true;
  633. }
  634. }
  635. /*
  636. * The H_INT_GET_SOURCE_INFO hcall() is used to obtain the logical
  637. * real address of the MMIO page through which the Event State Buffer
  638. * entry associated with the value of the "lisn" parameter is managed.
  639. *
  640. * Parameters:
  641. * Input
  642. * - R4: "flags"
  643. * Bits 0-63 reserved
  644. * - R5: "lisn" is per "interrupts", "interrupt-map", or
  645. * "ibm,xive-lisn-ranges" properties, or as returned by the
  646. * ibm,query-interrupt-source-number RTAS call, or as returned
  647. * by the H_ALLOCATE_VAS_WINDOW hcall
  648. *
  649. * Output
  650. * - R4: "flags"
  651. * Bits 0-59: Reserved
  652. * Bit 60: H_INT_ESB must be used for Event State Buffer
  653. * management
  654. * Bit 61: 1 == LSI 0 == MSI
  655. * Bit 62: the full function page supports trigger
  656. * Bit 63: Store EOI Supported
  657. * - R5: Logical Real address of full function Event State Buffer
  658. * management page, -1 if H_INT_ESB hcall flag is set to 1.
  659. * - R6: Logical Real Address of trigger only Event State Buffer
  660. * management page or -1.
  661. * - R7: Power of 2 page size for the ESB management pages returned in
  662. * R5 and R6.
  663. */
  664. #define SPAPR_XIVE_SRC_H_INT_ESB PPC_BIT(60) /* ESB manage with H_INT_ESB */
  665. #define SPAPR_XIVE_SRC_LSI PPC_BIT(61) /* Virtual LSI type */
  666. #define SPAPR_XIVE_SRC_TRIGGER PPC_BIT(62) /* Trigger and management
  667. on same page */
  668. #define SPAPR_XIVE_SRC_STORE_EOI PPC_BIT(63) /* Store EOI support */
  669. static target_ulong h_int_get_source_info(PowerPCCPU *cpu,
  670. SpaprMachineState *spapr,
  671. target_ulong opcode,
  672. target_ulong *args)
  673. {
  674. SpaprXive *xive = spapr->xive;
  675. XiveSource *xsrc = &xive->source;
  676. target_ulong flags = args[0];
  677. target_ulong lisn = args[1];
  678. if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
  679. return H_FUNCTION;
  680. }
  681. if (flags) {
  682. return H_PARAMETER;
  683. }
  684. if (lisn >= xive->nr_irqs) {
  685. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
  686. lisn);
  687. return H_P2;
  688. }
  689. if (!xive_eas_is_valid(&xive->eat[lisn])) {
  690. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
  691. lisn);
  692. return H_P2;
  693. }
  694. /*
  695. * All sources are emulated under the main XIVE object and share
  696. * the same characteristics.
  697. */
  698. args[0] = 0;
  699. if (!xive_source_esb_has_2page(xsrc)) {
  700. args[0] |= SPAPR_XIVE_SRC_TRIGGER;
  701. }
  702. if (xsrc->esb_flags & XIVE_SRC_STORE_EOI) {
  703. args[0] |= SPAPR_XIVE_SRC_STORE_EOI;
  704. }
  705. /*
  706. * Force the use of the H_INT_ESB hcall in case of an LSI
  707. * interrupt. This is necessary under KVM to re-trigger the
  708. * interrupt if the level is still asserted
  709. */
  710. if (xive_source_irq_is_lsi(xsrc, lisn)) {
  711. args[0] |= SPAPR_XIVE_SRC_H_INT_ESB | SPAPR_XIVE_SRC_LSI;
  712. }
  713. if (!(args[0] & SPAPR_XIVE_SRC_H_INT_ESB)) {
  714. args[1] = xive->vc_base + xive_source_esb_mgmt(xsrc, lisn);
  715. } else {
  716. args[1] = -1;
  717. }
  718. if (xive_source_esb_has_2page(xsrc) &&
  719. !(args[0] & SPAPR_XIVE_SRC_H_INT_ESB)) {
  720. args[2] = xive->vc_base + xive_source_esb_page(xsrc, lisn);
  721. } else {
  722. args[2] = -1;
  723. }
  724. if (xive_source_esb_has_2page(xsrc)) {
  725. args[3] = xsrc->esb_shift - 1;
  726. } else {
  727. args[3] = xsrc->esb_shift;
  728. }
  729. return H_SUCCESS;
  730. }
  731. /*
  732. * The H_INT_SET_SOURCE_CONFIG hcall() is used to assign a Logical
  733. * Interrupt Source to a target. The Logical Interrupt Source is
  734. * designated with the "lisn" parameter and the target is designated
  735. * with the "target" and "priority" parameters. Upon return from the
  736. * hcall(), no additional interrupts will be directed to the old EQ.
  737. *
  738. * Parameters:
  739. * Input:
  740. * - R4: "flags"
  741. * Bits 0-61: Reserved
  742. * Bit 62: set the "eisn" in the EAS
  743. * Bit 63: masks the interrupt source in the hardware interrupt
  744. * control structure. An interrupt masked by this mechanism will
  745. * be dropped, but it's source state bits will still be
  746. * set. There is no race-free way of unmasking and restoring the
  747. * source. Thus this should only be used in interrupts that are
  748. * also masked at the source, and only in cases where the
  749. * interrupt is not meant to be used for a large amount of time
  750. * because no valid target exists for it for example
  751. * - R5: "lisn" is per "interrupts", "interrupt-map", or
  752. * "ibm,xive-lisn-ranges" properties, or as returned by the
  753. * ibm,query-interrupt-source-number RTAS call, or as returned by
  754. * the H_ALLOCATE_VAS_WINDOW hcall
  755. * - R6: "target" is per "ibm,ppc-interrupt-server#s" or
  756. * "ibm,ppc-interrupt-gserver#s"
  757. * - R7: "priority" is a valid priority not in
  758. * "ibm,plat-res-int-priorities"
  759. * - R8: "eisn" is the guest EISN associated with the "lisn"
  760. *
  761. * Output:
  762. * - None
  763. */
  764. #define SPAPR_XIVE_SRC_SET_EISN PPC_BIT(62)
  765. #define SPAPR_XIVE_SRC_MASK PPC_BIT(63)
  766. static target_ulong h_int_set_source_config(PowerPCCPU *cpu,
  767. SpaprMachineState *spapr,
  768. target_ulong opcode,
  769. target_ulong *args)
  770. {
  771. SpaprXive *xive = spapr->xive;
  772. XiveEAS eas, new_eas;
  773. target_ulong flags = args[0];
  774. target_ulong lisn = args[1];
  775. target_ulong target = args[2];
  776. target_ulong priority = args[3];
  777. target_ulong eisn = args[4];
  778. uint8_t end_blk;
  779. uint32_t end_idx;
  780. if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
  781. return H_FUNCTION;
  782. }
  783. if (flags & ~(SPAPR_XIVE_SRC_SET_EISN | SPAPR_XIVE_SRC_MASK)) {
  784. return H_PARAMETER;
  785. }
  786. if (lisn >= xive->nr_irqs) {
  787. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
  788. lisn);
  789. return H_P2;
  790. }
  791. eas = xive->eat[lisn];
  792. if (!xive_eas_is_valid(&eas)) {
  793. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
  794. lisn);
  795. return H_P2;
  796. }
  797. /* priority 0xff is used to reset the EAS */
  798. if (priority == 0xff) {
  799. new_eas.w = cpu_to_be64(EAS_VALID | EAS_MASKED);
  800. goto out;
  801. }
  802. if (flags & SPAPR_XIVE_SRC_MASK) {
  803. new_eas.w = eas.w | cpu_to_be64(EAS_MASKED);
  804. } else {
  805. new_eas.w = eas.w & cpu_to_be64(~EAS_MASKED);
  806. }
  807. if (spapr_xive_priority_is_reserved(priority)) {
  808. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
  809. " is reserved\n", priority);
  810. return H_P4;
  811. }
  812. /*
  813. * Validate that "target" is part of the list of threads allocated
  814. * to the partition. For that, find the END corresponding to the
  815. * target.
  816. */
  817. if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
  818. return H_P3;
  819. }
  820. new_eas.w = xive_set_field64(EAS_END_BLOCK, new_eas.w, end_blk);
  821. new_eas.w = xive_set_field64(EAS_END_INDEX, new_eas.w, end_idx);
  822. if (flags & SPAPR_XIVE_SRC_SET_EISN) {
  823. new_eas.w = xive_set_field64(EAS_END_DATA, new_eas.w, eisn);
  824. }
  825. if (kvm_irqchip_in_kernel()) {
  826. Error *local_err = NULL;
  827. kvmppc_xive_set_source_config(xive, lisn, &new_eas, &local_err);
  828. if (local_err) {
  829. error_report_err(local_err);
  830. return H_HARDWARE;
  831. }
  832. }
  833. out:
  834. xive->eat[lisn] = new_eas;
  835. return H_SUCCESS;
  836. }
  837. /*
  838. * The H_INT_GET_SOURCE_CONFIG hcall() is used to determine to which
  839. * target/priority pair is assigned to the specified Logical Interrupt
  840. * Source.
  841. *
  842. * Parameters:
  843. * Input:
  844. * - R4: "flags"
  845. * Bits 0-63 Reserved
  846. * - R5: "lisn" is per "interrupts", "interrupt-map", or
  847. * "ibm,xive-lisn-ranges" properties, or as returned by the
  848. * ibm,query-interrupt-source-number RTAS call, or as
  849. * returned by the H_ALLOCATE_VAS_WINDOW hcall
  850. *
  851. * Output:
  852. * - R4: Target to which the specified Logical Interrupt Source is
  853. * assigned
  854. * - R5: Priority to which the specified Logical Interrupt Source is
  855. * assigned
  856. * - R6: EISN for the specified Logical Interrupt Source (this will be
  857. * equivalent to the LISN if not changed by H_INT_SET_SOURCE_CONFIG)
  858. */
  859. static target_ulong h_int_get_source_config(PowerPCCPU *cpu,
  860. SpaprMachineState *spapr,
  861. target_ulong opcode,
  862. target_ulong *args)
  863. {
  864. SpaprXive *xive = spapr->xive;
  865. target_ulong flags = args[0];
  866. target_ulong lisn = args[1];
  867. XiveEAS eas;
  868. XiveEND *end;
  869. uint8_t nvt_blk;
  870. uint32_t end_idx, nvt_idx;
  871. if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
  872. return H_FUNCTION;
  873. }
  874. if (flags) {
  875. return H_PARAMETER;
  876. }
  877. if (lisn >= xive->nr_irqs) {
  878. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
  879. lisn);
  880. return H_P2;
  881. }
  882. eas = xive->eat[lisn];
  883. if (!xive_eas_is_valid(&eas)) {
  884. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
  885. lisn);
  886. return H_P2;
  887. }
  888. /* EAS_END_BLOCK is unused on sPAPR */
  889. end_idx = xive_get_field64(EAS_END_INDEX, eas.w);
  890. assert(end_idx < xive->nr_ends);
  891. end = &xive->endt[end_idx];
  892. nvt_blk = xive_get_field32(END_W6_NVT_BLOCK, end->w6);
  893. nvt_idx = xive_get_field32(END_W6_NVT_INDEX, end->w6);
  894. args[0] = spapr_xive_nvt_to_target(nvt_blk, nvt_idx);
  895. if (xive_eas_is_masked(&eas)) {
  896. args[1] = 0xff;
  897. } else {
  898. args[1] = xive_get_field32(END_W7_F0_PRIORITY, end->w7);
  899. }
  900. args[2] = xive_get_field64(EAS_END_DATA, eas.w);
  901. return H_SUCCESS;
  902. }
  903. /*
  904. * The H_INT_GET_QUEUE_INFO hcall() is used to get the logical real
  905. * address of the notification management page associated with the
  906. * specified target and priority.
  907. *
  908. * Parameters:
  909. * Input:
  910. * - R4: "flags"
  911. * Bits 0-63 Reserved
  912. * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
  913. * "ibm,ppc-interrupt-gserver#s"
  914. * - R6: "priority" is a valid priority not in
  915. * "ibm,plat-res-int-priorities"
  916. *
  917. * Output:
  918. * - R4: Logical real address of notification page
  919. * - R5: Power of 2 page size of the notification page
  920. */
  921. static target_ulong h_int_get_queue_info(PowerPCCPU *cpu,
  922. SpaprMachineState *spapr,
  923. target_ulong opcode,
  924. target_ulong *args)
  925. {
  926. SpaprXive *xive = spapr->xive;
  927. XiveENDSource *end_xsrc = &xive->end_source;
  928. target_ulong flags = args[0];
  929. target_ulong target = args[1];
  930. target_ulong priority = args[2];
  931. XiveEND *end;
  932. uint8_t end_blk;
  933. uint32_t end_idx;
  934. if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
  935. return H_FUNCTION;
  936. }
  937. if (flags) {
  938. return H_PARAMETER;
  939. }
  940. /*
  941. * H_STATE should be returned if a H_INT_RESET is in progress.
  942. * This is not needed when running the emulation under QEMU
  943. */
  944. if (spapr_xive_priority_is_reserved(priority)) {
  945. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
  946. " is reserved\n", priority);
  947. return H_P3;
  948. }
  949. /*
  950. * Validate that "target" is part of the list of threads allocated
  951. * to the partition. For that, find the END corresponding to the
  952. * target.
  953. */
  954. if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
  955. return H_P2;
  956. }
  957. assert(end_idx < xive->nr_ends);
  958. end = &xive->endt[end_idx];
  959. args[0] = xive->end_base + (1ull << (end_xsrc->esb_shift + 1)) * end_idx;
  960. if (xive_end_is_enqueue(end)) {
  961. args[1] = xive_get_field32(END_W0_QSIZE, end->w0) + 12;
  962. } else {
  963. args[1] = 0;
  964. }
  965. return H_SUCCESS;
  966. }
  967. /*
  968. * The H_INT_SET_QUEUE_CONFIG hcall() is used to set or reset a EQ for
  969. * a given "target" and "priority". It is also used to set the
  970. * notification config associated with the EQ. An EQ size of 0 is
  971. * used to reset the EQ config for a given target and priority. If
  972. * resetting the EQ config, the END associated with the given "target"
  973. * and "priority" will be changed to disable queueing.
  974. *
  975. * Upon return from the hcall(), no additional interrupts will be
  976. * directed to the old EQ (if one was set). The old EQ (if one was
  977. * set) should be investigated for interrupts that occurred prior to
  978. * or during the hcall().
  979. *
  980. * Parameters:
  981. * Input:
  982. * - R4: "flags"
  983. * Bits 0-62: Reserved
  984. * Bit 63: Unconditional Notify (n) per the XIVE spec
  985. * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
  986. * "ibm,ppc-interrupt-gserver#s"
  987. * - R6: "priority" is a valid priority not in
  988. * "ibm,plat-res-int-priorities"
  989. * - R7: "eventQueue": The logical real address of the start of the EQ
  990. * - R8: "eventQueueSize": The power of 2 EQ size per "ibm,xive-eq-sizes"
  991. *
  992. * Output:
  993. * - None
  994. */
  995. #define SPAPR_XIVE_END_ALWAYS_NOTIFY PPC_BIT(63)
  996. static target_ulong h_int_set_queue_config(PowerPCCPU *cpu,
  997. SpaprMachineState *spapr,
  998. target_ulong opcode,
  999. target_ulong *args)
  1000. {
  1001. SpaprXive *xive = spapr->xive;
  1002. target_ulong flags = args[0];
  1003. target_ulong target = args[1];
  1004. target_ulong priority = args[2];
  1005. target_ulong qpage = args[3];
  1006. target_ulong qsize = args[4];
  1007. XiveEND end;
  1008. uint8_t end_blk, nvt_blk;
  1009. uint32_t end_idx, nvt_idx;
  1010. if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
  1011. return H_FUNCTION;
  1012. }
  1013. if (flags & ~SPAPR_XIVE_END_ALWAYS_NOTIFY) {
  1014. return H_PARAMETER;
  1015. }
  1016. /*
  1017. * H_STATE should be returned if a H_INT_RESET is in progress.
  1018. * This is not needed when running the emulation under QEMU
  1019. */
  1020. if (spapr_xive_priority_is_reserved(priority)) {
  1021. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
  1022. " is reserved\n", priority);
  1023. return H_P3;
  1024. }
  1025. /*
  1026. * Validate that "target" is part of the list of threads allocated
  1027. * to the partition. For that, find the END corresponding to the
  1028. * target.
  1029. */
  1030. if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
  1031. return H_P2;
  1032. }
  1033. assert(end_idx < xive->nr_ends);
  1034. memcpy(&end, &xive->endt[end_idx], sizeof(XiveEND));
  1035. switch (qsize) {
  1036. case 12:
  1037. case 16:
  1038. case 21:
  1039. case 24:
  1040. if (!QEMU_IS_ALIGNED(qpage, 1ul << qsize)) {
  1041. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: EQ @0x%" HWADDR_PRIx
  1042. " is not naturally aligned with %" HWADDR_PRIx "\n",
  1043. qpage, (hwaddr)1 << qsize);
  1044. return H_P4;
  1045. }
  1046. end.w2 = cpu_to_be32((qpage >> 32) & 0x0fffffff);
  1047. end.w3 = cpu_to_be32(qpage & 0xffffffff);
  1048. end.w0 |= cpu_to_be32(END_W0_ENQUEUE);
  1049. end.w0 = xive_set_field32(END_W0_QSIZE, end.w0, qsize - 12);
  1050. break;
  1051. case 0:
  1052. /* reset queue and disable queueing */
  1053. spapr_xive_end_reset(&end);
  1054. goto out;
  1055. default:
  1056. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid EQ size %"PRIx64"\n",
  1057. qsize);
  1058. return H_P5;
  1059. }
  1060. if (qsize) {
  1061. hwaddr plen = 1 << qsize;
  1062. void *eq;
  1063. /*
  1064. * Validate the guest EQ. We should also check that the queue
  1065. * has been zeroed by the OS.
  1066. */
  1067. eq = address_space_map(CPU(cpu)->as, qpage, &plen, true,
  1068. MEMTXATTRS_UNSPECIFIED);
  1069. if (plen != 1 << qsize) {
  1070. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to map EQ @0x%"
  1071. HWADDR_PRIx "\n", qpage);
  1072. return H_P4;
  1073. }
  1074. address_space_unmap(CPU(cpu)->as, eq, plen, true, plen);
  1075. }
  1076. /* "target" should have been validated above */
  1077. if (spapr_xive_target_to_nvt(target, &nvt_blk, &nvt_idx)) {
  1078. g_assert_not_reached();
  1079. }
  1080. /*
  1081. * Ensure the priority and target are correctly set (they will not
  1082. * be right after allocation)
  1083. */
  1084. end.w6 = xive_set_field32(END_W6_NVT_BLOCK, 0ul, nvt_blk) |
  1085. xive_set_field32(END_W6_NVT_INDEX, 0ul, nvt_idx);
  1086. end.w7 = xive_set_field32(END_W7_F0_PRIORITY, 0ul, priority);
  1087. if (flags & SPAPR_XIVE_END_ALWAYS_NOTIFY) {
  1088. end.w0 |= cpu_to_be32(END_W0_UCOND_NOTIFY);
  1089. } else {
  1090. end.w0 &= cpu_to_be32((uint32_t)~END_W0_UCOND_NOTIFY);
  1091. }
  1092. /*
  1093. * The generation bit for the END starts at 1 and The END page
  1094. * offset counter starts at 0.
  1095. */
  1096. end.w1 = cpu_to_be32(END_W1_GENERATION) |
  1097. xive_set_field32(END_W1_PAGE_OFF, 0ul, 0ul);
  1098. end.w0 |= cpu_to_be32(END_W0_VALID);
  1099. /*
  1100. * TODO: issue syncs required to ensure all in-flight interrupts
  1101. * are complete on the old END
  1102. */
  1103. out:
  1104. if (kvm_irqchip_in_kernel()) {
  1105. Error *local_err = NULL;
  1106. kvmppc_xive_set_queue_config(xive, end_blk, end_idx, &end, &local_err);
  1107. if (local_err) {
  1108. error_report_err(local_err);
  1109. return H_HARDWARE;
  1110. }
  1111. }
  1112. /* Update END */
  1113. memcpy(&xive->endt[end_idx], &end, sizeof(XiveEND));
  1114. return H_SUCCESS;
  1115. }
  1116. /*
  1117. * The H_INT_GET_QUEUE_CONFIG hcall() is used to get a EQ for a given
  1118. * target and priority.
  1119. *
  1120. * Parameters:
  1121. * Input:
  1122. * - R4: "flags"
  1123. * Bits 0-62: Reserved
  1124. * Bit 63: Debug: Return debug data
  1125. * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
  1126. * "ibm,ppc-interrupt-gserver#s"
  1127. * - R6: "priority" is a valid priority not in
  1128. * "ibm,plat-res-int-priorities"
  1129. *
  1130. * Output:
  1131. * - R4: "flags":
  1132. * Bits 0-61: Reserved
  1133. * Bit 62: The value of Event Queue Generation Number (g) per
  1134. * the XIVE spec if "Debug" = 1
  1135. * Bit 63: The value of Unconditional Notify (n) per the XIVE spec
  1136. * - R5: The logical real address of the start of the EQ
  1137. * - R6: The power of 2 EQ size per "ibm,xive-eq-sizes"
  1138. * - R7: The value of Event Queue Offset Counter per XIVE spec
  1139. * if "Debug" = 1, else 0
  1140. *
  1141. */
  1142. #define SPAPR_XIVE_END_DEBUG PPC_BIT(63)
  1143. static target_ulong h_int_get_queue_config(PowerPCCPU *cpu,
  1144. SpaprMachineState *spapr,
  1145. target_ulong opcode,
  1146. target_ulong *args)
  1147. {
  1148. SpaprXive *xive = spapr->xive;
  1149. target_ulong flags = args[0];
  1150. target_ulong target = args[1];
  1151. target_ulong priority = args[2];
  1152. XiveEND *end;
  1153. uint8_t end_blk;
  1154. uint32_t end_idx;
  1155. if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
  1156. return H_FUNCTION;
  1157. }
  1158. if (flags & ~SPAPR_XIVE_END_DEBUG) {
  1159. return H_PARAMETER;
  1160. }
  1161. /*
  1162. * H_STATE should be returned if a H_INT_RESET is in progress.
  1163. * This is not needed when running the emulation under QEMU
  1164. */
  1165. if (spapr_xive_priority_is_reserved(priority)) {
  1166. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: priority " TARGET_FMT_ld
  1167. " is reserved\n", priority);
  1168. return H_P3;
  1169. }
  1170. /*
  1171. * Validate that "target" is part of the list of threads allocated
  1172. * to the partition. For that, find the END corresponding to the
  1173. * target.
  1174. */
  1175. if (spapr_xive_target_to_end(target, priority, &end_blk, &end_idx)) {
  1176. return H_P2;
  1177. }
  1178. assert(end_idx < xive->nr_ends);
  1179. end = &xive->endt[end_idx];
  1180. args[0] = 0;
  1181. if (xive_end_is_notify(end)) {
  1182. args[0] |= SPAPR_XIVE_END_ALWAYS_NOTIFY;
  1183. }
  1184. if (xive_end_is_enqueue(end)) {
  1185. args[1] = xive_end_qaddr(end);
  1186. args[2] = xive_get_field32(END_W0_QSIZE, end->w0) + 12;
  1187. } else {
  1188. args[1] = 0;
  1189. args[2] = 0;
  1190. }
  1191. if (kvm_irqchip_in_kernel()) {
  1192. Error *local_err = NULL;
  1193. kvmppc_xive_get_queue_config(xive, end_blk, end_idx, end, &local_err);
  1194. if (local_err) {
  1195. error_report_err(local_err);
  1196. return H_HARDWARE;
  1197. }
  1198. }
  1199. /* TODO: do we need any locking on the END ? */
  1200. if (flags & SPAPR_XIVE_END_DEBUG) {
  1201. /* Load the event queue generation number into the return flags */
  1202. args[0] |= (uint64_t)xive_get_field32(END_W1_GENERATION, end->w1) << 62;
  1203. /* Load R7 with the event queue offset counter */
  1204. args[3] = xive_get_field32(END_W1_PAGE_OFF, end->w1);
  1205. } else {
  1206. args[3] = 0;
  1207. }
  1208. return H_SUCCESS;
  1209. }
  1210. /*
  1211. * The H_INT_SET_OS_REPORTING_LINE hcall() is used to set the
  1212. * reporting cache line pair for the calling thread. The reporting
  1213. * cache lines will contain the OS interrupt context when the OS
  1214. * issues a CI store byte to @TIMA+0xC10 to acknowledge the OS
  1215. * interrupt. The reporting cache lines can be reset by inputting -1
  1216. * in "reportingLine". Issuing the CI store byte without reporting
  1217. * cache lines registered will result in the data not being accessible
  1218. * to the OS.
  1219. *
  1220. * Parameters:
  1221. * Input:
  1222. * - R4: "flags"
  1223. * Bits 0-63: Reserved
  1224. * - R5: "reportingLine": The logical real address of the reporting cache
  1225. * line pair
  1226. *
  1227. * Output:
  1228. * - None
  1229. */
  1230. static target_ulong h_int_set_os_reporting_line(PowerPCCPU *cpu,
  1231. SpaprMachineState *spapr,
  1232. target_ulong opcode,
  1233. target_ulong *args)
  1234. {
  1235. if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
  1236. return H_FUNCTION;
  1237. }
  1238. /*
  1239. * H_STATE should be returned if a H_INT_RESET is in progress.
  1240. * This is not needed when running the emulation under QEMU
  1241. */
  1242. /* TODO: H_INT_SET_OS_REPORTING_LINE */
  1243. return H_FUNCTION;
  1244. }
  1245. /*
  1246. * The H_INT_GET_OS_REPORTING_LINE hcall() is used to get the logical
  1247. * real address of the reporting cache line pair set for the input
  1248. * "target". If no reporting cache line pair has been set, -1 is
  1249. * returned.
  1250. *
  1251. * Parameters:
  1252. * Input:
  1253. * - R4: "flags"
  1254. * Bits 0-63: Reserved
  1255. * - R5: "target" is per "ibm,ppc-interrupt-server#s" or
  1256. * "ibm,ppc-interrupt-gserver#s"
  1257. * - R6: "reportingLine": The logical real address of the reporting
  1258. * cache line pair
  1259. *
  1260. * Output:
  1261. * - R4: The logical real address of the reporting line if set, else -1
  1262. */
  1263. static target_ulong h_int_get_os_reporting_line(PowerPCCPU *cpu,
  1264. SpaprMachineState *spapr,
  1265. target_ulong opcode,
  1266. target_ulong *args)
  1267. {
  1268. if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
  1269. return H_FUNCTION;
  1270. }
  1271. /*
  1272. * H_STATE should be returned if a H_INT_RESET is in progress.
  1273. * This is not needed when running the emulation under QEMU
  1274. */
  1275. /* TODO: H_INT_GET_OS_REPORTING_LINE */
  1276. return H_FUNCTION;
  1277. }
  1278. /*
  1279. * The H_INT_ESB hcall() is used to issue a load or store to the ESB
  1280. * page for the input "lisn". This hcall is only supported for LISNs
  1281. * that have the ESB hcall flag set to 1 when returned from hcall()
  1282. * H_INT_GET_SOURCE_INFO.
  1283. *
  1284. * Parameters:
  1285. * Input:
  1286. * - R4: "flags"
  1287. * Bits 0-62: Reserved
  1288. * bit 63: Store: Store=1, store operation, else load operation
  1289. * - R5: "lisn" is per "interrupts", "interrupt-map", or
  1290. * "ibm,xive-lisn-ranges" properties, or as returned by the
  1291. * ibm,query-interrupt-source-number RTAS call, or as
  1292. * returned by the H_ALLOCATE_VAS_WINDOW hcall
  1293. * - R6: "esbOffset" is the offset into the ESB page for the load or
  1294. * store operation
  1295. * - R7: "storeData" is the data to write for a store operation
  1296. *
  1297. * Output:
  1298. * - R4: The value of the load if load operation, else -1
  1299. */
  1300. #define SPAPR_XIVE_ESB_STORE PPC_BIT(63)
  1301. static target_ulong h_int_esb(PowerPCCPU *cpu,
  1302. SpaprMachineState *spapr,
  1303. target_ulong opcode,
  1304. target_ulong *args)
  1305. {
  1306. SpaprXive *xive = spapr->xive;
  1307. XiveEAS eas;
  1308. target_ulong flags = args[0];
  1309. target_ulong lisn = args[1];
  1310. target_ulong offset = args[2];
  1311. target_ulong data = args[3];
  1312. hwaddr mmio_addr;
  1313. XiveSource *xsrc = &xive->source;
  1314. if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
  1315. return H_FUNCTION;
  1316. }
  1317. if (flags & ~SPAPR_XIVE_ESB_STORE) {
  1318. return H_PARAMETER;
  1319. }
  1320. if (lisn >= xive->nr_irqs) {
  1321. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
  1322. lisn);
  1323. return H_P2;
  1324. }
  1325. eas = xive->eat[lisn];
  1326. if (!xive_eas_is_valid(&eas)) {
  1327. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
  1328. lisn);
  1329. return H_P2;
  1330. }
  1331. if (offset > (1ull << xsrc->esb_shift)) {
  1332. return H_P3;
  1333. }
  1334. if (kvm_irqchip_in_kernel()) {
  1335. args[0] = kvmppc_xive_esb_rw(xsrc, lisn, offset, data,
  1336. flags & SPAPR_XIVE_ESB_STORE);
  1337. } else {
  1338. mmio_addr = xive->vc_base + xive_source_esb_mgmt(xsrc, lisn) + offset;
  1339. if (dma_memory_rw(&address_space_memory, mmio_addr, &data, 8,
  1340. (flags & SPAPR_XIVE_ESB_STORE))) {
  1341. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to access ESB @0x%"
  1342. HWADDR_PRIx "\n", mmio_addr);
  1343. return H_HARDWARE;
  1344. }
  1345. args[0] = (flags & SPAPR_XIVE_ESB_STORE) ? -1 : data;
  1346. }
  1347. return H_SUCCESS;
  1348. }
  1349. /*
  1350. * The H_INT_SYNC hcall() is used to issue hardware syncs that will
  1351. * ensure any in flight events for the input lisn are in the event
  1352. * queue.
  1353. *
  1354. * Parameters:
  1355. * Input:
  1356. * - R4: "flags"
  1357. * Bits 0-63: Reserved
  1358. * - R5: "lisn" is per "interrupts", "interrupt-map", or
  1359. * "ibm,xive-lisn-ranges" properties, or as returned by the
  1360. * ibm,query-interrupt-source-number RTAS call, or as
  1361. * returned by the H_ALLOCATE_VAS_WINDOW hcall
  1362. *
  1363. * Output:
  1364. * - None
  1365. */
  1366. static target_ulong h_int_sync(PowerPCCPU *cpu,
  1367. SpaprMachineState *spapr,
  1368. target_ulong opcode,
  1369. target_ulong *args)
  1370. {
  1371. SpaprXive *xive = spapr->xive;
  1372. XiveEAS eas;
  1373. target_ulong flags = args[0];
  1374. target_ulong lisn = args[1];
  1375. if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
  1376. return H_FUNCTION;
  1377. }
  1378. if (flags) {
  1379. return H_PARAMETER;
  1380. }
  1381. if (lisn >= xive->nr_irqs) {
  1382. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN " TARGET_FMT_lx "\n",
  1383. lisn);
  1384. return H_P2;
  1385. }
  1386. eas = xive->eat[lisn];
  1387. if (!xive_eas_is_valid(&eas)) {
  1388. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN " TARGET_FMT_lx "\n",
  1389. lisn);
  1390. return H_P2;
  1391. }
  1392. /*
  1393. * H_STATE should be returned if a H_INT_RESET is in progress.
  1394. * This is not needed when running the emulation under QEMU
  1395. */
  1396. /*
  1397. * This is not real hardware. Nothing to be done unless when
  1398. * under KVM
  1399. */
  1400. if (kvm_irqchip_in_kernel()) {
  1401. Error *local_err = NULL;
  1402. kvmppc_xive_sync_source(xive, lisn, &local_err);
  1403. if (local_err) {
  1404. error_report_err(local_err);
  1405. return H_HARDWARE;
  1406. }
  1407. }
  1408. return H_SUCCESS;
  1409. }
  1410. /*
  1411. * The H_INT_RESET hcall() is used to reset all of the partition's
  1412. * interrupt exploitation structures to their initial state. This
  1413. * means losing all previously set interrupt state set via
  1414. * H_INT_SET_SOURCE_CONFIG and H_INT_SET_QUEUE_CONFIG.
  1415. *
  1416. * Parameters:
  1417. * Input:
  1418. * - R4: "flags"
  1419. * Bits 0-63: Reserved
  1420. *
  1421. * Output:
  1422. * - None
  1423. */
  1424. static target_ulong h_int_reset(PowerPCCPU *cpu,
  1425. SpaprMachineState *spapr,
  1426. target_ulong opcode,
  1427. target_ulong *args)
  1428. {
  1429. SpaprXive *xive = spapr->xive;
  1430. target_ulong flags = args[0];
  1431. if (!spapr_ovec_test(spapr->ov5_cas, OV5_XIVE_EXPLOIT)) {
  1432. return H_FUNCTION;
  1433. }
  1434. if (flags) {
  1435. return H_PARAMETER;
  1436. }
  1437. device_reset(DEVICE(xive));
  1438. if (kvm_irqchip_in_kernel()) {
  1439. Error *local_err = NULL;
  1440. kvmppc_xive_reset(xive, &local_err);
  1441. if (local_err) {
  1442. error_report_err(local_err);
  1443. return H_HARDWARE;
  1444. }
  1445. }
  1446. return H_SUCCESS;
  1447. }
  1448. void spapr_xive_hcall_init(SpaprMachineState *spapr)
  1449. {
  1450. spapr_register_hypercall(H_INT_GET_SOURCE_INFO, h_int_get_source_info);
  1451. spapr_register_hypercall(H_INT_SET_SOURCE_CONFIG, h_int_set_source_config);
  1452. spapr_register_hypercall(H_INT_GET_SOURCE_CONFIG, h_int_get_source_config);
  1453. spapr_register_hypercall(H_INT_GET_QUEUE_INFO, h_int_get_queue_info);
  1454. spapr_register_hypercall(H_INT_SET_QUEUE_CONFIG, h_int_set_queue_config);
  1455. spapr_register_hypercall(H_INT_GET_QUEUE_CONFIG, h_int_get_queue_config);
  1456. spapr_register_hypercall(H_INT_SET_OS_REPORTING_LINE,
  1457. h_int_set_os_reporting_line);
  1458. spapr_register_hypercall(H_INT_GET_OS_REPORTING_LINE,
  1459. h_int_get_os_reporting_line);
  1460. spapr_register_hypercall(H_INT_ESB, h_int_esb);
  1461. spapr_register_hypercall(H_INT_SYNC, h_int_sync);
  1462. spapr_register_hypercall(H_INT_RESET, h_int_reset);
  1463. }