xref: /openbmc/qemu/hw/pci/pci-qmp-cmds.c (revision 05caa062)
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 
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"
30 
31 static PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num);
32 
33 static PciMemoryRegionList *qmp_query_pci_regions(const PCIDevice *dev)
34 {
35     PciMemoryRegionList *head = NULL, **tail = &head;
36     int i;
37 
38     for (i = 0; i < PCI_NUM_REGIONS; i++) {
39         const PCIIORegion *r = &dev->io_regions[i];
40         PciMemoryRegion *region;
41 
42         if (!r->size) {
43             continue;
44         }
45 
46         region = g_malloc0(sizeof(*region));
47 
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);
56         }
57 
58         region->bar = i;
59         region->address = r->addr;
60         region->size = r->size;
61 
62         QAPI_LIST_APPEND(tail, region);
63     }
64 
65     return head;
66 }
67 
68 static PciBridgeInfo *qmp_query_pci_bridge(PCIDevice *dev, PCIBus *bus,
69                                            int bus_num)
70 {
71     PciBridgeInfo *info;
72     PciMemoryRange *range;
73 
74     info = g_new0(PciBridgeInfo, 1);
75 
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];
80 
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);
84 
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);
88 
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);
92 
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]);
100         }
101     }
102 
103     return info;
104 }
105 
106 static PciDeviceInfo *qmp_query_pci_device(PCIDevice *dev, PCIBus *bus,
107                                            int bus_num)
108 {
109     const pci_class_desc *desc;
110     PciDeviceInfo *info;
111     uint8_t type;
112     int class;
113 
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);
118 
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);
125     }
126 
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 : "");
132 
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];
137     }
138 
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);
152     }
153 
154     return info;
155 }
156 
157 static PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num)
158 {
159     PciDeviceInfoList *head = NULL, **tail = &head;
160     PCIDevice *dev;
161     int devfn;
162 
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));
167         }
168     }
169 
170     return head;
171 }
172 
173 static PciInfo *qmp_query_pci_bus(PCIBus *bus, int bus_num)
174 {
175     PciInfo *info = NULL;
176 
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);
182     }
183 
184     return info;
185 }
186 
187 PciInfoList *qmp_query_pci(Error **errp)
188 {
189     PciInfoList *head = NULL, **tail = &head;
190     PCIHostState *host_bridge;
191 
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)));
196     }
197 
198     return head;
199 }
200