bcm2836_control.c 13 KB

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