xive.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962
  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. /*
  512. * kvm_irqchip_in_kernel() will cause the compiler to turn this
  513. * info a nop if CONFIG_KVM isn't defined.
  514. */
  515. #define xive_in_kernel(xptr) \
  516. (kvm_irqchip_in_kernel() && \
  517. ({ \
  518. XivePresenterClass *xpc = XIVE_PRESENTER_GET_CLASS(xptr); \
  519. xpc->in_kernel ? xpc->in_kernel(xptr) : false; \
  520. }))
  521. void xive_tctx_pic_print_info(XiveTCTX *tctx, Monitor *mon)
  522. {
  523. int cpu_index;
  524. int i;
  525. /* Skip partially initialized vCPUs. This can happen on sPAPR when vCPUs
  526. * are hot plugged or unplugged.
  527. */
  528. if (!tctx) {
  529. return;
  530. }
  531. cpu_index = tctx->cs ? tctx->cs->cpu_index : -1;
  532. if (xive_in_kernel(tctx->xptr)) {
  533. Error *local_err = NULL;
  534. kvmppc_xive_cpu_synchronize_state(tctx, &local_err);
  535. if (local_err) {
  536. error_report_err(local_err);
  537. return;
  538. }
  539. }
  540. monitor_printf(mon, "CPU[%04x]: QW NSR CPPR IPB LSMFB ACK# INC AGE PIPR"
  541. " W2\n", cpu_index);
  542. for (i = 0; i < XIVE_TM_RING_COUNT; i++) {
  543. char *s = xive_tctx_ring_print(&tctx->regs[i * XIVE_TM_RING_SIZE]);
  544. monitor_printf(mon, "CPU[%04x]: %4s %s\n", cpu_index,
  545. xive_tctx_ring_names[i], s);
  546. g_free(s);
  547. }
  548. }
  549. void xive_tctx_reset(XiveTCTX *tctx)
  550. {
  551. memset(tctx->regs, 0, sizeof(tctx->regs));
  552. /* Set some defaults */
  553. tctx->regs[TM_QW1_OS + TM_LSMFB] = 0xFF;
  554. tctx->regs[TM_QW1_OS + TM_ACK_CNT] = 0xFF;
  555. tctx->regs[TM_QW1_OS + TM_AGE] = 0xFF;
  556. /*
  557. * Initialize PIPR to 0xFF to avoid phantom interrupts when the
  558. * CPPR is first set.
  559. */
  560. tctx->regs[TM_QW1_OS + TM_PIPR] =
  561. ipb_to_pipr(tctx->regs[TM_QW1_OS + TM_IPB]);
  562. tctx->regs[TM_QW3_HV_PHYS + TM_PIPR] =
  563. ipb_to_pipr(tctx->regs[TM_QW3_HV_PHYS + TM_IPB]);
  564. }
  565. static void xive_tctx_realize(DeviceState *dev, Error **errp)
  566. {
  567. XiveTCTX *tctx = XIVE_TCTX(dev);
  568. PowerPCCPU *cpu;
  569. CPUPPCState *env;
  570. assert(tctx->cs);
  571. assert(tctx->xptr);
  572. cpu = POWERPC_CPU(tctx->cs);
  573. env = &cpu->env;
  574. switch (PPC_INPUT(env)) {
  575. case PPC_FLAGS_INPUT_POWER9:
  576. tctx->hv_output = env->irq_inputs[POWER9_INPUT_HINT];
  577. tctx->os_output = env->irq_inputs[POWER9_INPUT_INT];
  578. break;
  579. default:
  580. error_setg(errp, "XIVE interrupt controller does not support "
  581. "this CPU bus model");
  582. return;
  583. }
  584. /* Connect the presenter to the VCPU (required for CPU hotplug) */
  585. if (xive_in_kernel(tctx->xptr)) {
  586. if (kvmppc_xive_cpu_connect(tctx, errp) < 0) {
  587. return;
  588. }
  589. }
  590. }
  591. static int vmstate_xive_tctx_pre_save(void *opaque)
  592. {
  593. XiveTCTX *tctx = XIVE_TCTX(opaque);
  594. Error *local_err = NULL;
  595. int ret;
  596. if (xive_in_kernel(tctx->xptr)) {
  597. ret = kvmppc_xive_cpu_get_state(tctx, &local_err);
  598. if (ret < 0) {
  599. error_report_err(local_err);
  600. return ret;
  601. }
  602. }
  603. return 0;
  604. }
  605. static int vmstate_xive_tctx_post_load(void *opaque, int version_id)
  606. {
  607. XiveTCTX *tctx = XIVE_TCTX(opaque);
  608. Error *local_err = NULL;
  609. int ret;
  610. if (xive_in_kernel(tctx->xptr)) {
  611. /*
  612. * Required for hotplugged CPU, for which the state comes
  613. * after all states of the machine.
  614. */
  615. ret = kvmppc_xive_cpu_set_state(tctx, &local_err);
  616. if (ret < 0) {
  617. error_report_err(local_err);
  618. return ret;
  619. }
  620. }
  621. return 0;
  622. }
  623. static const VMStateDescription vmstate_xive_tctx = {
  624. .name = TYPE_XIVE_TCTX,
  625. .version_id = 1,
  626. .minimum_version_id = 1,
  627. .pre_save = vmstate_xive_tctx_pre_save,
  628. .post_load = vmstate_xive_tctx_post_load,
  629. .fields = (VMStateField[]) {
  630. VMSTATE_BUFFER(regs, XiveTCTX),
  631. VMSTATE_END_OF_LIST()
  632. },
  633. };
  634. static Property xive_tctx_properties[] = {
  635. DEFINE_PROP_LINK("cpu", XiveTCTX, cs, TYPE_CPU, CPUState *),
  636. DEFINE_PROP_LINK("presenter", XiveTCTX, xptr, TYPE_XIVE_PRESENTER,
  637. XivePresenter *),
  638. DEFINE_PROP_END_OF_LIST(),
  639. };
  640. static void xive_tctx_class_init(ObjectClass *klass, void *data)
  641. {
  642. DeviceClass *dc = DEVICE_CLASS(klass);
  643. dc->desc = "XIVE Interrupt Thread Context";
  644. dc->realize = xive_tctx_realize;
  645. dc->vmsd = &vmstate_xive_tctx;
  646. device_class_set_props(dc, xive_tctx_properties);
  647. /*
  648. * Reason: part of XIVE interrupt controller, needs to be wired up
  649. * by xive_tctx_create().
  650. */
  651. dc->user_creatable = false;
  652. }
  653. static const TypeInfo xive_tctx_info = {
  654. .name = TYPE_XIVE_TCTX,
  655. .parent = TYPE_DEVICE,
  656. .instance_size = sizeof(XiveTCTX),
  657. .class_init = xive_tctx_class_init,
  658. };
  659. Object *xive_tctx_create(Object *cpu, XivePresenter *xptr, Error **errp)
  660. {
  661. Object *obj;
  662. obj = object_new(TYPE_XIVE_TCTX);
  663. object_property_add_child(cpu, TYPE_XIVE_TCTX, obj);
  664. object_unref(obj);
  665. object_property_set_link(obj, "cpu", cpu, &error_abort);
  666. object_property_set_link(obj, "presenter", OBJECT(xptr), &error_abort);
  667. if (!qdev_realize(DEVICE(obj), NULL, errp)) {
  668. object_unparent(obj);
  669. return NULL;
  670. }
  671. return obj;
  672. }
  673. void xive_tctx_destroy(XiveTCTX *tctx)
  674. {
  675. Object *obj = OBJECT(tctx);
  676. object_unparent(obj);
  677. }
  678. /*
  679. * XIVE ESB helpers
  680. */
  681. static uint8_t xive_esb_set(uint8_t *pq, uint8_t value)
  682. {
  683. uint8_t old_pq = *pq & 0x3;
  684. *pq &= ~0x3;
  685. *pq |= value & 0x3;
  686. return old_pq;
  687. }
  688. static bool xive_esb_trigger(uint8_t *pq)
  689. {
  690. uint8_t old_pq = *pq & 0x3;
  691. switch (old_pq) {
  692. case XIVE_ESB_RESET:
  693. xive_esb_set(pq, XIVE_ESB_PENDING);
  694. return true;
  695. case XIVE_ESB_PENDING:
  696. case XIVE_ESB_QUEUED:
  697. xive_esb_set(pq, XIVE_ESB_QUEUED);
  698. return false;
  699. case XIVE_ESB_OFF:
  700. xive_esb_set(pq, XIVE_ESB_OFF);
  701. return false;
  702. default:
  703. g_assert_not_reached();
  704. }
  705. }
  706. static bool xive_esb_eoi(uint8_t *pq)
  707. {
  708. uint8_t old_pq = *pq & 0x3;
  709. switch (old_pq) {
  710. case XIVE_ESB_RESET:
  711. case XIVE_ESB_PENDING:
  712. xive_esb_set(pq, XIVE_ESB_RESET);
  713. return false;
  714. case XIVE_ESB_QUEUED:
  715. xive_esb_set(pq, XIVE_ESB_PENDING);
  716. return true;
  717. case XIVE_ESB_OFF:
  718. xive_esb_set(pq, XIVE_ESB_OFF);
  719. return false;
  720. default:
  721. g_assert_not_reached();
  722. }
  723. }
  724. /*
  725. * XIVE Interrupt Source (or IVSE)
  726. */
  727. uint8_t xive_source_esb_get(XiveSource *xsrc, uint32_t srcno)
  728. {
  729. assert(srcno < xsrc->nr_irqs);
  730. return xsrc->status[srcno] & 0x3;
  731. }
  732. uint8_t xive_source_esb_set(XiveSource *xsrc, uint32_t srcno, uint8_t pq)
  733. {
  734. assert(srcno < xsrc->nr_irqs);
  735. return xive_esb_set(&xsrc->status[srcno], pq);
  736. }
  737. /*
  738. * Returns whether the event notification should be forwarded.
  739. */
  740. static bool xive_source_lsi_trigger(XiveSource *xsrc, uint32_t srcno)
  741. {
  742. uint8_t old_pq = xive_source_esb_get(xsrc, srcno);
  743. xsrc->status[srcno] |= XIVE_STATUS_ASSERTED;
  744. switch (old_pq) {
  745. case XIVE_ESB_RESET:
  746. xive_source_esb_set(xsrc, srcno, XIVE_ESB_PENDING);
  747. return true;
  748. default:
  749. return false;
  750. }
  751. }
  752. /*
  753. * Returns whether the event notification should be forwarded.
  754. */
  755. static bool xive_source_esb_trigger(XiveSource *xsrc, uint32_t srcno)
  756. {
  757. bool ret;
  758. assert(srcno < xsrc->nr_irqs);
  759. ret = xive_esb_trigger(&xsrc->status[srcno]);
  760. if (xive_source_irq_is_lsi(xsrc, srcno) &&
  761. xive_source_esb_get(xsrc, srcno) == XIVE_ESB_QUEUED) {
  762. qemu_log_mask(LOG_GUEST_ERROR,
  763. "XIVE: queued an event on LSI IRQ %d\n", srcno);
  764. }
  765. return ret;
  766. }
  767. /*
  768. * Returns whether the event notification should be forwarded.
  769. */
  770. static bool xive_source_esb_eoi(XiveSource *xsrc, uint32_t srcno)
  771. {
  772. bool ret;
  773. assert(srcno < xsrc->nr_irqs);
  774. ret = xive_esb_eoi(&xsrc->status[srcno]);
  775. /*
  776. * LSI sources do not set the Q bit but they can still be
  777. * asserted, in which case we should forward a new event
  778. * notification
  779. */
  780. if (xive_source_irq_is_lsi(xsrc, srcno) &&
  781. xsrc->status[srcno] & XIVE_STATUS_ASSERTED) {
  782. ret = xive_source_lsi_trigger(xsrc, srcno);
  783. }
  784. return ret;
  785. }
  786. /*
  787. * Forward the source event notification to the Router
  788. */
  789. static void xive_source_notify(XiveSource *xsrc, int srcno)
  790. {
  791. XiveNotifierClass *xnc = XIVE_NOTIFIER_GET_CLASS(xsrc->xive);
  792. if (xnc->notify) {
  793. xnc->notify(xsrc->xive, srcno);
  794. }
  795. }
  796. /*
  797. * In a two pages ESB MMIO setting, even page is the trigger page, odd
  798. * page is for management
  799. */
  800. static inline bool addr_is_even(hwaddr addr, uint32_t shift)
  801. {
  802. return !((addr >> shift) & 1);
  803. }
  804. static inline bool xive_source_is_trigger_page(XiveSource *xsrc, hwaddr addr)
  805. {
  806. return xive_source_esb_has_2page(xsrc) &&
  807. addr_is_even(addr, xsrc->esb_shift - 1);
  808. }
  809. /*
  810. * ESB MMIO loads
  811. * Trigger page Management/EOI page
  812. *
  813. * ESB MMIO setting 2 pages 1 or 2 pages
  814. *
  815. * 0x000 .. 0x3FF -1 EOI and return 0|1
  816. * 0x400 .. 0x7FF -1 EOI and return 0|1
  817. * 0x800 .. 0xBFF -1 return PQ
  818. * 0xC00 .. 0xCFF -1 return PQ and atomically PQ=00
  819. * 0xD00 .. 0xDFF -1 return PQ and atomically PQ=01
  820. * 0xE00 .. 0xDFF -1 return PQ and atomically PQ=10
  821. * 0xF00 .. 0xDFF -1 return PQ and atomically PQ=11
  822. */
  823. static uint64_t xive_source_esb_read(void *opaque, hwaddr addr, unsigned size)
  824. {
  825. XiveSource *xsrc = XIVE_SOURCE(opaque);
  826. uint32_t offset = addr & 0xFFF;
  827. uint32_t srcno = addr >> xsrc->esb_shift;
  828. uint64_t ret = -1;
  829. /* In a two pages ESB MMIO setting, trigger page should not be read */
  830. if (xive_source_is_trigger_page(xsrc, addr)) {
  831. qemu_log_mask(LOG_GUEST_ERROR,
  832. "XIVE: invalid load on IRQ %d trigger page at "
  833. "0x%"HWADDR_PRIx"\n", srcno, addr);
  834. return -1;
  835. }
  836. switch (offset) {
  837. case XIVE_ESB_LOAD_EOI ... XIVE_ESB_LOAD_EOI + 0x7FF:
  838. ret = xive_source_esb_eoi(xsrc, srcno);
  839. /* Forward the source event notification for routing */
  840. if (ret) {
  841. xive_source_notify(xsrc, srcno);
  842. }
  843. break;
  844. case XIVE_ESB_GET ... XIVE_ESB_GET + 0x3FF:
  845. ret = xive_source_esb_get(xsrc, srcno);
  846. break;
  847. case XIVE_ESB_SET_PQ_00 ... XIVE_ESB_SET_PQ_00 + 0x0FF:
  848. case XIVE_ESB_SET_PQ_01 ... XIVE_ESB_SET_PQ_01 + 0x0FF:
  849. case XIVE_ESB_SET_PQ_10 ... XIVE_ESB_SET_PQ_10 + 0x0FF:
  850. case XIVE_ESB_SET_PQ_11 ... XIVE_ESB_SET_PQ_11 + 0x0FF:
  851. ret = xive_source_esb_set(xsrc, srcno, (offset >> 8) & 0x3);
  852. break;
  853. default:
  854. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid ESB load addr %x\n",
  855. offset);
  856. }
  857. return ret;
  858. }
  859. /*
  860. * ESB MMIO stores
  861. * Trigger page Management/EOI page
  862. *
  863. * ESB MMIO setting 2 pages 1 or 2 pages
  864. *
  865. * 0x000 .. 0x3FF Trigger Trigger
  866. * 0x400 .. 0x7FF Trigger EOI
  867. * 0x800 .. 0xBFF Trigger undefined
  868. * 0xC00 .. 0xCFF Trigger PQ=00
  869. * 0xD00 .. 0xDFF Trigger PQ=01
  870. * 0xE00 .. 0xDFF Trigger PQ=10
  871. * 0xF00 .. 0xDFF Trigger PQ=11
  872. */
  873. static void xive_source_esb_write(void *opaque, hwaddr addr,
  874. uint64_t value, unsigned size)
  875. {
  876. XiveSource *xsrc = XIVE_SOURCE(opaque);
  877. uint32_t offset = addr & 0xFFF;
  878. uint32_t srcno = addr >> xsrc->esb_shift;
  879. bool notify = false;
  880. /* In a two pages ESB MMIO setting, trigger page only triggers */
  881. if (xive_source_is_trigger_page(xsrc, addr)) {
  882. notify = xive_source_esb_trigger(xsrc, srcno);
  883. goto out;
  884. }
  885. switch (offset) {
  886. case 0 ... 0x3FF:
  887. notify = xive_source_esb_trigger(xsrc, srcno);
  888. break;
  889. case XIVE_ESB_STORE_EOI ... XIVE_ESB_STORE_EOI + 0x3FF:
  890. if (!(xsrc->esb_flags & XIVE_SRC_STORE_EOI)) {
  891. qemu_log_mask(LOG_GUEST_ERROR,
  892. "XIVE: invalid Store EOI for IRQ %d\n", srcno);
  893. return;
  894. }
  895. notify = xive_source_esb_eoi(xsrc, srcno);
  896. break;
  897. case XIVE_ESB_SET_PQ_00 ... XIVE_ESB_SET_PQ_00 + 0x0FF:
  898. case XIVE_ESB_SET_PQ_01 ... XIVE_ESB_SET_PQ_01 + 0x0FF:
  899. case XIVE_ESB_SET_PQ_10 ... XIVE_ESB_SET_PQ_10 + 0x0FF:
  900. case XIVE_ESB_SET_PQ_11 ... XIVE_ESB_SET_PQ_11 + 0x0FF:
  901. xive_source_esb_set(xsrc, srcno, (offset >> 8) & 0x3);
  902. break;
  903. default:
  904. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid ESB write addr %x\n",
  905. offset);
  906. return;
  907. }
  908. out:
  909. /* Forward the source event notification for routing */
  910. if (notify) {
  911. xive_source_notify(xsrc, srcno);
  912. }
  913. }
  914. static const MemoryRegionOps xive_source_esb_ops = {
  915. .read = xive_source_esb_read,
  916. .write = xive_source_esb_write,
  917. .endianness = DEVICE_BIG_ENDIAN,
  918. .valid = {
  919. .min_access_size = 8,
  920. .max_access_size = 8,
  921. },
  922. .impl = {
  923. .min_access_size = 8,
  924. .max_access_size = 8,
  925. },
  926. };
  927. void xive_source_set_irq(void *opaque, int srcno, int val)
  928. {
  929. XiveSource *xsrc = XIVE_SOURCE(opaque);
  930. bool notify = false;
  931. if (xive_source_irq_is_lsi(xsrc, srcno)) {
  932. if (val) {
  933. notify = xive_source_lsi_trigger(xsrc, srcno);
  934. } else {
  935. xsrc->status[srcno] &= ~XIVE_STATUS_ASSERTED;
  936. }
  937. } else {
  938. if (val) {
  939. notify = xive_source_esb_trigger(xsrc, srcno);
  940. }
  941. }
  942. /* Forward the source event notification for routing */
  943. if (notify) {
  944. xive_source_notify(xsrc, srcno);
  945. }
  946. }
  947. void xive_source_pic_print_info(XiveSource *xsrc, uint32_t offset, Monitor *mon)
  948. {
  949. int i;
  950. for (i = 0; i < xsrc->nr_irqs; i++) {
  951. uint8_t pq = xive_source_esb_get(xsrc, i);
  952. if (pq == XIVE_ESB_OFF) {
  953. continue;
  954. }
  955. monitor_printf(mon, " %08x %s %c%c%c\n", i + offset,
  956. xive_source_irq_is_lsi(xsrc, i) ? "LSI" : "MSI",
  957. pq & XIVE_ESB_VAL_P ? 'P' : '-',
  958. pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
  959. xsrc->status[i] & XIVE_STATUS_ASSERTED ? 'A' : ' ');
  960. }
  961. }
  962. static void xive_source_reset(void *dev)
  963. {
  964. XiveSource *xsrc = XIVE_SOURCE(dev);
  965. /* Do not clear the LSI bitmap */
  966. /* PQs are initialized to 0b01 (Q=1) which corresponds to "ints off" */
  967. memset(xsrc->status, XIVE_ESB_OFF, xsrc->nr_irqs);
  968. }
  969. static void xive_source_realize(DeviceState *dev, Error **errp)
  970. {
  971. XiveSource *xsrc = XIVE_SOURCE(dev);
  972. size_t esb_len = xive_source_esb_len(xsrc);
  973. assert(xsrc->xive);
  974. if (!xsrc->nr_irqs) {
  975. error_setg(errp, "Number of interrupt needs to be greater than 0");
  976. return;
  977. }
  978. if (xsrc->esb_shift != XIVE_ESB_4K &&
  979. xsrc->esb_shift != XIVE_ESB_4K_2PAGE &&
  980. xsrc->esb_shift != XIVE_ESB_64K &&
  981. xsrc->esb_shift != XIVE_ESB_64K_2PAGE) {
  982. error_setg(errp, "Invalid ESB shift setting");
  983. return;
  984. }
  985. xsrc->status = g_malloc0(xsrc->nr_irqs);
  986. xsrc->lsi_map = bitmap_new(xsrc->nr_irqs);
  987. memory_region_init(&xsrc->esb_mmio, OBJECT(xsrc), "xive.esb", esb_len);
  988. memory_region_init_io(&xsrc->esb_mmio_emulated, OBJECT(xsrc),
  989. &xive_source_esb_ops, xsrc, "xive.esb-emulated",
  990. esb_len);
  991. memory_region_add_subregion(&xsrc->esb_mmio, 0, &xsrc->esb_mmio_emulated);
  992. qemu_register_reset(xive_source_reset, dev);
  993. }
  994. static const VMStateDescription vmstate_xive_source = {
  995. .name = TYPE_XIVE_SOURCE,
  996. .version_id = 1,
  997. .minimum_version_id = 1,
  998. .fields = (VMStateField[]) {
  999. VMSTATE_UINT32_EQUAL(nr_irqs, XiveSource, NULL),
  1000. VMSTATE_VBUFFER_UINT32(status, XiveSource, 1, NULL, nr_irqs),
  1001. VMSTATE_END_OF_LIST()
  1002. },
  1003. };
  1004. /*
  1005. * The default XIVE interrupt source setting for the ESB MMIOs is two
  1006. * 64k pages without Store EOI, to be in sync with KVM.
  1007. */
  1008. static Property xive_source_properties[] = {
  1009. DEFINE_PROP_UINT64("flags", XiveSource, esb_flags, 0),
  1010. DEFINE_PROP_UINT32("nr-irqs", XiveSource, nr_irqs, 0),
  1011. DEFINE_PROP_UINT32("shift", XiveSource, esb_shift, XIVE_ESB_64K_2PAGE),
  1012. DEFINE_PROP_LINK("xive", XiveSource, xive, TYPE_XIVE_NOTIFIER,
  1013. XiveNotifier *),
  1014. DEFINE_PROP_END_OF_LIST(),
  1015. };
  1016. static void xive_source_class_init(ObjectClass *klass, void *data)
  1017. {
  1018. DeviceClass *dc = DEVICE_CLASS(klass);
  1019. dc->desc = "XIVE Interrupt Source";
  1020. device_class_set_props(dc, xive_source_properties);
  1021. dc->realize = xive_source_realize;
  1022. dc->vmsd = &vmstate_xive_source;
  1023. /*
  1024. * Reason: part of XIVE interrupt controller, needs to be wired up,
  1025. * e.g. by spapr_xive_instance_init().
  1026. */
  1027. dc->user_creatable = false;
  1028. }
  1029. static const TypeInfo xive_source_info = {
  1030. .name = TYPE_XIVE_SOURCE,
  1031. .parent = TYPE_DEVICE,
  1032. .instance_size = sizeof(XiveSource),
  1033. .class_init = xive_source_class_init,
  1034. };
  1035. /*
  1036. * XiveEND helpers
  1037. */
  1038. void xive_end_queue_pic_print_info(XiveEND *end, uint32_t width, Monitor *mon)
  1039. {
  1040. uint64_t qaddr_base = xive_end_qaddr(end);
  1041. uint32_t qsize = xive_get_field32(END_W0_QSIZE, end->w0);
  1042. uint32_t qindex = xive_get_field32(END_W1_PAGE_OFF, end->w1);
  1043. uint32_t qentries = 1 << (qsize + 10);
  1044. int i;
  1045. /*
  1046. * print out the [ (qindex - (width - 1)) .. (qindex + 1)] window
  1047. */
  1048. monitor_printf(mon, " [ ");
  1049. qindex = (qindex - (width - 1)) & (qentries - 1);
  1050. for (i = 0; i < width; i++) {
  1051. uint64_t qaddr = qaddr_base + (qindex << 2);
  1052. uint32_t qdata = -1;
  1053. if (dma_memory_read(&address_space_memory, qaddr, &qdata,
  1054. sizeof(qdata))) {
  1055. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to read EQ @0x%"
  1056. HWADDR_PRIx "\n", qaddr);
  1057. return;
  1058. }
  1059. monitor_printf(mon, "%s%08x ", i == width - 1 ? "^" : "",
  1060. be32_to_cpu(qdata));
  1061. qindex = (qindex + 1) & (qentries - 1);
  1062. }
  1063. monitor_printf(mon, "]");
  1064. }
  1065. void xive_end_pic_print_info(XiveEND *end, uint32_t end_idx, Monitor *mon)
  1066. {
  1067. uint64_t qaddr_base = xive_end_qaddr(end);
  1068. uint32_t qindex = xive_get_field32(END_W1_PAGE_OFF, end->w1);
  1069. uint32_t qgen = xive_get_field32(END_W1_GENERATION, end->w1);
  1070. uint32_t qsize = xive_get_field32(END_W0_QSIZE, end->w0);
  1071. uint32_t qentries = 1 << (qsize + 10);
  1072. uint32_t nvt_blk = xive_get_field32(END_W6_NVT_BLOCK, end->w6);
  1073. uint32_t nvt_idx = xive_get_field32(END_W6_NVT_INDEX, end->w6);
  1074. uint8_t priority = xive_get_field32(END_W7_F0_PRIORITY, end->w7);
  1075. uint8_t pq;
  1076. if (!xive_end_is_valid(end)) {
  1077. return;
  1078. }
  1079. pq = xive_get_field32(END_W1_ESn, end->w1);
  1080. monitor_printf(mon, " %08x %c%c %c%c%c%c%c%c%c prio:%d nvt:%02x/%04x",
  1081. end_idx,
  1082. pq & XIVE_ESB_VAL_P ? 'P' : '-',
  1083. pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
  1084. xive_end_is_valid(end) ? 'v' : '-',
  1085. xive_end_is_enqueue(end) ? 'q' : '-',
  1086. xive_end_is_notify(end) ? 'n' : '-',
  1087. xive_end_is_backlog(end) ? 'b' : '-',
  1088. xive_end_is_escalate(end) ? 'e' : '-',
  1089. xive_end_is_uncond_escalation(end) ? 'u' : '-',
  1090. xive_end_is_silent_escalation(end) ? 's' : '-',
  1091. priority, nvt_blk, nvt_idx);
  1092. if (qaddr_base) {
  1093. monitor_printf(mon, " eq:@%08"PRIx64"% 6d/%5d ^%d",
  1094. qaddr_base, qindex, qentries, qgen);
  1095. xive_end_queue_pic_print_info(end, 6, mon);
  1096. }
  1097. monitor_printf(mon, "\n");
  1098. }
  1099. static void xive_end_enqueue(XiveEND *end, uint32_t data)
  1100. {
  1101. uint64_t qaddr_base = xive_end_qaddr(end);
  1102. uint32_t qsize = xive_get_field32(END_W0_QSIZE, end->w0);
  1103. uint32_t qindex = xive_get_field32(END_W1_PAGE_OFF, end->w1);
  1104. uint32_t qgen = xive_get_field32(END_W1_GENERATION, end->w1);
  1105. uint64_t qaddr = qaddr_base + (qindex << 2);
  1106. uint32_t qdata = cpu_to_be32((qgen << 31) | (data & 0x7fffffff));
  1107. uint32_t qentries = 1 << (qsize + 10);
  1108. if (dma_memory_write(&address_space_memory, qaddr, &qdata, sizeof(qdata))) {
  1109. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to write END data @0x%"
  1110. HWADDR_PRIx "\n", qaddr);
  1111. return;
  1112. }
  1113. qindex = (qindex + 1) & (qentries - 1);
  1114. if (qindex == 0) {
  1115. qgen ^= 1;
  1116. end->w1 = xive_set_field32(END_W1_GENERATION, end->w1, qgen);
  1117. }
  1118. end->w1 = xive_set_field32(END_W1_PAGE_OFF, end->w1, qindex);
  1119. }
  1120. void xive_end_eas_pic_print_info(XiveEND *end, uint32_t end_idx,
  1121. Monitor *mon)
  1122. {
  1123. XiveEAS *eas = (XiveEAS *) &end->w4;
  1124. uint8_t pq;
  1125. if (!xive_end_is_escalate(end)) {
  1126. return;
  1127. }
  1128. pq = xive_get_field32(END_W1_ESe, end->w1);
  1129. monitor_printf(mon, " %08x %c%c %c%c end:%02x/%04x data:%08x\n",
  1130. end_idx,
  1131. pq & XIVE_ESB_VAL_P ? 'P' : '-',
  1132. pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
  1133. xive_eas_is_valid(eas) ? 'V' : ' ',
  1134. xive_eas_is_masked(eas) ? 'M' : ' ',
  1135. (uint8_t) xive_get_field64(EAS_END_BLOCK, eas->w),
  1136. (uint32_t) xive_get_field64(EAS_END_INDEX, eas->w),
  1137. (uint32_t) xive_get_field64(EAS_END_DATA, eas->w));
  1138. }
  1139. /*
  1140. * XIVE Router (aka. Virtualization Controller or IVRE)
  1141. */
  1142. int xive_router_get_eas(XiveRouter *xrtr, uint8_t eas_blk, uint32_t eas_idx,
  1143. XiveEAS *eas)
  1144. {
  1145. XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
  1146. return xrc->get_eas(xrtr, eas_blk, eas_idx, eas);
  1147. }
  1148. int xive_router_get_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
  1149. XiveEND *end)
  1150. {
  1151. XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
  1152. return xrc->get_end(xrtr, end_blk, end_idx, end);
  1153. }
  1154. int xive_router_write_end(XiveRouter *xrtr, uint8_t end_blk, uint32_t end_idx,
  1155. XiveEND *end, uint8_t word_number)
  1156. {
  1157. XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
  1158. return xrc->write_end(xrtr, end_blk, end_idx, end, word_number);
  1159. }
  1160. int xive_router_get_nvt(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
  1161. XiveNVT *nvt)
  1162. {
  1163. XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
  1164. return xrc->get_nvt(xrtr, nvt_blk, nvt_idx, nvt);
  1165. }
  1166. int xive_router_write_nvt(XiveRouter *xrtr, uint8_t nvt_blk, uint32_t nvt_idx,
  1167. XiveNVT *nvt, uint8_t word_number)
  1168. {
  1169. XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
  1170. return xrc->write_nvt(xrtr, nvt_blk, nvt_idx, nvt, word_number);
  1171. }
  1172. static int xive_router_get_block_id(XiveRouter *xrtr)
  1173. {
  1174. XiveRouterClass *xrc = XIVE_ROUTER_GET_CLASS(xrtr);
  1175. return xrc->get_block_id(xrtr);
  1176. }
  1177. static void xive_router_realize(DeviceState *dev, Error **errp)
  1178. {
  1179. XiveRouter *xrtr = XIVE_ROUTER(dev);
  1180. assert(xrtr->xfb);
  1181. }
  1182. /*
  1183. * Encode the HW CAM line in the block group mode format :
  1184. *
  1185. * chip << 19 | 0000000 0 0001 thread (7Bit)
  1186. */
  1187. static uint32_t xive_tctx_hw_cam_line(XivePresenter *xptr, XiveTCTX *tctx)
  1188. {
  1189. CPUPPCState *env = &POWERPC_CPU(tctx->cs)->env;
  1190. uint32_t pir = env->spr_cb[SPR_PIR].default_value;
  1191. uint8_t blk = xive_router_get_block_id(XIVE_ROUTER(xptr));
  1192. return xive_nvt_cam_line(blk, 1 << 7 | (pir & 0x7f));
  1193. }
  1194. /*
  1195. * The thread context register words are in big-endian format.
  1196. */
  1197. int xive_presenter_tctx_match(XivePresenter *xptr, XiveTCTX *tctx,
  1198. uint8_t format,
  1199. uint8_t nvt_blk, uint32_t nvt_idx,
  1200. bool cam_ignore, uint32_t logic_serv)
  1201. {
  1202. uint32_t cam = xive_nvt_cam_line(nvt_blk, nvt_idx);
  1203. uint32_t qw3w2 = xive_tctx_word2(&tctx->regs[TM_QW3_HV_PHYS]);
  1204. uint32_t qw2w2 = xive_tctx_word2(&tctx->regs[TM_QW2_HV_POOL]);
  1205. uint32_t qw1w2 = xive_tctx_word2(&tctx->regs[TM_QW1_OS]);
  1206. uint32_t qw0w2 = xive_tctx_word2(&tctx->regs[TM_QW0_USER]);
  1207. /*
  1208. * TODO (PowerNV): ignore mode. The low order bits of the NVT
  1209. * identifier are ignored in the "CAM" match.
  1210. */
  1211. if (format == 0) {
  1212. if (cam_ignore == true) {
  1213. /*
  1214. * F=0 & i=1: Logical server notification (bits ignored at
  1215. * the end of the NVT identifier)
  1216. */
  1217. qemu_log_mask(LOG_UNIMP, "XIVE: no support for LS NVT %x/%x\n",
  1218. nvt_blk, nvt_idx);
  1219. return -1;
  1220. }
  1221. /* F=0 & i=0: Specific NVT notification */
  1222. /* PHYS ring */
  1223. if ((be32_to_cpu(qw3w2) & TM_QW3W2_VT) &&
  1224. cam == xive_tctx_hw_cam_line(xptr, tctx)) {
  1225. return TM_QW3_HV_PHYS;
  1226. }
  1227. /* HV POOL ring */
  1228. if ((be32_to_cpu(qw2w2) & TM_QW2W2_VP) &&
  1229. cam == xive_get_field32(TM_QW2W2_POOL_CAM, qw2w2)) {
  1230. return TM_QW2_HV_POOL;
  1231. }
  1232. /* OS ring */
  1233. if ((be32_to_cpu(qw1w2) & TM_QW1W2_VO) &&
  1234. cam == xive_get_field32(TM_QW1W2_OS_CAM, qw1w2)) {
  1235. return TM_QW1_OS;
  1236. }
  1237. } else {
  1238. /* F=1 : User level Event-Based Branch (EBB) notification */
  1239. /* USER ring */
  1240. if ((be32_to_cpu(qw1w2) & TM_QW1W2_VO) &&
  1241. (cam == xive_get_field32(TM_QW1W2_OS_CAM, qw1w2)) &&
  1242. (be32_to_cpu(qw0w2) & TM_QW0W2_VU) &&
  1243. (logic_serv == xive_get_field32(TM_QW0W2_LOGIC_SERV, qw0w2))) {
  1244. return TM_QW0_USER;
  1245. }
  1246. }
  1247. return -1;
  1248. }
  1249. /*
  1250. * This is our simple Xive Presenter Engine model. It is merged in the
  1251. * Router as it does not require an extra object.
  1252. *
  1253. * It receives notification requests sent by the IVRE to find one
  1254. * matching NVT (or more) dispatched on the processor threads. In case
  1255. * of a single NVT notification, the process is abreviated and the
  1256. * thread is signaled if a match is found. In case of a logical server
  1257. * notification (bits ignored at the end of the NVT identifier), the
  1258. * IVPE and IVRE select a winning thread using different filters. This
  1259. * involves 2 or 3 exchanges on the PowerBus that the model does not
  1260. * support.
  1261. *
  1262. * The parameters represent what is sent on the PowerBus
  1263. */
  1264. static bool xive_presenter_notify(XiveFabric *xfb, uint8_t format,
  1265. uint8_t nvt_blk, uint32_t nvt_idx,
  1266. bool cam_ignore, uint8_t priority,
  1267. uint32_t logic_serv)
  1268. {
  1269. XiveFabricClass *xfc = XIVE_FABRIC_GET_CLASS(xfb);
  1270. XiveTCTXMatch match = { .tctx = NULL, .ring = 0 };
  1271. int count;
  1272. /*
  1273. * Ask the machine to scan the interrupt controllers for a match
  1274. */
  1275. count = xfc->match_nvt(xfb, format, nvt_blk, nvt_idx, cam_ignore,
  1276. priority, logic_serv, &match);
  1277. if (count < 0) {
  1278. return false;
  1279. }
  1280. /* handle CPU exception delivery */
  1281. if (count) {
  1282. xive_tctx_ipb_update(match.tctx, match.ring, priority_to_ipb(priority));
  1283. }
  1284. return !!count;
  1285. }
  1286. /*
  1287. * Notification using the END ESe/ESn bit (Event State Buffer for
  1288. * escalation and notification). Provide further coalescing in the
  1289. * Router.
  1290. */
  1291. static bool xive_router_end_es_notify(XiveRouter *xrtr, uint8_t end_blk,
  1292. uint32_t end_idx, XiveEND *end,
  1293. uint32_t end_esmask)
  1294. {
  1295. uint8_t pq = xive_get_field32(end_esmask, end->w1);
  1296. bool notify = xive_esb_trigger(&pq);
  1297. if (pq != xive_get_field32(end_esmask, end->w1)) {
  1298. end->w1 = xive_set_field32(end_esmask, end->w1, pq);
  1299. xive_router_write_end(xrtr, end_blk, end_idx, end, 1);
  1300. }
  1301. /* ESe/n[Q]=1 : end of notification */
  1302. return notify;
  1303. }
  1304. /*
  1305. * An END trigger can come from an event trigger (IPI or HW) or from
  1306. * another chip. We don't model the PowerBus but the END trigger
  1307. * message has the same parameters than in the function below.
  1308. */
  1309. static void xive_router_end_notify(XiveRouter *xrtr, uint8_t end_blk,
  1310. uint32_t end_idx, uint32_t end_data)
  1311. {
  1312. XiveEND end;
  1313. uint8_t priority;
  1314. uint8_t format;
  1315. uint8_t nvt_blk;
  1316. uint32_t nvt_idx;
  1317. XiveNVT nvt;
  1318. bool found;
  1319. /* END cache lookup */
  1320. if (xive_router_get_end(xrtr, end_blk, end_idx, &end)) {
  1321. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No END %x/%x\n", end_blk,
  1322. end_idx);
  1323. return;
  1324. }
  1325. if (!xive_end_is_valid(&end)) {
  1326. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: END %x/%x is invalid\n",
  1327. end_blk, end_idx);
  1328. return;
  1329. }
  1330. if (xive_end_is_enqueue(&end)) {
  1331. xive_end_enqueue(&end, end_data);
  1332. /* Enqueuing event data modifies the EQ toggle and index */
  1333. xive_router_write_end(xrtr, end_blk, end_idx, &end, 1);
  1334. }
  1335. /*
  1336. * When the END is silent, we skip the notification part.
  1337. */
  1338. if (xive_end_is_silent_escalation(&end)) {
  1339. goto do_escalation;
  1340. }
  1341. /*
  1342. * The W7 format depends on the F bit in W6. It defines the type
  1343. * of the notification :
  1344. *
  1345. * F=0 : single or multiple NVT notification
  1346. * F=1 : User level Event-Based Branch (EBB) notification, no
  1347. * priority
  1348. */
  1349. format = xive_get_field32(END_W6_FORMAT_BIT, end.w6);
  1350. priority = xive_get_field32(END_W7_F0_PRIORITY, end.w7);
  1351. /* The END is masked */
  1352. if (format == 0 && priority == 0xff) {
  1353. return;
  1354. }
  1355. /*
  1356. * Check the END ESn (Event State Buffer for notification) for
  1357. * even further coalescing in the Router
  1358. */
  1359. if (!xive_end_is_notify(&end)) {
  1360. /* ESn[Q]=1 : end of notification */
  1361. if (!xive_router_end_es_notify(xrtr, end_blk, end_idx,
  1362. &end, END_W1_ESn)) {
  1363. return;
  1364. }
  1365. }
  1366. /*
  1367. * Follows IVPE notification
  1368. */
  1369. nvt_blk = xive_get_field32(END_W6_NVT_BLOCK, end.w6);
  1370. nvt_idx = xive_get_field32(END_W6_NVT_INDEX, end.w6);
  1371. /* NVT cache lookup */
  1372. if (xive_router_get_nvt(xrtr, nvt_blk, nvt_idx, &nvt)) {
  1373. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: no NVT %x/%x\n",
  1374. nvt_blk, nvt_idx);
  1375. return;
  1376. }
  1377. if (!xive_nvt_is_valid(&nvt)) {
  1378. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: NVT %x/%x is invalid\n",
  1379. nvt_blk, nvt_idx);
  1380. return;
  1381. }
  1382. found = xive_presenter_notify(xrtr->xfb, format, nvt_blk, nvt_idx,
  1383. xive_get_field32(END_W7_F0_IGNORE, end.w7),
  1384. priority,
  1385. xive_get_field32(END_W7_F1_LOG_SERVER_ID, end.w7));
  1386. /* TODO: Auto EOI. */
  1387. if (found) {
  1388. return;
  1389. }
  1390. /*
  1391. * If no matching NVT is dispatched on a HW thread :
  1392. * - specific VP: update the NVT structure if backlog is activated
  1393. * - logical server : forward request to IVPE (not supported)
  1394. */
  1395. if (xive_end_is_backlog(&end)) {
  1396. uint8_t ipb;
  1397. if (format == 1) {
  1398. qemu_log_mask(LOG_GUEST_ERROR,
  1399. "XIVE: END %x/%x invalid config: F1 & backlog\n",
  1400. end_blk, end_idx);
  1401. return;
  1402. }
  1403. /*
  1404. * Record the IPB in the associated NVT structure for later
  1405. * use. The presenter will resend the interrupt when the vCPU
  1406. * is dispatched again on a HW thread.
  1407. */
  1408. ipb = xive_get_field32(NVT_W4_IPB, nvt.w4) | priority_to_ipb(priority);
  1409. nvt.w4 = xive_set_field32(NVT_W4_IPB, nvt.w4, ipb);
  1410. xive_router_write_nvt(xrtr, nvt_blk, nvt_idx, &nvt, 4);
  1411. /*
  1412. * On HW, follows a "Broadcast Backlog" to IVPEs
  1413. */
  1414. }
  1415. do_escalation:
  1416. /*
  1417. * If activated, escalate notification using the ESe PQ bits and
  1418. * the EAS in w4-5
  1419. */
  1420. if (!xive_end_is_escalate(&end)) {
  1421. return;
  1422. }
  1423. /*
  1424. * Check the END ESe (Event State Buffer for escalation) for even
  1425. * further coalescing in the Router
  1426. */
  1427. if (!xive_end_is_uncond_escalation(&end)) {
  1428. /* ESe[Q]=1 : end of notification */
  1429. if (!xive_router_end_es_notify(xrtr, end_blk, end_idx,
  1430. &end, END_W1_ESe)) {
  1431. return;
  1432. }
  1433. }
  1434. /*
  1435. * The END trigger becomes an Escalation trigger
  1436. */
  1437. xive_router_end_notify(xrtr,
  1438. xive_get_field32(END_W4_ESC_END_BLOCK, end.w4),
  1439. xive_get_field32(END_W4_ESC_END_INDEX, end.w4),
  1440. xive_get_field32(END_W5_ESC_END_DATA, end.w5));
  1441. }
  1442. void xive_router_notify(XiveNotifier *xn, uint32_t lisn)
  1443. {
  1444. XiveRouter *xrtr = XIVE_ROUTER(xn);
  1445. uint8_t eas_blk = XIVE_EAS_BLOCK(lisn);
  1446. uint32_t eas_idx = XIVE_EAS_INDEX(lisn);
  1447. XiveEAS eas;
  1448. /* EAS cache lookup */
  1449. if (xive_router_get_eas(xrtr, eas_blk, eas_idx, &eas)) {
  1450. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN %x\n", lisn);
  1451. return;
  1452. }
  1453. /*
  1454. * The IVRE checks the State Bit Cache at this point. We skip the
  1455. * SBC lookup because the state bits of the sources are modeled
  1456. * internally in QEMU.
  1457. */
  1458. if (!xive_eas_is_valid(&eas)) {
  1459. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid LISN %x\n", lisn);
  1460. return;
  1461. }
  1462. if (xive_eas_is_masked(&eas)) {
  1463. /* Notification completed */
  1464. return;
  1465. }
  1466. /*
  1467. * The event trigger becomes an END trigger
  1468. */
  1469. xive_router_end_notify(xrtr,
  1470. xive_get_field64(EAS_END_BLOCK, eas.w),
  1471. xive_get_field64(EAS_END_INDEX, eas.w),
  1472. xive_get_field64(EAS_END_DATA, eas.w));
  1473. }
  1474. static Property xive_router_properties[] = {
  1475. DEFINE_PROP_LINK("xive-fabric", XiveRouter, xfb,
  1476. TYPE_XIVE_FABRIC, XiveFabric *),
  1477. DEFINE_PROP_END_OF_LIST(),
  1478. };
  1479. static void xive_router_class_init(ObjectClass *klass, void *data)
  1480. {
  1481. DeviceClass *dc = DEVICE_CLASS(klass);
  1482. XiveNotifierClass *xnc = XIVE_NOTIFIER_CLASS(klass);
  1483. dc->desc = "XIVE Router Engine";
  1484. device_class_set_props(dc, xive_router_properties);
  1485. /* Parent is SysBusDeviceClass. No need to call its realize hook */
  1486. dc->realize = xive_router_realize;
  1487. xnc->notify = xive_router_notify;
  1488. }
  1489. static const TypeInfo xive_router_info = {
  1490. .name = TYPE_XIVE_ROUTER,
  1491. .parent = TYPE_SYS_BUS_DEVICE,
  1492. .abstract = true,
  1493. .instance_size = sizeof(XiveRouter),
  1494. .class_size = sizeof(XiveRouterClass),
  1495. .class_init = xive_router_class_init,
  1496. .interfaces = (InterfaceInfo[]) {
  1497. { TYPE_XIVE_NOTIFIER },
  1498. { TYPE_XIVE_PRESENTER },
  1499. { }
  1500. }
  1501. };
  1502. void xive_eas_pic_print_info(XiveEAS *eas, uint32_t lisn, Monitor *mon)
  1503. {
  1504. if (!xive_eas_is_valid(eas)) {
  1505. return;
  1506. }
  1507. monitor_printf(mon, " %08x %s end:%02x/%04x data:%08x\n",
  1508. lisn, xive_eas_is_masked(eas) ? "M" : " ",
  1509. (uint8_t) xive_get_field64(EAS_END_BLOCK, eas->w),
  1510. (uint32_t) xive_get_field64(EAS_END_INDEX, eas->w),
  1511. (uint32_t) xive_get_field64(EAS_END_DATA, eas->w));
  1512. }
  1513. /*
  1514. * END ESB MMIO loads
  1515. */
  1516. static uint64_t xive_end_source_read(void *opaque, hwaddr addr, unsigned size)
  1517. {
  1518. XiveENDSource *xsrc = XIVE_END_SOURCE(opaque);
  1519. uint32_t offset = addr & 0xFFF;
  1520. uint8_t end_blk;
  1521. uint32_t end_idx;
  1522. XiveEND end;
  1523. uint32_t end_esmask;
  1524. uint8_t pq;
  1525. uint64_t ret = -1;
  1526. /*
  1527. * The block id should be deduced from the load address on the END
  1528. * ESB MMIO but our model only supports a single block per XIVE chip.
  1529. */
  1530. end_blk = xive_router_get_block_id(xsrc->xrtr);
  1531. end_idx = addr >> (xsrc->esb_shift + 1);
  1532. if (xive_router_get_end(xsrc->xrtr, end_blk, end_idx, &end)) {
  1533. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No END %x/%x\n", end_blk,
  1534. end_idx);
  1535. return -1;
  1536. }
  1537. if (!xive_end_is_valid(&end)) {
  1538. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: END %x/%x is invalid\n",
  1539. end_blk, end_idx);
  1540. return -1;
  1541. }
  1542. end_esmask = addr_is_even(addr, xsrc->esb_shift) ? END_W1_ESn : END_W1_ESe;
  1543. pq = xive_get_field32(end_esmask, end.w1);
  1544. switch (offset) {
  1545. case XIVE_ESB_LOAD_EOI ... XIVE_ESB_LOAD_EOI + 0x7FF:
  1546. ret = xive_esb_eoi(&pq);
  1547. /* Forward the source event notification for routing ?? */
  1548. break;
  1549. case XIVE_ESB_GET ... XIVE_ESB_GET + 0x3FF:
  1550. ret = pq;
  1551. break;
  1552. case XIVE_ESB_SET_PQ_00 ... XIVE_ESB_SET_PQ_00 + 0x0FF:
  1553. case XIVE_ESB_SET_PQ_01 ... XIVE_ESB_SET_PQ_01 + 0x0FF:
  1554. case XIVE_ESB_SET_PQ_10 ... XIVE_ESB_SET_PQ_10 + 0x0FF:
  1555. case XIVE_ESB_SET_PQ_11 ... XIVE_ESB_SET_PQ_11 + 0x0FF:
  1556. ret = xive_esb_set(&pq, (offset >> 8) & 0x3);
  1557. break;
  1558. default:
  1559. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid END ESB load addr %d\n",
  1560. offset);
  1561. return -1;
  1562. }
  1563. if (pq != xive_get_field32(end_esmask, end.w1)) {
  1564. end.w1 = xive_set_field32(end_esmask, end.w1, pq);
  1565. xive_router_write_end(xsrc->xrtr, end_blk, end_idx, &end, 1);
  1566. }
  1567. return ret;
  1568. }
  1569. /*
  1570. * END ESB MMIO stores are invalid
  1571. */
  1572. static void xive_end_source_write(void *opaque, hwaddr addr,
  1573. uint64_t value, unsigned size)
  1574. {
  1575. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid ESB write addr 0x%"
  1576. HWADDR_PRIx"\n", addr);
  1577. }
  1578. static const MemoryRegionOps xive_end_source_ops = {
  1579. .read = xive_end_source_read,
  1580. .write = xive_end_source_write,
  1581. .endianness = DEVICE_BIG_ENDIAN,
  1582. .valid = {
  1583. .min_access_size = 8,
  1584. .max_access_size = 8,
  1585. },
  1586. .impl = {
  1587. .min_access_size = 8,
  1588. .max_access_size = 8,
  1589. },
  1590. };
  1591. static void xive_end_source_realize(DeviceState *dev, Error **errp)
  1592. {
  1593. XiveENDSource *xsrc = XIVE_END_SOURCE(dev);
  1594. assert(xsrc->xrtr);
  1595. if (!xsrc->nr_ends) {
  1596. error_setg(errp, "Number of interrupt needs to be greater than 0");
  1597. return;
  1598. }
  1599. if (xsrc->esb_shift != XIVE_ESB_4K &&
  1600. xsrc->esb_shift != XIVE_ESB_64K) {
  1601. error_setg(errp, "Invalid ESB shift setting");
  1602. return;
  1603. }
  1604. /*
  1605. * Each END is assigned an even/odd pair of MMIO pages, the even page
  1606. * manages the ESn field while the odd page manages the ESe field.
  1607. */
  1608. memory_region_init_io(&xsrc->esb_mmio, OBJECT(xsrc),
  1609. &xive_end_source_ops, xsrc, "xive.end",
  1610. (1ull << (xsrc->esb_shift + 1)) * xsrc->nr_ends);
  1611. }
  1612. static Property xive_end_source_properties[] = {
  1613. DEFINE_PROP_UINT32("nr-ends", XiveENDSource, nr_ends, 0),
  1614. DEFINE_PROP_UINT32("shift", XiveENDSource, esb_shift, XIVE_ESB_64K),
  1615. DEFINE_PROP_LINK("xive", XiveENDSource, xrtr, TYPE_XIVE_ROUTER,
  1616. XiveRouter *),
  1617. DEFINE_PROP_END_OF_LIST(),
  1618. };
  1619. static void xive_end_source_class_init(ObjectClass *klass, void *data)
  1620. {
  1621. DeviceClass *dc = DEVICE_CLASS(klass);
  1622. dc->desc = "XIVE END Source";
  1623. device_class_set_props(dc, xive_end_source_properties);
  1624. dc->realize = xive_end_source_realize;
  1625. /*
  1626. * Reason: part of XIVE interrupt controller, needs to be wired up,
  1627. * e.g. by spapr_xive_instance_init().
  1628. */
  1629. dc->user_creatable = false;
  1630. }
  1631. static const TypeInfo xive_end_source_info = {
  1632. .name = TYPE_XIVE_END_SOURCE,
  1633. .parent = TYPE_DEVICE,
  1634. .instance_size = sizeof(XiveENDSource),
  1635. .class_init = xive_end_source_class_init,
  1636. };
  1637. /*
  1638. * XIVE Notifier
  1639. */
  1640. static const TypeInfo xive_notifier_info = {
  1641. .name = TYPE_XIVE_NOTIFIER,
  1642. .parent = TYPE_INTERFACE,
  1643. .class_size = sizeof(XiveNotifierClass),
  1644. };
  1645. /*
  1646. * XIVE Presenter
  1647. */
  1648. static const TypeInfo xive_presenter_info = {
  1649. .name = TYPE_XIVE_PRESENTER,
  1650. .parent = TYPE_INTERFACE,
  1651. .class_size = sizeof(XivePresenterClass),
  1652. };
  1653. /*
  1654. * XIVE Fabric
  1655. */
  1656. static const TypeInfo xive_fabric_info = {
  1657. .name = TYPE_XIVE_FABRIC,
  1658. .parent = TYPE_INTERFACE,
  1659. .class_size = sizeof(XiveFabricClass),
  1660. };
  1661. static void xive_register_types(void)
  1662. {
  1663. type_register_static(&xive_fabric_info);
  1664. type_register_static(&xive_source_info);
  1665. type_register_static(&xive_notifier_info);
  1666. type_register_static(&xive_presenter_info);
  1667. type_register_static(&xive_router_info);
  1668. type_register_static(&xive_end_source_info);
  1669. type_register_static(&xive_tctx_info);
  1670. }
  1671. type_init(xive_register_types)