arm_gic.c 21 KB

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