xref: /qemu/hw/char/serial-pci-multi.c (revision 9fc7fc4d3909817555ce0af6bcb69dff1606140d)
1d09ecd8cSThomas Huth /*
2d09ecd8cSThomas Huth  * QEMU 16550A multi UART emulation
3d09ecd8cSThomas Huth  *
4d09ecd8cSThomas Huth  * SPDX-License-Identifier: MIT
5d09ecd8cSThomas Huth  *
6d09ecd8cSThomas Huth  * Copyright (c) 2003-2004 Fabrice Bellard
7d09ecd8cSThomas Huth  * Copyright (c) 2008 Citrix Systems, Inc.
8d09ecd8cSThomas Huth  *
9d09ecd8cSThomas Huth  * Permission is hereby granted, free of charge, to any person obtaining a copy
10d09ecd8cSThomas Huth  * of this software and associated documentation files (the "Software"), to deal
11d09ecd8cSThomas Huth  * in the Software without restriction, including without limitation the rights
12d09ecd8cSThomas Huth  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13d09ecd8cSThomas Huth  * copies of the Software, and to permit persons to whom the Software is
14d09ecd8cSThomas Huth  * furnished to do so, subject to the following conditions:
15d09ecd8cSThomas Huth  *
16d09ecd8cSThomas Huth  * The above copyright notice and this permission notice shall be included in
17d09ecd8cSThomas Huth  * all copies or substantial portions of the Software.
18d09ecd8cSThomas Huth  *
19d09ecd8cSThomas Huth  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20d09ecd8cSThomas Huth  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21d09ecd8cSThomas Huth  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22d09ecd8cSThomas Huth  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23d09ecd8cSThomas Huth  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24d09ecd8cSThomas Huth  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25d09ecd8cSThomas Huth  * THE SOFTWARE.
26d09ecd8cSThomas Huth  */
27d09ecd8cSThomas Huth 
28d09ecd8cSThomas Huth /* see docs/specs/pci-serial.txt */
29d09ecd8cSThomas Huth 
30d09ecd8cSThomas Huth #include "qemu/osdep.h"
31d09ecd8cSThomas Huth #include "qapi/error.h"
32d09ecd8cSThomas Huth #include "hw/char/serial.h"
3364552b6bSMarkus Armbruster #include "hw/irq.h"
34d09ecd8cSThomas Huth #include "hw/pci/pci.h"
35a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
36d6454270SMarkus Armbruster #include "migration/vmstate.h"
37d09ecd8cSThomas Huth 
38d09ecd8cSThomas Huth #define PCI_SERIAL_MAX_PORTS 4
39d09ecd8cSThomas Huth 
40d09ecd8cSThomas Huth typedef struct PCIMultiSerialState {
41d09ecd8cSThomas Huth     PCIDevice    dev;
42d09ecd8cSThomas Huth     MemoryRegion iobar;
43d09ecd8cSThomas Huth     uint32_t     ports;
44d09ecd8cSThomas Huth     char         *name[PCI_SERIAL_MAX_PORTS];
45d09ecd8cSThomas Huth     SerialState  state[PCI_SERIAL_MAX_PORTS];
46d09ecd8cSThomas Huth     uint32_t     level[PCI_SERIAL_MAX_PORTS];
47d09ecd8cSThomas Huth     qemu_irq     *irqs;
48d09ecd8cSThomas Huth     uint8_t      prog_if;
49d09ecd8cSThomas Huth } PCIMultiSerialState;
50d09ecd8cSThomas Huth 
51d09ecd8cSThomas Huth static void multi_serial_pci_exit(PCIDevice *dev)
52d09ecd8cSThomas Huth {
53d09ecd8cSThomas Huth     PCIMultiSerialState *pci = DO_UPCAST(PCIMultiSerialState, dev, dev);
54d09ecd8cSThomas Huth     SerialState *s;
55d09ecd8cSThomas Huth     int i;
56d09ecd8cSThomas Huth 
57d09ecd8cSThomas Huth     for (i = 0; i < pci->ports; i++) {
58d09ecd8cSThomas Huth         s = pci->state + i;
59981c3dcdSMarkus Armbruster         qdev_unrealize(DEVICE(s));
60d09ecd8cSThomas Huth         memory_region_del_subregion(&pci->iobar, &s->io);
61d09ecd8cSThomas Huth         g_free(pci->name[i]);
62d09ecd8cSThomas Huth     }
63d09ecd8cSThomas Huth     qemu_free_irqs(pci->irqs, pci->ports);
64d09ecd8cSThomas Huth }
65d09ecd8cSThomas Huth 
66d09ecd8cSThomas Huth static void multi_serial_irq_mux(void *opaque, int n, int level)
67d09ecd8cSThomas Huth {
68d09ecd8cSThomas Huth     PCIMultiSerialState *pci = opaque;
69d09ecd8cSThomas Huth     int i, pending = 0;
70d09ecd8cSThomas Huth 
71d09ecd8cSThomas Huth     pci->level[n] = level;
72d09ecd8cSThomas Huth     for (i = 0; i < pci->ports; i++) {
73d09ecd8cSThomas Huth         if (pci->level[i]) {
74d09ecd8cSThomas Huth             pending = 1;
75d09ecd8cSThomas Huth         }
76d09ecd8cSThomas Huth     }
77d09ecd8cSThomas Huth     pci_set_irq(&pci->dev, pending);
78d09ecd8cSThomas Huth }
79d09ecd8cSThomas Huth 
804305d482SMarc-André Lureau static size_t multi_serial_get_port_count(PCIDeviceClass *pc)
814305d482SMarc-André Lureau {
824305d482SMarc-André Lureau     switch (pc->device_id) {
834305d482SMarc-André Lureau     case 0x0003:
844305d482SMarc-André Lureau         return 2;
854305d482SMarc-André Lureau     case 0x0004:
864305d482SMarc-André Lureau         return 4;
874305d482SMarc-André Lureau     }
884305d482SMarc-André Lureau 
894305d482SMarc-André Lureau     g_assert_not_reached();
904305d482SMarc-André Lureau }
914305d482SMarc-André Lureau 
924305d482SMarc-André Lureau 
93d09ecd8cSThomas Huth static void multi_serial_pci_realize(PCIDevice *dev, Error **errp)
94d09ecd8cSThomas Huth {
95d09ecd8cSThomas Huth     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
96d09ecd8cSThomas Huth     PCIMultiSerialState *pci = DO_UPCAST(PCIMultiSerialState, dev, dev);
97d09ecd8cSThomas Huth     SerialState *s;
98d09ecd8cSThomas Huth     Error *err = NULL;
994305d482SMarc-André Lureau     size_t i, nports = multi_serial_get_port_count(pc);
100d09ecd8cSThomas Huth 
101d09ecd8cSThomas Huth     pci->dev.config[PCI_CLASS_PROG] = pci->prog_if;
102d09ecd8cSThomas Huth     pci->dev.config[PCI_INTERRUPT_PIN] = 0x01;
1034305d482SMarc-André Lureau     memory_region_init(&pci->iobar, OBJECT(pci), "multiserial", 8 * nports);
104d09ecd8cSThomas Huth     pci_register_bar(&pci->dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &pci->iobar);
1054305d482SMarc-André Lureau     pci->irqs = qemu_allocate_irqs(multi_serial_irq_mux, pci, nports);
106d09ecd8cSThomas Huth 
1074305d482SMarc-André Lureau     for (i = 0; i < nports; i++) {
108d09ecd8cSThomas Huth         s = pci->state + i;
109c9808d60SMarc-André Lureau         object_property_set_bool(OBJECT(s), true, "realized", &err);
110d09ecd8cSThomas Huth         if (err != NULL) {
111d09ecd8cSThomas Huth             error_propagate(errp, err);
112d09ecd8cSThomas Huth             multi_serial_pci_exit(dev);
113d09ecd8cSThomas Huth             return;
114d09ecd8cSThomas Huth         }
115d09ecd8cSThomas Huth         s->irq = pci->irqs[i];
1164305d482SMarc-André Lureau         pci->name[i] = g_strdup_printf("uart #%zu", i + 1);
117d09ecd8cSThomas Huth         memory_region_init_io(&s->io, OBJECT(pci), &serial_io_ops, s,
118d09ecd8cSThomas Huth                               pci->name[i], 8);
119d09ecd8cSThomas Huth         memory_region_add_subregion(&pci->iobar, 8 * i, &s->io);
120d09ecd8cSThomas Huth         pci->ports++;
121d09ecd8cSThomas Huth     }
122d09ecd8cSThomas Huth }
123d09ecd8cSThomas Huth 
124d09ecd8cSThomas Huth static const VMStateDescription vmstate_pci_multi_serial = {
125d09ecd8cSThomas Huth     .name = "pci-serial-multi",
126d09ecd8cSThomas Huth     .version_id = 1,
127d09ecd8cSThomas Huth     .minimum_version_id = 1,
128d09ecd8cSThomas Huth     .fields = (VMStateField[]) {
129d09ecd8cSThomas Huth         VMSTATE_PCI_DEVICE(dev, PCIMultiSerialState),
130d09ecd8cSThomas Huth         VMSTATE_STRUCT_ARRAY(state, PCIMultiSerialState, PCI_SERIAL_MAX_PORTS,
131d09ecd8cSThomas Huth                              0, vmstate_serial, SerialState),
132d09ecd8cSThomas Huth         VMSTATE_UINT32_ARRAY(level, PCIMultiSerialState, PCI_SERIAL_MAX_PORTS),
133d09ecd8cSThomas Huth         VMSTATE_END_OF_LIST()
134d09ecd8cSThomas Huth     }
135d09ecd8cSThomas Huth };
136d09ecd8cSThomas Huth 
137d09ecd8cSThomas Huth static Property multi_2x_serial_pci_properties[] = {
138d09ecd8cSThomas Huth     DEFINE_PROP_CHR("chardev1",  PCIMultiSerialState, state[0].chr),
139d09ecd8cSThomas Huth     DEFINE_PROP_CHR("chardev2",  PCIMultiSerialState, state[1].chr),
140d09ecd8cSThomas Huth     DEFINE_PROP_UINT8("prog_if",  PCIMultiSerialState, prog_if, 0x02),
141d09ecd8cSThomas Huth     DEFINE_PROP_END_OF_LIST(),
142d09ecd8cSThomas Huth };
143d09ecd8cSThomas Huth 
144d09ecd8cSThomas Huth static Property multi_4x_serial_pci_properties[] = {
145d09ecd8cSThomas Huth     DEFINE_PROP_CHR("chardev1",  PCIMultiSerialState, state[0].chr),
146d09ecd8cSThomas Huth     DEFINE_PROP_CHR("chardev2",  PCIMultiSerialState, state[1].chr),
147d09ecd8cSThomas Huth     DEFINE_PROP_CHR("chardev3",  PCIMultiSerialState, state[2].chr),
148d09ecd8cSThomas Huth     DEFINE_PROP_CHR("chardev4",  PCIMultiSerialState, state[3].chr),
149d09ecd8cSThomas Huth     DEFINE_PROP_UINT8("prog_if",  PCIMultiSerialState, prog_if, 0x02),
150d09ecd8cSThomas Huth     DEFINE_PROP_END_OF_LIST(),
151d09ecd8cSThomas Huth };
152d09ecd8cSThomas Huth 
153d09ecd8cSThomas Huth static void multi_2x_serial_pci_class_initfn(ObjectClass *klass, void *data)
154d09ecd8cSThomas Huth {
155d09ecd8cSThomas Huth     DeviceClass *dc = DEVICE_CLASS(klass);
156d09ecd8cSThomas Huth     PCIDeviceClass *pc = PCI_DEVICE_CLASS(klass);
157d09ecd8cSThomas Huth     pc->realize = multi_serial_pci_realize;
158d09ecd8cSThomas Huth     pc->exit = multi_serial_pci_exit;
159d09ecd8cSThomas Huth     pc->vendor_id = PCI_VENDOR_ID_REDHAT;
160d09ecd8cSThomas Huth     pc->device_id = PCI_DEVICE_ID_REDHAT_SERIAL2;
161d09ecd8cSThomas Huth     pc->revision = 1;
162d09ecd8cSThomas Huth     pc->class_id = PCI_CLASS_COMMUNICATION_SERIAL;
163d09ecd8cSThomas Huth     dc->vmsd = &vmstate_pci_multi_serial;
1644f67d30bSMarc-André Lureau     device_class_set_props(dc, multi_2x_serial_pci_properties);
165d09ecd8cSThomas Huth     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
166d09ecd8cSThomas Huth }
167d09ecd8cSThomas Huth 
168d09ecd8cSThomas Huth static void multi_4x_serial_pci_class_initfn(ObjectClass *klass, void *data)
169d09ecd8cSThomas Huth {
170d09ecd8cSThomas Huth     DeviceClass *dc = DEVICE_CLASS(klass);
171d09ecd8cSThomas Huth     PCIDeviceClass *pc = PCI_DEVICE_CLASS(klass);
172d09ecd8cSThomas Huth     pc->realize = multi_serial_pci_realize;
173d09ecd8cSThomas Huth     pc->exit = multi_serial_pci_exit;
174d09ecd8cSThomas Huth     pc->vendor_id = PCI_VENDOR_ID_REDHAT;
175d09ecd8cSThomas Huth     pc->device_id = PCI_DEVICE_ID_REDHAT_SERIAL4;
176d09ecd8cSThomas Huth     pc->revision = 1;
177d09ecd8cSThomas Huth     pc->class_id = PCI_CLASS_COMMUNICATION_SERIAL;
178d09ecd8cSThomas Huth     dc->vmsd = &vmstate_pci_multi_serial;
1794f67d30bSMarc-André Lureau     device_class_set_props(dc, multi_4x_serial_pci_properties);
180d09ecd8cSThomas Huth     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
181d09ecd8cSThomas Huth }
182d09ecd8cSThomas Huth 
1837781b88eSMarc-André Lureau static void multi_serial_init(Object *o)
1847781b88eSMarc-André Lureau {
1857781b88eSMarc-André Lureau     PCIDevice *dev = PCI_DEVICE(o);
1867781b88eSMarc-André Lureau     PCIMultiSerialState *pms = DO_UPCAST(PCIMultiSerialState, dev, dev);
1877781b88eSMarc-André Lureau     size_t i, nports = multi_serial_get_port_count(PCI_DEVICE_GET_CLASS(dev));
1887781b88eSMarc-André Lureau 
1897781b88eSMarc-André Lureau     for (i = 0; i < nports; i++) {
190*9fc7fc4dSMarkus Armbruster         object_initialize_child(o, "serial[*]", &pms->state[i], TYPE_SERIAL);
1917781b88eSMarc-André Lureau     }
1927781b88eSMarc-André Lureau }
1937781b88eSMarc-André Lureau 
194d09ecd8cSThomas Huth static const TypeInfo multi_2x_serial_pci_info = {
195d09ecd8cSThomas Huth     .name          = "pci-serial-2x",
196d09ecd8cSThomas Huth     .parent        = TYPE_PCI_DEVICE,
197d09ecd8cSThomas Huth     .instance_size = sizeof(PCIMultiSerialState),
1987781b88eSMarc-André Lureau     .instance_init = multi_serial_init,
199d09ecd8cSThomas Huth     .class_init    = multi_2x_serial_pci_class_initfn,
200d09ecd8cSThomas Huth     .interfaces = (InterfaceInfo[]) {
201d09ecd8cSThomas Huth         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
202d09ecd8cSThomas Huth         { },
203d09ecd8cSThomas Huth     },
204d09ecd8cSThomas Huth };
205d09ecd8cSThomas Huth 
206d09ecd8cSThomas Huth static const TypeInfo multi_4x_serial_pci_info = {
207d09ecd8cSThomas Huth     .name          = "pci-serial-4x",
208d09ecd8cSThomas Huth     .parent        = TYPE_PCI_DEVICE,
209d09ecd8cSThomas Huth     .instance_size = sizeof(PCIMultiSerialState),
2107781b88eSMarc-André Lureau     .instance_init = multi_serial_init,
211d09ecd8cSThomas Huth     .class_init    = multi_4x_serial_pci_class_initfn,
212d09ecd8cSThomas Huth     .interfaces = (InterfaceInfo[]) {
213d09ecd8cSThomas Huth         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
214d09ecd8cSThomas Huth         { },
215d09ecd8cSThomas Huth     },
216d09ecd8cSThomas Huth };
217d09ecd8cSThomas Huth 
218d09ecd8cSThomas Huth static void multi_serial_pci_register_types(void)
219d09ecd8cSThomas Huth {
220d09ecd8cSThomas Huth     type_register_static(&multi_2x_serial_pci_info);
221d09ecd8cSThomas Huth     type_register_static(&multi_4x_serial_pci_info);
222d09ecd8cSThomas Huth }
223d09ecd8cSThomas Huth 
224d09ecd8cSThomas Huth type_init(multi_serial_pci_register_types)
225