xive2.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831
  1. /*
  2. * QEMU PowerPC XIVE2 interrupt controller model (POWER10)
  3. *
  4. * Copyright (c) 2019-2024, IBM Corporation..
  5. *
  6. * SPDX-License-Identifier: GPL-2.0-or-later
  7. */
  8. #include "qemu/osdep.h"
  9. #include "qemu/log.h"
  10. #include "qemu/module.h"
  11. #include "qapi/error.h"
  12. #include "target/ppc/cpu.h"
  13. #include "system/cpus.h"
  14. #include "system/dma.h"
  15. #include "hw/qdev-properties.h"
  16. #include "hw/ppc/xive.h"
  17. #include "hw/ppc/xive2.h"
  18. #include "hw/ppc/xive2_regs.h"
  19. #include "trace.h"
  20. uint32_t xive2_router_get_config(Xive2Router *xrtr)
  21. {
  22. Xive2RouterClass *xrc = XIVE2_ROUTER_GET_CLASS(xrtr);
  23. return xrc->get_config(xrtr);
  24. }
  25. static int xive2_router_get_block_id(Xive2Router *xrtr)
  26. {
  27. Xive2RouterClass *xrc = XIVE2_ROUTER_GET_CLASS(xrtr);
  28. return xrc->get_block_id(xrtr);
  29. }
  30. static uint64_t xive2_nvp_reporting_addr(Xive2Nvp *nvp)
  31. {
  32. uint64_t cache_addr;
  33. cache_addr = xive_get_field32(NVP2_W6_REPORTING_LINE, nvp->w6) << 24 |
  34. xive_get_field32(NVP2_W7_REPORTING_LINE, nvp->w7);
  35. cache_addr <<= 8; /* aligned on a cache line pair */
  36. return cache_addr;
  37. }
  38. static uint32_t xive2_nvgc_get_backlog(Xive2Nvgc *nvgc, uint8_t priority)
  39. {
  40. uint32_t val = 0;
  41. uint8_t *ptr, i;
  42. if (priority > 7) {
  43. return 0;
  44. }
  45. /*
  46. * The per-priority backlog counters are 24-bit and the structure
  47. * is stored in big endian. NVGC is 32-bytes long, so 24-bytes from
  48. * w2, which fits 8 priorities * 24-bits per priority.
  49. */
  50. ptr = (uint8_t *)&nvgc->w2 + priority * 3;
  51. for (i = 0; i < 3; i++, ptr++) {
  52. val = (val << 8) + *ptr;
  53. }
  54. return val;
  55. }
  56. static void xive2_nvgc_set_backlog(Xive2Nvgc *nvgc, uint8_t priority,
  57. uint32_t val)
  58. {
  59. uint8_t *ptr, i;
  60. uint32_t shift;
  61. if (priority > 7) {
  62. return;
  63. }
  64. if (val > 0xFFFFFF) {
  65. val = 0xFFFFFF;
  66. }
  67. /*
  68. * The per-priority backlog counters are 24-bit and the structure
  69. * is stored in big endian
  70. */
  71. ptr = (uint8_t *)&nvgc->w2 + priority * 3;
  72. for (i = 0; i < 3; i++, ptr++) {
  73. shift = 8 * (2 - i);
  74. *ptr = (val >> shift) & 0xFF;
  75. }
  76. }
  77. uint64_t xive2_presenter_nvgc_backlog_op(XivePresenter *xptr,
  78. bool crowd,
  79. uint8_t blk, uint32_t idx,
  80. uint16_t offset, uint16_t val)
  81. {
  82. Xive2Router *xrtr = XIVE2_ROUTER(xptr);
  83. uint8_t priority = GETFIELD(NVx_BACKLOG_PRIO, offset);
  84. uint8_t op = GETFIELD(NVx_BACKLOG_OP, offset);
  85. Xive2Nvgc nvgc;
  86. uint32_t count, old_count;
  87. if (xive2_router_get_nvgc(xrtr, crowd, blk, idx, &nvgc)) {
  88. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No %s %x/%x\n",
  89. crowd ? "NVC" : "NVG", blk, idx);
  90. return -1;
  91. }
  92. if (!xive2_nvgc_is_valid(&nvgc)) {
  93. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid NVG %x/%x\n", blk, idx);
  94. return -1;
  95. }
  96. old_count = xive2_nvgc_get_backlog(&nvgc, priority);
  97. count = old_count;
  98. /*
  99. * op:
  100. * 0b00 => increment
  101. * 0b01 => decrement
  102. * 0b1- => read
  103. */
  104. if (op == 0b00 || op == 0b01) {
  105. if (op == 0b00) {
  106. count += val;
  107. } else {
  108. if (count > val) {
  109. count -= val;
  110. } else {
  111. count = 0;
  112. }
  113. }
  114. xive2_nvgc_set_backlog(&nvgc, priority, count);
  115. xive2_router_write_nvgc(xrtr, crowd, blk, idx, &nvgc);
  116. }
  117. trace_xive_nvgc_backlog_op(crowd, blk, idx, op, priority, old_count);
  118. return old_count;
  119. }
  120. uint64_t xive2_presenter_nvp_backlog_op(XivePresenter *xptr,
  121. uint8_t blk, uint32_t idx,
  122. uint16_t offset)
  123. {
  124. Xive2Router *xrtr = XIVE2_ROUTER(xptr);
  125. uint8_t priority = GETFIELD(NVx_BACKLOG_PRIO, offset);
  126. uint8_t op = GETFIELD(NVx_BACKLOG_OP, offset);
  127. Xive2Nvp nvp;
  128. uint8_t ipb, old_ipb, rc;
  129. if (xive2_router_get_nvp(xrtr, blk, idx, &nvp)) {
  130. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No NVP %x/%x\n", blk, idx);
  131. return -1;
  132. }
  133. if (!xive2_nvp_is_valid(&nvp)) {
  134. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid NVP %x/%x\n", blk, idx);
  135. return -1;
  136. }
  137. old_ipb = xive_get_field32(NVP2_W2_IPB, nvp.w2);
  138. ipb = old_ipb;
  139. /*
  140. * op:
  141. * 0b00 => set priority bit
  142. * 0b01 => reset priority bit
  143. * 0b1- => read
  144. */
  145. if (op == 0b00 || op == 0b01) {
  146. if (op == 0b00) {
  147. ipb |= xive_priority_to_ipb(priority);
  148. } else {
  149. ipb &= ~xive_priority_to_ipb(priority);
  150. }
  151. nvp.w2 = xive_set_field32(NVP2_W2_IPB, nvp.w2, ipb);
  152. xive2_router_write_nvp(xrtr, blk, idx, &nvp, 2);
  153. }
  154. rc = !!(old_ipb & xive_priority_to_ipb(priority));
  155. trace_xive_nvp_backlog_op(blk, idx, op, priority, rc);
  156. return rc;
  157. }
  158. void xive2_eas_pic_print_info(Xive2Eas *eas, uint32_t lisn, GString *buf)
  159. {
  160. if (!xive2_eas_is_valid(eas)) {
  161. return;
  162. }
  163. g_string_append_printf(buf, " %08x %s end:%02x/%04x data:%08x\n",
  164. lisn, xive2_eas_is_masked(eas) ? "M" : " ",
  165. (uint8_t) xive_get_field64(EAS2_END_BLOCK, eas->w),
  166. (uint32_t) xive_get_field64(EAS2_END_INDEX, eas->w),
  167. (uint32_t) xive_get_field64(EAS2_END_DATA, eas->w));
  168. }
  169. void xive2_end_queue_pic_print_info(Xive2End *end, uint32_t width, GString *buf)
  170. {
  171. uint64_t qaddr_base = xive2_end_qaddr(end);
  172. uint32_t qsize = xive_get_field32(END2_W3_QSIZE, end->w3);
  173. uint32_t qindex = xive_get_field32(END2_W1_PAGE_OFF, end->w1);
  174. uint32_t qentries = 1 << (qsize + 10);
  175. int i;
  176. /*
  177. * print out the [ (qindex - (width - 1)) .. (qindex + 1)] window
  178. */
  179. g_string_append_printf(buf, " [ ");
  180. qindex = (qindex - (width - 1)) & (qentries - 1);
  181. for (i = 0; i < width; i++) {
  182. uint64_t qaddr = qaddr_base + (qindex << 2);
  183. uint32_t qdata = -1;
  184. if (dma_memory_read(&address_space_memory, qaddr, &qdata,
  185. sizeof(qdata), MEMTXATTRS_UNSPECIFIED)) {
  186. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to read EQ @0x%"
  187. HWADDR_PRIx "\n", qaddr);
  188. return;
  189. }
  190. g_string_append_printf(buf, "%s%08x ", i == width - 1 ? "^" : "",
  191. be32_to_cpu(qdata));
  192. qindex = (qindex + 1) & (qentries - 1);
  193. }
  194. g_string_append_printf(buf, "]");
  195. }
  196. void xive2_end_pic_print_info(Xive2End *end, uint32_t end_idx, GString *buf)
  197. {
  198. uint64_t qaddr_base = xive2_end_qaddr(end);
  199. uint32_t qindex = xive_get_field32(END2_W1_PAGE_OFF, end->w1);
  200. uint32_t qgen = xive_get_field32(END2_W1_GENERATION, end->w1);
  201. uint32_t qsize = xive_get_field32(END2_W3_QSIZE, end->w3);
  202. uint32_t qentries = 1 << (qsize + 10);
  203. uint32_t nvx_blk = xive_get_field32(END2_W6_VP_BLOCK, end->w6);
  204. uint32_t nvx_idx = xive_get_field32(END2_W6_VP_OFFSET, end->w6);
  205. uint8_t priority = xive_get_field32(END2_W7_F0_PRIORITY, end->w7);
  206. uint8_t pq;
  207. if (!xive2_end_is_valid(end)) {
  208. return;
  209. }
  210. pq = xive_get_field32(END2_W1_ESn, end->w1);
  211. g_string_append_printf(buf,
  212. " %08x %c%c %c%c%c%c%c%c%c%c%c%c%c %c%c "
  213. "prio:%d nvp:%02x/%04x",
  214. end_idx,
  215. pq & XIVE_ESB_VAL_P ? 'P' : '-',
  216. pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
  217. xive2_end_is_valid(end) ? 'v' : '-',
  218. xive2_end_is_enqueue(end) ? 'q' : '-',
  219. xive2_end_is_notify(end) ? 'n' : '-',
  220. xive2_end_is_backlog(end) ? 'b' : '-',
  221. xive2_end_is_precluded_escalation(end) ? 'p' : '-',
  222. xive2_end_is_escalate(end) ? 'e' : '-',
  223. xive2_end_is_escalate_end(end) ? 'N' : '-',
  224. xive2_end_is_uncond_escalation(end) ? 'u' : '-',
  225. xive2_end_is_silent_escalation(end) ? 's' : '-',
  226. xive2_end_is_firmware1(end) ? 'f' : '-',
  227. xive2_end_is_firmware2(end) ? 'F' : '-',
  228. xive2_end_is_ignore(end) ? 'i' : '-',
  229. xive2_end_is_crowd(end) ? 'c' : '-',
  230. priority, nvx_blk, nvx_idx);
  231. if (qaddr_base) {
  232. g_string_append_printf(buf, " eq:@%08"PRIx64"% 6d/%5d ^%d",
  233. qaddr_base, qindex, qentries, qgen);
  234. xive2_end_queue_pic_print_info(end, 6, buf);
  235. }
  236. g_string_append_c(buf, '\n');
  237. }
  238. void xive2_end_eas_pic_print_info(Xive2End *end, uint32_t end_idx,
  239. GString *buf)
  240. {
  241. Xive2Eas *eas = (Xive2Eas *) &end->w4;
  242. uint8_t pq;
  243. if (!xive2_end_is_escalate(end)) {
  244. return;
  245. }
  246. pq = xive_get_field32(END2_W1_ESe, end->w1);
  247. g_string_append_printf(buf, " %08x %c%c %c%c end:%02x/%04x data:%08x\n",
  248. end_idx,
  249. pq & XIVE_ESB_VAL_P ? 'P' : '-',
  250. pq & XIVE_ESB_VAL_Q ? 'Q' : '-',
  251. xive2_eas_is_valid(eas) ? 'v' : ' ',
  252. xive2_eas_is_masked(eas) ? 'M' : ' ',
  253. (uint8_t) xive_get_field64(EAS2_END_BLOCK, eas->w),
  254. (uint32_t) xive_get_field64(EAS2_END_INDEX, eas->w),
  255. (uint32_t) xive_get_field64(EAS2_END_DATA, eas->w));
  256. }
  257. void xive2_nvp_pic_print_info(Xive2Nvp *nvp, uint32_t nvp_idx, GString *buf)
  258. {
  259. uint8_t eq_blk = xive_get_field32(NVP2_W5_VP_END_BLOCK, nvp->w5);
  260. uint32_t eq_idx = xive_get_field32(NVP2_W5_VP_END_INDEX, nvp->w5);
  261. uint64_t cache_line = xive2_nvp_reporting_addr(nvp);
  262. if (!xive2_nvp_is_valid(nvp)) {
  263. return;
  264. }
  265. g_string_append_printf(buf, " %08x end:%02x/%04x IPB:%02x PGoFirst:%02x",
  266. nvp_idx, eq_blk, eq_idx,
  267. xive_get_field32(NVP2_W2_IPB, nvp->w2),
  268. xive_get_field32(NVP2_W0_PGOFIRST, nvp->w0));
  269. if (cache_line) {
  270. g_string_append_printf(buf, " reporting CL:%016"PRIx64, cache_line);
  271. }
  272. /*
  273. * When the NVP is HW controlled, more fields are updated
  274. */
  275. if (xive2_nvp_is_hw(nvp)) {
  276. g_string_append_printf(buf, " CPPR:%02x",
  277. xive_get_field32(NVP2_W2_CPPR, nvp->w2));
  278. if (xive2_nvp_is_co(nvp)) {
  279. g_string_append_printf(buf, " CO:%04x",
  280. xive_get_field32(NVP2_W1_CO_THRID, nvp->w1));
  281. }
  282. }
  283. g_string_append_c(buf, '\n');
  284. }
  285. void xive2_nvgc_pic_print_info(Xive2Nvgc *nvgc, uint32_t nvgc_idx, GString *buf)
  286. {
  287. uint8_t i;
  288. if (!xive2_nvgc_is_valid(nvgc)) {
  289. return;
  290. }
  291. g_string_append_printf(buf, " %08x PGoNext:%02x bklog: ", nvgc_idx,
  292. xive_get_field32(NVGC2_W0_PGONEXT, nvgc->w0));
  293. for (i = 0; i <= XIVE_PRIORITY_MAX; i++) {
  294. g_string_append_printf(buf, "[%d]=0x%x ",
  295. i, xive2_nvgc_get_backlog(nvgc, i));
  296. }
  297. g_string_append_printf(buf, "\n");
  298. }
  299. static void xive2_end_enqueue(Xive2End *end, uint32_t data)
  300. {
  301. uint64_t qaddr_base = xive2_end_qaddr(end);
  302. uint32_t qsize = xive_get_field32(END2_W3_QSIZE, end->w3);
  303. uint32_t qindex = xive_get_field32(END2_W1_PAGE_OFF, end->w1);
  304. uint32_t qgen = xive_get_field32(END2_W1_GENERATION, end->w1);
  305. uint64_t qaddr = qaddr_base + (qindex << 2);
  306. uint32_t qdata = cpu_to_be32((qgen << 31) | (data & 0x7fffffff));
  307. uint32_t qentries = 1 << (qsize + 10);
  308. if (dma_memory_write(&address_space_memory, qaddr, &qdata, sizeof(qdata),
  309. MEMTXATTRS_UNSPECIFIED)) {
  310. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: failed to write END data @0x%"
  311. HWADDR_PRIx "\n", qaddr);
  312. return;
  313. }
  314. qindex = (qindex + 1) & (qentries - 1);
  315. if (qindex == 0) {
  316. qgen ^= 1;
  317. end->w1 = xive_set_field32(END2_W1_GENERATION, end->w1, qgen);
  318. /* TODO(PowerNV): reset GF bit on a cache watch operation */
  319. end->w1 = xive_set_field32(END2_W1_GEN_FLIPPED, end->w1, qgen);
  320. }
  321. end->w1 = xive_set_field32(END2_W1_PAGE_OFF, end->w1, qindex);
  322. }
  323. static void xive2_pgofnext(uint8_t *nvgc_blk, uint32_t *nvgc_idx,
  324. uint8_t next_level)
  325. {
  326. uint32_t mask, next_idx;
  327. uint8_t next_blk;
  328. /*
  329. * Adjust the block and index of a VP for the next group/crowd
  330. * size (PGofFirst/PGofNext field in the NVP and NVGC structures).
  331. *
  332. * The 6-bit group level is split into a 2-bit crowd and 4-bit
  333. * group levels. Encoding is similar. However, we don't support
  334. * crowd size of 8. So a crowd level of 0b11 is bumped to a crowd
  335. * size of 16.
  336. */
  337. next_blk = NVx_CROWD_LVL(next_level);
  338. if (next_blk == 3) {
  339. next_blk = 4;
  340. }
  341. mask = (1 << next_blk) - 1;
  342. *nvgc_blk &= ~mask;
  343. *nvgc_blk |= mask >> 1;
  344. next_idx = NVx_GROUP_LVL(next_level);
  345. mask = (1 << next_idx) - 1;
  346. *nvgc_idx &= ~mask;
  347. *nvgc_idx |= mask >> 1;
  348. }
  349. /*
  350. * Scan the group chain and return the highest priority and group
  351. * level of pending group interrupts.
  352. */
  353. static uint8_t xive2_presenter_backlog_scan(XivePresenter *xptr,
  354. uint8_t nvx_blk, uint32_t nvx_idx,
  355. uint8_t first_group,
  356. uint8_t *out_level)
  357. {
  358. Xive2Router *xrtr = XIVE2_ROUTER(xptr);
  359. uint32_t nvgc_idx;
  360. uint32_t current_level, count;
  361. uint8_t nvgc_blk, prio;
  362. Xive2Nvgc nvgc;
  363. for (prio = 0; prio <= XIVE_PRIORITY_MAX; prio++) {
  364. current_level = first_group & 0x3F;
  365. nvgc_blk = nvx_blk;
  366. nvgc_idx = nvx_idx;
  367. while (current_level) {
  368. xive2_pgofnext(&nvgc_blk, &nvgc_idx, current_level);
  369. if (xive2_router_get_nvgc(xrtr, NVx_CROWD_LVL(current_level),
  370. nvgc_blk, nvgc_idx, &nvgc)) {
  371. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No NVGC %x/%x\n",
  372. nvgc_blk, nvgc_idx);
  373. return 0xFF;
  374. }
  375. if (!xive2_nvgc_is_valid(&nvgc)) {
  376. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid NVGC %x/%x\n",
  377. nvgc_blk, nvgc_idx);
  378. return 0xFF;
  379. }
  380. count = xive2_nvgc_get_backlog(&nvgc, prio);
  381. if (count) {
  382. *out_level = current_level;
  383. return prio;
  384. }
  385. current_level = xive_get_field32(NVGC2_W0_PGONEXT, nvgc.w0) & 0x3F;
  386. }
  387. }
  388. return 0xFF;
  389. }
  390. static void xive2_presenter_backlog_decr(XivePresenter *xptr,
  391. uint8_t nvx_blk, uint32_t nvx_idx,
  392. uint8_t group_prio,
  393. uint8_t group_level)
  394. {
  395. Xive2Router *xrtr = XIVE2_ROUTER(xptr);
  396. uint32_t nvgc_idx, count;
  397. uint8_t nvgc_blk;
  398. Xive2Nvgc nvgc;
  399. nvgc_blk = nvx_blk;
  400. nvgc_idx = nvx_idx;
  401. xive2_pgofnext(&nvgc_blk, &nvgc_idx, group_level);
  402. if (xive2_router_get_nvgc(xrtr, NVx_CROWD_LVL(group_level),
  403. nvgc_blk, nvgc_idx, &nvgc)) {
  404. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No NVGC %x/%x\n",
  405. nvgc_blk, nvgc_idx);
  406. return;
  407. }
  408. if (!xive2_nvgc_is_valid(&nvgc)) {
  409. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid NVGC %x/%x\n",
  410. nvgc_blk, nvgc_idx);
  411. return;
  412. }
  413. count = xive2_nvgc_get_backlog(&nvgc, group_prio);
  414. if (!count) {
  415. return;
  416. }
  417. xive2_nvgc_set_backlog(&nvgc, group_prio, count - 1);
  418. xive2_router_write_nvgc(xrtr, NVx_CROWD_LVL(group_level),
  419. nvgc_blk, nvgc_idx, &nvgc);
  420. }
  421. /*
  422. * XIVE Thread Interrupt Management Area (TIMA) - Gen2 mode
  423. *
  424. * TIMA Gen2 VP “save & restore” (S&R) indicated by H bit next to V bit
  425. *
  426. * - if a context is enabled with the H bit set, the VP context
  427. * information is retrieved from the NVP structure (“check out”)
  428. * and stored back on a context pull (“check in”), the SW receives
  429. * the same context pull information as on P9
  430. *
  431. * - the H bit cannot be changed while the V bit is set, i.e. a
  432. * context cannot be set up in the TIMA and then be “pushed” into
  433. * the NVP by changing the H bit while the context is enabled
  434. */
  435. static void xive2_tctx_save_ctx(Xive2Router *xrtr, XiveTCTX *tctx,
  436. uint8_t nvp_blk, uint32_t nvp_idx,
  437. uint8_t ring)
  438. {
  439. CPUPPCState *env = &POWERPC_CPU(tctx->cs)->env;
  440. uint32_t pir = env->spr_cb[SPR_PIR].default_value;
  441. Xive2Nvp nvp;
  442. uint8_t *regs = &tctx->regs[ring];
  443. if (xive2_router_get_nvp(xrtr, nvp_blk, nvp_idx, &nvp)) {
  444. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No NVP %x/%x\n",
  445. nvp_blk, nvp_idx);
  446. return;
  447. }
  448. if (!xive2_nvp_is_valid(&nvp)) {
  449. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid NVP %x/%x\n",
  450. nvp_blk, nvp_idx);
  451. return;
  452. }
  453. if (!xive2_nvp_is_hw(&nvp)) {
  454. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: NVP %x/%x is not HW owned\n",
  455. nvp_blk, nvp_idx);
  456. return;
  457. }
  458. if (!xive2_nvp_is_co(&nvp)) {
  459. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: NVP %x/%x is not checkout\n",
  460. nvp_blk, nvp_idx);
  461. return;
  462. }
  463. if (xive_get_field32(NVP2_W1_CO_THRID_VALID, nvp.w1) &&
  464. xive_get_field32(NVP2_W1_CO_THRID, nvp.w1) != pir) {
  465. qemu_log_mask(LOG_GUEST_ERROR,
  466. "XIVE: NVP %x/%x invalid checkout Thread %x\n",
  467. nvp_blk, nvp_idx, pir);
  468. return;
  469. }
  470. nvp.w2 = xive_set_field32(NVP2_W2_IPB, nvp.w2, regs[TM_IPB]);
  471. nvp.w2 = xive_set_field32(NVP2_W2_CPPR, nvp.w2, regs[TM_CPPR]);
  472. if (nvp.w0 & NVP2_W0_L) {
  473. /*
  474. * Typically not used. If LSMFB is restored with 0, it will
  475. * force a backlog rescan
  476. */
  477. nvp.w2 = xive_set_field32(NVP2_W2_LSMFB, nvp.w2, regs[TM_LSMFB]);
  478. }
  479. if (nvp.w0 & NVP2_W0_G) {
  480. nvp.w2 = xive_set_field32(NVP2_W2_LGS, nvp.w2, regs[TM_LGS]);
  481. }
  482. if (nvp.w0 & NVP2_W0_T) {
  483. nvp.w2 = xive_set_field32(NVP2_W2_T, nvp.w2, regs[TM_T]);
  484. }
  485. xive2_router_write_nvp(xrtr, nvp_blk, nvp_idx, &nvp, 2);
  486. nvp.w1 = xive_set_field32(NVP2_W1_CO, nvp.w1, 0);
  487. /* NVP2_W1_CO_THRID_VALID only set once */
  488. nvp.w1 = xive_set_field32(NVP2_W1_CO_THRID, nvp.w1, 0xFFFF);
  489. xive2_router_write_nvp(xrtr, nvp_blk, nvp_idx, &nvp, 1);
  490. }
  491. static void xive2_cam_decode(uint32_t cam, uint8_t *nvp_blk,
  492. uint32_t *nvp_idx, bool *valid, bool *hw)
  493. {
  494. *nvp_blk = xive2_nvp_blk(cam);
  495. *nvp_idx = xive2_nvp_idx(cam);
  496. *valid = !!(cam & TM2_W2_VALID);
  497. *hw = !!(cam & TM2_W2_HW);
  498. }
  499. /*
  500. * Encode the HW CAM line with 7bit or 8bit thread id. The thread id
  501. * width and block id width is configurable at the IC level.
  502. *
  503. * chipid << 24 | 0000 0000 0000 0000 1 threadid (7Bit)
  504. * chipid << 24 | 0000 0000 0000 0001 threadid (8Bit)
  505. */
  506. static uint32_t xive2_tctx_hw_cam_line(XivePresenter *xptr, XiveTCTX *tctx)
  507. {
  508. Xive2Router *xrtr = XIVE2_ROUTER(xptr);
  509. CPUPPCState *env = &POWERPC_CPU(tctx->cs)->env;
  510. uint32_t pir = env->spr_cb[SPR_PIR].default_value;
  511. uint8_t blk = xive2_router_get_block_id(xrtr);
  512. uint8_t tid_shift =
  513. xive2_router_get_config(xrtr) & XIVE2_THREADID_8BITS ? 8 : 7;
  514. uint8_t tid_mask = (1 << tid_shift) - 1;
  515. return xive2_nvp_cam_line(blk, 1 << tid_shift | (pir & tid_mask));
  516. }
  517. static uint64_t xive2_tm_pull_ctx(XivePresenter *xptr, XiveTCTX *tctx,
  518. hwaddr offset, unsigned size, uint8_t ring)
  519. {
  520. Xive2Router *xrtr = XIVE2_ROUTER(xptr);
  521. uint32_t target_ringw2 = xive_tctx_word2(&tctx->regs[ring]);
  522. uint32_t cam = be32_to_cpu(target_ringw2);
  523. uint8_t nvp_blk;
  524. uint32_t nvp_idx;
  525. uint8_t cur_ring;
  526. bool valid;
  527. bool do_save;
  528. xive2_cam_decode(cam, &nvp_blk, &nvp_idx, &valid, &do_save);
  529. if (!valid) {
  530. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: pulling invalid NVP %x/%x !?\n",
  531. nvp_blk, nvp_idx);
  532. }
  533. /* Invalidate CAM line of requested ring and all lower rings */
  534. for (cur_ring = TM_QW0_USER; cur_ring <= ring;
  535. cur_ring += XIVE_TM_RING_SIZE) {
  536. uint32_t ringw2 = xive_tctx_word2(&tctx->regs[cur_ring]);
  537. uint32_t ringw2_new = xive_set_field32(TM2_QW1W2_VO, ringw2, 0);
  538. memcpy(&tctx->regs[cur_ring + TM_WORD2], &ringw2_new, 4);
  539. }
  540. if (xive2_router_get_config(xrtr) & XIVE2_VP_SAVE_RESTORE && do_save) {
  541. xive2_tctx_save_ctx(xrtr, tctx, nvp_blk, nvp_idx, ring);
  542. }
  543. /*
  544. * Lower external interrupt line of requested ring and below except for
  545. * USER, which doesn't exist.
  546. */
  547. for (cur_ring = TM_QW1_OS; cur_ring <= ring;
  548. cur_ring += XIVE_TM_RING_SIZE) {
  549. xive_tctx_reset_signal(tctx, cur_ring);
  550. }
  551. return target_ringw2;
  552. }
  553. uint64_t xive2_tm_pull_os_ctx(XivePresenter *xptr, XiveTCTX *tctx,
  554. hwaddr offset, unsigned size)
  555. {
  556. return xive2_tm_pull_ctx(xptr, tctx, offset, size, TM_QW1_OS);
  557. }
  558. #define REPORT_LINE_GEN1_SIZE 16
  559. static void xive2_tm_report_line_gen1(XiveTCTX *tctx, uint8_t *data,
  560. uint8_t size)
  561. {
  562. uint8_t *regs = tctx->regs;
  563. g_assert(size == REPORT_LINE_GEN1_SIZE);
  564. memset(data, 0, size);
  565. /*
  566. * See xive architecture for description of what is saved. It is
  567. * hand-picked information to fit in 16 bytes.
  568. */
  569. data[0x0] = regs[TM_QW3_HV_PHYS + TM_NSR];
  570. data[0x1] = regs[TM_QW3_HV_PHYS + TM_CPPR];
  571. data[0x2] = regs[TM_QW3_HV_PHYS + TM_IPB];
  572. data[0x3] = regs[TM_QW2_HV_POOL + TM_IPB];
  573. data[0x4] = regs[TM_QW1_OS + TM_ACK_CNT];
  574. data[0x5] = regs[TM_QW3_HV_PHYS + TM_LGS];
  575. data[0x6] = 0xFF;
  576. data[0x7] = regs[TM_QW3_HV_PHYS + TM_WORD2] & 0x80;
  577. data[0x7] |= (regs[TM_QW2_HV_POOL + TM_WORD2] & 0x80) >> 1;
  578. data[0x7] |= (regs[TM_QW1_OS + TM_WORD2] & 0x80) >> 2;
  579. data[0x7] |= (regs[TM_QW3_HV_PHYS + TM_WORD2] & 0x3);
  580. data[0x8] = regs[TM_QW1_OS + TM_NSR];
  581. data[0x9] = regs[TM_QW1_OS + TM_CPPR];
  582. data[0xA] = regs[TM_QW1_OS + TM_IPB];
  583. data[0xB] = regs[TM_QW1_OS + TM_LGS];
  584. if (regs[TM_QW0_USER + TM_WORD2] & 0x80) {
  585. /*
  586. * Logical server extension, except VU bit replaced by EB bit
  587. * from NSR
  588. */
  589. data[0xC] = regs[TM_QW0_USER + TM_WORD2];
  590. data[0xC] &= ~0x80;
  591. data[0xC] |= regs[TM_QW0_USER + TM_NSR] & 0x80;
  592. data[0xD] = regs[TM_QW0_USER + TM_WORD2 + 1];
  593. data[0xE] = regs[TM_QW0_USER + TM_WORD2 + 2];
  594. data[0xF] = regs[TM_QW0_USER + TM_WORD2 + 3];
  595. }
  596. }
  597. static void xive2_tm_pull_ctx_ol(XivePresenter *xptr, XiveTCTX *tctx,
  598. hwaddr offset, uint64_t value,
  599. unsigned size, uint8_t ring)
  600. {
  601. Xive2Router *xrtr = XIVE2_ROUTER(xptr);
  602. uint32_t hw_cam, nvp_idx, xive2_cfg, reserved;
  603. uint8_t nvp_blk;
  604. Xive2Nvp nvp;
  605. uint64_t phys_addr;
  606. MemTxResult result;
  607. hw_cam = xive2_tctx_hw_cam_line(xptr, tctx);
  608. nvp_blk = xive2_nvp_blk(hw_cam);
  609. nvp_idx = xive2_nvp_idx(hw_cam);
  610. if (xive2_router_get_nvp(xrtr, nvp_blk, nvp_idx, &nvp)) {
  611. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No NVP %x/%x\n",
  612. nvp_blk, nvp_idx);
  613. return;
  614. }
  615. if (!xive2_nvp_is_valid(&nvp)) {
  616. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid NVP %x/%x\n",
  617. nvp_blk, nvp_idx);
  618. return;
  619. }
  620. xive2_cfg = xive2_router_get_config(xrtr);
  621. phys_addr = xive2_nvp_reporting_addr(&nvp) + 0x80; /* odd line */
  622. if (xive2_cfg & XIVE2_GEN1_TIMA_OS) {
  623. uint8_t pull_ctxt[REPORT_LINE_GEN1_SIZE];
  624. xive2_tm_report_line_gen1(tctx, pull_ctxt, REPORT_LINE_GEN1_SIZE);
  625. result = dma_memory_write(&address_space_memory, phys_addr,
  626. pull_ctxt, REPORT_LINE_GEN1_SIZE,
  627. MEMTXATTRS_UNSPECIFIED);
  628. assert(result == MEMTX_OK);
  629. } else {
  630. result = dma_memory_write(&address_space_memory, phys_addr,
  631. &tctx->regs, sizeof(tctx->regs),
  632. MEMTXATTRS_UNSPECIFIED);
  633. assert(result == MEMTX_OK);
  634. reserved = 0xFFFFFFFF;
  635. result = dma_memory_write(&address_space_memory, phys_addr + 12,
  636. &reserved, sizeof(reserved),
  637. MEMTXATTRS_UNSPECIFIED);
  638. assert(result == MEMTX_OK);
  639. }
  640. /* the rest is similar to pull context to registers */
  641. xive2_tm_pull_ctx(xptr, tctx, offset, size, ring);
  642. }
  643. void xive2_tm_pull_os_ctx_ol(XivePresenter *xptr, XiveTCTX *tctx,
  644. hwaddr offset, uint64_t value, unsigned size)
  645. {
  646. xive2_tm_pull_ctx_ol(xptr, tctx, offset, value, size, TM_QW1_OS);
  647. }
  648. void xive2_tm_pull_phys_ctx_ol(XivePresenter *xptr, XiveTCTX *tctx,
  649. hwaddr offset, uint64_t value, unsigned size)
  650. {
  651. xive2_tm_pull_ctx_ol(xptr, tctx, offset, value, size, TM_QW3_HV_PHYS);
  652. }
  653. static uint8_t xive2_tctx_restore_os_ctx(Xive2Router *xrtr, XiveTCTX *tctx,
  654. uint8_t nvp_blk, uint32_t nvp_idx,
  655. Xive2Nvp *nvp)
  656. {
  657. CPUPPCState *env = &POWERPC_CPU(tctx->cs)->env;
  658. uint32_t pir = env->spr_cb[SPR_PIR].default_value;
  659. uint8_t cppr;
  660. if (!xive2_nvp_is_hw(nvp)) {
  661. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: NVP %x/%x is not HW owned\n",
  662. nvp_blk, nvp_idx);
  663. return 0;
  664. }
  665. cppr = xive_get_field32(NVP2_W2_CPPR, nvp->w2);
  666. nvp->w2 = xive_set_field32(NVP2_W2_CPPR, nvp->w2, 0);
  667. xive2_router_write_nvp(xrtr, nvp_blk, nvp_idx, nvp, 2);
  668. tctx->regs[TM_QW1_OS + TM_CPPR] = cppr;
  669. tctx->regs[TM_QW1_OS + TM_LSMFB] = xive_get_field32(NVP2_W2_LSMFB, nvp->w2);
  670. tctx->regs[TM_QW1_OS + TM_LGS] = xive_get_field32(NVP2_W2_LGS, nvp->w2);
  671. tctx->regs[TM_QW1_OS + TM_T] = xive_get_field32(NVP2_W2_T, nvp->w2);
  672. nvp->w1 = xive_set_field32(NVP2_W1_CO, nvp->w1, 1);
  673. nvp->w1 = xive_set_field32(NVP2_W1_CO_THRID_VALID, nvp->w1, 1);
  674. nvp->w1 = xive_set_field32(NVP2_W1_CO_THRID, nvp->w1, pir);
  675. /*
  676. * Checkout privilege: 0:OS, 1:Pool, 2:Hard
  677. *
  678. * TODO: we only support OS push/pull
  679. */
  680. nvp->w1 = xive_set_field32(NVP2_W1_CO_PRIV, nvp->w1, 0);
  681. xive2_router_write_nvp(xrtr, nvp_blk, nvp_idx, nvp, 1);
  682. /* return restored CPPR to generate a CPU exception if needed */
  683. return cppr;
  684. }
  685. static void xive2_tctx_need_resend(Xive2Router *xrtr, XiveTCTX *tctx,
  686. uint8_t nvp_blk, uint32_t nvp_idx,
  687. bool do_restore)
  688. {
  689. XivePresenter *xptr = XIVE_PRESENTER(xrtr);
  690. uint8_t ipb;
  691. uint8_t backlog_level;
  692. uint8_t group_level;
  693. uint8_t first_group;
  694. uint8_t backlog_prio;
  695. uint8_t group_prio;
  696. uint8_t *regs = &tctx->regs[TM_QW1_OS];
  697. Xive2Nvp nvp;
  698. /*
  699. * Grab the associated thread interrupt context registers in the
  700. * associated NVP
  701. */
  702. if (xive2_router_get_nvp(xrtr, nvp_blk, nvp_idx, &nvp)) {
  703. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No NVP %x/%x\n",
  704. nvp_blk, nvp_idx);
  705. return;
  706. }
  707. if (!xive2_nvp_is_valid(&nvp)) {
  708. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid NVP %x/%x\n",
  709. nvp_blk, nvp_idx);
  710. return;
  711. }
  712. /* Automatically restore thread context registers */
  713. if (xive2_router_get_config(xrtr) & XIVE2_VP_SAVE_RESTORE &&
  714. do_restore) {
  715. xive2_tctx_restore_os_ctx(xrtr, tctx, nvp_blk, nvp_idx, &nvp);
  716. }
  717. ipb = xive_get_field32(NVP2_W2_IPB, nvp.w2);
  718. if (ipb) {
  719. nvp.w2 = xive_set_field32(NVP2_W2_IPB, nvp.w2, 0);
  720. xive2_router_write_nvp(xrtr, nvp_blk, nvp_idx, &nvp, 2);
  721. }
  722. regs[TM_IPB] |= ipb;
  723. backlog_prio = xive_ipb_to_pipr(ipb);
  724. backlog_level = 0;
  725. first_group = xive_get_field32(NVP2_W0_PGOFIRST, nvp.w0);
  726. if (first_group && regs[TM_LSMFB] < backlog_prio) {
  727. group_prio = xive2_presenter_backlog_scan(xptr, nvp_blk, nvp_idx,
  728. first_group, &group_level);
  729. regs[TM_LSMFB] = group_prio;
  730. if (regs[TM_LGS] && group_prio < backlog_prio) {
  731. /* VP can take a group interrupt */
  732. xive2_presenter_backlog_decr(xptr, nvp_blk, nvp_idx,
  733. group_prio, group_level);
  734. backlog_prio = group_prio;
  735. backlog_level = group_level;
  736. }
  737. }
  738. /*
  739. * Compute the PIPR based on the restored state.
  740. * It will raise the External interrupt signal if needed.
  741. */
  742. xive_tctx_pipr_update(tctx, TM_QW1_OS, backlog_prio, backlog_level);
  743. }
  744. /*
  745. * Updating the OS CAM line can trigger a resend of interrupt
  746. */
  747. void xive2_tm_push_os_ctx(XivePresenter *xptr, XiveTCTX *tctx,
  748. hwaddr offset, uint64_t value, unsigned size)
  749. {
  750. uint32_t cam;
  751. uint32_t qw1w2;
  752. uint64_t qw1dw1;
  753. uint8_t nvp_blk;
  754. uint32_t nvp_idx;
  755. bool vo;
  756. bool do_restore;
  757. /* First update the thead context */
  758. switch (size) {
  759. case 4:
  760. cam = value;
  761. qw1w2 = cpu_to_be32(cam);
  762. memcpy(&tctx->regs[TM_QW1_OS + TM_WORD2], &qw1w2, 4);
  763. break;
  764. case 8:
  765. cam = value >> 32;
  766. qw1dw1 = cpu_to_be64(value);
  767. memcpy(&tctx->regs[TM_QW1_OS + TM_WORD2], &qw1dw1, 8);
  768. break;
  769. default:
  770. g_assert_not_reached();
  771. }
  772. xive2_cam_decode(cam, &nvp_blk, &nvp_idx, &vo, &do_restore);
  773. /* Check the interrupt pending bits */
  774. if (vo) {
  775. xive2_tctx_need_resend(XIVE2_ROUTER(xptr), tctx, nvp_blk, nvp_idx,
  776. do_restore);
  777. }
  778. }
  779. static int xive2_tctx_get_nvp_indexes(XiveTCTX *tctx, uint8_t ring,
  780. uint32_t *nvp_blk, uint32_t *nvp_idx)
  781. {
  782. uint32_t w2, cam;
  783. w2 = xive_tctx_word2(&tctx->regs[ring]);
  784. switch (ring) {
  785. case TM_QW1_OS:
  786. if (!(be32_to_cpu(w2) & TM2_QW1W2_VO)) {
  787. return -1;
  788. }
  789. cam = xive_get_field32(TM2_QW1W2_OS_CAM, w2);
  790. break;
  791. case TM_QW2_HV_POOL:
  792. if (!(be32_to_cpu(w2) & TM2_QW2W2_VP)) {
  793. return -1;
  794. }
  795. cam = xive_get_field32(TM2_QW2W2_POOL_CAM, w2);
  796. break;
  797. case TM_QW3_HV_PHYS:
  798. if (!(be32_to_cpu(w2) & TM2_QW3W2_VT)) {
  799. return -1;
  800. }
  801. cam = xive2_tctx_hw_cam_line(tctx->xptr, tctx);
  802. break;
  803. default:
  804. return -1;
  805. }
  806. *nvp_blk = xive2_nvp_blk(cam);
  807. *nvp_idx = xive2_nvp_idx(cam);
  808. return 0;
  809. }
  810. static void xive2_tctx_set_cppr(XiveTCTX *tctx, uint8_t ring, uint8_t cppr)
  811. {
  812. uint8_t *regs = &tctx->regs[ring];
  813. Xive2Router *xrtr = XIVE2_ROUTER(tctx->xptr);
  814. uint8_t old_cppr, backlog_prio, first_group, group_level = 0;
  815. uint8_t pipr_min, lsmfb_min, ring_min;
  816. bool group_enabled;
  817. uint32_t nvp_blk, nvp_idx;
  818. Xive2Nvp nvp;
  819. int rc;
  820. trace_xive_tctx_set_cppr(tctx->cs->cpu_index, ring,
  821. regs[TM_IPB], regs[TM_PIPR],
  822. cppr, regs[TM_NSR]);
  823. if (cppr > XIVE_PRIORITY_MAX) {
  824. cppr = 0xff;
  825. }
  826. old_cppr = regs[TM_CPPR];
  827. regs[TM_CPPR] = cppr;
  828. /*
  829. * Recompute the PIPR based on local pending interrupts. It will
  830. * be adjusted below if needed in case of pending group interrupts.
  831. */
  832. pipr_min = xive_ipb_to_pipr(regs[TM_IPB]);
  833. group_enabled = !!regs[TM_LGS];
  834. lsmfb_min = (group_enabled) ? regs[TM_LSMFB] : 0xff;
  835. ring_min = ring;
  836. /* PHYS updates also depend on POOL values */
  837. if (ring == TM_QW3_HV_PHYS) {
  838. uint8_t *pregs = &tctx->regs[TM_QW2_HV_POOL];
  839. /* POOL values only matter if POOL ctx is valid */
  840. if (pregs[TM_WORD2] & 0x80) {
  841. uint8_t pool_pipr = xive_ipb_to_pipr(pregs[TM_IPB]);
  842. uint8_t pool_lsmfb = pregs[TM_LSMFB];
  843. /*
  844. * Determine highest priority interrupt and
  845. * remember which ring has it.
  846. */
  847. if (pool_pipr < pipr_min) {
  848. pipr_min = pool_pipr;
  849. if (pool_pipr < lsmfb_min) {
  850. ring_min = TM_QW2_HV_POOL;
  851. }
  852. }
  853. /* Values needed for group priority calculation */
  854. if (pregs[TM_LGS] && (pool_lsmfb < lsmfb_min)) {
  855. group_enabled = true;
  856. lsmfb_min = pool_lsmfb;
  857. if (lsmfb_min < pipr_min) {
  858. ring_min = TM_QW2_HV_POOL;
  859. }
  860. }
  861. }
  862. }
  863. regs[TM_PIPR] = pipr_min;
  864. rc = xive2_tctx_get_nvp_indexes(tctx, ring_min, &nvp_blk, &nvp_idx);
  865. if (rc) {
  866. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: set CPPR on invalid context\n");
  867. return;
  868. }
  869. if (cppr < old_cppr) {
  870. /*
  871. * FIXME: check if there's a group interrupt being presented
  872. * and if the new cppr prevents it. If so, then the group
  873. * interrupt needs to be re-added to the backlog and
  874. * re-triggered (see re-trigger END info in the NVGC
  875. * structure)
  876. */
  877. }
  878. if (group_enabled &&
  879. lsmfb_min < cppr &&
  880. lsmfb_min < regs[TM_PIPR]) {
  881. /*
  882. * Thread has seen a group interrupt with a higher priority
  883. * than the new cppr or pending local interrupt. Check the
  884. * backlog
  885. */
  886. if (xive2_router_get_nvp(xrtr, nvp_blk, nvp_idx, &nvp)) {
  887. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No NVP %x/%x\n",
  888. nvp_blk, nvp_idx);
  889. return;
  890. }
  891. if (!xive2_nvp_is_valid(&nvp)) {
  892. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid NVP %x/%x\n",
  893. nvp_blk, nvp_idx);
  894. return;
  895. }
  896. first_group = xive_get_field32(NVP2_W0_PGOFIRST, nvp.w0);
  897. if (!first_group) {
  898. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid NVP %x/%x\n",
  899. nvp_blk, nvp_idx);
  900. return;
  901. }
  902. backlog_prio = xive2_presenter_backlog_scan(tctx->xptr,
  903. nvp_blk, nvp_idx,
  904. first_group, &group_level);
  905. tctx->regs[ring_min + TM_LSMFB] = backlog_prio;
  906. if (backlog_prio != 0xFF) {
  907. xive2_presenter_backlog_decr(tctx->xptr, nvp_blk, nvp_idx,
  908. backlog_prio, group_level);
  909. regs[TM_PIPR] = backlog_prio;
  910. }
  911. }
  912. /* CPPR has changed, check if we need to raise a pending exception */
  913. xive_tctx_notify(tctx, ring_min, group_level);
  914. }
  915. void xive2_tm_set_hv_cppr(XivePresenter *xptr, XiveTCTX *tctx,
  916. hwaddr offset, uint64_t value, unsigned size)
  917. {
  918. xive2_tctx_set_cppr(tctx, TM_QW3_HV_PHYS, value & 0xff);
  919. }
  920. void xive2_tm_set_os_cppr(XivePresenter *xptr, XiveTCTX *tctx,
  921. hwaddr offset, uint64_t value, unsigned size)
  922. {
  923. xive2_tctx_set_cppr(tctx, TM_QW1_OS, value & 0xff);
  924. }
  925. static void xive2_tctx_set_target(XiveTCTX *tctx, uint8_t ring, uint8_t target)
  926. {
  927. uint8_t *regs = &tctx->regs[ring];
  928. regs[TM_T] = target;
  929. }
  930. void xive2_tm_set_hv_target(XivePresenter *xptr, XiveTCTX *tctx,
  931. hwaddr offset, uint64_t value, unsigned size)
  932. {
  933. xive2_tctx_set_target(tctx, TM_QW3_HV_PHYS, value & 0xff);
  934. }
  935. /*
  936. * XIVE Router (aka. Virtualization Controller or IVRE)
  937. */
  938. int xive2_router_get_eas(Xive2Router *xrtr, uint8_t eas_blk, uint32_t eas_idx,
  939. Xive2Eas *eas)
  940. {
  941. Xive2RouterClass *xrc = XIVE2_ROUTER_GET_CLASS(xrtr);
  942. return xrc->get_eas(xrtr, eas_blk, eas_idx, eas);
  943. }
  944. static
  945. int xive2_router_get_pq(Xive2Router *xrtr, uint8_t eas_blk, uint32_t eas_idx,
  946. uint8_t *pq)
  947. {
  948. Xive2RouterClass *xrc = XIVE2_ROUTER_GET_CLASS(xrtr);
  949. return xrc->get_pq(xrtr, eas_blk, eas_idx, pq);
  950. }
  951. static
  952. int xive2_router_set_pq(Xive2Router *xrtr, uint8_t eas_blk, uint32_t eas_idx,
  953. uint8_t *pq)
  954. {
  955. Xive2RouterClass *xrc = XIVE2_ROUTER_GET_CLASS(xrtr);
  956. return xrc->set_pq(xrtr, eas_blk, eas_idx, pq);
  957. }
  958. int xive2_router_get_end(Xive2Router *xrtr, uint8_t end_blk, uint32_t end_idx,
  959. Xive2End *end)
  960. {
  961. Xive2RouterClass *xrc = XIVE2_ROUTER_GET_CLASS(xrtr);
  962. return xrc->get_end(xrtr, end_blk, end_idx, end);
  963. }
  964. int xive2_router_write_end(Xive2Router *xrtr, uint8_t end_blk, uint32_t end_idx,
  965. Xive2End *end, uint8_t word_number)
  966. {
  967. Xive2RouterClass *xrc = XIVE2_ROUTER_GET_CLASS(xrtr);
  968. return xrc->write_end(xrtr, end_blk, end_idx, end, word_number);
  969. }
  970. int xive2_router_get_nvp(Xive2Router *xrtr, uint8_t nvp_blk, uint32_t nvp_idx,
  971. Xive2Nvp *nvp)
  972. {
  973. Xive2RouterClass *xrc = XIVE2_ROUTER_GET_CLASS(xrtr);
  974. return xrc->get_nvp(xrtr, nvp_blk, nvp_idx, nvp);
  975. }
  976. int xive2_router_write_nvp(Xive2Router *xrtr, uint8_t nvp_blk, uint32_t nvp_idx,
  977. Xive2Nvp *nvp, uint8_t word_number)
  978. {
  979. Xive2RouterClass *xrc = XIVE2_ROUTER_GET_CLASS(xrtr);
  980. return xrc->write_nvp(xrtr, nvp_blk, nvp_idx, nvp, word_number);
  981. }
  982. int xive2_router_get_nvgc(Xive2Router *xrtr, bool crowd,
  983. uint8_t nvgc_blk, uint32_t nvgc_idx,
  984. Xive2Nvgc *nvgc)
  985. {
  986. Xive2RouterClass *xrc = XIVE2_ROUTER_GET_CLASS(xrtr);
  987. return xrc->get_nvgc(xrtr, crowd, nvgc_blk, nvgc_idx, nvgc);
  988. }
  989. int xive2_router_write_nvgc(Xive2Router *xrtr, bool crowd,
  990. uint8_t nvgc_blk, uint32_t nvgc_idx,
  991. Xive2Nvgc *nvgc)
  992. {
  993. Xive2RouterClass *xrc = XIVE2_ROUTER_GET_CLASS(xrtr);
  994. return xrc->write_nvgc(xrtr, crowd, nvgc_blk, nvgc_idx, nvgc);
  995. }
  996. static bool xive2_vp_match_mask(uint32_t cam1, uint32_t cam2,
  997. uint32_t vp_mask)
  998. {
  999. return (cam1 & vp_mask) == (cam2 & vp_mask);
  1000. }
  1001. static uint8_t xive2_get_vp_block_mask(uint32_t nvt_blk, bool crowd)
  1002. {
  1003. uint8_t block_mask = 0b1111;
  1004. /* 3 supported crowd sizes: 2, 4, 16 */
  1005. if (crowd) {
  1006. uint32_t size = xive_get_vpgroup_size(nvt_blk);
  1007. if (size != 2 && size != 4 && size != 16) {
  1008. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid crowd size of %d",
  1009. size);
  1010. return block_mask;
  1011. }
  1012. block_mask &= ~(size - 1);
  1013. }
  1014. return block_mask;
  1015. }
  1016. static uint32_t xive2_get_vp_index_mask(uint32_t nvt_index, bool cam_ignore)
  1017. {
  1018. uint32_t index_mask = 0xFFFFFF; /* 24 bits */
  1019. if (cam_ignore) {
  1020. uint32_t size = xive_get_vpgroup_size(nvt_index);
  1021. if (size < 2) {
  1022. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid group size of %d",
  1023. size);
  1024. return index_mask;
  1025. }
  1026. index_mask &= ~(size - 1);
  1027. }
  1028. return index_mask;
  1029. }
  1030. /*
  1031. * The thread context register words are in big-endian format.
  1032. */
  1033. int xive2_presenter_tctx_match(XivePresenter *xptr, XiveTCTX *tctx,
  1034. uint8_t format,
  1035. uint8_t nvt_blk, uint32_t nvt_idx,
  1036. bool crowd, bool cam_ignore,
  1037. uint32_t logic_serv)
  1038. {
  1039. uint32_t cam = xive2_nvp_cam_line(nvt_blk, nvt_idx);
  1040. uint32_t qw3w2 = xive_tctx_word2(&tctx->regs[TM_QW3_HV_PHYS]);
  1041. uint32_t qw2w2 = xive_tctx_word2(&tctx->regs[TM_QW2_HV_POOL]);
  1042. uint32_t qw1w2 = xive_tctx_word2(&tctx->regs[TM_QW1_OS]);
  1043. uint32_t qw0w2 = xive_tctx_word2(&tctx->regs[TM_QW0_USER]);
  1044. uint32_t index_mask, vp_mask;
  1045. uint8_t block_mask;
  1046. if (format == 0) {
  1047. /*
  1048. * i=0: Specific NVT notification
  1049. * i=1: VP-group notification (bits ignored at the end of the
  1050. * NVT identifier)
  1051. */
  1052. block_mask = xive2_get_vp_block_mask(nvt_blk, crowd);
  1053. index_mask = xive2_get_vp_index_mask(nvt_idx, cam_ignore);
  1054. vp_mask = xive2_nvp_cam_line(block_mask, index_mask);
  1055. /* For VP-group notifications, threads with LGS=0 are excluded */
  1056. /* PHYS ring */
  1057. if ((be32_to_cpu(qw3w2) & TM2_QW3W2_VT) &&
  1058. !(cam_ignore && tctx->regs[TM_QW3_HV_PHYS + TM_LGS] == 0) &&
  1059. xive2_vp_match_mask(cam,
  1060. xive2_tctx_hw_cam_line(xptr, tctx),
  1061. vp_mask)) {
  1062. return TM_QW3_HV_PHYS;
  1063. }
  1064. /* HV POOL ring */
  1065. if ((be32_to_cpu(qw2w2) & TM2_QW2W2_VP) &&
  1066. !(cam_ignore && tctx->regs[TM_QW2_HV_POOL + TM_LGS] == 0) &&
  1067. xive2_vp_match_mask(cam,
  1068. xive_get_field32(TM2_QW2W2_POOL_CAM, qw2w2),
  1069. vp_mask)) {
  1070. return TM_QW2_HV_POOL;
  1071. }
  1072. /* OS ring */
  1073. if ((be32_to_cpu(qw1w2) & TM2_QW1W2_VO) &&
  1074. !(cam_ignore && tctx->regs[TM_QW1_OS + TM_LGS] == 0) &&
  1075. xive2_vp_match_mask(cam,
  1076. xive_get_field32(TM2_QW1W2_OS_CAM, qw1w2),
  1077. vp_mask)) {
  1078. return TM_QW1_OS;
  1079. }
  1080. } else {
  1081. /* F=1 : User level Event-Based Branch (EBB) notification */
  1082. /* FIXME: what if cam_ignore and LGS = 0 ? */
  1083. /* USER ring */
  1084. if ((be32_to_cpu(qw1w2) & TM2_QW1W2_VO) &&
  1085. (cam == xive_get_field32(TM2_QW1W2_OS_CAM, qw1w2)) &&
  1086. (be32_to_cpu(qw0w2) & TM2_QW0W2_VU) &&
  1087. (logic_serv == xive_get_field32(TM2_QW0W2_LOGIC_SERV, qw0w2))) {
  1088. return TM_QW0_USER;
  1089. }
  1090. }
  1091. return -1;
  1092. }
  1093. bool xive2_tm_irq_precluded(XiveTCTX *tctx, int ring, uint8_t priority)
  1094. {
  1095. /* HV_POOL ring uses HV_PHYS NSR, CPPR and PIPR registers */
  1096. uint8_t alt_ring = (ring == TM_QW2_HV_POOL) ? TM_QW3_HV_PHYS : ring;
  1097. uint8_t *alt_regs = &tctx->regs[alt_ring];
  1098. /*
  1099. * The xive2_presenter_tctx_match() above tells if there's a match
  1100. * but for VP-group notification, we still need to look at the
  1101. * priority to know if the thread can take the interrupt now or if
  1102. * it is precluded.
  1103. */
  1104. if (priority < alt_regs[TM_CPPR]) {
  1105. return false;
  1106. }
  1107. return true;
  1108. }
  1109. void xive2_tm_set_lsmfb(XiveTCTX *tctx, int ring, uint8_t priority)
  1110. {
  1111. uint8_t *regs = &tctx->regs[ring];
  1112. /*
  1113. * Called by the router during a VP-group notification when the
  1114. * thread matches but can't take the interrupt because it's
  1115. * already running at a more favored priority. It then stores the
  1116. * new interrupt priority in the LSMFB field.
  1117. */
  1118. regs[TM_LSMFB] = priority;
  1119. }
  1120. static void xive2_router_realize(DeviceState *dev, Error **errp)
  1121. {
  1122. Xive2Router *xrtr = XIVE2_ROUTER(dev);
  1123. assert(xrtr->xfb);
  1124. }
  1125. /*
  1126. * Notification using the END ESe/ESn bit (Event State Buffer for
  1127. * escalation and notification). Profide further coalescing in the
  1128. * Router.
  1129. */
  1130. static bool xive2_router_end_es_notify(Xive2Router *xrtr, uint8_t end_blk,
  1131. uint32_t end_idx, Xive2End *end,
  1132. uint32_t end_esmask)
  1133. {
  1134. uint8_t pq = xive_get_field32(end_esmask, end->w1);
  1135. bool notify = xive_esb_trigger(&pq);
  1136. if (pq != xive_get_field32(end_esmask, end->w1)) {
  1137. end->w1 = xive_set_field32(end_esmask, end->w1, pq);
  1138. xive2_router_write_end(xrtr, end_blk, end_idx, end, 1);
  1139. }
  1140. /* ESe/n[Q]=1 : end of notification */
  1141. return notify;
  1142. }
  1143. /*
  1144. * An END trigger can come from an event trigger (IPI or HW) or from
  1145. * another chip. We don't model the PowerBus but the END trigger
  1146. * message has the same parameters than in the function below.
  1147. */
  1148. static void xive2_router_end_notify(Xive2Router *xrtr, uint8_t end_blk,
  1149. uint32_t end_idx, uint32_t end_data)
  1150. {
  1151. Xive2End end;
  1152. uint8_t priority;
  1153. uint8_t format;
  1154. bool found, precluded;
  1155. uint8_t nvx_blk;
  1156. uint32_t nvx_idx;
  1157. /* END cache lookup */
  1158. if (xive2_router_get_end(xrtr, end_blk, end_idx, &end)) {
  1159. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No END %x/%x\n", end_blk,
  1160. end_idx);
  1161. return;
  1162. }
  1163. if (!xive2_end_is_valid(&end)) {
  1164. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: END %x/%x is invalid\n",
  1165. end_blk, end_idx);
  1166. return;
  1167. }
  1168. if (xive2_end_is_crowd(&end) && !xive2_end_is_ignore(&end)) {
  1169. qemu_log_mask(LOG_GUEST_ERROR,
  1170. "XIVE: invalid END, 'crowd' bit requires 'ignore' bit\n");
  1171. return;
  1172. }
  1173. if (xive2_end_is_enqueue(&end)) {
  1174. xive2_end_enqueue(&end, end_data);
  1175. /* Enqueuing event data modifies the EQ toggle and index */
  1176. xive2_router_write_end(xrtr, end_blk, end_idx, &end, 1);
  1177. }
  1178. /*
  1179. * When the END is silent, we skip the notification part.
  1180. */
  1181. if (xive2_end_is_silent_escalation(&end)) {
  1182. goto do_escalation;
  1183. }
  1184. /*
  1185. * The W7 format depends on the F bit in W6. It defines the type
  1186. * of the notification :
  1187. *
  1188. * F=0 : single or multiple NVP notification
  1189. * F=1 : User level Event-Based Branch (EBB) notification, no
  1190. * priority
  1191. */
  1192. format = xive_get_field32(END2_W6_FORMAT_BIT, end.w6);
  1193. priority = xive_get_field32(END2_W7_F0_PRIORITY, end.w7);
  1194. /* The END is masked */
  1195. if (format == 0 && priority == 0xff) {
  1196. return;
  1197. }
  1198. /*
  1199. * Check the END ESn (Event State Buffer for notification) for
  1200. * even further coalescing in the Router
  1201. */
  1202. if (!xive2_end_is_notify(&end)) {
  1203. /* ESn[Q]=1 : end of notification */
  1204. if (!xive2_router_end_es_notify(xrtr, end_blk, end_idx,
  1205. &end, END2_W1_ESn)) {
  1206. return;
  1207. }
  1208. }
  1209. /*
  1210. * Follows IVPE notification
  1211. */
  1212. nvx_blk = xive_get_field32(END2_W6_VP_BLOCK, end.w6);
  1213. nvx_idx = xive_get_field32(END2_W6_VP_OFFSET, end.w6);
  1214. found = xive_presenter_notify(xrtr->xfb, format, nvx_blk, nvx_idx,
  1215. xive2_end_is_crowd(&end), xive2_end_is_ignore(&end),
  1216. priority,
  1217. xive_get_field32(END2_W7_F1_LOG_SERVER_ID, end.w7),
  1218. &precluded);
  1219. /* TODO: Auto EOI. */
  1220. if (found) {
  1221. return;
  1222. }
  1223. /*
  1224. * If no matching NVP is dispatched on a HW thread :
  1225. * - specific VP: update the NVP structure if backlog is activated
  1226. * - VP-group: update the backlog counter for that priority in the NVG
  1227. */
  1228. if (xive2_end_is_backlog(&end)) {
  1229. if (format == 1) {
  1230. qemu_log_mask(LOG_GUEST_ERROR,
  1231. "XIVE: END %x/%x invalid config: F1 & backlog\n",
  1232. end_blk, end_idx);
  1233. return;
  1234. }
  1235. if (!xive2_end_is_ignore(&end)) {
  1236. uint8_t ipb;
  1237. Xive2Nvp nvp;
  1238. /* NVP cache lookup */
  1239. if (xive2_router_get_nvp(xrtr, nvx_blk, nvx_idx, &nvp)) {
  1240. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: no NVP %x/%x\n",
  1241. nvx_blk, nvx_idx);
  1242. return;
  1243. }
  1244. if (!xive2_nvp_is_valid(&nvp)) {
  1245. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: NVP %x/%x is invalid\n",
  1246. nvx_blk, nvx_idx);
  1247. return;
  1248. }
  1249. /*
  1250. * Record the IPB in the associated NVP structure for later
  1251. * use. The presenter will resend the interrupt when the vCPU
  1252. * is dispatched again on a HW thread.
  1253. */
  1254. ipb = xive_get_field32(NVP2_W2_IPB, nvp.w2) |
  1255. xive_priority_to_ipb(priority);
  1256. nvp.w2 = xive_set_field32(NVP2_W2_IPB, nvp.w2, ipb);
  1257. xive2_router_write_nvp(xrtr, nvx_blk, nvx_idx, &nvp, 2);
  1258. } else {
  1259. Xive2Nvgc nvgc;
  1260. uint32_t backlog;
  1261. bool crowd;
  1262. crowd = xive2_end_is_crowd(&end);
  1263. /*
  1264. * For groups and crowds, the per-priority backlog
  1265. * counters are stored in the NVG/NVC structures
  1266. */
  1267. if (xive2_router_get_nvgc(xrtr, crowd,
  1268. nvx_blk, nvx_idx, &nvgc)) {
  1269. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: no %s %x/%x\n",
  1270. crowd ? "NVC" : "NVG", nvx_blk, nvx_idx);
  1271. return;
  1272. }
  1273. if (!xive2_nvgc_is_valid(&nvgc)) {
  1274. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: NVG %x/%x is invalid\n",
  1275. nvx_blk, nvx_idx);
  1276. return;
  1277. }
  1278. /*
  1279. * Increment the backlog counter for that priority.
  1280. * We only call broadcast the first time the counter is
  1281. * incremented. broadcast will set the LSMFB field of the TIMA of
  1282. * relevant threads so that they know an interrupt is pending.
  1283. */
  1284. backlog = xive2_nvgc_get_backlog(&nvgc, priority) + 1;
  1285. xive2_nvgc_set_backlog(&nvgc, priority, backlog);
  1286. xive2_router_write_nvgc(xrtr, crowd, nvx_blk, nvx_idx, &nvgc);
  1287. if (backlog == 1) {
  1288. XiveFabricClass *xfc = XIVE_FABRIC_GET_CLASS(xrtr->xfb);
  1289. xfc->broadcast(xrtr->xfb, nvx_blk, nvx_idx,
  1290. xive2_end_is_crowd(&end),
  1291. xive2_end_is_ignore(&end),
  1292. priority);
  1293. if (!xive2_end_is_precluded_escalation(&end)) {
  1294. /*
  1295. * The interrupt will be picked up when the
  1296. * matching thread lowers its priority level
  1297. */
  1298. return;
  1299. }
  1300. }
  1301. }
  1302. }
  1303. do_escalation:
  1304. /*
  1305. * If activated, escalate notification using the ESe PQ bits and
  1306. * the EAS in w4-5
  1307. */
  1308. if (!xive2_end_is_escalate(&end)) {
  1309. return;
  1310. }
  1311. /*
  1312. * Check the END ESe (Event State Buffer for escalation) for even
  1313. * further coalescing in the Router
  1314. */
  1315. if (!xive2_end_is_uncond_escalation(&end)) {
  1316. /* ESe[Q]=1 : end of escalation notification */
  1317. if (!xive2_router_end_es_notify(xrtr, end_blk, end_idx,
  1318. &end, END2_W1_ESe)) {
  1319. return;
  1320. }
  1321. }
  1322. /*
  1323. * The END trigger becomes an Escalation trigger
  1324. */
  1325. xive2_router_end_notify(xrtr,
  1326. xive_get_field32(END2_W4_END_BLOCK, end.w4),
  1327. xive_get_field32(END2_W4_ESC_END_INDEX, end.w4),
  1328. xive_get_field32(END2_W5_ESC_END_DATA, end.w5));
  1329. }
  1330. void xive2_router_notify(XiveNotifier *xn, uint32_t lisn, bool pq_checked)
  1331. {
  1332. Xive2Router *xrtr = XIVE2_ROUTER(xn);
  1333. uint8_t eas_blk = XIVE_EAS_BLOCK(lisn);
  1334. uint32_t eas_idx = XIVE_EAS_INDEX(lisn);
  1335. Xive2Eas eas;
  1336. /* EAS cache lookup */
  1337. if (xive2_router_get_eas(xrtr, eas_blk, eas_idx, &eas)) {
  1338. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Unknown LISN %x\n", lisn);
  1339. return;
  1340. }
  1341. if (!pq_checked) {
  1342. bool notify;
  1343. uint8_t pq;
  1344. /* PQ cache lookup */
  1345. if (xive2_router_get_pq(xrtr, eas_blk, eas_idx, &pq)) {
  1346. /* Set FIR */
  1347. g_assert_not_reached();
  1348. }
  1349. notify = xive_esb_trigger(&pq);
  1350. if (xive2_router_set_pq(xrtr, eas_blk, eas_idx, &pq)) {
  1351. /* Set FIR */
  1352. g_assert_not_reached();
  1353. }
  1354. if (!notify) {
  1355. return;
  1356. }
  1357. }
  1358. if (!xive2_eas_is_valid(&eas)) {
  1359. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: Invalid LISN %x\n", lisn);
  1360. return;
  1361. }
  1362. if (xive2_eas_is_masked(&eas)) {
  1363. /* Notification completed */
  1364. return;
  1365. }
  1366. /*
  1367. * The event trigger becomes an END trigger
  1368. */
  1369. xive2_router_end_notify(xrtr,
  1370. xive_get_field64(EAS2_END_BLOCK, eas.w),
  1371. xive_get_field64(EAS2_END_INDEX, eas.w),
  1372. xive_get_field64(EAS2_END_DATA, eas.w));
  1373. }
  1374. static const Property xive2_router_properties[] = {
  1375. DEFINE_PROP_LINK("xive-fabric", Xive2Router, xfb,
  1376. TYPE_XIVE_FABRIC, XiveFabric *),
  1377. };
  1378. static void xive2_router_class_init(ObjectClass *klass, void *data)
  1379. {
  1380. DeviceClass *dc = DEVICE_CLASS(klass);
  1381. XiveNotifierClass *xnc = XIVE_NOTIFIER_CLASS(klass);
  1382. dc->desc = "XIVE2 Router Engine";
  1383. device_class_set_props(dc, xive2_router_properties);
  1384. /* Parent is SysBusDeviceClass. No need to call its realize hook */
  1385. dc->realize = xive2_router_realize;
  1386. xnc->notify = xive2_router_notify;
  1387. }
  1388. static const TypeInfo xive2_router_info = {
  1389. .name = TYPE_XIVE2_ROUTER,
  1390. .parent = TYPE_SYS_BUS_DEVICE,
  1391. .abstract = true,
  1392. .instance_size = sizeof(Xive2Router),
  1393. .class_size = sizeof(Xive2RouterClass),
  1394. .class_init = xive2_router_class_init,
  1395. .interfaces = (InterfaceInfo[]) {
  1396. { TYPE_XIVE_NOTIFIER },
  1397. { TYPE_XIVE_PRESENTER },
  1398. { }
  1399. }
  1400. };
  1401. static inline bool addr_is_even(hwaddr addr, uint32_t shift)
  1402. {
  1403. return !((addr >> shift) & 1);
  1404. }
  1405. static uint64_t xive2_end_source_read(void *opaque, hwaddr addr, unsigned size)
  1406. {
  1407. Xive2EndSource *xsrc = XIVE2_END_SOURCE(opaque);
  1408. uint32_t offset = addr & 0xFFF;
  1409. uint8_t end_blk;
  1410. uint32_t end_idx;
  1411. Xive2End end;
  1412. uint32_t end_esmask;
  1413. uint8_t pq;
  1414. uint64_t ret;
  1415. /*
  1416. * The block id should be deduced from the load address on the END
  1417. * ESB MMIO but our model only supports a single block per XIVE chip.
  1418. */
  1419. end_blk = xive2_router_get_block_id(xsrc->xrtr);
  1420. end_idx = addr >> (xsrc->esb_shift + 1);
  1421. if (xive2_router_get_end(xsrc->xrtr, end_blk, end_idx, &end)) {
  1422. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No END %x/%x\n", end_blk,
  1423. end_idx);
  1424. return -1;
  1425. }
  1426. if (!xive2_end_is_valid(&end)) {
  1427. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: END %x/%x is invalid\n",
  1428. end_blk, end_idx);
  1429. return -1;
  1430. }
  1431. end_esmask = addr_is_even(addr, xsrc->esb_shift) ? END2_W1_ESn :
  1432. END2_W1_ESe;
  1433. pq = xive_get_field32(end_esmask, end.w1);
  1434. switch (offset) {
  1435. case XIVE_ESB_LOAD_EOI ... XIVE_ESB_LOAD_EOI + 0x7FF:
  1436. ret = xive_esb_eoi(&pq);
  1437. /* Forward the source event notification for routing ?? */
  1438. break;
  1439. case XIVE_ESB_GET ... XIVE_ESB_GET + 0x3FF:
  1440. ret = pq;
  1441. break;
  1442. case XIVE_ESB_SET_PQ_00 ... XIVE_ESB_SET_PQ_00 + 0x0FF:
  1443. case XIVE_ESB_SET_PQ_01 ... XIVE_ESB_SET_PQ_01 + 0x0FF:
  1444. case XIVE_ESB_SET_PQ_10 ... XIVE_ESB_SET_PQ_10 + 0x0FF:
  1445. case XIVE_ESB_SET_PQ_11 ... XIVE_ESB_SET_PQ_11 + 0x0FF:
  1446. ret = xive_esb_set(&pq, (offset >> 8) & 0x3);
  1447. break;
  1448. default:
  1449. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid END ESB load addr %d\n",
  1450. offset);
  1451. return -1;
  1452. }
  1453. if (pq != xive_get_field32(end_esmask, end.w1)) {
  1454. end.w1 = xive_set_field32(end_esmask, end.w1, pq);
  1455. xive2_router_write_end(xsrc->xrtr, end_blk, end_idx, &end, 1);
  1456. }
  1457. return ret;
  1458. }
  1459. static void xive2_end_source_write(void *opaque, hwaddr addr,
  1460. uint64_t value, unsigned size)
  1461. {
  1462. Xive2EndSource *xsrc = XIVE2_END_SOURCE(opaque);
  1463. uint32_t offset = addr & 0xFFF;
  1464. uint8_t end_blk;
  1465. uint32_t end_idx;
  1466. Xive2End end;
  1467. uint32_t end_esmask;
  1468. uint8_t pq;
  1469. bool notify = false;
  1470. /*
  1471. * The block id should be deduced from the load address on the END
  1472. * ESB MMIO but our model only supports a single block per XIVE chip.
  1473. */
  1474. end_blk = xive2_router_get_block_id(xsrc->xrtr);
  1475. end_idx = addr >> (xsrc->esb_shift + 1);
  1476. if (xive2_router_get_end(xsrc->xrtr, end_blk, end_idx, &end)) {
  1477. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: No END %x/%x\n", end_blk,
  1478. end_idx);
  1479. return;
  1480. }
  1481. if (!xive2_end_is_valid(&end)) {
  1482. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: END %x/%x is invalid\n",
  1483. end_blk, end_idx);
  1484. return;
  1485. }
  1486. end_esmask = addr_is_even(addr, xsrc->esb_shift) ? END2_W1_ESn :
  1487. END2_W1_ESe;
  1488. pq = xive_get_field32(end_esmask, end.w1);
  1489. switch (offset) {
  1490. case 0 ... 0x3FF:
  1491. notify = xive_esb_trigger(&pq);
  1492. break;
  1493. case XIVE_ESB_STORE_EOI ... XIVE_ESB_STORE_EOI + 0x3FF:
  1494. /* TODO: can we check StoreEOI availability from the router ? */
  1495. notify = xive_esb_eoi(&pq);
  1496. break;
  1497. case XIVE_ESB_INJECT ... XIVE_ESB_INJECT + 0x3FF:
  1498. if (end_esmask == END2_W1_ESe) {
  1499. qemu_log_mask(LOG_GUEST_ERROR,
  1500. "XIVE: END %x/%x can not EQ inject on ESe\n",
  1501. end_blk, end_idx);
  1502. return;
  1503. }
  1504. notify = true;
  1505. break;
  1506. default:
  1507. qemu_log_mask(LOG_GUEST_ERROR, "XIVE: invalid END ESB write addr %d\n",
  1508. offset);
  1509. return;
  1510. }
  1511. if (pq != xive_get_field32(end_esmask, end.w1)) {
  1512. end.w1 = xive_set_field32(end_esmask, end.w1, pq);
  1513. xive2_router_write_end(xsrc->xrtr, end_blk, end_idx, &end, 1);
  1514. }
  1515. /* TODO: Forward the source event notification for routing */
  1516. if (notify) {
  1517. ;
  1518. }
  1519. }
  1520. static const MemoryRegionOps xive2_end_source_ops = {
  1521. .read = xive2_end_source_read,
  1522. .write = xive2_end_source_write,
  1523. .endianness = DEVICE_BIG_ENDIAN,
  1524. .valid = {
  1525. .min_access_size = 1,
  1526. .max_access_size = 8,
  1527. },
  1528. .impl = {
  1529. .min_access_size = 1,
  1530. .max_access_size = 8,
  1531. },
  1532. };
  1533. static void xive2_end_source_realize(DeviceState *dev, Error **errp)
  1534. {
  1535. Xive2EndSource *xsrc = XIVE2_END_SOURCE(dev);
  1536. assert(xsrc->xrtr);
  1537. if (!xsrc->nr_ends) {
  1538. error_setg(errp, "Number of interrupt needs to be greater than 0");
  1539. return;
  1540. }
  1541. if (xsrc->esb_shift != XIVE_ESB_4K &&
  1542. xsrc->esb_shift != XIVE_ESB_64K) {
  1543. error_setg(errp, "Invalid ESB shift setting");
  1544. return;
  1545. }
  1546. /*
  1547. * Each END is assigned an even/odd pair of MMIO pages, the even page
  1548. * manages the ESn field while the odd page manages the ESe field.
  1549. */
  1550. memory_region_init_io(&xsrc->esb_mmio, OBJECT(xsrc),
  1551. &xive2_end_source_ops, xsrc, "xive.end",
  1552. (1ull << (xsrc->esb_shift + 1)) * xsrc->nr_ends);
  1553. }
  1554. static const Property xive2_end_source_properties[] = {
  1555. DEFINE_PROP_UINT32("nr-ends", Xive2EndSource, nr_ends, 0),
  1556. DEFINE_PROP_UINT32("shift", Xive2EndSource, esb_shift, XIVE_ESB_64K),
  1557. DEFINE_PROP_LINK("xive", Xive2EndSource, xrtr, TYPE_XIVE2_ROUTER,
  1558. Xive2Router *),
  1559. };
  1560. static void xive2_end_source_class_init(ObjectClass *klass, void *data)
  1561. {
  1562. DeviceClass *dc = DEVICE_CLASS(klass);
  1563. dc->desc = "XIVE END Source";
  1564. device_class_set_props(dc, xive2_end_source_properties);
  1565. dc->realize = xive2_end_source_realize;
  1566. dc->user_creatable = false;
  1567. }
  1568. static const TypeInfo xive2_end_source_info = {
  1569. .name = TYPE_XIVE2_END_SOURCE,
  1570. .parent = TYPE_DEVICE,
  1571. .instance_size = sizeof(Xive2EndSource),
  1572. .class_init = xive2_end_source_class_init,
  1573. };
  1574. static void xive2_register_types(void)
  1575. {
  1576. type_register_static(&xive2_router_info);
  1577. type_register_static(&xive2_end_source_info);
  1578. }
  1579. type_init(xive2_register_types)