2
0

ompic.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Authors: Stafford Horne <shorne@gmail.com>
  7. */
  8. #include "qemu/osdep.h"
  9. #include "qemu/log.h"
  10. #include "qemu/module.h"
  11. #include "qapi/error.h"
  12. #include "hw/irq.h"
  13. #include "hw/qdev-properties.h"
  14. #include "hw/sysbus.h"
  15. #include "migration/vmstate.h"
  16. #include "exec/memory.h"
  17. #include "qom/object.h"
  18. #define TYPE_OR1K_OMPIC "or1k-ompic"
  19. OBJECT_DECLARE_SIMPLE_TYPE(OR1KOMPICState, OR1K_OMPIC)
  20. #define OMPIC_CTRL_IRQ_ACK (1 << 31)
  21. #define OMPIC_CTRL_IRQ_GEN (1 << 30)
  22. #define OMPIC_CTRL_DST(cpu) (((cpu) >> 16) & 0x3fff)
  23. #define OMPIC_REG(addr) (((addr) >> 2) & 0x1)
  24. #define OMPIC_SRC_CPU(addr) (((addr) >> 3) & 0x4f)
  25. #define OMPIC_DST_CPU(addr) (((addr) >> 3) & 0x4f)
  26. #define OMPIC_STATUS_IRQ_PENDING (1 << 30)
  27. #define OMPIC_STATUS_SRC(cpu) (((cpu) & 0x3fff) << 16)
  28. #define OMPIC_STATUS_DATA(data) ((data) & 0xffff)
  29. #define OMPIC_CONTROL 0
  30. #define OMPIC_STATUS 1
  31. #define OMPIC_MAX_CPUS 4 /* Real max is much higher, but dont waste memory */
  32. #define OMPIC_ADDRSPACE_SZ (OMPIC_MAX_CPUS * 2 * 4) /* 2 32-bit regs per cpu */
  33. typedef struct OR1KOMPICCPUState OR1KOMPICCPUState;
  34. struct OR1KOMPICCPUState {
  35. qemu_irq irq;
  36. uint32_t status;
  37. uint32_t control;
  38. };
  39. struct OR1KOMPICState {
  40. SysBusDevice parent_obj;
  41. MemoryRegion mr;
  42. OR1KOMPICCPUState cpus[OMPIC_MAX_CPUS];
  43. uint32_t num_cpus;
  44. };
  45. static uint64_t ompic_read(void *opaque, hwaddr addr, unsigned size)
  46. {
  47. OR1KOMPICState *s = opaque;
  48. int src_cpu = OMPIC_SRC_CPU(addr);
  49. /* We can only write to control control, write control + update status */
  50. if (OMPIC_REG(addr) == OMPIC_CONTROL) {
  51. return s->cpus[src_cpu].control;
  52. } else {
  53. return s->cpus[src_cpu].status;
  54. }
  55. }
  56. static void ompic_write(void *opaque, hwaddr addr, uint64_t data, unsigned size)
  57. {
  58. OR1KOMPICState *s = opaque;
  59. /* We can only write to control control, write control + update status */
  60. if (OMPIC_REG(addr) == OMPIC_CONTROL) {
  61. int src_cpu = OMPIC_SRC_CPU(addr);
  62. s->cpus[src_cpu].control = data;
  63. if (data & OMPIC_CTRL_IRQ_GEN) {
  64. int dst_cpu = OMPIC_CTRL_DST(data);
  65. s->cpus[dst_cpu].status = OMPIC_STATUS_IRQ_PENDING |
  66. OMPIC_STATUS_SRC(src_cpu) |
  67. OMPIC_STATUS_DATA(data);
  68. qemu_irq_raise(s->cpus[dst_cpu].irq);
  69. }
  70. if (data & OMPIC_CTRL_IRQ_ACK) {
  71. s->cpus[src_cpu].status &= ~OMPIC_STATUS_IRQ_PENDING;
  72. qemu_irq_lower(s->cpus[src_cpu].irq);
  73. }
  74. }
  75. }
  76. static const MemoryRegionOps ompic_ops = {
  77. .read = ompic_read,
  78. .write = ompic_write,
  79. .endianness = DEVICE_NATIVE_ENDIAN,
  80. .impl = {
  81. .max_access_size = 8,
  82. },
  83. };
  84. static void or1k_ompic_init(Object *obj)
  85. {
  86. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  87. OR1KOMPICState *s = OR1K_OMPIC(obj);
  88. memory_region_init_io(&s->mr, OBJECT(s), &ompic_ops, s,
  89. "or1k-ompic", OMPIC_ADDRSPACE_SZ);
  90. sysbus_init_mmio(sbd, &s->mr);
  91. }
  92. static void or1k_ompic_realize(DeviceState *dev, Error **errp)
  93. {
  94. OR1KOMPICState *s = OR1K_OMPIC(dev);
  95. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  96. int i;
  97. if (s->num_cpus > OMPIC_MAX_CPUS) {
  98. error_setg(errp, "Exceeded maximum CPUs %d", s->num_cpus);
  99. return;
  100. }
  101. /* Init IRQ sources for all CPUs */
  102. for (i = 0; i < s->num_cpus; i++) {
  103. sysbus_init_irq(sbd, &s->cpus[i].irq);
  104. }
  105. }
  106. static Property or1k_ompic_properties[] = {
  107. DEFINE_PROP_UINT32("num-cpus", OR1KOMPICState, num_cpus, 1),
  108. DEFINE_PROP_END_OF_LIST(),
  109. };
  110. static const VMStateDescription vmstate_or1k_ompic_cpu = {
  111. .name = "or1k_ompic_cpu",
  112. .version_id = 1,
  113. .minimum_version_id = 1,
  114. .fields = (VMStateField[]) {
  115. VMSTATE_UINT32(status, OR1KOMPICCPUState),
  116. VMSTATE_UINT32(control, OR1KOMPICCPUState),
  117. VMSTATE_END_OF_LIST()
  118. }
  119. };
  120. static const VMStateDescription vmstate_or1k_ompic = {
  121. .name = TYPE_OR1K_OMPIC,
  122. .version_id = 1,
  123. .minimum_version_id = 1,
  124. .fields = (VMStateField[]) {
  125. VMSTATE_STRUCT_ARRAY(cpus, OR1KOMPICState, OMPIC_MAX_CPUS, 1,
  126. vmstate_or1k_ompic_cpu, OR1KOMPICCPUState),
  127. VMSTATE_UINT32(num_cpus, OR1KOMPICState),
  128. VMSTATE_END_OF_LIST()
  129. }
  130. };
  131. static void or1k_ompic_class_init(ObjectClass *klass, void *data)
  132. {
  133. DeviceClass *dc = DEVICE_CLASS(klass);
  134. device_class_set_props(dc, or1k_ompic_properties);
  135. dc->realize = or1k_ompic_realize;
  136. dc->vmsd = &vmstate_or1k_ompic;
  137. }
  138. static const TypeInfo or1k_ompic_info = {
  139. .name = TYPE_OR1K_OMPIC,
  140. .parent = TYPE_SYS_BUS_DEVICE,
  141. .instance_size = sizeof(OR1KOMPICState),
  142. .instance_init = or1k_ompic_init,
  143. .class_init = or1k_ompic_class_init,
  144. };
  145. static void or1k_ompic_register_types(void)
  146. {
  147. type_register_static(&or1k_ompic_info);
  148. }
  149. type_init(or1k_ompic_register_types)