xref: /openbmc/qemu/hw/pci/pci-hmp-cmds.c (revision c276dc8930de708448e38695d5821c117ea1bc20)
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"
185ef4a0cbSMarkus Armbruster #include "monitor/hmp.h"
195ef4a0cbSMarkus Armbruster #include "monitor/monitor.h"
200bcaaff8SMarkus Armbruster #include "pci-internal.h"
215ef4a0cbSMarkus Armbruster #include "qapi/error.h"
22d0e67298SMarkus Armbruster #include "qapi/qmp/qdict.h"
235ef4a0cbSMarkus Armbruster #include "qapi/qapi-commands-pci.h"
24d0e67298SMarkus Armbruster #include "qemu/cutils.h"
255ef4a0cbSMarkus Armbruster 
265ef4a0cbSMarkus Armbruster static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
275ef4a0cbSMarkus Armbruster {
285ef4a0cbSMarkus Armbruster     PciMemoryRegionList *region;
295ef4a0cbSMarkus Armbruster 
305ef4a0cbSMarkus Armbruster     monitor_printf(mon, "  Bus %2" PRId64 ", ", dev->bus);
315ef4a0cbSMarkus Armbruster     monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
325ef4a0cbSMarkus Armbruster                    dev->slot, dev->function);
335ef4a0cbSMarkus Armbruster     monitor_printf(mon, "    ");
345ef4a0cbSMarkus Armbruster 
355ef4a0cbSMarkus Armbruster     if (dev->class_info->desc) {
365ef4a0cbSMarkus Armbruster         monitor_puts(mon, dev->class_info->desc);
375ef4a0cbSMarkus Armbruster     } else {
385ef4a0cbSMarkus Armbruster         monitor_printf(mon, "Class %04" PRId64, dev->class_info->q_class);
395ef4a0cbSMarkus Armbruster     }
405ef4a0cbSMarkus Armbruster 
415ef4a0cbSMarkus Armbruster     monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
425ef4a0cbSMarkus Armbruster                    dev->id->vendor, dev->id->device);
435ef4a0cbSMarkus Armbruster     if (dev->id->has_subsystem_vendor && dev->id->has_subsystem) {
445ef4a0cbSMarkus Armbruster         monitor_printf(mon, "      PCI subsystem %04" PRIx64 ":%04" PRIx64 "\n",
455ef4a0cbSMarkus Armbruster                        dev->id->subsystem_vendor, dev->id->subsystem);
465ef4a0cbSMarkus Armbruster     }
475ef4a0cbSMarkus Armbruster 
485ef4a0cbSMarkus Armbruster     if (dev->has_irq) {
495ef4a0cbSMarkus Armbruster         monitor_printf(mon, "      IRQ %" PRId64 ", pin %c\n",
505ef4a0cbSMarkus Armbruster                        dev->irq, (char)('A' + dev->irq_pin - 1));
515ef4a0cbSMarkus Armbruster     }
525ef4a0cbSMarkus Armbruster 
535ef4a0cbSMarkus Armbruster     if (dev->pci_bridge) {
545ef4a0cbSMarkus Armbruster         monitor_printf(mon, "      BUS %" PRId64 ".\n",
555ef4a0cbSMarkus Armbruster                        dev->pci_bridge->bus->number);
565ef4a0cbSMarkus Armbruster         monitor_printf(mon, "      secondary bus %" PRId64 ".\n",
575ef4a0cbSMarkus Armbruster                        dev->pci_bridge->bus->secondary);
585ef4a0cbSMarkus Armbruster         monitor_printf(mon, "      subordinate bus %" PRId64 ".\n",
595ef4a0cbSMarkus Armbruster                        dev->pci_bridge->bus->subordinate);
605ef4a0cbSMarkus Armbruster 
615ef4a0cbSMarkus Armbruster         monitor_printf(mon, "      IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
625ef4a0cbSMarkus Armbruster                        dev->pci_bridge->bus->io_range->base,
635ef4a0cbSMarkus Armbruster                        dev->pci_bridge->bus->io_range->limit);
645ef4a0cbSMarkus Armbruster 
655ef4a0cbSMarkus Armbruster         monitor_printf(mon,
665ef4a0cbSMarkus Armbruster                        "      memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
675ef4a0cbSMarkus Armbruster                        dev->pci_bridge->bus->memory_range->base,
685ef4a0cbSMarkus Armbruster                        dev->pci_bridge->bus->memory_range->limit);
695ef4a0cbSMarkus Armbruster 
705ef4a0cbSMarkus Armbruster         monitor_printf(mon, "      prefetchable memory range "
715ef4a0cbSMarkus Armbruster                        "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
725ef4a0cbSMarkus Armbruster                        dev->pci_bridge->bus->prefetchable_range->base,
735ef4a0cbSMarkus Armbruster                        dev->pci_bridge->bus->prefetchable_range->limit);
745ef4a0cbSMarkus Armbruster     }
755ef4a0cbSMarkus Armbruster 
765ef4a0cbSMarkus Armbruster     for (region = dev->regions; region; region = region->next) {
775ef4a0cbSMarkus Armbruster         uint64_t addr, size;
785ef4a0cbSMarkus Armbruster 
795ef4a0cbSMarkus Armbruster         addr = region->value->address;
805ef4a0cbSMarkus Armbruster         size = region->value->size;
815ef4a0cbSMarkus Armbruster 
825ef4a0cbSMarkus Armbruster         monitor_printf(mon, "      BAR%" PRId64 ": ", region->value->bar);
835ef4a0cbSMarkus Armbruster 
845ef4a0cbSMarkus Armbruster         if (!strcmp(region->value->type, "io")) {
855ef4a0cbSMarkus Armbruster             monitor_printf(mon, "I/O at 0x%04" PRIx64
865ef4a0cbSMarkus Armbruster                                 " [0x%04" PRIx64 "].\n",
875ef4a0cbSMarkus Armbruster                            addr, addr + size - 1);
885ef4a0cbSMarkus Armbruster         } else {
895ef4a0cbSMarkus Armbruster             monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
905ef4a0cbSMarkus Armbruster                                " [0x%08" PRIx64 "].\n",
915ef4a0cbSMarkus Armbruster                            region->value->mem_type_64 ? 64 : 32,
925ef4a0cbSMarkus Armbruster                            region->value->prefetch ? " prefetchable" : "",
935ef4a0cbSMarkus Armbruster                            addr, addr + size - 1);
945ef4a0cbSMarkus Armbruster         }
955ef4a0cbSMarkus Armbruster     }
965ef4a0cbSMarkus Armbruster 
975ef4a0cbSMarkus Armbruster     monitor_printf(mon, "      id \"%s\"\n", dev->qdev_id);
985ef4a0cbSMarkus Armbruster 
995ef4a0cbSMarkus Armbruster     if (dev->pci_bridge) {
1005ef4a0cbSMarkus Armbruster         if (dev->pci_bridge->has_devices) {
1015ef4a0cbSMarkus Armbruster             PciDeviceInfoList *cdev;
1025ef4a0cbSMarkus Armbruster             for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
1035ef4a0cbSMarkus Armbruster                 hmp_info_pci_device(mon, cdev->value);
1045ef4a0cbSMarkus Armbruster             }
1055ef4a0cbSMarkus Armbruster         }
1065ef4a0cbSMarkus Armbruster     }
1075ef4a0cbSMarkus Armbruster }
1085ef4a0cbSMarkus Armbruster 
1095ef4a0cbSMarkus Armbruster void hmp_info_pci(Monitor *mon, const QDict *qdict)
1105ef4a0cbSMarkus Armbruster {
1115ef4a0cbSMarkus Armbruster     PciInfoList *info_list, *info;
1125ef4a0cbSMarkus Armbruster 
1136be4ddffSMarkus Armbruster     info_list = qmp_query_pci(&error_abort);
1145ef4a0cbSMarkus Armbruster 
1155ef4a0cbSMarkus Armbruster     for (info = info_list; info; info = info->next) {
1165ef4a0cbSMarkus Armbruster         PciDeviceInfoList *dev;
1175ef4a0cbSMarkus Armbruster 
1185ef4a0cbSMarkus Armbruster         for (dev = info->value->devices; dev; dev = dev->next) {
1195ef4a0cbSMarkus Armbruster             hmp_info_pci_device(mon, dev->value);
1205ef4a0cbSMarkus Armbruster         }
1215ef4a0cbSMarkus Armbruster     }
1225ef4a0cbSMarkus Armbruster 
1235ef4a0cbSMarkus Armbruster     qapi_free_PciInfoList(info_list);
1245ef4a0cbSMarkus Armbruster }
1250bcaaff8SMarkus Armbruster 
1260bcaaff8SMarkus Armbruster void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent)
1270bcaaff8SMarkus Armbruster {
1280bcaaff8SMarkus Armbruster     PCIDevice *d = (PCIDevice *)dev;
1290bcaaff8SMarkus Armbruster     int class = pci_get_word(d->config + PCI_CLASS_DEVICE);
1300bcaaff8SMarkus Armbruster     const pci_class_desc *desc = get_class_desc(class);
1310bcaaff8SMarkus Armbruster     char ctxt[64];
1320bcaaff8SMarkus Armbruster     PCIIORegion *r;
1330bcaaff8SMarkus Armbruster     int i;
1340bcaaff8SMarkus Armbruster 
1350bcaaff8SMarkus Armbruster     if (desc->desc) {
1360bcaaff8SMarkus Armbruster         snprintf(ctxt, sizeof(ctxt), "%s", desc->desc);
1370bcaaff8SMarkus Armbruster     } else {
1380bcaaff8SMarkus Armbruster         snprintf(ctxt, sizeof(ctxt), "Class %04x", class);
1390bcaaff8SMarkus Armbruster     }
1400bcaaff8SMarkus Armbruster 
1410bcaaff8SMarkus Armbruster     monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
1420bcaaff8SMarkus Armbruster                    "pci id %04x:%04x (sub %04x:%04x)\n",
1430bcaaff8SMarkus Armbruster                    indent, "", ctxt, pci_dev_bus_num(d),
1440bcaaff8SMarkus Armbruster                    PCI_SLOT(d->devfn), PCI_FUNC(d->devfn),
1450bcaaff8SMarkus Armbruster                    pci_get_word(d->config + PCI_VENDOR_ID),
1460bcaaff8SMarkus Armbruster                    pci_get_word(d->config + PCI_DEVICE_ID),
1470bcaaff8SMarkus Armbruster                    pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID),
1480bcaaff8SMarkus Armbruster                    pci_get_word(d->config + PCI_SUBSYSTEM_ID));
1490bcaaff8SMarkus Armbruster     for (i = 0; i < PCI_NUM_REGIONS; i++) {
1500bcaaff8SMarkus Armbruster         r = &d->io_regions[i];
1510bcaaff8SMarkus Armbruster         if (!r->size) {
1520bcaaff8SMarkus Armbruster             continue;
1530bcaaff8SMarkus Armbruster         }
1540bcaaff8SMarkus Armbruster         monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
1550bcaaff8SMarkus Armbruster                        " [0x%"FMT_PCIBUS"]\n",
1560bcaaff8SMarkus Armbruster                        indent, "",
1570bcaaff8SMarkus Armbruster                        i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem",
1580bcaaff8SMarkus Armbruster                        r->addr, r->addr + r->size - 1);
1590bcaaff8SMarkus Armbruster     }
1600bcaaff8SMarkus Armbruster }
161d0e67298SMarkus Armbruster 
16274a11ca6SMarkus Armbruster void hmp_pcie_aer_inject_error(Monitor *mon, const QDict *qdict)
163d0e67298SMarkus Armbruster {
164d0e67298SMarkus Armbruster     const char *id = qdict_get_str(qdict, "id");
165d0e67298SMarkus Armbruster     const char *error_name;
166d0e67298SMarkus Armbruster     uint32_t error_status;
167d0e67298SMarkus Armbruster     unsigned int num;
168d0e67298SMarkus Armbruster     bool correctable;
169d0e67298SMarkus Armbruster     PCIDevice *dev;
170*c276dc89SMarkus Armbruster     PCIEAERErr aer_err;
171d0e67298SMarkus Armbruster     int ret;
172d0e67298SMarkus Armbruster 
173d0e67298SMarkus Armbruster     ret = pci_qdev_find_device(id, &dev);
174d0e67298SMarkus Armbruster     if (ret < 0) {
175d0e67298SMarkus Armbruster         monitor_printf(mon,
176d0e67298SMarkus Armbruster                        "id or pci device path is invalid or device not "
177d0e67298SMarkus Armbruster                        "found. %s\n", id);
17874a11ca6SMarkus Armbruster         return;
179d0e67298SMarkus Armbruster     }
180d0e67298SMarkus Armbruster     if (!pci_is_express(dev)) {
181d0e67298SMarkus Armbruster         monitor_printf(mon, "the device doesn't support pci express. %s\n",
182d0e67298SMarkus Armbruster                        id);
18374a11ca6SMarkus Armbruster         return;
184d0e67298SMarkus Armbruster     }
185d0e67298SMarkus Armbruster 
186d0e67298SMarkus Armbruster     error_name = qdict_get_str(qdict, "error_status");
187d0e67298SMarkus Armbruster     if (pcie_aer_parse_error_string(error_name, &error_status, &correctable)) {
188d0e67298SMarkus Armbruster         if (qemu_strtoui(error_name, NULL, 0, &num) < 0) {
189d0e67298SMarkus Armbruster             monitor_printf(mon, "invalid error status value. \"%s\"",
190d0e67298SMarkus Armbruster                            error_name);
19174a11ca6SMarkus Armbruster             return;
192d0e67298SMarkus Armbruster         }
193d0e67298SMarkus Armbruster         error_status = num;
194d0e67298SMarkus Armbruster         correctable = qdict_get_try_bool(qdict, "correctable", false);
195d0e67298SMarkus Armbruster     }
196*c276dc89SMarkus Armbruster     aer_err.status = error_status;
197*c276dc89SMarkus Armbruster     aer_err.source_id = pci_requester_id(dev);
198d0e67298SMarkus Armbruster 
199*c276dc89SMarkus Armbruster     aer_err.flags = 0;
200d0e67298SMarkus Armbruster     if (correctable) {
201*c276dc89SMarkus Armbruster         aer_err.flags |= PCIE_AER_ERR_IS_CORRECTABLE;
202d0e67298SMarkus Armbruster     }
203d0e67298SMarkus Armbruster     if (qdict_get_try_bool(qdict, "advisory_non_fatal", false)) {
204*c276dc89SMarkus Armbruster         aer_err.flags |= PCIE_AER_ERR_MAYBE_ADVISORY;
205d0e67298SMarkus Armbruster     }
206d0e67298SMarkus Armbruster     if (qdict_haskey(qdict, "header0")) {
207*c276dc89SMarkus Armbruster         aer_err.flags |= PCIE_AER_ERR_HEADER_VALID;
208d0e67298SMarkus Armbruster     }
209d0e67298SMarkus Armbruster     if (qdict_haskey(qdict, "prefix0")) {
210*c276dc89SMarkus Armbruster         aer_err.flags |= PCIE_AER_ERR_TLP_PREFIX_PRESENT;
211d0e67298SMarkus Armbruster     }
212d0e67298SMarkus Armbruster 
213*c276dc89SMarkus Armbruster     aer_err.header[0] = qdict_get_try_int(qdict, "header0", 0);
214*c276dc89SMarkus Armbruster     aer_err.header[1] = qdict_get_try_int(qdict, "header1", 0);
215*c276dc89SMarkus Armbruster     aer_err.header[2] = qdict_get_try_int(qdict, "header2", 0);
216*c276dc89SMarkus Armbruster     aer_err.header[3] = qdict_get_try_int(qdict, "header3", 0);
217d0e67298SMarkus Armbruster 
218*c276dc89SMarkus Armbruster     aer_err.prefix[0] = qdict_get_try_int(qdict, "prefix0", 0);
219*c276dc89SMarkus Armbruster     aer_err.prefix[1] = qdict_get_try_int(qdict, "prefix1", 0);
220*c276dc89SMarkus Armbruster     aer_err.prefix[2] = qdict_get_try_int(qdict, "prefix2", 0);
221*c276dc89SMarkus Armbruster     aer_err.prefix[3] = qdict_get_try_int(qdict, "prefix3", 0);
222d0e67298SMarkus Armbruster 
223*c276dc89SMarkus Armbruster     ret = pcie_aer_inject_error(dev, &aer_err);
224d0e67298SMarkus Armbruster     if (ret < 0) {
225d0e67298SMarkus Armbruster         monitor_printf(mon, "failed to inject error: %s\n",
226d0e67298SMarkus Armbruster                        strerror(-ret));
227d0e67298SMarkus Armbruster         return;
228d0e67298SMarkus Armbruster     }
229d0e67298SMarkus Armbruster 
230d0e67298SMarkus Armbruster     monitor_printf(mon, "OK id: %s root bus: %s, bus: %x devfn: %x.%x\n",
23174a11ca6SMarkus Armbruster                    id, pci_root_bus_path(dev), pci_dev_bus_num(dev),
23274a11ca6SMarkus Armbruster                    PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
233d0e67298SMarkus Armbruster }
234