xref: /openbmc/qemu/hw/char/serial-pci.c (revision 8fa3b702)
1 /*
2  * QEMU 16550A UART emulation
3  *
4  * Copyright (c) 2003-2004 Fabrice Bellard
5  * Copyright (c) 2008 Citrix Systems, Inc.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  */
25 
26 /* see docs/specs/pci-serial.txt */
27 
28 #include "qemu/osdep.h"
29 #include "qapi/error.h"
30 #include "qemu/module.h"
31 #include "hw/char/serial.h"
32 #include "hw/irq.h"
33 #include "hw/pci/pci.h"
34 #include "hw/qdev-properties.h"
35 #include "migration/vmstate.h"
36 #include "qom/object.h"
37 
38 struct PCISerialState {
39     PCIDevice dev;
40     SerialState state;
41     uint8_t prog_if;
42 };
43 typedef struct PCISerialState PCISerialState;
44 
45 #define TYPE_PCI_SERIAL "pci-serial"
46 DECLARE_INSTANCE_CHECKER(PCISerialState, PCI_SERIAL,
47                          TYPE_PCI_SERIAL)
48 
49 static void serial_pci_realize(PCIDevice *dev, Error **errp)
50 {
51     PCISerialState *pci = DO_UPCAST(PCISerialState, dev, dev);
52     SerialState *s = &pci->state;
53 
54     if (!qdev_realize(DEVICE(s), NULL, errp)) {
55         return;
56     }
57 
58     pci->dev.config[PCI_CLASS_PROG] = pci->prog_if;
59     pci->dev.config[PCI_INTERRUPT_PIN] = 0x01;
60     s->irq = pci_allocate_irq(&pci->dev);
61 
62     memory_region_init_io(&s->io, OBJECT(pci), &serial_io_ops, s, "serial", 8);
63     pci_register_bar(&pci->dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &s->io);
64 }
65 
66 static void serial_pci_exit(PCIDevice *dev)
67 {
68     PCISerialState *pci = DO_UPCAST(PCISerialState, dev, dev);
69     SerialState *s = &pci->state;
70 
71     qdev_unrealize(DEVICE(s));
72     qemu_free_irq(s->irq);
73 }
74 
75 static const VMStateDescription vmstate_pci_serial = {
76     .name = "pci-serial",
77     .version_id = 1,
78     .minimum_version_id = 1,
79     .fields = (VMStateField[]) {
80         VMSTATE_PCI_DEVICE(dev, PCISerialState),
81         VMSTATE_STRUCT(state, PCISerialState, 0, vmstate_serial, SerialState),
82         VMSTATE_END_OF_LIST()
83     }
84 };
85 
86 static Property serial_pci_properties[] = {
87     DEFINE_PROP_CHR("chardev",  PCISerialState, state.chr),
88     DEFINE_PROP_UINT8("prog_if",  PCISerialState, prog_if, 0x02),
89     DEFINE_PROP_END_OF_LIST(),
90 };
91 
92 static void serial_pci_class_initfn(ObjectClass *klass, void *data)
93 {
94     DeviceClass *dc = DEVICE_CLASS(klass);
95     PCIDeviceClass *pc = PCI_DEVICE_CLASS(klass);
96     pc->realize = serial_pci_realize;
97     pc->exit = serial_pci_exit;
98     pc->vendor_id = PCI_VENDOR_ID_REDHAT;
99     pc->device_id = PCI_DEVICE_ID_REDHAT_SERIAL;
100     pc->revision = 1;
101     pc->class_id = PCI_CLASS_COMMUNICATION_SERIAL;
102     dc->vmsd = &vmstate_pci_serial;
103     device_class_set_props(dc, serial_pci_properties);
104     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
105 }
106 
107 static void serial_pci_init(Object *o)
108 {
109     PCISerialState *ps = PCI_SERIAL(o);
110 
111     object_initialize_child(o, "serial", &ps->state, TYPE_SERIAL);
112 }
113 
114 static const TypeInfo serial_pci_info = {
115     .name          = TYPE_PCI_SERIAL,
116     .parent        = TYPE_PCI_DEVICE,
117     .instance_size = sizeof(PCISerialState),
118     .instance_init = serial_pci_init,
119     .class_init    = serial_pci_class_initfn,
120     .interfaces = (InterfaceInfo[]) {
121         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
122         { },
123     },
124 };
125 
126 static void serial_pci_register_types(void)
127 {
128     type_register_static(&serial_pci_info);
129 }
130 
131 type_init(serial_pci_register_types)
132