BochsDisplayConnector.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Platform.h>
  7. #include <Kernel/Arch/x86/Hypervisor/BochsDisplayConnector.h>
  8. #include <Kernel/Arch/x86/IO.h>
  9. #include <Kernel/Bus/PCI/Access.h>
  10. #include <Kernel/Debug.h>
  11. #include <Kernel/Devices/DeviceManagement.h>
  12. #include <Kernel/Graphics/Bochs/Definitions.h>
  13. #include <Kernel/Graphics/Console/ContiguousFramebufferConsole.h>
  14. #include <Kernel/Graphics/GraphicsManagement.h>
  15. namespace Kernel {
  16. static void set_register_with_io(u16 index, u16 data)
  17. {
  18. IO::out16(VBE_DISPI_IOPORT_INDEX, index);
  19. IO::out16(VBE_DISPI_IOPORT_DATA, data);
  20. }
  21. static u16 get_register_with_io(u16 index)
  22. {
  23. IO::out16(VBE_DISPI_IOPORT_INDEX, index);
  24. return IO::in16(VBE_DISPI_IOPORT_DATA);
  25. }
  26. LockRefPtr<BochsDisplayConnector> BochsDisplayConnector::try_create_for_vga_isa_connector()
  27. {
  28. VERIFY(PCI::Access::is_hardware_disabled());
  29. BochsDisplayConnector::IndexID index_id = get_register_with_io(0);
  30. if (index_id != VBE_DISPI_ID5)
  31. return {};
  32. auto video_ram_64k_chunks_count = get_register_with_io(to_underlying(BochsDISPIRegisters::VIDEO_RAM_64K_CHUNKS_COUNT));
  33. if (video_ram_64k_chunks_count == 0 || video_ram_64k_chunks_count == 0xffff) {
  34. dmesgln("Graphics: Bochs ISA VGA compatible adapter does not indicate amount of VRAM, default to 8 MiB");
  35. video_ram_64k_chunks_count = (8 * MiB) / (64 * KiB);
  36. } else {
  37. dmesgln("Graphics: Bochs ISA VGA compatible adapter indicates {} bytes of VRAM", video_ram_64k_chunks_count * (64 * KiB));
  38. }
  39. // Note: The default physical address for isa-vga framebuffer in QEMU is 0xE0000000.
  40. // Since this is probably hardcoded at other OSes in their guest drivers,
  41. // we can assume this is going to stay the same framebuffer physical address for
  42. // this device and will not be changed in the future.
  43. auto device_or_error = DeviceManagement::try_create_device<BochsDisplayConnector>(PhysicalAddress(0xE0000000), video_ram_64k_chunks_count * (64 * KiB));
  44. VERIFY(!device_or_error.is_error());
  45. auto connector = device_or_error.release_value();
  46. MUST(connector->create_attached_framebuffer_console());
  47. MUST(connector->initialize_edid_for_generic_monitor({}));
  48. return connector;
  49. }
  50. NonnullLockRefPtr<BochsDisplayConnector> BochsDisplayConnector::must_create(PhysicalAddress framebuffer_address, size_t framebuffer_resource_size, bool virtual_box_hardware)
  51. {
  52. auto device_or_error = DeviceManagement::try_create_device<BochsDisplayConnector>(framebuffer_address, framebuffer_resource_size);
  53. VERIFY(!device_or_error.is_error());
  54. auto connector = device_or_error.release_value();
  55. MUST(connector->create_attached_framebuffer_console());
  56. if (virtual_box_hardware)
  57. MUST(connector->initialize_edid_for_generic_monitor(Array<u8, 3> { 'V', 'B', 'X' }));
  58. else
  59. MUST(connector->initialize_edid_for_generic_monitor({}));
  60. return connector;
  61. }
  62. BochsDisplayConnector::BochsDisplayConnector(PhysicalAddress framebuffer_address, size_t framebuffer_resource_size)
  63. : DisplayConnector(framebuffer_address, framebuffer_resource_size, false)
  64. {
  65. }
  66. ErrorOr<void> BochsDisplayConnector::create_attached_framebuffer_console()
  67. {
  68. // We assume safe resolution is 1024x768x32
  69. m_framebuffer_console = Graphics::ContiguousFramebufferConsole::initialize(m_framebuffer_address.value(), 1024, 768, 1024 * sizeof(u32));
  70. GraphicsManagement::the().set_console(*m_framebuffer_console);
  71. return {};
  72. }
  73. BochsDisplayConnector::IndexID BochsDisplayConnector::index_id() const
  74. {
  75. return get_register_with_io(0);
  76. }
  77. void BochsDisplayConnector::enable_console()
  78. {
  79. VERIFY(m_control_lock.is_locked());
  80. VERIFY(m_framebuffer_console);
  81. m_framebuffer_console->enable();
  82. }
  83. void BochsDisplayConnector::disable_console()
  84. {
  85. VERIFY(m_control_lock.is_locked());
  86. VERIFY(m_framebuffer_console);
  87. m_framebuffer_console->disable();
  88. }
  89. ErrorOr<void> BochsDisplayConnector::flush_first_surface()
  90. {
  91. return Error::from_errno(ENOTSUP);
  92. }
  93. ErrorOr<void> BochsDisplayConnector::set_safe_mode_setting()
  94. {
  95. DisplayConnector::ModeSetting safe_mode_set {
  96. .horizontal_stride = 1024 * sizeof(u32),
  97. .pixel_clock_in_khz = 0, // Note: There's no pixel clock in paravirtualized hardware
  98. .horizontal_active = 1024,
  99. .horizontal_front_porch_pixels = 0, // Note: There's no horizontal_front_porch_pixels in paravirtualized hardware
  100. .horizontal_sync_time_pixels = 0, // Note: There's no horizontal_sync_time_pixels in paravirtualized hardware
  101. .horizontal_blank_pixels = 0, // Note: There's no horizontal_blank_pixels in paravirtualized hardware
  102. .vertical_active = 768,
  103. .vertical_front_porch_lines = 0, // Note: There's no vertical_front_porch_lines in paravirtualized hardware
  104. .vertical_sync_time_lines = 0, // Note: There's no vertical_sync_time_lines in paravirtualized hardware
  105. .vertical_blank_lines = 0, // Note: There's no vertical_blank_lines in paravirtualized hardware
  106. .horizontal_offset = 0,
  107. .vertical_offset = 0,
  108. };
  109. return set_mode_setting(safe_mode_set);
  110. }
  111. ErrorOr<void> BochsDisplayConnector::set_mode_setting(ModeSetting const& mode_setting)
  112. {
  113. SpinlockLocker locker(m_modeset_lock);
  114. size_t width = mode_setting.horizontal_active;
  115. size_t height = mode_setting.vertical_active;
  116. dbgln_if(BXVGA_DEBUG, "BochsDisplayConnector resolution registers set to - {}x{}", width, height);
  117. set_register_with_io(to_underlying(BochsDISPIRegisters::ENABLE), 0);
  118. set_register_with_io(to_underlying(BochsDISPIRegisters::XRES), (u16)width);
  119. set_register_with_io(to_underlying(BochsDISPIRegisters::YRES), (u16)height);
  120. set_register_with_io(to_underlying(BochsDISPIRegisters::VIRT_WIDTH), (u16)width);
  121. set_register_with_io(to_underlying(BochsDISPIRegisters::VIRT_HEIGHT), (u16)height * 2);
  122. set_register_with_io(to_underlying(BochsDISPIRegisters::BPP), 32);
  123. set_register_with_io(to_underlying(BochsDISPIRegisters::ENABLE), to_underlying(BochsFramebufferSettings::Enabled) | to_underlying(BochsFramebufferSettings::LinearFramebuffer));
  124. set_register_with_io(to_underlying(BochsDISPIRegisters::BANK), 0);
  125. if ((u16)width != get_register_with_io(to_underlying(BochsDISPIRegisters::XRES)) || (u16)height != get_register_with_io(to_underlying(BochsDISPIRegisters::YRES))) {
  126. return Error::from_errno(ENOTIMPL);
  127. }
  128. auto current_horizontal_active = get_register_with_io(to_underlying(BochsDISPIRegisters::XRES));
  129. auto current_vertical_active = get_register_with_io(to_underlying(BochsDISPIRegisters::YRES));
  130. DisplayConnector::ModeSetting mode_set {
  131. .horizontal_stride = current_horizontal_active * sizeof(u32),
  132. .pixel_clock_in_khz = 0, // Note: There's no pixel clock in paravirtualized hardware
  133. .horizontal_active = current_horizontal_active,
  134. .horizontal_front_porch_pixels = 0, // Note: There's no horizontal_front_porch_pixels in paravirtualized hardware
  135. .horizontal_sync_time_pixels = 0, // Note: There's no horizontal_sync_time_pixels in paravirtualized hardware
  136. .horizontal_blank_pixels = 0, // Note: There's no horizontal_blank_pixels in paravirtualized hardware
  137. .vertical_active = current_vertical_active,
  138. .vertical_front_porch_lines = 0, // Note: There's no vertical_front_porch_lines in paravirtualized hardware
  139. .vertical_sync_time_lines = 0, // Note: There's no vertical_sync_time_lines in paravirtualized hardware
  140. .vertical_blank_lines = 0, // Note: There's no vertical_blank_lines in paravirtualized hardware
  141. .horizontal_offset = 0,
  142. .vertical_offset = 0,
  143. };
  144. m_current_mode_setting = mode_set;
  145. return {};
  146. }
  147. ErrorOr<void> BochsDisplayConnector::set_y_offset(size_t)
  148. {
  149. // Note: Although when using this device on QEMU we can actually set the horizontal and vertical offsets
  150. // with IO ports, this class is meant to be used for plain old Bochs graphics which might not support
  151. // this feature at all.
  152. return Error::from_errno(ENOTIMPL);
  153. }
  154. ErrorOr<void> BochsDisplayConnector::unblank()
  155. {
  156. return Error::from_errno(ENOTIMPL);
  157. }
  158. }