arm_gicv3_dist.c 30 KB

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