arm_gic.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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 "sysbus.h"
  20. #include "arm_gic_internal.h"
  21. //#define DEBUG_GIC
  22. #ifdef DEBUG_GIC
  23. #define DPRINTF(fmt, ...) \
  24. do { fprintf(stderr, "arm_gic: " fmt , ## __VA_ARGS__); } while (0)
  25. #else
  26. #define DPRINTF(fmt, ...) do {} while(0)
  27. #endif
  28. static const uint8_t gic_id[] = {
  29. 0x90, 0x13, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1
  30. };
  31. #define NUM_CPU(s) ((s)->num_cpu)
  32. static inline int gic_get_current_cpu(GICState *s)
  33. {
  34. if (s->num_cpu > 1) {
  35. CPUState *cpu = ENV_GET_CPU(cpu_single_env);
  36. return 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(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(irq, cm))
  82. return;
  83. DPRINTF("Set %d pending cpu %d\n", irq, cpu);
  84. GIC_SET_PENDING(irq, cm);
  85. gic_update(s);
  86. }
  87. /* Process a change in an external IRQ input. */
  88. static void gic_set_irq(void *opaque, int irq, int level)
  89. {
  90. /* Meaning of the 'irq' parameter:
  91. * [0..N-1] : external interrupts
  92. * [N..N+31] : PPI (internal) interrupts for CPU 0
  93. * [N+32..N+63] : PPI (internal interrupts for CPU 1
  94. * ...
  95. */
  96. GICState *s = (GICState *)opaque;
  97. int cm, target;
  98. if (irq < (s->num_irq - GIC_INTERNAL)) {
  99. /* The first external input line is internal interrupt 32. */
  100. cm = ALL_CPU_MASK;
  101. irq += GIC_INTERNAL;
  102. target = GIC_TARGET(irq);
  103. } else {
  104. int cpu;
  105. irq -= (s->num_irq - GIC_INTERNAL);
  106. cpu = irq / GIC_INTERNAL;
  107. irq %= GIC_INTERNAL;
  108. cm = 1 << cpu;
  109. target = cm;
  110. }
  111. if (level == GIC_TEST_LEVEL(irq, cm)) {
  112. return;
  113. }
  114. if (level) {
  115. GIC_SET_LEVEL(irq, cm);
  116. if (GIC_TEST_TRIGGER(irq) || GIC_TEST_ENABLED(irq, cm)) {
  117. DPRINTF("Set %d pending mask %x\n", irq, target);
  118. GIC_SET_PENDING(irq, target);
  119. }
  120. } else {
  121. GIC_CLEAR_LEVEL(irq, cm);
  122. }
  123. gic_update(s);
  124. }
  125. static void gic_set_running_irq(GICState *s, int cpu, int irq)
  126. {
  127. s->running_irq[cpu] = irq;
  128. if (irq == 1023) {
  129. s->running_priority[cpu] = 0x100;
  130. } else {
  131. s->running_priority[cpu] = GIC_GET_PRIORITY(irq, cpu);
  132. }
  133. gic_update(s);
  134. }
  135. uint32_t gic_acknowledge_irq(GICState *s, int cpu)
  136. {
  137. int new_irq;
  138. int cm = 1 << cpu;
  139. new_irq = s->current_pending[cpu];
  140. if (new_irq == 1023
  141. || GIC_GET_PRIORITY(new_irq, cpu) >= s->running_priority[cpu]) {
  142. DPRINTF("ACK no pending IRQ\n");
  143. return 1023;
  144. }
  145. s->last_active[new_irq][cpu] = s->running_irq[cpu];
  146. /* Clear pending flags for both level and edge triggered interrupts.
  147. Level triggered IRQs will be reasserted once they become inactive. */
  148. GIC_CLEAR_PENDING(new_irq, GIC_TEST_MODEL(new_irq) ? ALL_CPU_MASK : cm);
  149. gic_set_running_irq(s, cpu, new_irq);
  150. DPRINTF("ACK %d\n", new_irq);
  151. return new_irq;
  152. }
  153. void gic_complete_irq(GICState *s, int cpu, int irq)
  154. {
  155. int update = 0;
  156. int cm = 1 << cpu;
  157. DPRINTF("EOI %d\n", irq);
  158. if (irq >= s->num_irq) {
  159. /* This handles two cases:
  160. * 1. If software writes the ID of a spurious interrupt [ie 1023]
  161. * to the GICC_EOIR, the GIC ignores that write.
  162. * 2. If software writes the number of a non-existent interrupt
  163. * this must be a subcase of "value written does not match the last
  164. * valid interrupt value read from the Interrupt Acknowledge
  165. * register" and so this is UNPREDICTABLE. We choose to ignore it.
  166. */
  167. return;
  168. }
  169. if (s->running_irq[cpu] == 1023)
  170. return; /* No active IRQ. */
  171. /* Mark level triggered interrupts as pending if they are still
  172. raised. */
  173. if (!GIC_TEST_TRIGGER(irq) && GIC_TEST_ENABLED(irq, cm)
  174. && GIC_TEST_LEVEL(irq, cm) && (GIC_TARGET(irq) & cm) != 0) {
  175. DPRINTF("Set %d pending mask %x\n", irq, cm);
  176. GIC_SET_PENDING(irq, cm);
  177. update = 1;
  178. }
  179. if (irq != s->running_irq[cpu]) {
  180. /* Complete an IRQ that is not currently running. */
  181. int tmp = s->running_irq[cpu];
  182. while (s->last_active[tmp][cpu] != 1023) {
  183. if (s->last_active[tmp][cpu] == irq) {
  184. s->last_active[tmp][cpu] = s->last_active[irq][cpu];
  185. break;
  186. }
  187. tmp = s->last_active[tmp][cpu];
  188. }
  189. if (update) {
  190. gic_update(s);
  191. }
  192. } else {
  193. /* Complete the current running IRQ. */
  194. gic_set_running_irq(s, cpu, s->last_active[s->running_irq[cpu]][cpu]);
  195. }
  196. }
  197. static uint32_t gic_dist_readb(void *opaque, hwaddr offset)
  198. {
  199. GICState *s = (GICState *)opaque;
  200. uint32_t res;
  201. int irq;
  202. int i;
  203. int cpu;
  204. int cm;
  205. int mask;
  206. cpu = gic_get_current_cpu(s);
  207. cm = 1 << cpu;
  208. if (offset < 0x100) {
  209. if (offset == 0)
  210. return s->enabled;
  211. if (offset == 4)
  212. return ((s->num_irq / 32) - 1) | ((NUM_CPU(s) - 1) << 5);
  213. if (offset < 0x08)
  214. return 0;
  215. if (offset >= 0x80) {
  216. /* Interrupt Security , RAZ/WI */
  217. return 0;
  218. }
  219. goto bad_reg;
  220. } else if (offset < 0x200) {
  221. /* Interrupt Set/Clear Enable. */
  222. if (offset < 0x180)
  223. irq = (offset - 0x100) * 8;
  224. else
  225. irq = (offset - 0x180) * 8;
  226. irq += GIC_BASE_IRQ;
  227. if (irq >= s->num_irq)
  228. goto bad_reg;
  229. res = 0;
  230. for (i = 0; i < 8; i++) {
  231. if (GIC_TEST_ENABLED(irq + i, cm)) {
  232. res |= (1 << i);
  233. }
  234. }
  235. } else if (offset < 0x300) {
  236. /* Interrupt Set/Clear Pending. */
  237. if (offset < 0x280)
  238. irq = (offset - 0x200) * 8;
  239. else
  240. irq = (offset - 0x280) * 8;
  241. irq += GIC_BASE_IRQ;
  242. if (irq >= s->num_irq)
  243. goto bad_reg;
  244. res = 0;
  245. mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK;
  246. for (i = 0; i < 8; i++) {
  247. if (GIC_TEST_PENDING(irq + i, mask)) {
  248. res |= (1 << i);
  249. }
  250. }
  251. } else if (offset < 0x400) {
  252. /* Interrupt Active. */
  253. irq = (offset - 0x300) * 8 + GIC_BASE_IRQ;
  254. if (irq >= s->num_irq)
  255. goto bad_reg;
  256. res = 0;
  257. mask = (irq < GIC_INTERNAL) ? cm : ALL_CPU_MASK;
  258. for (i = 0; i < 8; i++) {
  259. if (GIC_TEST_ACTIVE(irq + i, mask)) {
  260. res |= (1 << i);
  261. }
  262. }
  263. } else if (offset < 0x800) {
  264. /* Interrupt Priority. */
  265. irq = (offset - 0x400) + GIC_BASE_IRQ;
  266. if (irq >= s->num_irq)
  267. goto bad_reg;
  268. res = GIC_GET_PRIORITY(irq, cpu);
  269. } else if (offset < 0xc00) {
  270. /* Interrupt CPU Target. */
  271. if (s->num_cpu == 1 && s->revision != REV_11MPCORE) {
  272. /* For uniprocessor GICs these RAZ/WI */
  273. res = 0;
  274. } else {
  275. irq = (offset - 0x800) + GIC_BASE_IRQ;
  276. if (irq >= s->num_irq) {
  277. goto bad_reg;
  278. }
  279. if (irq >= 29 && irq <= 31) {
  280. res = cm;
  281. } else {
  282. res = GIC_TARGET(irq);
  283. }
  284. }
  285. } else if (offset < 0xf00) {
  286. /* Interrupt Configuration. */
  287. irq = (offset - 0xc00) * 2 + GIC_BASE_IRQ;
  288. if (irq >= s->num_irq)
  289. goto bad_reg;
  290. res = 0;
  291. for (i = 0; i < 4; i++) {
  292. if (GIC_TEST_MODEL(irq + i))
  293. res |= (1 << (i * 2));
  294. if (GIC_TEST_TRIGGER(irq + i))
  295. res |= (2 << (i * 2));
  296. }
  297. } else if (offset < 0xfe0) {
  298. goto bad_reg;
  299. } else /* offset >= 0xfe0 */ {
  300. if (offset & 3) {
  301. res = 0;
  302. } else {
  303. res = gic_id[(offset - 0xfe0) >> 2];
  304. }
  305. }
  306. return res;
  307. bad_reg:
  308. qemu_log_mask(LOG_GUEST_ERROR,
  309. "gic_dist_readb: Bad offset %x\n", (int)offset);
  310. return 0;
  311. }
  312. static uint32_t gic_dist_readw(void *opaque, hwaddr offset)
  313. {
  314. uint32_t val;
  315. val = gic_dist_readb(opaque, offset);
  316. val |= gic_dist_readb(opaque, offset + 1) << 8;
  317. return val;
  318. }
  319. static uint32_t gic_dist_readl(void *opaque, hwaddr offset)
  320. {
  321. uint32_t val;
  322. val = gic_dist_readw(opaque, offset);
  323. val |= gic_dist_readw(opaque, offset + 2) << 16;
  324. return val;
  325. }
  326. static void gic_dist_writeb(void *opaque, hwaddr offset,
  327. uint32_t value)
  328. {
  329. GICState *s = (GICState *)opaque;
  330. int irq;
  331. int i;
  332. int cpu;
  333. cpu = gic_get_current_cpu(s);
  334. if (offset < 0x100) {
  335. if (offset == 0) {
  336. s->enabled = (value & 1);
  337. DPRINTF("Distribution %sabled\n", s->enabled ? "En" : "Dis");
  338. } else if (offset < 4) {
  339. /* ignored. */
  340. } else if (offset >= 0x80) {
  341. /* Interrupt Security Registers, RAZ/WI */
  342. } else {
  343. goto bad_reg;
  344. }
  345. } else if (offset < 0x180) {
  346. /* Interrupt Set Enable. */
  347. irq = (offset - 0x100) * 8 + GIC_BASE_IRQ;
  348. if (irq >= s->num_irq)
  349. goto bad_reg;
  350. if (irq < 16)
  351. value = 0xff;
  352. for (i = 0; i < 8; i++) {
  353. if (value & (1 << i)) {
  354. int mask =
  355. (irq < GIC_INTERNAL) ? (1 << cpu) : GIC_TARGET(irq + i);
  356. int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
  357. if (!GIC_TEST_ENABLED(irq + i, cm)) {
  358. DPRINTF("Enabled IRQ %d\n", irq + i);
  359. }
  360. GIC_SET_ENABLED(irq + i, cm);
  361. /* If a raised level triggered IRQ enabled then mark
  362. is as pending. */
  363. if (GIC_TEST_LEVEL(irq + i, mask)
  364. && !GIC_TEST_TRIGGER(irq + i)) {
  365. DPRINTF("Set %d pending mask %x\n", irq + i, mask);
  366. GIC_SET_PENDING(irq + i, mask);
  367. }
  368. }
  369. }
  370. } else if (offset < 0x200) {
  371. /* Interrupt Clear Enable. */
  372. irq = (offset - 0x180) * 8 + GIC_BASE_IRQ;
  373. if (irq >= s->num_irq)
  374. goto bad_reg;
  375. if (irq < 16)
  376. value = 0;
  377. for (i = 0; i < 8; i++) {
  378. if (value & (1 << i)) {
  379. int cm = (irq < GIC_INTERNAL) ? (1 << cpu) : ALL_CPU_MASK;
  380. if (GIC_TEST_ENABLED(irq + i, cm)) {
  381. DPRINTF("Disabled IRQ %d\n", irq + i);
  382. }
  383. GIC_CLEAR_ENABLED(irq + i, cm);
  384. }
  385. }
  386. } else if (offset < 0x280) {
  387. /* Interrupt Set Pending. */
  388. irq = (offset - 0x200) * 8 + GIC_BASE_IRQ;
  389. if (irq >= s->num_irq)
  390. goto bad_reg;
  391. if (irq < 16)
  392. irq = 0;
  393. for (i = 0; i < 8; i++) {
  394. if (value & (1 << i)) {
  395. GIC_SET_PENDING(irq + i, GIC_TARGET(irq + i));
  396. }
  397. }
  398. } else if (offset < 0x300) {
  399. /* Interrupt Clear Pending. */
  400. irq = (offset - 0x280) * 8 + GIC_BASE_IRQ;
  401. if (irq >= s->num_irq)
  402. goto bad_reg;
  403. for (i = 0; i < 8; i++) {
  404. /* ??? This currently clears the pending bit for all CPUs, even
  405. for per-CPU interrupts. It's unclear whether this is the
  406. corect behavior. */
  407. if (value & (1 << i)) {
  408. GIC_CLEAR_PENDING(irq + i, ALL_CPU_MASK);
  409. }
  410. }
  411. } else if (offset < 0x400) {
  412. /* Interrupt Active. */
  413. goto bad_reg;
  414. } else if (offset < 0x800) {
  415. /* Interrupt Priority. */
  416. irq = (offset - 0x400) + GIC_BASE_IRQ;
  417. if (irq >= s->num_irq)
  418. goto bad_reg;
  419. if (irq < GIC_INTERNAL) {
  420. s->priority1[irq][cpu] = value;
  421. } else {
  422. s->priority2[irq - GIC_INTERNAL] = value;
  423. }
  424. } else if (offset < 0xc00) {
  425. /* Interrupt CPU Target. RAZ/WI on uniprocessor GICs, with the
  426. * annoying exception of the 11MPCore's GIC.
  427. */
  428. if (s->num_cpu != 1 || s->revision == REV_11MPCORE) {
  429. irq = (offset - 0x800) + GIC_BASE_IRQ;
  430. if (irq >= s->num_irq) {
  431. goto bad_reg;
  432. }
  433. if (irq < 29) {
  434. value = 0;
  435. } else if (irq < GIC_INTERNAL) {
  436. value = ALL_CPU_MASK;
  437. }
  438. s->irq_target[irq] = value & ALL_CPU_MASK;
  439. }
  440. } else if (offset < 0xf00) {
  441. /* Interrupt Configuration. */
  442. irq = (offset - 0xc00) * 4 + GIC_BASE_IRQ;
  443. if (irq >= s->num_irq)
  444. goto bad_reg;
  445. if (irq < GIC_INTERNAL)
  446. value |= 0xaa;
  447. for (i = 0; i < 4; i++) {
  448. if (value & (1 << (i * 2))) {
  449. GIC_SET_MODEL(irq + i);
  450. } else {
  451. GIC_CLEAR_MODEL(irq + i);
  452. }
  453. if (value & (2 << (i * 2))) {
  454. GIC_SET_TRIGGER(irq + i);
  455. } else {
  456. GIC_CLEAR_TRIGGER(irq + i);
  457. }
  458. }
  459. } else {
  460. /* 0xf00 is only handled for 32-bit writes. */
  461. goto bad_reg;
  462. }
  463. gic_update(s);
  464. return;
  465. bad_reg:
  466. qemu_log_mask(LOG_GUEST_ERROR,
  467. "gic_dist_writeb: Bad offset %x\n", (int)offset);
  468. }
  469. static void gic_dist_writew(void *opaque, hwaddr offset,
  470. uint32_t value)
  471. {
  472. gic_dist_writeb(opaque, offset, value & 0xff);
  473. gic_dist_writeb(opaque, offset + 1, value >> 8);
  474. }
  475. static void gic_dist_writel(void *opaque, hwaddr offset,
  476. uint32_t value)
  477. {
  478. GICState *s = (GICState *)opaque;
  479. if (offset == 0xf00) {
  480. int cpu;
  481. int irq;
  482. int mask;
  483. cpu = gic_get_current_cpu(s);
  484. irq = value & 0x3ff;
  485. switch ((value >> 24) & 3) {
  486. case 0:
  487. mask = (value >> 16) & ALL_CPU_MASK;
  488. break;
  489. case 1:
  490. mask = ALL_CPU_MASK ^ (1 << cpu);
  491. break;
  492. case 2:
  493. mask = 1 << cpu;
  494. break;
  495. default:
  496. DPRINTF("Bad Soft Int target filter\n");
  497. mask = ALL_CPU_MASK;
  498. break;
  499. }
  500. GIC_SET_PENDING(irq, mask);
  501. gic_update(s);
  502. return;
  503. }
  504. gic_dist_writew(opaque, offset, value & 0xffff);
  505. gic_dist_writew(opaque, offset + 2, value >> 16);
  506. }
  507. static const MemoryRegionOps gic_dist_ops = {
  508. .old_mmio = {
  509. .read = { gic_dist_readb, gic_dist_readw, gic_dist_readl, },
  510. .write = { gic_dist_writeb, gic_dist_writew, gic_dist_writel, },
  511. },
  512. .endianness = DEVICE_NATIVE_ENDIAN,
  513. };
  514. static uint32_t gic_cpu_read(GICState *s, int cpu, int offset)
  515. {
  516. switch (offset) {
  517. case 0x00: /* Control */
  518. return s->cpu_enabled[cpu];
  519. case 0x04: /* Priority mask */
  520. return s->priority_mask[cpu];
  521. case 0x08: /* Binary Point */
  522. /* ??? Not implemented. */
  523. return 0;
  524. case 0x0c: /* Acknowledge */
  525. return gic_acknowledge_irq(s, cpu);
  526. case 0x14: /* Running Priority */
  527. return s->running_priority[cpu];
  528. case 0x18: /* Highest Pending Interrupt */
  529. return s->current_pending[cpu];
  530. default:
  531. qemu_log_mask(LOG_GUEST_ERROR,
  532. "gic_cpu_read: Bad offset %x\n", (int)offset);
  533. return 0;
  534. }
  535. }
  536. static void gic_cpu_write(GICState *s, int cpu, int offset, uint32_t value)
  537. {
  538. switch (offset) {
  539. case 0x00: /* Control */
  540. s->cpu_enabled[cpu] = (value & 1);
  541. DPRINTF("CPU %d %sabled\n", cpu, s->cpu_enabled[cpu] ? "En" : "Dis");
  542. break;
  543. case 0x04: /* Priority mask */
  544. s->priority_mask[cpu] = (value & 0xff);
  545. break;
  546. case 0x08: /* Binary Point */
  547. /* ??? Not implemented. */
  548. break;
  549. case 0x10: /* End Of Interrupt */
  550. return gic_complete_irq(s, cpu, value & 0x3ff);
  551. default:
  552. qemu_log_mask(LOG_GUEST_ERROR,
  553. "gic_cpu_write: Bad offset %x\n", (int)offset);
  554. return;
  555. }
  556. gic_update(s);
  557. }
  558. /* Wrappers to read/write the GIC CPU interface for the current CPU */
  559. static uint64_t gic_thiscpu_read(void *opaque, hwaddr addr,
  560. unsigned size)
  561. {
  562. GICState *s = (GICState *)opaque;
  563. return gic_cpu_read(s, gic_get_current_cpu(s), addr);
  564. }
  565. static void gic_thiscpu_write(void *opaque, hwaddr addr,
  566. uint64_t value, unsigned size)
  567. {
  568. GICState *s = (GICState *)opaque;
  569. gic_cpu_write(s, gic_get_current_cpu(s), addr, value);
  570. }
  571. /* Wrappers to read/write the GIC CPU interface for a specific CPU.
  572. * These just decode the opaque pointer into GICState* + cpu id.
  573. */
  574. static uint64_t gic_do_cpu_read(void *opaque, hwaddr addr,
  575. unsigned size)
  576. {
  577. GICState **backref = (GICState **)opaque;
  578. GICState *s = *backref;
  579. int id = (backref - s->backref);
  580. return gic_cpu_read(s, id, addr);
  581. }
  582. static void gic_do_cpu_write(void *opaque, hwaddr addr,
  583. uint64_t value, unsigned size)
  584. {
  585. GICState **backref = (GICState **)opaque;
  586. GICState *s = *backref;
  587. int id = (backref - s->backref);
  588. gic_cpu_write(s, id, addr, value);
  589. }
  590. static const MemoryRegionOps gic_thiscpu_ops = {
  591. .read = gic_thiscpu_read,
  592. .write = gic_thiscpu_write,
  593. .endianness = DEVICE_NATIVE_ENDIAN,
  594. };
  595. static const MemoryRegionOps gic_cpu_ops = {
  596. .read = gic_do_cpu_read,
  597. .write = gic_do_cpu_write,
  598. .endianness = DEVICE_NATIVE_ENDIAN,
  599. };
  600. void gic_init_irqs_and_distributor(GICState *s, int num_irq)
  601. {
  602. int i;
  603. i = s->num_irq - GIC_INTERNAL;
  604. /* For the GIC, also expose incoming GPIO lines for PPIs for each CPU.
  605. * GPIO array layout is thus:
  606. * [0..N-1] SPIs
  607. * [N..N+31] PPIs for CPU 0
  608. * [N+32..N+63] PPIs for CPU 1
  609. * ...
  610. */
  611. if (s->revision != REV_NVIC) {
  612. i += (GIC_INTERNAL * s->num_cpu);
  613. }
  614. qdev_init_gpio_in(&s->busdev.qdev, gic_set_irq, i);
  615. for (i = 0; i < NUM_CPU(s); i++) {
  616. sysbus_init_irq(&s->busdev, &s->parent_irq[i]);
  617. }
  618. memory_region_init_io(&s->iomem, &gic_dist_ops, s, "gic_dist", 0x1000);
  619. }
  620. static int arm_gic_init(SysBusDevice *dev)
  621. {
  622. /* Device instance init function for the GIC sysbus device */
  623. int i;
  624. GICState *s = FROM_SYSBUS(GICState, dev);
  625. ARMGICClass *agc = ARM_GIC_GET_CLASS(s);
  626. agc->parent_init(dev);
  627. gic_init_irqs_and_distributor(s, s->num_irq);
  628. /* Memory regions for the CPU interfaces (NVIC doesn't have these):
  629. * a region for "CPU interface for this core", then a region for
  630. * "CPU interface for core 0", "for core 1", ...
  631. * NB that the memory region size of 0x100 applies for the 11MPCore
  632. * and also cores following the GIC v1 spec (ie A9).
  633. * GIC v2 defines a larger memory region (0x1000) so this will need
  634. * to be extended when we implement A15.
  635. */
  636. memory_region_init_io(&s->cpuiomem[0], &gic_thiscpu_ops, s,
  637. "gic_cpu", 0x100);
  638. for (i = 0; i < NUM_CPU(s); i++) {
  639. s->backref[i] = s;
  640. memory_region_init_io(&s->cpuiomem[i+1], &gic_cpu_ops, &s->backref[i],
  641. "gic_cpu", 0x100);
  642. }
  643. /* Distributor */
  644. sysbus_init_mmio(dev, &s->iomem);
  645. /* cpu interfaces (one for "current cpu" plus one per cpu) */
  646. for (i = 0; i <= NUM_CPU(s); i++) {
  647. sysbus_init_mmio(dev, &s->cpuiomem[i]);
  648. }
  649. return 0;
  650. }
  651. static void arm_gic_class_init(ObjectClass *klass, void *data)
  652. {
  653. DeviceClass *dc = DEVICE_CLASS(klass);
  654. SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(klass);
  655. ARMGICClass *agc = ARM_GIC_CLASS(klass);
  656. agc->parent_init = sbc->init;
  657. sbc->init = arm_gic_init;
  658. dc->no_user = 1;
  659. }
  660. static const TypeInfo arm_gic_info = {
  661. .name = TYPE_ARM_GIC,
  662. .parent = TYPE_ARM_GIC_COMMON,
  663. .instance_size = sizeof(GICState),
  664. .class_init = arm_gic_class_init,
  665. .class_size = sizeof(ARMGICClass),
  666. };
  667. static void arm_gic_register_types(void)
  668. {
  669. type_register_static(&arm_gic_info);
  670. }
  671. type_init(arm_gic_register_types)