xive.c 60 KB

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