2
0

ompic.c 4.9 KB

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