arm_gicv3_redist.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  1. /*
  2. * ARM GICv3 emulation: Redistributor
  3. *
  4. * Copyright (c) 2015 Huawei.
  5. * Copyright (c) 2016 Linaro Limited.
  6. * Written by Shlomo Pongratz, Peter Maydell
  7. *
  8. * This code is licensed under the GPL, version 2 or (at your option)
  9. * any later version.
  10. */
  11. #include "qemu/osdep.h"
  12. #include "qemu/log.h"
  13. #include "trace.h"
  14. #include "gicv3_internal.h"
  15. static uint32_t mask_group(GICv3CPUState *cs, MemTxAttrs attrs)
  16. {
  17. /* Return a 32-bit mask which should be applied for this set of 32
  18. * interrupts; each bit is 1 if access is permitted by the
  19. * combination of attrs.secure and GICR_GROUPR. (GICR_NSACR does
  20. * not affect config register accesses, unlike GICD_NSACR.)
  21. */
  22. if (!attrs.secure && !(cs->gic->gicd_ctlr & GICD_CTLR_DS)) {
  23. /* bits for Group 0 or Secure Group 1 interrupts are RAZ/WI */
  24. return cs->gicr_igroupr0;
  25. }
  26. return 0xFFFFFFFFU;
  27. }
  28. static int gicr_ns_access(GICv3CPUState *cs, int irq)
  29. {
  30. /* Return the 2 bit NSACR.NS_access field for this SGI */
  31. assert(irq < 16);
  32. return extract32(cs->gicr_nsacr, irq * 2, 2);
  33. }
  34. static void gicr_write_bitmap_reg(GICv3CPUState *cs, MemTxAttrs attrs,
  35. uint32_t *reg, uint32_t val)
  36. {
  37. /* Helper routine to implement writing to a "set" register */
  38. val &= mask_group(cs, attrs);
  39. *reg = val;
  40. gicv3_redist_update(cs);
  41. }
  42. static void gicr_write_set_bitmap_reg(GICv3CPUState *cs, MemTxAttrs attrs,
  43. uint32_t *reg, uint32_t val)
  44. {
  45. /* Helper routine to implement writing to a "set-bitmap" register */
  46. val &= mask_group(cs, attrs);
  47. *reg |= val;
  48. gicv3_redist_update(cs);
  49. }
  50. static void gicr_write_clear_bitmap_reg(GICv3CPUState *cs, MemTxAttrs attrs,
  51. uint32_t *reg, uint32_t val)
  52. {
  53. /* Helper routine to implement writing to a "clear-bitmap" register */
  54. val &= mask_group(cs, attrs);
  55. *reg &= ~val;
  56. gicv3_redist_update(cs);
  57. }
  58. static uint32_t gicr_read_bitmap_reg(GICv3CPUState *cs, MemTxAttrs attrs,
  59. uint32_t reg)
  60. {
  61. reg &= mask_group(cs, attrs);
  62. return reg;
  63. }
  64. static bool vcpu_resident(GICv3CPUState *cs, uint64_t vptaddr)
  65. {
  66. /*
  67. * Return true if a vCPU is resident, which is defined by
  68. * whether the GICR_VPENDBASER register is marked VALID and
  69. * has the right virtual pending table address.
  70. */
  71. if (!FIELD_EX64(cs->gicr_vpendbaser, GICR_VPENDBASER, VALID)) {
  72. return false;
  73. }
  74. return vptaddr == (cs->gicr_vpendbaser & R_GICR_VPENDBASER_PHYADDR_MASK);
  75. }
  76. /**
  77. * update_for_one_lpi: Update pending information if this LPI is better
  78. *
  79. * @cs: GICv3CPUState
  80. * @irq: interrupt to look up in the LPI Configuration table
  81. * @ctbase: physical address of the LPI Configuration table to use
  82. * @ds: true if priority value should not be shifted
  83. * @hpp: points to pending information to update
  84. *
  85. * Look up @irq in the Configuration table specified by @ctbase
  86. * to see if it is enabled and what its priority is. If it is an
  87. * enabled interrupt with a higher priority than that currently
  88. * recorded in @hpp, update @hpp.
  89. */
  90. static void update_for_one_lpi(GICv3CPUState *cs, int irq,
  91. uint64_t ctbase, bool ds, PendingIrq *hpp)
  92. {
  93. uint8_t lpite;
  94. uint8_t prio;
  95. address_space_read(&cs->gic->dma_as,
  96. ctbase + ((irq - GICV3_LPI_INTID_START) * sizeof(lpite)),
  97. MEMTXATTRS_UNSPECIFIED, &lpite, sizeof(lpite));
  98. if (!(lpite & LPI_CTE_ENABLED)) {
  99. return;
  100. }
  101. if (ds) {
  102. prio = lpite & LPI_PRIORITY_MASK;
  103. } else {
  104. prio = ((lpite & LPI_PRIORITY_MASK) >> 1) | 0x80;
  105. }
  106. if ((prio < hpp->prio) ||
  107. ((prio == hpp->prio) && (irq <= hpp->irq))) {
  108. hpp->irq = irq;
  109. hpp->prio = prio;
  110. hpp->nmi = false;
  111. /* LPIs and vLPIs are always non-secure Grp1 interrupts */
  112. hpp->grp = GICV3_G1NS;
  113. }
  114. }
  115. /**
  116. * update_for_all_lpis: Fully scan LPI tables and find best pending LPI
  117. *
  118. * @cs: GICv3CPUState
  119. * @ptbase: physical address of LPI Pending table
  120. * @ctbase: physical address of LPI Configuration table
  121. * @ptsizebits: size of tables, specified as number of interrupt ID bits minus 1
  122. * @ds: true if priority value should not be shifted
  123. * @hpp: points to pending information to set
  124. *
  125. * Recalculate the highest priority pending enabled LPI from scratch,
  126. * and set @hpp accordingly.
  127. *
  128. * We scan the LPI pending table @ptbase; for each pending LPI, we read the
  129. * corresponding entry in the LPI configuration table @ctbase to extract
  130. * the priority and enabled information.
  131. *
  132. * We take @ptsizebits in the form idbits-1 because this is the way that
  133. * LPI table sizes are architecturally specified in GICR_PROPBASER.IDBits
  134. * and in the VMAPP command's VPT_size field.
  135. */
  136. static void update_for_all_lpis(GICv3CPUState *cs, uint64_t ptbase,
  137. uint64_t ctbase, unsigned ptsizebits,
  138. bool ds, PendingIrq *hpp)
  139. {
  140. AddressSpace *as = &cs->gic->dma_as;
  141. uint8_t pend;
  142. uint32_t pendt_size = (1ULL << (ptsizebits + 1));
  143. int i, bit;
  144. hpp->prio = 0xff;
  145. hpp->nmi = false;
  146. for (i = GICV3_LPI_INTID_START / 8; i < pendt_size / 8; i++) {
  147. address_space_read(as, ptbase + i, MEMTXATTRS_UNSPECIFIED, &pend, 1);
  148. while (pend) {
  149. bit = ctz32(pend);
  150. update_for_one_lpi(cs, i * 8 + bit, ctbase, ds, hpp);
  151. pend &= ~(1 << bit);
  152. }
  153. }
  154. }
  155. /**
  156. * set_lpi_pending_bit: Set or clear pending bit for an LPI
  157. *
  158. * @cs: GICv3CPUState
  159. * @ptbase: physical address of LPI Pending table
  160. * @irq: LPI to change pending state for
  161. * @level: false to clear pending state, true to set
  162. *
  163. * Returns true if we needed to do something, false if the pending bit
  164. * was already at @level.
  165. */
  166. static bool set_pending_table_bit(GICv3CPUState *cs, uint64_t ptbase,
  167. int irq, bool level)
  168. {
  169. AddressSpace *as = &cs->gic->dma_as;
  170. uint64_t addr = ptbase + irq / 8;
  171. uint8_t pend;
  172. address_space_read(as, addr, MEMTXATTRS_UNSPECIFIED, &pend, 1);
  173. if (extract32(pend, irq % 8, 1) == level) {
  174. /* Bit already at requested state, no action required */
  175. return false;
  176. }
  177. pend = deposit32(pend, irq % 8, 1, level ? 1 : 0);
  178. address_space_write(as, addr, MEMTXATTRS_UNSPECIFIED, &pend, 1);
  179. return true;
  180. }
  181. static uint8_t gicr_read_ipriorityr(GICv3CPUState *cs, MemTxAttrs attrs,
  182. int irq)
  183. {
  184. /* Read the value of GICR_IPRIORITYR<n> for the specified interrupt,
  185. * honouring security state (these are RAZ/WI for Group 0 or Secure
  186. * Group 1 interrupts).
  187. */
  188. uint32_t prio;
  189. prio = cs->gicr_ipriorityr[irq];
  190. if (!attrs.secure && !(cs->gic->gicd_ctlr & GICD_CTLR_DS)) {
  191. if (!(cs->gicr_igroupr0 & (1U << irq))) {
  192. /* Fields for Group 0 or Secure Group 1 interrupts are RAZ/WI */
  193. return 0;
  194. }
  195. /* NS view of the interrupt priority */
  196. prio = (prio << 1) & 0xff;
  197. }
  198. return prio;
  199. }
  200. static void gicr_write_ipriorityr(GICv3CPUState *cs, MemTxAttrs attrs, int irq,
  201. uint8_t value)
  202. {
  203. /* Write the value of GICD_IPRIORITYR<n> for the specified interrupt,
  204. * honouring security state (these are RAZ/WI for Group 0 or Secure
  205. * Group 1 interrupts).
  206. */
  207. if (!attrs.secure && !(cs->gic->gicd_ctlr & GICD_CTLR_DS)) {
  208. if (!(cs->gicr_igroupr0 & (1U << irq))) {
  209. /* Fields for Group 0 or Secure Group 1 interrupts are RAZ/WI */
  210. return;
  211. }
  212. /* NS view of the interrupt priority */
  213. value = 0x80 | (value >> 1);
  214. }
  215. cs->gicr_ipriorityr[irq] = value;
  216. }
  217. static void gicv3_redist_update_vlpi_only(GICv3CPUState *cs)
  218. {
  219. uint64_t ptbase, ctbase, idbits;
  220. if (!FIELD_EX64(cs->gicr_vpendbaser, GICR_VPENDBASER, VALID)) {
  221. cs->hppvlpi.prio = 0xff;
  222. cs->hppvlpi.nmi = false;
  223. return;
  224. }
  225. ptbase = cs->gicr_vpendbaser & R_GICR_VPENDBASER_PHYADDR_MASK;
  226. ctbase = cs->gicr_vpropbaser & R_GICR_VPROPBASER_PHYADDR_MASK;
  227. idbits = FIELD_EX64(cs->gicr_vpropbaser, GICR_VPROPBASER, IDBITS);
  228. update_for_all_lpis(cs, ptbase, ctbase, idbits, true, &cs->hppvlpi);
  229. }
  230. static void gicv3_redist_update_vlpi(GICv3CPUState *cs)
  231. {
  232. gicv3_redist_update_vlpi_only(cs);
  233. gicv3_cpuif_virt_irq_fiq_update(cs);
  234. }
  235. static void gicr_write_vpendbaser(GICv3CPUState *cs, uint64_t newval)
  236. {
  237. /* Write @newval to GICR_VPENDBASER, handling its effects */
  238. bool oldvalid = FIELD_EX64(cs->gicr_vpendbaser, GICR_VPENDBASER, VALID);
  239. bool newvalid = FIELD_EX64(newval, GICR_VPENDBASER, VALID);
  240. bool pendinglast;
  241. /*
  242. * The DIRTY bit is read-only and for us is always zero;
  243. * other fields are writable.
  244. */
  245. newval &= R_GICR_VPENDBASER_INNERCACHE_MASK |
  246. R_GICR_VPENDBASER_SHAREABILITY_MASK |
  247. R_GICR_VPENDBASER_PHYADDR_MASK |
  248. R_GICR_VPENDBASER_OUTERCACHE_MASK |
  249. R_GICR_VPENDBASER_PENDINGLAST_MASK |
  250. R_GICR_VPENDBASER_IDAI_MASK |
  251. R_GICR_VPENDBASER_VALID_MASK;
  252. if (oldvalid && newvalid) {
  253. /*
  254. * Changing other fields while VALID is 1 is UNPREDICTABLE;
  255. * we choose to log and ignore the write.
  256. */
  257. if (cs->gicr_vpendbaser ^ newval) {
  258. qemu_log_mask(LOG_GUEST_ERROR,
  259. "%s: Changing GICR_VPENDBASER when VALID=1 "
  260. "is UNPREDICTABLE\n", __func__);
  261. }
  262. return;
  263. }
  264. if (!oldvalid && !newvalid) {
  265. cs->gicr_vpendbaser = newval;
  266. return;
  267. }
  268. if (newvalid) {
  269. /*
  270. * Valid going from 0 to 1: update hppvlpi from tables.
  271. * If IDAI is 0 we are allowed to use the info we cached in
  272. * the IMPDEF area of the table.
  273. * PendingLast is RES1 when we make this transition.
  274. */
  275. pendinglast = true;
  276. } else {
  277. /*
  278. * Valid going from 1 to 0:
  279. * Set PendingLast if there was a pending enabled interrupt
  280. * for the vPE that was just descheduled.
  281. * If we cache info in the IMPDEF area, write it out here.
  282. */
  283. pendinglast = cs->hppvlpi.prio != 0xff;
  284. }
  285. newval = FIELD_DP64(newval, GICR_VPENDBASER, PENDINGLAST, pendinglast);
  286. cs->gicr_vpendbaser = newval;
  287. gicv3_redist_update_vlpi(cs);
  288. }
  289. static MemTxResult gicr_readb(GICv3CPUState *cs, hwaddr offset,
  290. uint64_t *data, MemTxAttrs attrs)
  291. {
  292. switch (offset) {
  293. case GICR_IPRIORITYR ... GICR_IPRIORITYR + 0x1f:
  294. *data = gicr_read_ipriorityr(cs, attrs, offset - GICR_IPRIORITYR);
  295. return MEMTX_OK;
  296. default:
  297. return MEMTX_ERROR;
  298. }
  299. }
  300. static MemTxResult gicr_writeb(GICv3CPUState *cs, hwaddr offset,
  301. uint64_t value, MemTxAttrs attrs)
  302. {
  303. switch (offset) {
  304. case GICR_IPRIORITYR ... GICR_IPRIORITYR + 0x1f:
  305. gicr_write_ipriorityr(cs, attrs, offset - GICR_IPRIORITYR, value);
  306. gicv3_redist_update(cs);
  307. return MEMTX_OK;
  308. default:
  309. return MEMTX_ERROR;
  310. }
  311. }
  312. static MemTxResult gicr_readl(GICv3CPUState *cs, hwaddr offset,
  313. uint64_t *data, MemTxAttrs attrs)
  314. {
  315. switch (offset) {
  316. case GICR_CTLR:
  317. *data = cs->gicr_ctlr;
  318. return MEMTX_OK;
  319. case GICR_IIDR:
  320. *data = gicv3_iidr();
  321. return MEMTX_OK;
  322. case GICR_TYPER:
  323. *data = extract64(cs->gicr_typer, 0, 32);
  324. return MEMTX_OK;
  325. case GICR_TYPER + 4:
  326. *data = extract64(cs->gicr_typer, 32, 32);
  327. return MEMTX_OK;
  328. case GICR_STATUSR:
  329. /* RAZ/WI for us (this is an optional register and our implementation
  330. * does not track RO/WO/reserved violations to report them to the guest)
  331. */
  332. *data = 0;
  333. return MEMTX_OK;
  334. case GICR_WAKER:
  335. *data = cs->gicr_waker;
  336. return MEMTX_OK;
  337. case GICR_PROPBASER:
  338. *data = extract64(cs->gicr_propbaser, 0, 32);
  339. return MEMTX_OK;
  340. case GICR_PROPBASER + 4:
  341. *data = extract64(cs->gicr_propbaser, 32, 32);
  342. return MEMTX_OK;
  343. case GICR_PENDBASER:
  344. *data = extract64(cs->gicr_pendbaser, 0, 32);
  345. return MEMTX_OK;
  346. case GICR_PENDBASER + 4:
  347. *data = extract64(cs->gicr_pendbaser, 32, 32);
  348. return MEMTX_OK;
  349. case GICR_IGROUPR0:
  350. if (!attrs.secure && !(cs->gic->gicd_ctlr & GICD_CTLR_DS)) {
  351. *data = 0;
  352. return MEMTX_OK;
  353. }
  354. *data = cs->gicr_igroupr0;
  355. return MEMTX_OK;
  356. case GICR_ISENABLER0:
  357. case GICR_ICENABLER0:
  358. *data = gicr_read_bitmap_reg(cs, attrs, cs->gicr_ienabler0);
  359. return MEMTX_OK;
  360. case GICR_ISPENDR0:
  361. case GICR_ICPENDR0:
  362. {
  363. /* The pending register reads as the logical OR of the pending
  364. * latch and the input line level for level-triggered interrupts.
  365. */
  366. uint32_t val = cs->gicr_ipendr0 | (~cs->edge_trigger & cs->level);
  367. *data = gicr_read_bitmap_reg(cs, attrs, val);
  368. return MEMTX_OK;
  369. }
  370. case GICR_ISACTIVER0:
  371. case GICR_ICACTIVER0:
  372. *data = gicr_read_bitmap_reg(cs, attrs, cs->gicr_iactiver0);
  373. return MEMTX_OK;
  374. case GICR_IPRIORITYR ... GICR_IPRIORITYR + 0x1f:
  375. {
  376. int i, irq = offset - GICR_IPRIORITYR;
  377. uint32_t value = 0;
  378. for (i = irq + 3; i >= irq; i--) {
  379. value <<= 8;
  380. value |= gicr_read_ipriorityr(cs, attrs, i);
  381. }
  382. *data = value;
  383. return MEMTX_OK;
  384. }
  385. case GICR_INMIR0:
  386. *data = cs->gic->nmi_support ?
  387. gicr_read_bitmap_reg(cs, attrs, cs->gicr_inmir0) : 0;
  388. return MEMTX_OK;
  389. case GICR_ICFGR0:
  390. case GICR_ICFGR1:
  391. {
  392. /* Our edge_trigger bitmap is one bit per irq; take the correct
  393. * half of it, and spread it out into the odd bits.
  394. */
  395. uint32_t value;
  396. value = cs->edge_trigger & mask_group(cs, attrs);
  397. value = extract32(value, (offset == GICR_ICFGR1) ? 16 : 0, 16);
  398. value = half_shuffle32(value) << 1;
  399. *data = value;
  400. return MEMTX_OK;
  401. }
  402. case GICR_IGRPMODR0:
  403. if ((cs->gic->gicd_ctlr & GICD_CTLR_DS) || !attrs.secure) {
  404. /* RAZ/WI if security disabled, or if
  405. * security enabled and this is an NS access
  406. */
  407. *data = 0;
  408. return MEMTX_OK;
  409. }
  410. *data = cs->gicr_igrpmodr0;
  411. return MEMTX_OK;
  412. case GICR_NSACR:
  413. if ((cs->gic->gicd_ctlr & GICD_CTLR_DS) || !attrs.secure) {
  414. /* RAZ/WI if security disabled, or if
  415. * security enabled and this is an NS access
  416. */
  417. *data = 0;
  418. return MEMTX_OK;
  419. }
  420. *data = cs->gicr_nsacr;
  421. return MEMTX_OK;
  422. case GICR_IDREGS ... GICR_IDREGS + 0x2f:
  423. *data = gicv3_idreg(cs->gic, offset - GICR_IDREGS, GICV3_PIDR0_REDIST);
  424. return MEMTX_OK;
  425. /*
  426. * VLPI frame registers. We don't need a version check for
  427. * VPROPBASER and VPENDBASER because gicv3_redist_size() will
  428. * prevent pre-v4 GIC from passing us offsets this high.
  429. */
  430. case GICR_VPROPBASER:
  431. *data = extract64(cs->gicr_vpropbaser, 0, 32);
  432. return MEMTX_OK;
  433. case GICR_VPROPBASER + 4:
  434. *data = extract64(cs->gicr_vpropbaser, 32, 32);
  435. return MEMTX_OK;
  436. case GICR_VPENDBASER:
  437. *data = extract64(cs->gicr_vpendbaser, 0, 32);
  438. return MEMTX_OK;
  439. case GICR_VPENDBASER + 4:
  440. *data = extract64(cs->gicr_vpendbaser, 32, 32);
  441. return MEMTX_OK;
  442. default:
  443. return MEMTX_ERROR;
  444. }
  445. }
  446. static MemTxResult gicr_writel(GICv3CPUState *cs, hwaddr offset,
  447. uint64_t value, MemTxAttrs attrs)
  448. {
  449. switch (offset) {
  450. case GICR_CTLR:
  451. /* For our implementation, GICR_TYPER.DPGS is 0 and so all
  452. * the DPG bits are RAZ/WI. We don't do anything asynchronously,
  453. * so UWP and RWP are RAZ/WI. GICR_TYPER.LPIS is 1 (we
  454. * implement LPIs) so Enable_LPIs is programmable.
  455. */
  456. if (cs->gicr_typer & GICR_TYPER_PLPIS) {
  457. if (value & GICR_CTLR_ENABLE_LPIS) {
  458. cs->gicr_ctlr |= GICR_CTLR_ENABLE_LPIS;
  459. /* Check for any pending interr in pending table */
  460. gicv3_redist_update_lpi(cs);
  461. } else {
  462. cs->gicr_ctlr &= ~GICR_CTLR_ENABLE_LPIS;
  463. /* cs->hppi might have been an LPI; recalculate */
  464. gicv3_redist_update(cs);
  465. }
  466. }
  467. return MEMTX_OK;
  468. case GICR_STATUSR:
  469. /* RAZ/WI for our implementation */
  470. return MEMTX_OK;
  471. case GICR_WAKER:
  472. /* Only the ProcessorSleep bit is writable. When the guest sets
  473. * it, it requests that we transition the channel between the
  474. * redistributor and the cpu interface to quiescent, and that
  475. * we set the ChildrenAsleep bit once the interface has reached the
  476. * quiescent state.
  477. * Setting the ProcessorSleep to 0 reverses the quiescing, and
  478. * ChildrenAsleep is cleared once the transition is complete.
  479. * Since our interface is not asynchronous, we complete these
  480. * transitions instantaneously, so we set ChildrenAsleep to the
  481. * same value as ProcessorSleep here.
  482. */
  483. value &= GICR_WAKER_ProcessorSleep;
  484. if (value & GICR_WAKER_ProcessorSleep) {
  485. value |= GICR_WAKER_ChildrenAsleep;
  486. }
  487. cs->gicr_waker = value;
  488. return MEMTX_OK;
  489. case GICR_PROPBASER:
  490. cs->gicr_propbaser = deposit64(cs->gicr_propbaser, 0, 32, value);
  491. return MEMTX_OK;
  492. case GICR_PROPBASER + 4:
  493. cs->gicr_propbaser = deposit64(cs->gicr_propbaser, 32, 32, value);
  494. return MEMTX_OK;
  495. case GICR_PENDBASER:
  496. cs->gicr_pendbaser = deposit64(cs->gicr_pendbaser, 0, 32, value);
  497. return MEMTX_OK;
  498. case GICR_PENDBASER + 4:
  499. cs->gicr_pendbaser = deposit64(cs->gicr_pendbaser, 32, 32, value);
  500. return MEMTX_OK;
  501. case GICR_IGROUPR0:
  502. if (!attrs.secure && !(cs->gic->gicd_ctlr & GICD_CTLR_DS)) {
  503. return MEMTX_OK;
  504. }
  505. cs->gicr_igroupr0 = value;
  506. gicv3_redist_update(cs);
  507. return MEMTX_OK;
  508. case GICR_ISENABLER0:
  509. gicr_write_set_bitmap_reg(cs, attrs, &cs->gicr_ienabler0, value);
  510. return MEMTX_OK;
  511. case GICR_ICENABLER0:
  512. gicr_write_clear_bitmap_reg(cs, attrs, &cs->gicr_ienabler0, value);
  513. return MEMTX_OK;
  514. case GICR_ISPENDR0:
  515. gicr_write_set_bitmap_reg(cs, attrs, &cs->gicr_ipendr0, value);
  516. return MEMTX_OK;
  517. case GICR_ICPENDR0:
  518. gicr_write_clear_bitmap_reg(cs, attrs, &cs->gicr_ipendr0, value);
  519. return MEMTX_OK;
  520. case GICR_ISACTIVER0:
  521. gicr_write_set_bitmap_reg(cs, attrs, &cs->gicr_iactiver0, value);
  522. return MEMTX_OK;
  523. case GICR_ICACTIVER0:
  524. gicr_write_clear_bitmap_reg(cs, attrs, &cs->gicr_iactiver0, value);
  525. return MEMTX_OK;
  526. case GICR_IPRIORITYR ... GICR_IPRIORITYR + 0x1f:
  527. {
  528. int i, irq = offset - GICR_IPRIORITYR;
  529. for (i = irq; i < irq + 4; i++, value >>= 8) {
  530. gicr_write_ipriorityr(cs, attrs, i, value);
  531. }
  532. gicv3_redist_update(cs);
  533. return MEMTX_OK;
  534. }
  535. case GICR_INMIR0:
  536. if (cs->gic->nmi_support) {
  537. gicr_write_bitmap_reg(cs, attrs, &cs->gicr_inmir0, value);
  538. }
  539. return MEMTX_OK;
  540. case GICR_ICFGR0:
  541. /* Register is all RAZ/WI or RAO/WI bits */
  542. return MEMTX_OK;
  543. case GICR_ICFGR1:
  544. {
  545. uint32_t mask;
  546. /* Since our edge_trigger bitmap is one bit per irq, our input
  547. * 32-bits will compress down into 16 bits which we need
  548. * to write into the bitmap.
  549. */
  550. value = half_unshuffle32(value >> 1) << 16;
  551. mask = mask_group(cs, attrs) & 0xffff0000U;
  552. cs->edge_trigger &= ~mask;
  553. cs->edge_trigger |= (value & mask);
  554. gicv3_redist_update(cs);
  555. return MEMTX_OK;
  556. }
  557. case GICR_IGRPMODR0:
  558. if ((cs->gic->gicd_ctlr & GICD_CTLR_DS) || !attrs.secure) {
  559. /* RAZ/WI if security disabled, or if
  560. * security enabled and this is an NS access
  561. */
  562. return MEMTX_OK;
  563. }
  564. cs->gicr_igrpmodr0 = value;
  565. gicv3_redist_update(cs);
  566. return MEMTX_OK;
  567. case GICR_NSACR:
  568. if ((cs->gic->gicd_ctlr & GICD_CTLR_DS) || !attrs.secure) {
  569. /* RAZ/WI if security disabled, or if
  570. * security enabled and this is an NS access
  571. */
  572. return MEMTX_OK;
  573. }
  574. cs->gicr_nsacr = value;
  575. /* no update required as this only affects access permission checks */
  576. return MEMTX_OK;
  577. case GICR_IIDR:
  578. case GICR_TYPER:
  579. case GICR_IDREGS ... GICR_IDREGS + 0x2f:
  580. /* RO registers, ignore the write */
  581. qemu_log_mask(LOG_GUEST_ERROR,
  582. "%s: invalid guest write to RO register at offset "
  583. HWADDR_FMT_plx "\n", __func__, offset);
  584. return MEMTX_OK;
  585. /*
  586. * VLPI frame registers. We don't need a version check for
  587. * VPROPBASER and VPENDBASER because gicv3_redist_size() will
  588. * prevent pre-v4 GIC from passing us offsets this high.
  589. */
  590. case GICR_VPROPBASER:
  591. cs->gicr_vpropbaser = deposit64(cs->gicr_vpropbaser, 0, 32, value);
  592. return MEMTX_OK;
  593. case GICR_VPROPBASER + 4:
  594. cs->gicr_vpropbaser = deposit64(cs->gicr_vpropbaser, 32, 32, value);
  595. return MEMTX_OK;
  596. case GICR_VPENDBASER:
  597. gicr_write_vpendbaser(cs, deposit64(cs->gicr_vpendbaser, 0, 32, value));
  598. return MEMTX_OK;
  599. case GICR_VPENDBASER + 4:
  600. gicr_write_vpendbaser(cs, deposit64(cs->gicr_vpendbaser, 32, 32, value));
  601. return MEMTX_OK;
  602. default:
  603. return MEMTX_ERROR;
  604. }
  605. }
  606. static MemTxResult gicr_readll(GICv3CPUState *cs, hwaddr offset,
  607. uint64_t *data, MemTxAttrs attrs)
  608. {
  609. switch (offset) {
  610. case GICR_TYPER:
  611. *data = cs->gicr_typer;
  612. return MEMTX_OK;
  613. case GICR_PROPBASER:
  614. *data = cs->gicr_propbaser;
  615. return MEMTX_OK;
  616. case GICR_PENDBASER:
  617. *data = cs->gicr_pendbaser;
  618. return MEMTX_OK;
  619. /*
  620. * VLPI frame registers. We don't need a version check for
  621. * VPROPBASER and VPENDBASER because gicv3_redist_size() will
  622. * prevent pre-v4 GIC from passing us offsets this high.
  623. */
  624. case GICR_VPROPBASER:
  625. *data = cs->gicr_vpropbaser;
  626. return MEMTX_OK;
  627. case GICR_VPENDBASER:
  628. *data = cs->gicr_vpendbaser;
  629. return MEMTX_OK;
  630. default:
  631. return MEMTX_ERROR;
  632. }
  633. }
  634. static MemTxResult gicr_writell(GICv3CPUState *cs, hwaddr offset,
  635. uint64_t value, MemTxAttrs attrs)
  636. {
  637. switch (offset) {
  638. case GICR_PROPBASER:
  639. cs->gicr_propbaser = value;
  640. return MEMTX_OK;
  641. case GICR_PENDBASER:
  642. cs->gicr_pendbaser = value;
  643. return MEMTX_OK;
  644. case GICR_TYPER:
  645. /* RO register, ignore the write */
  646. qemu_log_mask(LOG_GUEST_ERROR,
  647. "%s: invalid guest write to RO register at offset "
  648. HWADDR_FMT_plx "\n", __func__, offset);
  649. return MEMTX_OK;
  650. /*
  651. * VLPI frame registers. We don't need a version check for
  652. * VPROPBASER and VPENDBASER because gicv3_redist_size() will
  653. * prevent pre-v4 GIC from passing us offsets this high.
  654. */
  655. case GICR_VPROPBASER:
  656. cs->gicr_vpropbaser = value;
  657. return MEMTX_OK;
  658. case GICR_VPENDBASER:
  659. gicr_write_vpendbaser(cs, value);
  660. return MEMTX_OK;
  661. default:
  662. return MEMTX_ERROR;
  663. }
  664. }
  665. MemTxResult gicv3_redist_read(void *opaque, hwaddr offset, uint64_t *data,
  666. unsigned size, MemTxAttrs attrs)
  667. {
  668. GICv3RedistRegion *region = opaque;
  669. GICv3State *s = region->gic;
  670. GICv3CPUState *cs;
  671. MemTxResult r;
  672. int cpuidx;
  673. assert((offset & (size - 1)) == 0);
  674. /*
  675. * There are (for GICv3) two 64K redistributor pages per CPU.
  676. * In some cases the redistributor pages for all CPUs are not
  677. * contiguous (eg on the virt board they are split into two
  678. * parts if there are too many CPUs to all fit in the same place
  679. * in the memory map); if so then the GIC has multiple MemoryRegions
  680. * for the redistributors.
  681. */
  682. cpuidx = region->cpuidx + offset / gicv3_redist_size(s);
  683. offset %= gicv3_redist_size(s);
  684. cs = &s->cpu[cpuidx];
  685. switch (size) {
  686. case 1:
  687. r = gicr_readb(cs, offset, data, attrs);
  688. break;
  689. case 4:
  690. r = gicr_readl(cs, offset, data, attrs);
  691. break;
  692. case 8:
  693. r = gicr_readll(cs, offset, data, attrs);
  694. break;
  695. default:
  696. r = MEMTX_ERROR;
  697. break;
  698. }
  699. if (r != MEMTX_OK) {
  700. qemu_log_mask(LOG_GUEST_ERROR,
  701. "%s: invalid guest read at offset " HWADDR_FMT_plx
  702. " size %u\n", __func__, offset, size);
  703. trace_gicv3_redist_badread(gicv3_redist_affid(cs), offset,
  704. size, attrs.secure);
  705. /* The spec requires that reserved registers are RAZ/WI;
  706. * so use MEMTX_ERROR returns from leaf functions as a way to
  707. * trigger the guest-error logging but don't return it to
  708. * the caller, or we'll cause a spurious guest data abort.
  709. */
  710. r = MEMTX_OK;
  711. *data = 0;
  712. } else {
  713. trace_gicv3_redist_read(gicv3_redist_affid(cs), offset, *data,
  714. size, attrs.secure);
  715. }
  716. return r;
  717. }
  718. MemTxResult gicv3_redist_write(void *opaque, hwaddr offset, uint64_t data,
  719. unsigned size, MemTxAttrs attrs)
  720. {
  721. GICv3RedistRegion *region = opaque;
  722. GICv3State *s = region->gic;
  723. GICv3CPUState *cs;
  724. MemTxResult r;
  725. int cpuidx;
  726. assert((offset & (size - 1)) == 0);
  727. /*
  728. * There are (for GICv3) two 64K redistributor pages per CPU.
  729. * In some cases the redistributor pages for all CPUs are not
  730. * contiguous (eg on the virt board they are split into two
  731. * parts if there are too many CPUs to all fit in the same place
  732. * in the memory map); if so then the GIC has multiple MemoryRegions
  733. * for the redistributors.
  734. */
  735. cpuidx = region->cpuidx + offset / gicv3_redist_size(s);
  736. offset %= gicv3_redist_size(s);
  737. cs = &s->cpu[cpuidx];
  738. switch (size) {
  739. case 1:
  740. r = gicr_writeb(cs, offset, data, attrs);
  741. break;
  742. case 4:
  743. r = gicr_writel(cs, offset, data, attrs);
  744. break;
  745. case 8:
  746. r = gicr_writell(cs, offset, data, attrs);
  747. break;
  748. default:
  749. r = MEMTX_ERROR;
  750. break;
  751. }
  752. if (r != MEMTX_OK) {
  753. qemu_log_mask(LOG_GUEST_ERROR,
  754. "%s: invalid guest write at offset " HWADDR_FMT_plx
  755. " size %u\n", __func__, offset, size);
  756. trace_gicv3_redist_badwrite(gicv3_redist_affid(cs), offset, data,
  757. size, attrs.secure);
  758. /* The spec requires that reserved registers are RAZ/WI;
  759. * so use MEMTX_ERROR returns from leaf functions as a way to
  760. * trigger the guest-error logging but don't return it to
  761. * the caller, or we'll cause a spurious guest data abort.
  762. */
  763. r = MEMTX_OK;
  764. } else {
  765. trace_gicv3_redist_write(gicv3_redist_affid(cs), offset, data,
  766. size, attrs.secure);
  767. }
  768. return r;
  769. }
  770. static void gicv3_redist_check_lpi_priority(GICv3CPUState *cs, int irq)
  771. {
  772. uint64_t lpict_baddr = cs->gicr_propbaser & R_GICR_PROPBASER_PHYADDR_MASK;
  773. update_for_one_lpi(cs, irq, lpict_baddr,
  774. cs->gic->gicd_ctlr & GICD_CTLR_DS,
  775. &cs->hpplpi);
  776. }
  777. void gicv3_redist_update_lpi_only(GICv3CPUState *cs)
  778. {
  779. /*
  780. * This function scans the LPI pending table and for each pending
  781. * LPI, reads the corresponding entry from LPI configuration table
  782. * to extract the priority info and determine if the current LPI
  783. * priority is lower than the last computed high priority lpi interrupt.
  784. * If yes, replace current LPI as the new high priority lpi interrupt.
  785. */
  786. uint64_t lpipt_baddr, lpict_baddr;
  787. uint64_t idbits;
  788. idbits = MIN(FIELD_EX64(cs->gicr_propbaser, GICR_PROPBASER, IDBITS),
  789. GICD_TYPER_IDBITS);
  790. if (!(cs->gicr_ctlr & GICR_CTLR_ENABLE_LPIS)) {
  791. return;
  792. }
  793. lpipt_baddr = cs->gicr_pendbaser & R_GICR_PENDBASER_PHYADDR_MASK;
  794. lpict_baddr = cs->gicr_propbaser & R_GICR_PROPBASER_PHYADDR_MASK;
  795. update_for_all_lpis(cs, lpipt_baddr, lpict_baddr, idbits,
  796. cs->gic->gicd_ctlr & GICD_CTLR_DS, &cs->hpplpi);
  797. }
  798. void gicv3_redist_update_lpi(GICv3CPUState *cs)
  799. {
  800. gicv3_redist_update_lpi_only(cs);
  801. gicv3_redist_update(cs);
  802. }
  803. void gicv3_redist_lpi_pending(GICv3CPUState *cs, int irq, int level)
  804. {
  805. /*
  806. * This function updates the pending bit in lpi pending table for
  807. * the irq being activated or deactivated.
  808. */
  809. uint64_t lpipt_baddr;
  810. lpipt_baddr = cs->gicr_pendbaser & R_GICR_PENDBASER_PHYADDR_MASK;
  811. if (!set_pending_table_bit(cs, lpipt_baddr, irq, level)) {
  812. /* no change in the value of pending bit, return */
  813. return;
  814. }
  815. /*
  816. * check if this LPI is better than the current hpplpi, if yes
  817. * just set hpplpi.prio and .irq without doing a full rescan
  818. */
  819. if (level) {
  820. gicv3_redist_check_lpi_priority(cs, irq);
  821. gicv3_redist_update(cs);
  822. } else {
  823. if (irq == cs->hpplpi.irq) {
  824. gicv3_redist_update_lpi(cs);
  825. }
  826. }
  827. }
  828. void gicv3_redist_process_lpi(GICv3CPUState *cs, int irq, int level)
  829. {
  830. uint64_t idbits;
  831. idbits = MIN(FIELD_EX64(cs->gicr_propbaser, GICR_PROPBASER, IDBITS),
  832. GICD_TYPER_IDBITS);
  833. if (!(cs->gicr_ctlr & GICR_CTLR_ENABLE_LPIS) ||
  834. (irq > (1ULL << (idbits + 1)) - 1) || irq < GICV3_LPI_INTID_START) {
  835. return;
  836. }
  837. /* set/clear the pending bit for this irq */
  838. gicv3_redist_lpi_pending(cs, irq, level);
  839. }
  840. void gicv3_redist_inv_lpi(GICv3CPUState *cs, int irq)
  841. {
  842. /*
  843. * The only cached information for LPIs we have is the HPPLPI.
  844. * We could be cleverer about identifying when we don't need
  845. * to do a full rescan of the pending table, but until we find
  846. * this is a performance issue, just always recalculate.
  847. */
  848. gicv3_redist_update_lpi(cs);
  849. }
  850. void gicv3_redist_mov_lpi(GICv3CPUState *src, GICv3CPUState *dest, int irq)
  851. {
  852. /*
  853. * Move the specified LPI's pending state from the source redistributor
  854. * to the destination.
  855. *
  856. * If LPIs are disabled on dest this is CONSTRAINED UNPREDICTABLE:
  857. * we choose to NOP. If LPIs are disabled on source there's nothing
  858. * to be transferred anyway.
  859. */
  860. uint64_t idbits;
  861. uint32_t pendt_size;
  862. uint64_t src_baddr;
  863. if (!(src->gicr_ctlr & GICR_CTLR_ENABLE_LPIS) ||
  864. !(dest->gicr_ctlr & GICR_CTLR_ENABLE_LPIS)) {
  865. return;
  866. }
  867. idbits = MIN(FIELD_EX64(src->gicr_propbaser, GICR_PROPBASER, IDBITS),
  868. GICD_TYPER_IDBITS);
  869. idbits = MIN(FIELD_EX64(dest->gicr_propbaser, GICR_PROPBASER, IDBITS),
  870. idbits);
  871. pendt_size = 1ULL << (idbits + 1);
  872. if ((irq / 8) >= pendt_size) {
  873. return;
  874. }
  875. src_baddr = src->gicr_pendbaser & R_GICR_PENDBASER_PHYADDR_MASK;
  876. if (!set_pending_table_bit(src, src_baddr, irq, 0)) {
  877. /* Not pending on source, nothing to do */
  878. return;
  879. }
  880. if (irq == src->hpplpi.irq) {
  881. /*
  882. * We just made this LPI not-pending so only need to update
  883. * if it was previously the highest priority pending LPI
  884. */
  885. gicv3_redist_update_lpi(src);
  886. }
  887. /* Mark it pending on the destination */
  888. gicv3_redist_lpi_pending(dest, irq, 1);
  889. }
  890. void gicv3_redist_movall_lpis(GICv3CPUState *src, GICv3CPUState *dest)
  891. {
  892. /*
  893. * We must move all pending LPIs from the source redistributor
  894. * to the destination. That is, for every pending LPI X on
  895. * src, we must set it not-pending on src and pending on dest.
  896. * LPIs that are already pending on dest are not cleared.
  897. *
  898. * If LPIs are disabled on dest this is CONSTRAINED UNPREDICTABLE:
  899. * we choose to NOP. If LPIs are disabled on source there's nothing
  900. * to be transferred anyway.
  901. */
  902. AddressSpace *as = &src->gic->dma_as;
  903. uint64_t idbits;
  904. uint32_t pendt_size;
  905. uint64_t src_baddr, dest_baddr;
  906. int i;
  907. if (!(src->gicr_ctlr & GICR_CTLR_ENABLE_LPIS) ||
  908. !(dest->gicr_ctlr & GICR_CTLR_ENABLE_LPIS)) {
  909. return;
  910. }
  911. idbits = MIN(FIELD_EX64(src->gicr_propbaser, GICR_PROPBASER, IDBITS),
  912. GICD_TYPER_IDBITS);
  913. idbits = MIN(FIELD_EX64(dest->gicr_propbaser, GICR_PROPBASER, IDBITS),
  914. idbits);
  915. pendt_size = 1ULL << (idbits + 1);
  916. src_baddr = src->gicr_pendbaser & R_GICR_PENDBASER_PHYADDR_MASK;
  917. dest_baddr = dest->gicr_pendbaser & R_GICR_PENDBASER_PHYADDR_MASK;
  918. for (i = GICV3_LPI_INTID_START / 8; i < pendt_size / 8; i++) {
  919. uint8_t src_pend, dest_pend;
  920. address_space_read(as, src_baddr + i, MEMTXATTRS_UNSPECIFIED,
  921. &src_pend, sizeof(src_pend));
  922. if (!src_pend) {
  923. continue;
  924. }
  925. address_space_read(as, dest_baddr + i, MEMTXATTRS_UNSPECIFIED,
  926. &dest_pend, sizeof(dest_pend));
  927. dest_pend |= src_pend;
  928. src_pend = 0;
  929. address_space_write(as, src_baddr + i, MEMTXATTRS_UNSPECIFIED,
  930. &src_pend, sizeof(src_pend));
  931. address_space_write(as, dest_baddr + i, MEMTXATTRS_UNSPECIFIED,
  932. &dest_pend, sizeof(dest_pend));
  933. }
  934. gicv3_redist_update_lpi(src);
  935. gicv3_redist_update_lpi(dest);
  936. }
  937. void gicv3_redist_vlpi_pending(GICv3CPUState *cs, int irq, int level)
  938. {
  939. /*
  940. * Change the pending state of the specified vLPI.
  941. * Unlike gicv3_redist_process_vlpi(), we know here that the
  942. * vCPU is definitely resident on this redistributor, and that
  943. * the irq is in range.
  944. */
  945. uint64_t vptbase, ctbase;
  946. vptbase = FIELD_EX64(cs->gicr_vpendbaser, GICR_VPENDBASER, PHYADDR) << 16;
  947. if (set_pending_table_bit(cs, vptbase, irq, level)) {
  948. if (level) {
  949. /* Check whether this vLPI is now the best */
  950. ctbase = cs->gicr_vpropbaser & R_GICR_VPROPBASER_PHYADDR_MASK;
  951. update_for_one_lpi(cs, irq, ctbase, true, &cs->hppvlpi);
  952. gicv3_cpuif_virt_irq_fiq_update(cs);
  953. } else {
  954. /* Only need to recalculate if this was previously the best vLPI */
  955. if (irq == cs->hppvlpi.irq) {
  956. gicv3_redist_update_vlpi(cs);
  957. }
  958. }
  959. }
  960. }
  961. void gicv3_redist_process_vlpi(GICv3CPUState *cs, int irq, uint64_t vptaddr,
  962. int doorbell, int level)
  963. {
  964. bool bit_changed;
  965. bool resident = vcpu_resident(cs, vptaddr);
  966. uint64_t ctbase;
  967. if (resident) {
  968. uint32_t idbits = FIELD_EX64(cs->gicr_vpropbaser, GICR_VPROPBASER, IDBITS);
  969. if (irq >= (1ULL << (idbits + 1))) {
  970. return;
  971. }
  972. }
  973. bit_changed = set_pending_table_bit(cs, vptaddr, irq, level);
  974. if (resident && bit_changed) {
  975. if (level) {
  976. /* Check whether this vLPI is now the best */
  977. ctbase = cs->gicr_vpropbaser & R_GICR_VPROPBASER_PHYADDR_MASK;
  978. update_for_one_lpi(cs, irq, ctbase, true, &cs->hppvlpi);
  979. gicv3_cpuif_virt_irq_fiq_update(cs);
  980. } else {
  981. /* Only need to recalculate if this was previously the best vLPI */
  982. if (irq == cs->hppvlpi.irq) {
  983. gicv3_redist_update_vlpi(cs);
  984. }
  985. }
  986. }
  987. if (!resident && level && doorbell != INTID_SPURIOUS &&
  988. (cs->gicr_ctlr & GICR_CTLR_ENABLE_LPIS)) {
  989. /* vCPU is not currently resident: ring the doorbell */
  990. gicv3_redist_process_lpi(cs, doorbell, 1);
  991. }
  992. }
  993. void gicv3_redist_mov_vlpi(GICv3CPUState *src, uint64_t src_vptaddr,
  994. GICv3CPUState *dest, uint64_t dest_vptaddr,
  995. int irq, int doorbell)
  996. {
  997. /*
  998. * Move the specified vLPI's pending state from the source redistributor
  999. * to the destination.
  1000. */
  1001. if (!set_pending_table_bit(src, src_vptaddr, irq, 0)) {
  1002. /* Not pending on source, nothing to do */
  1003. return;
  1004. }
  1005. if (vcpu_resident(src, src_vptaddr) && irq == src->hppvlpi.irq) {
  1006. /*
  1007. * Update src's cached highest-priority pending vLPI if we just made
  1008. * it not-pending
  1009. */
  1010. gicv3_redist_update_vlpi(src);
  1011. }
  1012. /*
  1013. * Mark the vLPI pending on the destination (ringing the doorbell
  1014. * if the vCPU isn't resident)
  1015. */
  1016. gicv3_redist_process_vlpi(dest, irq, dest_vptaddr, doorbell, irq);
  1017. }
  1018. void gicv3_redist_vinvall(GICv3CPUState *cs, uint64_t vptaddr)
  1019. {
  1020. if (!vcpu_resident(cs, vptaddr)) {
  1021. /* We don't have anything cached if the vCPU isn't resident */
  1022. return;
  1023. }
  1024. /* Otherwise, our only cached information is the HPPVLPI info */
  1025. gicv3_redist_update_vlpi(cs);
  1026. }
  1027. void gicv3_redist_inv_vlpi(GICv3CPUState *cs, int irq, uint64_t vptaddr)
  1028. {
  1029. /*
  1030. * The only cached information for LPIs we have is the HPPLPI.
  1031. * We could be cleverer about identifying when we don't need
  1032. * to do a full rescan of the pending table, but until we find
  1033. * this is a performance issue, just always recalculate.
  1034. */
  1035. gicv3_redist_vinvall(cs, vptaddr);
  1036. }
  1037. void gicv3_redist_set_irq(GICv3CPUState *cs, int irq, int level)
  1038. {
  1039. /* Update redistributor state for a change in an external PPI input line */
  1040. if (level == extract32(cs->level, irq, 1)) {
  1041. return;
  1042. }
  1043. trace_gicv3_redist_set_irq(gicv3_redist_affid(cs), irq, level);
  1044. cs->level = deposit32(cs->level, irq, 1, level);
  1045. if (level) {
  1046. /* 0->1 edges latch the pending bit for edge-triggered interrupts */
  1047. if (extract32(cs->edge_trigger, irq, 1)) {
  1048. cs->gicr_ipendr0 = deposit32(cs->gicr_ipendr0, irq, 1, 1);
  1049. }
  1050. }
  1051. gicv3_redist_update(cs);
  1052. }
  1053. void gicv3_redist_send_sgi(GICv3CPUState *cs, int grp, int irq, bool ns)
  1054. {
  1055. /* Update redistributor state for a generated SGI */
  1056. int irqgrp = gicv3_irq_group(cs->gic, cs, irq);
  1057. /* If we are asked for a Secure Group 1 SGI and it's actually
  1058. * configured as Secure Group 0 this is OK (subject to the usual
  1059. * NSACR checks).
  1060. */
  1061. if (grp == GICV3_G1 && irqgrp == GICV3_G0) {
  1062. grp = GICV3_G0;
  1063. }
  1064. if (grp != irqgrp) {
  1065. return;
  1066. }
  1067. if (ns && !(cs->gic->gicd_ctlr & GICD_CTLR_DS)) {
  1068. /* If security is enabled we must test the NSACR bits */
  1069. int nsaccess = gicr_ns_access(cs, irq);
  1070. if ((irqgrp == GICV3_G0 && nsaccess < 1) ||
  1071. (irqgrp == GICV3_G1 && nsaccess < 2)) {
  1072. return;
  1073. }
  1074. }
  1075. /* OK, we can accept the SGI */
  1076. trace_gicv3_redist_send_sgi(gicv3_redist_affid(cs), irq);
  1077. cs->gicr_ipendr0 = deposit32(cs->gicr_ipendr0, irq, 1, 1);
  1078. gicv3_redist_update(cs);
  1079. }