vga-pci.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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/hw.h"
  27. #include "ui/console.h"
  28. #include "hw/pci/pci.h"
  29. #include "vga_int.h"
  30. #include "ui/pixel_ops.h"
  31. #include "qemu/timer.h"
  32. #include "hw/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. .fields = (VMStateField[]) {
  54. VMSTATE_PCI_DEVICE(dev, PCIVGAState),
  55. VMSTATE_STRUCT(vga, PCIVGAState, 0, vmstate_vga_common, VGACommonState),
  56. VMSTATE_END_OF_LIST()
  57. }
  58. };
  59. static uint64_t pci_vga_ioport_read(void *ptr, hwaddr addr,
  60. unsigned size)
  61. {
  62. PCIVGAState *d = ptr;
  63. uint64_t ret = 0;
  64. switch (size) {
  65. case 1:
  66. ret = vga_ioport_read(&d->vga, addr);
  67. break;
  68. case 2:
  69. ret = vga_ioport_read(&d->vga, addr);
  70. ret |= vga_ioport_read(&d->vga, addr+1) << 8;
  71. break;
  72. }
  73. return ret;
  74. }
  75. static void pci_vga_ioport_write(void *ptr, hwaddr addr,
  76. uint64_t val, unsigned size)
  77. {
  78. PCIVGAState *d = ptr;
  79. switch (size) {
  80. case 1:
  81. vga_ioport_write(&d->vga, addr + 0x3c0, val);
  82. break;
  83. case 2:
  84. /*
  85. * Update bytes in little endian order. Allows to update
  86. * indexed registers with a single word write because the
  87. * index byte is updated first.
  88. */
  89. vga_ioport_write(&d->vga, addr + 0x3c0, val & 0xff);
  90. vga_ioport_write(&d->vga, addr + 0x3c1, (val >> 8) & 0xff);
  91. break;
  92. }
  93. }
  94. static const MemoryRegionOps pci_vga_ioport_ops = {
  95. .read = pci_vga_ioport_read,
  96. .write = pci_vga_ioport_write,
  97. .valid.min_access_size = 1,
  98. .valid.max_access_size = 4,
  99. .impl.min_access_size = 1,
  100. .impl.max_access_size = 2,
  101. .endianness = DEVICE_LITTLE_ENDIAN,
  102. };
  103. static uint64_t pci_vga_bochs_read(void *ptr, hwaddr addr,
  104. unsigned size)
  105. {
  106. PCIVGAState *d = ptr;
  107. int index = addr >> 1;
  108. vbe_ioport_write_index(&d->vga, 0, index);
  109. return vbe_ioport_read_data(&d->vga, 0);
  110. }
  111. static void pci_vga_bochs_write(void *ptr, hwaddr addr,
  112. uint64_t val, unsigned size)
  113. {
  114. PCIVGAState *d = ptr;
  115. int index = addr >> 1;
  116. vbe_ioport_write_index(&d->vga, 0, index);
  117. vbe_ioport_write_data(&d->vga, 0, val);
  118. }
  119. static const MemoryRegionOps pci_vga_bochs_ops = {
  120. .read = pci_vga_bochs_read,
  121. .write = pci_vga_bochs_write,
  122. .valid.min_access_size = 1,
  123. .valid.max_access_size = 4,
  124. .impl.min_access_size = 2,
  125. .impl.max_access_size = 2,
  126. .endianness = DEVICE_LITTLE_ENDIAN,
  127. };
  128. static int pci_std_vga_initfn(PCIDevice *dev)
  129. {
  130. PCIVGAState *d = DO_UPCAST(PCIVGAState, dev, dev);
  131. VGACommonState *s = &d->vga;
  132. /* vga + console init */
  133. vga_common_init(s, OBJECT(dev), true);
  134. vga_init(s, OBJECT(dev), pci_address_space(dev), pci_address_space_io(dev),
  135. true);
  136. s->con = graphic_console_init(DEVICE(dev), 0, s->hw_ops, s);
  137. /* XXX: VGA_RAM_SIZE must be a power of two */
  138. pci_register_bar(&d->dev, 0, PCI_BASE_ADDRESS_MEM_PREFETCH, &s->vram);
  139. /* mmio bar for vga register access */
  140. if (d->flags & (1 << PCI_VGA_FLAG_ENABLE_MMIO)) {
  141. memory_region_init(&d->mmio, NULL, "vga.mmio", 4096);
  142. memory_region_init_io(&d->ioport, NULL, &pci_vga_ioport_ops, d,
  143. "vga ioports remapped", PCI_VGA_IOPORT_SIZE);
  144. memory_region_init_io(&d->bochs, NULL, &pci_vga_bochs_ops, d,
  145. "bochs dispi interface", PCI_VGA_BOCHS_SIZE);
  146. memory_region_add_subregion(&d->mmio, PCI_VGA_IOPORT_OFFSET,
  147. &d->ioport);
  148. memory_region_add_subregion(&d->mmio, PCI_VGA_BOCHS_OFFSET,
  149. &d->bochs);
  150. pci_register_bar(&d->dev, 2, PCI_BASE_ADDRESS_SPACE_MEMORY, &d->mmio);
  151. }
  152. if (!dev->rom_bar) {
  153. /* compatibility with pc-0.13 and older */
  154. vga_init_vbe(s, OBJECT(dev), pci_address_space(dev));
  155. }
  156. return 0;
  157. }
  158. static int pci_secondary_vga_initfn(PCIDevice *dev)
  159. {
  160. PCIVGAState *d = DO_UPCAST(PCIVGAState, dev, dev);
  161. VGACommonState *s = &d->vga;
  162. /* vga + console init */
  163. vga_common_init(s, OBJECT(dev), false);
  164. s->con = graphic_console_init(DEVICE(dev), 0, s->hw_ops, s);
  165. /* mmio bar */
  166. memory_region_init(&d->mmio, OBJECT(dev), "vga.mmio", 4096);
  167. memory_region_init_io(&d->ioport, OBJECT(dev), &pci_vga_ioport_ops, d,
  168. "vga ioports remapped", PCI_VGA_IOPORT_SIZE);
  169. memory_region_init_io(&d->bochs, OBJECT(dev), &pci_vga_bochs_ops, d,
  170. "bochs dispi interface", PCI_VGA_BOCHS_SIZE);
  171. memory_region_add_subregion(&d->mmio, PCI_VGA_IOPORT_OFFSET,
  172. &d->ioport);
  173. memory_region_add_subregion(&d->mmio, PCI_VGA_BOCHS_OFFSET,
  174. &d->bochs);
  175. pci_register_bar(&d->dev, 0, PCI_BASE_ADDRESS_MEM_PREFETCH, &s->vram);
  176. pci_register_bar(&d->dev, 2, PCI_BASE_ADDRESS_SPACE_MEMORY, &d->mmio);
  177. return 0;
  178. }
  179. static void pci_secondary_vga_reset(DeviceState *dev)
  180. {
  181. PCIVGAState *d = DO_UPCAST(PCIVGAState, dev.qdev, dev);
  182. vga_common_reset(&d->vga);
  183. }
  184. static Property vga_pci_properties[] = {
  185. DEFINE_PROP_UINT32("vgamem_mb", PCIVGAState, vga.vram_size_mb, 16),
  186. DEFINE_PROP_BIT("mmio", PCIVGAState, flags, PCI_VGA_FLAG_ENABLE_MMIO, true),
  187. DEFINE_PROP_END_OF_LIST(),
  188. };
  189. static Property secondary_pci_properties[] = {
  190. DEFINE_PROP_UINT32("vgamem_mb", PCIVGAState, vga.vram_size_mb, 16),
  191. DEFINE_PROP_END_OF_LIST(),
  192. };
  193. static void vga_class_init(ObjectClass *klass, void *data)
  194. {
  195. DeviceClass *dc = DEVICE_CLASS(klass);
  196. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  197. k->init = pci_std_vga_initfn;
  198. k->romfile = "vgabios-stdvga.bin";
  199. k->vendor_id = PCI_VENDOR_ID_QEMU;
  200. k->device_id = PCI_DEVICE_ID_QEMU_VGA;
  201. k->class_id = PCI_CLASS_DISPLAY_VGA;
  202. dc->vmsd = &vmstate_vga_pci;
  203. dc->props = vga_pci_properties;
  204. dc->hotpluggable = false;
  205. set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
  206. }
  207. static void secondary_class_init(ObjectClass *klass, void *data)
  208. {
  209. DeviceClass *dc = DEVICE_CLASS(klass);
  210. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  211. k->init = pci_secondary_vga_initfn;
  212. k->vendor_id = PCI_VENDOR_ID_QEMU;
  213. k->device_id = PCI_DEVICE_ID_QEMU_VGA;
  214. k->class_id = PCI_CLASS_DISPLAY_OTHER;
  215. dc->vmsd = &vmstate_vga_pci;
  216. dc->props = secondary_pci_properties;
  217. dc->reset = pci_secondary_vga_reset;
  218. }
  219. static const TypeInfo vga_info = {
  220. .name = "VGA",
  221. .parent = TYPE_PCI_DEVICE,
  222. .instance_size = sizeof(PCIVGAState),
  223. .class_init = vga_class_init,
  224. };
  225. static const TypeInfo secondary_info = {
  226. .name = "secondary-vga",
  227. .parent = TYPE_PCI_DEVICE,
  228. .instance_size = sizeof(PCIVGAState),
  229. .class_init = secondary_class_init,
  230. };
  231. static void vga_register_types(void)
  232. {
  233. type_register_static(&vga_info);
  234. type_register_static(&secondary_info);
  235. }
  236. type_init(vga_register_types)