GraphicsManagement.cpp 8.9 KB

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