hw/ppc: Clean up local variable shadowing in _FDT helper routine
[qemu/kevin.git] / hw / pci / pci-qmp-cmds.c
blob5d9f4817f50cea859d61df284385b238a741ce77
1 /*
2 * QMP commands related to PCI
4 * Copyright (c) 2004 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "qemu/osdep.h"
26 #include "hw/pci/pci.h"
27 #include "hw/pci/pci_bridge.h"
28 #include "pci-internal.h"
29 #include "qapi/qapi-commands-pci.h"
31 static PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num);
33 static PciMemoryRegionList *qmp_query_pci_regions(const PCIDevice *dev)
35 PciMemoryRegionList *head = NULL, **tail = &head;
36 int i;
38 for (i = 0; i < PCI_NUM_REGIONS; i++) {
39 const PCIIORegion *r = &dev->io_regions[i];
40 PciMemoryRegion *region;
42 if (!r->size) {
43 continue;
46 region = g_malloc0(sizeof(*region));
48 if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
49 region->type = g_strdup("io");
50 } else {
51 region->type = g_strdup("memory");
52 region->has_prefetch = true;
53 region->prefetch = !!(r->type & PCI_BASE_ADDRESS_MEM_PREFETCH);
54 region->has_mem_type_64 = true;
55 region->mem_type_64 = !!(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64);
58 region->bar = i;
59 region->address = r->addr;
60 region->size = r->size;
62 QAPI_LIST_APPEND(tail, region);
65 return head;
68 static PciBridgeInfo *qmp_query_pci_bridge(PCIDevice *dev, PCIBus *bus,
69 int bus_num)
71 PciBridgeInfo *info;
72 PciMemoryRange *range;
74 info = g_new0(PciBridgeInfo, 1);
76 info->bus = g_new0(PciBusInfo, 1);
77 info->bus->number = dev->config[PCI_PRIMARY_BUS];
78 info->bus->secondary = dev->config[PCI_SECONDARY_BUS];
79 info->bus->subordinate = dev->config[PCI_SUBORDINATE_BUS];
81 range = info->bus->io_range = g_new0(PciMemoryRange, 1);
82 range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_IO);
83 range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_IO);
85 range = info->bus->memory_range = g_new0(PciMemoryRange, 1);
86 range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY);
87 range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY);
89 range = info->bus->prefetchable_range = g_new0(PciMemoryRange, 1);
90 range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
91 range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
93 if (dev->config[PCI_SECONDARY_BUS] != 0) {
94 PCIBus *child_bus = pci_find_bus_nr(bus,
95 dev->config[PCI_SECONDARY_BUS]);
96 if (child_bus) {
97 info->has_devices = true;
98 info->devices = qmp_query_pci_devices(child_bus,
99 dev->config[PCI_SECONDARY_BUS]);
103 return info;
106 static PciDeviceInfo *qmp_query_pci_device(PCIDevice *dev, PCIBus *bus,
107 int bus_num)
109 const pci_class_desc *desc;
110 PciDeviceInfo *info;
111 uint8_t type;
112 int class;
114 info = g_new0(PciDeviceInfo, 1);
115 info->bus = bus_num;
116 info->slot = PCI_SLOT(dev->devfn);
117 info->function = PCI_FUNC(dev->devfn);
119 info->class_info = g_new0(PciDeviceClass, 1);
120 class = pci_get_word(dev->config + PCI_CLASS_DEVICE);
121 info->class_info->q_class = class;
122 desc = get_class_desc(class);
123 if (desc->desc) {
124 info->class_info->desc = g_strdup(desc->desc);
127 info->id = g_new0(PciDeviceId, 1);
128 info->id->vendor = pci_get_word(dev->config + PCI_VENDOR_ID);
129 info->id->device = pci_get_word(dev->config + PCI_DEVICE_ID);
130 info->regions = qmp_query_pci_regions(dev);
131 info->qdev_id = g_strdup(dev->qdev.id ? dev->qdev.id : "");
133 info->irq_pin = dev->config[PCI_INTERRUPT_PIN];
134 if (dev->config[PCI_INTERRUPT_PIN] != 0) {
135 info->has_irq = true;
136 info->irq = dev->config[PCI_INTERRUPT_LINE];
139 type = dev->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
140 if (type == PCI_HEADER_TYPE_BRIDGE) {
141 info->pci_bridge = qmp_query_pci_bridge(dev, bus, bus_num);
142 } else if (type == PCI_HEADER_TYPE_NORMAL) {
143 info->id->has_subsystem = info->id->has_subsystem_vendor = true;
144 info->id->subsystem = pci_get_word(dev->config + PCI_SUBSYSTEM_ID);
145 info->id->subsystem_vendor =
146 pci_get_word(dev->config + PCI_SUBSYSTEM_VENDOR_ID);
147 } else if (type == PCI_HEADER_TYPE_CARDBUS) {
148 info->id->has_subsystem = info->id->has_subsystem_vendor = true;
149 info->id->subsystem = pci_get_word(dev->config + PCI_CB_SUBSYSTEM_ID);
150 info->id->subsystem_vendor =
151 pci_get_word(dev->config + PCI_CB_SUBSYSTEM_VENDOR_ID);
154 return info;
157 static PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num)
159 PciDeviceInfoList *head = NULL, **tail = &head;
160 PCIDevice *dev;
161 int devfn;
163 for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
164 dev = bus->devices[devfn];
165 if (dev) {
166 QAPI_LIST_APPEND(tail, qmp_query_pci_device(dev, bus, bus_num));
170 return head;
173 static PciInfo *qmp_query_pci_bus(PCIBus *bus, int bus_num)
175 PciInfo *info = NULL;
177 bus = pci_find_bus_nr(bus, bus_num);
178 if (bus) {
179 info = g_malloc0(sizeof(*info));
180 info->bus = bus_num;
181 info->devices = qmp_query_pci_devices(bus, bus_num);
184 return info;
187 PciInfoList *qmp_query_pci(Error **errp)
189 PciInfoList *head = NULL, **tail = &head;
190 PCIHostState *host_bridge;
192 QLIST_FOREACH(host_bridge, &pci_host_bridges, next) {
193 QAPI_LIST_APPEND(tail,
194 qmp_query_pci_bus(host_bridge->bus,
195 pci_bus_num(host_bridge->bus)));
198 return head;