GraphicsManagement.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Singleton.h>
  7. #include <Kernel/Arch/x86/IO.h>
  8. #include <Kernel/Bus/PCI/API.h>
  9. #include <Kernel/Bus/PCI/IDs.h>
  10. #include <Kernel/CommandLine.h>
  11. #include <Kernel/Graphics/Bochs/GraphicsAdapter.h>
  12. #include <Kernel/Graphics/Console/BootFramebufferConsole.h>
  13. #include <Kernel/Graphics/GraphicsManagement.h>
  14. #include <Kernel/Graphics/Intel/NativeGraphicsAdapter.h>
  15. #include <Kernel/Graphics/VGA/ISAAdapter.h>
  16. #include <Kernel/Graphics/VGA/PCIAdapter.h>
  17. #include <Kernel/Graphics/VGA/VGACompatibleAdapter.h>
  18. #include <Kernel/Graphics/VirtIOGPU/GraphicsAdapter.h>
  19. #include <Kernel/Memory/AnonymousVMObject.h>
  20. #include <Kernel/Multiboot.h>
  21. #include <Kernel/Sections.h>
  22. namespace Kernel {
  23. static Singleton<GraphicsManagement> s_the;
  24. extern Atomic<Graphics::Console*> g_boot_console;
  25. GraphicsManagement& GraphicsManagement::the()
  26. {
  27. return *s_the;
  28. }
  29. bool GraphicsManagement::is_initialized()
  30. {
  31. return s_the.is_initialized();
  32. }
  33. UNMAP_AFTER_INIT GraphicsManagement::GraphicsManagement()
  34. {
  35. }
  36. void GraphicsManagement::disable_vga_emulation_access_permanently()
  37. {
  38. SpinlockLocker locker(m_main_vga_lock);
  39. disable_vga_text_mode_console_cursor();
  40. IO::out8(0x3c4, 1);
  41. u8 sr1 = IO::in8(0x3c5);
  42. IO::out8(0x3c5, sr1 | 1 << 5);
  43. IO::delay(1000);
  44. m_vga_access_is_disabled = true;
  45. }
  46. void GraphicsManagement::enable_vga_text_mode_console_cursor()
  47. {
  48. SpinlockLocker locker(m_main_vga_lock);
  49. if (m_vga_access_is_disabled)
  50. return;
  51. IO::out8(0x3D4, 0xA);
  52. IO::out8(0x3D5, 0);
  53. }
  54. void GraphicsManagement::disable_vga_text_mode_console_cursor()
  55. {
  56. SpinlockLocker locker(m_main_vga_lock);
  57. if (m_vga_access_is_disabled)
  58. return;
  59. IO::out8(0x3D4, 0xA);
  60. IO::out8(0x3D5, 0x20);
  61. }
  62. void GraphicsManagement::set_vga_text_mode_cursor(size_t console_width, size_t x, size_t y)
  63. {
  64. SpinlockLocker locker(m_main_vga_lock);
  65. if (m_vga_access_is_disabled)
  66. return;
  67. enable_vga_text_mode_console_cursor();
  68. u16 value = y * console_width + x;
  69. IO::out8(0x3d4, 0x0e);
  70. IO::out8(0x3d5, MSB(value));
  71. IO::out8(0x3d4, 0x0f);
  72. IO::out8(0x3d5, LSB(value));
  73. }
  74. void GraphicsManagement::deactivate_graphical_mode()
  75. {
  76. for (auto& connector : m_display_connector_nodes) {
  77. connector.set_display_mode({}, DisplayConnector::DisplayMode::Console);
  78. }
  79. }
  80. void GraphicsManagement::activate_graphical_mode()
  81. {
  82. for (auto& connector : m_display_connector_nodes) {
  83. connector.set_display_mode({}, DisplayConnector::DisplayMode::Graphical);
  84. }
  85. }
  86. void GraphicsManagement::attach_new_display_connector(Badge<DisplayConnector>, DisplayConnector& connector)
  87. {
  88. m_display_connector_nodes.append(connector);
  89. }
  90. void GraphicsManagement::detach_display_connector(Badge<DisplayConnector>, DisplayConnector& connector)
  91. {
  92. m_display_connector_nodes.remove(connector);
  93. }
  94. static inline bool is_vga_compatible_pci_device(PCI::DeviceIdentifier const& device_identifier)
  95. {
  96. // Note: Check for Display Controller, VGA Compatible Controller or
  97. // Unclassified, VGA-Compatible Unclassified Device
  98. auto is_display_controller_vga_compatible = device_identifier.class_code().value() == 0x3 && device_identifier.subclass_code().value() == 0x0;
  99. auto is_general_pci_vga_compatible = device_identifier.class_code().value() == 0x0 && device_identifier.subclass_code().value() == 0x1;
  100. return is_display_controller_vga_compatible || is_general_pci_vga_compatible;
  101. }
  102. static inline bool is_display_controller_pci_device(PCI::DeviceIdentifier const& device_identifier)
  103. {
  104. return device_identifier.class_code().value() == 0x3;
  105. }
  106. UNMAP_AFTER_INIT bool GraphicsManagement::determine_and_initialize_isa_graphics_device()
  107. {
  108. dmesgln("Graphics: Using a ISA VGA compatible generic adapter");
  109. auto adapter = ISAVGAAdapter::initialize();
  110. m_graphics_devices.append(*adapter);
  111. m_vga_adapter = adapter;
  112. return true;
  113. }
  114. UNMAP_AFTER_INIT bool GraphicsManagement::determine_and_initialize_graphics_device(PCI::DeviceIdentifier const& device_identifier)
  115. {
  116. VERIFY(is_vga_compatible_pci_device(device_identifier) || is_display_controller_pci_device(device_identifier));
  117. RefPtr<GenericGraphicsAdapter> adapter;
  118. auto create_bootloader_framebuffer_device = [&]() {
  119. if (multiboot_framebuffer_addr.is_null()) {
  120. // Prekernel sets the framebuffer address to 0 if MULTIBOOT_INFO_FRAMEBUFFER_INFO
  121. // is not present, as there is likely never a valid framebuffer at this physical address.
  122. dmesgln("Graphics: Bootloader did not set up a framebuffer");
  123. } else if (multiboot_framebuffer_type != MULTIBOOT_FRAMEBUFFER_TYPE_RGB) {
  124. dmesgln("Graphics: The framebuffer set up by the bootloader is not RGB");
  125. } else {
  126. dmesgln("Graphics: Using a preset resolution from the bootloader");
  127. adapter = PCIVGACompatibleAdapter::initialize_with_preset_resolution(device_identifier,
  128. multiboot_framebuffer_addr,
  129. multiboot_framebuffer_width,
  130. multiboot_framebuffer_height,
  131. multiboot_framebuffer_pitch);
  132. }
  133. };
  134. if (!adapter) {
  135. switch (device_identifier.hardware_id().vendor_id) {
  136. case PCI::VendorID::QEMUOld:
  137. if (device_identifier.hardware_id().device_id == 0x1111)
  138. adapter = BochsGraphicsAdapter::initialize(device_identifier);
  139. break;
  140. case PCI::VendorID::VirtualBox:
  141. if (device_identifier.hardware_id().device_id == 0xbeef)
  142. adapter = BochsGraphicsAdapter::initialize(device_identifier);
  143. break;
  144. case PCI::VendorID::Intel:
  145. adapter = IntelNativeGraphicsAdapter::initialize(device_identifier);
  146. break;
  147. case PCI::VendorID::VirtIO:
  148. dmesgln("Graphics: Using VirtIO console");
  149. adapter = VirtIOGraphicsAdapter::initialize(device_identifier);
  150. break;
  151. default:
  152. if (!is_vga_compatible_pci_device(device_identifier))
  153. break;
  154. // Note: Although technically possible that a system has a
  155. // non-compatible VGA graphics device that was initialized by the
  156. // Multiboot bootloader to provide a framebuffer, in practice we
  157. // probably want to support these devices natively instead of
  158. // initializing them as some sort of a generic GenericGraphicsAdapter. For now,
  159. // the only known example of this sort of device is qxl in QEMU. For VGA
  160. // compatible devices we don't have a special driver for (e.g. ati-vga,
  161. // qxl-vga, cirrus-vga, vmware-svga in QEMU), it's much more likely that
  162. // these devices will be supported by the Multiboot loader that will
  163. // utilize VESA BIOS extensions (that we don't currently) of these cards
  164. // support, so we want to utilize the provided framebuffer of these
  165. // devices, if possible.
  166. if (!m_vga_adapter && PCI::is_io_space_enabled(device_identifier.address())) {
  167. create_bootloader_framebuffer_device();
  168. } else {
  169. dmesgln("Graphics: Using a PCI VGA compatible generic adapter");
  170. adapter = PCIVGACompatibleAdapter::initialize(device_identifier);
  171. }
  172. break;
  173. }
  174. }
  175. if (!adapter)
  176. return false;
  177. m_graphics_devices.append(*adapter);
  178. // Note: If IO space is enabled, this VGA adapter is operating in VGA mode.
  179. // Note: If no other VGA adapter is attached as m_vga_adapter, we should attach it then.
  180. if (!m_vga_adapter && PCI::is_io_space_enabled(device_identifier.address()) && adapter->vga_compatible()) {
  181. dbgln("Graphics adapter @ {} is operating in VGA mode", device_identifier.address());
  182. m_vga_adapter = static_ptr_cast<VGACompatibleAdapter>(adapter);
  183. }
  184. return true;
  185. }
  186. UNMAP_AFTER_INIT bool GraphicsManagement::initialize()
  187. {
  188. /* Explanation on the flow when not requesting to force not creating any
  189. * framebuffer devices:
  190. * If the user wants to use a Console instead of the graphical environment,
  191. * they doesn't need to request text mode.
  192. * Graphical mode might not be accessible on bare-metal hardware because
  193. * the bootloader didn't set a framebuffer and we don't have a native driver
  194. * to set a framebuffer for it. We don't have VBE modesetting capabilities
  195. * in the kernel yet, so what will happen is one of the following situations:
  196. * 1. The bootloader didn't specify settings of a pre-set framebuffer. The
  197. * kernel has a native driver for a detected display adapter, therefore
  198. * the kernel can still set a framebuffer.
  199. * 2. The bootloader specified settings of a pre-set framebuffer, and the
  200. * kernel has a native driver for a detected display adapter, therefore
  201. * the kernel can still set a framebuffer and change the settings of it.
  202. * In that situation, the kernel will simply ignore the Multiboot pre-set
  203. * framebuffer.
  204. * 2. The bootloader specified settings of a pre-set framebuffer, and the
  205. * kernel does not have a native driver for a detected display adapter,
  206. * therefore the kernel will use the pre-set framebuffer. Modesetting is not
  207. * available in this situation.
  208. * 3. The bootloader didn't specify settings of a pre-set framebuffer, and
  209. * the kernel does not have a native driver for a detected display adapter,
  210. * therefore the kernel will try to initialize a VGA text mode console.
  211. * In that situation, the kernel will assume that VGA text mode was already
  212. * initialized, but will still try to modeset it. No switching to graphical
  213. * environment is allowed in this case.
  214. *
  215. * By default, the kernel assumes that no framebuffer was created until it
  216. * was proven that there's an existing framebuffer or we can modeset the
  217. * screen resolution to create a framebuffer.
  218. *
  219. * Special cases:
  220. * 1. If the user disabled PCI access, the kernel behaves like it's running
  221. * on a pure ISA PC machine and therefore the kernel will try to initialize
  222. * a variant that is suitable for ISA VGA handling, and not PCI adapters.
  223. *
  224. * If the user requests to force no initialization of framebuffer devices
  225. * the same flow above will happen, except that no framebuffer device will
  226. * be created, so SystemServer will not try to initialize WindowServer.
  227. */
  228. auto graphics_subsystem_mode = kernel_command_line().graphics_subsystem_mode();
  229. if (graphics_subsystem_mode == CommandLine::GraphicsSubsystemMode::Disabled)
  230. return true;
  231. if (graphics_subsystem_mode == CommandLine::GraphicsSubsystemMode::Limited && !multiboot_framebuffer_addr.is_null() && multiboot_framebuffer_type != MULTIBOOT_FRAMEBUFFER_TYPE_RGB) {
  232. dmesgln("Graphics: Using a preset resolution from the bootloader, without knowing the PCI device");
  233. m_preset_resolution_generic_display_connector = GenericDisplayConnector::must_create_with_preset_resolution(
  234. multiboot_framebuffer_addr,
  235. multiboot_framebuffer_width,
  236. multiboot_framebuffer_height,
  237. multiboot_framebuffer_pitch);
  238. return true;
  239. }
  240. if (PCI::Access::is_disabled()) {
  241. determine_and_initialize_isa_graphics_device();
  242. return true;
  243. }
  244. MUST(PCI::enumerate([&](PCI::DeviceIdentifier const& device_identifier) {
  245. // Note: Each graphics controller will try to set its native screen resolution
  246. // upon creation. Later on, if we don't want to have framebuffer devices, a
  247. // framebuffer console will take the control instead.
  248. if (!is_vga_compatible_pci_device(device_identifier) && !is_display_controller_pci_device(device_identifier))
  249. return;
  250. determine_and_initialize_graphics_device(device_identifier);
  251. }));
  252. if (!m_console) {
  253. // If no graphics driver was instantiated and we had a bootloader provided
  254. // framebuffer console we can simply re-use it.
  255. if (auto* boot_console = g_boot_console.load()) {
  256. m_console = *boot_console;
  257. boot_console->unref(); // Drop the leaked reference from Kernel::init()
  258. }
  259. }
  260. if (m_graphics_devices.is_empty()) {
  261. dbgln("No graphics adapter was initialized.");
  262. return false;
  263. }
  264. return true;
  265. }
  266. void GraphicsManagement::set_console(Graphics::Console& console)
  267. {
  268. m_console = console;
  269. if (auto* boot_console = g_boot_console.exchange(nullptr)) {
  270. // Disable the initial boot framebuffer console permanently
  271. boot_console->disable();
  272. // TODO: Even though we swapped the pointer and disabled the console
  273. // we technically can't safely destroy it as other CPUs might still
  274. // try to use it. Once we solve this problem we can drop the reference
  275. // that we intentionally leaked in Kernel::init().
  276. }
  277. }
  278. }