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" 18*edf5ca5dSMarkus 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 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")) { 865ef4a0cbSMarkus Armbruster monitor_printf(mon, "I/O at 0x%04" PRIx64 875ef4a0cbSMarkus Armbruster " [0x%04" PRIx64 "].\n", 885ef4a0cbSMarkus Armbruster addr, addr + size - 1); 895ef4a0cbSMarkus Armbruster } else { 905ef4a0cbSMarkus Armbruster monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64 915ef4a0cbSMarkus Armbruster " [0x%08" PRIx64 "].\n", 925ef4a0cbSMarkus Armbruster region->value->mem_type_64 ? 64 : 32, 935ef4a0cbSMarkus Armbruster region->value->prefetch ? " prefetchable" : "", 945ef4a0cbSMarkus Armbruster addr, addr + size - 1); 955ef4a0cbSMarkus Armbruster } 965ef4a0cbSMarkus Armbruster } 975ef4a0cbSMarkus Armbruster 985ef4a0cbSMarkus Armbruster monitor_printf(mon, " id \"%s\"\n", dev->qdev_id); 995ef4a0cbSMarkus Armbruster 1005ef4a0cbSMarkus Armbruster if (dev->pci_bridge) { 1015ef4a0cbSMarkus Armbruster if (dev->pci_bridge->has_devices) { 1025ef4a0cbSMarkus Armbruster PciDeviceInfoList *cdev; 1035ef4a0cbSMarkus Armbruster for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) { 1045ef4a0cbSMarkus Armbruster hmp_info_pci_device(mon, cdev->value); 1055ef4a0cbSMarkus Armbruster } 1065ef4a0cbSMarkus Armbruster } 1075ef4a0cbSMarkus Armbruster } 1085ef4a0cbSMarkus Armbruster } 1095ef4a0cbSMarkus Armbruster 1105ef4a0cbSMarkus Armbruster void hmp_info_pci(Monitor *mon, const QDict *qdict) 1115ef4a0cbSMarkus Armbruster { 1125ef4a0cbSMarkus Armbruster PciInfoList *info_list, *info; 1135ef4a0cbSMarkus Armbruster 1146be4ddffSMarkus Armbruster info_list = qmp_query_pci(&error_abort); 1155ef4a0cbSMarkus Armbruster 1165ef4a0cbSMarkus Armbruster for (info = info_list; info; info = info->next) { 1175ef4a0cbSMarkus Armbruster PciDeviceInfoList *dev; 1185ef4a0cbSMarkus Armbruster 1195ef4a0cbSMarkus Armbruster for (dev = info->value->devices; dev; dev = dev->next) { 1205ef4a0cbSMarkus Armbruster hmp_info_pci_device(mon, dev->value); 1215ef4a0cbSMarkus Armbruster } 1225ef4a0cbSMarkus Armbruster } 1235ef4a0cbSMarkus Armbruster 1245ef4a0cbSMarkus Armbruster qapi_free_PciInfoList(info_list); 1255ef4a0cbSMarkus Armbruster } 1260bcaaff8SMarkus Armbruster 1270bcaaff8SMarkus Armbruster void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent) 1280bcaaff8SMarkus Armbruster { 1290bcaaff8SMarkus Armbruster PCIDevice *d = (PCIDevice *)dev; 1300bcaaff8SMarkus Armbruster int class = pci_get_word(d->config + PCI_CLASS_DEVICE); 1310bcaaff8SMarkus Armbruster const pci_class_desc *desc = get_class_desc(class); 1320bcaaff8SMarkus Armbruster char ctxt[64]; 1330bcaaff8SMarkus Armbruster PCIIORegion *r; 1340bcaaff8SMarkus Armbruster int i; 1350bcaaff8SMarkus Armbruster 1360bcaaff8SMarkus Armbruster if (desc->desc) { 1370bcaaff8SMarkus Armbruster snprintf(ctxt, sizeof(ctxt), "%s", desc->desc); 1380bcaaff8SMarkus Armbruster } else { 1390bcaaff8SMarkus Armbruster snprintf(ctxt, sizeof(ctxt), "Class %04x", class); 1400bcaaff8SMarkus Armbruster } 1410bcaaff8SMarkus Armbruster 1420bcaaff8SMarkus Armbruster monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, " 1430bcaaff8SMarkus Armbruster "pci id %04x:%04x (sub %04x:%04x)\n", 1440bcaaff8SMarkus Armbruster indent, "", ctxt, pci_dev_bus_num(d), 1450bcaaff8SMarkus Armbruster PCI_SLOT(d->devfn), PCI_FUNC(d->devfn), 1460bcaaff8SMarkus Armbruster pci_get_word(d->config + PCI_VENDOR_ID), 1470bcaaff8SMarkus Armbruster pci_get_word(d->config + PCI_DEVICE_ID), 1480bcaaff8SMarkus Armbruster pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID), 1490bcaaff8SMarkus Armbruster pci_get_word(d->config + PCI_SUBSYSTEM_ID)); 1500bcaaff8SMarkus Armbruster for (i = 0; i < PCI_NUM_REGIONS; i++) { 1510bcaaff8SMarkus Armbruster r = &d->io_regions[i]; 1520bcaaff8SMarkus Armbruster if (!r->size) { 1530bcaaff8SMarkus Armbruster continue; 1540bcaaff8SMarkus Armbruster } 1550bcaaff8SMarkus Armbruster monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS 1560bcaaff8SMarkus Armbruster " [0x%"FMT_PCIBUS"]\n", 1570bcaaff8SMarkus Armbruster indent, "", 1580bcaaff8SMarkus Armbruster i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem", 1590bcaaff8SMarkus Armbruster r->addr, r->addr + r->size - 1); 1600bcaaff8SMarkus Armbruster } 1610bcaaff8SMarkus Armbruster } 162d0e67298SMarkus Armbruster 16374a11ca6SMarkus Armbruster void hmp_pcie_aer_inject_error(Monitor *mon, const QDict *qdict) 164d0e67298SMarkus Armbruster { 165ba235d33SMarkus Armbruster Error *err = NULL; 166d0e67298SMarkus Armbruster const char *id = qdict_get_str(qdict, "id"); 167d0e67298SMarkus Armbruster const char *error_name; 168d0e67298SMarkus Armbruster uint32_t error_status; 169d0e67298SMarkus Armbruster unsigned int num; 170d0e67298SMarkus Armbruster bool correctable; 171d0e67298SMarkus Armbruster PCIDevice *dev; 172c276dc89SMarkus Armbruster PCIEAERErr aer_err; 173d0e67298SMarkus Armbruster int ret; 174d0e67298SMarkus Armbruster 175d0e67298SMarkus Armbruster ret = pci_qdev_find_device(id, &dev); 176ba235d33SMarkus Armbruster if (ret == -ENODEV) { 177ba235d33SMarkus Armbruster error_setg(&err, "device '%s' not found", id); 178ba235d33SMarkus Armbruster goto out; 179d0e67298SMarkus Armbruster } 180ba235d33SMarkus Armbruster if (ret < 0 || !pci_is_express(dev)) { 181ba235d33SMarkus Armbruster error_setg(&err, "device '%s' is not a PCIe device", id); 182ba235d33SMarkus Armbruster goto out; 183d0e67298SMarkus Armbruster } 184d0e67298SMarkus Armbruster 185d0e67298SMarkus Armbruster error_name = qdict_get_str(qdict, "error_status"); 186d0e67298SMarkus Armbruster if (pcie_aer_parse_error_string(error_name, &error_status, &correctable)) { 187d0e67298SMarkus Armbruster if (qemu_strtoui(error_name, NULL, 0, &num) < 0) { 188ba235d33SMarkus Armbruster error_setg(&err, "invalid error status value '%s'", error_name); 189ba235d33SMarkus Armbruster goto out; 190d0e67298SMarkus Armbruster } 191d0e67298SMarkus Armbruster error_status = num; 192d0e67298SMarkus Armbruster correctable = qdict_get_try_bool(qdict, "correctable", false); 193e221cfacSMarkus Armbruster } else { 194e221cfacSMarkus Armbruster if (qdict_haskey(qdict, "correctable")) { 195e221cfacSMarkus Armbruster error_setg(&err, "-c is only valid with numeric error status"); 196e221cfacSMarkus Armbruster goto out; 197e221cfacSMarkus Armbruster } 198d0e67298SMarkus Armbruster } 199c276dc89SMarkus Armbruster aer_err.status = error_status; 200c276dc89SMarkus Armbruster aer_err.source_id = pci_requester_id(dev); 201d0e67298SMarkus Armbruster 202c276dc89SMarkus Armbruster aer_err.flags = 0; 203d0e67298SMarkus Armbruster if (correctable) { 204c276dc89SMarkus Armbruster aer_err.flags |= PCIE_AER_ERR_IS_CORRECTABLE; 205d0e67298SMarkus Armbruster } 206d0e67298SMarkus Armbruster if (qdict_get_try_bool(qdict, "advisory_non_fatal", false)) { 207c276dc89SMarkus Armbruster aer_err.flags |= PCIE_AER_ERR_MAYBE_ADVISORY; 208d0e67298SMarkus Armbruster } 209d0e67298SMarkus Armbruster if (qdict_haskey(qdict, "header0")) { 210c276dc89SMarkus Armbruster aer_err.flags |= PCIE_AER_ERR_HEADER_VALID; 211d0e67298SMarkus Armbruster } 212d0e67298SMarkus Armbruster if (qdict_haskey(qdict, "prefix0")) { 213c276dc89SMarkus Armbruster aer_err.flags |= PCIE_AER_ERR_TLP_PREFIX_PRESENT; 214d0e67298SMarkus Armbruster } 215d0e67298SMarkus Armbruster 216c276dc89SMarkus Armbruster aer_err.header[0] = qdict_get_try_int(qdict, "header0", 0); 217c276dc89SMarkus Armbruster aer_err.header[1] = qdict_get_try_int(qdict, "header1", 0); 218c276dc89SMarkus Armbruster aer_err.header[2] = qdict_get_try_int(qdict, "header2", 0); 219c276dc89SMarkus Armbruster aer_err.header[3] = qdict_get_try_int(qdict, "header3", 0); 220d0e67298SMarkus Armbruster 221c276dc89SMarkus Armbruster aer_err.prefix[0] = qdict_get_try_int(qdict, "prefix0", 0); 222c276dc89SMarkus Armbruster aer_err.prefix[1] = qdict_get_try_int(qdict, "prefix1", 0); 223c276dc89SMarkus Armbruster aer_err.prefix[2] = qdict_get_try_int(qdict, "prefix2", 0); 224c276dc89SMarkus Armbruster aer_err.prefix[3] = qdict_get_try_int(qdict, "prefix3", 0); 225d0e67298SMarkus Armbruster 226c276dc89SMarkus Armbruster ret = pcie_aer_inject_error(dev, &aer_err); 227d0e67298SMarkus Armbruster if (ret < 0) { 228ba235d33SMarkus Armbruster error_setg_errno(&err, -ret, "failed to inject error"); 229ba235d33SMarkus Armbruster goto out; 230d0e67298SMarkus Armbruster } 231d0e67298SMarkus Armbruster 232ba235d33SMarkus Armbruster 233d0e67298SMarkus Armbruster monitor_printf(mon, "OK id: %s root bus: %s, bus: %x devfn: %x.%x\n", 23474a11ca6SMarkus Armbruster id, pci_root_bus_path(dev), pci_dev_bus_num(dev), 23574a11ca6SMarkus Armbruster PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn)); 236ba235d33SMarkus Armbruster 237ba235d33SMarkus Armbruster out: 238ba235d33SMarkus Armbruster hmp_handle_error(mon, err); 239d0e67298SMarkus Armbruster } 240