xref: /qemu/hw/char/serial-pci-multi.c (revision 4ef6d66547cbd1331628530dfe03620bf41e3fae)
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 
283669b594SPeter Maydell /* see docs/specs/pci-serial.rst */
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"
34edf5ca5dSMarkus Armbruster #include "hw/pci/pci_device.h"
35a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
36ce35e229SEduardo Habkost #include "hw/qdev-properties-system.h"
37d6454270SMarkus Armbruster #include "migration/vmstate.h"
38d09ecd8cSThomas Huth 
39d09ecd8cSThomas Huth #define PCI_SERIAL_MAX_PORTS 4
40d09ecd8cSThomas Huth 
41d09ecd8cSThomas Huth typedef struct PCIMultiSerialState {
42d09ecd8cSThomas Huth     PCIDevice    dev;
43d09ecd8cSThomas Huth     MemoryRegion iobar;
44d09ecd8cSThomas Huth     uint32_t     ports;
45d09ecd8cSThomas Huth     char         *name[PCI_SERIAL_MAX_PORTS];
46d09ecd8cSThomas Huth     SerialState  state[PCI_SERIAL_MAX_PORTS];
47d09ecd8cSThomas Huth     uint32_t     level[PCI_SERIAL_MAX_PORTS];
48*4ef6d665SPhilippe Mathieu-Daudé     IRQState     irqs[PCI_SERIAL_MAX_PORTS];
49d09ecd8cSThomas Huth     uint8_t      prog_if;
50d09ecd8cSThomas Huth } PCIMultiSerialState;
51d09ecd8cSThomas Huth 
52d09ecd8cSThomas Huth static void multi_serial_pci_exit(PCIDevice *dev)
53d09ecd8cSThomas Huth {
54d09ecd8cSThomas Huth     PCIMultiSerialState *pci = DO_UPCAST(PCIMultiSerialState, dev, dev);
55d09ecd8cSThomas Huth     SerialState *s;
56d09ecd8cSThomas Huth     int i;
57d09ecd8cSThomas Huth 
58d09ecd8cSThomas Huth     for (i = 0; i < pci->ports; i++) {
59d09ecd8cSThomas Huth         s = pci->state + i;
60981c3dcdSMarkus Armbruster         qdev_unrealize(DEVICE(s));
61d09ecd8cSThomas Huth         memory_region_del_subregion(&pci->iobar, &s->io);
62d09ecd8cSThomas Huth         g_free(pci->name[i]);
63d09ecd8cSThomas Huth     }
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;
984305d482SMarc-André Lureau     size_t i, nports = multi_serial_get_port_count(pc);
99d09ecd8cSThomas Huth 
100d09ecd8cSThomas Huth     pci->dev.config[PCI_CLASS_PROG] = pci->prog_if;
101d09ecd8cSThomas Huth     pci->dev.config[PCI_INTERRUPT_PIN] = 0x01;
1024305d482SMarc-André Lureau     memory_region_init(&pci->iobar, OBJECT(pci), "multiserial", 8 * nports);
103d09ecd8cSThomas Huth     pci_register_bar(&pci->dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &pci->iobar);
104d09ecd8cSThomas Huth 
1054305d482SMarc-André Lureau     for (i = 0; i < nports; i++) {
106d09ecd8cSThomas Huth         s = pci->state + i;
107668f62ecSMarkus Armbruster         if (!qdev_realize(DEVICE(s), NULL, errp)) {
108d09ecd8cSThomas Huth             multi_serial_pci_exit(dev);
109d09ecd8cSThomas Huth             return;
110d09ecd8cSThomas Huth         }
111*4ef6d665SPhilippe Mathieu-Daudé         s->irq = &pci->irqs[i];
1124305d482SMarc-André Lureau         pci->name[i] = g_strdup_printf("uart #%zu", i + 1);
113d09ecd8cSThomas Huth         memory_region_init_io(&s->io, OBJECT(pci), &serial_io_ops, s,
114d09ecd8cSThomas Huth                               pci->name[i], 8);
115d09ecd8cSThomas Huth         memory_region_add_subregion(&pci->iobar, 8 * i, &s->io);
116d09ecd8cSThomas Huth         pci->ports++;
117d09ecd8cSThomas Huth     }
118d09ecd8cSThomas Huth }
119d09ecd8cSThomas Huth 
120d09ecd8cSThomas Huth static const VMStateDescription vmstate_pci_multi_serial = {
121d09ecd8cSThomas Huth     .name = "pci-serial-multi",
122d09ecd8cSThomas Huth     .version_id = 1,
123d09ecd8cSThomas Huth     .minimum_version_id = 1,
1242f6cab05SRichard Henderson     .fields = (const VMStateField[]) {
125d09ecd8cSThomas Huth         VMSTATE_PCI_DEVICE(dev, PCIMultiSerialState),
126d09ecd8cSThomas Huth         VMSTATE_STRUCT_ARRAY(state, PCIMultiSerialState, PCI_SERIAL_MAX_PORTS,
127d09ecd8cSThomas Huth                              0, vmstate_serial, SerialState),
128d09ecd8cSThomas Huth         VMSTATE_UINT32_ARRAY(level, PCIMultiSerialState, PCI_SERIAL_MAX_PORTS),
129d09ecd8cSThomas Huth         VMSTATE_END_OF_LIST()
130d09ecd8cSThomas Huth     }
131d09ecd8cSThomas Huth };
132d09ecd8cSThomas Huth 
133312f37d1SRichard Henderson static const Property multi_2x_serial_pci_properties[] = {
134d09ecd8cSThomas Huth     DEFINE_PROP_CHR("chardev1",  PCIMultiSerialState, state[0].chr),
135d09ecd8cSThomas Huth     DEFINE_PROP_CHR("chardev2",  PCIMultiSerialState, state[1].chr),
136d09ecd8cSThomas Huth     DEFINE_PROP_UINT8("prog_if",  PCIMultiSerialState, prog_if, 0x02),
137d09ecd8cSThomas Huth };
138d09ecd8cSThomas Huth 
139312f37d1SRichard Henderson static const Property multi_4x_serial_pci_properties[] = {
140d09ecd8cSThomas Huth     DEFINE_PROP_CHR("chardev1",  PCIMultiSerialState, state[0].chr),
141d09ecd8cSThomas Huth     DEFINE_PROP_CHR("chardev2",  PCIMultiSerialState, state[1].chr),
142d09ecd8cSThomas Huth     DEFINE_PROP_CHR("chardev3",  PCIMultiSerialState, state[2].chr),
143d09ecd8cSThomas Huth     DEFINE_PROP_CHR("chardev4",  PCIMultiSerialState, state[3].chr),
144d09ecd8cSThomas Huth     DEFINE_PROP_UINT8("prog_if",  PCIMultiSerialState, prog_if, 0x02),
145d09ecd8cSThomas Huth };
146d09ecd8cSThomas Huth 
147d09ecd8cSThomas Huth static void multi_2x_serial_pci_class_initfn(ObjectClass *klass, void *data)
148d09ecd8cSThomas Huth {
149d09ecd8cSThomas Huth     DeviceClass *dc = DEVICE_CLASS(klass);
150d09ecd8cSThomas Huth     PCIDeviceClass *pc = PCI_DEVICE_CLASS(klass);
151d09ecd8cSThomas Huth     pc->realize = multi_serial_pci_realize;
152d09ecd8cSThomas Huth     pc->exit = multi_serial_pci_exit;
153d09ecd8cSThomas Huth     pc->vendor_id = PCI_VENDOR_ID_REDHAT;
154d09ecd8cSThomas Huth     pc->device_id = PCI_DEVICE_ID_REDHAT_SERIAL2;
155d09ecd8cSThomas Huth     pc->revision = 1;
156d09ecd8cSThomas Huth     pc->class_id = PCI_CLASS_COMMUNICATION_SERIAL;
157d09ecd8cSThomas Huth     dc->vmsd = &vmstate_pci_multi_serial;
1584f67d30bSMarc-André Lureau     device_class_set_props(dc, multi_2x_serial_pci_properties);
159d09ecd8cSThomas Huth     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
160d09ecd8cSThomas Huth }
161d09ecd8cSThomas Huth 
162d09ecd8cSThomas Huth static void multi_4x_serial_pci_class_initfn(ObjectClass *klass, void *data)
163d09ecd8cSThomas Huth {
164d09ecd8cSThomas Huth     DeviceClass *dc = DEVICE_CLASS(klass);
165d09ecd8cSThomas Huth     PCIDeviceClass *pc = PCI_DEVICE_CLASS(klass);
166d09ecd8cSThomas Huth     pc->realize = multi_serial_pci_realize;
167d09ecd8cSThomas Huth     pc->exit = multi_serial_pci_exit;
168d09ecd8cSThomas Huth     pc->vendor_id = PCI_VENDOR_ID_REDHAT;
169d09ecd8cSThomas Huth     pc->device_id = PCI_DEVICE_ID_REDHAT_SERIAL4;
170d09ecd8cSThomas Huth     pc->revision = 1;
171d09ecd8cSThomas Huth     pc->class_id = PCI_CLASS_COMMUNICATION_SERIAL;
172d09ecd8cSThomas Huth     dc->vmsd = &vmstate_pci_multi_serial;
1734f67d30bSMarc-André Lureau     device_class_set_props(dc, multi_4x_serial_pci_properties);
174d09ecd8cSThomas Huth     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
175d09ecd8cSThomas Huth }
176d09ecd8cSThomas Huth 
1777781b88eSMarc-André Lureau static void multi_serial_init(Object *o)
1787781b88eSMarc-André Lureau {
1797781b88eSMarc-André Lureau     PCIDevice *dev = PCI_DEVICE(o);
1807781b88eSMarc-André Lureau     PCIMultiSerialState *pms = DO_UPCAST(PCIMultiSerialState, dev, dev);
1817781b88eSMarc-André Lureau     size_t i, nports = multi_serial_get_port_count(PCI_DEVICE_GET_CLASS(dev));
1827781b88eSMarc-André Lureau 
1837781b88eSMarc-André Lureau     for (i = 0; i < nports; i++) {
184*4ef6d665SPhilippe Mathieu-Daudé         qemu_init_irq(&pms->irqs[i], multi_serial_irq_mux, pms, i);
1859fc7fc4dSMarkus Armbruster         object_initialize_child(o, "serial[*]", &pms->state[i], TYPE_SERIAL);
1867781b88eSMarc-André Lureau     }
1877781b88eSMarc-André Lureau }
1887781b88eSMarc-André Lureau 
189d09ecd8cSThomas Huth static const TypeInfo multi_2x_serial_pci_info = {
190d09ecd8cSThomas Huth     .name          = "pci-serial-2x",
191d09ecd8cSThomas Huth     .parent        = TYPE_PCI_DEVICE,
192d09ecd8cSThomas Huth     .instance_size = sizeof(PCIMultiSerialState),
1937781b88eSMarc-André Lureau     .instance_init = multi_serial_init,
194d09ecd8cSThomas Huth     .class_init    = multi_2x_serial_pci_class_initfn,
195d09ecd8cSThomas Huth     .interfaces = (InterfaceInfo[]) {
196d09ecd8cSThomas Huth         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
197d09ecd8cSThomas Huth         { },
198d09ecd8cSThomas Huth     },
199d09ecd8cSThomas Huth };
200d09ecd8cSThomas Huth 
201d09ecd8cSThomas Huth static const TypeInfo multi_4x_serial_pci_info = {
202d09ecd8cSThomas Huth     .name          = "pci-serial-4x",
203d09ecd8cSThomas Huth     .parent        = TYPE_PCI_DEVICE,
204d09ecd8cSThomas Huth     .instance_size = sizeof(PCIMultiSerialState),
2057781b88eSMarc-André Lureau     .instance_init = multi_serial_init,
206d09ecd8cSThomas Huth     .class_init    = multi_4x_serial_pci_class_initfn,
207d09ecd8cSThomas Huth     .interfaces = (InterfaceInfo[]) {
208d09ecd8cSThomas Huth         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
209d09ecd8cSThomas Huth         { },
210d09ecd8cSThomas Huth     },
211d09ecd8cSThomas Huth };
212d09ecd8cSThomas Huth 
213d09ecd8cSThomas Huth static void multi_serial_pci_register_types(void)
214d09ecd8cSThomas Huth {
215d09ecd8cSThomas Huth     type_register_static(&multi_2x_serial_pci_info);
216d09ecd8cSThomas Huth     type_register_static(&multi_4x_serial_pci_info);
217d09ecd8cSThomas Huth }
218d09ecd8cSThomas Huth 
219d09ecd8cSThomas Huth type_init(multi_serial_pci_register_types)
220