2 * Copyright (C) 2021 Red Hat, Inc.
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.
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.
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/>.
18 #include "qemu/osdep.h"
20 #include "hw/pci/pci_device.h"
21 #include "hw/pci/pci_bus.h"
22 #include "qapi/error.h"
23 #include "ui/console.h"
26 * Recursively (in reverse order) appends addresses of PCI devices as it moves
27 * up in the PCI hierarchy.
29 * @returns true on success, false when the buffer wasn't large enough
31 static bool append_pci_address(char *buf
, size_t buf_size
, const PCIDevice
*pci
)
33 PCIBus
*bus
= pci_get_bus(pci
);
35 * equivalent to if (!pci_bus_is_root(bus)), but the function is not built
36 * with PCI_CONFIG=n, avoid using an #ifdef by checking directly
38 if (bus
->parent_dev
!= NULL
) {
39 append_pci_address(buf
, buf_size
, bus
->parent_dev
);
42 size_t len
= strlen(buf
);
43 ssize_t written
= snprintf(buf
+ len
, buf_size
- len
, "/%02x.%x",
44 PCI_SLOT(pci
->devfn
), PCI_FUNC(pci
->devfn
));
46 return written
> 0 && written
< buf_size
- len
;
49 bool qemu_console_fill_device_address(QemuConsole
*con
,
54 DeviceState
*dev
= DEVICE(object_property_get_link(OBJECT(con
),
57 PCIDevice
*pci
= (PCIDevice
*) object_dynamic_cast(OBJECT(dev
),
61 error_setg(errp
, "Setting device address of a display device: "
66 strncpy(device_address
, "pci/0000", size
);
67 if (!append_pci_address(device_address
, size
, pci
)) {
68 error_setg(errp
, "Setting device address of a display device: "
69 "Too many PCI devices in the chain.");