xilinx_intc.c 5.2 KB

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