GraphicsAdapter.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Atomic.h>
  7. #include <AK/Checked.h>
  8. #include <Kernel/Bus/PCI/Access.h>
  9. #include <Kernel/Bus/PCI/IDs.h>
  10. #include <Kernel/Debug.h>
  11. #include <Kernel/Graphics/Bochs/GraphicsAdapter.h>
  12. #include <Kernel/Graphics/Console/ContiguousFramebufferConsole.h>
  13. #include <Kernel/Graphics/GraphicsManagement.h>
  14. #include <Kernel/IO.h>
  15. #include <Kernel/Memory/TypedMapping.h>
  16. #include <Kernel/Sections.h>
  17. #define VBE_DISPI_IOPORT_INDEX 0x01CE
  18. #define VBE_DISPI_IOPORT_DATA 0x01CF
  19. #define VBE_DISPI_INDEX_ID 0x0
  20. #define VBE_DISPI_INDEX_XRES 0x1
  21. #define VBE_DISPI_INDEX_YRES 0x2
  22. #define VBE_DISPI_INDEX_BPP 0x3
  23. #define VBE_DISPI_INDEX_ENABLE 0x4
  24. #define VBE_DISPI_INDEX_BANK 0x5
  25. #define VBE_DISPI_INDEX_VIRT_WIDTH 0x6
  26. #define VBE_DISPI_INDEX_VIRT_HEIGHT 0x7
  27. #define VBE_DISPI_INDEX_X_OFFSET 0x8
  28. #define VBE_DISPI_INDEX_Y_OFFSET 0x9
  29. #define VBE_DISPI_DISABLED 0x00
  30. #define VBE_DISPI_ENABLED 0x01
  31. #define VBE_DISPI_LFB_ENABLED 0x40
  32. namespace Kernel {
  33. struct [[gnu::packed]] DISPIInterface {
  34. u16 index_id;
  35. u16 xres;
  36. u16 yres;
  37. u16 bpp;
  38. u16 enable;
  39. u16 bank;
  40. u16 virt_width;
  41. u16 virt_height;
  42. u16 x_offset;
  43. u16 y_offset;
  44. };
  45. struct [[gnu::packed]] BochsDisplayMMIORegisters {
  46. u8 edid_data[0x400];
  47. u16 vga_ioports[0x10];
  48. u8 reserved[0xE0];
  49. DISPIInterface bochs_regs;
  50. };
  51. UNMAP_AFTER_INIT NonnullRefPtr<BochsGraphicsAdapter> BochsGraphicsAdapter::initialize(PCI::Address address)
  52. {
  53. PCI::ID id = PCI::get_id(address);
  54. VERIFY((id.vendor_id == PCI::VendorID::QEMUOld && id.device_id == 0x1111) || (id.vendor_id == PCI::VendorID::VirtualBox && id.device_id == 0xbeef));
  55. return adopt_ref(*new BochsGraphicsAdapter(address));
  56. }
  57. UNMAP_AFTER_INIT BochsGraphicsAdapter::BochsGraphicsAdapter(PCI::Address pci_address)
  58. : PCI::DeviceController(pci_address)
  59. , m_mmio_registers(PCI::get_BAR2(pci_address) & 0xfffffff0)
  60. , m_registers(Memory::map_typed_writable<BochsDisplayMMIORegisters volatile>(m_mmio_registers))
  61. {
  62. // We assume safe resolutio is 1024x768x32
  63. m_framebuffer_console = Graphics::ContiguousFramebufferConsole::initialize(PhysicalAddress(PCI::get_BAR0(pci_address) & 0xfffffff0), 1024, 768, 1024 * sizeof(u32));
  64. // FIXME: This is a very wrong way to do this...
  65. GraphicsManagement::the().m_console = m_framebuffer_console;
  66. // Note: If we use VirtualBox graphics adapter (which is based on Bochs one), we need to use IO ports
  67. auto id = PCI::get_id(pci_address);
  68. if (id.vendor_id == 0x80ee && id.device_id == 0xbeef)
  69. m_io_required = true;
  70. // Note: According to Gerd Hoffmann - "The linux driver simply does
  71. // the unblank unconditionally. With bochs-display this is not needed but
  72. // it also has no bad side effect".
  73. unblank();
  74. set_safe_resolution();
  75. }
  76. UNMAP_AFTER_INIT void BochsGraphicsAdapter::initialize_framebuffer_devices()
  77. {
  78. // FIXME: Find a better way to determine default resolution...
  79. m_framebuffer_device = FramebufferDevice::create(*this, 0, PhysicalAddress(PCI::get_BAR0(pci_address()) & 0xfffffff0), 1024, 768, 1024 * sizeof(u32));
  80. // FIXME: Would be nice to be able to return a KResult here.
  81. VERIFY(!m_framebuffer_device->initialize().is_error());
  82. }
  83. GraphicsDevice::Type BochsGraphicsAdapter::type() const
  84. {
  85. if (PCI::get_class(pci_address()) == 0x3 && PCI::get_subclass(pci_address()) == 0x0)
  86. return Type::VGACompatible;
  87. return Type::Bochs;
  88. }
  89. void BochsGraphicsAdapter::unblank()
  90. {
  91. full_memory_barrier();
  92. m_registers->vga_ioports[0] = 0x20;
  93. full_memory_barrier();
  94. }
  95. void BochsGraphicsAdapter::set_safe_resolution()
  96. {
  97. VERIFY(m_framebuffer_console);
  98. auto result = try_to_set_resolution(0, 1024, 768);
  99. VERIFY(result);
  100. }
  101. static void set_register_with_io(u16 index, u16 data)
  102. {
  103. IO::out16(VBE_DISPI_IOPORT_INDEX, index);
  104. IO::out16(VBE_DISPI_IOPORT_DATA, data);
  105. }
  106. static u16 get_register_with_io(u16 index)
  107. {
  108. IO::out16(VBE_DISPI_IOPORT_INDEX, index);
  109. return IO::in16(VBE_DISPI_IOPORT_DATA);
  110. }
  111. void BochsGraphicsAdapter::set_resolution_registers_via_io(size_t width, size_t height)
  112. {
  113. dbgln_if(BXVGA_DEBUG, "BochsGraphicsAdapter resolution registers set to - {}x{}", width, height);
  114. set_register_with_io(VBE_DISPI_INDEX_ENABLE, VBE_DISPI_DISABLED);
  115. set_register_with_io(VBE_DISPI_INDEX_XRES, (u16)width);
  116. set_register_with_io(VBE_DISPI_INDEX_YRES, (u16)height);
  117. set_register_with_io(VBE_DISPI_INDEX_VIRT_WIDTH, (u16)width);
  118. set_register_with_io(VBE_DISPI_INDEX_VIRT_HEIGHT, (u16)height * 2);
  119. set_register_with_io(VBE_DISPI_INDEX_BPP, 32);
  120. set_register_with_io(VBE_DISPI_INDEX_ENABLE, VBE_DISPI_ENABLED | VBE_DISPI_LFB_ENABLED);
  121. set_register_with_io(VBE_DISPI_INDEX_BANK, 0);
  122. }
  123. void BochsGraphicsAdapter::set_resolution_registers(size_t width, size_t height)
  124. {
  125. dbgln_if(BXVGA_DEBUG, "BochsGraphicsAdapter resolution registers set to - {}x{}", width, height);
  126. m_registers->bochs_regs.enable = VBE_DISPI_DISABLED;
  127. full_memory_barrier();
  128. m_registers->bochs_regs.xres = width;
  129. m_registers->bochs_regs.yres = height;
  130. m_registers->bochs_regs.virt_width = width;
  131. m_registers->bochs_regs.virt_height = height * 2;
  132. m_registers->bochs_regs.bpp = 32;
  133. full_memory_barrier();
  134. m_registers->bochs_regs.enable = VBE_DISPI_ENABLED | VBE_DISPI_LFB_ENABLED;
  135. full_memory_barrier();
  136. m_registers->bochs_regs.bank = 0;
  137. }
  138. bool BochsGraphicsAdapter::try_to_set_resolution(size_t output_port_index, size_t width, size_t height)
  139. {
  140. // Note: There's only one output port for this adapter
  141. VERIFY(output_port_index == 0);
  142. VERIFY(m_framebuffer_console);
  143. if (Checked<size_t>::multiplication_would_overflow(width, height, sizeof(u32)))
  144. return false;
  145. if (m_io_required)
  146. set_resolution_registers_via_io(width, height);
  147. else
  148. set_resolution_registers(width, height);
  149. dbgln_if(BXVGA_DEBUG, "BochsGraphicsAdapter resolution test - {}x{}", width, height);
  150. if (m_io_required) {
  151. if (!validate_setup_resolution_with_io(width, height))
  152. return false;
  153. } else {
  154. if (!validate_setup_resolution(width, height))
  155. return false;
  156. }
  157. dbgln("BochsGraphicsAdapter: resolution set to {}x{}", width, height);
  158. m_framebuffer_console->set_resolution(width, height, width * sizeof(u32));
  159. return true;
  160. }
  161. bool BochsGraphicsAdapter::validate_setup_resolution_with_io(size_t width, size_t height)
  162. {
  163. if ((u16)width != get_register_with_io(VBE_DISPI_INDEX_XRES) || (u16)height != get_register_with_io(VBE_DISPI_INDEX_YRES)) {
  164. return false;
  165. }
  166. return true;
  167. }
  168. bool BochsGraphicsAdapter::validate_setup_resolution(size_t width, size_t height)
  169. {
  170. if ((u16)width != m_registers->bochs_regs.xres || (u16)height != m_registers->bochs_regs.yres) {
  171. return false;
  172. }
  173. return true;
  174. }
  175. bool BochsGraphicsAdapter::set_y_offset(size_t output_port_index, size_t y_offset)
  176. {
  177. VERIFY(output_port_index == 0);
  178. if (m_console_enabled)
  179. return false;
  180. m_registers->bochs_regs.y_offset = y_offset;
  181. return true;
  182. }
  183. void BochsGraphicsAdapter::enable_consoles()
  184. {
  185. ScopedSpinLock lock(m_console_mode_switch_lock);
  186. VERIFY(m_framebuffer_console);
  187. m_console_enabled = true;
  188. m_registers->bochs_regs.y_offset = 0;
  189. if (m_framebuffer_device)
  190. m_framebuffer_device->deactivate_writes();
  191. m_framebuffer_console->enable();
  192. }
  193. void BochsGraphicsAdapter::disable_consoles()
  194. {
  195. ScopedSpinLock lock(m_console_mode_switch_lock);
  196. VERIFY(m_framebuffer_console);
  197. VERIFY(m_framebuffer_device);
  198. m_console_enabled = false;
  199. m_registers->bochs_regs.y_offset = 0;
  200. m_framebuffer_console->disable();
  201. m_framebuffer_device->activate_writes();
  202. }
  203. }