2
0

xive.c 71 KB

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