vga-pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 "qemu/osdep.h"
  27. #include "hw/hw.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. enum vga_pci_flags {
  34. PCI_VGA_FLAG_ENABLE_MMIO = 1,
  35. PCI_VGA_FLAG_ENABLE_QEXT = 2,
  36. };
  37. typedef struct PCIVGAState {
  38. PCIDevice dev;
  39. VGACommonState vga;
  40. uint32_t flags;
  41. MemoryRegion mmio;
  42. MemoryRegion mrs[3];
  43. } PCIVGAState;
  44. #define TYPE_PCI_VGA "pci-vga"
  45. #define PCI_VGA(obj) OBJECT_CHECK(PCIVGAState, (obj), TYPE_PCI_VGA)
  46. static const VMStateDescription vmstate_vga_pci = {
  47. .name = "vga",
  48. .version_id = 2,
  49. .minimum_version_id = 2,
  50. .fields = (VMStateField[]) {
  51. VMSTATE_PCI_DEVICE(dev, PCIVGAState),
  52. VMSTATE_STRUCT(vga, PCIVGAState, 0, vmstate_vga_common, VGACommonState),
  53. VMSTATE_END_OF_LIST()
  54. }
  55. };
  56. static uint64_t pci_vga_ioport_read(void *ptr, hwaddr addr,
  57. unsigned size)
  58. {
  59. VGACommonState *s = ptr;
  60. uint64_t ret = 0;
  61. switch (size) {
  62. case 1:
  63. ret = vga_ioport_read(s, addr + 0x3c0);
  64. break;
  65. case 2:
  66. ret = vga_ioport_read(s, addr + 0x3c0);
  67. ret |= vga_ioport_read(s, addr + 0x3c1) << 8;
  68. break;
  69. }
  70. return ret;
  71. }
  72. static void pci_vga_ioport_write(void *ptr, hwaddr addr,
  73. uint64_t val, unsigned size)
  74. {
  75. VGACommonState *s = ptr;
  76. switch (size) {
  77. case 1:
  78. vga_ioport_write(s, addr + 0x3c0, val);
  79. break;
  80. case 2:
  81. /*
  82. * Update bytes in little endian order. Allows to update
  83. * indexed registers with a single word write because the
  84. * index byte is updated first.
  85. */
  86. vga_ioport_write(s, addr + 0x3c0, val & 0xff);
  87. vga_ioport_write(s, addr + 0x3c1, (val >> 8) & 0xff);
  88. break;
  89. }
  90. }
  91. static const MemoryRegionOps pci_vga_ioport_ops = {
  92. .read = pci_vga_ioport_read,
  93. .write = pci_vga_ioport_write,
  94. .valid.min_access_size = 1,
  95. .valid.max_access_size = 4,
  96. .impl.min_access_size = 1,
  97. .impl.max_access_size = 2,
  98. .endianness = DEVICE_LITTLE_ENDIAN,
  99. };
  100. static uint64_t pci_vga_bochs_read(void *ptr, hwaddr addr,
  101. unsigned size)
  102. {
  103. VGACommonState *s = ptr;
  104. int index = addr >> 1;
  105. vbe_ioport_write_index(s, 0, index);
  106. return vbe_ioport_read_data(s, 0);
  107. }
  108. static void pci_vga_bochs_write(void *ptr, hwaddr addr,
  109. uint64_t val, unsigned size)
  110. {
  111. VGACommonState *s = ptr;
  112. int index = addr >> 1;
  113. vbe_ioport_write_index(s, 0, index);
  114. vbe_ioport_write_data(s, 0, val);
  115. }
  116. static const MemoryRegionOps pci_vga_bochs_ops = {
  117. .read = pci_vga_bochs_read,
  118. .write = pci_vga_bochs_write,
  119. .valid.min_access_size = 1,
  120. .valid.max_access_size = 4,
  121. .impl.min_access_size = 2,
  122. .impl.max_access_size = 2,
  123. .endianness = DEVICE_LITTLE_ENDIAN,
  124. };
  125. static uint64_t pci_vga_qext_read(void *ptr, hwaddr addr, unsigned size)
  126. {
  127. VGACommonState *s = ptr;
  128. switch (addr) {
  129. case PCI_VGA_QEXT_REG_SIZE:
  130. return PCI_VGA_QEXT_SIZE;
  131. case PCI_VGA_QEXT_REG_BYTEORDER:
  132. return s->big_endian_fb ?
  133. PCI_VGA_QEXT_BIG_ENDIAN : PCI_VGA_QEXT_LITTLE_ENDIAN;
  134. default:
  135. return 0;
  136. }
  137. }
  138. static void pci_vga_qext_write(void *ptr, hwaddr addr,
  139. uint64_t val, unsigned size)
  140. {
  141. VGACommonState *s = ptr;
  142. switch (addr) {
  143. case PCI_VGA_QEXT_REG_BYTEORDER:
  144. if (val == PCI_VGA_QEXT_BIG_ENDIAN) {
  145. s->big_endian_fb = true;
  146. }
  147. if (val == PCI_VGA_QEXT_LITTLE_ENDIAN) {
  148. s->big_endian_fb = false;
  149. }
  150. break;
  151. }
  152. }
  153. static bool vga_get_big_endian_fb(Object *obj, Error **errp)
  154. {
  155. PCIVGAState *d = PCI_VGA(PCI_DEVICE(obj));
  156. return d->vga.big_endian_fb;
  157. }
  158. static void vga_set_big_endian_fb(Object *obj, bool value, Error **errp)
  159. {
  160. PCIVGAState *d = PCI_VGA(PCI_DEVICE(obj));
  161. d->vga.big_endian_fb = value;
  162. }
  163. static const MemoryRegionOps pci_vga_qext_ops = {
  164. .read = pci_vga_qext_read,
  165. .write = pci_vga_qext_write,
  166. .valid.min_access_size = 4,
  167. .valid.max_access_size = 4,
  168. .endianness = DEVICE_LITTLE_ENDIAN,
  169. };
  170. void pci_std_vga_mmio_region_init(VGACommonState *s,
  171. MemoryRegion *parent,
  172. MemoryRegion *subs,
  173. bool qext)
  174. {
  175. memory_region_init_io(&subs[0], NULL, &pci_vga_ioport_ops, s,
  176. "vga ioports remapped", PCI_VGA_IOPORT_SIZE);
  177. memory_region_add_subregion(parent, PCI_VGA_IOPORT_OFFSET,
  178. &subs[0]);
  179. memory_region_init_io(&subs[1], NULL, &pci_vga_bochs_ops, s,
  180. "bochs dispi interface", PCI_VGA_BOCHS_SIZE);
  181. memory_region_add_subregion(parent, PCI_VGA_BOCHS_OFFSET,
  182. &subs[1]);
  183. if (qext) {
  184. memory_region_init_io(&subs[2], NULL, &pci_vga_qext_ops, s,
  185. "qemu extended regs", PCI_VGA_QEXT_SIZE);
  186. memory_region_add_subregion(parent, PCI_VGA_QEXT_OFFSET,
  187. &subs[2]);
  188. }
  189. }
  190. static void pci_std_vga_realize(PCIDevice *dev, Error **errp)
  191. {
  192. PCIVGAState *d = PCI_VGA(dev);
  193. VGACommonState *s = &d->vga;
  194. bool qext = false;
  195. /* vga + console init */
  196. vga_common_init(s, OBJECT(dev), true);
  197. vga_init(s, OBJECT(dev), pci_address_space(dev), pci_address_space_io(dev),
  198. true);
  199. s->con = graphic_console_init(DEVICE(dev), 0, s->hw_ops, s);
  200. /* XXX: VGA_RAM_SIZE must be a power of two */
  201. pci_register_bar(&d->dev, 0, PCI_BASE_ADDRESS_MEM_PREFETCH, &s->vram);
  202. /* mmio bar for vga register access */
  203. if (d->flags & (1 << PCI_VGA_FLAG_ENABLE_MMIO)) {
  204. memory_region_init(&d->mmio, NULL, "vga.mmio", 4096);
  205. if (d->flags & (1 << PCI_VGA_FLAG_ENABLE_QEXT)) {
  206. qext = true;
  207. pci_set_byte(&d->dev.config[PCI_REVISION_ID], 2);
  208. }
  209. pci_std_vga_mmio_region_init(s, &d->mmio, d->mrs, qext);
  210. pci_register_bar(&d->dev, 2, PCI_BASE_ADDRESS_SPACE_MEMORY, &d->mmio);
  211. }
  212. if (!dev->rom_bar) {
  213. /* compatibility with pc-0.13 and older */
  214. vga_init_vbe(s, OBJECT(dev), pci_address_space(dev));
  215. }
  216. }
  217. static void pci_std_vga_init(Object *obj)
  218. {
  219. /* Expose framebuffer byteorder via QOM */
  220. object_property_add_bool(obj, "big-endian-framebuffer",
  221. vga_get_big_endian_fb, vga_set_big_endian_fb, NULL);
  222. }
  223. static void pci_secondary_vga_realize(PCIDevice *dev, Error **errp)
  224. {
  225. PCIVGAState *d = PCI_VGA(dev);
  226. VGACommonState *s = &d->vga;
  227. bool qext = false;
  228. /* vga + console init */
  229. vga_common_init(s, OBJECT(dev), false);
  230. s->con = graphic_console_init(DEVICE(dev), 0, s->hw_ops, s);
  231. /* mmio bar */
  232. memory_region_init(&d->mmio, OBJECT(dev), "vga.mmio", 4096);
  233. if (d->flags & (1 << PCI_VGA_FLAG_ENABLE_QEXT)) {
  234. qext = true;
  235. pci_set_byte(&d->dev.config[PCI_REVISION_ID], 2);
  236. }
  237. pci_std_vga_mmio_region_init(s, &d->mmio, d->mrs, qext);
  238. pci_register_bar(&d->dev, 0, PCI_BASE_ADDRESS_MEM_PREFETCH, &s->vram);
  239. pci_register_bar(&d->dev, 2, PCI_BASE_ADDRESS_SPACE_MEMORY, &d->mmio);
  240. }
  241. static void pci_secondary_vga_exit(PCIDevice *dev)
  242. {
  243. PCIVGAState *d = PCI_VGA(dev);
  244. VGACommonState *s = &d->vga;
  245. graphic_console_close(s->con);
  246. }
  247. static void pci_secondary_vga_init(Object *obj)
  248. {
  249. /* Expose framebuffer byteorder via QOM */
  250. object_property_add_bool(obj, "big-endian-framebuffer",
  251. vga_get_big_endian_fb, vga_set_big_endian_fb, NULL);
  252. }
  253. static void pci_secondary_vga_reset(DeviceState *dev)
  254. {
  255. PCIVGAState *d = PCI_VGA(PCI_DEVICE(dev));
  256. vga_common_reset(&d->vga);
  257. }
  258. static Property vga_pci_properties[] = {
  259. DEFINE_PROP_UINT32("vgamem_mb", PCIVGAState, vga.vram_size_mb, 16),
  260. DEFINE_PROP_BIT("mmio", PCIVGAState, flags, PCI_VGA_FLAG_ENABLE_MMIO, true),
  261. DEFINE_PROP_BIT("qemu-extended-regs",
  262. PCIVGAState, flags, PCI_VGA_FLAG_ENABLE_QEXT, true),
  263. DEFINE_PROP_END_OF_LIST(),
  264. };
  265. static Property secondary_pci_properties[] = {
  266. DEFINE_PROP_UINT32("vgamem_mb", PCIVGAState, vga.vram_size_mb, 16),
  267. DEFINE_PROP_BIT("qemu-extended-regs",
  268. PCIVGAState, flags, PCI_VGA_FLAG_ENABLE_QEXT, true),
  269. DEFINE_PROP_END_OF_LIST(),
  270. };
  271. static void vga_pci_class_init(ObjectClass *klass, void *data)
  272. {
  273. DeviceClass *dc = DEVICE_CLASS(klass);
  274. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  275. k->vendor_id = PCI_VENDOR_ID_QEMU;
  276. k->device_id = PCI_DEVICE_ID_QEMU_VGA;
  277. dc->vmsd = &vmstate_vga_pci;
  278. set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
  279. }
  280. static const TypeInfo vga_pci_type_info = {
  281. .name = TYPE_PCI_VGA,
  282. .parent = TYPE_PCI_DEVICE,
  283. .instance_size = sizeof(PCIVGAState),
  284. .abstract = true,
  285. .class_init = vga_pci_class_init,
  286. .interfaces = (InterfaceInfo[]) {
  287. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  288. { },
  289. },
  290. };
  291. static void vga_class_init(ObjectClass *klass, void *data)
  292. {
  293. DeviceClass *dc = DEVICE_CLASS(klass);
  294. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  295. k->realize = pci_std_vga_realize;
  296. k->romfile = "vgabios-stdvga.bin";
  297. k->class_id = PCI_CLASS_DISPLAY_VGA;
  298. dc->props = vga_pci_properties;
  299. dc->hotpluggable = false;
  300. }
  301. static void secondary_class_init(ObjectClass *klass, void *data)
  302. {
  303. DeviceClass *dc = DEVICE_CLASS(klass);
  304. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  305. k->realize = pci_secondary_vga_realize;
  306. k->exit = pci_secondary_vga_exit;
  307. k->class_id = PCI_CLASS_DISPLAY_OTHER;
  308. dc->props = secondary_pci_properties;
  309. dc->reset = pci_secondary_vga_reset;
  310. }
  311. static const TypeInfo vga_info = {
  312. .name = "VGA",
  313. .parent = TYPE_PCI_VGA,
  314. .instance_init = pci_std_vga_init,
  315. .class_init = vga_class_init,
  316. };
  317. static const TypeInfo secondary_info = {
  318. .name = "secondary-vga",
  319. .parent = TYPE_PCI_VGA,
  320. .instance_init = pci_secondary_vga_init,
  321. .class_init = secondary_class_init,
  322. };
  323. static void vga_register_types(void)
  324. {
  325. type_register_static(&vga_pci_type_info);
  326. type_register_static(&vga_info);
  327. type_register_static(&secondary_info);
  328. }
  329. type_init(vga_register_types)