xref: /openbmc/qemu/ui/util.c (revision 05caa062)
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 
18 #include "qemu/osdep.h"
19 
20 #include "hw/pci/pci_device.h"
21 #include "hw/pci/pci_bus.h"
22 #include "qapi/error.h"
23 #include "ui/console.h"
24 
25 /*
26  * Recursively (in reverse order) appends addresses of PCI devices as it moves
27  * up in the PCI hierarchy.
28  *
29  * @returns true on success, false when the buffer wasn't large enough
30  */
31 static bool append_pci_address(char *buf, size_t buf_size, const PCIDevice *pci)
32 {
33     PCIBus *bus = pci_get_bus(pci);
34     /*
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
37      */
38     if (bus->parent_dev != NULL) {
39         append_pci_address(buf, buf_size, bus->parent_dev);
40     }
41 
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));
45 
46     return written > 0 && written < buf_size - len;
47 }
48 
49 bool qemu_console_fill_device_address(QemuConsole *con,
50                                       char *device_address,
51                                       size_t size,
52                                       Error **errp)
53 {
54     DeviceState *dev = DEVICE(object_property_get_link(OBJECT(con),
55                                                        "device",
56                                                        &error_abort));
57     PCIDevice *pci = (PCIDevice *) object_dynamic_cast(OBJECT(dev),
58                                                        TYPE_PCI_DEVICE);
59 
60     if (pci == NULL) {
61         error_setg(errp, "Setting device address of a display device: "
62                    "Not a PCI device.");
63         return false;
64     }
65 
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.");
70         return false;
71     }
72 
73     return true;
74 }
75