arm_gicv3_dist.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. /*
  2. * ARM GICv3 emulation: Distributor
  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. /* The GICD_NSACR registers contain a two bit field for each interrupt which
  16. * allows the guest to give NonSecure code access to registers controlling
  17. * Secure interrupts:
  18. * 0b00: no access (NS accesses to bits for Secure interrupts will RAZ/WI)
  19. * 0b01: NS r/w accesses permitted to ISPENDR, SETSPI_NSR, SGIR
  20. * 0b10: as 0b01, and also r/w to ICPENDR, r/o to ISACTIVER/ICACTIVER,
  21. * and w/o to CLRSPI_NSR
  22. * 0b11: as 0b10, and also r/w to IROUTER and ITARGETSR
  23. *
  24. * Given a (multiple-of-32) interrupt number, these mask functions return
  25. * a mask word where each bit is 1 if the NSACR settings permit access
  26. * to the interrupt. The mask returned can then be ORed with the GICD_GROUP
  27. * word for this set of interrupts to give an overall mask.
  28. */
  29. typedef uint32_t maskfn(GICv3State *s, int irq);
  30. static uint32_t mask_nsacr_ge1(GICv3State *s, int irq)
  31. {
  32. /* Return a mask where each bit is set if the NSACR field is >= 1 */
  33. uint64_t raw_nsacr = s->gicd_nsacr[irq / 16 + 1];
  34. raw_nsacr = raw_nsacr << 32 | s->gicd_nsacr[irq / 16];
  35. raw_nsacr = (raw_nsacr >> 1) | raw_nsacr;
  36. return half_unshuffle64(raw_nsacr);
  37. }
  38. static uint32_t mask_nsacr_ge2(GICv3State *s, int irq)
  39. {
  40. /* Return a mask where each bit is set if the NSACR field is >= 2 */
  41. uint64_t raw_nsacr = s->gicd_nsacr[irq / 16 + 1];
  42. raw_nsacr = raw_nsacr << 32 | s->gicd_nsacr[irq / 16];
  43. raw_nsacr = raw_nsacr >> 1;
  44. return half_unshuffle64(raw_nsacr);
  45. }
  46. /* We don't need a mask_nsacr_ge3() because IROUTER<n> isn't a bitmap register,
  47. * but it would be implemented using:
  48. * raw_nsacr = (raw_nsacr >> 1) & raw_nsacr;
  49. */
  50. static uint32_t mask_group_and_nsacr(GICv3State *s, MemTxAttrs attrs,
  51. maskfn *maskfn, int irq)
  52. {
  53. /* Return a 32-bit mask which should be applied for this set of 32
  54. * interrupts; each bit is 1 if access is permitted by the
  55. * combination of attrs.secure, GICD_GROUPR and GICD_NSACR.
  56. */
  57. uint32_t mask;
  58. if (!attrs.secure && !(s->gicd_ctlr & GICD_CTLR_DS)) {
  59. /* bits for Group 0 or Secure Group 1 interrupts are RAZ/WI
  60. * unless the NSACR bits permit access.
  61. */
  62. mask = *gic_bmp_ptr32(s->group, irq);
  63. if (maskfn) {
  64. mask |= maskfn(s, irq);
  65. }
  66. return mask;
  67. }
  68. return 0xFFFFFFFFU;
  69. }
  70. static int gicd_ns_access(GICv3State *s, int irq)
  71. {
  72. /* Return the 2 bit NS_access<x> field from GICD_NSACR<n> for the
  73. * specified interrupt.
  74. */
  75. if (irq < GIC_INTERNAL || irq >= s->num_irq) {
  76. return 0;
  77. }
  78. return extract32(s->gicd_nsacr[irq / 16], (irq % 16) * 2, 2);
  79. }
  80. static void gicd_write_bitmap_reg(GICv3State *s, MemTxAttrs attrs,
  81. uint32_t *bmp, maskfn *maskfn,
  82. int offset, uint32_t val)
  83. {
  84. /*
  85. * Helper routine to implement writing to a "set" register
  86. * (GICD_INMIR, etc).
  87. * Semantics implemented here:
  88. * RAZ/WI for SGIs, PPIs, unimplemented IRQs
  89. * Bits corresponding to Group 0 or Secure Group 1 interrupts RAZ/WI.
  90. * offset should be the offset in bytes of the register from the start
  91. * of its group.
  92. */
  93. int irq = offset * 8;
  94. if (irq < GIC_INTERNAL || irq >= s->num_irq) {
  95. return;
  96. }
  97. val &= mask_group_and_nsacr(s, attrs, maskfn, irq);
  98. *gic_bmp_ptr32(bmp, irq) = val;
  99. gicv3_update(s, irq, 32);
  100. }
  101. static void gicd_write_set_bitmap_reg(GICv3State *s, MemTxAttrs attrs,
  102. uint32_t *bmp,
  103. maskfn *maskfn,
  104. int offset, uint32_t val)
  105. {
  106. /* Helper routine to implement writing to a "set-bitmap" register
  107. * (GICD_ISENABLER, GICD_ISPENDR, etc).
  108. * Semantics implemented here:
  109. * RAZ/WI for SGIs, PPIs, unimplemented IRQs
  110. * Bits corresponding to Group 0 or Secure Group 1 interrupts RAZ/WI.
  111. * Writing 1 means "set bit in bitmap"; writing 0 is ignored.
  112. * offset should be the offset in bytes of the register from the start
  113. * of its group.
  114. */
  115. int irq = offset * 8;
  116. if (irq < GIC_INTERNAL || irq >= s->num_irq) {
  117. return;
  118. }
  119. val &= mask_group_and_nsacr(s, attrs, maskfn, irq);
  120. *gic_bmp_ptr32(bmp, irq) |= val;
  121. gicv3_update(s, irq, 32);
  122. }
  123. static void gicd_write_clear_bitmap_reg(GICv3State *s, MemTxAttrs attrs,
  124. uint32_t *bmp,
  125. maskfn *maskfn,
  126. int offset, uint32_t val)
  127. {
  128. /* Helper routine to implement writing to a "clear-bitmap" register
  129. * (GICD_ICENABLER, GICD_ICPENDR, etc).
  130. * Semantics implemented here:
  131. * RAZ/WI for SGIs, PPIs, unimplemented IRQs
  132. * Bits corresponding to Group 0 or Secure Group 1 interrupts RAZ/WI.
  133. * Writing 1 means "clear bit in bitmap"; writing 0 is ignored.
  134. * offset should be the offset in bytes of the register from the start
  135. * of its group.
  136. */
  137. int irq = offset * 8;
  138. if (irq < GIC_INTERNAL || irq >= s->num_irq) {
  139. return;
  140. }
  141. val &= mask_group_and_nsacr(s, attrs, maskfn, irq);
  142. *gic_bmp_ptr32(bmp, irq) &= ~val;
  143. gicv3_update(s, irq, 32);
  144. }
  145. static uint32_t gicd_read_bitmap_reg(GICv3State *s, MemTxAttrs attrs,
  146. uint32_t *bmp,
  147. maskfn *maskfn,
  148. int offset)
  149. {
  150. /* Helper routine to implement reading a "set/clear-bitmap" register
  151. * (GICD_ICENABLER, GICD_ISENABLER, GICD_ICPENDR, etc).
  152. * Semantics implemented here:
  153. * RAZ/WI for SGIs, PPIs, unimplemented IRQs
  154. * Bits corresponding to Group 0 or Secure Group 1 interrupts RAZ/WI.
  155. * offset should be the offset in bytes of the register from the start
  156. * of its group.
  157. */
  158. int irq = offset * 8;
  159. uint32_t val;
  160. if (irq < GIC_INTERNAL || irq >= s->num_irq) {
  161. return 0;
  162. }
  163. val = *gic_bmp_ptr32(bmp, irq);
  164. if (bmp == s->pending) {
  165. /* The PENDING register is a special case -- for level triggered
  166. * interrupts, the PENDING state is the logical OR of the state of
  167. * the PENDING latch with the input line level.
  168. */
  169. uint32_t edge = *gic_bmp_ptr32(s->edge_trigger, irq);
  170. uint32_t level = *gic_bmp_ptr32(s->level, irq);
  171. val |= (~edge & level);
  172. }
  173. val &= mask_group_and_nsacr(s, attrs, maskfn, irq);
  174. return val;
  175. }
  176. static uint8_t gicd_read_ipriorityr(GICv3State *s, MemTxAttrs attrs, int irq)
  177. {
  178. /* Read the value of GICD_IPRIORITYR<n> for the specified interrupt,
  179. * honouring security state (these are RAZ/WI for Group 0 or Secure
  180. * Group 1 interrupts).
  181. */
  182. uint32_t prio;
  183. if (irq < GIC_INTERNAL || irq >= s->num_irq) {
  184. return 0;
  185. }
  186. prio = s->gicd_ipriority[irq];
  187. if (!attrs.secure && !(s->gicd_ctlr & GICD_CTLR_DS)) {
  188. if (!gicv3_gicd_group_test(s, irq)) {
  189. /* Fields for Group 0 or Secure Group 1 interrupts are RAZ/WI */
  190. return 0;
  191. }
  192. /* NS view of the interrupt priority */
  193. prio = (prio << 1) & 0xff;
  194. }
  195. return prio;
  196. }
  197. static void gicd_write_ipriorityr(GICv3State *s, MemTxAttrs attrs, int irq,
  198. uint8_t value)
  199. {
  200. /* Write the value of GICD_IPRIORITYR<n> for the specified interrupt,
  201. * honouring security state (these are RAZ/WI for Group 0 or Secure
  202. * Group 1 interrupts).
  203. */
  204. if (irq < GIC_INTERNAL || irq >= s->num_irq) {
  205. return;
  206. }
  207. if (!attrs.secure && !(s->gicd_ctlr & GICD_CTLR_DS)) {
  208. if (!gicv3_gicd_group_test(s, 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. s->gicd_ipriority[irq] = value;
  216. }
  217. static uint64_t gicd_read_irouter(GICv3State *s, MemTxAttrs attrs, int irq)
  218. {
  219. /* Read the value of GICD_IROUTER<n> for the specified interrupt,
  220. * honouring security state.
  221. */
  222. if (irq < GIC_INTERNAL || irq >= s->num_irq) {
  223. return 0;
  224. }
  225. if (!attrs.secure && !(s->gicd_ctlr & GICD_CTLR_DS)) {
  226. /* RAZ/WI for NS accesses to secure interrupts */
  227. if (!gicv3_gicd_group_test(s, irq)) {
  228. if (gicd_ns_access(s, irq) != 3) {
  229. return 0;
  230. }
  231. }
  232. }
  233. return s->gicd_irouter[irq];
  234. }
  235. static void gicd_write_irouter(GICv3State *s, MemTxAttrs attrs, int irq,
  236. uint64_t val)
  237. {
  238. /* Write the value of GICD_IROUTER<n> for the specified interrupt,
  239. * honouring security state.
  240. */
  241. if (irq < GIC_INTERNAL || irq >= s->num_irq) {
  242. return;
  243. }
  244. if (!attrs.secure && !(s->gicd_ctlr & GICD_CTLR_DS)) {
  245. /* RAZ/WI for NS accesses to secure interrupts */
  246. if (!gicv3_gicd_group_test(s, irq)) {
  247. if (gicd_ns_access(s, irq) != 3) {
  248. return;
  249. }
  250. }
  251. }
  252. s->gicd_irouter[irq] = val;
  253. gicv3_cache_target_cpustate(s, irq);
  254. gicv3_update(s, irq, 1);
  255. }
  256. /**
  257. * gicd_readb
  258. * gicd_readw
  259. * gicd_readl
  260. * gicd_readq
  261. * gicd_writeb
  262. * gicd_writew
  263. * gicd_writel
  264. * gicd_writeq
  265. *
  266. * Return %true if the operation succeeded, %false otherwise.
  267. */
  268. static bool gicd_readb(GICv3State *s, hwaddr offset,
  269. uint64_t *data, MemTxAttrs attrs)
  270. {
  271. /* Most GICv3 distributor registers do not support byte accesses. */
  272. switch (offset) {
  273. case GICD_CPENDSGIR ... GICD_CPENDSGIR + 0xf:
  274. case GICD_SPENDSGIR ... GICD_SPENDSGIR + 0xf:
  275. case GICD_ITARGETSR ... GICD_ITARGETSR + 0x3ff:
  276. /* This GIC implementation always has affinity routing enabled,
  277. * so these registers are all RAZ/WI.
  278. */
  279. return true;
  280. case GICD_IPRIORITYR ... GICD_IPRIORITYR + 0x3ff:
  281. *data = gicd_read_ipriorityr(s, attrs, offset - GICD_IPRIORITYR);
  282. return true;
  283. default:
  284. return false;
  285. }
  286. }
  287. static bool gicd_writeb(GICv3State *s, hwaddr offset,
  288. uint64_t value, MemTxAttrs attrs)
  289. {
  290. /* Most GICv3 distributor registers do not support byte accesses. */
  291. switch (offset) {
  292. case GICD_CPENDSGIR ... GICD_CPENDSGIR + 0xf:
  293. case GICD_SPENDSGIR ... GICD_SPENDSGIR + 0xf:
  294. case GICD_ITARGETSR ... GICD_ITARGETSR + 0x3ff:
  295. /* This GIC implementation always has affinity routing enabled,
  296. * so these registers are all RAZ/WI.
  297. */
  298. return true;
  299. case GICD_IPRIORITYR ... GICD_IPRIORITYR + 0x3ff:
  300. {
  301. int irq = offset - GICD_IPRIORITYR;
  302. if (irq < GIC_INTERNAL || irq >= s->num_irq) {
  303. return true;
  304. }
  305. gicd_write_ipriorityr(s, attrs, irq, value);
  306. gicv3_update(s, irq, 1);
  307. return true;
  308. }
  309. default:
  310. return false;
  311. }
  312. }
  313. static bool gicd_readw(GICv3State *s, hwaddr offset,
  314. uint64_t *data, MemTxAttrs attrs)
  315. {
  316. /* Only GICD_SETSPI_NSR, GICD_CLRSPI_NSR, GICD_SETSPI_SR and GICD_SETSPI_NSR
  317. * support 16 bit accesses, and those registers are all part of the
  318. * optional message-based SPI feature which this GIC does not currently
  319. * implement (ie for us GICD_TYPER.MBIS == 0), so for us they are
  320. * reserved.
  321. */
  322. return false;
  323. }
  324. static bool gicd_writew(GICv3State *s, hwaddr offset,
  325. uint64_t value, MemTxAttrs attrs)
  326. {
  327. /* Only GICD_SETSPI_NSR, GICD_CLRSPI_NSR, GICD_SETSPI_SR and GICD_SETSPI_NSR
  328. * support 16 bit accesses, and those registers are all part of the
  329. * optional message-based SPI feature which this GIC does not currently
  330. * implement (ie for us GICD_TYPER.MBIS == 0), so for us they are
  331. * reserved.
  332. */
  333. return false;
  334. }
  335. static bool gicd_readl(GICv3State *s, hwaddr offset,
  336. uint64_t *data, MemTxAttrs attrs)
  337. {
  338. /* Almost all GICv3 distributor registers are 32-bit.
  339. * Note that WO registers must return an UNKNOWN value on reads,
  340. * not an abort.
  341. */
  342. switch (offset) {
  343. case GICD_CTLR:
  344. if (!attrs.secure && !(s->gicd_ctlr & GICD_CTLR_DS)) {
  345. /* The NS view of the GICD_CTLR sees only certain bits:
  346. * + bit [31] (RWP) is an alias of the Secure bit [31]
  347. * + bit [4] (ARE_NS) is an alias of Secure bit [5]
  348. * + bit [1] (EnableGrp1A) is an alias of Secure bit [1] if
  349. * NS affinity routing is enabled, otherwise RES0
  350. * + bit [0] (EnableGrp1) is an alias of Secure bit [1] if
  351. * NS affinity routing is not enabled, otherwise RES0
  352. * Since for QEMU affinity routing is always enabled
  353. * for both S and NS this means that bits [4] and [5] are
  354. * both always 1, and we can simply make the NS view
  355. * be bits 31, 4 and 1 of the S view.
  356. */
  357. *data = s->gicd_ctlr & (GICD_CTLR_ARE_S |
  358. GICD_CTLR_EN_GRP1NS |
  359. GICD_CTLR_RWP);
  360. } else {
  361. *data = s->gicd_ctlr;
  362. }
  363. return true;
  364. case GICD_TYPER:
  365. {
  366. /* For this implementation:
  367. * No1N == 1 (1-of-N SPI interrupts not supported)
  368. * A3V == 1 (non-zero values of Affinity level 3 supported)
  369. * IDbits == 0xf (we support 16-bit interrupt identifiers)
  370. * DVIS == 1 (Direct virtual LPI injection supported) if GICv4
  371. * LPIS == 1 (LPIs are supported if affinity routing is enabled)
  372. * num_LPIs == 0b00000 (bits [15:11],Number of LPIs as indicated
  373. * by GICD_TYPER.IDbits)
  374. * MBIS == 0 (message-based SPIs not supported)
  375. * SecurityExtn == 1 if security extns supported
  376. * NMI = 1 if Non-maskable interrupt property is supported
  377. * CPUNumber == 0 since for us ARE is always 1
  378. * ITLinesNumber == (((max SPI IntID + 1) / 32) - 1)
  379. */
  380. int itlinesnumber = (s->num_irq / 32) - 1;
  381. /*
  382. * SecurityExtn must be RAZ if GICD_CTLR.DS == 1, and
  383. * "security extensions not supported" always implies DS == 1,
  384. * so we only need to check the DS bit.
  385. */
  386. bool sec_extn = !(s->gicd_ctlr & GICD_CTLR_DS);
  387. bool dvis = s->revision >= 4;
  388. *data = (1 << 25) | (1 << 24) | (dvis << 18) | (sec_extn << 10) |
  389. (s->nmi_support << GICD_TYPER_NMI_SHIFT) |
  390. (s->lpi_enable << GICD_TYPER_LPIS_SHIFT) |
  391. (0xf << 19) | itlinesnumber;
  392. return true;
  393. }
  394. case GICD_IIDR:
  395. /* We claim to be an ARM r0p0 with a zero ProductID.
  396. * This is the same as an r0p0 GIC-500.
  397. */
  398. *data = gicv3_iidr();
  399. return true;
  400. case GICD_STATUSR:
  401. /* RAZ/WI for us (this is an optional register and our implementation
  402. * does not track RO/WO/reserved violations to report them to the guest)
  403. */
  404. *data = 0;
  405. return true;
  406. case GICD_IGROUPR ... GICD_IGROUPR + 0x7f:
  407. {
  408. int irq;
  409. if (!attrs.secure && !(s->gicd_ctlr & GICD_CTLR_DS)) {
  410. *data = 0;
  411. return true;
  412. }
  413. /* RAZ/WI for SGIs, PPIs, unimplemented irqs */
  414. irq = (offset - GICD_IGROUPR) * 8;
  415. if (irq < GIC_INTERNAL || irq >= s->num_irq) {
  416. *data = 0;
  417. return true;
  418. }
  419. *data = *gic_bmp_ptr32(s->group, irq);
  420. return true;
  421. }
  422. case GICD_ISENABLER ... GICD_ISENABLER + 0x7f:
  423. *data = gicd_read_bitmap_reg(s, attrs, s->enabled, NULL,
  424. offset - GICD_ISENABLER);
  425. return true;
  426. case GICD_ICENABLER ... GICD_ICENABLER + 0x7f:
  427. *data = gicd_read_bitmap_reg(s, attrs, s->enabled, NULL,
  428. offset - GICD_ICENABLER);
  429. return true;
  430. case GICD_ISPENDR ... GICD_ISPENDR + 0x7f:
  431. *data = gicd_read_bitmap_reg(s, attrs, s->pending, mask_nsacr_ge1,
  432. offset - GICD_ISPENDR);
  433. return true;
  434. case GICD_ICPENDR ... GICD_ICPENDR + 0x7f:
  435. *data = gicd_read_bitmap_reg(s, attrs, s->pending, mask_nsacr_ge2,
  436. offset - GICD_ICPENDR);
  437. return true;
  438. case GICD_ISACTIVER ... GICD_ISACTIVER + 0x7f:
  439. *data = gicd_read_bitmap_reg(s, attrs, s->active, mask_nsacr_ge2,
  440. offset - GICD_ISACTIVER);
  441. return true;
  442. case GICD_ICACTIVER ... GICD_ICACTIVER + 0x7f:
  443. *data = gicd_read_bitmap_reg(s, attrs, s->active, mask_nsacr_ge2,
  444. offset - GICD_ICACTIVER);
  445. return true;
  446. case GICD_IPRIORITYR ... GICD_IPRIORITYR + 0x3ff:
  447. {
  448. int i, irq = offset - GICD_IPRIORITYR;
  449. uint32_t value = 0;
  450. for (i = irq + 3; i >= irq; i--) {
  451. value <<= 8;
  452. value |= gicd_read_ipriorityr(s, attrs, i);
  453. }
  454. *data = value;
  455. return true;
  456. }
  457. case GICD_ITARGETSR ... GICD_ITARGETSR + 0x3ff:
  458. /* RAZ/WI since affinity routing is always enabled */
  459. *data = 0;
  460. return true;
  461. case GICD_ICFGR ... GICD_ICFGR + 0xff:
  462. {
  463. /* Here only the even bits are used; odd bits are RES0 */
  464. int irq = (offset - GICD_ICFGR) * 4;
  465. uint32_t value = 0;
  466. if (irq < GIC_INTERNAL || irq >= s->num_irq) {
  467. *data = 0;
  468. return true;
  469. }
  470. /* Since our edge_trigger bitmap is one bit per irq, we only need
  471. * half of the 32-bit word, which we can then spread out
  472. * into the odd bits.
  473. */
  474. value = *gic_bmp_ptr32(s->edge_trigger, irq & ~0x1f);
  475. value &= mask_group_and_nsacr(s, attrs, NULL, irq & ~0x1f);
  476. value = extract32(value, (irq & 0x1f) ? 16 : 0, 16);
  477. value = half_shuffle32(value) << 1;
  478. *data = value;
  479. return true;
  480. }
  481. case GICD_IGRPMODR ... GICD_IGRPMODR + 0xff:
  482. {
  483. int irq;
  484. if ((s->gicd_ctlr & GICD_CTLR_DS) || !attrs.secure) {
  485. /* RAZ/WI if security disabled, or if
  486. * security enabled and this is an NS access
  487. */
  488. *data = 0;
  489. return true;
  490. }
  491. /* RAZ/WI for SGIs, PPIs, unimplemented irqs */
  492. irq = (offset - GICD_IGRPMODR) * 8;
  493. if (irq < GIC_INTERNAL || irq >= s->num_irq) {
  494. *data = 0;
  495. return true;
  496. }
  497. *data = *gic_bmp_ptr32(s->grpmod, irq);
  498. return true;
  499. }
  500. case GICD_NSACR ... GICD_NSACR + 0xff:
  501. {
  502. /* Two bits per interrupt */
  503. int irq = (offset - GICD_NSACR) * 4;
  504. if (irq < GIC_INTERNAL || irq >= s->num_irq) {
  505. *data = 0;
  506. return true;
  507. }
  508. if ((s->gicd_ctlr & GICD_CTLR_DS) || !attrs.secure) {
  509. /* RAZ/WI if security disabled, or if
  510. * security enabled and this is an NS access
  511. */
  512. *data = 0;
  513. return true;
  514. }
  515. *data = s->gicd_nsacr[irq / 16];
  516. return true;
  517. }
  518. case GICD_CPENDSGIR ... GICD_CPENDSGIR + 0xf:
  519. case GICD_SPENDSGIR ... GICD_SPENDSGIR + 0xf:
  520. /* RAZ/WI since affinity routing is always enabled */
  521. *data = 0;
  522. return true;
  523. case GICD_INMIR ... GICD_INMIR + 0x7f:
  524. *data = (!s->nmi_support) ? 0 :
  525. gicd_read_bitmap_reg(s, attrs, s->nmi, NULL,
  526. offset - GICD_INMIR);
  527. return true;
  528. case GICD_IROUTER ... GICD_IROUTER + 0x1fdf:
  529. {
  530. uint64_t r;
  531. int irq = (offset - GICD_IROUTER) / 8;
  532. r = gicd_read_irouter(s, attrs, irq);
  533. if (offset & 7) {
  534. *data = r >> 32;
  535. } else {
  536. *data = (uint32_t)r;
  537. }
  538. return true;
  539. }
  540. case GICD_IDREGS ... GICD_IDREGS + 0x2f:
  541. /* ID registers */
  542. *data = gicv3_idreg(s, offset - GICD_IDREGS, GICV3_PIDR0_DIST);
  543. return true;
  544. case GICD_SGIR:
  545. /* WO registers, return unknown value */
  546. qemu_log_mask(LOG_GUEST_ERROR,
  547. "%s: invalid guest read from WO register at offset "
  548. HWADDR_FMT_plx "\n", __func__, offset);
  549. *data = 0;
  550. return true;
  551. default:
  552. return false;
  553. }
  554. }
  555. static bool gicd_writel(GICv3State *s, hwaddr offset,
  556. uint64_t value, MemTxAttrs attrs)
  557. {
  558. /* Almost all GICv3 distributor registers are 32-bit. Note that
  559. * RO registers must ignore writes, not abort.
  560. */
  561. switch (offset) {
  562. case GICD_CTLR:
  563. {
  564. uint32_t mask;
  565. /* GICv3 5.3.20 */
  566. if (s->gicd_ctlr & GICD_CTLR_DS) {
  567. /* With only one security state, E1NWF is RAZ/WI, DS is RAO/WI,
  568. * ARE is RAO/WI (affinity routing always on), and only
  569. * bits 0 and 1 (group enables) are writable.
  570. */
  571. mask = GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1NS;
  572. } else {
  573. if (attrs.secure) {
  574. /* for secure access:
  575. * ARE_NS and ARE_S are RAO/WI (affinity routing always on)
  576. * E1NWF is RAZ/WI (we don't support enable-1-of-n-wakeup)
  577. *
  578. * We can only modify bits[2:0] (the group enables).
  579. */
  580. mask = GICD_CTLR_DS | GICD_CTLR_EN_GRP0 | GICD_CTLR_EN_GRP1_ALL;
  581. } else {
  582. /* For non secure access ARE_NS is RAO/WI and EnableGrp1
  583. * is RES0. The only writable bit is [1] (EnableGrp1A), which
  584. * is an alias of the Secure bit [1].
  585. */
  586. mask = GICD_CTLR_EN_GRP1NS;
  587. }
  588. }
  589. s->gicd_ctlr = (s->gicd_ctlr & ~mask) | (value & mask);
  590. if (value & mask & GICD_CTLR_DS) {
  591. /* We just set DS, so the ARE_NS and EnG1S bits are now RES0.
  592. * Note that this is a one-way transition because if DS is set
  593. * then it's not writable, so it can only go back to 0 with a
  594. * hardware reset.
  595. */
  596. s->gicd_ctlr &= ~(GICD_CTLR_EN_GRP1S | GICD_CTLR_ARE_NS);
  597. }
  598. gicv3_full_update(s);
  599. return true;
  600. }
  601. case GICD_STATUSR:
  602. /* RAZ/WI for our implementation */
  603. return true;
  604. case GICD_IGROUPR ... GICD_IGROUPR + 0x7f:
  605. {
  606. int irq;
  607. if (!attrs.secure && !(s->gicd_ctlr & GICD_CTLR_DS)) {
  608. return true;
  609. }
  610. /* RAZ/WI for SGIs, PPIs, unimplemented irqs */
  611. irq = (offset - GICD_IGROUPR) * 8;
  612. if (irq < GIC_INTERNAL || irq >= s->num_irq) {
  613. return true;
  614. }
  615. *gic_bmp_ptr32(s->group, irq) = value;
  616. gicv3_update(s, irq, 32);
  617. return true;
  618. }
  619. case GICD_ISENABLER ... GICD_ISENABLER + 0x7f:
  620. gicd_write_set_bitmap_reg(s, attrs, s->enabled, NULL,
  621. offset - GICD_ISENABLER, value);
  622. return true;
  623. case GICD_ICENABLER ... GICD_ICENABLER + 0x7f:
  624. gicd_write_clear_bitmap_reg(s, attrs, s->enabled, NULL,
  625. offset - GICD_ICENABLER, value);
  626. return true;
  627. case GICD_ISPENDR ... GICD_ISPENDR + 0x7f:
  628. gicd_write_set_bitmap_reg(s, attrs, s->pending, mask_nsacr_ge1,
  629. offset - GICD_ISPENDR, value);
  630. return true;
  631. case GICD_ICPENDR ... GICD_ICPENDR + 0x7f:
  632. gicd_write_clear_bitmap_reg(s, attrs, s->pending, mask_nsacr_ge2,
  633. offset - GICD_ICPENDR, value);
  634. return true;
  635. case GICD_ISACTIVER ... GICD_ISACTIVER + 0x7f:
  636. gicd_write_set_bitmap_reg(s, attrs, s->active, NULL,
  637. offset - GICD_ISACTIVER, value);
  638. return true;
  639. case GICD_ICACTIVER ... GICD_ICACTIVER + 0x7f:
  640. gicd_write_clear_bitmap_reg(s, attrs, s->active, NULL,
  641. offset - GICD_ICACTIVER, value);
  642. return true;
  643. case GICD_IPRIORITYR ... GICD_IPRIORITYR + 0x3ff:
  644. {
  645. int i, irq = offset - GICD_IPRIORITYR;
  646. if (irq < GIC_INTERNAL || irq + 3 >= s->num_irq) {
  647. return true;
  648. }
  649. for (i = irq; i < irq + 4; i++, value >>= 8) {
  650. gicd_write_ipriorityr(s, attrs, i, value);
  651. }
  652. gicv3_update(s, irq, 4);
  653. return true;
  654. }
  655. case GICD_ITARGETSR ... GICD_ITARGETSR + 0x3ff:
  656. /* RAZ/WI since affinity routing is always enabled */
  657. return true;
  658. case GICD_ICFGR ... GICD_ICFGR + 0xff:
  659. {
  660. /* Here only the odd bits are used; even bits are RES0 */
  661. int irq = (offset - GICD_ICFGR) * 4;
  662. uint32_t mask, oldval;
  663. if (irq < GIC_INTERNAL || irq >= s->num_irq) {
  664. return true;
  665. }
  666. /* Since our edge_trigger bitmap is one bit per irq, our input
  667. * 32-bits will compress down into 16 bits which we need
  668. * to write into the bitmap.
  669. */
  670. value = half_unshuffle32(value >> 1);
  671. mask = mask_group_and_nsacr(s, attrs, NULL, irq & ~0x1f);
  672. if (irq & 0x1f) {
  673. value <<= 16;
  674. mask &= 0xffff0000U;
  675. } else {
  676. mask &= 0xffff;
  677. }
  678. oldval = *gic_bmp_ptr32(s->edge_trigger, (irq & ~0x1f));
  679. value = (oldval & ~mask) | (value & mask);
  680. *gic_bmp_ptr32(s->edge_trigger, irq & ~0x1f) = value;
  681. return true;
  682. }
  683. case GICD_IGRPMODR ... GICD_IGRPMODR + 0xff:
  684. {
  685. int irq;
  686. if ((s->gicd_ctlr & GICD_CTLR_DS) || !attrs.secure) {
  687. /* RAZ/WI if security disabled, or if
  688. * security enabled and this is an NS access
  689. */
  690. return true;
  691. }
  692. /* RAZ/WI for SGIs, PPIs, unimplemented irqs */
  693. irq = (offset - GICD_IGRPMODR) * 8;
  694. if (irq < GIC_INTERNAL || irq >= s->num_irq) {
  695. return true;
  696. }
  697. *gic_bmp_ptr32(s->grpmod, irq) = value;
  698. gicv3_update(s, irq, 32);
  699. return true;
  700. }
  701. case GICD_NSACR ... GICD_NSACR + 0xff:
  702. {
  703. /* Two bits per interrupt */
  704. int irq = (offset - GICD_NSACR) * 4;
  705. if (irq < GIC_INTERNAL || irq >= s->num_irq) {
  706. return true;
  707. }
  708. if ((s->gicd_ctlr & GICD_CTLR_DS) || !attrs.secure) {
  709. /* RAZ/WI if security disabled, or if
  710. * security enabled and this is an NS access
  711. */
  712. return true;
  713. }
  714. s->gicd_nsacr[irq / 16] = value;
  715. /* No update required as this only affects access permission checks */
  716. return true;
  717. }
  718. case GICD_SGIR:
  719. /* RES0 if affinity routing is enabled */
  720. return true;
  721. case GICD_CPENDSGIR ... GICD_CPENDSGIR + 0xf:
  722. case GICD_SPENDSGIR ... GICD_SPENDSGIR + 0xf:
  723. /* RAZ/WI since affinity routing is always enabled */
  724. return true;
  725. case GICD_INMIR ... GICD_INMIR + 0x7f:
  726. if (s->nmi_support) {
  727. gicd_write_bitmap_reg(s, attrs, s->nmi, NULL,
  728. offset - GICD_INMIR, value);
  729. }
  730. return true;
  731. case GICD_IROUTER ... GICD_IROUTER + 0x1fdf:
  732. {
  733. uint64_t r;
  734. int irq = (offset - GICD_IROUTER) / 8;
  735. if (irq < GIC_INTERNAL || irq >= s->num_irq) {
  736. return true;
  737. }
  738. /* Write half of the 64-bit register */
  739. r = gicd_read_irouter(s, attrs, irq);
  740. r = deposit64(r, (offset & 7) ? 32 : 0, 32, value);
  741. gicd_write_irouter(s, attrs, irq, r);
  742. return true;
  743. }
  744. case GICD_IDREGS ... GICD_IDREGS + 0x2f:
  745. case GICD_TYPER:
  746. case GICD_IIDR:
  747. /* RO registers, ignore the write */
  748. qemu_log_mask(LOG_GUEST_ERROR,
  749. "%s: invalid guest write to RO register at offset "
  750. HWADDR_FMT_plx "\n", __func__, offset);
  751. return true;
  752. default:
  753. return false;
  754. }
  755. }
  756. static bool gicd_writeq(GICv3State *s, hwaddr offset,
  757. uint64_t value, MemTxAttrs attrs)
  758. {
  759. /* Our only 64-bit registers are GICD_IROUTER<n> */
  760. int irq;
  761. switch (offset) {
  762. case GICD_IROUTER ... GICD_IROUTER + 0x1fdf:
  763. irq = (offset - GICD_IROUTER) / 8;
  764. gicd_write_irouter(s, attrs, irq, value);
  765. return true;
  766. default:
  767. return false;
  768. }
  769. }
  770. static bool gicd_readq(GICv3State *s, hwaddr offset,
  771. uint64_t *data, MemTxAttrs attrs)
  772. {
  773. /* Our only 64-bit registers are GICD_IROUTER<n> */
  774. int irq;
  775. switch (offset) {
  776. case GICD_IROUTER ... GICD_IROUTER + 0x1fdf:
  777. irq = (offset - GICD_IROUTER) / 8;
  778. *data = gicd_read_irouter(s, attrs, irq);
  779. return true;
  780. default:
  781. return false;
  782. }
  783. }
  784. MemTxResult gicv3_dist_read(void *opaque, hwaddr offset, uint64_t *data,
  785. unsigned size, MemTxAttrs attrs)
  786. {
  787. GICv3State *s = (GICv3State *)opaque;
  788. bool r;
  789. switch (size) {
  790. case 1:
  791. r = gicd_readb(s, offset, data, attrs);
  792. break;
  793. case 2:
  794. r = gicd_readw(s, offset, data, attrs);
  795. break;
  796. case 4:
  797. r = gicd_readl(s, offset, data, attrs);
  798. break;
  799. case 8:
  800. r = gicd_readq(s, offset, data, attrs);
  801. break;
  802. default:
  803. r = false;
  804. break;
  805. }
  806. if (!r) {
  807. qemu_log_mask(LOG_GUEST_ERROR,
  808. "%s: invalid guest read at offset " HWADDR_FMT_plx
  809. " size %u\n", __func__, offset, size);
  810. trace_gicv3_dist_badread(offset, size, attrs.secure);
  811. /* The spec requires that reserved registers are RAZ/WI;
  812. * so use MEMTX_ERROR returns from leaf functions as a way to
  813. * trigger the guest-error logging but don't return it to
  814. * the caller, or we'll cause a spurious guest data abort.
  815. */
  816. *data = 0;
  817. } else {
  818. trace_gicv3_dist_read(offset, *data, size, attrs.secure);
  819. }
  820. return MEMTX_OK;
  821. }
  822. MemTxResult gicv3_dist_write(void *opaque, hwaddr offset, uint64_t data,
  823. unsigned size, MemTxAttrs attrs)
  824. {
  825. GICv3State *s = (GICv3State *)opaque;
  826. bool r;
  827. switch (size) {
  828. case 1:
  829. r = gicd_writeb(s, offset, data, attrs);
  830. break;
  831. case 2:
  832. r = gicd_writew(s, offset, data, attrs);
  833. break;
  834. case 4:
  835. r = gicd_writel(s, offset, data, attrs);
  836. break;
  837. case 8:
  838. r = gicd_writeq(s, offset, data, attrs);
  839. break;
  840. default:
  841. r = false;
  842. break;
  843. }
  844. if (!r) {
  845. qemu_log_mask(LOG_GUEST_ERROR,
  846. "%s: invalid guest write at offset " HWADDR_FMT_plx
  847. " size %u\n", __func__, offset, size);
  848. trace_gicv3_dist_badwrite(offset, data, size, attrs.secure);
  849. /* The spec requires that reserved registers are RAZ/WI;
  850. * so use MEMTX_ERROR returns from leaf functions as a way to
  851. * trigger the guest-error logging but don't return it to
  852. * the caller, or we'll cause a spurious guest data abort.
  853. */
  854. } else {
  855. trace_gicv3_dist_write(offset, data, size, attrs.secure);
  856. }
  857. return MEMTX_OK;
  858. }
  859. void gicv3_dist_set_irq(GICv3State *s, int irq, int level)
  860. {
  861. /* Update distributor state for a change in an external SPI input line */
  862. if (level == gicv3_gicd_level_test(s, irq)) {
  863. return;
  864. }
  865. trace_gicv3_dist_set_irq(irq, level);
  866. gicv3_gicd_level_replace(s, irq, level);
  867. if (level) {
  868. /* 0->1 edges latch the pending bit for edge-triggered interrupts */
  869. if (gicv3_gicd_edge_trigger_test(s, irq)) {
  870. gicv3_gicd_pending_set(s, irq);
  871. }
  872. }
  873. gicv3_update(s, irq, 1);
  874. }