sun4c_intctl.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 "console.h"
  27. //#define DEBUG_IRQ_COUNT
  28. //#define DEBUG_IRQ
  29. #ifdef DEBUG_IRQ
  30. #define DPRINTF(fmt, args...) \
  31. do { printf("IRQ: " fmt , ##args); } while (0)
  32. #else
  33. #define DPRINTF(fmt, args...)
  34. #endif
  35. /*
  36. * Registers of interrupt controller in sun4c.
  37. *
  38. */
  39. #define MAX_PILS 16
  40. typedef struct Sun4c_INTCTLState {
  41. #ifdef DEBUG_IRQ_COUNT
  42. uint64_t irq_count;
  43. #endif
  44. qemu_irq *cpu_irqs;
  45. const uint32_t *intbit_to_level;
  46. uint32_t pil_out;
  47. uint8_t reg;
  48. uint8_t pending;
  49. } Sun4c_INTCTLState;
  50. #define INTCTL_SIZE 1
  51. static void sun4c_check_interrupts(void *opaque);
  52. static uint32_t sun4c_intctl_mem_readb(void *opaque, target_phys_addr_t addr)
  53. {
  54. Sun4c_INTCTLState *s = opaque;
  55. uint32_t ret;
  56. ret = s->reg;
  57. DPRINTF("read reg 0x" TARGET_FMT_plx " = %x\n", addr, ret);
  58. return ret;
  59. }
  60. static void sun4c_intctl_mem_writeb(void *opaque, target_phys_addr_t addr,
  61. uint32_t val)
  62. {
  63. Sun4c_INTCTLState *s = opaque;
  64. DPRINTF("write reg 0x" TARGET_FMT_plx " = %x\n", addr, val);
  65. val &= 0xbf;
  66. s->reg = val;
  67. sun4c_check_interrupts(s);
  68. }
  69. static CPUReadMemoryFunc *sun4c_intctl_mem_read[3] = {
  70. sun4c_intctl_mem_readb,
  71. NULL,
  72. NULL,
  73. };
  74. static CPUWriteMemoryFunc *sun4c_intctl_mem_write[3] = {
  75. sun4c_intctl_mem_writeb,
  76. NULL,
  77. NULL,
  78. };
  79. void sun4c_pic_info(void *opaque)
  80. {
  81. Sun4c_INTCTLState *s = opaque;
  82. term_printf("master: pending 0x%2.2x, enabled 0x%2.2x\n", s->pending,
  83. s->reg);
  84. }
  85. void sun4c_irq_info(void *opaque)
  86. {
  87. #ifndef DEBUG_IRQ_COUNT
  88. term_printf("irq statistic code not compiled.\n");
  89. #else
  90. Sun4c_INTCTLState *s = opaque;
  91. int64_t count;
  92. term_printf("IRQ statistics:\n");
  93. count = s->irq_count[i];
  94. if (count > 0)
  95. term_printf("%2d: %" PRId64 "\n", i, count);
  96. #endif
  97. }
  98. static const uint32_t intbit_to_level[] = { 0, 1, 4, 6, 8, 10, 0, 14, };
  99. static void sun4c_check_interrupts(void *opaque)
  100. {
  101. Sun4c_INTCTLState *s = opaque;
  102. uint32_t pil_pending;
  103. unsigned int i;
  104. DPRINTF("pending %x disabled %x\n", pending, s->intregm_disabled);
  105. pil_pending = 0;
  106. if (s->pending && !(s->reg & 0x80000000)) {
  107. for (i = 0; i < 8; i++) {
  108. if (s->pending & (1 << i))
  109. pil_pending |= 1 << intbit_to_level[i];
  110. }
  111. }
  112. for (i = 0; i < MAX_PILS; i++) {
  113. if (pil_pending & (1 << i)) {
  114. if (!(s->pil_out & (1 << i)))
  115. qemu_irq_raise(s->cpu_irqs[i]);
  116. } else {
  117. if (s->pil_out & (1 << i))
  118. qemu_irq_lower(s->cpu_irqs[i]);
  119. }
  120. }
  121. s->pil_out = pil_pending;
  122. }
  123. /*
  124. * "irq" here is the bit number in the system interrupt register
  125. */
  126. static void sun4c_set_irq(void *opaque, int irq, int level)
  127. {
  128. Sun4c_INTCTLState *s = opaque;
  129. uint32_t mask = 1 << irq;
  130. uint32_t pil = intbit_to_level[irq];
  131. DPRINTF("Set irq %d -> pil %d level %d\n", irq, pil,
  132. level);
  133. if (pil > 0) {
  134. if (level) {
  135. #ifdef DEBUG_IRQ_COUNT
  136. s->irq_count[pil]++;
  137. #endif
  138. s->pending |= mask;
  139. } else {
  140. s->pending &= ~mask;
  141. }
  142. sun4c_check_interrupts(s);
  143. }
  144. }
  145. static void sun4c_intctl_save(QEMUFile *f, void *opaque)
  146. {
  147. Sun4c_INTCTLState *s = opaque;
  148. qemu_put_8s(f, &s->reg);
  149. qemu_put_8s(f, &s->pending);
  150. }
  151. static int sun4c_intctl_load(QEMUFile *f, void *opaque, int version_id)
  152. {
  153. Sun4c_INTCTLState *s = opaque;
  154. if (version_id != 1)
  155. return -EINVAL;
  156. qemu_get_8s(f, &s->reg);
  157. qemu_get_8s(f, &s->pending);
  158. sun4c_check_interrupts(s);
  159. return 0;
  160. }
  161. static void sun4c_intctl_reset(void *opaque)
  162. {
  163. Sun4c_INTCTLState *s = opaque;
  164. s->reg = 1;
  165. s->pending = 0;
  166. sun4c_check_interrupts(s);
  167. }
  168. void *sun4c_intctl_init(target_phys_addr_t addr, qemu_irq **irq,
  169. qemu_irq *parent_irq)
  170. {
  171. int sun4c_intctl_io_memory;
  172. Sun4c_INTCTLState *s;
  173. s = qemu_mallocz(sizeof(Sun4c_INTCTLState));
  174. sun4c_intctl_io_memory = cpu_register_io_memory(0, sun4c_intctl_mem_read,
  175. sun4c_intctl_mem_write, s);
  176. cpu_register_physical_memory(addr, INTCTL_SIZE, sun4c_intctl_io_memory);
  177. s->cpu_irqs = parent_irq;
  178. register_savevm("sun4c_intctl", addr, 1, sun4c_intctl_save,
  179. sun4c_intctl_load, s);
  180. qemu_register_reset(sun4c_intctl_reset, s);
  181. *irq = qemu_allocate_irqs(sun4c_set_irq, s, 8);
  182. sun4c_intctl_reset(s);
  183. return s;
  184. }