GraphicsAdapter.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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/API.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_ID5 0xB0C5
  20. #define VBE_DISPI_INDEX_ID 0x0
  21. #define VBE_DISPI_INDEX_XRES 0x1
  22. #define VBE_DISPI_INDEX_YRES 0x2
  23. #define VBE_DISPI_INDEX_BPP 0x3
  24. #define VBE_DISPI_INDEX_ENABLE 0x4
  25. #define VBE_DISPI_INDEX_BANK 0x5
  26. #define VBE_DISPI_INDEX_VIRT_WIDTH 0x6
  27. #define VBE_DISPI_INDEX_VIRT_HEIGHT 0x7
  28. #define VBE_DISPI_INDEX_X_OFFSET 0x8
  29. #define VBE_DISPI_INDEX_Y_OFFSET 0x9
  30. #define VBE_DISPI_DISABLED 0x00
  31. #define VBE_DISPI_ENABLED 0x01
  32. #define VBE_DISPI_LFB_ENABLED 0x40
  33. #define BOCHS_DISPLAY_LITTLE_ENDIAN 0x1e1e1e1e
  34. #define BOCHS_DISPLAY_BIG_ENDIAN 0xbebebebe
  35. namespace Kernel {
  36. struct [[gnu::packed]] DISPIInterface {
  37. u16 index_id;
  38. u16 xres;
  39. u16 yres;
  40. u16 bpp;
  41. u16 enable;
  42. u16 bank;
  43. u16 virt_width;
  44. u16 virt_height;
  45. u16 x_offset;
  46. u16 y_offset;
  47. };
  48. struct [[gnu::packed]] ExtensionRegisters {
  49. u32 region_size;
  50. u32 framebuffer_byteorder;
  51. };
  52. struct [[gnu::packed]] BochsDisplayMMIORegisters {
  53. u8 edid_data[0x400];
  54. u16 vga_ioports[0x10];
  55. u8 reserved[0xE0];
  56. DISPIInterface bochs_regs;
  57. u8 reserved2[0x100 - sizeof(DISPIInterface)];
  58. ExtensionRegisters extension_regs;
  59. };
  60. UNMAP_AFTER_INIT NonnullRefPtr<BochsGraphicsAdapter> BochsGraphicsAdapter::initialize(PCI::Address address)
  61. {
  62. PCI::ID id = PCI::get_id(address);
  63. VERIFY((id.vendor_id == PCI::VendorID::QEMUOld && id.device_id == 0x1111) || (id.vendor_id == PCI::VendorID::VirtualBox && id.device_id == 0xbeef));
  64. return adopt_ref(*new BochsGraphicsAdapter(address));
  65. }
  66. void BochsGraphicsAdapter::set_framebuffer_to_big_endian_format()
  67. {
  68. dbgln_if(BXVGA_DEBUG, "BochsGraphicsAdapter set_framebuffer_to_big_endian_format");
  69. full_memory_barrier();
  70. if (m_registers->extension_regs.region_size == 0xFFFFFFFF || m_registers->extension_regs.region_size == 0)
  71. return;
  72. full_memory_barrier();
  73. m_registers->extension_regs.framebuffer_byteorder = BOCHS_DISPLAY_BIG_ENDIAN;
  74. full_memory_barrier();
  75. }
  76. void BochsGraphicsAdapter::set_framebuffer_to_little_endian_format()
  77. {
  78. dbgln_if(BXVGA_DEBUG, "BochsGraphicsAdapter set_framebuffer_to_little_endian_format");
  79. full_memory_barrier();
  80. if (m_registers->extension_regs.region_size == 0xFFFFFFFF || m_registers->extension_regs.region_size == 0)
  81. return;
  82. full_memory_barrier();
  83. m_registers->extension_regs.framebuffer_byteorder = BOCHS_DISPLAY_LITTLE_ENDIAN;
  84. full_memory_barrier();
  85. }
  86. UNMAP_AFTER_INIT BochsGraphicsAdapter::BochsGraphicsAdapter(PCI::Address pci_address)
  87. : PCI::Device(pci_address)
  88. , m_mmio_registers(PCI::get_BAR2(pci_address) & 0xfffffff0)
  89. , m_registers(Memory::map_typed_writable<BochsDisplayMMIORegisters volatile>(m_mmio_registers))
  90. {
  91. // We assume safe resolutio is 1024x768x32
  92. m_framebuffer_console = Graphics::ContiguousFramebufferConsole::initialize(PhysicalAddress(PCI::get_BAR0(pci_address) & 0xfffffff0), 1024, 768, 1024 * sizeof(u32));
  93. // FIXME: This is a very wrong way to do this...
  94. GraphicsManagement::the().m_console = m_framebuffer_console;
  95. // Note: If we use VirtualBox graphics adapter (which is based on Bochs one), we need to use IO ports
  96. auto id = PCI::get_id(pci_address);
  97. if (id.vendor_id == 0x80ee && id.device_id == 0xbeef)
  98. m_io_required = true;
  99. // Note: According to Gerd Hoffmann - "The linux driver simply does
  100. // the unblank unconditionally. With bochs-display this is not needed but
  101. // it also has no bad side effect".
  102. unblank();
  103. set_safe_resolution();
  104. }
  105. UNMAP_AFTER_INIT void BochsGraphicsAdapter::initialize_framebuffer_devices()
  106. {
  107. // FIXME: Find a better way to determine default resolution...
  108. m_framebuffer_device = FramebufferDevice::create(*this, 0, PhysicalAddress(PCI::get_BAR0(pci_address()) & 0xfffffff0), 1024, 768, 1024 * sizeof(u32));
  109. // FIXME: Would be nice to be able to return a KResult here.
  110. VERIFY(!m_framebuffer_device->initialize().is_error());
  111. }
  112. GraphicsDevice::Type BochsGraphicsAdapter::type() const
  113. {
  114. if (PCI::get_class(pci_address()) == 0x3 && PCI::get_subclass(pci_address()) == 0x0)
  115. return Type::VGACompatible;
  116. return Type::Bochs;
  117. }
  118. void BochsGraphicsAdapter::unblank()
  119. {
  120. full_memory_barrier();
  121. m_registers->vga_ioports[0] = 0x20;
  122. full_memory_barrier();
  123. }
  124. void BochsGraphicsAdapter::set_safe_resolution()
  125. {
  126. VERIFY(m_framebuffer_console);
  127. auto result = try_to_set_resolution(0, 1024, 768);
  128. VERIFY(result);
  129. }
  130. static void set_register_with_io(u16 index, u16 data)
  131. {
  132. IO::out16(VBE_DISPI_IOPORT_INDEX, index);
  133. IO::out16(VBE_DISPI_IOPORT_DATA, data);
  134. }
  135. static u16 get_register_with_io(u16 index)
  136. {
  137. IO::out16(VBE_DISPI_IOPORT_INDEX, index);
  138. return IO::in16(VBE_DISPI_IOPORT_DATA);
  139. }
  140. BochsGraphicsAdapter::IndexID BochsGraphicsAdapter::index_id() const
  141. {
  142. if (m_io_required) {
  143. return get_register_with_io(0);
  144. }
  145. return m_registers->bochs_regs.index_id;
  146. }
  147. void BochsGraphicsAdapter::set_resolution_registers_via_io(size_t width, size_t height)
  148. {
  149. dbgln_if(BXVGA_DEBUG, "BochsGraphicsAdapter resolution registers set to - {}x{}", width, height);
  150. set_register_with_io(VBE_DISPI_INDEX_ENABLE, VBE_DISPI_DISABLED);
  151. set_register_with_io(VBE_DISPI_INDEX_XRES, (u16)width);
  152. set_register_with_io(VBE_DISPI_INDEX_YRES, (u16)height);
  153. set_register_with_io(VBE_DISPI_INDEX_VIRT_WIDTH, (u16)width);
  154. set_register_with_io(VBE_DISPI_INDEX_VIRT_HEIGHT, (u16)height * 2);
  155. set_register_with_io(VBE_DISPI_INDEX_BPP, 32);
  156. set_register_with_io(VBE_DISPI_INDEX_ENABLE, VBE_DISPI_ENABLED | VBE_DISPI_LFB_ENABLED);
  157. set_register_with_io(VBE_DISPI_INDEX_BANK, 0);
  158. }
  159. void BochsGraphicsAdapter::set_resolution_registers(size_t width, size_t height)
  160. {
  161. dbgln_if(BXVGA_DEBUG, "BochsGraphicsAdapter resolution registers set to - {}x{}", width, height);
  162. m_registers->bochs_regs.enable = VBE_DISPI_DISABLED;
  163. full_memory_barrier();
  164. m_registers->bochs_regs.xres = width;
  165. m_registers->bochs_regs.yres = height;
  166. m_registers->bochs_regs.virt_width = width;
  167. m_registers->bochs_regs.virt_height = height * 2;
  168. m_registers->bochs_regs.bpp = 32;
  169. full_memory_barrier();
  170. m_registers->bochs_regs.enable = VBE_DISPI_ENABLED | VBE_DISPI_LFB_ENABLED;
  171. full_memory_barrier();
  172. m_registers->bochs_regs.bank = 0;
  173. if (index_id().value() == VBE_DISPI_ID5) {
  174. set_framebuffer_to_little_endian_format();
  175. }
  176. }
  177. bool BochsGraphicsAdapter::try_to_set_resolution(size_t output_port_index, size_t width, size_t height)
  178. {
  179. // Note: There's only one output port for this adapter
  180. VERIFY(output_port_index == 0);
  181. VERIFY(m_framebuffer_console);
  182. if (Checked<size_t>::multiplication_would_overflow(width, height, sizeof(u32)))
  183. return false;
  184. if (m_io_required)
  185. set_resolution_registers_via_io(width, height);
  186. else
  187. set_resolution_registers(width, height);
  188. dbgln_if(BXVGA_DEBUG, "BochsGraphicsAdapter resolution test - {}x{}", width, height);
  189. if (m_io_required) {
  190. if (!validate_setup_resolution_with_io(width, height))
  191. return false;
  192. } else {
  193. if (!validate_setup_resolution(width, height))
  194. return false;
  195. }
  196. dbgln("BochsGraphicsAdapter: resolution set to {}x{}", width, height);
  197. m_framebuffer_console->set_resolution(width, height, width * sizeof(u32));
  198. return true;
  199. }
  200. bool BochsGraphicsAdapter::validate_setup_resolution_with_io(size_t width, size_t height)
  201. {
  202. if ((u16)width != get_register_with_io(VBE_DISPI_INDEX_XRES) || (u16)height != get_register_with_io(VBE_DISPI_INDEX_YRES)) {
  203. return false;
  204. }
  205. return true;
  206. }
  207. bool BochsGraphicsAdapter::validate_setup_resolution(size_t width, size_t height)
  208. {
  209. if ((u16)width != m_registers->bochs_regs.xres || (u16)height != m_registers->bochs_regs.yres) {
  210. return false;
  211. }
  212. return true;
  213. }
  214. bool BochsGraphicsAdapter::set_y_offset(size_t output_port_index, size_t y_offset)
  215. {
  216. VERIFY(output_port_index == 0);
  217. if (m_console_enabled)
  218. return false;
  219. m_registers->bochs_regs.y_offset = y_offset;
  220. return true;
  221. }
  222. void BochsGraphicsAdapter::enable_consoles()
  223. {
  224. SpinlockLocker lock(m_console_mode_switch_lock);
  225. VERIFY(m_framebuffer_console);
  226. m_console_enabled = true;
  227. m_registers->bochs_regs.y_offset = 0;
  228. if (m_framebuffer_device)
  229. m_framebuffer_device->deactivate_writes();
  230. m_framebuffer_console->enable();
  231. }
  232. void BochsGraphicsAdapter::disable_consoles()
  233. {
  234. SpinlockLocker lock(m_console_mode_switch_lock);
  235. VERIFY(m_framebuffer_console);
  236. VERIFY(m_framebuffer_device);
  237. m_console_enabled = false;
  238. m_registers->bochs_regs.y_offset = 0;
  239. m_framebuffer_console->disable();
  240. m_framebuffer_device->activate_writes();
  241. }
  242. }