2
0

sun4c_intctl.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * QEMU Sparc Sun4c interrupt controller emulation
  3. *
  4. * Based on slavio_intctl, copyright (c) 2003-2005 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "hw.h"
  25. #include "sun4m.h"
  26. #include "monitor/monitor.h"
  27. #include "sysbus.h"
  28. //#define DEBUG_IRQ_COUNT
  29. //#define DEBUG_IRQ
  30. #ifdef DEBUG_IRQ
  31. #define DPRINTF(fmt, ...) \
  32. do { printf("IRQ: " fmt , ## __VA_ARGS__); } while (0)
  33. #else
  34. #define DPRINTF(fmt, ...)
  35. #endif
  36. /*
  37. * Registers of interrupt controller in sun4c.
  38. *
  39. */
  40. #define MAX_PILS 16
  41. typedef struct Sun4c_INTCTLState {
  42. SysBusDevice busdev;
  43. MemoryRegion iomem;
  44. #ifdef DEBUG_IRQ_COUNT
  45. uint64_t irq_count;
  46. #endif
  47. qemu_irq cpu_irqs[MAX_PILS];
  48. const uint32_t *intbit_to_level;
  49. uint32_t pil_out;
  50. uint8_t reg;
  51. uint8_t pending;
  52. } Sun4c_INTCTLState;
  53. #define INTCTL_SIZE 1
  54. static void sun4c_check_interrupts(void *opaque);
  55. static uint64_t sun4c_intctl_mem_read(void *opaque, hwaddr addr,
  56. unsigned size)
  57. {
  58. Sun4c_INTCTLState *s = opaque;
  59. uint32_t ret;
  60. ret = s->reg;
  61. DPRINTF("read reg 0x" TARGET_FMT_plx " = %x\n", addr, ret);
  62. return ret;
  63. }
  64. static void sun4c_intctl_mem_write(void *opaque, hwaddr addr,
  65. uint64_t val, unsigned size)
  66. {
  67. Sun4c_INTCTLState *s = opaque;
  68. DPRINTF("write reg 0x" TARGET_FMT_plx " = %x\n", addr, (unsigned)val);
  69. val &= 0xbf;
  70. s->reg = val;
  71. sun4c_check_interrupts(s);
  72. }
  73. static const MemoryRegionOps sun4c_intctl_mem_ops = {
  74. .read = sun4c_intctl_mem_read,
  75. .write = sun4c_intctl_mem_write,
  76. .endianness = DEVICE_NATIVE_ENDIAN,
  77. .valid = {
  78. .min_access_size = 1,
  79. .max_access_size = 1,
  80. },
  81. };
  82. static const uint32_t intbit_to_level[] = { 0, 1, 4, 6, 8, 10, 0, 14, };
  83. static void sun4c_check_interrupts(void *opaque)
  84. {
  85. Sun4c_INTCTLState *s = opaque;
  86. uint32_t pil_pending;
  87. unsigned int i;
  88. pil_pending = 0;
  89. if (s->pending && !(s->reg & 0x80000000)) {
  90. for (i = 0; i < 8; i++) {
  91. if (s->pending & (1 << i))
  92. pil_pending |= 1 << intbit_to_level[i];
  93. }
  94. }
  95. for (i = 0; i < MAX_PILS; i++) {
  96. if (pil_pending & (1 << i)) {
  97. if (!(s->pil_out & (1 << i)))
  98. qemu_irq_raise(s->cpu_irqs[i]);
  99. } else {
  100. if (s->pil_out & (1 << i))
  101. qemu_irq_lower(s->cpu_irqs[i]);
  102. }
  103. }
  104. s->pil_out = pil_pending;
  105. }
  106. /*
  107. * "irq" here is the bit number in the system interrupt register
  108. */
  109. static void sun4c_set_irq(void *opaque, int irq, int level)
  110. {
  111. Sun4c_INTCTLState *s = opaque;
  112. uint32_t mask = 1 << irq;
  113. uint32_t pil = intbit_to_level[irq];
  114. DPRINTF("Set irq %d -> pil %d level %d\n", irq, pil,
  115. level);
  116. if (pil > 0) {
  117. if (level) {
  118. #ifdef DEBUG_IRQ_COUNT
  119. s->irq_count++;
  120. #endif
  121. s->pending |= mask;
  122. } else {
  123. s->pending &= ~mask;
  124. }
  125. sun4c_check_interrupts(s);
  126. }
  127. }
  128. static const VMStateDescription vmstate_sun4c_intctl = {
  129. .name ="sun4c_intctl",
  130. .version_id = 1,
  131. .minimum_version_id = 1,
  132. .minimum_version_id_old = 1,
  133. .fields = (VMStateField []) {
  134. VMSTATE_UINT8(reg, Sun4c_INTCTLState),
  135. VMSTATE_UINT8(pending, Sun4c_INTCTLState),
  136. VMSTATE_END_OF_LIST()
  137. }
  138. };
  139. static void sun4c_intctl_reset(DeviceState *d)
  140. {
  141. Sun4c_INTCTLState *s = container_of(d, Sun4c_INTCTLState, busdev.qdev);
  142. s->reg = 1;
  143. s->pending = 0;
  144. }
  145. static int sun4c_intctl_init1(SysBusDevice *dev)
  146. {
  147. Sun4c_INTCTLState *s = FROM_SYSBUS(Sun4c_INTCTLState, dev);
  148. unsigned int i;
  149. memory_region_init_io(&s->iomem, &sun4c_intctl_mem_ops, s,
  150. "intctl", INTCTL_SIZE);
  151. sysbus_init_mmio(dev, &s->iomem);
  152. qdev_init_gpio_in(&dev->qdev, sun4c_set_irq, 8);
  153. for (i = 0; i < MAX_PILS; i++) {
  154. sysbus_init_irq(dev, &s->cpu_irqs[i]);
  155. }
  156. return 0;
  157. }
  158. static void sun4c_intctl_class_init(ObjectClass *klass, void *data)
  159. {
  160. DeviceClass *dc = DEVICE_CLASS(klass);
  161. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  162. k->init = sun4c_intctl_init1;
  163. dc->reset = sun4c_intctl_reset;
  164. dc->vmsd = &vmstate_sun4c_intctl;
  165. }
  166. static const TypeInfo sun4c_intctl_info = {
  167. .name = "sun4c_intctl",
  168. .parent = TYPE_SYS_BUS_DEVICE,
  169. .instance_size = sizeof(Sun4c_INTCTLState),
  170. .class_init = sun4c_intctl_class_init,
  171. };
  172. static void sun4c_intctl_register_types(void)
  173. {
  174. type_register_static(&sun4c_intctl_info);
  175. }
  176. type_init(sun4c_intctl_register_types)