xilinx_intc.c 5.3 KB

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