2
0

sun4c_intctl.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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.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, target_phys_addr_t 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, target_phys_addr_t 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. void sun4c_pic_info(Monitor *mon, void *opaque)
  83. {
  84. Sun4c_INTCTLState *s = opaque;
  85. monitor_printf(mon, "master: pending 0x%2.2x, enabled 0x%2.2x\n",
  86. s->pending, s->reg);
  87. }
  88. void sun4c_irq_info(Monitor *mon, void *opaque)
  89. {
  90. #ifndef DEBUG_IRQ_COUNT
  91. monitor_printf(mon, "irq statistic code not compiled.\n");
  92. #else
  93. Sun4c_INTCTLState *s = opaque;
  94. int64_t count;
  95. monitor_printf(mon, "IRQ statistics:\n");
  96. count = s->irq_count;
  97. if (count > 0)
  98. monitor_printf(mon, " %" PRId64 "\n", count);
  99. #endif
  100. }
  101. static const uint32_t intbit_to_level[] = { 0, 1, 4, 6, 8, 10, 0, 14, };
  102. static void sun4c_check_interrupts(void *opaque)
  103. {
  104. Sun4c_INTCTLState *s = opaque;
  105. uint32_t pil_pending;
  106. unsigned int i;
  107. pil_pending = 0;
  108. if (s->pending && !(s->reg & 0x80000000)) {
  109. for (i = 0; i < 8; i++) {
  110. if (s->pending & (1 << i))
  111. pil_pending |= 1 << intbit_to_level[i];
  112. }
  113. }
  114. for (i = 0; i < MAX_PILS; i++) {
  115. if (pil_pending & (1 << i)) {
  116. if (!(s->pil_out & (1 << i)))
  117. qemu_irq_raise(s->cpu_irqs[i]);
  118. } else {
  119. if (s->pil_out & (1 << i))
  120. qemu_irq_lower(s->cpu_irqs[i]);
  121. }
  122. }
  123. s->pil_out = pil_pending;
  124. }
  125. /*
  126. * "irq" here is the bit number in the system interrupt register
  127. */
  128. static void sun4c_set_irq(void *opaque, int irq, int level)
  129. {
  130. Sun4c_INTCTLState *s = opaque;
  131. uint32_t mask = 1 << irq;
  132. uint32_t pil = intbit_to_level[irq];
  133. DPRINTF("Set irq %d -> pil %d level %d\n", irq, pil,
  134. level);
  135. if (pil > 0) {
  136. if (level) {
  137. #ifdef DEBUG_IRQ_COUNT
  138. s->irq_count++;
  139. #endif
  140. s->pending |= mask;
  141. } else {
  142. s->pending &= ~mask;
  143. }
  144. sun4c_check_interrupts(s);
  145. }
  146. }
  147. static const VMStateDescription vmstate_sun4c_intctl = {
  148. .name ="sun4c_intctl",
  149. .version_id = 1,
  150. .minimum_version_id = 1,
  151. .minimum_version_id_old = 1,
  152. .fields = (VMStateField []) {
  153. VMSTATE_UINT8(reg, Sun4c_INTCTLState),
  154. VMSTATE_UINT8(pending, Sun4c_INTCTLState),
  155. VMSTATE_END_OF_LIST()
  156. }
  157. };
  158. static void sun4c_intctl_reset(DeviceState *d)
  159. {
  160. Sun4c_INTCTLState *s = container_of(d, Sun4c_INTCTLState, busdev.qdev);
  161. s->reg = 1;
  162. s->pending = 0;
  163. }
  164. static int sun4c_intctl_init1(SysBusDevice *dev)
  165. {
  166. Sun4c_INTCTLState *s = FROM_SYSBUS(Sun4c_INTCTLState, dev);
  167. unsigned int i;
  168. memory_region_init_io(&s->iomem, &sun4c_intctl_mem_ops, s,
  169. "intctl", INTCTL_SIZE);
  170. sysbus_init_mmio(dev, &s->iomem);
  171. qdev_init_gpio_in(&dev->qdev, sun4c_set_irq, 8);
  172. for (i = 0; i < MAX_PILS; i++) {
  173. sysbus_init_irq(dev, &s->cpu_irqs[i]);
  174. }
  175. return 0;
  176. }
  177. static void sun4c_intctl_class_init(ObjectClass *klass, void *data)
  178. {
  179. DeviceClass *dc = DEVICE_CLASS(klass);
  180. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  181. k->init = sun4c_intctl_init1;
  182. dc->reset = sun4c_intctl_reset;
  183. dc->vmsd = &vmstate_sun4c_intctl;
  184. }
  185. static TypeInfo sun4c_intctl_info = {
  186. .name = "sun4c_intctl",
  187. .parent = TYPE_SYS_BUS_DEVICE,
  188. .instance_size = sizeof(Sun4c_INTCTLState),
  189. .class_init = sun4c_intctl_class_init,
  190. };
  191. static void sun4c_intctl_register_types(void)
  192. {
  193. type_register_static(&sun4c_intctl_info);
  194. }
  195. type_init(sun4c_intctl_register_types)