2
0

vga-pci.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * QEMU PCI VGA Emulator.
  3. *
  4. * see docs/specs/standard-vga.txt for virtual hardware specs.
  5. *
  6. * Copyright (c) 2003 Fabrice Bellard
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "hw.h"
  27. #include "ui/console.h"
  28. #include "pci/pci.h"
  29. #include "vga_int.h"
  30. #include "ui/pixel_ops.h"
  31. #include "qemu/timer.h"
  32. #include "loader.h"
  33. #define PCI_VGA_IOPORT_OFFSET 0x400
  34. #define PCI_VGA_IOPORT_SIZE (0x3e0 - 0x3c0)
  35. #define PCI_VGA_BOCHS_OFFSET 0x500
  36. #define PCI_VGA_BOCHS_SIZE (0x0b * 2)
  37. #define PCI_VGA_MMIO_SIZE 0x1000
  38. enum vga_pci_flags {
  39. PCI_VGA_FLAG_ENABLE_MMIO = 1,
  40. };
  41. typedef struct PCIVGAState {
  42. PCIDevice dev;
  43. VGACommonState vga;
  44. uint32_t flags;
  45. MemoryRegion mmio;
  46. MemoryRegion ioport;
  47. MemoryRegion bochs;
  48. } PCIVGAState;
  49. static const VMStateDescription vmstate_vga_pci = {
  50. .name = "vga",
  51. .version_id = 2,
  52. .minimum_version_id = 2,
  53. .minimum_version_id_old = 2,
  54. .fields = (VMStateField []) {
  55. VMSTATE_PCI_DEVICE(dev, PCIVGAState),
  56. VMSTATE_STRUCT(vga, PCIVGAState, 0, vmstate_vga_common, VGACommonState),
  57. VMSTATE_END_OF_LIST()
  58. }
  59. };
  60. static uint64_t pci_vga_ioport_read(void *ptr, hwaddr addr,
  61. unsigned size)
  62. {
  63. PCIVGAState *d = ptr;
  64. uint64_t ret = 0;
  65. switch (size) {
  66. case 1:
  67. ret = vga_ioport_read(&d->vga, addr);
  68. break;
  69. case 2:
  70. ret = vga_ioport_read(&d->vga, addr);
  71. ret |= vga_ioport_read(&d->vga, addr+1) << 8;
  72. break;
  73. }
  74. return ret;
  75. }
  76. static void pci_vga_ioport_write(void *ptr, hwaddr addr,
  77. uint64_t val, unsigned size)
  78. {
  79. PCIVGAState *d = ptr;
  80. switch (size) {
  81. case 1:
  82. vga_ioport_write(&d->vga, addr + 0x3c0, val);
  83. break;
  84. case 2:
  85. /*
  86. * Update bytes in little endian order. Allows to update
  87. * indexed registers with a single word write because the
  88. * index byte is updated first.
  89. */
  90. vga_ioport_write(&d->vga, addr + 0x3c0, val & 0xff);
  91. vga_ioport_write(&d->vga, addr + 0x3c1, (val >> 8) & 0xff);
  92. break;
  93. }
  94. }
  95. static const MemoryRegionOps pci_vga_ioport_ops = {
  96. .read = pci_vga_ioport_read,
  97. .write = pci_vga_ioport_write,
  98. .valid.min_access_size = 1,
  99. .valid.max_access_size = 4,
  100. .impl.min_access_size = 1,
  101. .impl.max_access_size = 2,
  102. .endianness = DEVICE_LITTLE_ENDIAN,
  103. };
  104. static uint64_t pci_vga_bochs_read(void *ptr, hwaddr addr,
  105. unsigned size)
  106. {
  107. PCIVGAState *d = ptr;
  108. int index = addr >> 1;
  109. vbe_ioport_write_index(&d->vga, 0, index);
  110. return vbe_ioport_read_data(&d->vga, 0);
  111. }
  112. static void pci_vga_bochs_write(void *ptr, hwaddr addr,
  113. uint64_t val, unsigned size)
  114. {
  115. PCIVGAState *d = ptr;
  116. int index = addr >> 1;
  117. vbe_ioport_write_index(&d->vga, 0, index);
  118. vbe_ioport_write_data(&d->vga, 0, val);
  119. }
  120. static const MemoryRegionOps pci_vga_bochs_ops = {
  121. .read = pci_vga_bochs_read,
  122. .write = pci_vga_bochs_write,
  123. .valid.min_access_size = 1,
  124. .valid.max_access_size = 4,
  125. .impl.min_access_size = 2,
  126. .impl.max_access_size = 2,
  127. .endianness = DEVICE_LITTLE_ENDIAN,
  128. };
  129. static int pci_std_vga_initfn(PCIDevice *dev)
  130. {
  131. PCIVGAState *d = DO_UPCAST(PCIVGAState, dev, dev);
  132. VGACommonState *s = &d->vga;
  133. /* vga + console init */
  134. vga_common_init(s);
  135. vga_init(s, pci_address_space(dev), pci_address_space_io(dev), true);
  136. s->ds = graphic_console_init(s->update, s->invalidate,
  137. s->screen_dump, s->text_update, s);
  138. /* XXX: VGA_RAM_SIZE must be a power of two */
  139. pci_register_bar(&d->dev, 0, PCI_BASE_ADDRESS_MEM_PREFETCH, &s->vram);
  140. /* mmio bar for vga register access */
  141. if (d->flags & (1 << PCI_VGA_FLAG_ENABLE_MMIO)) {
  142. memory_region_init(&d->mmio, "vga.mmio", 4096);
  143. memory_region_init_io(&d->ioport, &pci_vga_ioport_ops, d,
  144. "vga ioports remapped", PCI_VGA_IOPORT_SIZE);
  145. memory_region_init_io(&d->bochs, &pci_vga_bochs_ops, d,
  146. "bochs dispi interface", PCI_VGA_BOCHS_SIZE);
  147. memory_region_add_subregion(&d->mmio, PCI_VGA_IOPORT_OFFSET,
  148. &d->ioport);
  149. memory_region_add_subregion(&d->mmio, PCI_VGA_BOCHS_OFFSET,
  150. &d->bochs);
  151. pci_register_bar(&d->dev, 2, PCI_BASE_ADDRESS_SPACE_MEMORY, &d->mmio);
  152. }
  153. if (!dev->rom_bar) {
  154. /* compatibility with pc-0.13 and older */
  155. vga_init_vbe(s, pci_address_space(dev));
  156. }
  157. return 0;
  158. }
  159. static Property vga_pci_properties[] = {
  160. DEFINE_PROP_UINT32("vgamem_mb", PCIVGAState, vga.vram_size_mb, 16),
  161. DEFINE_PROP_BIT("mmio", PCIVGAState, flags, PCI_VGA_FLAG_ENABLE_MMIO, true),
  162. DEFINE_PROP_END_OF_LIST(),
  163. };
  164. static void vga_class_init(ObjectClass *klass, void *data)
  165. {
  166. DeviceClass *dc = DEVICE_CLASS(klass);
  167. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  168. k->no_hotplug = 1;
  169. k->init = pci_std_vga_initfn;
  170. k->romfile = "vgabios-stdvga.bin";
  171. k->vendor_id = PCI_VENDOR_ID_QEMU;
  172. k->device_id = PCI_DEVICE_ID_QEMU_VGA;
  173. k->class_id = PCI_CLASS_DISPLAY_VGA;
  174. dc->vmsd = &vmstate_vga_pci;
  175. dc->props = vga_pci_properties;
  176. }
  177. static const TypeInfo vga_info = {
  178. .name = "VGA",
  179. .parent = TYPE_PCI_DEVICE,
  180. .instance_size = sizeof(PCIVGAState),
  181. .class_init = vga_class_init,
  182. };
  183. static void vga_register_types(void)
  184. {
  185. type_register_static(&vga_info);
  186. }
  187. type_init(vga_register_types)