xive.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949
  1. /*
  2. * QEMU PowerPC 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 "target/ppc/cpu.h"
  14. #include "sysemu/cpus.h"
  15. #include "sysemu/dma.h"
  16. #include "sysemu/reset.h"
  17. #include "hw/qdev-properties.h"
  18. #include "migration/vmstate.h"
  19. #include "monitor/monitor.h"
  20. #include "hw/irq.h"
  21. #include "hw/ppc/xive.h"
  22. #include "hw/ppc/xive_regs.h"
  23. /*
  24. * XIVE Thread Interrupt Management context
  25. */
  26. /*
  27. * Convert a priority number to an Interrupt Pending Buffer (IPB)
  28. * register, which indicates a pending interrupt at the priority
  29. * corresponding to the bit number
  30. */
  31. static uint8_t priority_to_ipb(uint8_t priority)
  32. {
  33. return priority > XIVE_PRIORITY_MAX ?
  34. 0 : 1 << (XIVE_PRIORITY_MAX - priority);
  35. }
  36. /*
  37. * Convert an Interrupt Pending Buffer (IPB) register to a Pending
  38. * Interrupt Priority Register (PIPR), which contains the priority of
  39. * the most favored pending notification.
  40. */
  41. static uint8_t ipb_to_pipr(uint8_t ibp)
  42. {
  43. return ibp ? clz32((uint32_t)ibp << 24) : 0xff;
  44. }
  45. static uint8_t exception_mask(uint8_t ring)
  46. {
  47. switch (ring) {
  48. case TM_QW1_OS:
  49. return TM_QW1_NSR_EO;
  50. case TM_QW3_HV_PHYS:
  51. return TM_QW3_NSR_HE;
  52. default:
  53. g_assert_not_reached();
  54. }
  55. }
  56. static qemu_irq xive_tctx_output(XiveTCTX *tctx, uint8_t ring)
  57. {
  58. switch (ring) {
  59. case TM_QW0_USER:
  60. return 0; /* Not supported */
  61. case TM_QW1_OS:
  62. return tctx->os_output;
  63. case TM_QW2_HV_POOL:
  64. case TM_QW3_HV_PHYS:
  65. return tctx->hv_output;
  66. default:
  67. return 0;
  68. }
  69. }
  70. static uint64_t xive_tctx_accept(XiveTCTX *tctx, uint8_t ring)
  71. {
  72. uint8_t *regs = &tctx->regs[ring];
  73. uint8_t nsr = regs[TM_NSR];
  74. uint8_t mask = exception_mask(ring);
  75. qemu_irq_lower(xive_tctx_output(tctx, ring));
  76. if (regs[TM_NSR] & mask) {
  77. uint8_t cppr = regs[TM_PIPR];
  78. regs[TM_CPPR] = cppr;
  79. /* Reset the pending buffer bit */
  80. regs[TM_IPB] &= ~priority_to_ipb(cppr);
  81. regs[TM_PIPR] = ipb_to_pipr(regs[TM_IPB]);
  82. /* Drop Exception bit */
  83. regs[TM_NSR] &= ~mask;
  84. }
  85. return (nsr << 8) | regs[TM_CPPR];
  86. }
  87. static void xive_tctx_notify(XiveTCTX *tctx, uint8_t ring)
  88. {
  89. uint8_t *regs = &tctx->regs[ring];
  90. if (regs[TM_PIPR] < regs[TM_CPPR]) {
  91. switch (ring) {
  92. case TM_QW1_OS:
  93. regs[TM_NSR] |= TM_QW1_NSR_EO;
  94. break;
  95. case TM_QW3_HV_PHYS:
  96. regs[TM_NSR] |= (TM_QW3_NSR_HE_PHYS << 6);
  97. break;
  98. default:
  99. g_assert_not_reached();
  100. }
  101. qemu_irq_raise(xive_tctx_output(tctx, ring));
  102. }
  103. }
  104. static void xive_tctx_set_cppr(XiveTCTX *tctx, uint8_t ring, uint8_t cppr)
  105. {
  106. if (cppr > XIVE_PRIORITY_MAX) {
  107. cppr = 0xff;
  108. }
  109. tctx->regs[ring + TM_CPPR] = cppr;
  110. /* CPPR has changed, check if we need to raise a pending exception */
  111. xive_tctx_notify(tctx, ring);
  112. }
  113. void xive_tctx_ipb_update(XiveTCTX *tctx, uint8_t ring, uint8_t ipb)
  114. {
  115. uint8_t *regs = &tctx->regs[ring];
  116. regs[TM_IPB] |= ipb;
  117. regs[TM_PIPR] = ipb_to_pipr(regs[TM_IPB]);
  118. xive_tctx_notify(tctx, ring);
  119. }
  120. static inline uint32_t xive_tctx_word2(uint8_t *ring)
  121. {
  122. return *((uint32_t *) &ring[TM_WORD2]);
  123. }
  124. /*
  125. * XIVE Thread Interrupt Management Area (TIMA)
  126. */
  127. static void xive_tm_set_hv_cppr(XivePresenter *xptr, XiveTCTX *tctx,
  128. hwaddr offset, uint64_t value, unsigned size)
  129. {
  130. xive_tctx_set_cppr(tctx, TM_QW3_HV_PHYS, value & 0xff);
  131. }
  132. static uint64_t xive_tm_ack_hv_reg(XivePresenter *xptr, XiveTCTX *tctx,
  133. hwaddr offset, unsigned size)
  134. {
  135. return xive_tctx_accept(tctx, TM_QW3_HV_PHYS);
  136. }
  137. static uint64_t xive_tm_pull_pool_ctx(XivePresenter *xptr, XiveTCTX *tctx,
  138. hwaddr offset, unsigned size)
  139. {
  140. uint32_t qw2w2_prev = xive_tctx_word2(&tctx->regs[TM_QW2_HV_POOL]);
  141. uint32_t qw2w2;
  142. qw2w2 = xive_set_field32(TM_QW2W2_VP, qw2w2_prev, 0);
  143. memcpy(&tctx->regs[TM_QW2_HV_POOL + TM_WORD2], &qw2w2, 4);
  144. return qw2w2;
  145. }
  146. static void xive_tm_vt_push(XivePresenter *xptr, XiveTCTX *tctx, hwaddr offset,
  147. uint64_t value, unsigned size)
  148. {
  149. tctx->regs[TM_QW3_HV_PHYS + TM_WORD2] = value & 0xff;
  150. }
  151. static uint64_t xive_tm_vt_poll(XivePresenter *xptr, XiveTCTX *tctx,
  152. hwaddr offset, unsigned size)
  153. {
  154. return tctx->regs[TM_QW3_HV_PHYS + TM_WORD2] & 0xff;
  155. }
  156. /*
  157. * Define an access map for each page of the TIMA that we will use in
  158. * the memory region ops to filter values when doing loads and stores
  159. * of raw registers values
  160. *
  161. * Registers accessibility bits :
  162. *
  163. * 0x0 - no access
  164. * 0x1 - write only
  165. * 0x2 - read only
  166. * 0x3 - read/write
  167. */
  168. static const uint8_t xive_tm_hw_view[] = {
  169. 3, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 0, /* QW-0 User */
  170. 3, 3, 3, 3, 3, 3, 0, 2, 3, 3, 3, 3, 0, 0, 0, 0, /* QW-1 OS */
  171. 0, 0, 3, 3, 0, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 0, /* QW-2 POOL */
  172. 3, 3, 3, 3, 0, 3, 0, 2, 3, 0, 0, 3, 3, 3, 3, 0, /* QW-3 PHYS */
  173. };
  174. static const uint8_t xive_tm_hv_view[] = {
  175. 3, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 0, /* QW-0 User */
  176. 3, 3, 3, 3, 3, 3, 0, 2, 3, 3, 3, 3, 0, 0, 0, 0, /* QW-1 OS */
  177. 0, 0, 3, 3, 0, 0, 0, 0, 0, 3, 3, 3, 0, 0, 0, 0, /* QW-2 POOL */
  178. 3, 3, 3, 3, 0, 3, 0, 2, 3, 0, 0, 3, 0, 0, 0, 0, /* QW-3 PHYS */
  179. };
  180. static const uint8_t xive_tm_os_view[] = {
  181. 3, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 0, 0, 0, 0, /* QW-0 User */
  182. 2, 3, 2, 2, 2, 2, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, /* QW-1 OS */
  183. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* QW-2 POOL */
  184. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* QW-3 PHYS */
  185. };
  186. static const uint8_t xive_tm_user_view[] = {
  187. 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* QW-0 User */
  188. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* QW-1 OS */
  189. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* QW-2 POOL */
  190. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* QW-3 PHYS */
  191. };
  192. /*
  193. * Overall TIMA access map for the thread interrupt management context
  194. * registers
  195. */
  196. static const uint8_t *xive_tm_views[] = {
  197. [XIVE_TM_HW_PAGE] = xive_tm_hw_view,
  198. [XIVE_TM_HV_PAGE] = xive_tm_hv_view,
  199. [XIVE_TM_OS_PAGE] = xive_tm_os_view,
  200. [XIVE_TM_USER_PAGE] = xive_tm_user_view,
  201. };
  202. /*
  203. * Computes a register access mask for a given offset in the TIMA
  204. */
  205. static uint64_t xive_tm_mask(hwaddr offset, unsigned size, bool write)
  206. {
  207. uint8_t page_offset = (offset >> TM_SHIFT) & 0x3;
  208. uint8_t reg_offset = offset & 0x3F;
  209. uint8_t reg_mask = write ? 0x1 : 0x2;
  210. uint64_t mask = 0x0;
  211. int i;
  212. for (i = 0; i < size; i++) {
  213. if (xive_tm_views[page_offset][reg_offset + i] & reg_mask) {
  214. mask |= (uint64_t) 0xff << (8 * (size - i - 1));
  215. }
  216. }
  217. return mask;
  218. }
  219. static void xive_tm_raw_write(XiveTCTX *tctx, hwaddr offset, uint64_t value,
  220. unsigned size)
  221. {
  222. uint8_t ring_offset = offset & 0x30;
  223. uint8_t reg_offset = offset & 0x3F;
  224. uint64_t mask = xive_tm_mask(offset, size, true);
  225. int i;
  226. /*
  227. * Only 4 or 8 bytes stores are allowed and the User ring is
  228. * excluded
  229. */
  230. if (size < 4 || !mask || ring_offset == TM_QW0_USER) {
  231. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid write access at TIMA @%"
  232. HWADDR_PRIx"\n", offset);
  233. return;
  234. }
  235. /*
  236. * Use the register offset for the raw values and filter out
  237. * reserved values
  238. */
  239. for (i = 0; i < size; i++) {
  240. uint8_t byte_mask = (mask >> (8 * (size - i - 1)));
  241. if (byte_mask) {
  242. tctx->regs[reg_offset + i] = (value >> (8 * (size - i - 1))) &
  243. byte_mask;
  244. }
  245. }
  246. }
  247. static uint64_t xive_tm_raw_read(XiveTCTX *tctx, hwaddr offset, unsigned size)
  248. {
  249. uint8_t ring_offset = offset & 0x30;
  250. uint8_t reg_offset = offset & 0x3F;
  251. uint64_t mask = xive_tm_mask(offset, size, false);
  252. uint64_t ret;
  253. int i;
  254. /*
  255. * Only 4 or 8 bytes loads are allowed and the User ring is
  256. * excluded
  257. */
  258. if (size < 4 || !mask || ring_offset == TM_QW0_USER) {
  259. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid read access at TIMA @%"
  260. HWADDR_PRIx"\n", offset);
  261. return -1;
  262. }
  263. /* Use the register offset for the raw values */
  264. ret = 0;
  265. for (i = 0; i < size; i++) {
  266. ret |= (uint64_t) tctx->regs[reg_offset + i] << (8 * (size - i - 1));
  267. }
  268. /* filter out reserved values */
  269. return ret & mask;
  270. }
  271. /*
  272. * The TM context is mapped twice within each page. Stores and loads
  273. * to the first mapping below 2K write and read the specified values
  274. * without modification. The second mapping above 2K performs specific
  275. * state changes (side effects) in addition to setting/returning the
  276. * interrupt management area context of the processor thread.
  277. */
  278. static uint64_t xive_tm_ack_os_reg(XivePresenter *xptr, XiveTCTX *tctx,
  279. hwaddr offset, unsigned size)
  280. {
  281. return xive_tctx_accept(tctx, TM_QW1_OS);
  282. }
  283. static void xive_tm_set_os_cppr(XivePresenter *xptr, XiveTCTX *tctx,
  284. hwaddr offset, uint64_t value, unsigned size)
  285. {
  286. xive_tctx_set_cppr(tctx, TM_QW1_OS, value & 0xff);
  287. }
  288. /*
  289. * Adjust the IPB to allow a CPU to process event queues of other
  290. * priorities during one physical interrupt cycle.
  291. */
  292. static void xive_tm_set_os_pending(XivePresenter *xptr, XiveTCTX *tctx,
  293. hwaddr offset, uint64_t value, unsigned size)
  294. {
  295. xive_tctx_ipb_update(tctx, TM_QW1_OS, priority_to_ipb(value & 0xff));
  296. }
  297. static void xive_os_cam_decode(uint32_t cam, uint8_t *nvt_blk,
  298. uint32_t *nvt_idx, bool *vo)
  299. {
  300. if (nvt_blk) {
  301. *nvt_blk = xive_nvt_blk(cam);
  302. }
  303. if (nvt_idx) {
  304. *nvt_idx = xive_nvt_idx(cam);
  305. }
  306. if (vo) {
  307. *vo = !!(cam & TM_QW1W2_VO);
  308. }
  309. }
  310. static uint32_t xive_tctx_get_os_cam(XiveTCTX *tctx, uint8_t *nvt_blk,
  311. uint32_t *nvt_idx, bool *vo)
  312. {
  313. uint32_t qw1w2 = xive_tctx_word2(&tctx->regs[TM_QW1_OS]);
  314. uint32_t cam = be32_to_cpu(qw1w2);
  315. xive_os_cam_decode(cam, nvt_blk, nvt_idx, vo);
  316. return qw1w2;
  317. }
  318. static void xive_tctx_set_os_cam(XiveTCTX *tctx, uint32_t qw1w2)
  319. {
  320. memcpy(&tctx->regs[TM_QW1_OS + TM_WORD2], &qw1w2, 4);
  321. }
  322. static uint64_t xive_tm_pull_os_ctx(XivePresenter *xptr, XiveTCTX *tctx,
  323. hwaddr offset, unsigned size)
  324. {
  325. uint32_t qw1w2;
  326. uint32_t qw1w2_new;
  327. uint8_t nvt_blk;
  328. uint32_t nvt_idx;
  329. bool vo;
  330. qw1w2 = xive_tctx_get_os_cam(tctx, &nvt_blk, &nvt_idx, &vo);
  331. if (!vo) {
  332. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: pulling invalid NVT %x/%x !?\n",
  333. nvt_blk, nvt_idx);
  334. }
  335. /* Invalidate CAM line */
  336. qw1w2_new = xive_set_field32(TM_QW1W2_VO, qw1w2, 0);
  337. xive_tctx_set_os_cam(tctx, qw1w2_new);
  338. return qw1w2;
  339. }
  340. static void xive_tctx_need_resend(XiveRouter *xrtr, XiveTCTX *tctx,
  341. uint8_t nvt_blk, uint32_t nvt_idx)
  342. {
  343. XiveNVT nvt;
  344. uint8_t ipb;
  345. /*
  346. * Grab the associated NVT to pull the pending bits, and merge
  347. * them with the IPB of the thread interrupt context registers
  348. */
  349. if (xive_router_get_nvt(xrtr, nvt_blk, nvt_idx, &nvt)) {
  350. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid NVT %x/%x\n",
  351. nvt_blk, nvt_idx);
  352. return;
  353. }
  354. ipb = xive_get_field32(NVT_W4_IPB, nvt.w4);
  355. if (ipb) {
  356. /* Reset the NVT value */
  357. nvt.w4 = xive_set_field32(NVT_W4_IPB, nvt.w4, 0);
  358. xive_router_write_nvt(xrtr, nvt_blk, nvt_idx, &nvt, 4);
  359. /* Merge in current context */
  360. xive_tctx_ipb_update(tctx, TM_QW1_OS, ipb);
  361. }
  362. }
  363. /*
  364. * Updating the OS CAM line can trigger a resend of interrupt
  365. */
  366. static void xive_tm_push_os_ctx(XivePresenter *xptr, XiveTCTX *tctx,
  367. hwaddr offset, uint64_t value, unsigned size)
  368. {
  369. uint32_t cam = value;
  370. uint32_t qw1w2 = cpu_to_be32(cam);
  371. uint8_t nvt_blk;
  372. uint32_t nvt_idx;
  373. bool vo;
  374. xive_os_cam_decode(cam, &nvt_blk, &nvt_idx, &vo);
  375. /* First update the registers */
  376. xive_tctx_set_os_cam(tctx, qw1w2);
  377. /* Check the interrupt pending bits */
  378. if (vo) {
  379. xive_tctx_need_resend(XIVE_ROUTER(xptr), tctx, nvt_blk, nvt_idx);
  380. }
  381. }
  382. /*
  383. * Define a mapping of "special" operations depending on the TIMA page
  384. * offset and the size of the operation.
  385. */
  386. typedef struct XiveTmOp {
  387. uint8_t page_offset;
  388. uint32_t op_offset;
  389. unsigned size;
  390. void (*write_handler)(XivePresenter *xptr, XiveTCTX *tctx,
  391. hwaddr offset,
  392. uint64_t value, unsigned size);
  393. uint64_t (*read_handler)(XivePresenter *xptr, XiveTCTX *tctx, hwaddr offset,
  394. unsigned size);
  395. } XiveTmOp;
  396. static const XiveTmOp xive_tm_operations[] = {
  397. /*
  398. * MMIOs below 2K : raw values and special operations without side
  399. * effects
  400. */
  401. { XIVE_TM_OS_PAGE, TM_QW1_OS + TM_CPPR, 1, xive_tm_set_os_cppr, NULL },
  402. { XIVE_TM_HV_PAGE, TM_QW1_OS + TM_WORD2, 4, xive_tm_push_os_ctx, NULL },
  403. { XIVE_TM_HV_PAGE, TM_QW3_HV_PHYS + TM_CPPR, 1, xive_tm_set_hv_cppr, NULL },
  404. { XIVE_TM_HV_PAGE, TM_QW3_HV_PHYS + TM_WORD2, 1, xive_tm_vt_push, NULL },
  405. { XIVE_TM_HV_PAGE, TM_QW3_HV_PHYS + TM_WORD2, 1, NULL, xive_tm_vt_poll },
  406. /* MMIOs above 2K : special operations with side effects */
  407. { XIVE_TM_OS_PAGE, TM_SPC_ACK_OS_REG, 2, NULL, xive_tm_ack_os_reg },
  408. { XIVE_TM_OS_PAGE, TM_SPC_SET_OS_PENDING, 1, xive_tm_set_os_pending, NULL },
  409. { XIVE_TM_HV_PAGE, TM_SPC_PULL_OS_CTX, 4, NULL, xive_tm_pull_os_ctx },
  410. { XIVE_TM_HV_PAGE, TM_SPC_PULL_OS_CTX, 8, NULL, xive_tm_pull_os_ctx },
  411. { XIVE_TM_HV_PAGE, TM_SPC_ACK_HV_REG, 2, NULL, xive_tm_ack_hv_reg },
  412. { XIVE_TM_HV_PAGE, TM_SPC_PULL_POOL_CTX, 4, NULL, xive_tm_pull_pool_ctx },
  413. { XIVE_TM_HV_PAGE, TM_SPC_PULL_POOL_CTX, 8, NULL, xive_tm_pull_pool_ctx },
  414. };
  415. static const XiveTmOp *xive_tm_find_op(hwaddr offset, unsigned size, bool write)
  416. {
  417. uint8_t page_offset = (offset >> TM_SHIFT) & 0x3;
  418. uint32_t op_offset = offset & 0xFFF;
  419. int i;
  420. for (i = 0; i < ARRAY_SIZE(xive_tm_operations); i++) {
  421. const XiveTmOp *xto = &xive_tm_operations[i];
  422. /* Accesses done from a more privileged TIMA page is allowed */
  423. if (xto->page_offset >= page_offset &&
  424. xto->op_offset == op_offset &&
  425. xto->size == size &&
  426. ((write && xto->write_handler) || (!write && xto->read_handler))) {
  427. return xto;
  428. }
  429. }
  430. return NULL;
  431. }
  432. /*
  433. * TIMA MMIO handlers
  434. */
  435. void xive_tctx_tm_write(XivePresenter *xptr, XiveTCTX *tctx, hwaddr offset,
  436. uint64_t value, unsigned size)
  437. {
  438. const XiveTmOp *xto;
  439. /*
  440. * TODO: check V bit in Q[0-3]W2
  441. */
  442. /*
  443. * First, check for special operations in the 2K region
  444. */
  445. if (offset & 0x800) {
  446. xto = xive_tm_find_op(offset, size, true);
  447. if (!xto) {
  448. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid write access at TIMA "
  449. "@%"HWADDR_PRIx"\n", offset);
  450. } else {
  451. xto->write_handler(xptr, tctx, offset, value, size);
  452. }
  453. return;
  454. }
  455. /*
  456. * Then, for special operations in the region below 2K.
  457. */
  458. xto = xive_tm_find_op(offset, size, true);
  459. if (xto) {
  460. xto->write_handler(xptr, tctx, offset, value, size);
  461. return;
  462. }
  463. /*
  464. * Finish with raw access to the register values
  465. */
  466. xive_tm_raw_write(tctx, offset, value, size);
  467. }
  468. uint64_t xive_tctx_tm_read(XivePresenter *xptr, XiveTCTX *tctx, hwaddr offset,
  469. unsigned size)
  470. {
  471. const XiveTmOp *xto;
  472. /*
  473. * TODO: check V bit in Q[0-3]W2
  474. */
  475. /*
  476. * First, check for special operations in the 2K region
  477. */
  478. if (offset & 0x800) {
  479. xto = xive_tm_find_op(offset, size, false);
  480. if (!xto) {
  481. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid read access to TIMA"
  482. "@%"HWADDR_PRIx"\n", offset);
  483. return -1;
  484. }
  485. return xto->read_handler(xptr, tctx, offset, size);
  486. }
  487. /*
  488. * Then, for special operations in the region below 2K.
  489. */
  490. xto = xive_tm_find_op(offset, size, false);
  491. if (xto) {
  492. return xto->read_handler(xptr, tctx, offset, size);
  493. }
  494. /*
  495. * Finish with raw access to the register values
  496. */
  497. return xive_tm_raw_read(tctx, offset, size);
  498. }
  499. static char *xive_tctx_ring_print(uint8_t *ring)
  500. {
  501. uint32_t w2 = xive_tctx_word2(ring);
  502. return g_strdup_printf("%02x %02x %02x %02x %02x "
  503. "%02x %02x %02x %08x",
  504. ring[TM_NSR], ring[TM_CPPR], ring[TM_IPB], ring[TM_LSMFB],
  505. ring[TM_ACK_CNT], ring[TM_INC], ring[TM_AGE], ring[TM_PIPR],
  506. be32_to_cpu(w2));
  507. }
  508. static const char * const xive_tctx_ring_names[] = {
  509. "USER", "OS", "POOL", "PHYS",
  510. };
  511. void xive_tctx_pic_print_info(XiveTCTX *tctx, Monitor *mon)
  512. {
  513. int cpu_index;
  514. int i;
  515. /* Skip partially initialized vCPUs. This can happen on sPAPR when vCPUs
  516. * are hot plugged or unplugged.
  517. */
  518. if (!tctx) {
  519. return;
  520. }
  521. cpu_index = tctx->cs ? tctx->cs->cpu_index : -1;
  522. if (kvm_irqchip_in_kernel()) {
  523. Error *local_err = NULL;
  524. kvmppc_xive_cpu_synchronize_state(tctx, &local_err);
  525. if (local_err) {
  526. error_report_err(local_err);
  527. return;
  528. }
  529. }
  530. monitor_printf(mon, "CPU[%04x]: QW NSR CPPR IPB LSMFB ACK# INC AGE PIPR"
  531. " W2\n", cpu_index);
  532. for (i = 0; i < XIVE_TM_RING_COUNT; i++) {
  533. char *s = xive_tctx_ring_print(&tctx->regs[i * XIVE_TM_RING_SIZE]);
  534. monitor_printf(mon, "CPU[%04x]: %4s %s\n", cpu_index,
  535. xive_tctx_ring_names[i], s);
  536. g_free(s);
  537. }
  538. }
  539. void xive_tctx_reset(XiveTCTX *tctx)
  540. {
  541. memset(tctx->regs, 0, sizeof(tctx->regs));
  542. /* Set some defaults */
  543. tctx->regs[TM_QW1_OS + TM_LSMFB] = 0xFF;
  544. tctx->regs[TM_QW1_OS + TM_ACK_CNT] = 0xFF;
  545. tctx->regs[TM_QW1_OS + TM_AGE] = 0xFF;
  546. /*
  547. * Initialize PIPR to 0xFF to avoid phantom interrupts when the
  548. * CPPR is first set.
  549. */
  550. tctx->regs[TM_QW1_OS + TM_PIPR] =
  551. ipb_to_pipr(tctx->regs[TM_QW1_OS + TM_IPB]);
  552. tctx->regs[TM_QW3_HV_PHYS + TM_PIPR] =
  553. ipb_to_pipr(tctx->regs[TM_QW3_HV_PHYS + TM_IPB]);
  554. }
  555. static void xive_tctx_realize(DeviceState *dev, Error **errp)
  556. {
  557. XiveTCTX *tctx = XIVE_TCTX(dev);
  558. PowerPCCPU *cpu;
  559. CPUPPCState *env;
  560. Error *local_err = NULL;
  561. assert(tctx->cs);
  562. assert(tctx->xptr);
  563. cpu = POWERPC_CPU(tctx->cs);
  564. env = &cpu->env;
  565. switch (PPC_INPUT(env)) {
  566. case PPC_FLAGS_INPUT_POWER9:
  567. tctx->hv_output = env->irq_inputs[POWER9_INPUT_HINT];
  568. tctx->os_output = env->irq_inputs[POWER9_INPUT_INT];
  569. break;
  570. default:
  571. error_setg(errp, "XIVE interrupt controller does not support "
  572. "this CPU bus model");
  573. return;
  574. }
  575. /* Connect the presenter to the VCPU (required for CPU hotplug) */
  576. if (kvm_irqchip_in_kernel()) {
  577. kvmppc_xive_cpu_connect(tctx, &local_err);
  578. if (local_err) {
  579. error_propagate(errp, local_err);
  580. return;
  581. }
  582. }
  583. }
  584. static int vmstate_xive_tctx_pre_save(void *opaque)
  585. {
  586. Error *local_err = NULL;
  587. if (kvm_irqchip_in_kernel()) {
  588. kvmppc_xive_cpu_get_state(XIVE_TCTX(opaque), &local_err);
  589. if (local_err) {
  590. error_report_err(local_err);
  591. return -1;
  592. }
  593. }
  594. return 0;
  595. }
  596. static int vmstate_xive_tctx_post_load(void *opaque, int version_id)
  597. {
  598. Error *local_err = NULL;
  599. if (kvm_irqchip_in_kernel()) {
  600. /*
  601. * Required for hotplugged CPU, for which the state comes
  602. * after all states of the machine.
  603. */
  604. kvmppc_xive_cpu_set_state(XIVE_TCTX(opaque), &local_err);
  605. if (local_err) {
  606. error_report_err(local_err);
  607. return -1;
  608. }
  609. }
  610. return 0;
  611. }
  612. static const VMStateDescription vmstate_xive_tctx = {
  613. .name = TYPE_XIVE_TCTX,
  614. .version_id = 1,
  615. .minimum_version_id = 1,
  616. .pre_save = vmstate_xive_tctx_pre_save,
  617. .post_load = vmstate_xive_tctx_post_load,
  618. .fields = (VMStateField[]) {
  619. VMSTATE_BUFFER(regs, XiveTCTX),
  620. VMSTATE_END_OF_LIST()
  621. },
  622. };
  623. static Property xive_tctx_properties[] = {
  624. DEFINE_PROP_LINK("cpu", XiveTCTX, cs, TYPE_CPU, CPUState *),
  625. DEFINE_PROP_LINK("presenter", XiveTCTX, xptr, TYPE_XIVE_PRESENTER,
  626. XivePresenter *),
  627. DEFINE_PROP_END_OF_LIST(),
  628. };
  629. static void xive_tctx_class_init(ObjectClass *klass, void *data)
  630. {
  631. DeviceClass *dc = DEVICE_CLASS(klass);
  632. dc->desc = "XIVE Interrupt Thread Context";
  633. dc->realize = xive_tctx_realize;
  634. dc->vmsd = &vmstate_xive_tctx;
  635. device_class_set_props(dc, xive_tctx_properties);
  636. /*
  637. * Reason: part of XIVE interrupt controller, needs to be wired up
  638. * by xive_tctx_create().
  639. */
  640. dc->user_creatable = false;
  641. }
  642. static const TypeInfo xive_tctx_info = {
  643. .name = TYPE_XIVE_TCTX,
  644. .parent = TYPE_DEVICE,
  645. .instance_size = sizeof(XiveTCTX),
  646. .class_init = xive_tctx_class_init,
  647. };
  648. Object *xive_tctx_create(Object *cpu, XivePresenter *xptr, Error **errp)
  649. {
  650. Object *obj;
  651. obj = object_new(TYPE_XIVE_TCTX);
  652. object_property_add_child(cpu, TYPE_XIVE_TCTX, obj);
  653. object_unref(obj);
  654. object_property_set_link(obj, "cpu", cpu, &error_abort);
  655. object_property_set_link(obj, "presenter", OBJECT(xptr), &error_abort);
  656. if (!qdev_realize(DEVICE(obj), NULL, errp)) {
  657. object_unparent(obj);
  658. return NULL;
  659. }
  660. return obj;
  661. }
  662. void xive_tctx_destroy(XiveTCTX *tctx)
  663. {
  664. Object *obj = OBJECT(tctx);
  665. object_unparent(obj);
  666. }
  667. /*
  668. * XIVE ESB helpers
  669. */
  670. static uint8_t xive_esb_set(uint8_t *pq, uint8_t value)
  671. {
  672. uint8_t old_pq = *pq & 0x3;
  673. *pq &= ~0x3;
  674. *pq |= value & 0x3;
  675. return old_pq;
  676. }
  677. static bool xive_esb_trigger(uint8_t *pq)
  678. {
  679. uint8_t old_pq = *pq & 0x3;
  680. switch (old_pq) {
  681. case XIVE_ESB_RESET:
  682. xive_esb_set(pq, XIVE_ESB_PENDING);
  683. return true;
  684. case XIVE_ESB_PENDING:
  685. case XIVE_ESB_QUEUED:
  686. xive_esb_set(pq, XIVE_ESB_QUEUED);
  687. return false;
  688. case XIVE_ESB_OFF:
  689. xive_esb_set(pq, XIVE_ESB_OFF);
  690. return false;
  691. default:
  692. g_assert_not_reached();
  693. }
  694. }
  695. static bool xive_esb_eoi(uint8_t *pq)
  696. {
  697. uint8_t old_pq = *pq & 0x3;
  698. switch (old_pq) {
  699. case XIVE_ESB_RESET:
  700. case XIVE_ESB_PENDING:
  701. xive_esb_set(pq, XIVE_ESB_RESET);
  702. return false;
  703. case XIVE_ESB_QUEUED:
  704. xive_esb_set(pq, XIVE_ESB_PENDING);
  705. return true;
  706. case XIVE_ESB_OFF:
  707. xive_esb_set(pq, XIVE_ESB_OFF);
  708. return false;
  709. default:
  710. g_assert_not_reached();
  711. }
  712. }
  713. /*
  714. * XIVE Interrupt Source (or IVSE)
  715. */
  716. uint8_t xive_source_esb_get(XiveSource *xsrc, uint32_t srcno)
  717. {
  718. assert(srcno < xsrc->nr_irqs);
  719. return xsrc->status[srcno] & 0x3;
  720. }
  721. uint8_t xive_source_esb_set(XiveSource *xsrc, uint32_t srcno, uint8_t pq)
  722. {
  723. assert(srcno < xsrc->nr_irqs);
  724. return xive_esb_set(&xsrc->status[srcno], pq);
  725. }
  726. /*
  727. * Returns whether the event notification should be forwarded.
  728. */
  729. static bool xive_source_lsi_trigger(XiveSource *xsrc, uint32_t srcno)
  730. {
  731. uint8_t old_pq = xive_source_esb_get(xsrc, srcno);
  732. xsrc->status[srcno] |= XIVE_STATUS_ASSERTED;
  733. switch (old_pq) {
  734. case XIVE_ESB_RESET:
  735. xive_source_esb_set(xsrc, srcno, XIVE_ESB_PENDING);
  736. return true;
  737. default:
  738. return false;
  739. }
  740. }
  741. /*
  742. * Returns whether the event notification should be forwarded.
  743. */
  744. static bool xive_source_esb_trigger(XiveSource *xsrc, uint32_t srcno)
  745. {
  746. bool ret;
  747. assert(srcno < xsrc->nr_irqs);
  748. ret = xive_esb_trigger(&xsrc->status[srcno]);
  749. if (xive_source_irq_is_lsi(xsrc, srcno) &&
  750. xive_source_esb_get(xsrc, srcno) == XIVE_ESB_QUEUED) {
  751. qemu_log_mask(LOG_GUEST_ERROR,
  752. "XIVE: queued an event on LSI IRQ %d\n", srcno);
  753. }
  754. return ret;
  755. }
  756. /*
  757. * Returns whether the event notification should be forwarded.
  758. */
  759. static bool xive_source_esb_eoi(XiveSource *xsrc, uint32_t srcno)
  760. {
  761. bool ret;
  762. assert(srcno < xsrc->nr_irqs);
  763. ret = xive_esb_eoi(&xsrc->status[srcno]);
  764. /*
  765. * LSI sources do not set the Q bit but they can still be
  766. * asserted, in which case we should forward a new event
  767. * notification
  768. */
  769. if (xive_source_irq_is_lsi(xsrc, srcno) &&
  770. xsrc->status[srcno] & XIVE_STATUS_ASSERTED) {
  771. ret = xive_source_lsi_trigger(xsrc, srcno);
  772. }
  773. return ret;
  774. }
  775. /*
  776. * Forward the source event notification to the Router
  777. */
  778. static void xive_source_notify(XiveSource *xsrc, int srcno)
  779. {
  780. XiveNotifierClass *xnc = XIVE_NOTIFIER_GET_CLASS(xsrc->xive);
  781. if (xnc->notify) {
  782. xnc->notify(xsrc->xive, srcno);
  783. }
  784. }
  785. /*
  786. * In a two pages ESB MMIO setting, even page is the trigger page, odd
  787. * page is for management
  788. */
  789. static inline bool addr_is_even(hwaddr addr, uint32_t shift)
  790. {
  791. return !((addr >> shift) & 1);
  792. }
  793. static inline bool xive_source_is_trigger_page(XiveSource *xsrc, hwaddr addr)
  794. {
  795. return xive_source_esb_has_2page(xsrc) &&
  796. addr_is_even(addr, xsrc->esb_shift - 1);
  797. }
  798. /*
  799. * ESB MMIO loads
  800. * Trigger page Management/EOI page
  801. *
  802. * ESB MMIO setting 2 pages 1 or 2 pages
  803. *
  804. * 0x000 .. 0x3FF -1 EOI and return 0|1
  805. * 0x400 .. 0x7FF -1 EOI and return 0|1
  806. * 0x800 .. 0xBFF -1 return PQ
  807. * 0xC00 .. 0xCFF -1 return PQ and atomically PQ=00
  808. * 0xD00 .. 0xDFF -1 return PQ and atomically PQ=01
  809. * 0xE00 .. 0xDFF -1 return PQ and atomically PQ=10
  810. * 0xF00 .. 0xDFF -1 return PQ and atomically PQ=11
  811. */
  812. static uint64_t xive_source_esb_read(void *opaque, hwaddr addr, unsigned size)
  813. {
  814. XiveSource *xsrc = XIVE_SOURCE(opaque);
  815. uint32_t offset = addr & 0xFFF;
  816. uint32_t srcno = addr >> xsrc->esb_shift;
  817. uint64_t ret = -1;
  818. /* In a two pages ESB MMIO setting, trigger page should not be read */
  819. if (xive_source_is_trigger_page(xsrc, addr)) {
  820. qemu_log_mask(LOG_GUEST_ERROR,
  821. "XIVE: invalid load on IRQ %d trigger page at "
  822. "0x%"HWADDR_PRIx"\n", srcno, addr);
  823. return -1;
  824. }
  825. switch (offset) {
  826. case XIVE_ESB_LOAD_EOI ... XIVE_ESB_LOAD_EOI + 0x7FF:
  827. ret = xive_source_esb_eoi(xsrc, srcno);
  828. /* Forward the source event notification for routing */
  829. if (ret) {
  830. xive_source_notify(xsrc, srcno);
  831. }
  832. break;
  833. case XIVE_ESB_GET ... XIVE_ESB_GET + 0x3FF:
  834. ret = xive_source_esb_get(xsrc, srcno);
  835. break;
  836. case XIVE_ESB_SET_PQ_00 ... XIVE_ESB_SET_PQ_00 + 0x0FF:
  837. case XIVE_ESB_SET_PQ_01 ... XIVE_ESB_SET_PQ_01 + 0x0FF:
  838. case XIVE_ESB_SET_PQ_10 ... XIVE_ESB_SET_PQ_10 + 0x0FF:
  839. case XIVE_ESB_SET_PQ_11 ... XIVE_ESB_SET_PQ_11 + 0x0FF:
  840. ret = xive_source_esb_set(xsrc, srcno, (offset >> 8) & 0x3);
  841. break;
  842. default:
  843. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid ESB load addr %x\n",
  844. offset);
  845. }
  846. return ret;
  847. }
  848. /*
  849. * ESB MMIO stores
  850. * Trigger page Management/EOI page
  851. *
  852. * ESB MMIO setting 2 pages 1 or 2 pages
  853. *
  854. * 0x000 .. 0x3FF Trigger Trigger
  855. * 0x400 .. 0x7FF Trigger EOI
  856. * 0x800 .. 0xBFF Trigger undefined
  857. * 0xC00 .. 0xCFF Trigger PQ=00
  858. * 0xD00 .. 0xDFF Trigger PQ=01
  859. * 0xE00 .. 0xDFF Trigger PQ=10
  860. * 0xF00 .. 0xDFF Trigger PQ=11
  861. */
  862. static void xive_source_esb_write(void *opaque, hwaddr addr,
  863. uint64_t value, unsigned size)
  864. {
  865. XiveSource *xsrc = XIVE_SOURCE(opaque);
  866. uint32_t offset = addr & 0xFFF;
  867. uint32_t srcno = addr >> xsrc->esb_shift;
  868. bool notify = false;
  869. /* In a two pages ESB MMIO setting, trigger page only triggers */
  870. if (xive_source_is_trigger_page(xsrc, addr)) {
  871. notify = xive_source_esb_trigger(xsrc, srcno);
  872. goto out;
  873. }
  874. switch (offset) {
  875. case 0 ... 0x3FF:
  876. notify = xive_source_esb_trigger(xsrc, srcno);
  877. break;
  878. case XIVE_ESB_STORE_EOI ... XIVE_ESB_STORE_EOI + 0x3FF:
  879. if (!(xsrc->esb_flags & XIVE_SRC_STORE_EOI)) {
  880. qemu_log_mask(LOG_GUEST_ERROR,
  881. "XIVE: invalid Store EOI for IRQ %d\n", srcno);
  882. return;
  883. }
  884. notify = xive_source_esb_eoi(xsrc, srcno);
  885. break;
  886. case XIVE_ESB_SET_PQ_00 ... XIVE_ESB_SET_PQ_00 + 0x0FF:
  887. case XIVE_ESB_SET_PQ_01 ... XIVE_ESB_SET_PQ_01 + 0x0FF:
  888. case XIVE_ESB_SET_PQ_10 ... XIVE_ESB_SET_PQ_10 + 0x0FF:
  889. case XIVE_ESB_SET_PQ_11 ... XIVE_ESB_SET_PQ_11 + 0x0FF:
  890. xive_source_esb_set(xsrc, srcno, (offset >> 8) & 0x3);
  891. break;
  892. default:
  893. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid ESB write addr %x\n",
  894. offset);
  895. return;
  896. }
  897. out:
  898. /* Forward the source event notification for routing */
  899. if (notify) {
  900. xive_source_notify(xsrc, srcno);
  901. }
  902. }
  903. static const MemoryRegionOps xive_source_esb_ops = {
  904. .read = xive_source_esb_read,
  905. .write = xive_source_esb_write,
  906. .endianness = DEVICE_BIG_ENDIAN,
  907. .valid = {
  908. .min_access_size = 8,
  909. .max_access_size = 8,
  910. },
  911. .impl = {
  912. .min_access_size = 8,
  913. .max_access_size = 8,
  914. },
  915. };
  916. void xive_source_set_irq(void *opaque, int srcno, int val)
  917. {
  918. XiveSource *xsrc = XIVE_SOURCE(opaque);
  919. bool notify = false;
  920. if (xive_source_irq_is_lsi(xsrc, srcno)) {
  921. if (val) {
  922. notify = xive_source_lsi_trigger(xsrc, srcno);
  923. } else {
  924. xsrc->status[srcno] &= ~XIVE_STATUS_ASSERTED;
  925. }
  926. } else {
  927. if (val) {
  928. notify = xive_source_esb_trigger(xsrc, srcno);
  929. }
  930. }
  931. /* Forward the source event notification for routing */
  932. if (notify) {
  933. xive_source_notify(xsrc, srcno);
  934. }
  935. }
  936. void xive_source_pic_print_info(XiveSource *xsrc, uint32_t offset, Monitor *mon)
  937. {
  938. int i;
  939. for (i = 0; i < xsrc->nr_irqs; i++) {
  940. uint8_t pq = xive_source_esb_get(xsrc, i);
  941. if (pq == XIVE_ESB_OFF) {
  942. continue;
  943. }
  944. monitor_printf(mon, " %08x %s %c%c%c\n", i + offset,
  945. xive_source_irq_is_lsi(xsrc, i) ? "LSI" : "MSI",
  946. pq & XIVE_ESB_VAL_P ? 'P' : '-',
  947. pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
  948. xsrc->status[i] & XIVE_STATUS_ASSERTED ? 'A' : ' ');
  949. }
  950. }
  951. static void xive_source_reset(void *dev)
  952. {
  953. XiveSource *xsrc = XIVE_SOURCE(dev);
  954. /* Do not clear the LSI bitmap */
  955. /* PQs are initialized to 0b01 (Q=1) which corresponds to "ints off" */
  956. memset(xsrc->status, XIVE_ESB_OFF, xsrc->nr_irqs);
  957. }
  958. static void xive_source_realize(DeviceState *dev, Error **errp)
  959. {
  960. XiveSource *xsrc = XIVE_SOURCE(dev);
  961. assert(xsrc->xive);
  962. if (!xsrc->nr_irqs) {
  963. error_setg(errp, "Number of interrupt needs to be greater than 0");
  964. return;
  965. }
  966. if (xsrc->esb_shift != XIVE_ESB_4K &&
  967. xsrc->esb_shift != XIVE_ESB_4K_2PAGE &&
  968. xsrc->esb_shift != XIVE_ESB_64K &&
  969. xsrc->esb_shift != XIVE_ESB_64K_2PAGE) {
  970. error_setg(errp, "Invalid ESB shift setting");
  971. return;
  972. }
  973. xsrc->status = g_malloc0(xsrc->nr_irqs);
  974. xsrc->lsi_map = bitmap_new(xsrc->nr_irqs);
  975. if (!kvm_irqchip_in_kernel()) {
  976. memory_region_init_io(&xsrc->esb_mmio, OBJECT(xsrc),
  977. &xive_source_esb_ops, xsrc, "xive.esb",
  978. (1ull << xsrc->esb_shift) * xsrc->nr_irqs);
  979. }
  980. qemu_register_reset(xive_source_reset, dev);
  981. }
  982. static const VMStateDescription vmstate_xive_source = {
  983. .name = TYPE_XIVE_SOURCE,
  984. .version_id = 1,
  985. .minimum_version_id = 1,
  986. .fields = (VMStateField[]) {
  987. VMSTATE_UINT32_EQUAL(nr_irqs, XiveSource, NULL),
  988. VMSTATE_VBUFFER_UINT32(status, XiveSource, 1, NULL, nr_irqs),
  989. VMSTATE_END_OF_LIST()
  990. },
  991. };
  992. /*
  993. * The default XIVE interrupt source setting for the ESB MMIOs is two
  994. * 64k pages without Store EOI, to be in sync with KVM.
  995. */
  996. static Property xive_source_properties[] = {
  997. DEFINE_PROP_UINT64("flags", XiveSource, esb_flags, 0),
  998. DEFINE_PROP_UINT32("nr-irqs", XiveSource, nr_irqs, 0),
  999. DEFINE_PROP_UINT32("shift", XiveSource, esb_shift, XIVE_ESB_64K_2PAGE),
  1000. DEFINE_PROP_LINK("xive", XiveSource, xive, TYPE_XIVE_NOTIFIER,
  1001. XiveNotifier *),
  1002. DEFINE_PROP_END_OF_LIST(),
  1003. };
  1004. static void xive_source_class_init(ObjectClass *klass, void *data)
  1005. {
  1006. DeviceClass *dc = DEVICE_CLASS(klass);
  1007. dc->desc = "XIVE Interrupt Source";
  1008. device_class_set_props(dc, xive_source_properties);
  1009. dc->realize = xive_source_realize;
  1010. dc->vmsd = &vmstate_xive_source;
  1011. /*
  1012. * Reason: part of XIVE interrupt controller, needs to be wired up,
  1013. * e.g. by spapr_xive_instance_init().
  1014. */
  1015. dc->user_creatable = false;
  1016. }
  1017. static const TypeInfo xive_source_info = {
  1018. .name = TYPE_XIVE_SOURCE,
  1019. .parent = TYPE_DEVICE,
  1020. .instance_size = sizeof(XiveSource),
  1021. .class_init = xive_source_class_init,
  1022. };
  1023. /*
  1024. * XiveEND helpers
  1025. */
  1026. void xive_end_queue_pic_print_info(XiveEND *end, uint32_t width, Monitor *mon)
  1027. {
  1028. uint64_t qaddr_base = xive_end_qaddr(end);
  1029. uint32_t qsize = xive_get_field32(END_W0_QSIZE, end->w0);
  1030. uint32_t qindex = xive_get_field32(END_W1_PAGE_OFF, end->w1);
  1031. uint32_t qentries = 1 << (qsize + 10);
  1032. int i;
  1033. /*
  1034. * print out the [ (qindex - (width - 1)) .. (qindex + 1)] window
  1035. */
  1036. monitor_printf(mon, " [ ");
  1037. qindex = (qindex - (width - 1)) & (qentries - 1);
  1038. for (i = 0; i < width; i++) {
  1039. uint64_t qaddr = qaddr_base + (qindex << 2);
  1040. uint32_t qdata = -1;
  1041. if (dma_memory_read(&address_space_memory, qaddr, &qdata,
  1042. sizeof(qdata))) {
  1043. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to read EQ @0x%"
  1044. HWADDR_PRIx "\n", qaddr);
  1045. return;
  1046. }
  1047. monitor_printf(mon, "%s%08x ", i == width - 1 ? "^" : "",
  1048. be32_to_cpu(qdata));
  1049. qindex = (qindex + 1) & (qentries - 1);
  1050. }
  1051. monitor_printf(mon, "]");
  1052. }
  1053. void xive_end_pic_print_info(XiveEND *end, uint32_t end_idx, Monitor *mon)
  1054. {
  1055. uint64_t qaddr_base = xive_end_qaddr(end);
  1056. uint32_t qindex = xive_get_field32(END_W1_PAGE_OFF, end->w1);
  1057. uint32_t qgen = xive_get_field32(END_W1_GENERATION, end->w1);
  1058. uint32_t qsize = xive_get_field32(END_W0_QSIZE, end->w0);
  1059. uint32_t qentries = 1 << (qsize + 10);
  1060. uint32_t nvt_blk = xive_get_field32(END_W6_NVT_BLOCK, end->w6);
  1061. uint32_t nvt_idx = xive_get_field32(END_W6_NVT_INDEX, end->w6);
  1062. uint8_t priority = xive_get_field32(END_W7_F0_PRIORITY, end->w7);
  1063. uint8_t pq;
  1064. if (!xive_end_is_valid(end)) {
  1065. return;
  1066. }
  1067. pq = xive_get_field32(END_W1_ESn, end->w1);
  1068. monitor_printf(mon, " %08x %c%c %c%c%c%c%c%c%c prio:%d nvt:%02x/%04x",
  1069. end_idx,
  1070. pq & XIVE_ESB_VAL_P ? 'P' : '-',
  1071. pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
  1072. xive_end_is_valid(end) ? 'v' : '-',
  1073. xive_end_is_enqueue(end) ? 'q' : '-',
  1074. xive_end_is_notify(end) ? 'n' : '-',
  1075. xive_end_is_backlog(end) ? 'b' : '-',
  1076. xive_end_is_escalate(end) ? 'e' : '-',
  1077. xive_end_is_uncond_escalation(end) ? 'u' : '-',
  1078. xive_end_is_silent_escalation(end) ? 's' : '-',
  1079. priority, nvt_blk, nvt_idx);
  1080. if (qaddr_base) {
  1081. monitor_printf(mon, " eq:@%08"PRIx64"% 6d/%5d ^%d",
  1082. qaddr_base, qindex, qentries, qgen);
  1083. xive_end_queue_pic_print_info(end, 6, mon);
  1084. }
  1085. monitor_printf(mon, "\n");
  1086. }
  1087. static void xive_end_enqueue(XiveEND *end, uint32_t data)
  1088. {
  1089. uint64_t qaddr_base = xive_end_qaddr(end);
  1090. uint32_t qsize = xive_get_field32(END_W0_QSIZE, end->w0);
  1091. uint32_t qindex = xive_get_field32(END_W1_PAGE_OFF, end->w1);
  1092. uint32_t qgen = xive_get_field32(END_W1_GENERATION, end->w1);
  1093. uint64_t qaddr = qaddr_base + (qindex << 2);
  1094. uint32_t qdata = cpu_to_be32((qgen << 31) | (data & 0x7fffffff));
  1095. uint32_t qentries = 1 << (qsize + 10);
  1096. if (dma_memory_write(&address_space_memory, qaddr, &qdata, sizeof(qdata))) {
  1097. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to write END data @0x%"
  1098. HWADDR_PRIx "\n", qaddr);
  1099. return;
  1100. }
  1101. qindex = (qindex + 1) & (qentries - 1);
  1102. if (qindex == 0) {
  1103. qgen ^= 1;
  1104. end->w1 = xive_set_field32(END_W1_GENERATION, end->w1, qgen);
  1105. }
  1106. end->w1 = xive_set_field32(END_W1_PAGE_OFF, end->w1, qindex);
  1107. }
  1108. void xive_end_eas_pic_print_info(XiveEND *end, uint32_t end_idx,
  1109. Monitor *mon)
  1110. {
  1111. XiveEAS *eas = (XiveEAS *) &end->w4;
  1112. uint8_t pq;
  1113. if (!xive_end_is_escalate(end)) {
  1114. return;
  1115. }
  1116. pq = xive_get_field32(END_W1_ESe, end->w1);
  1117. monitor_printf(mon, " %08x %c%c %c%c end:%02x/%04x data:%08x\n",
  1118. end_idx,
  1119. pq & XIVE_ESB_VAL_P ? 'P' : '-',
  1120. pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
  1121. xive_eas_is_valid(eas) ? 'V' : ' ',
  1122. xive_eas_is_masked(eas) ? 'M' : ' ',
  1123. (uint8_t) xive_get_field64(EAS_END_BLOCK, eas->w),
  1124. (uint32_t) xive_get_field64(EAS_END_INDEX, eas->w),
  1125. (uint32_t) xive_get_field64(EAS_END_DATA, eas->w));
  1126. }
  1127. /*
  1128. * XIVE Router (aka. Virtualization Controller or IVRE)
  1129. */
  1130. int xive_router_get_eas(XiveRouter *xrtr, uint8_t eas_blk, uint32_t eas_idx,
  1131. XiveEAS *eas)
  1132. {
  1133. XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
  1134. return xrc->get_eas(xrtr, eas_blk, eas_idx, eas);
  1135. }
  1136. int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
  1137. XiveEND *end)
  1138. {
  1139. XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
  1140. return xrc->get_end(xrtr, end_blk, end_idx, end);
  1141. }
  1142. int xive_router_write_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
  1143. XiveEND *end, uint8_t word_number)
  1144. {
  1145. XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
  1146. return xrc->write_end(xrtr, end_blk, end_idx, end, word_number);
  1147. }
  1148. int xive_router_get_nvt(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
  1149. XiveNVT *nvt)
  1150. {
  1151. XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
  1152. return xrc->get_nvt(xrtr, nvt_blk, nvt_idx, nvt);
  1153. }
  1154. int xive_router_write_nvt(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
  1155. XiveNVT *nvt, uint8_t word_number)
  1156. {
  1157. XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
  1158. return xrc->write_nvt(xrtr, nvt_blk, nvt_idx, nvt, word_number);
  1159. }
  1160. static int xive_router_get_block_id(XiveRouter *xrtr)
  1161. {
  1162. XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
  1163. return xrc->get_block_id(xrtr);
  1164. }
  1165. static void xive_router_realize(DeviceState *dev, Error **errp)
  1166. {
  1167. XiveRouter *xrtr = XIVE_ROUTER(dev);
  1168. assert(xrtr->xfb);
  1169. }
  1170. /*
  1171. * Encode the HW CAM line in the block group mode format :
  1172. *
  1173. * chip << 19 | 0000000 0 0001 thread (7Bit)
  1174. */
  1175. static uint32_t xive_tctx_hw_cam_line(XivePresenter *xptr, XiveTCTX *tctx)
  1176. {
  1177. CPUPPCState *env = &POWERPC_CPU(tctx->cs)->env;
  1178. uint32_t pir = env->spr_cb[SPR_PIR].default_value;
  1179. uint8_t blk = xive_router_get_block_id(XIVE_ROUTER(xptr));
  1180. return xive_nvt_cam_line(blk, 1 << 7 | (pir & 0x7f));
  1181. }
  1182. /*
  1183. * The thread context register words are in big-endian format.
  1184. */
  1185. int xive_presenter_tctx_match(XivePresenter *xptr, XiveTCTX *tctx,
  1186. uint8_t format,
  1187. uint8_t nvt_blk, uint32_t nvt_idx,
  1188. bool cam_ignore, uint32_t logic_serv)
  1189. {
  1190. uint32_t cam = xive_nvt_cam_line(nvt_blk, nvt_idx);
  1191. uint32_t qw3w2 = xive_tctx_word2(&tctx->regs[TM_QW3_HV_PHYS]);
  1192. uint32_t qw2w2 = xive_tctx_word2(&tctx->regs[TM_QW2_HV_POOL]);
  1193. uint32_t qw1w2 = xive_tctx_word2(&tctx->regs[TM_QW1_OS]);
  1194. uint32_t qw0w2 = xive_tctx_word2(&tctx->regs[TM_QW0_USER]);
  1195. /*
  1196. * TODO (PowerNV): ignore mode. The low order bits of the NVT
  1197. * identifier are ignored in the "CAM" match.
  1198. */
  1199. if (format == 0) {
  1200. if (cam_ignore == true) {
  1201. /*
  1202. * F=0 & i=1: Logical server notification (bits ignored at
  1203. * the end of the NVT identifier)
  1204. */
  1205. qemu_log_mask(LOG_UNIMP, "XIVE: no support for LS NVT %x/%x\n",
  1206. nvt_blk, nvt_idx);
  1207. return -1;
  1208. }
  1209. /* F=0 & i=0: Specific NVT notification */
  1210. /* PHYS ring */
  1211. if ((be32_to_cpu(qw3w2) & TM_QW3W2_VT) &&
  1212. cam == xive_tctx_hw_cam_line(xptr, tctx)) {
  1213. return TM_QW3_HV_PHYS;
  1214. }
  1215. /* HV POOL ring */
  1216. if ((be32_to_cpu(qw2w2) & TM_QW2W2_VP) &&
  1217. cam == xive_get_field32(TM_QW2W2_POOL_CAM, qw2w2)) {
  1218. return TM_QW2_HV_POOL;
  1219. }
  1220. /* OS ring */
  1221. if ((be32_to_cpu(qw1w2) & TM_QW1W2_VO) &&
  1222. cam == xive_get_field32(TM_QW1W2_OS_CAM, qw1w2)) {
  1223. return TM_QW1_OS;
  1224. }
  1225. } else {
  1226. /* F=1 : User level Event-Based Branch (EBB) notification */
  1227. /* USER ring */
  1228. if ((be32_to_cpu(qw1w2) & TM_QW1W2_VO) &&
  1229. (cam == xive_get_field32(TM_QW1W2_OS_CAM, qw1w2)) &&
  1230. (be32_to_cpu(qw0w2) & TM_QW0W2_VU) &&
  1231. (logic_serv == xive_get_field32(TM_QW0W2_LOGIC_SERV, qw0w2))) {
  1232. return TM_QW0_USER;
  1233. }
  1234. }
  1235. return -1;
  1236. }
  1237. /*
  1238. * This is our simple Xive Presenter Engine model. It is merged in the
  1239. * Router as it does not require an extra object.
  1240. *
  1241. * It receives notification requests sent by the IVRE to find one
  1242. * matching NVT (or more) dispatched on the processor threads. In case
  1243. * of a single NVT notification, the process is abreviated and the
  1244. * thread is signaled if a match is found. In case of a logical server
  1245. * notification (bits ignored at the end of the NVT identifier), the
  1246. * IVPE and IVRE select a winning thread using different filters. This
  1247. * involves 2 or 3 exchanges on the PowerBus that the model does not
  1248. * support.
  1249. *
  1250. * The parameters represent what is sent on the PowerBus
  1251. */
  1252. static bool xive_presenter_notify(XiveFabric *xfb, uint8_t format,
  1253. uint8_t nvt_blk, uint32_t nvt_idx,
  1254. bool cam_ignore, uint8_t priority,
  1255. uint32_t logic_serv)
  1256. {
  1257. XiveFabricClass *xfc = XIVE_FABRIC_GET_CLASS(xfb);
  1258. XiveTCTXMatch match = { .tctx = NULL, .ring = 0 };
  1259. int count;
  1260. /*
  1261. * Ask the machine to scan the interrupt controllers for a match
  1262. */
  1263. count = xfc->match_nvt(xfb, format, nvt_blk, nvt_idx, cam_ignore,
  1264. priority, logic_serv, &match);
  1265. if (count < 0) {
  1266. return false;
  1267. }
  1268. /* handle CPU exception delivery */
  1269. if (count) {
  1270. xive_tctx_ipb_update(match.tctx, match.ring, priority_to_ipb(priority));
  1271. }
  1272. return !!count;
  1273. }
  1274. /*
  1275. * Notification using the END ESe/ESn bit (Event State Buffer for
  1276. * escalation and notification). Profide futher coalescing in the
  1277. * Router.
  1278. */
  1279. static bool xive_router_end_es_notify(XiveRouter *xrtr, uint8_t end_blk,
  1280. uint32_t end_idx, XiveEND *end,
  1281. uint32_t end_esmask)
  1282. {
  1283. uint8_t pq = xive_get_field32(end_esmask, end->w1);
  1284. bool notify = xive_esb_trigger(&pq);
  1285. if (pq != xive_get_field32(end_esmask, end->w1)) {
  1286. end->w1 = xive_set_field32(end_esmask, end->w1, pq);
  1287. xive_router_write_end(xrtr, end_blk, end_idx, end, 1);
  1288. }
  1289. /* ESe/n[Q]=1 : end of notification */
  1290. return notify;
  1291. }
  1292. /*
  1293. * An END trigger can come from an event trigger (IPI or HW) or from
  1294. * another chip. We don't model the PowerBus but the END trigger
  1295. * message has the same parameters than in the function below.
  1296. */
  1297. static void xive_router_end_notify(XiveRouter *xrtr, uint8_t end_blk,
  1298. uint32_t end_idx, uint32_t end_data)
  1299. {
  1300. XiveEND end;
  1301. uint8_t priority;
  1302. uint8_t format;
  1303. uint8_t nvt_blk;
  1304. uint32_t nvt_idx;
  1305. XiveNVT nvt;
  1306. bool found;
  1307. /* END cache lookup */
  1308. if (xive_router_get_end(xrtr, end_blk, end_idx, &end)) {
  1309. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No END %x/%x\n", end_blk,
  1310. end_idx);
  1311. return;
  1312. }
  1313. if (!xive_end_is_valid(&end)) {
  1314. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: END %x/%x is invalid\n",
  1315. end_blk, end_idx);
  1316. return;
  1317. }
  1318. if (xive_end_is_enqueue(&end)) {
  1319. xive_end_enqueue(&end, end_data);
  1320. /* Enqueuing event data modifies the EQ toggle and index */
  1321. xive_router_write_end(xrtr, end_blk, end_idx, &end, 1);
  1322. }
  1323. /*
  1324. * When the END is silent, we skip the notification part.
  1325. */
  1326. if (xive_end_is_silent_escalation(&end)) {
  1327. goto do_escalation;
  1328. }
  1329. /*
  1330. * The W7 format depends on the F bit in W6. It defines the type
  1331. * of the notification :
  1332. *
  1333. * F=0 : single or multiple NVT notification
  1334. * F=1 : User level Event-Based Branch (EBB) notification, no
  1335. * priority
  1336. */
  1337. format = xive_get_field32(END_W6_FORMAT_BIT, end.w6);
  1338. priority = xive_get_field32(END_W7_F0_PRIORITY, end.w7);
  1339. /* The END is masked */
  1340. if (format == 0 && priority == 0xff) {
  1341. return;
  1342. }
  1343. /*
  1344. * Check the END ESn (Event State Buffer for notification) for
  1345. * even futher coalescing in the Router
  1346. */
  1347. if (!xive_end_is_notify(&end)) {
  1348. /* ESn[Q]=1 : end of notification */
  1349. if (!xive_router_end_es_notify(xrtr, end_blk, end_idx,
  1350. &end, END_W1_ESn)) {
  1351. return;
  1352. }
  1353. }
  1354. /*
  1355. * Follows IVPE notification
  1356. */
  1357. nvt_blk = xive_get_field32(END_W6_NVT_BLOCK, end.w6);
  1358. nvt_idx = xive_get_field32(END_W6_NVT_INDEX, end.w6);
  1359. /* NVT cache lookup */
  1360. if (xive_router_get_nvt(xrtr, nvt_blk, nvt_idx, &nvt)) {
  1361. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: no NVT %x/%x\n",
  1362. nvt_blk, nvt_idx);
  1363. return;
  1364. }
  1365. if (!xive_nvt_is_valid(&nvt)) {
  1366. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: NVT %x/%x is invalid\n",
  1367. nvt_blk, nvt_idx);
  1368. return;
  1369. }
  1370. found = xive_presenter_notify(xrtr->xfb, format, nvt_blk, nvt_idx,
  1371. xive_get_field32(END_W7_F0_IGNORE, end.w7),
  1372. priority,
  1373. xive_get_field32(END_W7_F1_LOG_SERVER_ID, end.w7));
  1374. /* TODO: Auto EOI. */
  1375. if (found) {
  1376. return;
  1377. }
  1378. /*
  1379. * If no matching NVT is dispatched on a HW thread :
  1380. * - specific VP: update the NVT structure if backlog is activated
  1381. * - logical server : forward request to IVPE (not supported)
  1382. */
  1383. if (xive_end_is_backlog(&end)) {
  1384. uint8_t ipb;
  1385. if (format == 1) {
  1386. qemu_log_mask(LOG_GUEST_ERROR,
  1387. "XIVE: END %x/%x invalid config: F1 & backlog\n",
  1388. end_blk, end_idx);
  1389. return;
  1390. }
  1391. /*
  1392. * Record the IPB in the associated NVT structure for later
  1393. * use. The presenter will resend the interrupt when the vCPU
  1394. * is dispatched again on a HW thread.
  1395. */
  1396. ipb = xive_get_field32(NVT_W4_IPB, nvt.w4) | priority_to_ipb(priority);
  1397. nvt.w4 = xive_set_field32(NVT_W4_IPB, nvt.w4, ipb);
  1398. xive_router_write_nvt(xrtr, nvt_blk, nvt_idx, &nvt, 4);
  1399. /*
  1400. * On HW, follows a "Broadcast Backlog" to IVPEs
  1401. */
  1402. }
  1403. do_escalation:
  1404. /*
  1405. * If activated, escalate notification using the ESe PQ bits and
  1406. * the EAS in w4-5
  1407. */
  1408. if (!xive_end_is_escalate(&end)) {
  1409. return;
  1410. }
  1411. /*
  1412. * Check the END ESe (Event State Buffer for escalation) for even
  1413. * futher coalescing in the Router
  1414. */
  1415. if (!xive_end_is_uncond_escalation(&end)) {
  1416. /* ESe[Q]=1 : end of notification */
  1417. if (!xive_router_end_es_notify(xrtr, end_blk, end_idx,
  1418. &end, END_W1_ESe)) {
  1419. return;
  1420. }
  1421. }
  1422. /*
  1423. * The END trigger becomes an Escalation trigger
  1424. */
  1425. xive_router_end_notify(xrtr,
  1426. xive_get_field32(END_W4_ESC_END_BLOCK, end.w4),
  1427. xive_get_field32(END_W4_ESC_END_INDEX, end.w4),
  1428. xive_get_field32(END_W5_ESC_END_DATA, end.w5));
  1429. }
  1430. void xive_router_notify(XiveNotifier *xn, uint32_t lisn)
  1431. {
  1432. XiveRouter *xrtr = XIVE_ROUTER(xn);
  1433. uint8_t eas_blk = XIVE_EAS_BLOCK(lisn);
  1434. uint32_t eas_idx = XIVE_EAS_INDEX(lisn);
  1435. XiveEAS eas;
  1436. /* EAS cache lookup */
  1437. if (xive_router_get_eas(xrtr, eas_blk, eas_idx, &eas)) {
  1438. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN %x\n", lisn);
  1439. return;
  1440. }
  1441. /*
  1442. * The IVRE checks the State Bit Cache at this point. We skip the
  1443. * SBC lookup because the state bits of the sources are modeled
  1444. * internally in QEMU.
  1445. */
  1446. if (!xive_eas_is_valid(&eas)) {
  1447. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid LISN %x\n", lisn);
  1448. return;
  1449. }
  1450. if (xive_eas_is_masked(&eas)) {
  1451. /* Notification completed */
  1452. return;
  1453. }
  1454. /*
  1455. * The event trigger becomes an END trigger
  1456. */
  1457. xive_router_end_notify(xrtr,
  1458. xive_get_field64(EAS_END_BLOCK, eas.w),
  1459. xive_get_field64(EAS_END_INDEX, eas.w),
  1460. xive_get_field64(EAS_END_DATA, eas.w));
  1461. }
  1462. static Property xive_router_properties[] = {
  1463. DEFINE_PROP_LINK("xive-fabric", XiveRouter, xfb,
  1464. TYPE_XIVE_FABRIC, XiveFabric *),
  1465. DEFINE_PROP_END_OF_LIST(),
  1466. };
  1467. static void xive_router_class_init(ObjectClass *klass, void *data)
  1468. {
  1469. DeviceClass *dc = DEVICE_CLASS(klass);
  1470. XiveNotifierClass *xnc = XIVE_NOTIFIER_CLASS(klass);
  1471. dc->desc = "XIVE Router Engine";
  1472. device_class_set_props(dc, xive_router_properties);
  1473. /* Parent is SysBusDeviceClass. No need to call its realize hook */
  1474. dc->realize = xive_router_realize;
  1475. xnc->notify = xive_router_notify;
  1476. }
  1477. static const TypeInfo xive_router_info = {
  1478. .name = TYPE_XIVE_ROUTER,
  1479. .parent = TYPE_SYS_BUS_DEVICE,
  1480. .abstract = true,
  1481. .instance_size = sizeof(XiveRouter),
  1482. .class_size = sizeof(XiveRouterClass),
  1483. .class_init = xive_router_class_init,
  1484. .interfaces = (InterfaceInfo[]) {
  1485. { TYPE_XIVE_NOTIFIER },
  1486. { TYPE_XIVE_PRESENTER },
  1487. { }
  1488. }
  1489. };
  1490. void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon)
  1491. {
  1492. if (!xive_eas_is_valid(eas)) {
  1493. return;
  1494. }
  1495. monitor_printf(mon, " %08x %s end:%02x/%04x data:%08x\n",
  1496. lisn, xive_eas_is_masked(eas) ? "M" : " ",
  1497. (uint8_t) xive_get_field64(EAS_END_BLOCK, eas->w),
  1498. (uint32_t) xive_get_field64(EAS_END_INDEX, eas->w),
  1499. (uint32_t) xive_get_field64(EAS_END_DATA, eas->w));
  1500. }
  1501. /*
  1502. * END ESB MMIO loads
  1503. */
  1504. static uint64_t xive_end_source_read(void *opaque, hwaddr addr, unsigned size)
  1505. {
  1506. XiveENDSource *xsrc = XIVE_END_SOURCE(opaque);
  1507. uint32_t offset = addr & 0xFFF;
  1508. uint8_t end_blk;
  1509. uint32_t end_idx;
  1510. XiveEND end;
  1511. uint32_t end_esmask;
  1512. uint8_t pq;
  1513. uint64_t ret = -1;
  1514. /*
  1515. * The block id should be deduced from the load address on the END
  1516. * ESB MMIO but our model only supports a single block per XIVE chip.
  1517. */
  1518. end_blk = xive_router_get_block_id(xsrc->xrtr);
  1519. end_idx = addr >> (xsrc->esb_shift + 1);
  1520. if (xive_router_get_end(xsrc->xrtr, end_blk, end_idx, &end)) {
  1521. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No END %x/%x\n", end_blk,
  1522. end_idx);
  1523. return -1;
  1524. }
  1525. if (!xive_end_is_valid(&end)) {
  1526. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: END %x/%x is invalid\n",
  1527. end_blk, end_idx);
  1528. return -1;
  1529. }
  1530. end_esmask = addr_is_even(addr, xsrc->esb_shift) ? END_W1_ESn : END_W1_ESe;
  1531. pq = xive_get_field32(end_esmask, end.w1);
  1532. switch (offset) {
  1533. case XIVE_ESB_LOAD_EOI ... XIVE_ESB_LOAD_EOI + 0x7FF:
  1534. ret = xive_esb_eoi(&pq);
  1535. /* Forward the source event notification for routing ?? */
  1536. break;
  1537. case XIVE_ESB_GET ... XIVE_ESB_GET + 0x3FF:
  1538. ret = pq;
  1539. break;
  1540. case XIVE_ESB_SET_PQ_00 ... XIVE_ESB_SET_PQ_00 + 0x0FF:
  1541. case XIVE_ESB_SET_PQ_01 ... XIVE_ESB_SET_PQ_01 + 0x0FF:
  1542. case XIVE_ESB_SET_PQ_10 ... XIVE_ESB_SET_PQ_10 + 0x0FF:
  1543. case XIVE_ESB_SET_PQ_11 ... XIVE_ESB_SET_PQ_11 + 0x0FF:
  1544. ret = xive_esb_set(&pq, (offset >> 8) & 0x3);
  1545. break;
  1546. default:
  1547. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid END ESB load addr %d\n",
  1548. offset);
  1549. return -1;
  1550. }
  1551. if (pq != xive_get_field32(end_esmask, end.w1)) {
  1552. end.w1 = xive_set_field32(end_esmask, end.w1, pq);
  1553. xive_router_write_end(xsrc->xrtr, end_blk, end_idx, &end, 1);
  1554. }
  1555. return ret;
  1556. }
  1557. /*
  1558. * END ESB MMIO stores are invalid
  1559. */
  1560. static void xive_end_source_write(void *opaque, hwaddr addr,
  1561. uint64_t value, unsigned size)
  1562. {
  1563. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid ESB write addr 0x%"
  1564. HWADDR_PRIx"\n", addr);
  1565. }
  1566. static const MemoryRegionOps xive_end_source_ops = {
  1567. .read = xive_end_source_read,
  1568. .write = xive_end_source_write,
  1569. .endianness = DEVICE_BIG_ENDIAN,
  1570. .valid = {
  1571. .min_access_size = 8,
  1572. .max_access_size = 8,
  1573. },
  1574. .impl = {
  1575. .min_access_size = 8,
  1576. .max_access_size = 8,
  1577. },
  1578. };
  1579. static void xive_end_source_realize(DeviceState *dev, Error **errp)
  1580. {
  1581. XiveENDSource *xsrc = XIVE_END_SOURCE(dev);
  1582. assert(xsrc->xrtr);
  1583. if (!xsrc->nr_ends) {
  1584. error_setg(errp, "Number of interrupt needs to be greater than 0");
  1585. return;
  1586. }
  1587. if (xsrc->esb_shift != XIVE_ESB_4K &&
  1588. xsrc->esb_shift != XIVE_ESB_64K) {
  1589. error_setg(errp, "Invalid ESB shift setting");
  1590. return;
  1591. }
  1592. /*
  1593. * Each END is assigned an even/odd pair of MMIO pages, the even page
  1594. * manages the ESn field while the odd page manages the ESe field.
  1595. */
  1596. memory_region_init_io(&xsrc->esb_mmio, OBJECT(xsrc),
  1597. &xive_end_source_ops, xsrc, "xive.end",
  1598. (1ull << (xsrc->esb_shift + 1)) * xsrc->nr_ends);
  1599. }
  1600. static Property xive_end_source_properties[] = {
  1601. DEFINE_PROP_UINT32("nr-ends", XiveENDSource, nr_ends, 0),
  1602. DEFINE_PROP_UINT32("shift", XiveENDSource, esb_shift, XIVE_ESB_64K),
  1603. DEFINE_PROP_LINK("xive", XiveENDSource, xrtr, TYPE_XIVE_ROUTER,
  1604. XiveRouter *),
  1605. DEFINE_PROP_END_OF_LIST(),
  1606. };
  1607. static void xive_end_source_class_init(ObjectClass *klass, void *data)
  1608. {
  1609. DeviceClass *dc = DEVICE_CLASS(klass);
  1610. dc->desc = "XIVE END Source";
  1611. device_class_set_props(dc, xive_end_source_properties);
  1612. dc->realize = xive_end_source_realize;
  1613. /*
  1614. * Reason: part of XIVE interrupt controller, needs to be wired up,
  1615. * e.g. by spapr_xive_instance_init().
  1616. */
  1617. dc->user_creatable = false;
  1618. }
  1619. static const TypeInfo xive_end_source_info = {
  1620. .name = TYPE_XIVE_END_SOURCE,
  1621. .parent = TYPE_DEVICE,
  1622. .instance_size = sizeof(XiveENDSource),
  1623. .class_init = xive_end_source_class_init,
  1624. };
  1625. /*
  1626. * XIVE Notifier
  1627. */
  1628. static const TypeInfo xive_notifier_info = {
  1629. .name = TYPE_XIVE_NOTIFIER,
  1630. .parent = TYPE_INTERFACE,
  1631. .class_size = sizeof(XiveNotifierClass),
  1632. };
  1633. /*
  1634. * XIVE Presenter
  1635. */
  1636. static const TypeInfo xive_presenter_info = {
  1637. .name = TYPE_XIVE_PRESENTER,
  1638. .parent = TYPE_INTERFACE,
  1639. .class_size = sizeof(XivePresenterClass),
  1640. };
  1641. /*
  1642. * XIVE Fabric
  1643. */
  1644. static const TypeInfo xive_fabric_info = {
  1645. .name = TYPE_XIVE_FABRIC,
  1646. .parent = TYPE_INTERFACE,
  1647. .class_size = sizeof(XiveFabricClass),
  1648. };
  1649. static void xive_register_types(void)
  1650. {
  1651. type_register_static(&xive_fabric_info);
  1652. type_register_static(&xive_source_info);
  1653. type_register_static(&xive_notifier_info);
  1654. type_register_static(&xive_presenter_info);
  1655. type_register_static(&xive_router_info);
  1656. type_register_static(&xive_end_source_info);
  1657. type_register_static(&xive_tctx_info);
  1658. }
  1659. type_init(xive_register_types)