arm_gicv3.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * ARM Generic Interrupt Controller v3 (emulation)
  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. /* This file contains implementation code for an interrupt controller
  12. * which implements the GICv3 architecture. Specifically this is where
  13. * the device class itself and the functions for handling interrupts
  14. * coming in and going out live.
  15. */
  16. #include "qemu/osdep.h"
  17. #include "qapi/error.h"
  18. #include "qemu/module.h"
  19. #include "hw/intc/arm_gicv3.h"
  20. #include "gicv3_internal.h"
  21. static bool irqbetter(GICv3CPUState *cs, int irq, uint8_t prio, bool nmi)
  22. {
  23. /* Return true if this IRQ at this priority should take
  24. * precedence over the current recorded highest priority
  25. * pending interrupt for this CPU. We also return true if
  26. * the current recorded highest priority pending interrupt
  27. * is the same as this one (a property which the calling code
  28. * relies on).
  29. */
  30. if (prio != cs->hppi.prio) {
  31. return prio < cs->hppi.prio;
  32. }
  33. /*
  34. * The same priority IRQ with non-maskable property should signal to
  35. * the CPU as it have the priority higher than the labelled 0x80 or 0x00.
  36. */
  37. if (nmi != cs->hppi.nmi) {
  38. return nmi;
  39. }
  40. /* If multiple pending interrupts have the same priority then it is an
  41. * IMPDEF choice which of them to signal to the CPU. We choose to
  42. * signal the one with the lowest interrupt number.
  43. */
  44. if (irq <= cs->hppi.irq) {
  45. return true;
  46. }
  47. return false;
  48. }
  49. static uint32_t gicd_int_pending(GICv3State *s, int irq)
  50. {
  51. /* Recalculate which distributor interrupts are actually pending
  52. * in the group of 32 interrupts starting at irq (which should be a multiple
  53. * of 32), and return a 32-bit integer which has a bit set for each
  54. * interrupt that is eligible to be signaled to the CPU interface.
  55. *
  56. * An interrupt is pending if:
  57. * + the PENDING latch is set OR it is level triggered and the input is 1
  58. * + its ENABLE bit is set
  59. * + the GICD enable bit for its group is set
  60. * + its ACTIVE bit is not set (otherwise it would be Active+Pending)
  61. * Conveniently we can bulk-calculate this with bitwise operations.
  62. */
  63. uint32_t pend, grpmask;
  64. uint32_t pending = *gic_bmp_ptr32(s->pending, irq);
  65. uint32_t edge_trigger = *gic_bmp_ptr32(s->edge_trigger, irq);
  66. uint32_t level = *gic_bmp_ptr32(s->level, irq);
  67. uint32_t group = *gic_bmp_ptr32(s->group, irq);
  68. uint32_t grpmod = *gic_bmp_ptr32(s->grpmod, irq);
  69. uint32_t enable = *gic_bmp_ptr32(s->enabled, irq);
  70. uint32_t active = *gic_bmp_ptr32(s->active, irq);
  71. pend = pending | (~edge_trigger & level);
  72. pend &= enable;
  73. pend &= ~active;
  74. if (s->gicd_ctlr & GICD_CTLR_DS) {
  75. grpmod = 0;
  76. }
  77. grpmask = 0;
  78. if (s->gicd_ctlr & GICD_CTLR_EN_GRP1NS) {
  79. grpmask |= group;
  80. }
  81. if (s->gicd_ctlr & GICD_CTLR_EN_GRP1S) {
  82. grpmask |= (~group & grpmod);
  83. }
  84. if (s->gicd_ctlr & GICD_CTLR_EN_GRP0) {
  85. grpmask |= (~group & ~grpmod);
  86. }
  87. pend &= grpmask;
  88. return pend;
  89. }
  90. static uint32_t gicr_int_pending(GICv3CPUState *cs)
  91. {
  92. /* Recalculate which redistributor interrupts are actually pending,
  93. * and return a 32-bit integer which has a bit set for each interrupt
  94. * that is eligible to be signaled to the CPU interface.
  95. *
  96. * An interrupt is pending if:
  97. * + the PENDING latch is set OR it is level triggered and the input is 1
  98. * + its ENABLE bit is set
  99. * + the GICD enable bit for its group is set
  100. * + its ACTIVE bit is not set (otherwise it would be Active+Pending)
  101. * Conveniently we can bulk-calculate this with bitwise operations.
  102. */
  103. uint32_t pend, grpmask, grpmod;
  104. pend = cs->gicr_ipendr0 | (~cs->edge_trigger & cs->level);
  105. pend &= cs->gicr_ienabler0;
  106. pend &= ~cs->gicr_iactiver0;
  107. if (cs->gic->gicd_ctlr & GICD_CTLR_DS) {
  108. grpmod = 0;
  109. } else {
  110. grpmod = cs->gicr_igrpmodr0;
  111. }
  112. grpmask = 0;
  113. if (cs->gic->gicd_ctlr & GICD_CTLR_EN_GRP1NS) {
  114. grpmask |= cs->gicr_igroupr0;
  115. }
  116. if (cs->gic->gicd_ctlr & GICD_CTLR_EN_GRP1S) {
  117. grpmask |= (~cs->gicr_igroupr0 & grpmod);
  118. }
  119. if (cs->gic->gicd_ctlr & GICD_CTLR_EN_GRP0) {
  120. grpmask |= (~cs->gicr_igroupr0 & ~grpmod);
  121. }
  122. pend &= grpmask;
  123. return pend;
  124. }
  125. static bool gicv3_get_priority(GICv3CPUState *cs, bool is_redist, int irq,
  126. uint8_t *prio)
  127. {
  128. uint32_t nmi = 0x0;
  129. if (is_redist) {
  130. nmi = extract32(cs->gicr_inmir0, irq, 1);
  131. } else {
  132. nmi = *gic_bmp_ptr32(cs->gic->nmi, irq);
  133. nmi = nmi & (1 << (irq & 0x1f));
  134. }
  135. if (nmi) {
  136. /* DS = 0 & Non-secure NMI */
  137. if (!(cs->gic->gicd_ctlr & GICD_CTLR_DS) &&
  138. ((is_redist && extract32(cs->gicr_igroupr0, irq, 1)) ||
  139. (!is_redist && gicv3_gicd_group_test(cs->gic, irq)))) {
  140. *prio = 0x80;
  141. } else {
  142. *prio = 0x0;
  143. }
  144. return true;
  145. }
  146. if (is_redist) {
  147. *prio = cs->gicr_ipriorityr[irq];
  148. } else {
  149. *prio = cs->gic->gicd_ipriority[irq];
  150. }
  151. return false;
  152. }
  153. /* Update the interrupt status after state in a redistributor
  154. * or CPU interface has changed, but don't tell the CPU i/f.
  155. */
  156. static void gicv3_redist_update_noirqset(GICv3CPUState *cs)
  157. {
  158. /* Find the highest priority pending interrupt among the
  159. * redistributor interrupts (SGIs and PPIs).
  160. */
  161. bool seenbetter = false;
  162. uint8_t prio;
  163. int i;
  164. uint32_t pend;
  165. bool nmi = false;
  166. /* Find out which redistributor interrupts are eligible to be
  167. * signaled to the CPU interface.
  168. */
  169. pend = gicr_int_pending(cs);
  170. if (pend) {
  171. for (i = 0; i < GIC_INTERNAL; i++) {
  172. if (!(pend & (1 << i))) {
  173. continue;
  174. }
  175. nmi = gicv3_get_priority(cs, true, i, &prio);
  176. if (irqbetter(cs, i, prio, nmi)) {
  177. cs->hppi.irq = i;
  178. cs->hppi.prio = prio;
  179. cs->hppi.nmi = nmi;
  180. seenbetter = true;
  181. }
  182. }
  183. }
  184. if (seenbetter) {
  185. cs->hppi.grp = gicv3_irq_group(cs->gic, cs, cs->hppi.irq);
  186. }
  187. if ((cs->gicr_ctlr & GICR_CTLR_ENABLE_LPIS) && cs->gic->lpi_enable &&
  188. (cs->gic->gicd_ctlr & GICD_CTLR_EN_GRP1NS) &&
  189. (cs->hpplpi.prio != 0xff)) {
  190. if (irqbetter(cs, cs->hpplpi.irq, cs->hpplpi.prio, cs->hpplpi.nmi)) {
  191. cs->hppi.irq = cs->hpplpi.irq;
  192. cs->hppi.prio = cs->hpplpi.prio;
  193. cs->hppi.nmi = cs->hpplpi.nmi;
  194. cs->hppi.grp = cs->hpplpi.grp;
  195. seenbetter = true;
  196. }
  197. }
  198. /* If the best interrupt we just found would preempt whatever
  199. * was the previous best interrupt before this update, then
  200. * we know it's definitely the best one now.
  201. * If we didn't find an interrupt that would preempt the previous
  202. * best, and the previous best is outside our range (or there was no
  203. * previous pending interrupt at all), then that is still valid, and
  204. * we leave it as the best.
  205. * Otherwise, we need to do a full update (because the previous best
  206. * interrupt has reduced in priority and any other interrupt could
  207. * now be the new best one).
  208. */
  209. if (!seenbetter && cs->hppi.prio != 0xff &&
  210. (cs->hppi.irq < GIC_INTERNAL ||
  211. cs->hppi.irq >= GICV3_LPI_INTID_START)) {
  212. gicv3_full_update_noirqset(cs->gic);
  213. }
  214. }
  215. /* Update the GIC status after state in a redistributor or
  216. * CPU interface has changed, and inform the CPU i/f of
  217. * its new highest priority pending interrupt.
  218. */
  219. void gicv3_redist_update(GICv3CPUState *cs)
  220. {
  221. gicv3_redist_update_noirqset(cs);
  222. gicv3_cpuif_update(cs);
  223. }
  224. /* Update the GIC status after state in the distributor has
  225. * changed affecting @len interrupts starting at @start,
  226. * but don't tell the CPU i/f.
  227. */
  228. static void gicv3_update_noirqset(GICv3State *s, int start, int len)
  229. {
  230. int i;
  231. uint8_t prio;
  232. uint32_t pend = 0;
  233. bool nmi = false;
  234. assert(start >= GIC_INTERNAL);
  235. assert(len > 0);
  236. for (i = 0; i < s->num_cpu; i++) {
  237. s->cpu[i].seenbetter = false;
  238. }
  239. /* Find the highest priority pending interrupt in this range. */
  240. for (i = start; i < start + len; i++) {
  241. GICv3CPUState *cs;
  242. if (i == start || (i & 0x1f) == 0) {
  243. /* Calculate the next 32 bits worth of pending status */
  244. pend = gicd_int_pending(s, i & ~0x1f);
  245. }
  246. if (!(pend & (1 << (i & 0x1f)))) {
  247. continue;
  248. }
  249. cs = s->gicd_irouter_target[i];
  250. if (!cs) {
  251. /* Interrupts targeting no implemented CPU should remain pending
  252. * and not be forwarded to any CPU.
  253. */
  254. continue;
  255. }
  256. nmi = gicv3_get_priority(cs, false, i, &prio);
  257. if (irqbetter(cs, i, prio, nmi)) {
  258. cs->hppi.irq = i;
  259. cs->hppi.prio = prio;
  260. cs->hppi.nmi = nmi;
  261. cs->seenbetter = true;
  262. }
  263. }
  264. /* If the best interrupt we just found would preempt whatever
  265. * was the previous best interrupt before this update, then
  266. * we know it's definitely the best one now.
  267. * If we didn't find an interrupt that would preempt the previous
  268. * best, and the previous best is outside our range (or there was
  269. * no previous pending interrupt at all), then that
  270. * is still valid, and we leave it as the best.
  271. * Otherwise, we need to do a full update (because the previous best
  272. * interrupt has reduced in priority and any other interrupt could
  273. * now be the new best one).
  274. */
  275. for (i = 0; i < s->num_cpu; i++) {
  276. GICv3CPUState *cs = &s->cpu[i];
  277. if (cs->seenbetter) {
  278. cs->hppi.grp = gicv3_irq_group(cs->gic, cs, cs->hppi.irq);
  279. }
  280. if (!cs->seenbetter && cs->hppi.prio != 0xff &&
  281. cs->hppi.irq >= start && cs->hppi.irq < start + len) {
  282. gicv3_full_update_noirqset(s);
  283. break;
  284. }
  285. }
  286. }
  287. void gicv3_update(GICv3State *s, int start, int len)
  288. {
  289. int i;
  290. gicv3_update_noirqset(s, start, len);
  291. for (i = 0; i < s->num_cpu; i++) {
  292. gicv3_cpuif_update(&s->cpu[i]);
  293. }
  294. }
  295. void gicv3_full_update_noirqset(GICv3State *s)
  296. {
  297. /* Completely recalculate the GIC status from scratch, but
  298. * don't update any outbound IRQ lines.
  299. */
  300. int i;
  301. for (i = 0; i < s->num_cpu; i++) {
  302. s->cpu[i].hppi.prio = 0xff;
  303. s->cpu[i].hppi.nmi = false;
  304. }
  305. /* Note that we can guarantee that these functions will not
  306. * recursively call back into gicv3_full_update(), because
  307. * at each point the "previous best" is always outside the
  308. * range we ask them to update.
  309. */
  310. gicv3_update_noirqset(s, GIC_INTERNAL, s->num_irq - GIC_INTERNAL);
  311. for (i = 0; i < s->num_cpu; i++) {
  312. gicv3_redist_update_noirqset(&s->cpu[i]);
  313. }
  314. }
  315. void gicv3_full_update(GICv3State *s)
  316. {
  317. /* Completely recalculate the GIC status from scratch, including
  318. * updating outbound IRQ lines.
  319. */
  320. int i;
  321. gicv3_full_update_noirqset(s);
  322. for (i = 0; i < s->num_cpu; i++) {
  323. gicv3_cpuif_update(&s->cpu[i]);
  324. }
  325. }
  326. /* Process a change in an external IRQ input. */
  327. static void gicv3_set_irq(void *opaque, int irq, int level)
  328. {
  329. /* Meaning of the 'irq' parameter:
  330. * [0..N-1] : external interrupts
  331. * [N..N+31] : PPI (internal) interrupts for CPU 0
  332. * [N+32..N+63] : PPI (internal interrupts for CPU 1
  333. * ...
  334. */
  335. GICv3State *s = opaque;
  336. if (irq < (s->num_irq - GIC_INTERNAL)) {
  337. /* external interrupt (SPI) */
  338. gicv3_dist_set_irq(s, irq + GIC_INTERNAL, level);
  339. } else {
  340. /* per-cpu interrupt (PPI) */
  341. int cpu;
  342. irq -= (s->num_irq - GIC_INTERNAL);
  343. cpu = irq / GIC_INTERNAL;
  344. irq %= GIC_INTERNAL;
  345. assert(cpu < s->num_cpu);
  346. /* Raising SGIs via this function would be a bug in how the board
  347. * model wires up interrupts.
  348. */
  349. assert(irq >= GIC_NR_SGIS);
  350. gicv3_redist_set_irq(&s->cpu[cpu], irq, level);
  351. }
  352. }
  353. static void arm_gicv3_post_load(GICv3State *s)
  354. {
  355. int i;
  356. /* Recalculate our cached idea of the current highest priority
  357. * pending interrupt, but don't set IRQ or FIQ lines.
  358. */
  359. for (i = 0; i < s->num_cpu; i++) {
  360. gicv3_redist_update_lpi_only(&s->cpu[i]);
  361. }
  362. gicv3_full_update_noirqset(s);
  363. /* Repopulate the cache of GICv3CPUState pointers for target CPUs */
  364. gicv3_cache_all_target_cpustates(s);
  365. }
  366. static const MemoryRegionOps gic_ops[] = {
  367. {
  368. .read_with_attrs = gicv3_dist_read,
  369. .write_with_attrs = gicv3_dist_write,
  370. .endianness = DEVICE_NATIVE_ENDIAN,
  371. .valid.min_access_size = 1,
  372. .valid.max_access_size = 8,
  373. .impl.min_access_size = 1,
  374. .impl.max_access_size = 8,
  375. },
  376. {
  377. .read_with_attrs = gicv3_redist_read,
  378. .write_with_attrs = gicv3_redist_write,
  379. .endianness = DEVICE_NATIVE_ENDIAN,
  380. .valid.min_access_size = 1,
  381. .valid.max_access_size = 8,
  382. .impl.min_access_size = 1,
  383. .impl.max_access_size = 8,
  384. }
  385. };
  386. static void arm_gic_realize(DeviceState *dev, Error **errp)
  387. {
  388. /* Device instance realize function for the GIC sysbus device */
  389. GICv3State *s = ARM_GICV3(dev);
  390. ARMGICv3Class *agc = ARM_GICV3_GET_CLASS(s);
  391. Error *local_err = NULL;
  392. agc->parent_realize(dev, &local_err);
  393. if (local_err) {
  394. error_propagate(errp, local_err);
  395. return;
  396. }
  397. gicv3_init_irqs_and_mmio(s, gicv3_set_irq, gic_ops);
  398. gicv3_init_cpuif(s);
  399. }
  400. static void arm_gicv3_class_init(ObjectClass *klass, void *data)
  401. {
  402. DeviceClass *dc = DEVICE_CLASS(klass);
  403. ARMGICv3CommonClass *agcc = ARM_GICV3_COMMON_CLASS(klass);
  404. ARMGICv3Class *agc = ARM_GICV3_CLASS(klass);
  405. agcc->post_load = arm_gicv3_post_load;
  406. device_class_set_parent_realize(dc, arm_gic_realize, &agc->parent_realize);
  407. }
  408. static const TypeInfo arm_gicv3_info = {
  409. .name = TYPE_ARM_GICV3,
  410. .parent = TYPE_ARM_GICV3_COMMON,
  411. .instance_size = sizeof(GICv3State),
  412. .class_init = arm_gicv3_class_init,
  413. .class_size = sizeof(ARMGICv3Class),
  414. };
  415. static void arm_gicv3_register_types(void)
  416. {
  417. type_register_static(&arm_gicv3_info);
  418. }
  419. type_init(arm_gicv3_register_types)