arm_gic.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /*
  2. * ARM Generic/Distributed Interrupt Controller
  3. *
  4. * Copyright (c) 2006-2007 CodeSourcery.
  5. * Written by Paul Brook
  6. *
  7. * This code is licensed under the GPL.
  8. */
  9. /* This file contains implementation code for the RealView EB interrupt
  10. * controller, MPCore distributed interrupt controller and ARMv7-M
  11. * Nested Vectored Interrupt Controller.
  12. * It is compiled in two ways:
  13. * (1) as a standalone file to produce a sysbus device which is a GIC
  14. * that can be used on the realview board and as one of the builtin
  15. * private peripherals for the ARM MP CPUs (11MPCore, A9, etc)
  16. * (2) by being directly #included into armv7m_nvic.c to produce the
  17. * armv7m_nvic device.
  18. */
  19. #include "hw/sysbus.h"
  20. #include "gic_internal.h"
  21. #include "qom/cpu.h"
  22. //#define DEBUG_GIC
  23. #ifdef DEBUG_GIC
  24. #define DPRINTF(fmt, ...) \
  25. do { fprintf(stderr, "arm_gic: " fmt , ## __VA_ARGS__); } while (0)
  26. #else
  27. #define DPRINTF(fmt, ...) do {} while(0)
  28. #endif
  29. static const uint8_t gic_id[] = {
  30. 0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1
  31. };
  32. #define NUM_CPU(s) ((s)->num_cpu)
  33. static inline int gic_get_current_cpu(GICState *s)
  34. {
  35. if (s->num_cpu > 1) {
  36. return current_cpu->cpu_index;
  37. }
  38. return 0;
  39. }
  40. /* TODO: Many places that call this routine could be optimized. */
  41. /* Update interrupt status after enabled or pending bits have been changed. */
  42. void gic_update(GICState *s)
  43. {
  44. int best_irq;
  45. int best_prio;
  46. int irq;
  47. int level;
  48. int cpu;
  49. int cm;
  50. for (cpu = 0; cpu < NUM_CPU(s); cpu++) {
  51. cm = 1 << cpu;
  52. s->current_pending[cpu] = 1023;
  53. if (!s->enabled || !s->cpu_enabled[cpu]) {
  54. qemu_irq_lower(s->parent_irq[cpu]);
  55. return;
  56. }
  57. best_prio = 0x100;
  58. best_irq = 1023;
  59. for (irq = 0; irq < s->num_irq; irq++) {
  60. if (GIC_TEST_ENABLED(irq, cm) && gic_test_pending(s, irq, cm)) {
  61. if (GIC_GET_PRIORITY(irq, cpu) < best_prio) {
  62. best_prio = GIC_GET_PRIORITY(irq, cpu);
  63. best_irq = irq;
  64. }
  65. }
  66. }
  67. level = 0;
  68. if (best_prio < s->priority_mask[cpu]) {
  69. s->current_pending[cpu] = best_irq;
  70. if (best_prio < s->running_priority[cpu]) {
  71. DPRINTF("Raised pending IRQ %d (cpu %d)\n", best_irq, cpu);
  72. level = 1;
  73. }
  74. }
  75. qemu_set_irq(s->parent_irq[cpu], level);
  76. }
  77. }
  78. void gic_set_pending_private(GICState *s, int cpu, int irq)
  79. {
  80. int cm = 1 << cpu;
  81. if (gic_test_pending(s, irq, cm)) {
  82. return;
  83. }
  84. DPRINTF("Set %d pending cpu %d\n", irq, cpu);
  85. GIC_SET_PENDING(irq, cm);
  86. gic_update(s);
  87. }
  88. static void gic_set_irq_11mpcore(GICState *s, int irq, int level,
  89. int cm, int target)
  90. {
  91. if (level) {
  92. GIC_SET_LEVEL(irq, cm);
  93. if (GIC_TEST_EDGE_TRIGGER(irq) || GIC_TEST_ENABLED(irq, cm)) {
  94. DPRINTF("Set %d pending mask %x\n", irq, target);
  95. GIC_SET_PENDING(irq, target);
  96. }
  97. } else {
  98. GIC_CLEAR_LEVEL(irq, cm);
  99. }
  100. }
  101. static void gic_set_irq_generic(GICState *s, int irq, int level,
  102. int cm, int target)
  103. {
  104. if (level) {
  105. GIC_SET_LEVEL(irq, cm);
  106. DPRINTF("Set %d pending mask %x\n", irq, target);
  107. if (GIC_TEST_EDGE_TRIGGER(irq)) {
  108. GIC_SET_PENDING(irq, target);
  109. }
  110. } else {
  111. GIC_CLEAR_LEVEL(irq, cm);
  112. }
  113. }
  114. /* Process a change in an external IRQ input. */
  115. static void gic_set_irq(void *opaque, int irq, int level)
  116. {
  117. /* Meaning of the 'irq' parameter:
  118. * [0..N-1] : external interrupts
  119. * [N..N+31] : PPI (internal) interrupts for CPU 0
  120. * [N+32..N+63] : PPI (internal interrupts for CPU 1
  121. * ...
  122. */
  123. GICState *s = (GICState *)opaque;
  124. int cm, target;
  125. if (irq < (s->num_irq - GIC_INTERNAL)) {
  126. /* The first external input line is internal interrupt 32. */
  127. cm = ALL_CPU_MASK;
  128. irq += GIC_INTERNAL;
  129. target = GIC_TARGET(irq);
  130. } else {
  131. int cpu;
  132. irq -= (s->num_irq - GIC_INTERNAL);
  133. cpu = irq / GIC_INTERNAL;
  134. irq %= GIC_INTERNAL;
  135. cm = 1 << cpu;
  136. target = cm;
  137. }
  138. assert(irq >= GIC_NR_SGIS);
  139. if (level == GIC_TEST_LEVEL(irq, cm)) {
  140. return;
  141. }
  142. if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
  143. gic_set_irq_11mpcore(s, irq, level, cm, target);
  144. } else {
  145. gic_set_irq_generic(s, irq, level, cm, target);
  146. }
  147. gic_update(s);
  148. }
  149. static void gic_set_running_irq(GICState *s, int cpu, int irq)
  150. {
  151. s->running_irq[cpu] = irq;
  152. if (irq == 1023) {
  153. s->running_priority[cpu] = 0x100;
  154. } else {
  155. s->running_priority[cpu] = GIC_GET_PRIORITY(irq, cpu);
  156. }
  157. gic_update(s);
  158. }
  159. uint32_t gic_acknowledge_irq(GICState *s, int cpu)
  160. {
  161. int ret, irq, src;
  162. int cm = 1 << cpu;
  163. irq = s->current_pending[cpu];
  164. if (irq == 1023
  165. || GIC_GET_PRIORITY(irq, cpu) >= s->running_priority[cpu]) {
  166. DPRINTF("ACK no pending IRQ\n");
  167. return 1023;
  168. }
  169. s->last_active[irq][cpu] = s->running_irq[cpu];
  170. if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
  171. /* Clear pending flags for both level and edge triggered interrupts.
  172. * Level triggered IRQs will be reasserted once they become inactive.
  173. */
  174. GIC_CLEAR_PENDING(irq, GIC_TEST_MODEL(irq) ? ALL_CPU_MASK : cm);
  175. ret = irq;
  176. } else {
  177. if (irq < GIC_NR_SGIS) {
  178. /* Lookup the source CPU for the SGI and clear this in the
  179. * sgi_pending map. Return the src and clear the overall pending
  180. * state on this CPU if the SGI is not pending from any CPUs.
  181. */
  182. assert(s->sgi_pending[irq][cpu] != 0);
  183. src = ctz32(s->sgi_pending[irq][cpu]);
  184. s->sgi_pending[irq][cpu] &= ~(1 << src);
  185. if (s->sgi_pending[irq][cpu] == 0) {
  186. GIC_CLEAR_PENDING(irq, GIC_TEST_MODEL(irq) ? ALL_CPU_MASK : cm);
  187. }
  188. ret = irq | ((src & 0x7) << 10);
  189. } else {
  190. /* Clear pending state for both level and edge triggered
  191. * interrupts. (level triggered interrupts with an active line
  192. * remain pending, see gic_test_pending)
  193. */
  194. GIC_CLEAR_PENDING(irq, GIC_TEST_MODEL(irq) ? ALL_CPU_MASK : cm);
  195. ret = irq;
  196. }
  197. }
  198. gic_set_running_irq(s, cpu, irq);
  199. DPRINTF("ACK %d\n", irq);
  200. return ret;
  201. }
  202. void gic_set_priority(GICState *s, int cpu, int irq, uint8_t val)
  203. {
  204. if (irq < GIC_INTERNAL) {
  205. s->priority1[irq][cpu] = val;
  206. } else {
  207. s->priority2[(irq) - GIC_INTERNAL] = val;
  208. }
  209. }
  210. void gic_complete_irq(GICState *s, int cpu, int irq)
  211. {
  212. int update = 0;
  213. int cm = 1 << cpu;
  214. DPRINTF("EOI %d\n", irq);
  215. if (irq >= s->num_irq) {
  216. /* This handles two cases:
  217. * 1. If software writes the ID of a spurious interrupt [ie 1023]
  218. * to the GICC_EOIR, the GIC ignores that write.
  219. * 2. If software writes the number of a non-existent interrupt
  220. * this must be a subcase of "value written does not match the last
  221. * valid interrupt value read from the Interrupt Acknowledge
  222. * register" and so this is UNPREDICTABLE. We choose to ignore it.
  223. */
  224. return;
  225. }
  226. if (s->running_irq[cpu] == 1023)
  227. return; /* No active IRQ. */
  228. if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
  229. /* Mark level triggered interrupts as pending if they are still
  230. raised. */
  231. if (!GIC_TEST_EDGE_TRIGGER(irq) && GIC_TEST_ENABLED(irq, cm)
  232. && GIC_TEST_LEVEL(irq, cm) && (GIC_TARGET(irq) & cm) != 0) {
  233. DPRINTF("Set %d pending mask %x\n", irq, cm);
  234. GIC_SET_PENDING(irq, cm);
  235. update = 1;
  236. }
  237. }
  238. if (irq != s->running_irq[cpu]) {
  239. /* Complete an IRQ that is not currently running. */
  240. int tmp = s->running_irq[cpu];
  241. while (s->last_active[tmp][cpu] != 1023) {
  242. if (s->last_active[tmp][cpu] == irq) {
  243. s->last_active[tmp][cpu] = s->last_active[irq][cpu];
  244. break;
  245. }
  246. tmp = s->last_active[tmp][cpu];
  247. }
  248. if (update) {
  249. gic_update(s);
  250. }
  251. } else {
  252. /* Complete the current running IRQ. */
  253. gic_set_running_irq(s, cpu, s->last_active[s->running_irq[cpu]][cpu]);
  254. }
  255. }
  256. static uint32_t gic_dist_readb(void *opaque, hwaddr offset)
  257. {
  258. GICState *s = (GICState *)opaque;
  259. uint32_t res;
  260. int irq;
  261. int i;
  262. int cpu;
  263. int cm;
  264. int mask;
  265. cpu = gic_get_current_cpu(s);
  266. cm = 1 << cpu;
  267. if (offset < 0x100) {
  268. if (offset == 0)
  269. return s->enabled;
  270. if (offset == 4)
  271. return ((s->num_irq / 32) - 1) | ((NUM_CPU(s) - 1) << 5);
  272. if (offset < 0x08)
  273. return 0;
  274. if (offset >= 0x80) {
  275. /* Interrupt Security , RAZ/WI */
  276. return 0;
  277. }
  278. goto bad_reg;
  279. } else if (offset < 0x200) {
  280. /* Interrupt Set/Clear Enable. */
  281. if (offset < 0x180)
  282. irq = (offset - 0x100) * 8;
  283. else
  284. irq = (offset - 0x180) * 8;
  285. irq += GIC_BASE_IRQ;
  286. if (irq >= s->num_irq)
  287. goto bad_reg;
  288. res = 0;
  289. for (i = 0; i < 8; i++) {
  290. if (GIC_TEST_ENABLED(irq + i, cm)) {
  291. res |= (1 << i);
  292. }
  293. }
  294. } else if (offset < 0x300) {
  295. /* Interrupt Set/Clear Pending. */
  296. if (offset < 0x280)
  297. irq = (offset - 0x200) * 8;
  298. else
  299. irq = (offset - 0x280) * 8;
  300. irq += GIC_BASE_IRQ;
  301. if (irq >= s->num_irq)
  302. goto bad_reg;
  303. res = 0;
  304. mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK;
  305. for (i = 0; i < 8; i++) {
  306. if (gic_test_pending(s, irq + i, mask)) {
  307. res |= (1 << i);
  308. }
  309. }
  310. } else if (offset < 0x400) {
  311. /* Interrupt Active. */
  312. irq = (offset - 0x300) * 8 + GIC_BASE_IRQ;
  313. if (irq >= s->num_irq)
  314. goto bad_reg;
  315. res = 0;
  316. mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK;
  317. for (i = 0; i < 8; i++) {
  318. if (GIC_TEST_ACTIVE(irq + i, mask)) {
  319. res |= (1 << i);
  320. }
  321. }
  322. } else if (offset < 0x800) {
  323. /* Interrupt Priority. */
  324. irq = (offset - 0x400) + GIC_BASE_IRQ;
  325. if (irq >= s->num_irq)
  326. goto bad_reg;
  327. res = GIC_GET_PRIORITY(irq, cpu);
  328. } else if (offset < 0xc00) {
  329. /* Interrupt CPU Target. */
  330. if (s->num_cpu == 1 && s->revision != REV_11MPCORE) {
  331. /* For uniprocessor GICs these RAZ/WI */
  332. res = 0;
  333. } else {
  334. irq = (offset - 0x800) + GIC_BASE_IRQ;
  335. if (irq >= s->num_irq) {
  336. goto bad_reg;
  337. }
  338. if (irq >= 29 && irq <= 31) {
  339. res = cm;
  340. } else {
  341. res = GIC_TARGET(irq);
  342. }
  343. }
  344. } else if (offset < 0xf00) {
  345. /* Interrupt Configuration. */
  346. irq = (offset - 0xc00) * 2 + GIC_BASE_IRQ;
  347. if (irq >= s->num_irq)
  348. goto bad_reg;
  349. res = 0;
  350. for (i = 0; i < 4; i++) {
  351. if (GIC_TEST_MODEL(irq + i))
  352. res |= (1 << (i * 2));
  353. if (GIC_TEST_EDGE_TRIGGER(irq + i))
  354. res |= (2 << (i * 2));
  355. }
  356. } else if (offset < 0xf10) {
  357. goto bad_reg;
  358. } else if (offset < 0xf30) {
  359. if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
  360. goto bad_reg;
  361. }
  362. if (offset < 0xf20) {
  363. /* GICD_CPENDSGIRn */
  364. irq = (offset - 0xf10);
  365. } else {
  366. irq = (offset - 0xf20);
  367. /* GICD_SPENDSGIRn */
  368. }
  369. res = s->sgi_pending[irq][cpu];
  370. } else if (offset < 0xfe0) {
  371. goto bad_reg;
  372. } else /* offset >= 0xfe0 */ {
  373. if (offset & 3) {
  374. res = 0;
  375. } else {
  376. res = gic_id[(offset - 0xfe0) >> 2];
  377. }
  378. }
  379. return res;
  380. bad_reg:
  381. qemu_log_mask(LOG_GUEST_ERROR,
  382. "gic_dist_readb: Bad offset %x\n", (int)offset);
  383. return 0;
  384. }
  385. static uint32_t gic_dist_readw(void *opaque, hwaddr offset)
  386. {
  387. uint32_t val;
  388. val = gic_dist_readb(opaque, offset);
  389. val |= gic_dist_readb(opaque, offset + 1) << 8;
  390. return val;
  391. }
  392. static uint32_t gic_dist_readl(void *opaque, hwaddr offset)
  393. {
  394. uint32_t val;
  395. val = gic_dist_readw(opaque, offset);
  396. val |= gic_dist_readw(opaque, offset + 2) << 16;
  397. return val;
  398. }
  399. static void gic_dist_writeb(void *opaque, hwaddr offset,
  400. uint32_t value)
  401. {
  402. GICState *s = (GICState *)opaque;
  403. int irq;
  404. int i;
  405. int cpu;
  406. cpu = gic_get_current_cpu(s);
  407. if (offset < 0x100) {
  408. if (offset == 0) {
  409. s->enabled = (value & 1);
  410. DPRINTF("Distribution %sabled\n", s->enabled ? "En" : "Dis");
  411. } else if (offset < 4) {
  412. /* ignored. */
  413. } else if (offset >= 0x80) {
  414. /* Interrupt Security Registers, RAZ/WI */
  415. } else {
  416. goto bad_reg;
  417. }
  418. } else if (offset < 0x180) {
  419. /* Interrupt Set Enable. */
  420. irq = (offset - 0x100) * 8 + GIC_BASE_IRQ;
  421. if (irq >= s->num_irq)
  422. goto bad_reg;
  423. if (irq < GIC_NR_SGIS) {
  424. value = 0xff;
  425. }
  426. for (i = 0; i < 8; i++) {
  427. if (value & (1 << i)) {
  428. int mask =
  429. (irq < GIC_INTERNAL) ? (1 << cpu) : GIC_TARGET(irq + i);
  430. int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
  431. if (!GIC_TEST_ENABLED(irq + i, cm)) {
  432. DPRINTF("Enabled IRQ %d\n", irq + i);
  433. }
  434. GIC_SET_ENABLED(irq + i, cm);
  435. /* If a raised level triggered IRQ enabled then mark
  436. is as pending. */
  437. if (GIC_TEST_LEVEL(irq + i, mask)
  438. && !GIC_TEST_EDGE_TRIGGER(irq + i)) {
  439. DPRINTF("Set %d pending mask %x\n", irq + i, mask);
  440. GIC_SET_PENDING(irq + i, mask);
  441. }
  442. }
  443. }
  444. } else if (offset < 0x200) {
  445. /* Interrupt Clear Enable. */
  446. irq = (offset - 0x180) * 8 + GIC_BASE_IRQ;
  447. if (irq >= s->num_irq)
  448. goto bad_reg;
  449. if (irq < GIC_NR_SGIS) {
  450. value = 0;
  451. }
  452. for (i = 0; i < 8; i++) {
  453. if (value & (1 << i)) {
  454. int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
  455. if (GIC_TEST_ENABLED(irq + i, cm)) {
  456. DPRINTF("Disabled IRQ %d\n", irq + i);
  457. }
  458. GIC_CLEAR_ENABLED(irq + i, cm);
  459. }
  460. }
  461. } else if (offset < 0x280) {
  462. /* Interrupt Set Pending. */
  463. irq = (offset - 0x200) * 8 + GIC_BASE_IRQ;
  464. if (irq >= s->num_irq)
  465. goto bad_reg;
  466. if (irq < GIC_NR_SGIS) {
  467. value = 0;
  468. }
  469. for (i = 0; i < 8; i++) {
  470. if (value & (1 << i)) {
  471. GIC_SET_PENDING(irq + i, GIC_TARGET(irq + i));
  472. }
  473. }
  474. } else if (offset < 0x300) {
  475. /* Interrupt Clear Pending. */
  476. irq = (offset - 0x280) * 8 + GIC_BASE_IRQ;
  477. if (irq >= s->num_irq)
  478. goto bad_reg;
  479. if (irq < GIC_NR_SGIS) {
  480. value = 0;
  481. }
  482. for (i = 0; i < 8; i++) {
  483. /* ??? This currently clears the pending bit for all CPUs, even
  484. for per-CPU interrupts. It's unclear whether this is the
  485. corect behavior. */
  486. if (value & (1 << i)) {
  487. GIC_CLEAR_PENDING(irq + i, ALL_CPU_MASK);
  488. }
  489. }
  490. } else if (offset < 0x400) {
  491. /* Interrupt Active. */
  492. goto bad_reg;
  493. } else if (offset < 0x800) {
  494. /* Interrupt Priority. */
  495. irq = (offset - 0x400) + GIC_BASE_IRQ;
  496. if (irq >= s->num_irq)
  497. goto bad_reg;
  498. gic_set_priority(s, cpu, irq, value);
  499. } else if (offset < 0xc00) {
  500. /* Interrupt CPU Target. RAZ/WI on uniprocessor GICs, with the
  501. * annoying exception of the 11MPCore's GIC.
  502. */
  503. if (s->num_cpu != 1 || s->revision == REV_11MPCORE) {
  504. irq = (offset - 0x800) + GIC_BASE_IRQ;
  505. if (irq >= s->num_irq) {
  506. goto bad_reg;
  507. }
  508. if (irq < 29) {
  509. value = 0;
  510. } else if (irq < GIC_INTERNAL) {
  511. value = ALL_CPU_MASK;
  512. }
  513. s->irq_target[irq] = value & ALL_CPU_MASK;
  514. }
  515. } else if (offset < 0xf00) {
  516. /* Interrupt Configuration. */
  517. irq = (offset - 0xc00) * 4 + GIC_BASE_IRQ;
  518. if (irq >= s->num_irq)
  519. goto bad_reg;
  520. if (irq < GIC_INTERNAL)
  521. value |= 0xaa;
  522. for (i = 0; i < 4; i++) {
  523. if (value & (1 << (i * 2))) {
  524. GIC_SET_MODEL(irq + i);
  525. } else {
  526. GIC_CLEAR_MODEL(irq + i);
  527. }
  528. if (value & (2 << (i * 2))) {
  529. GIC_SET_EDGE_TRIGGER(irq + i);
  530. } else {
  531. GIC_CLEAR_EDGE_TRIGGER(irq + i);
  532. }
  533. }
  534. } else if (offset < 0xf10) {
  535. /* 0xf00 is only handled for 32-bit writes. */
  536. goto bad_reg;
  537. } else if (offset < 0xf20) {
  538. /* GICD_CPENDSGIRn */
  539. if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
  540. goto bad_reg;
  541. }
  542. irq = (offset - 0xf10);
  543. s->sgi_pending[irq][cpu] &= ~value;
  544. if (s->sgi_pending[irq][cpu] == 0) {
  545. GIC_CLEAR_PENDING(irq, 1 << cpu);
  546. }
  547. } else if (offset < 0xf30) {
  548. /* GICD_SPENDSGIRn */
  549. if (s->revision == REV_11MPCORE || s->revision == REV_NVIC) {
  550. goto bad_reg;
  551. }
  552. irq = (offset - 0xf20);
  553. GIC_SET_PENDING(irq, 1 << cpu);
  554. s->sgi_pending[irq][cpu] |= value;
  555. } else {
  556. goto bad_reg;
  557. }
  558. gic_update(s);
  559. return;
  560. bad_reg:
  561. qemu_log_mask(LOG_GUEST_ERROR,
  562. "gic_dist_writeb: Bad offset %x\n", (int)offset);
  563. }
  564. static void gic_dist_writew(void *opaque, hwaddr offset,
  565. uint32_t value)
  566. {
  567. gic_dist_writeb(opaque, offset, value & 0xff);
  568. gic_dist_writeb(opaque, offset + 1, value >> 8);
  569. }
  570. static void gic_dist_writel(void *opaque, hwaddr offset,
  571. uint32_t value)
  572. {
  573. GICState *s = (GICState *)opaque;
  574. if (offset == 0xf00) {
  575. int cpu;
  576. int irq;
  577. int mask;
  578. int target_cpu;
  579. cpu = gic_get_current_cpu(s);
  580. irq = value & 0x3ff;
  581. switch ((value >> 24) & 3) {
  582. case 0:
  583. mask = (value >> 16) & ALL_CPU_MASK;
  584. break;
  585. case 1:
  586. mask = ALL_CPU_MASK ^ (1 << cpu);
  587. break;
  588. case 2:
  589. mask = 1 << cpu;
  590. break;
  591. default:
  592. DPRINTF("Bad Soft Int target filter\n");
  593. mask = ALL_CPU_MASK;
  594. break;
  595. }
  596. GIC_SET_PENDING(irq, mask);
  597. target_cpu = ctz32(mask);
  598. while (target_cpu < GIC_NCPU) {
  599. s->sgi_pending[irq][target_cpu] |= (1 << cpu);
  600. mask &= ~(1 << target_cpu);
  601. target_cpu = ctz32(mask);
  602. }
  603. gic_update(s);
  604. return;
  605. }
  606. gic_dist_writew(opaque, offset, value & 0xffff);
  607. gic_dist_writew(opaque, offset + 2, value >> 16);
  608. }
  609. static const MemoryRegionOps gic_dist_ops = {
  610. .old_mmio = {
  611. .read = { gic_dist_readb, gic_dist_readw, gic_dist_readl, },
  612. .write = { gic_dist_writeb, gic_dist_writew, gic_dist_writel, },
  613. },
  614. .endianness = DEVICE_NATIVE_ENDIAN,
  615. };
  616. static uint32_t gic_cpu_read(GICState *s, int cpu, int offset)
  617. {
  618. switch (offset) {
  619. case 0x00: /* Control */
  620. return s->cpu_enabled[cpu];
  621. case 0x04: /* Priority mask */
  622. return s->priority_mask[cpu];
  623. case 0x08: /* Binary Point */
  624. return s->bpr[cpu];
  625. case 0x0c: /* Acknowledge */
  626. return gic_acknowledge_irq(s, cpu);
  627. case 0x14: /* Running Priority */
  628. return s->running_priority[cpu];
  629. case 0x18: /* Highest Pending Interrupt */
  630. return s->current_pending[cpu];
  631. case 0x1c: /* Aliased Binary Point */
  632. return s->abpr[cpu];
  633. case 0xd0: case 0xd4: case 0xd8: case 0xdc:
  634. return s->apr[(offset - 0xd0) / 4][cpu];
  635. default:
  636. qemu_log_mask(LOG_GUEST_ERROR,
  637. "gic_cpu_read: Bad offset %x\n", (int)offset);
  638. return 0;
  639. }
  640. }
  641. static void gic_cpu_write(GICState *s, int cpu, int offset, uint32_t value)
  642. {
  643. switch (offset) {
  644. case 0x00: /* Control */
  645. s->cpu_enabled[cpu] = (value & 1);
  646. DPRINTF("CPU %d %sabled\n", cpu, s->cpu_enabled[cpu] ? "En" : "Dis");
  647. break;
  648. case 0x04: /* Priority mask */
  649. s->priority_mask[cpu] = (value & 0xff);
  650. break;
  651. case 0x08: /* Binary Point */
  652. s->bpr[cpu] = (value & 0x7);
  653. break;
  654. case 0x10: /* End Of Interrupt */
  655. return gic_complete_irq(s, cpu, value & 0x3ff);
  656. case 0x1c: /* Aliased Binary Point */
  657. if (s->revision >= 2) {
  658. s->abpr[cpu] = (value & 0x7);
  659. }
  660. break;
  661. case 0xd0: case 0xd4: case 0xd8: case 0xdc:
  662. qemu_log_mask(LOG_UNIMP, "Writing APR not implemented\n");
  663. break;
  664. default:
  665. qemu_log_mask(LOG_GUEST_ERROR,
  666. "gic_cpu_write: Bad offset %x\n", (int)offset);
  667. return;
  668. }
  669. gic_update(s);
  670. }
  671. /* Wrappers to read/write the GIC CPU interface for the current CPU */
  672. static uint64_t gic_thiscpu_read(void *opaque, hwaddr addr,
  673. unsigned size)
  674. {
  675. GICState *s = (GICState *)opaque;
  676. return gic_cpu_read(s, gic_get_current_cpu(s), addr);
  677. }
  678. static void gic_thiscpu_write(void *opaque, hwaddr addr,
  679. uint64_t value, unsigned size)
  680. {
  681. GICState *s = (GICState *)opaque;
  682. gic_cpu_write(s, gic_get_current_cpu(s), addr, value);
  683. }
  684. /* Wrappers to read/write the GIC CPU interface for a specific CPU.
  685. * These just decode the opaque pointer into GICState* + cpu id.
  686. */
  687. static uint64_t gic_do_cpu_read(void *opaque, hwaddr addr,
  688. unsigned size)
  689. {
  690. GICState **backref = (GICState **)opaque;
  691. GICState *s = *backref;
  692. int id = (backref - s->backref);
  693. return gic_cpu_read(s, id, addr);
  694. }
  695. static void gic_do_cpu_write(void *opaque, hwaddr addr,
  696. uint64_t value, unsigned size)
  697. {
  698. GICState **backref = (GICState **)opaque;
  699. GICState *s = *backref;
  700. int id = (backref - s->backref);
  701. gic_cpu_write(s, id, addr, value);
  702. }
  703. static const MemoryRegionOps gic_thiscpu_ops = {
  704. .read = gic_thiscpu_read,
  705. .write = gic_thiscpu_write,
  706. .endianness = DEVICE_NATIVE_ENDIAN,
  707. };
  708. static const MemoryRegionOps gic_cpu_ops = {
  709. .read = gic_do_cpu_read,
  710. .write = gic_do_cpu_write,
  711. .endianness = DEVICE_NATIVE_ENDIAN,
  712. };
  713. void gic_init_irqs_and_distributor(GICState *s, int num_irq)
  714. {
  715. SysBusDevice *sbd = SYS_BUS_DEVICE(s);
  716. int i;
  717. i = s->num_irq - GIC_INTERNAL;
  718. /* For the GIC, also expose incoming GPIO lines for PPIs for each CPU.
  719. * GPIO array layout is thus:
  720. * [0..N-1] SPIs
  721. * [N..N+31] PPIs for CPU 0
  722. * [N+32..N+63] PPIs for CPU 1
  723. * ...
  724. */
  725. if (s->revision != REV_NVIC) {
  726. i += (GIC_INTERNAL * s->num_cpu);
  727. }
  728. qdev_init_gpio_in(DEVICE(s), gic_set_irq, i);
  729. for (i = 0; i < NUM_CPU(s); i++) {
  730. sysbus_init_irq(sbd, &s->parent_irq[i]);
  731. }
  732. memory_region_init_io(&s->iomem, OBJECT(s), &gic_dist_ops, s,
  733. "gic_dist", 0x1000);
  734. }
  735. static void arm_gic_realize(DeviceState *dev, Error **errp)
  736. {
  737. /* Device instance realize function for the GIC sysbus device */
  738. int i;
  739. GICState *s = ARM_GIC(dev);
  740. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  741. ARMGICClass *agc = ARM_GIC_GET_CLASS(s);
  742. Error *local_err = NULL;
  743. agc->parent_realize(dev, &local_err);
  744. if (local_err) {
  745. error_propagate(errp, local_err);
  746. return;
  747. }
  748. gic_init_irqs_and_distributor(s, s->num_irq);
  749. /* Memory regions for the CPU interfaces (NVIC doesn't have these):
  750. * a region for "CPU interface for this core", then a region for
  751. * "CPU interface for core 0", "for core 1", ...
  752. * NB that the memory region size of 0x100 applies for the 11MPCore
  753. * and also cores following the GIC v1 spec (ie A9).
  754. * GIC v2 defines a larger memory region (0x1000) so this will need
  755. * to be extended when we implement A15.
  756. */
  757. memory_region_init_io(&s->cpuiomem[0], OBJECT(s), &gic_thiscpu_ops, s,
  758. "gic_cpu", 0x100);
  759. for (i = 0; i < NUM_CPU(s); i++) {
  760. s->backref[i] = s;
  761. memory_region_init_io(&s->cpuiomem[i+1], OBJECT(s), &gic_cpu_ops,
  762. &s->backref[i], "gic_cpu", 0x100);
  763. }
  764. /* Distributor */
  765. sysbus_init_mmio(sbd, &s->iomem);
  766. /* cpu interfaces (one for "current cpu" plus one per cpu) */
  767. for (i = 0; i <= NUM_CPU(s); i++) {
  768. sysbus_init_mmio(sbd, &s->cpuiomem[i]);
  769. }
  770. }
  771. static void arm_gic_class_init(ObjectClass *klass, void *data)
  772. {
  773. DeviceClass *dc = DEVICE_CLASS(klass);
  774. ARMGICClass *agc = ARM_GIC_CLASS(klass);
  775. agc->parent_realize = dc->realize;
  776. dc->realize = arm_gic_realize;
  777. }
  778. static const TypeInfo arm_gic_info = {
  779. .name = TYPE_ARM_GIC,
  780. .parent = TYPE_ARM_GIC_COMMON,
  781. .instance_size = sizeof(GICState),
  782. .class_init = arm_gic_class_init,
  783. .class_size = sizeof(ARMGICClass),
  784. };
  785. static void arm_gic_register_types(void)
  786. {
  787. type_register_static(&arm_gic_info);
  788. }
  789. type_init(arm_gic_register_types)