pci-hmp-cmds.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * HMP commands related to PCI
  3. *
  4. * Copyright IBM, Corp. 2011
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. *
  12. * Contributions after 2012-01-13 are licensed under the terms of the
  13. * GNU GPL, version 2 or (at your option) any later version.
  14. */
  15. #include "qemu/osdep.h"
  16. #include "hw/pci/pci.h"
  17. #include "hw/pci/pci_device.h"
  18. #include "monitor/hmp.h"
  19. #include "monitor/monitor.h"
  20. #include "pci-internal.h"
  21. #include "qapi/error.h"
  22. #include "qobject/qdict.h"
  23. #include "qapi/qapi-commands-pci.h"
  24. #include "qemu/cutils.h"
  25. static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
  26. {
  27. PciMemoryRegionList *region;
  28. monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
  29. monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
  30. dev->slot, dev->function);
  31. monitor_printf(mon, " ");
  32. if (dev->class_info->desc) {
  33. monitor_puts(mon, dev->class_info->desc);
  34. } else {
  35. monitor_printf(mon, "Class %04" PRId64, dev->class_info->q_class);
  36. }
  37. monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
  38. dev->id->vendor, dev->id->device);
  39. if (dev->id->has_subsystem_vendor && dev->id->has_subsystem) {
  40. monitor_printf(mon, " PCI subsystem %04" PRIx64 ":%04" PRIx64 "\n",
  41. dev->id->subsystem_vendor, dev->id->subsystem);
  42. }
  43. if (dev->has_irq) {
  44. monitor_printf(mon, " IRQ %" PRId64 ", pin %c\n",
  45. dev->irq, (char)('A' + dev->irq_pin - 1));
  46. }
  47. if (dev->pci_bridge) {
  48. monitor_printf(mon, " BUS %" PRId64 ".\n",
  49. dev->pci_bridge->bus->number);
  50. monitor_printf(mon, " secondary bus %" PRId64 ".\n",
  51. dev->pci_bridge->bus->secondary);
  52. monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
  53. dev->pci_bridge->bus->subordinate);
  54. monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
  55. dev->pci_bridge->bus->io_range->base,
  56. dev->pci_bridge->bus->io_range->limit);
  57. monitor_printf(mon,
  58. " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
  59. dev->pci_bridge->bus->memory_range->base,
  60. dev->pci_bridge->bus->memory_range->limit);
  61. monitor_printf(mon, " prefetchable memory range "
  62. "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
  63. dev->pci_bridge->bus->prefetchable_range->base,
  64. dev->pci_bridge->bus->prefetchable_range->limit);
  65. }
  66. for (region = dev->regions; region; region = region->next) {
  67. uint64_t addr, size;
  68. addr = region->value->address;
  69. size = region->value->size;
  70. monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
  71. if (!strcmp(region->value->type, "io")) {
  72. if (addr != PCI_BAR_UNMAPPED) {
  73. monitor_printf(mon, "I/O at 0x%04" PRIx64
  74. " [0x%04" PRIx64 "]\n",
  75. addr, addr + size - 1);
  76. } else {
  77. monitor_printf(mon, "I/O (not mapped)\n");
  78. }
  79. } else {
  80. if (addr != PCI_BAR_UNMAPPED) {
  81. monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
  82. " [0x%08" PRIx64 "]\n",
  83. region->value->mem_type_64 ? 64 : 32,
  84. region->value->prefetch ? " prefetchable" : "",
  85. addr, addr + size - 1);
  86. } else {
  87. monitor_printf(mon, "%d bit%s memory (not mapped)\n",
  88. region->value->mem_type_64 ? 64 : 32,
  89. region->value->prefetch ? " prefetchable" : "");
  90. }
  91. }
  92. }
  93. monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
  94. if (dev->pci_bridge) {
  95. if (dev->pci_bridge->has_devices) {
  96. PciDeviceInfoList *cdev;
  97. for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
  98. hmp_info_pci_device(mon, cdev->value);
  99. }
  100. }
  101. }
  102. }
  103. void hmp_info_pci(Monitor *mon, const QDict *qdict)
  104. {
  105. PciInfoList *info_list, *info;
  106. info_list = qmp_query_pci(&error_abort);
  107. for (info = info_list; info; info = info->next) {
  108. PciDeviceInfoList *dev;
  109. for (dev = info->value->devices; dev; dev = dev->next) {
  110. hmp_info_pci_device(mon, dev->value);
  111. }
  112. }
  113. qapi_free_PciInfoList(info_list);
  114. }
  115. void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent)
  116. {
  117. PCIDevice *d = (PCIDevice *)dev;
  118. int class = pci_get_word(d->config + PCI_CLASS_DEVICE);
  119. const pci_class_desc *desc = get_class_desc(class);
  120. char ctxt[64];
  121. PCIIORegion *r;
  122. int i;
  123. if (desc->desc) {
  124. snprintf(ctxt, sizeof(ctxt), "%s", desc->desc);
  125. } else {
  126. snprintf(ctxt, sizeof(ctxt), "Class %04x", class);
  127. }
  128. monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
  129. "pci id %04x:%04x (sub %04x:%04x)\n",
  130. indent, "", ctxt, pci_dev_bus_num(d),
  131. PCI_SLOT(d->devfn), PCI_FUNC(d->devfn),
  132. pci_get_word(d->config + PCI_VENDOR_ID),
  133. pci_get_word(d->config + PCI_DEVICE_ID),
  134. pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID),
  135. pci_get_word(d->config + PCI_SUBSYSTEM_ID));
  136. for (i = 0; i < PCI_NUM_REGIONS; i++) {
  137. r = &d->io_regions[i];
  138. if (!r->size) {
  139. continue;
  140. }
  141. monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
  142. " [0x%"FMT_PCIBUS"]\n",
  143. indent, "",
  144. i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem",
  145. r->addr, r->addr + r->size - 1);
  146. }
  147. }
  148. void hmp_pcie_aer_inject_error(Monitor *mon, const QDict *qdict)
  149. {
  150. Error *err = NULL;
  151. const char *id = qdict_get_str(qdict, "id");
  152. const char *error_name;
  153. uint32_t error_status;
  154. unsigned int num;
  155. bool correctable;
  156. PCIDevice *dev;
  157. PCIEAERErr aer_err;
  158. int ret;
  159. ret = pci_qdev_find_device(id, &dev);
  160. if (ret == -ENODEV) {
  161. error_setg(&err, "device '%s' not found", id);
  162. goto out;
  163. }
  164. if (ret < 0 || !pci_is_express(dev)) {
  165. error_setg(&err, "device '%s' is not a PCIe device", id);
  166. goto out;
  167. }
  168. error_name = qdict_get_str(qdict, "error_status");
  169. if (pcie_aer_parse_error_string(error_name, &error_status, &correctable)) {
  170. if (qemu_strtoui(error_name, NULL, 0, &num) < 0) {
  171. error_setg(&err, "invalid error status value '%s'", error_name);
  172. goto out;
  173. }
  174. error_status = num;
  175. correctable = qdict_get_try_bool(qdict, "correctable", false);
  176. } else {
  177. if (qdict_haskey(qdict, "correctable")) {
  178. error_setg(&err, "-c is only valid with numeric error status");
  179. goto out;
  180. }
  181. }
  182. aer_err.status = error_status;
  183. aer_err.source_id = pci_requester_id(dev);
  184. aer_err.flags = 0;
  185. if (correctable) {
  186. aer_err.flags |= PCIE_AER_ERR_IS_CORRECTABLE;
  187. }
  188. if (qdict_get_try_bool(qdict, "advisory_non_fatal", false)) {
  189. aer_err.flags |= PCIE_AER_ERR_MAYBE_ADVISORY;
  190. }
  191. if (qdict_haskey(qdict, "header0")) {
  192. aer_err.flags |= PCIE_AER_ERR_HEADER_VALID;
  193. }
  194. if (qdict_haskey(qdict, "prefix0")) {
  195. aer_err.flags |= PCIE_AER_ERR_TLP_PREFIX_PRESENT;
  196. }
  197. aer_err.header[0] = qdict_get_try_int(qdict, "header0", 0);
  198. aer_err.header[1] = qdict_get_try_int(qdict, "header1", 0);
  199. aer_err.header[2] = qdict_get_try_int(qdict, "header2", 0);
  200. aer_err.header[3] = qdict_get_try_int(qdict, "header3", 0);
  201. aer_err.prefix[0] = qdict_get_try_int(qdict, "prefix0", 0);
  202. aer_err.prefix[1] = qdict_get_try_int(qdict, "prefix1", 0);
  203. aer_err.prefix[2] = qdict_get_try_int(qdict, "prefix2", 0);
  204. aer_err.prefix[3] = qdict_get_try_int(qdict, "prefix3", 0);
  205. ret = pcie_aer_inject_error(dev, &aer_err);
  206. if (ret < 0) {
  207. error_setg_errno(&err, -ret, "failed to inject error");
  208. goto out;
  209. }
  210. monitor_printf(mon, "OK id: %s root bus: %s, bus: %x devfn: %x.%x\n",
  211. id, pci_root_bus_path(dev), pci_dev_bus_num(dev),
  212. PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
  213. out:
  214. hmp_handle_error(mon, err);
  215. }