bcm2836_control.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Rasperry Pi 2 emulation ARM control logic module.
  3. * Copyright (c) 2015, Microsoft
  4. * Written by Andrew Baumann
  5. *
  6. * Based on bcm2835_ic.c (Raspberry Pi emulation) (c) 2012 Gregory Estrade
  7. *
  8. * At present, only implements interrupt routing, and mailboxes (i.e.,
  9. * not PMU interrupt, or AXI counters).
  10. *
  11. * ARM Local Timer IRQ Copyright (c) 2019. Zoltán Baldaszti
  12. *
  13. * Ref:
  14. * https://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2836/QA7_rev3.4.pdf
  15. *
  16. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  17. * See the COPYING file in the top-level directory.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "hw/intc/bcm2836_control.h"
  21. #include "hw/irq.h"
  22. #include "migration/vmstate.h"
  23. #include "qemu/log.h"
  24. #include "qemu/module.h"
  25. #define REG_GPU_ROUTE 0x0c
  26. #define REG_LOCALTIMERROUTING 0x24
  27. #define REG_LOCALTIMERCONTROL 0x34
  28. #define REG_LOCALTIMERACK 0x38
  29. #define REG_TIMERCONTROL 0x40
  30. #define REG_MBOXCONTROL 0x50
  31. #define REG_IRQSRC 0x60
  32. #define REG_FIQSRC 0x70
  33. #define REG_MBOX0_WR 0x80
  34. #define REG_MBOX0_RDCLR 0xc0
  35. #define REG_LIMIT 0x100
  36. #define IRQ_BIT(cntrl, num) (((cntrl) & (1 << (num))) != 0)
  37. #define FIQ_BIT(cntrl, num) (((cntrl) & (1 << ((num) + 4))) != 0)
  38. #define IRQ_CNTPSIRQ 0
  39. #define IRQ_CNTPNSIRQ 1
  40. #define IRQ_CNTHPIRQ 2
  41. #define IRQ_CNTVIRQ 3
  42. #define IRQ_MAILBOX0 4
  43. #define IRQ_MAILBOX1 5
  44. #define IRQ_MAILBOX2 6
  45. #define IRQ_MAILBOX3 7
  46. #define IRQ_GPU 8
  47. #define IRQ_PMU 9
  48. #define IRQ_AXI 10
  49. #define IRQ_TIMER 11
  50. #define IRQ_MAX IRQ_TIMER
  51. #define LOCALTIMER_FREQ 38400000
  52. #define LOCALTIMER_INTFLAG (1 << 31)
  53. #define LOCALTIMER_RELOAD (1 << 30)
  54. #define LOCALTIMER_INTENABLE (1 << 29)
  55. #define LOCALTIMER_ENABLE (1 << 28)
  56. #define LOCALTIMER_VALUE(x) ((x) & 0xfffffff)
  57. static void deliver_local(BCM2836ControlState *s, uint8_t core, uint8_t irq,
  58. uint32_t controlreg, uint8_t controlidx)
  59. {
  60. if (FIQ_BIT(controlreg, controlidx)) {
  61. /* deliver a FIQ */
  62. s->fiqsrc[core] |= (uint32_t)1 << irq;
  63. } else if (IRQ_BIT(controlreg, controlidx)) {
  64. /* deliver an IRQ */
  65. s->irqsrc[core] |= (uint32_t)1 << irq;
  66. } else {
  67. /* the interrupt is masked */
  68. }
  69. }
  70. /* Update interrupts. */
  71. static void bcm2836_control_update(BCM2836ControlState *s)
  72. {
  73. int i, j;
  74. /* reset pending IRQs/FIQs */
  75. for (i = 0; i < BCM2836_NCORES; i++) {
  76. s->irqsrc[i] = s->fiqsrc[i] = 0;
  77. }
  78. /* apply routing logic, update status regs */
  79. if (s->gpu_irq) {
  80. assert(s->route_gpu_irq < BCM2836_NCORES);
  81. s->irqsrc[s->route_gpu_irq] |= (uint32_t)1 << IRQ_GPU;
  82. }
  83. if (s->gpu_fiq) {
  84. assert(s->route_gpu_fiq < BCM2836_NCORES);
  85. s->fiqsrc[s->route_gpu_fiq] |= (uint32_t)1 << IRQ_GPU;
  86. }
  87. /*
  88. * handle the control module 'local timer' interrupt for one of the
  89. * cores' IRQ/FIQ; this is distinct from the per-CPU timer
  90. * interrupts handled below.
  91. */
  92. if ((s->local_timer_control & LOCALTIMER_INTENABLE) &&
  93. (s->local_timer_control & LOCALTIMER_INTFLAG)) {
  94. if (s->route_localtimer & 4) {
  95. s->fiqsrc[(s->route_localtimer & 3)] |= (uint32_t)1 << IRQ_TIMER;
  96. } else {
  97. s->irqsrc[(s->route_localtimer & 3)] |= (uint32_t)1 << IRQ_TIMER;
  98. }
  99. }
  100. for (i = 0; i < BCM2836_NCORES; i++) {
  101. /* handle local timer interrupts for this core */
  102. if (s->timerirqs[i]) {
  103. assert(s->timerirqs[i] < (1 << (IRQ_CNTVIRQ + 1))); /* sane mask? */
  104. for (j = 0; j <= IRQ_CNTVIRQ; j++) {
  105. if ((s->timerirqs[i] & (1 << j)) != 0) {
  106. /* local interrupt j is set */
  107. deliver_local(s, i, j, s->timercontrol[i], j);
  108. }
  109. }
  110. }
  111. /* handle mailboxes for this core */
  112. for (j = 0; j < BCM2836_MBPERCORE; j++) {
  113. if (s->mailboxes[i * BCM2836_MBPERCORE + j] != 0) {
  114. /* mailbox j is set */
  115. deliver_local(s, i, j + IRQ_MAILBOX0, s->mailboxcontrol[i], j);
  116. }
  117. }
  118. }
  119. /* call set_irq appropriately for each output */
  120. for (i = 0; i < BCM2836_NCORES; i++) {
  121. qemu_set_irq(s->irq[i], s->irqsrc[i] != 0);
  122. qemu_set_irq(s->fiq[i], s->fiqsrc[i] != 0);
  123. }
  124. }
  125. static void bcm2836_control_set_local_irq(void *opaque, int core, int local_irq,
  126. int level)
  127. {
  128. BCM2836ControlState *s = opaque;
  129. assert(core >= 0 && core < BCM2836_NCORES);
  130. assert(local_irq >= 0 && local_irq <= IRQ_CNTVIRQ);
  131. s->timerirqs[core] = deposit32(s->timerirqs[core], local_irq, 1, !!level);
  132. bcm2836_control_update(s);
  133. }
  134. /* XXX: the following wrapper functions are a kludgy workaround,
  135. * needed because I can't seem to pass useful information in the "irq"
  136. * parameter when using named interrupts. Feel free to clean this up!
  137. */
  138. static void bcm2836_control_set_local_irq0(void *opaque, int core, int level)
  139. {
  140. bcm2836_control_set_local_irq(opaque, core, IRQ_CNTPSIRQ, level);
  141. }
  142. static void bcm2836_control_set_local_irq1(void *opaque, int core, int level)
  143. {
  144. bcm2836_control_set_local_irq(opaque, core, IRQ_CNTPNSIRQ, level);
  145. }
  146. static void bcm2836_control_set_local_irq2(void *opaque, int core, int level)
  147. {
  148. bcm2836_control_set_local_irq(opaque, core, IRQ_CNTHPIRQ, level);
  149. }
  150. static void bcm2836_control_set_local_irq3(void *opaque, int core, int level)
  151. {
  152. bcm2836_control_set_local_irq(opaque, core, IRQ_CNTVIRQ, level);
  153. }
  154. static void bcm2836_control_set_gpu_irq(void *opaque, int irq, int level)
  155. {
  156. BCM2836ControlState *s = opaque;
  157. s->gpu_irq = level;
  158. bcm2836_control_update(s);
  159. }
  160. static void bcm2836_control_set_gpu_fiq(void *opaque, int irq, int level)
  161. {
  162. BCM2836ControlState *s = opaque;
  163. s->gpu_fiq = level;
  164. bcm2836_control_update(s);
  165. }
  166. static void bcm2836_control_local_timer_set_next(void *opaque)
  167. {
  168. BCM2836ControlState *s = opaque;
  169. uint64_t next_event;
  170. assert(LOCALTIMER_VALUE(s->local_timer_control) > 0);
  171. next_event = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
  172. muldiv64(LOCALTIMER_VALUE(s->local_timer_control),
  173. NANOSECONDS_PER_SECOND, LOCALTIMER_FREQ);
  174. timer_mod(&s->timer, next_event);
  175. }
  176. static void bcm2836_control_local_timer_tick(void *opaque)
  177. {
  178. BCM2836ControlState *s = opaque;
  179. bcm2836_control_local_timer_set_next(s);
  180. s->local_timer_control |= LOCALTIMER_INTFLAG;
  181. bcm2836_control_update(s);
  182. }
  183. static void bcm2836_control_local_timer_control(void *opaque, uint32_t val)
  184. {
  185. BCM2836ControlState *s = opaque;
  186. s->local_timer_control = val;
  187. if (val & LOCALTIMER_ENABLE) {
  188. bcm2836_control_local_timer_set_next(s);
  189. } else {
  190. timer_del(&s->timer);
  191. }
  192. }
  193. static void bcm2836_control_local_timer_ack(void *opaque, uint32_t val)
  194. {
  195. BCM2836ControlState *s = opaque;
  196. if (val & LOCALTIMER_INTFLAG) {
  197. s->local_timer_control &= ~LOCALTIMER_INTFLAG;
  198. }
  199. if ((val & LOCALTIMER_RELOAD) &&
  200. (s->local_timer_control & LOCALTIMER_ENABLE)) {
  201. bcm2836_control_local_timer_set_next(s);
  202. }
  203. }
  204. static uint64_t bcm2836_control_read(void *opaque, hwaddr offset, unsigned size)
  205. {
  206. BCM2836ControlState *s = opaque;
  207. if (offset == REG_GPU_ROUTE) {
  208. assert(s->route_gpu_fiq < BCM2836_NCORES
  209. && s->route_gpu_irq < BCM2836_NCORES);
  210. return ((uint32_t)s->route_gpu_fiq << 2) | s->route_gpu_irq;
  211. } else if (offset == REG_LOCALTIMERROUTING) {
  212. return s->route_localtimer;
  213. } else if (offset == REG_LOCALTIMERCONTROL) {
  214. return s->local_timer_control;
  215. } else if (offset == REG_LOCALTIMERACK) {
  216. return 0;
  217. } else if (offset >= REG_TIMERCONTROL && offset < REG_MBOXCONTROL) {
  218. return s->timercontrol[(offset - REG_TIMERCONTROL) >> 2];
  219. } else if (offset >= REG_MBOXCONTROL && offset < REG_IRQSRC) {
  220. return s->mailboxcontrol[(offset - REG_MBOXCONTROL) >> 2];
  221. } else if (offset >= REG_IRQSRC && offset < REG_FIQSRC) {
  222. return s->irqsrc[(offset - REG_IRQSRC) >> 2];
  223. } else if (offset >= REG_FIQSRC && offset < REG_MBOX0_WR) {
  224. return s->fiqsrc[(offset - REG_FIQSRC) >> 2];
  225. } else if (offset >= REG_MBOX0_RDCLR && offset < REG_LIMIT) {
  226. return s->mailboxes[(offset - REG_MBOX0_RDCLR) >> 2];
  227. } else {
  228. qemu_log_mask(LOG_UNIMP, "%s: Unsupported offset 0x%"HWADDR_PRIx"\n",
  229. __func__, offset);
  230. return 0;
  231. }
  232. }
  233. static void bcm2836_control_write(void *opaque, hwaddr offset,
  234. uint64_t val, unsigned size)
  235. {
  236. BCM2836ControlState *s = opaque;
  237. if (offset == REG_GPU_ROUTE) {
  238. s->route_gpu_irq = val & 0x3;
  239. s->route_gpu_fiq = (val >> 2) & 0x3;
  240. } else if (offset == REG_LOCALTIMERROUTING) {
  241. s->route_localtimer = val & 7;
  242. } else if (offset == REG_LOCALTIMERCONTROL) {
  243. bcm2836_control_local_timer_control(s, val);
  244. } else if (offset == REG_LOCALTIMERACK) {
  245. bcm2836_control_local_timer_ack(s, val);
  246. } else if (offset >= REG_TIMERCONTROL && offset < REG_MBOXCONTROL) {
  247. s->timercontrol[(offset - REG_TIMERCONTROL) >> 2] = val & 0xff;
  248. } else if (offset >= REG_MBOXCONTROL && offset < REG_IRQSRC) {
  249. s->mailboxcontrol[(offset - REG_MBOXCONTROL) >> 2] = val & 0xff;
  250. } else if (offset >= REG_MBOX0_WR && offset < REG_MBOX0_RDCLR) {
  251. s->mailboxes[(offset - REG_MBOX0_WR) >> 2] |= val;
  252. } else if (offset >= REG_MBOX0_RDCLR && offset < REG_LIMIT) {
  253. s->mailboxes[(offset - REG_MBOX0_RDCLR) >> 2] &= ~val;
  254. } else {
  255. qemu_log_mask(LOG_UNIMP, "%s: Unsupported offset 0x%"HWADDR_PRIx
  256. " value 0x%"PRIx64"\n",
  257. __func__, offset, val);
  258. return;
  259. }
  260. bcm2836_control_update(s);
  261. }
  262. static const MemoryRegionOps bcm2836_control_ops = {
  263. .read = bcm2836_control_read,
  264. .write = bcm2836_control_write,
  265. .endianness = DEVICE_NATIVE_ENDIAN,
  266. .valid.min_access_size = 4,
  267. .valid.max_access_size = 4,
  268. };
  269. static void bcm2836_control_reset(DeviceState *d)
  270. {
  271. BCM2836ControlState *s = BCM2836_CONTROL(d);
  272. int i;
  273. s->route_gpu_irq = s->route_gpu_fiq = 0;
  274. timer_del(&s->timer);
  275. s->route_localtimer = 0;
  276. s->local_timer_control = 0;
  277. for (i = 0; i < BCM2836_NCORES; i++) {
  278. s->timercontrol[i] = 0;
  279. s->mailboxcontrol[i] = 0;
  280. }
  281. for (i = 0; i < BCM2836_NCORES * BCM2836_MBPERCORE; i++) {
  282. s->mailboxes[i] = 0;
  283. }
  284. }
  285. static void bcm2836_control_init(Object *obj)
  286. {
  287. BCM2836ControlState *s = BCM2836_CONTROL(obj);
  288. DeviceState *dev = DEVICE(obj);
  289. memory_region_init_io(&s->iomem, obj, &bcm2836_control_ops, s,
  290. TYPE_BCM2836_CONTROL, REG_LIMIT);
  291. sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem);
  292. /* inputs from each CPU core */
  293. qdev_init_gpio_in_named(dev, bcm2836_control_set_local_irq0, "cntpsirq",
  294. BCM2836_NCORES);
  295. qdev_init_gpio_in_named(dev, bcm2836_control_set_local_irq1, "cntpnsirq",
  296. BCM2836_NCORES);
  297. qdev_init_gpio_in_named(dev, bcm2836_control_set_local_irq2, "cnthpirq",
  298. BCM2836_NCORES);
  299. qdev_init_gpio_in_named(dev, bcm2836_control_set_local_irq3, "cntvirq",
  300. BCM2836_NCORES);
  301. /* IRQ and FIQ inputs from upstream bcm2835 controller */
  302. qdev_init_gpio_in_named(dev, bcm2836_control_set_gpu_irq, "gpu-irq", 1);
  303. qdev_init_gpio_in_named(dev, bcm2836_control_set_gpu_fiq, "gpu-fiq", 1);
  304. /* outputs to CPU cores */
  305. qdev_init_gpio_out_named(dev, s->irq, "irq", BCM2836_NCORES);
  306. qdev_init_gpio_out_named(dev, s->fiq, "fiq", BCM2836_NCORES);
  307. /* create a qemu virtual timer */
  308. timer_init_ns(&s->timer, QEMU_CLOCK_VIRTUAL,
  309. bcm2836_control_local_timer_tick, s);
  310. }
  311. static const VMStateDescription vmstate_bcm2836_control = {
  312. .name = TYPE_BCM2836_CONTROL,
  313. .version_id = 2,
  314. .minimum_version_id = 1,
  315. .fields = (const VMStateField[]) {
  316. VMSTATE_UINT32_ARRAY(mailboxes, BCM2836ControlState,
  317. BCM2836_NCORES * BCM2836_MBPERCORE),
  318. VMSTATE_UINT8(route_gpu_irq, BCM2836ControlState),
  319. VMSTATE_UINT8(route_gpu_fiq, BCM2836ControlState),
  320. VMSTATE_UINT32_ARRAY(timercontrol, BCM2836ControlState, BCM2836_NCORES),
  321. VMSTATE_UINT32_ARRAY(mailboxcontrol, BCM2836ControlState,
  322. BCM2836_NCORES),
  323. VMSTATE_TIMER_V(timer, BCM2836ControlState, 2),
  324. VMSTATE_UINT32_V(local_timer_control, BCM2836ControlState, 2),
  325. VMSTATE_UINT8_V(route_localtimer, BCM2836ControlState, 2),
  326. VMSTATE_END_OF_LIST()
  327. }
  328. };
  329. static void bcm2836_control_class_init(ObjectClass *klass, void *data)
  330. {
  331. DeviceClass *dc = DEVICE_CLASS(klass);
  332. device_class_set_legacy_reset(dc, bcm2836_control_reset);
  333. dc->vmsd = &vmstate_bcm2836_control;
  334. }
  335. static const TypeInfo bcm2836_control_info = {
  336. .name = TYPE_BCM2836_CONTROL,
  337. .parent = TYPE_SYS_BUS_DEVICE,
  338. .instance_size = sizeof(BCM2836ControlState),
  339. .class_init = bcm2836_control_class_init,
  340. .instance_init = bcm2836_control_init,
  341. };
  342. static void bcm2836_control_register_types(void)
  343. {
  344. type_register_static(&bcm2836_control_info);
  345. }
  346. type_init(bcm2836_control_register_types)