2
0

pci-qmp-cmds.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * QMP commands related to PCI
  3. *
  4. * Copyright (c) 2004 Fabrice Bellard
  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/pci/pci.h"
  26. #include "hw/pci/pci_bridge.h"
  27. #include "pci-internal.h"
  28. #include "qapi/qapi-commands-pci.h"
  29. static PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num);
  30. static PciMemoryRegionList *qmp_query_pci_regions(const PCIDevice *dev)
  31. {
  32. PciMemoryRegionList *head = NULL, **tail = &head;
  33. int i;
  34. for (i = 0; i < PCI_NUM_REGIONS; i++) {
  35. const PCIIORegion *r = &dev->io_regions[i];
  36. PciMemoryRegion *region;
  37. if (!r->size) {
  38. continue;
  39. }
  40. region = g_malloc0(sizeof(*region));
  41. if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
  42. region->type = g_strdup("io");
  43. } else {
  44. region->type = g_strdup("memory");
  45. region->has_prefetch = true;
  46. region->prefetch = !!(r->type & PCI_BASE_ADDRESS_MEM_PREFETCH);
  47. region->has_mem_type_64 = true;
  48. region->mem_type_64 = !!(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64);
  49. }
  50. region->bar = i;
  51. region->address = r->addr;
  52. region->size = r->size;
  53. QAPI_LIST_APPEND(tail, region);
  54. }
  55. return head;
  56. }
  57. static PciBridgeInfo *qmp_query_pci_bridge(PCIDevice *dev, PCIBus *bus,
  58. int bus_num)
  59. {
  60. PciBridgeInfo *info;
  61. PciMemoryRange *range;
  62. info = g_new0(PciBridgeInfo, 1);
  63. info->bus = g_new0(PciBusInfo, 1);
  64. info->bus->number = dev->config[PCI_PRIMARY_BUS];
  65. info->bus->secondary = dev->config[PCI_SECONDARY_BUS];
  66. info->bus->subordinate = dev->config[PCI_SUBORDINATE_BUS];
  67. range = info->bus->io_range = g_new0(PciMemoryRange, 1);
  68. range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_IO);
  69. range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_IO);
  70. range = info->bus->memory_range = g_new0(PciMemoryRange, 1);
  71. range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY);
  72. range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY);
  73. range = info->bus->prefetchable_range = g_new0(PciMemoryRange, 1);
  74. range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
  75. range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
  76. if (dev->config[PCI_SECONDARY_BUS] != 0) {
  77. PCIBus *child_bus = pci_find_bus_nr(bus,
  78. dev->config[PCI_SECONDARY_BUS]);
  79. if (child_bus) {
  80. info->has_devices = true;
  81. info->devices = qmp_query_pci_devices(child_bus,
  82. dev->config[PCI_SECONDARY_BUS]);
  83. }
  84. }
  85. return info;
  86. }
  87. static PciDeviceInfo *qmp_query_pci_device(PCIDevice *dev, PCIBus *bus,
  88. int bus_num)
  89. {
  90. const pci_class_desc *desc;
  91. PciDeviceInfo *info;
  92. uint8_t type;
  93. int class;
  94. info = g_new0(PciDeviceInfo, 1);
  95. info->bus = bus_num;
  96. info->slot = PCI_SLOT(dev->devfn);
  97. info->function = PCI_FUNC(dev->devfn);
  98. info->class_info = g_new0(PciDeviceClass, 1);
  99. class = pci_get_word(dev->config + PCI_CLASS_DEVICE);
  100. info->class_info->q_class = class;
  101. desc = get_class_desc(class);
  102. if (desc->desc) {
  103. info->class_info->desc = g_strdup(desc->desc);
  104. }
  105. info->id = g_new0(PciDeviceId, 1);
  106. info->id->vendor = pci_get_word(dev->config + PCI_VENDOR_ID);
  107. info->id->device = pci_get_word(dev->config + PCI_DEVICE_ID);
  108. info->regions = qmp_query_pci_regions(dev);
  109. info->qdev_id = g_strdup(dev->qdev.id ? dev->qdev.id : "");
  110. info->irq_pin = dev->config[PCI_INTERRUPT_PIN];
  111. if (dev->config[PCI_INTERRUPT_PIN] != 0) {
  112. info->has_irq = true;
  113. info->irq = dev->config[PCI_INTERRUPT_LINE];
  114. }
  115. type = dev->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
  116. if (type == PCI_HEADER_TYPE_BRIDGE) {
  117. info->pci_bridge = qmp_query_pci_bridge(dev, bus, bus_num);
  118. } else if (type == PCI_HEADER_TYPE_NORMAL) {
  119. info->id->has_subsystem = info->id->has_subsystem_vendor = true;
  120. info->id->subsystem = pci_get_word(dev->config + PCI_SUBSYSTEM_ID);
  121. info->id->subsystem_vendor =
  122. pci_get_word(dev->config + PCI_SUBSYSTEM_VENDOR_ID);
  123. } else if (type == PCI_HEADER_TYPE_CARDBUS) {
  124. info->id->has_subsystem = info->id->has_subsystem_vendor = true;
  125. info->id->subsystem = pci_get_word(dev->config + PCI_CB_SUBSYSTEM_ID);
  126. info->id->subsystem_vendor =
  127. pci_get_word(dev->config + PCI_CB_SUBSYSTEM_VENDOR_ID);
  128. }
  129. return info;
  130. }
  131. static PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num)
  132. {
  133. PciDeviceInfoList *head = NULL, **tail = &head;
  134. PCIDevice *dev;
  135. int devfn;
  136. for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
  137. dev = bus->devices[devfn];
  138. if (dev) {
  139. QAPI_LIST_APPEND(tail, qmp_query_pci_device(dev, bus, bus_num));
  140. }
  141. }
  142. return head;
  143. }
  144. static PciInfo *qmp_query_pci_bus(PCIBus *bus, int bus_num)
  145. {
  146. PciInfo *info = NULL;
  147. bus = pci_find_bus_nr(bus, bus_num);
  148. if (bus) {
  149. info = g_malloc0(sizeof(*info));
  150. info->bus = bus_num;
  151. info->devices = qmp_query_pci_devices(bus, bus_num);
  152. }
  153. return info;
  154. }
  155. PciInfoList *qmp_query_pci(Error **errp)
  156. {
  157. PciInfoList *head = NULL, **tail = &head;
  158. PCIHostState *host_bridge;
  159. QLIST_FOREACH(host_bridge, &pci_host_bridges, next) {
  160. QAPI_LIST_APPEND(tail,
  161. qmp_query_pci_bus(host_bridge->bus,
  162. pci_bus_num(host_bridge->bus)));
  163. }
  164. return head;
  165. }