xive.c 55 KB

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