arm_gicv2m.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * GICv2m extension for MSI/MSI-x support with a GICv2-based system
  3. *
  4. * Copyright (C) 2015 Linaro, All rights reserved.
  5. *
  6. * Author: Christoffer Dall <christoffer.dall@linaro.org>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /* This file implements an emulated GICv2m widget as described in the ARM
  22. * Server Base System Architecture (SBSA) specification Version 2.2
  23. * (ARM-DEN-0029 v2.2) pages 35-39 without any optional implementation defined
  24. * identification registers and with a single non-secure MSI register frame.
  25. */
  26. #include "qemu/osdep.h"
  27. #include "qapi/error.h"
  28. #include "hw/sysbus.h"
  29. #include "hw/irq.h"
  30. #include "hw/pci/msi.h"
  31. #include "hw/qdev-properties.h"
  32. #include "sysemu/kvm.h"
  33. #include "qemu/log.h"
  34. #include "qemu/module.h"
  35. #define TYPE_ARM_GICV2M "arm-gicv2m"
  36. #define ARM_GICV2M(obj) OBJECT_CHECK(ARMGICv2mState, (obj), TYPE_ARM_GICV2M)
  37. #define GICV2M_NUM_SPI_MAX 128
  38. #define V2M_MSI_TYPER 0x008
  39. #define V2M_MSI_SETSPI_NS 0x040
  40. #define V2M_MSI_IIDR 0xFCC
  41. #define V2M_IIDR0 0xFD0
  42. #define V2M_IIDR11 0xFFC
  43. #define PRODUCT_ID_QEMU 0x51 /* ASCII code Q */
  44. typedef struct ARMGICv2mState {
  45. SysBusDevice parent_obj;
  46. MemoryRegion iomem;
  47. qemu_irq spi[GICV2M_NUM_SPI_MAX];
  48. uint32_t base_spi;
  49. uint32_t num_spi;
  50. } ARMGICv2mState;
  51. static void gicv2m_set_irq(void *opaque, int irq)
  52. {
  53. ARMGICv2mState *s = (ARMGICv2mState *)opaque;
  54. qemu_irq_pulse(s->spi[irq]);
  55. }
  56. static uint64_t gicv2m_read(void *opaque, hwaddr offset,
  57. unsigned size)
  58. {
  59. ARMGICv2mState *s = (ARMGICv2mState *)opaque;
  60. uint32_t val;
  61. if (size != 4) {
  62. qemu_log_mask(LOG_GUEST_ERROR, "gicv2m_read: bad size %u\n", size);
  63. return 0;
  64. }
  65. switch (offset) {
  66. case V2M_MSI_TYPER:
  67. val = (s->base_spi + 32) << 16;
  68. val |= s->num_spi;
  69. return val;
  70. case V2M_MSI_IIDR:
  71. /* We don't have any valid implementor so we leave that field as zero
  72. * and we return 0 in the arch revision as per the spec.
  73. */
  74. return (PRODUCT_ID_QEMU << 20);
  75. case V2M_IIDR0 ... V2M_IIDR11:
  76. /* We do not implement any optional identification registers and the
  77. * mandatory MSI_PIDR2 register reads as 0x0, so we capture all
  78. * implementation defined registers here.
  79. */
  80. return 0;
  81. default:
  82. qemu_log_mask(LOG_GUEST_ERROR,
  83. "gicv2m_read: Bad offset %x\n", (int)offset);
  84. return 0;
  85. }
  86. }
  87. static void gicv2m_write(void *opaque, hwaddr offset,
  88. uint64_t value, unsigned size)
  89. {
  90. ARMGICv2mState *s = (ARMGICv2mState *)opaque;
  91. if (size != 2 && size != 4) {
  92. qemu_log_mask(LOG_GUEST_ERROR, "gicv2m_write: bad size %u\n", size);
  93. return;
  94. }
  95. switch (offset) {
  96. case V2M_MSI_SETSPI_NS: {
  97. int spi;
  98. spi = (value & 0x3ff) - (s->base_spi + 32);
  99. if (spi >= 0 && spi < s->num_spi) {
  100. gicv2m_set_irq(s, spi);
  101. }
  102. return;
  103. }
  104. default:
  105. qemu_log_mask(LOG_GUEST_ERROR,
  106. "gicv2m_write: Bad offset %x\n", (int)offset);
  107. }
  108. }
  109. static const MemoryRegionOps gicv2m_ops = {
  110. .read = gicv2m_read,
  111. .write = gicv2m_write,
  112. .endianness = DEVICE_LITTLE_ENDIAN,
  113. };
  114. static void gicv2m_realize(DeviceState *dev, Error **errp)
  115. {
  116. ARMGICv2mState *s = ARM_GICV2M(dev);
  117. int i;
  118. if (s->num_spi > GICV2M_NUM_SPI_MAX) {
  119. error_setg(errp,
  120. "requested %u SPIs exceeds GICv2m frame maximum %d",
  121. s->num_spi, GICV2M_NUM_SPI_MAX);
  122. return;
  123. }
  124. if (s->base_spi + 32 > 1020 - s->num_spi) {
  125. error_setg(errp,
  126. "requested base SPI %u+%u exceeds max. number 1020",
  127. s->base_spi + 32, s->num_spi);
  128. return;
  129. }
  130. for (i = 0; i < s->num_spi; i++) {
  131. sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->spi[i]);
  132. }
  133. msi_nonbroken = true;
  134. kvm_gsi_direct_mapping = true;
  135. kvm_msi_via_irqfd_allowed = kvm_irqfds_enabled();
  136. }
  137. static void gicv2m_init(Object *obj)
  138. {
  139. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  140. ARMGICv2mState *s = ARM_GICV2M(obj);
  141. memory_region_init_io(&s->iomem, OBJECT(s), &gicv2m_ops, s,
  142. "gicv2m", 0x1000);
  143. sysbus_init_mmio(sbd, &s->iomem);
  144. }
  145. static Property gicv2m_properties[] = {
  146. DEFINE_PROP_UINT32("base-spi", ARMGICv2mState, base_spi, 0),
  147. DEFINE_PROP_UINT32("num-spi", ARMGICv2mState, num_spi, 64),
  148. DEFINE_PROP_END_OF_LIST(),
  149. };
  150. static void gicv2m_class_init(ObjectClass *klass, void *data)
  151. {
  152. DeviceClass *dc = DEVICE_CLASS(klass);
  153. dc->props = gicv2m_properties;
  154. dc->realize = gicv2m_realize;
  155. }
  156. static const TypeInfo gicv2m_info = {
  157. .name = TYPE_ARM_GICV2M,
  158. .parent = TYPE_SYS_BUS_DEVICE,
  159. .instance_size = sizeof(ARMGICv2mState),
  160. .instance_init = gicv2m_init,
  161. .class_init = gicv2m_class_init,
  162. };
  163. static void gicv2m_register_types(void)
  164. {
  165. type_register_static(&gicv2m_info);
  166. }
  167. type_init(gicv2m_register_types)