xref: /openbmc/qemu/hw/pci/pci-hmp-cmds.c (revision 3baa3c9d62bb35904cf6dadbf152922d87f06e8f)
15ef4a0cbSMarkus Armbruster /*
25ef4a0cbSMarkus Armbruster  * HMP commands related to PCI
35ef4a0cbSMarkus Armbruster  *
45ef4a0cbSMarkus Armbruster  * Copyright IBM, Corp. 2011
55ef4a0cbSMarkus Armbruster  *
65ef4a0cbSMarkus Armbruster  * Authors:
75ef4a0cbSMarkus Armbruster  *  Anthony Liguori   <aliguori@us.ibm.com>
85ef4a0cbSMarkus Armbruster  *
95ef4a0cbSMarkus Armbruster  * This work is licensed under the terms of the GNU GPL, version 2.  See
105ef4a0cbSMarkus Armbruster  * the COPYING file in the top-level directory.
115ef4a0cbSMarkus Armbruster  *
125ef4a0cbSMarkus Armbruster  * Contributions after 2012-01-13 are licensed under the terms of the
135ef4a0cbSMarkus Armbruster  * GNU GPL, version 2 or (at your option) any later version.
145ef4a0cbSMarkus Armbruster  */
155ef4a0cbSMarkus Armbruster 
165ef4a0cbSMarkus Armbruster #include "qemu/osdep.h"
170bcaaff8SMarkus Armbruster #include "hw/pci/pci.h"
18edf5ca5dSMarkus Armbruster #include "hw/pci/pci_device.h"
195ef4a0cbSMarkus Armbruster #include "monitor/hmp.h"
205ef4a0cbSMarkus Armbruster #include "monitor/monitor.h"
210bcaaff8SMarkus Armbruster #include "pci-internal.h"
225ef4a0cbSMarkus Armbruster #include "qapi/error.h"
23d0e67298SMarkus Armbruster #include "qapi/qmp/qdict.h"
245ef4a0cbSMarkus Armbruster #include "qapi/qapi-commands-pci.h"
25d0e67298SMarkus Armbruster #include "qemu/cutils.h"
265ef4a0cbSMarkus Armbruster 
hmp_info_pci_device(Monitor * mon,const PciDeviceInfo * dev)275ef4a0cbSMarkus Armbruster static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
285ef4a0cbSMarkus Armbruster {
295ef4a0cbSMarkus Armbruster     PciMemoryRegionList *region;
305ef4a0cbSMarkus Armbruster 
315ef4a0cbSMarkus Armbruster     monitor_printf(mon, "  Bus %2" PRId64 ", ", dev->bus);
325ef4a0cbSMarkus Armbruster     monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
335ef4a0cbSMarkus Armbruster                    dev->slot, dev->function);
345ef4a0cbSMarkus Armbruster     monitor_printf(mon, "    ");
355ef4a0cbSMarkus Armbruster 
365ef4a0cbSMarkus Armbruster     if (dev->class_info->desc) {
375ef4a0cbSMarkus Armbruster         monitor_puts(mon, dev->class_info->desc);
385ef4a0cbSMarkus Armbruster     } else {
395ef4a0cbSMarkus Armbruster         monitor_printf(mon, "Class %04" PRId64, dev->class_info->q_class);
405ef4a0cbSMarkus Armbruster     }
415ef4a0cbSMarkus Armbruster 
425ef4a0cbSMarkus Armbruster     monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
435ef4a0cbSMarkus Armbruster                    dev->id->vendor, dev->id->device);
445ef4a0cbSMarkus Armbruster     if (dev->id->has_subsystem_vendor && dev->id->has_subsystem) {
455ef4a0cbSMarkus Armbruster         monitor_printf(mon, "      PCI subsystem %04" PRIx64 ":%04" PRIx64 "\n",
465ef4a0cbSMarkus Armbruster                        dev->id->subsystem_vendor, dev->id->subsystem);
475ef4a0cbSMarkus Armbruster     }
485ef4a0cbSMarkus Armbruster 
495ef4a0cbSMarkus Armbruster     if (dev->has_irq) {
505ef4a0cbSMarkus Armbruster         monitor_printf(mon, "      IRQ %" PRId64 ", pin %c\n",
515ef4a0cbSMarkus Armbruster                        dev->irq, (char)('A' + dev->irq_pin - 1));
525ef4a0cbSMarkus Armbruster     }
535ef4a0cbSMarkus Armbruster 
545ef4a0cbSMarkus Armbruster     if (dev->pci_bridge) {
555ef4a0cbSMarkus Armbruster         monitor_printf(mon, "      BUS %" PRId64 ".\n",
565ef4a0cbSMarkus Armbruster                        dev->pci_bridge->bus->number);
575ef4a0cbSMarkus Armbruster         monitor_printf(mon, "      secondary bus %" PRId64 ".\n",
585ef4a0cbSMarkus Armbruster                        dev->pci_bridge->bus->secondary);
595ef4a0cbSMarkus Armbruster         monitor_printf(mon, "      subordinate bus %" PRId64 ".\n",
605ef4a0cbSMarkus Armbruster                        dev->pci_bridge->bus->subordinate);
615ef4a0cbSMarkus Armbruster 
625ef4a0cbSMarkus Armbruster         monitor_printf(mon, "      IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
635ef4a0cbSMarkus Armbruster                        dev->pci_bridge->bus->io_range->base,
645ef4a0cbSMarkus Armbruster                        dev->pci_bridge->bus->io_range->limit);
655ef4a0cbSMarkus Armbruster 
665ef4a0cbSMarkus Armbruster         monitor_printf(mon,
675ef4a0cbSMarkus Armbruster                        "      memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
685ef4a0cbSMarkus Armbruster                        dev->pci_bridge->bus->memory_range->base,
695ef4a0cbSMarkus Armbruster                        dev->pci_bridge->bus->memory_range->limit);
705ef4a0cbSMarkus Armbruster 
715ef4a0cbSMarkus Armbruster         monitor_printf(mon, "      prefetchable memory range "
725ef4a0cbSMarkus Armbruster                        "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
735ef4a0cbSMarkus Armbruster                        dev->pci_bridge->bus->prefetchable_range->base,
745ef4a0cbSMarkus Armbruster                        dev->pci_bridge->bus->prefetchable_range->limit);
755ef4a0cbSMarkus Armbruster     }
765ef4a0cbSMarkus Armbruster 
775ef4a0cbSMarkus Armbruster     for (region = dev->regions; region; region = region->next) {
785ef4a0cbSMarkus Armbruster         uint64_t addr, size;
795ef4a0cbSMarkus Armbruster 
805ef4a0cbSMarkus Armbruster         addr = region->value->address;
815ef4a0cbSMarkus Armbruster         size = region->value->size;
825ef4a0cbSMarkus Armbruster 
835ef4a0cbSMarkus Armbruster         monitor_printf(mon, "      BAR%" PRId64 ": ", region->value->bar);
845ef4a0cbSMarkus Armbruster 
855ef4a0cbSMarkus Armbruster         if (!strcmp(region->value->type, "io")) {
86*aa37616fSPhilippe Mathieu-Daudé             if (addr != PCI_BAR_UNMAPPED) {
875ef4a0cbSMarkus Armbruster                 monitor_printf(mon, "I/O at 0x%04" PRIx64
88*aa37616fSPhilippe Mathieu-Daudé                                     " [0x%04" PRIx64 "]\n",
895ef4a0cbSMarkus Armbruster                                addr, addr + size - 1);
905ef4a0cbSMarkus Armbruster             } else {
91*aa37616fSPhilippe Mathieu-Daudé                 monitor_printf(mon, "I/O (not mapped)\n");
92*aa37616fSPhilippe Mathieu-Daudé             }
93*aa37616fSPhilippe Mathieu-Daudé         } else {
94*aa37616fSPhilippe Mathieu-Daudé             if (addr != PCI_BAR_UNMAPPED) {
955ef4a0cbSMarkus Armbruster                 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
96*aa37616fSPhilippe Mathieu-Daudé                                    " [0x%08" PRIx64 "]\n",
975ef4a0cbSMarkus Armbruster                                region->value->mem_type_64 ? 64 : 32,
985ef4a0cbSMarkus Armbruster                                region->value->prefetch ? " prefetchable" : "",
995ef4a0cbSMarkus Armbruster                                addr, addr + size - 1);
100*aa37616fSPhilippe Mathieu-Daudé             } else {
101*aa37616fSPhilippe Mathieu-Daudé                 monitor_printf(mon, "%d bit%s memory (not mapped)\n",
102*aa37616fSPhilippe Mathieu-Daudé                                region->value->mem_type_64 ? 64 : 32,
103*aa37616fSPhilippe Mathieu-Daudé                                region->value->prefetch ? " prefetchable" : "");
104*aa37616fSPhilippe Mathieu-Daudé             }
1055ef4a0cbSMarkus Armbruster         }
1065ef4a0cbSMarkus Armbruster     }
1075ef4a0cbSMarkus Armbruster 
1085ef4a0cbSMarkus Armbruster     monitor_printf(mon, "      id \"%s\"\n", dev->qdev_id);
1095ef4a0cbSMarkus Armbruster 
1105ef4a0cbSMarkus Armbruster     if (dev->pci_bridge) {
1115ef4a0cbSMarkus Armbruster         if (dev->pci_bridge->has_devices) {
1125ef4a0cbSMarkus Armbruster             PciDeviceInfoList *cdev;
1135ef4a0cbSMarkus Armbruster             for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
1145ef4a0cbSMarkus Armbruster                 hmp_info_pci_device(mon, cdev->value);
1155ef4a0cbSMarkus Armbruster             }
1165ef4a0cbSMarkus Armbruster         }
1175ef4a0cbSMarkus Armbruster     }
1185ef4a0cbSMarkus Armbruster }
1195ef4a0cbSMarkus Armbruster 
hmp_info_pci(Monitor * mon,const QDict * qdict)1205ef4a0cbSMarkus Armbruster void hmp_info_pci(Monitor *mon, const QDict *qdict)
1215ef4a0cbSMarkus Armbruster {
1225ef4a0cbSMarkus Armbruster     PciInfoList *info_list, *info;
1235ef4a0cbSMarkus Armbruster 
1246be4ddffSMarkus Armbruster     info_list = qmp_query_pci(&error_abort);
1255ef4a0cbSMarkus Armbruster 
1265ef4a0cbSMarkus Armbruster     for (info = info_list; info; info = info->next) {
1275ef4a0cbSMarkus Armbruster         PciDeviceInfoList *dev;
1285ef4a0cbSMarkus Armbruster 
1295ef4a0cbSMarkus Armbruster         for (dev = info->value->devices; dev; dev = dev->next) {
1305ef4a0cbSMarkus Armbruster             hmp_info_pci_device(mon, dev->value);
1315ef4a0cbSMarkus Armbruster         }
1325ef4a0cbSMarkus Armbruster     }
1335ef4a0cbSMarkus Armbruster 
1345ef4a0cbSMarkus Armbruster     qapi_free_PciInfoList(info_list);
1355ef4a0cbSMarkus Armbruster }
1360bcaaff8SMarkus Armbruster 
pcibus_dev_print(Monitor * mon,DeviceState * dev,int indent)1370bcaaff8SMarkus Armbruster void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent)
1380bcaaff8SMarkus Armbruster {
1390bcaaff8SMarkus Armbruster     PCIDevice *d = (PCIDevice *)dev;
1400bcaaff8SMarkus Armbruster     int class = pci_get_word(d->config + PCI_CLASS_DEVICE);
1410bcaaff8SMarkus Armbruster     const pci_class_desc *desc = get_class_desc(class);
1420bcaaff8SMarkus Armbruster     char ctxt[64];
1430bcaaff8SMarkus Armbruster     PCIIORegion *r;
1440bcaaff8SMarkus Armbruster     int i;
1450bcaaff8SMarkus Armbruster 
1460bcaaff8SMarkus Armbruster     if (desc->desc) {
1470bcaaff8SMarkus Armbruster         snprintf(ctxt, sizeof(ctxt), "%s", desc->desc);
1480bcaaff8SMarkus Armbruster     } else {
1490bcaaff8SMarkus Armbruster         snprintf(ctxt, sizeof(ctxt), "Class %04x", class);
1500bcaaff8SMarkus Armbruster     }
1510bcaaff8SMarkus Armbruster 
1520bcaaff8SMarkus Armbruster     monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
1530bcaaff8SMarkus Armbruster                    "pci id %04x:%04x (sub %04x:%04x)\n",
1540bcaaff8SMarkus Armbruster                    indent, "", ctxt, pci_dev_bus_num(d),
1550bcaaff8SMarkus Armbruster                    PCI_SLOT(d->devfn), PCI_FUNC(d->devfn),
1560bcaaff8SMarkus Armbruster                    pci_get_word(d->config + PCI_VENDOR_ID),
1570bcaaff8SMarkus Armbruster                    pci_get_word(d->config + PCI_DEVICE_ID),
1580bcaaff8SMarkus Armbruster                    pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID),
1590bcaaff8SMarkus Armbruster                    pci_get_word(d->config + PCI_SUBSYSTEM_ID));
1600bcaaff8SMarkus Armbruster     for (i = 0; i < PCI_NUM_REGIONS; i++) {
1610bcaaff8SMarkus Armbruster         r = &d->io_regions[i];
1620bcaaff8SMarkus Armbruster         if (!r->size) {
1630bcaaff8SMarkus Armbruster             continue;
1640bcaaff8SMarkus Armbruster         }
1650bcaaff8SMarkus Armbruster         monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
1660bcaaff8SMarkus Armbruster                        " [0x%"FMT_PCIBUS"]\n",
1670bcaaff8SMarkus Armbruster                        indent, "",
1680bcaaff8SMarkus Armbruster                        i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem",
1690bcaaff8SMarkus Armbruster                        r->addr, r->addr + r->size - 1);
1700bcaaff8SMarkus Armbruster     }
1710bcaaff8SMarkus Armbruster }
172d0e67298SMarkus Armbruster 
hmp_pcie_aer_inject_error(Monitor * mon,const QDict * qdict)17374a11ca6SMarkus Armbruster void hmp_pcie_aer_inject_error(Monitor *mon, const QDict *qdict)
174d0e67298SMarkus Armbruster {
175ba235d33SMarkus Armbruster     Error *err = NULL;
176d0e67298SMarkus Armbruster     const char *id = qdict_get_str(qdict, "id");
177d0e67298SMarkus Armbruster     const char *error_name;
178d0e67298SMarkus Armbruster     uint32_t error_status;
179d0e67298SMarkus Armbruster     unsigned int num;
180d0e67298SMarkus Armbruster     bool correctable;
181d0e67298SMarkus Armbruster     PCIDevice *dev;
182c276dc89SMarkus Armbruster     PCIEAERErr aer_err;
183d0e67298SMarkus Armbruster     int ret;
184d0e67298SMarkus Armbruster 
185d0e67298SMarkus Armbruster     ret = pci_qdev_find_device(id, &dev);
186ba235d33SMarkus Armbruster     if (ret == -ENODEV) {
187ba235d33SMarkus Armbruster         error_setg(&err, "device '%s' not found", id);
188ba235d33SMarkus Armbruster         goto out;
189d0e67298SMarkus Armbruster     }
190ba235d33SMarkus Armbruster     if (ret < 0 || !pci_is_express(dev)) {
191ba235d33SMarkus Armbruster         error_setg(&err, "device '%s' is not a PCIe device", id);
192ba235d33SMarkus Armbruster         goto out;
193d0e67298SMarkus Armbruster     }
194d0e67298SMarkus Armbruster 
195d0e67298SMarkus Armbruster     error_name = qdict_get_str(qdict, "error_status");
196d0e67298SMarkus Armbruster     if (pcie_aer_parse_error_string(error_name, &error_status, &correctable)) {
197d0e67298SMarkus Armbruster         if (qemu_strtoui(error_name, NULL, 0, &num) < 0) {
198ba235d33SMarkus Armbruster             error_setg(&err, "invalid error status value '%s'", error_name);
199ba235d33SMarkus Armbruster             goto out;
200d0e67298SMarkus Armbruster         }
201d0e67298SMarkus Armbruster         error_status = num;
202d0e67298SMarkus Armbruster         correctable = qdict_get_try_bool(qdict, "correctable", false);
203e221cfacSMarkus Armbruster     } else {
204e221cfacSMarkus Armbruster         if (qdict_haskey(qdict, "correctable")) {
205e221cfacSMarkus Armbruster             error_setg(&err, "-c is only valid with numeric error status");
206e221cfacSMarkus Armbruster             goto out;
207e221cfacSMarkus Armbruster         }
208d0e67298SMarkus Armbruster     }
209c276dc89SMarkus Armbruster     aer_err.status = error_status;
210c276dc89SMarkus Armbruster     aer_err.source_id = pci_requester_id(dev);
211d0e67298SMarkus Armbruster 
212c276dc89SMarkus Armbruster     aer_err.flags = 0;
213d0e67298SMarkus Armbruster     if (correctable) {
214c276dc89SMarkus Armbruster         aer_err.flags |= PCIE_AER_ERR_IS_CORRECTABLE;
215d0e67298SMarkus Armbruster     }
216d0e67298SMarkus Armbruster     if (qdict_get_try_bool(qdict, "advisory_non_fatal", false)) {
217c276dc89SMarkus Armbruster         aer_err.flags |= PCIE_AER_ERR_MAYBE_ADVISORY;
218d0e67298SMarkus Armbruster     }
219d0e67298SMarkus Armbruster     if (qdict_haskey(qdict, "header0")) {
220c276dc89SMarkus Armbruster         aer_err.flags |= PCIE_AER_ERR_HEADER_VALID;
221d0e67298SMarkus Armbruster     }
222d0e67298SMarkus Armbruster     if (qdict_haskey(qdict, "prefix0")) {
223c276dc89SMarkus Armbruster         aer_err.flags |= PCIE_AER_ERR_TLP_PREFIX_PRESENT;
224d0e67298SMarkus Armbruster     }
225d0e67298SMarkus Armbruster 
226c276dc89SMarkus Armbruster     aer_err.header[0] = qdict_get_try_int(qdict, "header0", 0);
227c276dc89SMarkus Armbruster     aer_err.header[1] = qdict_get_try_int(qdict, "header1", 0);
228c276dc89SMarkus Armbruster     aer_err.header[2] = qdict_get_try_int(qdict, "header2", 0);
229c276dc89SMarkus Armbruster     aer_err.header[3] = qdict_get_try_int(qdict, "header3", 0);
230d0e67298SMarkus Armbruster 
231c276dc89SMarkus Armbruster     aer_err.prefix[0] = qdict_get_try_int(qdict, "prefix0", 0);
232c276dc89SMarkus Armbruster     aer_err.prefix[1] = qdict_get_try_int(qdict, "prefix1", 0);
233c276dc89SMarkus Armbruster     aer_err.prefix[2] = qdict_get_try_int(qdict, "prefix2", 0);
234c276dc89SMarkus Armbruster     aer_err.prefix[3] = qdict_get_try_int(qdict, "prefix3", 0);
235d0e67298SMarkus Armbruster 
236c276dc89SMarkus Armbruster     ret = pcie_aer_inject_error(dev, &aer_err);
237d0e67298SMarkus Armbruster     if (ret < 0) {
238ba235d33SMarkus Armbruster         error_setg_errno(&err, -ret, "failed to inject error");
239ba235d33SMarkus Armbruster         goto out;
240d0e67298SMarkus Armbruster     }
241d0e67298SMarkus Armbruster 
242ba235d33SMarkus Armbruster 
243d0e67298SMarkus Armbruster     monitor_printf(mon, "OK id: %s root bus: %s, bus: %x devfn: %x.%x\n",
24474a11ca6SMarkus Armbruster                    id, pci_root_bus_path(dev), pci_dev_bus_num(dev),
24574a11ca6SMarkus Armbruster                    PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
246ba235d33SMarkus Armbruster 
247ba235d33SMarkus Armbruster out:
248ba235d33SMarkus Armbruster     hmp_handle_error(mon, err);
249d0e67298SMarkus Armbruster }
250