GraphicsManagement.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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/GraphicsManagement.h>
  13. #include <Kernel/Graphics/Intel/NativeGraphicsAdapter.h>
  14. #include <Kernel/Graphics/VGACompatibleAdapter.h>
  15. #include <Kernel/Graphics/VirtIOGPU/GraphicsAdapter.h>
  16. #include <Kernel/Memory/AnonymousVMObject.h>
  17. #include <Kernel/Multiboot.h>
  18. #include <Kernel/Sections.h>
  19. namespace Kernel {
  20. static Singleton<GraphicsManagement> s_the;
  21. GraphicsManagement& GraphicsManagement::the()
  22. {
  23. return *s_the;
  24. }
  25. bool GraphicsManagement::is_initialized()
  26. {
  27. return s_the.is_initialized();
  28. }
  29. UNMAP_AFTER_INIT GraphicsManagement::GraphicsManagement()
  30. {
  31. }
  32. bool GraphicsManagement::framebuffer_devices_use_bootloader_framebuffer() const
  33. {
  34. return kernel_command_line().are_framebuffer_devices_enabled() == CommandLine::FrameBufferDevices::BootloaderOnly;
  35. }
  36. bool GraphicsManagement::framebuffer_devices_console_only() const
  37. {
  38. return kernel_command_line().are_framebuffer_devices_enabled() == CommandLine::FrameBufferDevices::ConsoleOnly;
  39. }
  40. void GraphicsManagement::deactivate_graphical_mode()
  41. {
  42. for (auto& graphics_device : m_graphics_devices) {
  43. graphics_device.enable_consoles();
  44. }
  45. }
  46. void GraphicsManagement::activate_graphical_mode()
  47. {
  48. for (auto& graphics_device : m_graphics_devices) {
  49. graphics_device.disable_consoles();
  50. }
  51. }
  52. static inline bool is_vga_compatible_pci_device(PCI::DeviceIdentifier const& device_identifier)
  53. {
  54. // Note: Check for Display Controller, VGA Compatible Controller or
  55. // Unclassified, VGA-Compatible Unclassified Device
  56. auto is_display_controller_vga_compatible = device_identifier.class_code().value() == 0x3 && device_identifier.subclass_code().value() == 0x0;
  57. auto is_general_pci_vga_compatible = device_identifier.class_code().value() == 0x0 && device_identifier.subclass_code().value() == 0x1;
  58. return is_display_controller_vga_compatible || is_general_pci_vga_compatible;
  59. }
  60. static inline bool is_display_controller_pci_device(PCI::DeviceIdentifier const& device_identifier)
  61. {
  62. return device_identifier.class_code().value() == 0x3;
  63. }
  64. UNMAP_AFTER_INIT bool GraphicsManagement::determine_and_initialize_graphics_device(PCI::DeviceIdentifier const& device_identifier)
  65. {
  66. VERIFY(is_vga_compatible_pci_device(device_identifier) || is_display_controller_pci_device(device_identifier));
  67. auto add_and_configure_adapter = [&](GenericGraphicsAdapter& graphics_device) {
  68. m_graphics_devices.append(graphics_device);
  69. if (framebuffer_devices_console_only()) {
  70. graphics_device.enable_consoles();
  71. return;
  72. }
  73. graphics_device.initialize_framebuffer_devices();
  74. };
  75. RefPtr<GenericGraphicsAdapter> adapter;
  76. auto create_bootloader_framebuffer_device = [&]() {
  77. if (multiboot_framebuffer_type == MULTIBOOT_FRAMEBUFFER_TYPE_RGB) {
  78. dmesgln("Graphics: Using a preset resolution from the bootloader");
  79. adapter = VGACompatibleAdapter::initialize_with_preset_resolution(device_identifier,
  80. multiboot_framebuffer_addr,
  81. multiboot_framebuffer_width,
  82. multiboot_framebuffer_height,
  83. multiboot_framebuffer_pitch);
  84. }
  85. };
  86. if (framebuffer_devices_use_bootloader_framebuffer())
  87. create_bootloader_framebuffer_device();
  88. if (!adapter) {
  89. switch (device_identifier.hardware_id().vendor_id) {
  90. case PCI::VendorID::QEMUOld:
  91. if (device_identifier.hardware_id().device_id == 0x1111)
  92. adapter = BochsGraphicsAdapter::initialize(device_identifier);
  93. break;
  94. case PCI::VendorID::VirtualBox:
  95. if (device_identifier.hardware_id().device_id == 0xbeef)
  96. adapter = BochsGraphicsAdapter::initialize(device_identifier);
  97. break;
  98. case PCI::VendorID::Intel:
  99. adapter = IntelNativeGraphicsAdapter::initialize(device_identifier);
  100. break;
  101. case PCI::VendorID::VirtIO:
  102. dmesgln("Graphics: Using VirtIO console");
  103. adapter = Graphics::VirtIOGPU::GraphicsAdapter::initialize(device_identifier);
  104. break;
  105. default:
  106. if (!is_vga_compatible_pci_device(device_identifier))
  107. break;
  108. // Note: Although technically possible that a system has a
  109. // non-compatible VGA graphics device that was initialized by the
  110. // Multiboot bootloader to provide a framebuffer, in practice we
  111. // probably want to support these devices natively instead of
  112. // initializing them as some sort of a generic GenericGraphicsAdapter. For now,
  113. // the only known example of this sort of device is qxl in QEMU. For VGA
  114. // compatible devices we don't have a special driver for (e.g. ati-vga,
  115. // qxl-vga, cirrus-vga, vmware-svga in QEMU), it's much more likely that
  116. // these devices will be supported by the Multiboot loader that will
  117. // utilize VESA BIOS extensions (that we don't currently) of these cards
  118. // support, so we want to utilize the provided framebuffer of these
  119. // devices, if possible.
  120. if (!m_vga_adapter && PCI::is_io_space_enabled(device_identifier.address())) {
  121. create_bootloader_framebuffer_device();
  122. } else {
  123. dmesgln("Graphics: Using a VGA compatible generic adapter");
  124. adapter = VGACompatibleAdapter::initialize(device_identifier);
  125. }
  126. break;
  127. }
  128. }
  129. if (!adapter)
  130. return false;
  131. add_and_configure_adapter(*adapter);
  132. // Note: If IO space is enabled, this VGA adapter is operating in VGA mode.
  133. // Note: If no other VGA adapter is attached as m_vga_adapter, we should attach it then.
  134. if (!m_vga_adapter && PCI::is_io_space_enabled(device_identifier.address()) && adapter->vga_compatible()) {
  135. dbgln("Graphics adapter @ {} is operating in VGA mode", device_identifier.address());
  136. m_vga_adapter = static_ptr_cast<VGACompatibleAdapter>(adapter);
  137. }
  138. return true;
  139. }
  140. UNMAP_AFTER_INIT bool GraphicsManagement::initialize()
  141. {
  142. /* Explanation on the flow when not requesting to force not creating any
  143. * framebuffer devices:
  144. * If the user wants to use a Console instead of the graphical environment,
  145. * they doesn't need to request text mode.
  146. * Graphical mode might not be accessible on bare-metal hardware because
  147. * the bootloader didn't set a framebuffer and we don't have a native driver
  148. * to set a framebuffer for it. We don't have VBE modesetting capabilities
  149. * in the kernel yet, so what will happen is one of the following situations:
  150. * 1. The bootloader didn't specify settings of a pre-set framebuffer. The
  151. * kernel has a native driver for a detected display adapter, therefore
  152. * the kernel can still set a framebuffer.
  153. * 2. The bootloader specified settings of a pre-set framebuffer, and the
  154. * kernel has a native driver for a detected display adapter, therefore
  155. * the kernel can still set a framebuffer and change the settings of it.
  156. * In that situation, the kernel will simply ignore the Multiboot pre-set
  157. * framebuffer.
  158. * 2. The bootloader specified settings of a pre-set framebuffer, and the
  159. * kernel does not have a native driver for a detected display adapter,
  160. * therefore the kernel will use the pre-set framebuffer. Modesetting is not
  161. * available in this situation.
  162. * 3. The bootloader didn't specify settings of a pre-set framebuffer, and
  163. * the kernel does not have a native driver for a detected display adapter,
  164. * therefore the kernel will try to initialize a VGA text mode console.
  165. * In that situation, the kernel will assume that VGA text mode was already
  166. * initialized, but will still try to modeset it. No switching to graphical
  167. * environment is allowed in this case.
  168. *
  169. * By default, the kernel assumes that no framebuffer was created until it
  170. * was proven that there's an existing framebuffer or we can modeset the
  171. * screen resolution to create a framebuffer.
  172. *
  173. * If the user requests to force no initialization of framebuffer devices
  174. * the same flow above will happen, except that no framebuffer device will
  175. * be created, so SystemServer will not try to initialize WindowServer.
  176. */
  177. if (framebuffer_devices_console_only())
  178. dbgln("Forcing non-initialization of framebuffer devices (console only)");
  179. else if (framebuffer_devices_use_bootloader_framebuffer())
  180. dbgln("Forcing use of framebuffer set up by the bootloader");
  181. PCI::enumerate([&](PCI::DeviceIdentifier const& device_identifier) {
  182. // Note: Each graphics controller will try to set its native screen resolution
  183. // upon creation. Later on, if we don't want to have framebuffer devices, a
  184. // framebuffer console will take the control instead.
  185. if (!is_vga_compatible_pci_device(device_identifier) && !is_display_controller_pci_device(device_identifier))
  186. return;
  187. determine_and_initialize_graphics_device(device_identifier);
  188. });
  189. if (m_graphics_devices.is_empty()) {
  190. dbgln("No graphics adapter was initialized.");
  191. return false;
  192. }
  193. return true;
  194. }
  195. bool GraphicsManagement::framebuffer_devices_exist() const
  196. {
  197. for (auto& graphics_device : m_graphics_devices) {
  198. if (graphics_device.framebuffer_devices_initialized())
  199. return true;
  200. }
  201. return false;
  202. }
  203. }