BochsGraphicsAdapter.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <AK/Singleton.h>
  9. #include <Kernel/Debug.h>
  10. #include <Kernel/Graphics/Bochs.h>
  11. #include <Kernel/Graphics/BochsFramebufferDevice.h>
  12. #include <Kernel/Graphics/BochsGraphicsAdapter.h>
  13. #include <Kernel/Graphics/Console/FramebufferConsole.h>
  14. #include <Kernel/Graphics/GraphicsManagement.h>
  15. #include <Kernel/IO.h>
  16. #include <Kernel/PCI/Access.h>
  17. #include <Kernel/Process.h>
  18. #include <Kernel/VM/AnonymousVMObject.h>
  19. #include <Kernel/VM/MemoryManager.h>
  20. #include <Kernel/VM/TypedMapping.h>
  21. #include <LibC/errno_numbers.h>
  22. #include <LibC/sys/ioctl_numbers.h>
  23. namespace Kernel {
  24. struct [[gnu::packed]] DISPIInterface {
  25. u16 index_id;
  26. u16 xres;
  27. u16 yres;
  28. u16 bpp;
  29. u16 enable;
  30. u16 bank;
  31. u16 virt_width;
  32. u16 virt_height;
  33. u16 x_offset;
  34. u16 y_offset;
  35. };
  36. struct [[gnu::packed]] BochsDisplayMMIORegisters {
  37. u8 edid_data[0x400];
  38. u16 vga_ioports[0x10];
  39. u8 reserved[0xE0];
  40. DISPIInterface bochs_regs;
  41. };
  42. UNMAP_AFTER_INIT NonnullRefPtr<BochsGraphicsAdapter> BochsGraphicsAdapter::initialize(PCI::Address address)
  43. {
  44. return adopt_ref(*new BochsGraphicsAdapter(address));
  45. }
  46. UNMAP_AFTER_INIT BochsGraphicsAdapter::BochsGraphicsAdapter(PCI::Address pci_address)
  47. : PCI::DeviceController(pci_address)
  48. , m_mmio_registers(PCI::get_BAR2(pci_address) & 0xfffffff0)
  49. {
  50. // We assume safe resolutio is 1024x768x32
  51. m_framebuffer_console = Graphics::FramebufferConsole::initialize(PhysicalAddress(PCI::get_BAR0(pci_address) & 0xfffffff0), 1024, 768, 1024 * sizeof(u32));
  52. // FIXME: This is a very wrong way to do this...
  53. GraphicsManagement::the().m_console = m_framebuffer_console;
  54. set_safe_resolution();
  55. }
  56. UNMAP_AFTER_INIT void BochsGraphicsAdapter::initialize_framebuffer_devices()
  57. {
  58. // FIXME: Find a better way to determine default resolution...
  59. m_framebuffer_device = BochsFramebufferDevice::create(*this, PhysicalAddress(PCI::get_BAR0(pci_address()) & 0xfffffff0), 1024, 768, 1024 * sizeof(u32));
  60. m_framebuffer_device->initialize();
  61. }
  62. GraphicsDevice::Type BochsGraphicsAdapter::type() const
  63. {
  64. if (PCI::get_class(pci_address()) == 0x3 && PCI::get_subclass(pci_address()) == 0x0)
  65. return Type::VGACompatible;
  66. return Type::Bochs;
  67. }
  68. void BochsGraphicsAdapter::set_safe_resolution()
  69. {
  70. VERIFY(m_framebuffer_console);
  71. set_resolution(1024, 768);
  72. }
  73. void BochsGraphicsAdapter::set_resolution_registers(size_t width, size_t height)
  74. {
  75. dbgln_if(BXVGA_DEBUG, "BochsGraphicsAdapter resolution registers set to - {}x{}", width, height);
  76. auto registers = map_typed_writable<volatile BochsDisplayMMIORegisters>(m_mmio_registers);
  77. registers->bochs_regs.enable = VBE_DISPI_DISABLED;
  78. full_memory_barrier();
  79. registers->bochs_regs.xres = width;
  80. registers->bochs_regs.yres = height;
  81. registers->bochs_regs.virt_width = width;
  82. registers->bochs_regs.virt_height = height * 2;
  83. registers->bochs_regs.bpp = 32;
  84. full_memory_barrier();
  85. registers->bochs_regs.enable = VBE_DISPI_ENABLED | VBE_DISPI_LFB_ENABLED;
  86. full_memory_barrier();
  87. registers->bochs_regs.bank = 0;
  88. }
  89. bool BochsGraphicsAdapter::try_to_set_resolution(size_t width, size_t height)
  90. {
  91. dbgln_if(BXVGA_DEBUG, "BochsGraphicsAdapter resolution test - {}x{}", width, height);
  92. set_resolution_registers(width, height);
  93. return validate_setup_resolution(width, height);
  94. }
  95. bool BochsGraphicsAdapter::set_resolution(size_t width, size_t height)
  96. {
  97. VERIFY(m_framebuffer_console);
  98. if (Checked<size_t>::multiplication_would_overflow(width, height, sizeof(u32)))
  99. return false;
  100. if (!try_to_set_resolution(width, height))
  101. return false;
  102. dbgln("BochsGraphicsAdapter: resolution set to {}x{}", width, height);
  103. m_framebuffer_console->set_resolution(width, height, width * sizeof(u32));
  104. return true;
  105. }
  106. bool BochsGraphicsAdapter::validate_setup_resolution(size_t width, size_t height)
  107. {
  108. auto registers = map_typed_writable<volatile BochsDisplayMMIORegisters>(m_mmio_registers);
  109. if ((u16)width != registers->bochs_regs.xres || (u16)height != registers->bochs_regs.yres) {
  110. return false;
  111. }
  112. return true;
  113. }
  114. void BochsGraphicsAdapter::set_y_offset(size_t y_offset)
  115. {
  116. if (m_console_enabled)
  117. return;
  118. auto registers = map_typed_writable<volatile BochsDisplayMMIORegisters>(m_mmio_registers);
  119. registers->bochs_regs.y_offset = y_offset;
  120. }
  121. void BochsGraphicsAdapter::enable_consoles()
  122. {
  123. ScopedSpinLock lock(m_console_mode_switch_lock);
  124. VERIFY(m_framebuffer_console);
  125. m_console_enabled = true;
  126. auto registers = map_typed_writable<volatile BochsDisplayMMIORegisters>(m_mmio_registers);
  127. registers->bochs_regs.y_offset = 0;
  128. if (m_framebuffer_device)
  129. m_framebuffer_device->deactivate_writes();
  130. m_framebuffer_console->enable();
  131. }
  132. void BochsGraphicsAdapter::disable_consoles()
  133. {
  134. ScopedSpinLock lock(m_console_mode_switch_lock);
  135. VERIFY(m_framebuffer_console);
  136. VERIFY(m_framebuffer_device);
  137. m_console_enabled = false;
  138. auto registers = map_typed_writable<volatile BochsDisplayMMIORegisters>(m_mmio_registers);
  139. registers->bochs_regs.y_offset = 0;
  140. m_framebuffer_console->disable();
  141. m_framebuffer_device->activate_writes();
  142. }
  143. }