2
0

xilinx_intc.c 5.0 KB

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