xilinx_intc.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * QEMU Xilinx OPB Interrupt Controller.
  3. *
  4. * Copyright (c) 2009 Edgar E. Iglesias.
  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 "qemu/osdep.h"
  25. #include "hw/sysbus.h"
  26. #include "qemu/module.h"
  27. #include "hw/irq.h"
  28. #include "hw/qdev-properties.h"
  29. #include "qom/object.h"
  30. #define D(x)
  31. #define R_ISR 0
  32. #define R_IPR 1
  33. #define R_IER 2
  34. #define R_IAR 3
  35. #define R_SIE 4
  36. #define R_CIE 5
  37. #define R_IVR 6
  38. #define R_MER 7
  39. #define R_MAX 8
  40. #define TYPE_XILINX_INTC "xlnx.xps-intc"
  41. DECLARE_INSTANCE_CHECKER(struct xlx_pic, XILINX_INTC,
  42. TYPE_XILINX_INTC)
  43. struct xlx_pic
  44. {
  45. SysBusDevice parent_obj;
  46. MemoryRegion mmio;
  47. qemu_irq parent_irq;
  48. /* Configuration reg chosen at synthesis-time. QEMU populates
  49. the bits at board-setup. */
  50. uint32_t c_kind_of_intr;
  51. /* Runtime control registers. */
  52. uint32_t regs[R_MAX];
  53. /* state of the interrupt input pins */
  54. uint32_t irq_pin_state;
  55. };
  56. static void update_irq(struct xlx_pic *p)
  57. {
  58. uint32_t i;
  59. /* level triggered interrupt */
  60. if (p->regs[R_MER] & 2) {
  61. p->regs[R_ISR] |= p->irq_pin_state & ~p->c_kind_of_intr;
  62. }
  63. /* Update the pending register. */
  64. p->regs[R_IPR] = p->regs[R_ISR] & p->regs[R_IER];
  65. /* Update the vector register. */
  66. for (i = 0; i < 32; i++) {
  67. if (p->regs[R_IPR] & (1U << i)) {
  68. break;
  69. }
  70. }
  71. if (i == 32)
  72. i = ~0;
  73. p->regs[R_IVR] = i;
  74. qemu_set_irq(p->parent_irq, (p->regs[R_MER] & 1) && p->regs[R_IPR]);
  75. }
  76. static uint64_t
  77. pic_read(void *opaque, hwaddr addr, unsigned int size)
  78. {
  79. struct xlx_pic *p = opaque;
  80. uint32_t r = 0;
  81. addr >>= 2;
  82. switch (addr)
  83. {
  84. default:
  85. if (addr < ARRAY_SIZE(p->regs))
  86. r = p->regs[addr];
  87. break;
  88. }
  89. D(printf("%s %x=%x\n", __func__, addr * 4, r));
  90. return r;
  91. }
  92. static void
  93. pic_write(void *opaque, hwaddr addr,
  94. uint64_t val64, unsigned int size)
  95. {
  96. struct xlx_pic *p = opaque;
  97. uint32_t value = val64;
  98. addr >>= 2;
  99. D(qemu_log("%s addr=%x val=%x\n", __func__, addr * 4, value));
  100. switch (addr)
  101. {
  102. case R_IAR:
  103. p->regs[R_ISR] &= ~value; /* ACK. */
  104. break;
  105. case R_SIE:
  106. p->regs[R_IER] |= value; /* Atomic set ie. */
  107. break;
  108. case R_CIE:
  109. p->regs[R_IER] &= ~value; /* Atomic clear ie. */
  110. break;
  111. case R_MER:
  112. p->regs[R_MER] = value & 0x3;
  113. break;
  114. case R_ISR:
  115. if ((p->regs[R_MER] & 2)) {
  116. break;
  117. }
  118. /* fallthrough */
  119. default:
  120. if (addr < ARRAY_SIZE(p->regs))
  121. p->regs[addr] = value;
  122. break;
  123. }
  124. update_irq(p);
  125. }
  126. static const MemoryRegionOps pic_ops = {
  127. .read = pic_read,
  128. .write = pic_write,
  129. .endianness = DEVICE_NATIVE_ENDIAN,
  130. .valid = {
  131. .min_access_size = 4,
  132. .max_access_size = 4
  133. }
  134. };
  135. static void irq_handler(void *opaque, int irq, int level)
  136. {
  137. struct xlx_pic *p = opaque;
  138. /* edge triggered interrupt */
  139. if (p->c_kind_of_intr & (1 << irq) && p->regs[R_MER] & 2) {
  140. p->regs[R_ISR] |= (level << irq);
  141. }
  142. p->irq_pin_state &= ~(1 << irq);
  143. p->irq_pin_state |= level << irq;
  144. update_irq(p);
  145. }
  146. static void xilinx_intc_init(Object *obj)
  147. {
  148. struct xlx_pic *p = XILINX_INTC(obj);
  149. qdev_init_gpio_in(DEVICE(obj), irq_handler, 32);
  150. sysbus_init_irq(SYS_BUS_DEVICE(obj), &p->parent_irq);
  151. memory_region_init_io(&p->mmio, obj, &pic_ops, p, "xlnx.xps-intc",
  152. R_MAX * 4);
  153. sysbus_init_mmio(SYS_BUS_DEVICE(obj), &p->mmio);
  154. }
  155. static Property xilinx_intc_properties[] = {
  156. DEFINE_PROP_UINT32("kind-of-intr", struct xlx_pic, c_kind_of_intr, 0),
  157. DEFINE_PROP_END_OF_LIST(),
  158. };
  159. static void xilinx_intc_class_init(ObjectClass *klass, void *data)
  160. {
  161. DeviceClass *dc = DEVICE_CLASS(klass);
  162. device_class_set_props(dc, xilinx_intc_properties);
  163. }
  164. static const TypeInfo xilinx_intc_info = {
  165. .name = TYPE_XILINX_INTC,
  166. .parent = TYPE_SYS_BUS_DEVICE,
  167. .instance_size = sizeof(struct xlx_pic),
  168. .instance_init = xilinx_intc_init,
  169. .class_init = xilinx_intc_class_init,
  170. };
  171. static void xilinx_intc_register_types(void)
  172. {
  173. type_register_static(&xilinx_intc_info);
  174. }
  175. type_init(xilinx_intc_register_types)