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.1 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 "system/kvm.h"
  33. #include "qemu/log.h"
  34. #include "qemu/module.h"
  35. #include "qom/object.h"
  36. #define TYPE_ARM_GICV2M "arm-gicv2m"
  37. OBJECT_DECLARE_SIMPLE_TYPE(ARMGICv2mState, ARM_GICV2M)
  38. #define GICV2M_NUM_SPI_MAX 128
  39. #define V2M_MSI_TYPER 0x008
  40. #define V2M_MSI_SETSPI_NS 0x040
  41. #define V2M_MSI_IIDR 0xFCC
  42. #define V2M_IIDR0 0xFD0
  43. #define V2M_IIDR11 0xFFC
  44. #define PRODUCT_ID_QEMU 0x51 /* ASCII code Q */
  45. struct ARMGICv2mState {
  46. SysBusDevice parent_obj;
  47. MemoryRegion iomem;
  48. qemu_irq spi[GICV2M_NUM_SPI_MAX];
  49. uint32_t base_spi;
  50. uint32_t num_spi;
  51. };
  52. static void gicv2m_set_irq(void *opaque, int irq)
  53. {
  54. ARMGICv2mState *s = (ARMGICv2mState *)opaque;
  55. qemu_irq_pulse(s->spi[irq]);
  56. }
  57. static uint64_t gicv2m_read(void *opaque, hwaddr offset,
  58. unsigned size)
  59. {
  60. ARMGICv2mState *s = (ARMGICv2mState *)opaque;
  61. uint32_t val;
  62. if (size != 4) {
  63. qemu_log_mask(LOG_GUEST_ERROR, "gicv2m_read: bad size %u\n", size);
  64. return 0;
  65. }
  66. switch (offset) {
  67. case V2M_MSI_TYPER:
  68. val = (s->base_spi + 32) << 16;
  69. val |= s->num_spi;
  70. return val;
  71. case V2M_MSI_IIDR:
  72. /* We don't have any valid implementor so we leave that field as zero
  73. * and we return 0 in the arch revision as per the spec.
  74. */
  75. return (PRODUCT_ID_QEMU << 20);
  76. case V2M_IIDR0 ... V2M_IIDR11:
  77. /* We do not implement any optional identification registers and the
  78. * mandatory MSI_PIDR2 register reads as 0x0, so we capture all
  79. * implementation defined registers here.
  80. */
  81. return 0;
  82. default:
  83. qemu_log_mask(LOG_GUEST_ERROR,
  84. "gicv2m_read: Bad offset %x\n", (int)offset);
  85. return 0;
  86. }
  87. }
  88. static void gicv2m_write(void *opaque, hwaddr offset,
  89. uint64_t value, unsigned size)
  90. {
  91. ARMGICv2mState *s = (ARMGICv2mState *)opaque;
  92. if (size != 2 && size != 4) {
  93. qemu_log_mask(LOG_GUEST_ERROR, "gicv2m_write: bad size %u\n", size);
  94. return;
  95. }
  96. switch (offset) {
  97. case V2M_MSI_SETSPI_NS: {
  98. int spi;
  99. spi = (value & 0x3ff) - (s->base_spi + 32);
  100. if (spi >= 0 && spi < s->num_spi) {
  101. gicv2m_set_irq(s, spi);
  102. }
  103. return;
  104. }
  105. default:
  106. qemu_log_mask(LOG_GUEST_ERROR,
  107. "gicv2m_write: Bad offset %x\n", (int)offset);
  108. }
  109. }
  110. static const MemoryRegionOps gicv2m_ops = {
  111. .read = gicv2m_read,
  112. .write = gicv2m_write,
  113. .endianness = DEVICE_LITTLE_ENDIAN,
  114. };
  115. static void gicv2m_realize(DeviceState *dev, Error **errp)
  116. {
  117. ARMGICv2mState *s = ARM_GICV2M(dev);
  118. int i;
  119. if (s->num_spi > GICV2M_NUM_SPI_MAX) {
  120. error_setg(errp,
  121. "requested %u SPIs exceeds GICv2m frame maximum %d",
  122. s->num_spi, GICV2M_NUM_SPI_MAX);
  123. return;
  124. }
  125. if (s->base_spi + 32 > 1020 - s->num_spi) {
  126. error_setg(errp,
  127. "requested base SPI %u+%u exceeds max. number 1020",
  128. s->base_spi + 32, s->num_spi);
  129. return;
  130. }
  131. for (i = 0; i < s->num_spi; i++) {
  132. sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->spi[i]);
  133. }
  134. msi_nonbroken = true;
  135. kvm_gsi_direct_mapping = true;
  136. kvm_msi_via_irqfd_allowed = kvm_irqfds_enabled();
  137. }
  138. static void gicv2m_init(Object *obj)
  139. {
  140. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  141. ARMGICv2mState *s = ARM_GICV2M(obj);
  142. memory_region_init_io(&s->iomem, OBJECT(s), &gicv2m_ops, s,
  143. "gicv2m", 0x1000);
  144. sysbus_init_mmio(sbd, &s->iomem);
  145. }
  146. static const Property gicv2m_properties[] = {
  147. DEFINE_PROP_UINT32("base-spi", ARMGICv2mState, base_spi, 0),
  148. DEFINE_PROP_UINT32("num-spi", ARMGICv2mState, num_spi, 64),
  149. };
  150. static void gicv2m_class_init(ObjectClass *klass, void *data)
  151. {
  152. DeviceClass *dc = DEVICE_CLASS(klass);
  153. device_class_set_props(dc, 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)