GraphicsManagement.cpp 8.5 KB

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