arm_gic.c 21 KB

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