util.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (C) 2021 Red Hat, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 or
  7. * (at your option) version 3 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "qemu/osdep.h"
  18. #include "hw/pci/pci_device.h"
  19. #include "hw/pci/pci_bus.h"
  20. #include "qapi/error.h"
  21. #include "ui/console.h"
  22. /*
  23. * Recursively (in reverse order) appends addresses of PCI devices as it moves
  24. * up in the PCI hierarchy.
  25. *
  26. * @returns true on success, false when the buffer wasn't large enough
  27. */
  28. static bool append_pci_address(char *buf, size_t buf_size, const PCIDevice *pci)
  29. {
  30. PCIBus *bus = pci_get_bus(pci);
  31. /*
  32. * equivalent to if (!pci_bus_is_root(bus)), but the function is not built
  33. * with PCI_CONFIG=n, avoid using an #ifdef by checking directly
  34. */
  35. if (bus->parent_dev != NULL) {
  36. append_pci_address(buf, buf_size, bus->parent_dev);
  37. }
  38. size_t len = strlen(buf);
  39. ssize_t written = snprintf(buf + len, buf_size - len, "/%02x.%x",
  40. PCI_SLOT(pci->devfn), PCI_FUNC(pci->devfn));
  41. return written > 0 && written < buf_size - len;
  42. }
  43. bool qemu_console_fill_device_address(QemuConsole *con,
  44. char *device_address,
  45. size_t size,
  46. Error **errp)
  47. {
  48. DeviceState *dev = DEVICE(object_property_get_link(OBJECT(con),
  49. "device",
  50. &error_abort));
  51. PCIDevice *pci = (PCIDevice *) object_dynamic_cast(OBJECT(dev),
  52. TYPE_PCI_DEVICE);
  53. if (pci == NULL) {
  54. error_setg(errp, "Setting device address of a display device: "
  55. "Not a PCI device.");
  56. return false;
  57. }
  58. strncpy(device_address, "pci/0000", size);
  59. if (!append_pci_address(device_address, size, pci)) {
  60. error_setg(errp, "Setting device address of a display device: "
  61. "Too many PCI devices in the chain.");
  62. return false;
  63. }
  64. return true;
  65. }