2
0

serial-pci-multi.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * QEMU 16550A multi UART emulation
  3. *
  4. * SPDX-License-Identifier: MIT
  5. *
  6. * Copyright (c) 2003-2004 Fabrice Bellard
  7. * Copyright (c) 2008 Citrix Systems, Inc.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. */
  27. /* see docs/specs/pci-serial.rst */
  28. #include "qemu/osdep.h"
  29. #include "qapi/error.h"
  30. #include "hw/char/serial.h"
  31. #include "hw/irq.h"
  32. #include "hw/pci/pci_device.h"
  33. #include "hw/qdev-properties.h"
  34. #include "hw/qdev-properties-system.h"
  35. #include "migration/vmstate.h"
  36. #define PCI_SERIAL_MAX_PORTS 4
  37. typedef struct PCIMultiSerialState {
  38. PCIDevice dev;
  39. MemoryRegion iobar;
  40. uint32_t ports;
  41. char *name[PCI_SERIAL_MAX_PORTS];
  42. SerialState state[PCI_SERIAL_MAX_PORTS];
  43. uint32_t level[PCI_SERIAL_MAX_PORTS];
  44. IRQState irqs[PCI_SERIAL_MAX_PORTS];
  45. uint8_t prog_if;
  46. } PCIMultiSerialState;
  47. static void multi_serial_pci_exit(PCIDevice *dev)
  48. {
  49. PCIMultiSerialState *pci = DO_UPCAST(PCIMultiSerialState, dev, dev);
  50. SerialState *s;
  51. int i;
  52. for (i = 0; i < pci->ports; i++) {
  53. s = pci->state + i;
  54. qdev_unrealize(DEVICE(s));
  55. memory_region_del_subregion(&pci->iobar, &s->io);
  56. g_free(pci->name[i]);
  57. }
  58. }
  59. static void multi_serial_irq_mux(void *opaque, int n, int level)
  60. {
  61. PCIMultiSerialState *pci = opaque;
  62. int i, pending = 0;
  63. pci->level[n] = level;
  64. for (i = 0; i < pci->ports; i++) {
  65. if (pci->level[i]) {
  66. pending = 1;
  67. }
  68. }
  69. pci_set_irq(&pci->dev, pending);
  70. }
  71. static size_t multi_serial_get_port_count(PCIDeviceClass *pc)
  72. {
  73. switch (pc->device_id) {
  74. case 0x0003:
  75. return 2;
  76. case 0x0004:
  77. return 4;
  78. }
  79. g_assert_not_reached();
  80. }
  81. static void multi_serial_pci_realize(PCIDevice *dev, Error **errp)
  82. {
  83. PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
  84. PCIMultiSerialState *pci = DO_UPCAST(PCIMultiSerialState, dev, dev);
  85. SerialState *s;
  86. size_t i, nports = multi_serial_get_port_count(pc);
  87. pci->dev.config[PCI_CLASS_PROG] = pci->prog_if;
  88. pci->dev.config[PCI_INTERRUPT_PIN] = 0x01;
  89. memory_region_init(&pci->iobar, OBJECT(pci), "multiserial", 8 * nports);
  90. pci_register_bar(&pci->dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &pci->iobar);
  91. for (i = 0; i < nports; i++) {
  92. s = pci->state + i;
  93. if (!qdev_realize(DEVICE(s), NULL, errp)) {
  94. multi_serial_pci_exit(dev);
  95. return;
  96. }
  97. s->irq = &pci->irqs[i];
  98. pci->name[i] = g_strdup_printf("uart #%zu", i + 1);
  99. memory_region_init_io(&s->io, OBJECT(pci), &serial_io_ops, s,
  100. pci->name[i], 8);
  101. memory_region_add_subregion(&pci->iobar, 8 * i, &s->io);
  102. pci->ports++;
  103. }
  104. }
  105. static const VMStateDescription vmstate_pci_multi_serial = {
  106. .name = "pci-serial-multi",
  107. .version_id = 1,
  108. .minimum_version_id = 1,
  109. .fields = (const VMStateField[]) {
  110. VMSTATE_PCI_DEVICE(dev, PCIMultiSerialState),
  111. VMSTATE_STRUCT_ARRAY(state, PCIMultiSerialState, PCI_SERIAL_MAX_PORTS,
  112. 0, vmstate_serial, SerialState),
  113. VMSTATE_UINT32_ARRAY(level, PCIMultiSerialState, PCI_SERIAL_MAX_PORTS),
  114. VMSTATE_END_OF_LIST()
  115. }
  116. };
  117. static const Property multi_2x_serial_pci_properties[] = {
  118. DEFINE_PROP_CHR("chardev1", PCIMultiSerialState, state[0].chr),
  119. DEFINE_PROP_CHR("chardev2", PCIMultiSerialState, state[1].chr),
  120. DEFINE_PROP_UINT8("prog_if", PCIMultiSerialState, prog_if, 0x02),
  121. };
  122. static const Property multi_4x_serial_pci_properties[] = {
  123. DEFINE_PROP_CHR("chardev1", PCIMultiSerialState, state[0].chr),
  124. DEFINE_PROP_CHR("chardev2", PCIMultiSerialState, state[1].chr),
  125. DEFINE_PROP_CHR("chardev3", PCIMultiSerialState, state[2].chr),
  126. DEFINE_PROP_CHR("chardev4", PCIMultiSerialState, state[3].chr),
  127. DEFINE_PROP_UINT8("prog_if", PCIMultiSerialState, prog_if, 0x02),
  128. };
  129. static void multi_2x_serial_pci_class_initfn(ObjectClass *klass, void *data)
  130. {
  131. DeviceClass *dc = DEVICE_CLASS(klass);
  132. PCIDeviceClass *pc = PCI_DEVICE_CLASS(klass);
  133. pc->realize = multi_serial_pci_realize;
  134. pc->exit = multi_serial_pci_exit;
  135. pc->vendor_id = PCI_VENDOR_ID_REDHAT;
  136. pc->device_id = PCI_DEVICE_ID_REDHAT_SERIAL2;
  137. pc->revision = 1;
  138. pc->class_id = PCI_CLASS_COMMUNICATION_SERIAL;
  139. dc->vmsd = &vmstate_pci_multi_serial;
  140. device_class_set_props(dc, multi_2x_serial_pci_properties);
  141. set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
  142. }
  143. static void multi_4x_serial_pci_class_initfn(ObjectClass *klass, void *data)
  144. {
  145. DeviceClass *dc = DEVICE_CLASS(klass);
  146. PCIDeviceClass *pc = PCI_DEVICE_CLASS(klass);
  147. pc->realize = multi_serial_pci_realize;
  148. pc->exit = multi_serial_pci_exit;
  149. pc->vendor_id = PCI_VENDOR_ID_REDHAT;
  150. pc->device_id = PCI_DEVICE_ID_REDHAT_SERIAL4;
  151. pc->revision = 1;
  152. pc->class_id = PCI_CLASS_COMMUNICATION_SERIAL;
  153. dc->vmsd = &vmstate_pci_multi_serial;
  154. device_class_set_props(dc, multi_4x_serial_pci_properties);
  155. set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
  156. }
  157. static void multi_serial_init(Object *o)
  158. {
  159. PCIDevice *dev = PCI_DEVICE(o);
  160. PCIMultiSerialState *pms = DO_UPCAST(PCIMultiSerialState, dev, dev);
  161. size_t i, nports = multi_serial_get_port_count(PCI_DEVICE_GET_CLASS(dev));
  162. for (i = 0; i < nports; i++) {
  163. qemu_init_irq(&pms->irqs[i], multi_serial_irq_mux, pms, i);
  164. object_initialize_child(o, "serial[*]", &pms->state[i], TYPE_SERIAL);
  165. }
  166. }
  167. static const TypeInfo multi_2x_serial_pci_info = {
  168. .name = "pci-serial-2x",
  169. .parent = TYPE_PCI_DEVICE,
  170. .instance_size = sizeof(PCIMultiSerialState),
  171. .instance_init = multi_serial_init,
  172. .class_init = multi_2x_serial_pci_class_initfn,
  173. .interfaces = (InterfaceInfo[]) {
  174. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  175. { },
  176. },
  177. };
  178. static const TypeInfo multi_4x_serial_pci_info = {
  179. .name = "pci-serial-4x",
  180. .parent = TYPE_PCI_DEVICE,
  181. .instance_size = sizeof(PCIMultiSerialState),
  182. .instance_init = multi_serial_init,
  183. .class_init = multi_4x_serial_pci_class_initfn,
  184. .interfaces = (InterfaceInfo[]) {
  185. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  186. { },
  187. },
  188. };
  189. static void multi_serial_pci_register_types(void)
  190. {
  191. type_register_static(&multi_2x_serial_pci_info);
  192. type_register_static(&multi_4x_serial_pci_info);
  193. }
  194. type_init(multi_serial_pci_register_types)