goldfish_pic.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * SPDX-License-Identifier: GPL-2.0-or-later
  3. *
  4. * Goldfish PIC
  5. *
  6. * (c) 2020 Laurent Vivier <laurent@vivier.eu>
  7. *
  8. */
  9. #include "qemu/osdep.h"
  10. #include "hw/irq.h"
  11. #include "hw/qdev-properties.h"
  12. #include "hw/sysbus.h"
  13. #include "migration/vmstate.h"
  14. #include "monitor/monitor.h"
  15. #include "qemu/log.h"
  16. #include "trace.h"
  17. #include "hw/intc/intc.h"
  18. #include "hw/intc/goldfish_pic.h"
  19. /* registers */
  20. enum {
  21. REG_STATUS = 0x00,
  22. REG_IRQ_PENDING = 0x04,
  23. REG_IRQ_DISABLE_ALL = 0x08,
  24. REG_DISABLE = 0x0c,
  25. REG_ENABLE = 0x10,
  26. };
  27. static bool goldfish_pic_get_statistics(InterruptStatsProvider *obj,
  28. uint64_t **irq_counts,
  29. unsigned int *nb_irqs)
  30. {
  31. GoldfishPICState *s = GOLDFISH_PIC(obj);
  32. *irq_counts = s->stats_irq_count;
  33. *nb_irqs = ARRAY_SIZE(s->stats_irq_count);
  34. return true;
  35. }
  36. static void goldfish_pic_print_info(InterruptStatsProvider *obj, Monitor *mon)
  37. {
  38. GoldfishPICState *s = GOLDFISH_PIC(obj);
  39. monitor_printf(mon, "goldfish-pic.%d: pending=0x%08x enabled=0x%08x\n",
  40. s->idx, s->pending, s->enabled);
  41. }
  42. static void goldfish_pic_update(GoldfishPICState *s)
  43. {
  44. if (s->pending & s->enabled) {
  45. qemu_irq_raise(s->irq);
  46. } else {
  47. qemu_irq_lower(s->irq);
  48. }
  49. }
  50. static void goldfish_irq_request(void *opaque, int irq, int level)
  51. {
  52. GoldfishPICState *s = opaque;
  53. trace_goldfish_irq_request(s, s->idx, irq, level);
  54. if (level) {
  55. s->pending |= 1 << irq;
  56. s->stats_irq_count[irq]++;
  57. } else {
  58. s->pending &= ~(1 << irq);
  59. }
  60. goldfish_pic_update(s);
  61. }
  62. static uint64_t goldfish_pic_read(void *opaque, hwaddr addr,
  63. unsigned size)
  64. {
  65. GoldfishPICState *s = opaque;
  66. uint64_t value = 0;
  67. switch (addr) {
  68. case REG_STATUS:
  69. /* The number of pending interrupts (0 to 32) */
  70. value = ctpop32(s->pending & s->enabled);
  71. break;
  72. case REG_IRQ_PENDING:
  73. /* The pending interrupt mask */
  74. value = s->pending & s->enabled;
  75. break;
  76. default:
  77. qemu_log_mask(LOG_UNIMP,
  78. "%s: unimplemented register read 0x%02"HWADDR_PRIx"\n",
  79. __func__, addr);
  80. break;
  81. }
  82. trace_goldfish_pic_read(s, s->idx, addr, size, value);
  83. return value;
  84. }
  85. static void goldfish_pic_write(void *opaque, hwaddr addr,
  86. uint64_t value, unsigned size)
  87. {
  88. GoldfishPICState *s = opaque;
  89. trace_goldfish_pic_write(s, s->idx, addr, size, value);
  90. switch (addr) {
  91. case REG_IRQ_DISABLE_ALL:
  92. s->enabled = 0;
  93. s->pending = 0;
  94. break;
  95. case REG_DISABLE:
  96. s->enabled &= ~value;
  97. break;
  98. case REG_ENABLE:
  99. s->enabled |= value;
  100. break;
  101. default:
  102. qemu_log_mask(LOG_UNIMP,
  103. "%s: unimplemented register write 0x%02"HWADDR_PRIx"\n",
  104. __func__, addr);
  105. break;
  106. }
  107. goldfish_pic_update(s);
  108. }
  109. static const MemoryRegionOps goldfish_pic_ops = {
  110. .read = goldfish_pic_read,
  111. .write = goldfish_pic_write,
  112. .endianness = DEVICE_NATIVE_ENDIAN,
  113. .valid.max_access_size = 4,
  114. .impl.min_access_size = 4,
  115. .impl.max_access_size = 4,
  116. };
  117. static void goldfish_pic_reset(DeviceState *dev)
  118. {
  119. GoldfishPICState *s = GOLDFISH_PIC(dev);
  120. int i;
  121. trace_goldfish_pic_reset(s, s->idx);
  122. s->pending = 0;
  123. s->enabled = 0;
  124. for (i = 0; i < ARRAY_SIZE(s->stats_irq_count); i++) {
  125. s->stats_irq_count[i] = 0;
  126. }
  127. }
  128. static void goldfish_pic_realize(DeviceState *dev, Error **errp)
  129. {
  130. GoldfishPICState *s = GOLDFISH_PIC(dev);
  131. trace_goldfish_pic_realize(s, s->idx);
  132. memory_region_init_io(&s->iomem, OBJECT(s), &goldfish_pic_ops, s,
  133. "goldfish_pic", 0x24);
  134. }
  135. static const VMStateDescription vmstate_goldfish_pic = {
  136. .name = "goldfish_pic",
  137. .version_id = 1,
  138. .minimum_version_id = 1,
  139. .fields = (VMStateField[]) {
  140. VMSTATE_UINT32(pending, GoldfishPICState),
  141. VMSTATE_UINT32(enabled, GoldfishPICState),
  142. VMSTATE_END_OF_LIST()
  143. }
  144. };
  145. static void goldfish_pic_instance_init(Object *obj)
  146. {
  147. SysBusDevice *dev = SYS_BUS_DEVICE(obj);
  148. GoldfishPICState *s = GOLDFISH_PIC(obj);
  149. trace_goldfish_pic_instance_init(s);
  150. sysbus_init_mmio(dev, &s->iomem);
  151. sysbus_init_irq(dev, &s->irq);
  152. qdev_init_gpio_in(DEVICE(obj), goldfish_irq_request, GOLDFISH_PIC_IRQ_NB);
  153. }
  154. static Property goldfish_pic_properties[] = {
  155. DEFINE_PROP_UINT8("index", GoldfishPICState, idx, 0),
  156. DEFINE_PROP_END_OF_LIST(),
  157. };
  158. static void goldfish_pic_class_init(ObjectClass *oc, void *data)
  159. {
  160. DeviceClass *dc = DEVICE_CLASS(oc);
  161. InterruptStatsProviderClass *ic = INTERRUPT_STATS_PROVIDER_CLASS(oc);
  162. dc->reset = goldfish_pic_reset;
  163. dc->realize = goldfish_pic_realize;
  164. dc->vmsd = &vmstate_goldfish_pic;
  165. ic->get_statistics = goldfish_pic_get_statistics;
  166. ic->print_info = goldfish_pic_print_info;
  167. device_class_set_props(dc, goldfish_pic_properties);
  168. }
  169. static const TypeInfo goldfish_pic_info = {
  170. .name = TYPE_GOLDFISH_PIC,
  171. .parent = TYPE_SYS_BUS_DEVICE,
  172. .class_init = goldfish_pic_class_init,
  173. .instance_init = goldfish_pic_instance_init,
  174. .instance_size = sizeof(GoldfishPICState),
  175. .interfaces = (InterfaceInfo[]) {
  176. { TYPE_INTERRUPT_STATS_PROVIDER },
  177. { }
  178. },
  179. };
  180. static void goldfish_pic_register_types(void)
  181. {
  182. type_register_static(&goldfish_pic_info);
  183. }
  184. type_init(goldfish_pic_register_types)