vga-pci.c 13 KB

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