arm_gicv3.c 12 KB

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