2
0

arm_gic.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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 enable bits are only banked for per-cpu interrupts. */
  35. unsigned enabled:NCPU;
  36. unsigned pending:NCPU;
  37. unsigned active:NCPU;
  38. unsigned level:NCPU;
  39. unsigned model:1; /* 0 = N:N, 1 = 1:N */
  40. unsigned trigger:1; /* nonzero = edge triggered. */
  41. } gic_irq_state;
  42. #define ALL_CPU_MASK ((1 << NCPU) - 1)
  43. #if NCPU > 1
  44. #define NUM_CPU(s) ((s)->num_cpu)
  45. #else
  46. #define NUM_CPU(s) 1
  47. #endif
  48. #define GIC_SET_ENABLED(irq, cm) s->irq_state[irq].enabled |= (cm)
  49. #define GIC_CLEAR_ENABLED(irq, cm) s->irq_state[irq].enabled &= ~(cm)
  50. #define GIC_TEST_ENABLED(irq, cm) ((s->irq_state[irq].enabled & (cm)) != 0)
  51. #define GIC_SET_PENDING(irq, cm) s->irq_state[irq].pending |= (cm)
  52. #define GIC_CLEAR_PENDING(irq, cm) s->irq_state[irq].pending &= ~(cm)
  53. #define GIC_TEST_PENDING(irq, cm) ((s->irq_state[irq].pending & (cm)) != 0)
  54. #define GIC_SET_ACTIVE(irq, cm) s->irq_state[irq].active |= (cm)
  55. #define GIC_CLEAR_ACTIVE(irq, cm) s->irq_state[irq].active &= ~(cm)
  56. #define GIC_TEST_ACTIVE(irq, cm) ((s->irq_state[irq].active & (cm)) != 0)
  57. #define GIC_SET_MODEL(irq) s->irq_state[irq].model = 1
  58. #define GIC_CLEAR_MODEL(irq) s->irq_state[irq].model = 0
  59. #define GIC_TEST_MODEL(irq) s->irq_state[irq].model
  60. #define GIC_SET_LEVEL(irq, cm) s->irq_state[irq].level = (cm)
  61. #define GIC_CLEAR_LEVEL(irq, cm) s->irq_state[irq].level &= ~(cm)
  62. #define GIC_TEST_LEVEL(irq, cm) ((s->irq_state[irq].level & (cm)) != 0)
  63. #define GIC_SET_TRIGGER(irq) s->irq_state[irq].trigger = 1
  64. #define GIC_CLEAR_TRIGGER(irq) s->irq_state[irq].trigger = 0
  65. #define GIC_TEST_TRIGGER(irq) s->irq_state[irq].trigger
  66. #define GIC_GET_PRIORITY(irq, cpu) \
  67. (((irq) < 32) ? s->priority1[irq][cpu] : s->priority2[(irq) - 32])
  68. #ifdef NVIC
  69. #define GIC_TARGET(irq) 1
  70. #else
  71. #define GIC_TARGET(irq) s->irq_target[irq]
  72. #endif
  73. typedef struct gic_state
  74. {
  75. SysBusDevice busdev;
  76. qemu_irq parent_irq[NCPU];
  77. int enabled;
  78. int cpu_enabled[NCPU];
  79. gic_irq_state irq_state[GIC_NIRQ];
  80. #ifndef NVIC
  81. int irq_target[GIC_NIRQ];
  82. #endif
  83. int priority1[32][NCPU];
  84. int priority2[GIC_NIRQ - 32];
  85. int last_active[GIC_NIRQ][NCPU];
  86. int priority_mask[NCPU];
  87. int running_irq[NCPU];
  88. int running_priority[NCPU];
  89. int current_pending[NCPU];
  90. #if NCPU > 1
  91. int num_cpu;
  92. #endif
  93. MemoryRegion iomem;
  94. } gic_state;
  95. /* TODO: Many places that call this routine could be optimized. */
  96. /* Update interrupt status after enabled or pending bits have been changed. */
  97. static void gic_update(gic_state *s)
  98. {
  99. int best_irq;
  100. int best_prio;
  101. int irq;
  102. int level;
  103. int cpu;
  104. int cm;
  105. for (cpu = 0; cpu < NUM_CPU(s); cpu++) {
  106. cm = 1 << cpu;
  107. s->current_pending[cpu] = 1023;
  108. if (!s->enabled || !s->cpu_enabled[cpu]) {
  109. qemu_irq_lower(s->parent_irq[cpu]);
  110. return;
  111. }
  112. best_prio = 0x100;
  113. best_irq = 1023;
  114. for (irq = 0; irq < GIC_NIRQ; irq++) {
  115. if (GIC_TEST_ENABLED(irq, cm) && GIC_TEST_PENDING(irq, cm)) {
  116. if (GIC_GET_PRIORITY(irq, cpu) < best_prio) {
  117. best_prio = GIC_GET_PRIORITY(irq, cpu);
  118. best_irq = irq;
  119. }
  120. }
  121. }
  122. level = 0;
  123. if (best_prio <= s->priority_mask[cpu]) {
  124. s->current_pending[cpu] = best_irq;
  125. if (best_prio < s->running_priority[cpu]) {
  126. DPRINTF("Raised pending IRQ %d\n", best_irq);
  127. level = 1;
  128. }
  129. }
  130. qemu_set_irq(s->parent_irq[cpu], level);
  131. }
  132. }
  133. static void __attribute__((unused))
  134. gic_set_pending_private(gic_state *s, int cpu, int irq)
  135. {
  136. int cm = 1 << cpu;
  137. if (GIC_TEST_PENDING(irq, cm))
  138. return;
  139. DPRINTF("Set %d pending cpu %d\n", irq, cpu);
  140. GIC_SET_PENDING(irq, cm);
  141. gic_update(s);
  142. }
  143. /* Process a change in an external IRQ input. */
  144. static void gic_set_irq(void *opaque, int irq, int level)
  145. {
  146. gic_state *s = (gic_state *)opaque;
  147. /* The first external input line is internal interrupt 32. */
  148. irq += 32;
  149. if (level == GIC_TEST_LEVEL(irq, ALL_CPU_MASK))
  150. return;
  151. if (level) {
  152. GIC_SET_LEVEL(irq, ALL_CPU_MASK);
  153. if (GIC_TEST_TRIGGER(irq) || GIC_TEST_ENABLED(irq, ALL_CPU_MASK)) {
  154. DPRINTF("Set %d pending mask %x\n", irq, GIC_TARGET(irq));
  155. GIC_SET_PENDING(irq, GIC_TARGET(irq));
  156. }
  157. } else {
  158. GIC_CLEAR_LEVEL(irq, ALL_CPU_MASK);
  159. }
  160. gic_update(s);
  161. }
  162. static void gic_set_running_irq(gic_state *s, int cpu, int irq)
  163. {
  164. s->running_irq[cpu] = irq;
  165. if (irq == 1023) {
  166. s->running_priority[cpu] = 0x100;
  167. } else {
  168. s->running_priority[cpu] = GIC_GET_PRIORITY(irq, cpu);
  169. }
  170. gic_update(s);
  171. }
  172. static uint32_t gic_acknowledge_irq(gic_state *s, int cpu)
  173. {
  174. int new_irq;
  175. int cm = 1 << cpu;
  176. new_irq = s->current_pending[cpu];
  177. if (new_irq == 1023
  178. || GIC_GET_PRIORITY(new_irq, cpu) >= s->running_priority[cpu]) {
  179. DPRINTF("ACK no pending IRQ\n");
  180. return 1023;
  181. }
  182. s->last_active[new_irq][cpu] = s->running_irq[cpu];
  183. /* Clear pending flags for both level and edge triggered interrupts.
  184. Level triggered IRQs will be reasserted once they become inactive. */
  185. GIC_CLEAR_PENDING(new_irq, GIC_TEST_MODEL(new_irq) ? ALL_CPU_MASK : cm);
  186. gic_set_running_irq(s, cpu, new_irq);
  187. DPRINTF("ACK %d\n", new_irq);
  188. return new_irq;
  189. }
  190. static void gic_complete_irq(gic_state * s, int cpu, int irq)
  191. {
  192. int update = 0;
  193. int cm = 1 << cpu;
  194. DPRINTF("EOI %d\n", irq);
  195. if (s->running_irq[cpu] == 1023)
  196. return; /* No active IRQ. */
  197. if (irq != 1023) {
  198. /* Mark level triggered interrupts as pending if they are still
  199. raised. */
  200. if (!GIC_TEST_TRIGGER(irq) && GIC_TEST_ENABLED(irq, cm)
  201. && GIC_TEST_LEVEL(irq, cm) && (GIC_TARGET(irq) & cm) != 0) {
  202. DPRINTF("Set %d pending mask %x\n", irq, cm);
  203. GIC_SET_PENDING(irq, cm);
  204. update = 1;
  205. }
  206. }
  207. if (irq != s->running_irq[cpu]) {
  208. /* Complete an IRQ that is not currently running. */
  209. int tmp = s->running_irq[cpu];
  210. while (s->last_active[tmp][cpu] != 1023) {
  211. if (s->last_active[tmp][cpu] == irq) {
  212. s->last_active[tmp][cpu] = s->last_active[irq][cpu];
  213. break;
  214. }
  215. tmp = s->last_active[tmp][cpu];
  216. }
  217. if (update) {
  218. gic_update(s);
  219. }
  220. } else {
  221. /* Complete the current running IRQ. */
  222. gic_set_running_irq(s, cpu, s->last_active[s->running_irq[cpu]][cpu]);
  223. }
  224. }
  225. static uint32_t gic_dist_readb(void *opaque, target_phys_addr_t offset)
  226. {
  227. gic_state *s = (gic_state *)opaque;
  228. uint32_t res;
  229. int irq;
  230. int i;
  231. int cpu;
  232. int cm;
  233. int mask;
  234. cpu = gic_get_current_cpu();
  235. cm = 1 << cpu;
  236. if (offset < 0x100) {
  237. #ifndef NVIC
  238. if (offset == 0)
  239. return s->enabled;
  240. if (offset == 4)
  241. return ((GIC_NIRQ / 32) - 1) | ((NUM_CPU(s) - 1) << 5);
  242. if (offset < 0x08)
  243. return 0;
  244. #endif
  245. goto bad_reg;
  246. } else if (offset < 0x200) {
  247. /* Interrupt Set/Clear Enable. */
  248. if (offset < 0x180)
  249. irq = (offset - 0x100) * 8;
  250. else
  251. irq = (offset - 0x180) * 8;
  252. irq += GIC_BASE_IRQ;
  253. if (irq >= GIC_NIRQ)
  254. goto bad_reg;
  255. res = 0;
  256. for (i = 0; i < 8; i++) {
  257. if (GIC_TEST_ENABLED(irq + i, cm)) {
  258. res |= (1 << i);
  259. }
  260. }
  261. } else if (offset < 0x300) {
  262. /* Interrupt Set/Clear Pending. */
  263. if (offset < 0x280)
  264. irq = (offset - 0x200) * 8;
  265. else
  266. irq = (offset - 0x280) * 8;
  267. irq += GIC_BASE_IRQ;
  268. if (irq >= GIC_NIRQ)
  269. goto bad_reg;
  270. res = 0;
  271. mask = (irq < 32) ? cm : ALL_CPU_MASK;
  272. for (i = 0; i < 8; i++) {
  273. if (GIC_TEST_PENDING(irq + i, mask)) {
  274. res |= (1 << i);
  275. }
  276. }
  277. } else if (offset < 0x400) {
  278. /* Interrupt Active. */
  279. irq = (offset - 0x300) * 8 + GIC_BASE_IRQ;
  280. if (irq >= GIC_NIRQ)
  281. goto bad_reg;
  282. res = 0;
  283. mask = (irq < 32) ? cm : ALL_CPU_MASK;
  284. for (i = 0; i < 8; i++) {
  285. if (GIC_TEST_ACTIVE(irq + i, mask)) {
  286. res |= (1 << i);
  287. }
  288. }
  289. } else if (offset < 0x800) {
  290. /* Interrupt Priority. */
  291. irq = (offset - 0x400) + GIC_BASE_IRQ;
  292. if (irq >= GIC_NIRQ)
  293. goto bad_reg;
  294. res = GIC_GET_PRIORITY(irq, cpu);
  295. #ifndef NVIC
  296. } else if (offset < 0xc00) {
  297. /* Interrupt CPU Target. */
  298. irq = (offset - 0x800) + GIC_BASE_IRQ;
  299. if (irq >= GIC_NIRQ)
  300. goto bad_reg;
  301. if (irq >= 29 && irq <= 31) {
  302. res = cm;
  303. } else {
  304. res = GIC_TARGET(irq);
  305. }
  306. } else if (offset < 0xf00) {
  307. /* Interrupt Configuration. */
  308. irq = (offset - 0xc00) * 2 + GIC_BASE_IRQ;
  309. if (irq >= GIC_NIRQ)
  310. goto bad_reg;
  311. res = 0;
  312. for (i = 0; i < 4; i++) {
  313. if (GIC_TEST_MODEL(irq + i))
  314. res |= (1 << (i * 2));
  315. if (GIC_TEST_TRIGGER(irq + i))
  316. res |= (2 << (i * 2));
  317. }
  318. #endif
  319. } else if (offset < 0xfe0) {
  320. goto bad_reg;
  321. } else /* offset >= 0xfe0 */ {
  322. if (offset & 3) {
  323. res = 0;
  324. } else {
  325. res = gic_id[(offset - 0xfe0) >> 2];
  326. }
  327. }
  328. return res;
  329. bad_reg:
  330. hw_error("gic_dist_readb: Bad offset %x\n", (int)offset);
  331. return 0;
  332. }
  333. static uint32_t gic_dist_readw(void *opaque, target_phys_addr_t offset)
  334. {
  335. uint32_t val;
  336. val = gic_dist_readb(opaque, offset);
  337. val |= gic_dist_readb(opaque, offset + 1) << 8;
  338. return val;
  339. }
  340. static uint32_t gic_dist_readl(void *opaque, target_phys_addr_t offset)
  341. {
  342. uint32_t val;
  343. #ifdef NVIC
  344. gic_state *s = (gic_state *)opaque;
  345. uint32_t addr;
  346. addr = offset;
  347. if (addr < 0x100 || addr > 0xd00)
  348. return nvic_readl(s, addr);
  349. #endif
  350. val = gic_dist_readw(opaque, offset);
  351. val |= gic_dist_readw(opaque, offset + 2) << 16;
  352. return val;
  353. }
  354. static void gic_dist_writeb(void *opaque, target_phys_addr_t offset,
  355. uint32_t value)
  356. {
  357. gic_state *s = (gic_state *)opaque;
  358. int irq;
  359. int i;
  360. int cpu;
  361. cpu = gic_get_current_cpu();
  362. if (offset < 0x100) {
  363. #ifdef NVIC
  364. goto bad_reg;
  365. #else
  366. if (offset == 0) {
  367. s->enabled = (value & 1);
  368. DPRINTF("Distribution %sabled\n", s->enabled ? "En" : "Dis");
  369. } else if (offset < 4) {
  370. /* ignored. */
  371. } else {
  372. goto bad_reg;
  373. }
  374. #endif
  375. } else if (offset < 0x180) {
  376. /* Interrupt Set Enable. */
  377. irq = (offset - 0x100) * 8 + GIC_BASE_IRQ;
  378. if (irq >= GIC_NIRQ)
  379. goto bad_reg;
  380. if (irq < 16)
  381. value = 0xff;
  382. for (i = 0; i < 8; i++) {
  383. if (value & (1 << i)) {
  384. int mask = (irq < 32) ? (1 << cpu) : GIC_TARGET(irq);
  385. int cm = (irq < 32) ? (1 << cpu) : ALL_CPU_MASK;
  386. if (!GIC_TEST_ENABLED(irq + i, cm)) {
  387. DPRINTF("Enabled IRQ %d\n", irq + i);
  388. }
  389. GIC_SET_ENABLED(irq + i, cm);
  390. /* If a raised level triggered IRQ enabled then mark
  391. is as pending. */
  392. if (GIC_TEST_LEVEL(irq + i, mask)
  393. && !GIC_TEST_TRIGGER(irq + i)) {
  394. DPRINTF("Set %d pending mask %x\n", irq + i, mask);
  395. GIC_SET_PENDING(irq + i, mask);
  396. }
  397. }
  398. }
  399. } else if (offset < 0x200) {
  400. /* Interrupt Clear Enable. */
  401. irq = (offset - 0x180) * 8 + GIC_BASE_IRQ;
  402. if (irq >= GIC_NIRQ)
  403. goto bad_reg;
  404. if (irq < 16)
  405. value = 0;
  406. for (i = 0; i < 8; i++) {
  407. if (value & (1 << i)) {
  408. int cm = (irq < 32) ? (1 << cpu) : ALL_CPU_MASK;
  409. if (GIC_TEST_ENABLED(irq + i, cm)) {
  410. DPRINTF("Disabled IRQ %d\n", irq + i);
  411. }
  412. GIC_CLEAR_ENABLED(irq + i, cm);
  413. }
  414. }
  415. } else if (offset < 0x280) {
  416. /* Interrupt Set Pending. */
  417. irq = (offset - 0x200) * 8 + GIC_BASE_IRQ;
  418. if (irq >= GIC_NIRQ)
  419. goto bad_reg;
  420. if (irq < 16)
  421. irq = 0;
  422. for (i = 0; i < 8; i++) {
  423. if (value & (1 << i)) {
  424. GIC_SET_PENDING(irq + i, GIC_TARGET(irq));
  425. }
  426. }
  427. } else if (offset < 0x300) {
  428. /* Interrupt Clear Pending. */
  429. irq = (offset - 0x280) * 8 + GIC_BASE_IRQ;
  430. if (irq >= GIC_NIRQ)
  431. goto bad_reg;
  432. for (i = 0; i < 8; i++) {
  433. /* ??? This currently clears the pending bit for all CPUs, even
  434. for per-CPU interrupts. It's unclear whether this is the
  435. corect behavior. */
  436. if (value & (1 << i)) {
  437. GIC_CLEAR_PENDING(irq + i, ALL_CPU_MASK);
  438. }
  439. }
  440. } else if (offset < 0x400) {
  441. /* Interrupt Active. */
  442. goto bad_reg;
  443. } else if (offset < 0x800) {
  444. /* Interrupt Priority. */
  445. irq = (offset - 0x400) + GIC_BASE_IRQ;
  446. if (irq >= GIC_NIRQ)
  447. goto bad_reg;
  448. if (irq < 32) {
  449. s->priority1[irq][cpu] = value;
  450. } else {
  451. s->priority2[irq - 32] = value;
  452. }
  453. #ifndef NVIC
  454. } else if (offset < 0xc00) {
  455. /* Interrupt CPU Target. */
  456. irq = (offset - 0x800) + GIC_BASE_IRQ;
  457. if (irq >= GIC_NIRQ)
  458. goto bad_reg;
  459. if (irq < 29)
  460. value = 0;
  461. else if (irq < 32)
  462. value = ALL_CPU_MASK;
  463. s->irq_target[irq] = value & ALL_CPU_MASK;
  464. } else if (offset < 0xf00) {
  465. /* Interrupt Configuration. */
  466. irq = (offset - 0xc00) * 4 + GIC_BASE_IRQ;
  467. if (irq >= GIC_NIRQ)
  468. goto bad_reg;
  469. if (irq < 32)
  470. value |= 0xaa;
  471. for (i = 0; i < 4; i++) {
  472. if (value & (1 << (i * 2))) {
  473. GIC_SET_MODEL(irq + i);
  474. } else {
  475. GIC_CLEAR_MODEL(irq + i);
  476. }
  477. if (value & (2 << (i * 2))) {
  478. GIC_SET_TRIGGER(irq + i);
  479. } else {
  480. GIC_CLEAR_TRIGGER(irq + i);
  481. }
  482. }
  483. #endif
  484. } else {
  485. /* 0xf00 is only handled for 32-bit writes. */
  486. goto bad_reg;
  487. }
  488. gic_update(s);
  489. return;
  490. bad_reg:
  491. hw_error("gic_dist_writeb: Bad offset %x\n", (int)offset);
  492. }
  493. static void gic_dist_writew(void *opaque, target_phys_addr_t offset,
  494. uint32_t value)
  495. {
  496. gic_dist_writeb(opaque, offset, value & 0xff);
  497. gic_dist_writeb(opaque, offset + 1, value >> 8);
  498. }
  499. static void gic_dist_writel(void *opaque, target_phys_addr_t offset,
  500. uint32_t value)
  501. {
  502. gic_state *s = (gic_state *)opaque;
  503. #ifdef NVIC
  504. uint32_t addr;
  505. addr = offset;
  506. if (addr < 0x100 || (addr > 0xd00 && addr != 0xf00)) {
  507. nvic_writel(s, addr, value);
  508. return;
  509. }
  510. #endif
  511. if (offset == 0xf00) {
  512. int cpu;
  513. int irq;
  514. int mask;
  515. cpu = gic_get_current_cpu();
  516. irq = value & 0x3ff;
  517. switch ((value >> 24) & 3) {
  518. case 0:
  519. mask = (value >> 16) & ALL_CPU_MASK;
  520. break;
  521. case 1:
  522. mask = ALL_CPU_MASK ^ (1 << cpu);
  523. break;
  524. case 2:
  525. mask = 1 << cpu;
  526. break;
  527. default:
  528. DPRINTF("Bad Soft Int target filter\n");
  529. mask = ALL_CPU_MASK;
  530. break;
  531. }
  532. GIC_SET_PENDING(irq, mask);
  533. gic_update(s);
  534. return;
  535. }
  536. gic_dist_writew(opaque, offset, value & 0xffff);
  537. gic_dist_writew(opaque, offset + 2, value >> 16);
  538. }
  539. static const MemoryRegionOps gic_dist_ops = {
  540. .old_mmio = {
  541. .read = { gic_dist_readb, gic_dist_readw, gic_dist_readl, },
  542. .write = { gic_dist_writeb, gic_dist_writew, gic_dist_writel, },
  543. },
  544. .endianness = DEVICE_NATIVE_ENDIAN,
  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, ALL_CPU_MASK);
  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. for (j = 0; j < 32; j++)
  626. qemu_put_be32(f, s->priority1[j][i]);
  627. for (j = 0; j < GIC_NIRQ; j++)
  628. qemu_put_be32(f, s->last_active[j][i]);
  629. qemu_put_be32(f, s->priority_mask[i]);
  630. qemu_put_be32(f, s->running_irq[i]);
  631. qemu_put_be32(f, s->running_priority[i]);
  632. qemu_put_be32(f, s->current_pending[i]);
  633. }
  634. for (i = 0; i < GIC_NIRQ - 32; i++) {
  635. qemu_put_be32(f, s->priority2[i]);
  636. }
  637. for (i = 0; i < GIC_NIRQ; i++) {
  638. #ifndef NVIC
  639. qemu_put_be32(f, s->irq_target[i]);
  640. #endif
  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 != 2)
  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. for (j = 0; j < 32; j++)
  660. s->priority1[j][i] = qemu_get_be32(f);
  661. for (j = 0; j < GIC_NIRQ; j++)
  662. s->last_active[j][i] = qemu_get_be32(f);
  663. s->priority_mask[i] = qemu_get_be32(f);
  664. s->running_irq[i] = qemu_get_be32(f);
  665. s->running_priority[i] = qemu_get_be32(f);
  666. s->current_pending[i] = qemu_get_be32(f);
  667. }
  668. for (i = 0; i < GIC_NIRQ - 32; i++) {
  669. s->priority2[i] = qemu_get_be32(f);
  670. }
  671. for (i = 0; i < GIC_NIRQ; i++) {
  672. #ifndef NVIC
  673. s->irq_target[i] = qemu_get_be32(f);
  674. #endif
  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. memory_region_init_io(&s->iomem, &gic_dist_ops, s, "gic_dist", 0x1000);
  699. gic_reset(s);
  700. register_savevm(NULL, "arm_gic", -1, 2, gic_save, gic_load, s);
  701. }