sun4c_intctl.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. #ifdef DEBUG_IRQ_COUNT
  44. uint64_t irq_count;
  45. #endif
  46. qemu_irq cpu_irqs[MAX_PILS];
  47. const uint32_t *intbit_to_level;
  48. uint32_t pil_out;
  49. uint8_t reg;
  50. uint8_t pending;
  51. } Sun4c_INTCTLState;
  52. #define INTCTL_SIZE 1
  53. static void sun4c_check_interrupts(void *opaque);
  54. static uint32_t sun4c_intctl_mem_readb(void *opaque, target_phys_addr_t addr)
  55. {
  56. Sun4c_INTCTLState *s = opaque;
  57. uint32_t ret;
  58. ret = s->reg;
  59. DPRINTF("read reg 0x" TARGET_FMT_plx " = %x\n", addr, ret);
  60. return ret;
  61. }
  62. static void sun4c_intctl_mem_writeb(void *opaque, target_phys_addr_t addr,
  63. uint32_t val)
  64. {
  65. Sun4c_INTCTLState *s = opaque;
  66. DPRINTF("write reg 0x" TARGET_FMT_plx " = %x\n", addr, val);
  67. val &= 0xbf;
  68. s->reg = val;
  69. sun4c_check_interrupts(s);
  70. }
  71. static CPUReadMemoryFunc * const sun4c_intctl_mem_read[3] = {
  72. sun4c_intctl_mem_readb,
  73. NULL,
  74. NULL,
  75. };
  76. static CPUWriteMemoryFunc * const sun4c_intctl_mem_write[3] = {
  77. sun4c_intctl_mem_writeb,
  78. NULL,
  79. NULL,
  80. };
  81. void sun4c_pic_info(Monitor *mon, void *opaque)
  82. {
  83. Sun4c_INTCTLState *s = opaque;
  84. monitor_printf(mon, "master: pending 0x%2.2x, enabled 0x%2.2x\n",
  85. s->pending, s->reg);
  86. }
  87. void sun4c_irq_info(Monitor *mon, void *opaque)
  88. {
  89. #ifndef DEBUG_IRQ_COUNT
  90. monitor_printf(mon, "irq statistic code not compiled.\n");
  91. #else
  92. Sun4c_INTCTLState *s = opaque;
  93. int64_t count;
  94. monitor_printf(mon, "IRQ statistics:\n");
  95. count = s->irq_count;
  96. if (count > 0)
  97. monitor_printf(mon, " %" PRId64 "\n", count);
  98. #endif
  99. }
  100. static const uint32_t intbit_to_level[] = { 0, 1, 4, 6, 8, 10, 0, 14, };
  101. static void sun4c_check_interrupts(void *opaque)
  102. {
  103. Sun4c_INTCTLState *s = opaque;
  104. uint32_t pil_pending;
  105. unsigned int i;
  106. pil_pending = 0;
  107. if (s->pending && !(s->reg & 0x80000000)) {
  108. for (i = 0; i < 8; i++) {
  109. if (s->pending & (1 << i))
  110. pil_pending |= 1 << intbit_to_level[i];
  111. }
  112. }
  113. for (i = 0; i < MAX_PILS; i++) {
  114. if (pil_pending & (1 << i)) {
  115. if (!(s->pil_out & (1 << i)))
  116. qemu_irq_raise(s->cpu_irqs[i]);
  117. } else {
  118. if (s->pil_out & (1 << i))
  119. qemu_irq_lower(s->cpu_irqs[i]);
  120. }
  121. }
  122. s->pil_out = pil_pending;
  123. }
  124. /*
  125. * "irq" here is the bit number in the system interrupt register
  126. */
  127. static void sun4c_set_irq(void *opaque, int irq, int level)
  128. {
  129. Sun4c_INTCTLState *s = opaque;
  130. uint32_t mask = 1 << irq;
  131. uint32_t pil = intbit_to_level[irq];
  132. DPRINTF("Set irq %d -> pil %d level %d\n", irq, pil,
  133. level);
  134. if (pil > 0) {
  135. if (level) {
  136. #ifdef DEBUG_IRQ_COUNT
  137. s->irq_count++;
  138. #endif
  139. s->pending |= mask;
  140. } else {
  141. s->pending &= ~mask;
  142. }
  143. sun4c_check_interrupts(s);
  144. }
  145. }
  146. static const VMStateDescription vmstate_sun4c_intctl = {
  147. .name ="sun4c_intctl",
  148. .version_id = 1,
  149. .minimum_version_id = 1,
  150. .minimum_version_id_old = 1,
  151. .fields = (VMStateField []) {
  152. VMSTATE_UINT8(reg, Sun4c_INTCTLState),
  153. VMSTATE_UINT8(pending, Sun4c_INTCTLState),
  154. VMSTATE_END_OF_LIST()
  155. }
  156. };
  157. static void sun4c_intctl_reset(DeviceState *d)
  158. {
  159. Sun4c_INTCTLState *s = container_of(d, Sun4c_INTCTLState, busdev.qdev);
  160. s->reg = 1;
  161. s->pending = 0;
  162. }
  163. static int sun4c_intctl_init1(SysBusDevice *dev)
  164. {
  165. Sun4c_INTCTLState *s = FROM_SYSBUS(Sun4c_INTCTLState, dev);
  166. int io_memory;
  167. unsigned int i;
  168. io_memory = cpu_register_io_memory(sun4c_intctl_mem_read,
  169. sun4c_intctl_mem_write, s,
  170. DEVICE_NATIVE_ENDIAN);
  171. sysbus_init_mmio(dev, INTCTL_SIZE, io_memory);
  172. qdev_init_gpio_in(&dev->qdev, sun4c_set_irq, 8);
  173. for (i = 0; i < MAX_PILS; i++) {
  174. sysbus_init_irq(dev, &s->cpu_irqs[i]);
  175. }
  176. return 0;
  177. }
  178. static SysBusDeviceInfo sun4c_intctl_info = {
  179. .init = sun4c_intctl_init1,
  180. .qdev.name = "sun4c_intctl",
  181. .qdev.size = sizeof(Sun4c_INTCTLState),
  182. .qdev.vmsd = &vmstate_sun4c_intctl,
  183. .qdev.reset = sun4c_intctl_reset,
  184. };
  185. static void sun4c_intctl_register_devices(void)
  186. {
  187. sysbus_register_withprop(&sun4c_intctl_info);
  188. }
  189. device_init(sun4c_intctl_register_devices)