2
0

spapr_xive.c 55 KB

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