arm_gic.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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. //#define DEBUG_GIC
  13. #ifdef DEBUG_GIC
  14. #define DPRINTF(fmt, ...) \
  15. do { printf("arm_gic: " fmt , ## __VA_ARGS__); } while (0)
  16. #else
  17. #define DPRINTF(fmt, ...) do {} while(0)
  18. #endif
  19. #ifdef NVIC
  20. static const uint8_t gic_id[] =
  21. { 0x00, 0xb0, 0x1b, 0x00, 0x0d, 0xe0, 0x05, 0xb1 };
  22. /* The NVIC has 16 internal vectors. However these are not exposed
  23. through the normal GIC interface. */
  24. #define GIC_BASE_IRQ 32
  25. #else
  26. static const uint8_t gic_id[] =
  27. { 0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
  28. #define GIC_BASE_IRQ 0
  29. #endif
  30. #define FROM_SYSBUSGIC(type, dev) \
  31. DO_UPCAST(type, gic, FROM_SYSBUS(gic_state, dev))
  32. typedef struct gic_irq_state
  33. {
  34. /* ??? The documentation seems to imply the enable bits are global, even
  35. for per-cpu interrupts. This seems strange. */
  36. unsigned enabled:1;
  37. unsigned pending:NCPU;
  38. unsigned active:NCPU;
  39. unsigned level:NCPU;
  40. unsigned model:1; /* 0 = N:N, 1 = 1:N */
  41. unsigned trigger:1; /* nonzero = edge triggered. */
  42. } gic_irq_state;
  43. #define ALL_CPU_MASK ((1 << NCPU) - 1)
  44. #if NCPU > 1
  45. #define NUM_CPU(s) ((s)->num_cpu)
  46. #else
  47. #define NUM_CPU(s) 1
  48. #endif
  49. #define GIC_SET_ENABLED(irq) s->irq_state[irq].enabled = 1
  50. #define GIC_CLEAR_ENABLED(irq) s->irq_state[irq].enabled = 0
  51. #define GIC_TEST_ENABLED(irq) s->irq_state[irq].enabled
  52. #define GIC_SET_PENDING(irq, cm) s->irq_state[irq].pending |= (cm)
  53. #define GIC_CLEAR_PENDING(irq, cm) s->irq_state[irq].pending &= ~(cm)
  54. #define GIC_TEST_PENDING(irq, cm) ((s->irq_state[irq].pending & (cm)) != 0)
  55. #define GIC_SET_ACTIVE(irq, cm) s->irq_state[irq].active |= (cm)
  56. #define GIC_CLEAR_ACTIVE(irq, cm) s->irq_state[irq].active &= ~(cm)
  57. #define GIC_TEST_ACTIVE(irq, cm) ((s->irq_state[irq].active & (cm)) != 0)
  58. #define GIC_SET_MODEL(irq) s->irq_state[irq].model = 1
  59. #define GIC_CLEAR_MODEL(irq) s->irq_state[irq].model = 0
  60. #define GIC_TEST_MODEL(irq) s->irq_state[irq].model
  61. #define GIC_SET_LEVEL(irq, cm) s->irq_state[irq].level = (cm)
  62. #define GIC_CLEAR_LEVEL(irq, cm) s->irq_state[irq].level &= ~(cm)
  63. #define GIC_TEST_LEVEL(irq, cm) ((s->irq_state[irq].level & (cm)) != 0)
  64. #define GIC_SET_TRIGGER(irq) s->irq_state[irq].trigger = 1
  65. #define GIC_CLEAR_TRIGGER(irq) s->irq_state[irq].trigger = 0
  66. #define GIC_TEST_TRIGGER(irq) s->irq_state[irq].trigger
  67. #define GIC_GET_PRIORITY(irq, cpu) \
  68. (((irq) < 32) ? s->priority1[irq][cpu] : s->priority2[(irq) - 32])
  69. #ifdef NVIC
  70. #define GIC_TARGET(irq) 1
  71. #else
  72. #define GIC_TARGET(irq) s->irq_target[irq]
  73. #endif
  74. typedef struct gic_state
  75. {
  76. SysBusDevice busdev;
  77. qemu_irq parent_irq[NCPU];
  78. int enabled;
  79. int cpu_enabled[NCPU];
  80. gic_irq_state irq_state[GIC_NIRQ];
  81. #ifndef NVIC
  82. int irq_target[GIC_NIRQ];
  83. #endif
  84. int priority1[32][NCPU];
  85. int priority2[GIC_NIRQ - 32];
  86. int last_active[GIC_NIRQ][NCPU];
  87. int priority_mask[NCPU];
  88. int running_irq[NCPU];
  89. int running_priority[NCPU];
  90. int current_pending[NCPU];
  91. #if NCPU > 1
  92. int num_cpu;
  93. #endif
  94. int iomemtype;
  95. } gic_state;
  96. /* TODO: Many places that call this routine could be optimized. */
  97. /* Update interrupt status after enabled or pending bits have been changed. */
  98. static void gic_update(gic_state *s)
  99. {
  100. int best_irq;
  101. int best_prio;
  102. int irq;
  103. int level;
  104. int cpu;
  105. int cm;
  106. for (cpu = 0; cpu < NUM_CPU(s); cpu++) {
  107. cm = 1 << cpu;
  108. s->current_pending[cpu] = 1023;
  109. if (!s->enabled || !s->cpu_enabled[cpu]) {
  110. qemu_irq_lower(s->parent_irq[cpu]);
  111. return;
  112. }
  113. best_prio = 0x100;
  114. best_irq = 1023;
  115. for (irq = 0; irq < GIC_NIRQ; irq++) {
  116. if (GIC_TEST_ENABLED(irq) && GIC_TEST_PENDING(irq, cm)) {
  117. if (GIC_GET_PRIORITY(irq, cpu) < best_prio) {
  118. best_prio = GIC_GET_PRIORITY(irq, cpu);
  119. best_irq = irq;
  120. }
  121. }
  122. }
  123. level = 0;
  124. if (best_prio <= s->priority_mask[cpu]) {
  125. s->current_pending[cpu] = best_irq;
  126. if (best_prio < s->running_priority[cpu]) {
  127. DPRINTF("Raised pending IRQ %d\n", best_irq);
  128. level = 1;
  129. }
  130. }
  131. qemu_set_irq(s->parent_irq[cpu], level);
  132. }
  133. }
  134. static void __attribute__((unused))
  135. gic_set_pending_private(gic_state *s, int cpu, int irq)
  136. {
  137. int cm = 1 << cpu;
  138. if (GIC_TEST_PENDING(irq, cm))
  139. return;
  140. DPRINTF("Set %d pending cpu %d\n", irq, cpu);
  141. GIC_SET_PENDING(irq, cm);
  142. gic_update(s);
  143. }
  144. /* Process a change in an external IRQ input. */
  145. static void gic_set_irq(void *opaque, int irq, int level)
  146. {
  147. gic_state *s = (gic_state *)opaque;
  148. /* The first external input line is internal interrupt 32. */
  149. irq += 32;
  150. if (level == GIC_TEST_LEVEL(irq, ALL_CPU_MASK))
  151. return;
  152. if (level) {
  153. GIC_SET_LEVEL(irq, ALL_CPU_MASK);
  154. if (GIC_TEST_TRIGGER(irq) || GIC_TEST_ENABLED(irq)) {
  155. DPRINTF("Set %d pending mask %x\n", irq, GIC_TARGET(irq));
  156. GIC_SET_PENDING(irq, GIC_TARGET(irq));
  157. }
  158. } else {
  159. GIC_CLEAR_LEVEL(irq, ALL_CPU_MASK);
  160. }
  161. gic_update(s);
  162. }
  163. static void gic_set_running_irq(gic_state *s, int cpu, int irq)
  164. {
  165. s->running_irq[cpu] = irq;
  166. if (irq == 1023) {
  167. s->running_priority[cpu] = 0x100;
  168. } else {
  169. s->running_priority[cpu] = GIC_GET_PRIORITY(irq, cpu);
  170. }
  171. gic_update(s);
  172. }
  173. static uint32_t gic_acknowledge_irq(gic_state *s, int cpu)
  174. {
  175. int new_irq;
  176. int cm = 1 << cpu;
  177. new_irq = s->current_pending[cpu];
  178. if (new_irq == 1023
  179. || GIC_GET_PRIORITY(new_irq, cpu) >= s->running_priority[cpu]) {
  180. DPRINTF("ACK no pending IRQ\n");
  181. return 1023;
  182. }
  183. s->last_active[new_irq][cpu] = s->running_irq[cpu];
  184. /* Clear pending flags for both level and edge triggered interrupts.
  185. Level triggered IRQs will be reasserted once they become inactive. */
  186. GIC_CLEAR_PENDING(new_irq, GIC_TEST_MODEL(new_irq) ? ALL_CPU_MASK : cm);
  187. gic_set_running_irq(s, cpu, new_irq);
  188. DPRINTF("ACK %d\n", new_irq);
  189. return new_irq;
  190. }
  191. static void gic_complete_irq(gic_state * s, int cpu, int irq)
  192. {
  193. int update = 0;
  194. int cm = 1 << cpu;
  195. DPRINTF("EOI %d\n", irq);
  196. if (s->running_irq[cpu] == 1023)
  197. return; /* No active IRQ. */
  198. if (irq != 1023) {
  199. /* Mark level triggered interrupts as pending if they are still
  200. raised. */
  201. if (!GIC_TEST_TRIGGER(irq) && GIC_TEST_ENABLED(irq)
  202. && GIC_TEST_LEVEL(irq, cm) && (GIC_TARGET(irq) & cm) != 0) {
  203. DPRINTF("Set %d pending mask %x\n", irq, cm);
  204. GIC_SET_PENDING(irq, cm);
  205. update = 1;
  206. }
  207. }
  208. if (irq != s->running_irq[cpu]) {
  209. /* Complete an IRQ that is not currently running. */
  210. int tmp = s->running_irq[cpu];
  211. while (s->last_active[tmp][cpu] != 1023) {
  212. if (s->last_active[tmp][cpu] == irq) {
  213. s->last_active[tmp][cpu] = s->last_active[irq][cpu];
  214. break;
  215. }
  216. tmp = s->last_active[tmp][cpu];
  217. }
  218. if (update) {
  219. gic_update(s);
  220. }
  221. } else {
  222. /* Complete the current running IRQ. */
  223. gic_set_running_irq(s, cpu, s->last_active[s->running_irq[cpu]][cpu]);
  224. }
  225. }
  226. static uint32_t gic_dist_readb(void *opaque, target_phys_addr_t offset)
  227. {
  228. gic_state *s = (gic_state *)opaque;
  229. uint32_t res;
  230. int irq;
  231. int i;
  232. int cpu;
  233. int cm;
  234. int mask;
  235. cpu = gic_get_current_cpu();
  236. cm = 1 << cpu;
  237. if (offset < 0x100) {
  238. #ifndef NVIC
  239. if (offset == 0)
  240. return s->enabled;
  241. if (offset == 4)
  242. return ((GIC_NIRQ / 32) - 1) | ((NUM_CPU(s) - 1) << 5);
  243. if (offset < 0x08)
  244. return 0;
  245. #endif
  246. goto bad_reg;
  247. } else if (offset < 0x200) {
  248. /* Interrupt Set/Clear Enable. */
  249. if (offset < 0x180)
  250. irq = (offset - 0x100) * 8;
  251. else
  252. irq = (offset - 0x180) * 8;
  253. irq += GIC_BASE_IRQ;
  254. if (irq >= GIC_NIRQ)
  255. goto bad_reg;
  256. res = 0;
  257. for (i = 0; i < 8; i++) {
  258. if (GIC_TEST_ENABLED(irq + i)) {
  259. res |= (1 << i);
  260. }
  261. }
  262. } else if (offset < 0x300) {
  263. /* Interrupt Set/Clear Pending. */
  264. if (offset < 0x280)
  265. irq = (offset - 0x200) * 8;
  266. else
  267. irq = (offset - 0x280) * 8;
  268. irq += GIC_BASE_IRQ;
  269. if (irq >= GIC_NIRQ)
  270. goto bad_reg;
  271. res = 0;
  272. mask = (irq < 32) ? cm : ALL_CPU_MASK;
  273. for (i = 0; i < 8; i++) {
  274. if (GIC_TEST_PENDING(irq + i, mask)) {
  275. res |= (1 << i);
  276. }
  277. }
  278. } else if (offset < 0x400) {
  279. /* Interrupt Active. */
  280. irq = (offset - 0x300) * 8 + GIC_BASE_IRQ;
  281. if (irq >= GIC_NIRQ)
  282. goto bad_reg;
  283. res = 0;
  284. mask = (irq < 32) ? cm : ALL_CPU_MASK;
  285. for (i = 0; i < 8; i++) {
  286. if (GIC_TEST_ACTIVE(irq + i, mask)) {
  287. res |= (1 << i);
  288. }
  289. }
  290. } else if (offset < 0x800) {
  291. /* Interrupt Priority. */
  292. irq = (offset - 0x400) + GIC_BASE_IRQ;
  293. if (irq >= GIC_NIRQ)
  294. goto bad_reg;
  295. res = GIC_GET_PRIORITY(irq, cpu);
  296. #ifndef NVIC
  297. } else if (offset < 0xc00) {
  298. /* Interrupt CPU Target. */
  299. irq = (offset - 0x800) + GIC_BASE_IRQ;
  300. if (irq >= GIC_NIRQ)
  301. goto bad_reg;
  302. if (irq >= 29 && irq <= 31) {
  303. res = cm;
  304. } else {
  305. res = GIC_TARGET(irq);
  306. }
  307. } else if (offset < 0xf00) {
  308. /* Interrupt Configuration. */
  309. irq = (offset - 0xc00) * 2 + GIC_BASE_IRQ;
  310. if (irq >= GIC_NIRQ)
  311. goto bad_reg;
  312. res = 0;
  313. for (i = 0; i < 4; i++) {
  314. if (GIC_TEST_MODEL(irq + i))
  315. res |= (1 << (i * 2));
  316. if (GIC_TEST_TRIGGER(irq + i))
  317. res |= (2 << (i * 2));
  318. }
  319. #endif
  320. } else if (offset < 0xfe0) {
  321. goto bad_reg;
  322. } else /* offset >= 0xfe0 */ {
  323. if (offset & 3) {
  324. res = 0;
  325. } else {
  326. res = gic_id[(offset - 0xfe0) >> 2];
  327. }
  328. }
  329. return res;
  330. bad_reg:
  331. hw_error("gic_dist_readb: Bad offset %x\n", (int)offset);
  332. return 0;
  333. }
  334. static uint32_t gic_dist_readw(void *opaque, target_phys_addr_t offset)
  335. {
  336. uint32_t val;
  337. val = gic_dist_readb(opaque, offset);
  338. val |= gic_dist_readb(opaque, offset + 1) << 8;
  339. return val;
  340. }
  341. static uint32_t gic_dist_readl(void *opaque, target_phys_addr_t offset)
  342. {
  343. uint32_t val;
  344. #ifdef NVIC
  345. gic_state *s = (gic_state *)opaque;
  346. uint32_t addr;
  347. addr = offset;
  348. if (addr < 0x100 || addr > 0xd00)
  349. return nvic_readl(s, addr);
  350. #endif
  351. val = gic_dist_readw(opaque, offset);
  352. val |= gic_dist_readw(opaque, offset + 2) << 16;
  353. return val;
  354. }
  355. static void gic_dist_writeb(void *opaque, target_phys_addr_t offset,
  356. uint32_t value)
  357. {
  358. gic_state *s = (gic_state *)opaque;
  359. int irq;
  360. int i;
  361. int cpu;
  362. cpu = gic_get_current_cpu();
  363. if (offset < 0x100) {
  364. #ifdef NVIC
  365. goto bad_reg;
  366. #else
  367. if (offset == 0) {
  368. s->enabled = (value & 1);
  369. DPRINTF("Distribution %sabled\n", s->enabled ? "En" : "Dis");
  370. } else if (offset < 4) {
  371. /* ignored. */
  372. } else {
  373. goto bad_reg;
  374. }
  375. #endif
  376. } else if (offset < 0x180) {
  377. /* Interrupt Set Enable. */
  378. irq = (offset - 0x100) * 8 + GIC_BASE_IRQ;
  379. if (irq >= GIC_NIRQ)
  380. goto bad_reg;
  381. if (irq < 16)
  382. value = 0xff;
  383. for (i = 0; i < 8; i++) {
  384. if (value & (1 << i)) {
  385. int mask = (irq < 32) ? (1 << cpu) : GIC_TARGET(irq);
  386. if (!GIC_TEST_ENABLED(irq + i))
  387. DPRINTF("Enabled IRQ %d\n", irq + i);
  388. GIC_SET_ENABLED(irq + i);
  389. /* If a raised level triggered IRQ enabled then mark
  390. is as pending. */
  391. if (GIC_TEST_LEVEL(irq + i, mask)
  392. && !GIC_TEST_TRIGGER(irq + i)) {
  393. DPRINTF("Set %d pending mask %x\n", irq + i, mask);
  394. GIC_SET_PENDING(irq + i, mask);
  395. }
  396. }
  397. }
  398. } else if (offset < 0x200) {
  399. /* Interrupt Clear Enable. */
  400. irq = (offset - 0x180) * 8 + GIC_BASE_IRQ;
  401. if (irq >= GIC_NIRQ)
  402. goto bad_reg;
  403. if (irq < 16)
  404. value = 0;
  405. for (i = 0; i < 8; i++) {
  406. if (value & (1 << i)) {
  407. if (GIC_TEST_ENABLED(irq + i))
  408. DPRINTF("Disabled IRQ %d\n", irq + i);
  409. GIC_CLEAR_ENABLED(irq + i);
  410. }
  411. }
  412. } else if (offset < 0x280) {
  413. /* Interrupt Set Pending. */
  414. irq = (offset - 0x200) * 8 + GIC_BASE_IRQ;
  415. if (irq >= GIC_NIRQ)
  416. goto bad_reg;
  417. if (irq < 16)
  418. irq = 0;
  419. for (i = 0; i < 8; i++) {
  420. if (value & (1 << i)) {
  421. GIC_SET_PENDING(irq + i, GIC_TARGET(irq));
  422. }
  423. }
  424. } else if (offset < 0x300) {
  425. /* Interrupt Clear Pending. */
  426. irq = (offset - 0x280) * 8 + GIC_BASE_IRQ;
  427. if (irq >= GIC_NIRQ)
  428. goto bad_reg;
  429. for (i = 0; i < 8; i++) {
  430. /* ??? This currently clears the pending bit for all CPUs, even
  431. for per-CPU interrupts. It's unclear whether this is the
  432. corect behavior. */
  433. if (value & (1 << i)) {
  434. GIC_CLEAR_PENDING(irq + i, ALL_CPU_MASK);
  435. }
  436. }
  437. } else if (offset < 0x400) {
  438. /* Interrupt Active. */
  439. goto bad_reg;
  440. } else if (offset < 0x800) {
  441. /* Interrupt Priority. */
  442. irq = (offset - 0x400) + GIC_BASE_IRQ;
  443. if (irq >= GIC_NIRQ)
  444. goto bad_reg;
  445. if (irq < 32) {
  446. s->priority1[irq][cpu] = value;
  447. } else {
  448. s->priority2[irq - 32] = value;
  449. }
  450. #ifndef NVIC
  451. } else if (offset < 0xc00) {
  452. /* Interrupt CPU Target. */
  453. irq = (offset - 0x800) + GIC_BASE_IRQ;
  454. if (irq >= GIC_NIRQ)
  455. goto bad_reg;
  456. if (irq < 29)
  457. value = 0;
  458. else if (irq < 32)
  459. value = ALL_CPU_MASK;
  460. s->irq_target[irq] = value & ALL_CPU_MASK;
  461. } else if (offset < 0xf00) {
  462. /* Interrupt Configuration. */
  463. irq = (offset - 0xc00) * 4 + GIC_BASE_IRQ;
  464. if (irq >= GIC_NIRQ)
  465. goto bad_reg;
  466. if (irq < 32)
  467. value |= 0xaa;
  468. for (i = 0; i < 4; i++) {
  469. if (value & (1 << (i * 2))) {
  470. GIC_SET_MODEL(irq + i);
  471. } else {
  472. GIC_CLEAR_MODEL(irq + i);
  473. }
  474. if (value & (2 << (i * 2))) {
  475. GIC_SET_TRIGGER(irq + i);
  476. } else {
  477. GIC_CLEAR_TRIGGER(irq + i);
  478. }
  479. }
  480. #endif
  481. } else {
  482. /* 0xf00 is only handled for 32-bit writes. */
  483. goto bad_reg;
  484. }
  485. gic_update(s);
  486. return;
  487. bad_reg:
  488. hw_error("gic_dist_writeb: Bad offset %x\n", (int)offset);
  489. }
  490. static void gic_dist_writew(void *opaque, target_phys_addr_t offset,
  491. uint32_t value)
  492. {
  493. gic_dist_writeb(opaque, offset, value & 0xff);
  494. gic_dist_writeb(opaque, offset + 1, value >> 8);
  495. }
  496. static void gic_dist_writel(void *opaque, target_phys_addr_t offset,
  497. uint32_t value)
  498. {
  499. gic_state *s = (gic_state *)opaque;
  500. #ifdef NVIC
  501. uint32_t addr;
  502. addr = offset;
  503. if (addr < 0x100 || (addr > 0xd00 && addr != 0xf00)) {
  504. nvic_writel(s, addr, value);
  505. return;
  506. }
  507. #endif
  508. if (offset == 0xf00) {
  509. int cpu;
  510. int irq;
  511. int mask;
  512. cpu = gic_get_current_cpu();
  513. irq = value & 0x3ff;
  514. switch ((value >> 24) & 3) {
  515. case 0:
  516. mask = (value >> 16) & ALL_CPU_MASK;
  517. break;
  518. case 1:
  519. mask = ALL_CPU_MASK ^ (1 << cpu);
  520. break;
  521. case 2:
  522. mask = 1 << cpu;
  523. break;
  524. default:
  525. DPRINTF("Bad Soft Int target filter\n");
  526. mask = ALL_CPU_MASK;
  527. break;
  528. }
  529. GIC_SET_PENDING(irq, mask);
  530. gic_update(s);
  531. return;
  532. }
  533. gic_dist_writew(opaque, offset, value & 0xffff);
  534. gic_dist_writew(opaque, offset + 2, value >> 16);
  535. }
  536. static CPUReadMemoryFunc * const gic_dist_readfn[] = {
  537. gic_dist_readb,
  538. gic_dist_readw,
  539. gic_dist_readl
  540. };
  541. static CPUWriteMemoryFunc * const gic_dist_writefn[] = {
  542. gic_dist_writeb,
  543. gic_dist_writew,
  544. gic_dist_writel
  545. };
  546. #ifndef NVIC
  547. static uint32_t gic_cpu_read(gic_state *s, int cpu, int offset)
  548. {
  549. switch (offset) {
  550. case 0x00: /* Control */
  551. return s->cpu_enabled[cpu];
  552. case 0x04: /* Priority mask */
  553. return s->priority_mask[cpu];
  554. case 0x08: /* Binary Point */
  555. /* ??? Not implemented. */
  556. return 0;
  557. case 0x0c: /* Acknowledge */
  558. return gic_acknowledge_irq(s, cpu);
  559. case 0x14: /* Runing Priority */
  560. return s->running_priority[cpu];
  561. case 0x18: /* Highest Pending Interrupt */
  562. return s->current_pending[cpu];
  563. default:
  564. hw_error("gic_cpu_read: Bad offset %x\n", (int)offset);
  565. return 0;
  566. }
  567. }
  568. static void gic_cpu_write(gic_state *s, int cpu, int offset, uint32_t value)
  569. {
  570. switch (offset) {
  571. case 0x00: /* Control */
  572. s->cpu_enabled[cpu] = (value & 1);
  573. DPRINTF("CPU %d %sabled\n", cpu, s->cpu_enabled ? "En" : "Dis");
  574. break;
  575. case 0x04: /* Priority mask */
  576. s->priority_mask[cpu] = (value & 0xff);
  577. break;
  578. case 0x08: /* Binary Point */
  579. /* ??? Not implemented. */
  580. break;
  581. case 0x10: /* End Of Interrupt */
  582. return gic_complete_irq(s, cpu, value & 0x3ff);
  583. default:
  584. hw_error("gic_cpu_write: Bad offset %x\n", (int)offset);
  585. return;
  586. }
  587. gic_update(s);
  588. }
  589. #endif
  590. static void gic_reset(gic_state *s)
  591. {
  592. int i;
  593. memset(s->irq_state, 0, GIC_NIRQ * sizeof(gic_irq_state));
  594. for (i = 0 ; i < NUM_CPU(s); i++) {
  595. s->priority_mask[i] = 0xf0;
  596. s->current_pending[i] = 1023;
  597. s->running_irq[i] = 1023;
  598. s->running_priority[i] = 0x100;
  599. #ifdef NVIC
  600. /* The NVIC doesn't have per-cpu interfaces, so enable by default. */
  601. s->cpu_enabled[i] = 1;
  602. #else
  603. s->cpu_enabled[i] = 0;
  604. #endif
  605. }
  606. for (i = 0; i < 16; i++) {
  607. GIC_SET_ENABLED(i);
  608. GIC_SET_TRIGGER(i);
  609. }
  610. #ifdef NVIC
  611. /* The NVIC is always enabled. */
  612. s->enabled = 1;
  613. #else
  614. s->enabled = 0;
  615. #endif
  616. }
  617. static void gic_save(QEMUFile *f, void *opaque)
  618. {
  619. gic_state *s = (gic_state *)opaque;
  620. int i;
  621. int j;
  622. qemu_put_be32(f, s->enabled);
  623. for (i = 0; i < NUM_CPU(s); i++) {
  624. qemu_put_be32(f, s->cpu_enabled[i]);
  625. #ifndef NVIC
  626. qemu_put_be32(f, s->irq_target[i]);
  627. #endif
  628. for (j = 0; j < 32; j++)
  629. qemu_put_be32(f, s->priority1[j][i]);
  630. for (j = 0; j < GIC_NIRQ; j++)
  631. qemu_put_be32(f, s->last_active[j][i]);
  632. qemu_put_be32(f, s->priority_mask[i]);
  633. qemu_put_be32(f, s->running_irq[i]);
  634. qemu_put_be32(f, s->running_priority[i]);
  635. qemu_put_be32(f, s->current_pending[i]);
  636. }
  637. for (i = 0; i < GIC_NIRQ - 32; i++) {
  638. qemu_put_be32(f, s->priority2[i]);
  639. }
  640. for (i = 0; i < GIC_NIRQ; i++) {
  641. qemu_put_byte(f, s->irq_state[i].enabled);
  642. qemu_put_byte(f, s->irq_state[i].pending);
  643. qemu_put_byte(f, s->irq_state[i].active);
  644. qemu_put_byte(f, s->irq_state[i].level);
  645. qemu_put_byte(f, s->irq_state[i].model);
  646. qemu_put_byte(f, s->irq_state[i].trigger);
  647. }
  648. }
  649. static int gic_load(QEMUFile *f, void *opaque, int version_id)
  650. {
  651. gic_state *s = (gic_state *)opaque;
  652. int i;
  653. int j;
  654. if (version_id != 1)
  655. return -EINVAL;
  656. s->enabled = qemu_get_be32(f);
  657. for (i = 0; i < NUM_CPU(s); i++) {
  658. s->cpu_enabled[i] = qemu_get_be32(f);
  659. #ifndef NVIC
  660. s->irq_target[i] = qemu_get_be32(f);
  661. #endif
  662. for (j = 0; j < 32; j++)
  663. s->priority1[j][i] = qemu_get_be32(f);
  664. for (j = 0; j < GIC_NIRQ; j++)
  665. s->last_active[j][i] = qemu_get_be32(f);
  666. s->priority_mask[i] = qemu_get_be32(f);
  667. s->running_irq[i] = qemu_get_be32(f);
  668. s->running_priority[i] = qemu_get_be32(f);
  669. s->current_pending[i] = qemu_get_be32(f);
  670. }
  671. for (i = 0; i < GIC_NIRQ - 32; i++) {
  672. s->priority2[i] = qemu_get_be32(f);
  673. }
  674. for (i = 0; i < GIC_NIRQ; i++) {
  675. s->irq_state[i].enabled = qemu_get_byte(f);
  676. s->irq_state[i].pending = qemu_get_byte(f);
  677. s->irq_state[i].active = qemu_get_byte(f);
  678. s->irq_state[i].level = qemu_get_byte(f);
  679. s->irq_state[i].model = qemu_get_byte(f);
  680. s->irq_state[i].trigger = qemu_get_byte(f);
  681. }
  682. return 0;
  683. }
  684. #if NCPU > 1
  685. static void gic_init(gic_state *s, int num_cpu)
  686. #else
  687. static void gic_init(gic_state *s)
  688. #endif
  689. {
  690. int i;
  691. #if NCPU > 1
  692. s->num_cpu = num_cpu;
  693. #endif
  694. qdev_init_gpio_in(&s->busdev.qdev, gic_set_irq, GIC_NIRQ - 32);
  695. for (i = 0; i < NUM_CPU(s); i++) {
  696. sysbus_init_irq(&s->busdev, &s->parent_irq[i]);
  697. }
  698. s->iomemtype = cpu_register_io_memory(gic_dist_readfn,
  699. gic_dist_writefn, s,
  700. DEVICE_NATIVE_ENDIAN);
  701. gic_reset(s);
  702. register_savevm(NULL, "arm_gic", -1, 1, gic_save, gic_load, s);
  703. }