1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-24 01:55:06 +00:00
serenity/Kernel/Graphics/VMWare/GraphicsAdapter.cpp
Liav A 1f9d3a3523 Kernel/PCI: Hold a reference to DeviceIdentifier in the Device class
There are now 2 separate classes for almost the same object type:
- EnumerableDeviceIdentifier, which is used in the enumeration code for
  all PCI host controller classes. This is allowed to be moved and
  copied, as it doesn't support ref-counting.
- DeviceIdentifier, which inherits from EnumerableDeviceIdentifier. This
  class uses ref-counting, and is not allowed to be copied. It has a
  spinlock member in its structure to allow safely executing complicated
  IO sequences on a PCI device and its space configuration.
  There's a static method that allows a quick conversion from
  EnumerableDeviceIdentifier to DeviceIdentifier while creating a
  NonnullRefPtr out of it.

The reason for doing this is for the sake of integrity and reliablity of
the system in 2 places:
- Ensure that "complicated" tasks that rely on manipulating PCI device
  registers are done in a safe manner. For example, determining a PCI
  BAR space size requires multiple read and writes to the same register,
  and if another CPU tries to do something else with our selected
  register, then the result will be a catastrophe.
- Allow the PCI API to have a united form around a shared object which
  actually holds much more data than the PCI::Address structure. This is
  fundamental if we want to do certain types of optimizations, and be
  able to support more features of the PCI bus in the foreseeable
  future.

This patch already has several implications:
- All PCI::Device(s) hold a reference to a DeviceIdentifier structure
  being given originally from the PCI::Access singleton. This means that
  all instances of DeviceIdentifier structures are located in one place,
  and all references are pointing to that location. This ensures that
  locking the operation spinlock will take effect in all the appropriate
  places.
- We no longer support adding PCI host controllers and then immediately
  allow for enumerating it with a lambda function. It was found that
  this method is extremely broken and too much complicated to work
  reliably with the new paradigm being introduced in this patch. This
  means that for Volume Management Devices (Intel VMD devices), we
  simply first enumerate the PCI bus for such devices in the storage
  code, and if we find a device, we attach it in the PCI::Access method
  which will scan for devices behind that bridge and will add new
  DeviceIdentifier(s) objects to its internal Vector. Afterwards, we
  just continue as usual with scanning for actual storage controllers,
  so we will find a corresponding NVMe controllers if there were any
  behind that VMD bridge.
2023-01-26 23:04:26 +01:00

193 lines
8 KiB
C++

/*
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Atomic.h>
#include <AK/Checked.h>
#include <AK/Try.h>
#include <Kernel/Bus/PCI/API.h>
#include <Kernel/Bus/PCI/IDs.h>
#include <Kernel/Graphics/Console/ContiguousFramebufferConsole.h>
#include <Kernel/Graphics/GraphicsManagement.h>
#include <Kernel/Graphics/VMWare/Definitions.h>
#include <Kernel/Graphics/VMWare/DisplayConnector.h>
#include <Kernel/Graphics/VMWare/GraphicsAdapter.h>
#include <Kernel/IOWindow.h>
#include <Kernel/Memory/TypedMapping.h>
#include <Kernel/Sections.h>
namespace Kernel {
ErrorOr<bool> VMWareGraphicsAdapter::probe(PCI::DeviceIdentifier const& pci_device_identifier)
{
PCI::HardwareID id = pci_device_identifier.hardware_id();
// Note: We only support VMWare SVGA II adapter
return id.vendor_id == PCI::VendorID::VMWare && id.device_id == 0x0405;
}
ErrorOr<NonnullLockRefPtr<GenericGraphicsAdapter>> VMWareGraphicsAdapter::create(PCI::DeviceIdentifier const& pci_device_identifier)
{
auto registers_io_window = TRY(IOWindow::create_for_pci_device_bar(pci_device_identifier, PCI::HeaderType0BaseRegister::BAR0));
auto adapter = TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) VMWareGraphicsAdapter(pci_device_identifier, move(registers_io_window))));
TRY(adapter->initialize_adapter());
return adapter;
}
UNMAP_AFTER_INIT VMWareGraphicsAdapter::VMWareGraphicsAdapter(PCI::DeviceIdentifier const& pci_device_identifier, NonnullOwnPtr<IOWindow> registers_io_window)
: PCI::Device(const_cast<PCI::DeviceIdentifier&>(pci_device_identifier))
, m_registers_io_window(move(registers_io_window))
{
dbgln("VMWare SVGA @ {}, {}", pci_device_identifier.address(), m_registers_io_window);
}
u32 VMWareGraphicsAdapter::read_io_register(VMWareDisplayRegistersOffset register_offset) const
{
SpinlockLocker locker(m_io_access_lock);
m_registers_io_window->write32(0, to_underlying(register_offset));
return m_registers_io_window->read32_unaligned(1);
}
void VMWareGraphicsAdapter::write_io_register(VMWareDisplayRegistersOffset register_offset, u32 value)
{
SpinlockLocker locker(m_io_access_lock);
m_registers_io_window->write32(0, to_underlying(register_offset));
m_registers_io_window->write32_unaligned(1, value);
}
UNMAP_AFTER_INIT ErrorOr<void> VMWareGraphicsAdapter::negotiate_device_version()
{
write_io_register(VMWareDisplayRegistersOffset::ID, vmware_svga_version_2_id);
auto accepted_version = read_io_register(VMWareDisplayRegistersOffset::ID);
dbgln("VMWare SVGA @ {}: Accepted version {}", device_identifier().address(), accepted_version);
if (read_io_register(VMWareDisplayRegistersOffset::ID) == vmware_svga_version_2_id)
return {};
return Error::from_errno(ENOTSUP);
}
UNMAP_AFTER_INIT ErrorOr<void> VMWareGraphicsAdapter::initialize_fifo_registers()
{
auto framebuffer_size = read_io_register(VMWareDisplayRegistersOffset::FB_SIZE);
auto fifo_size = read_io_register(VMWareDisplayRegistersOffset::MEM_SIZE);
auto fifo_physical_address = PhysicalAddress(PCI::get_BAR2(device_identifier()) & 0xfffffff0);
dbgln("VMWare SVGA @ {}: framebuffer size {} bytes, FIFO size {} bytes @ {}", device_identifier().address(), framebuffer_size, fifo_size, fifo_physical_address);
if (framebuffer_size < 0x100000 || fifo_size < 0x10000) {
dbgln("VMWare SVGA @ {}: invalid framebuffer or fifo size", device_identifier().address());
return Error::from_errno(ENOTSUP);
}
m_fifo_registers = TRY(Memory::map_typed<VMWareDisplayFIFORegisters volatile>(fifo_physical_address, fifo_size, Memory::Region::Access::ReadWrite));
m_fifo_registers->start = 16;
m_fifo_registers->size = 16 + (10 * 1024);
m_fifo_registers->next_command = 16;
m_fifo_registers->stop = 16;
return {};
}
UNMAP_AFTER_INIT void VMWareGraphicsAdapter::print_svga_capabilities() const
{
auto svga_capabilities = read_io_register(VMWareDisplayRegistersOffset::CAPABILITIES);
dbgln("VMWare SVGA capabilities (raw {:x}):", svga_capabilities);
if (svga_capabilities & (1 << 1))
dbgln("\tRect copy");
if (svga_capabilities & (1 << 5))
dbgln("\tCursor");
if (svga_capabilities & (1 << 6))
dbgln("\tCursor Bypass");
if (svga_capabilities & (1 << 7))
dbgln("\tCursor Bypass 2");
if (svga_capabilities & (1 << 8))
dbgln("\t8 Bit emulation");
if (svga_capabilities & (1 << 9))
dbgln("\tAlpha Cursor");
if (svga_capabilities & (1 << 14))
dbgln("\t3D acceleration");
if (svga_capabilities & (1 << 15))
dbgln("\tExtended FIFO");
if (svga_capabilities & (1 << 16))
dbgln("\tMulti-monitor (legacy)");
if (svga_capabilities & (1 << 17))
dbgln("\tPitch lock");
if (svga_capabilities & (1 << 18))
dbgln("\tIRQ masking");
if (svga_capabilities & (1 << 19))
dbgln("\tDisplay topology");
if (svga_capabilities & (1 << 20))
dbgln("\tGMR");
if (svga_capabilities & (1 << 21))
dbgln("\tTraces");
if (svga_capabilities & (1 << 22))
dbgln("\tGMR2");
if (svga_capabilities & (1 << 23))
dbgln("\tScreen object 2");
}
ErrorOr<void> VMWareGraphicsAdapter::modeset_primary_screen_resolution(Badge<VMWareDisplayConnector>, size_t width, size_t height)
{
auto max_width = read_io_register(VMWareDisplayRegistersOffset::MAX_WIDTH);
auto max_height = read_io_register(VMWareDisplayRegistersOffset::MAX_HEIGHT);
if (width > max_width || height > max_height)
return Error::from_errno(ENOTSUP);
modeset_primary_screen_resolution(width, height);
return {};
}
size_t VMWareGraphicsAdapter::primary_screen_width(Badge<VMWareDisplayConnector>) const
{
SpinlockLocker locker(m_operation_lock);
return read_io_register(VMWareDisplayRegistersOffset::WIDTH);
}
size_t VMWareGraphicsAdapter::primary_screen_height(Badge<VMWareDisplayConnector>) const
{
SpinlockLocker locker(m_operation_lock);
return read_io_register(VMWareDisplayRegistersOffset::HEIGHT);
}
size_t VMWareGraphicsAdapter::primary_screen_pitch(Badge<VMWareDisplayConnector>) const
{
SpinlockLocker locker(m_operation_lock);
return read_io_register(VMWareDisplayRegistersOffset::BYTES_PER_LINE);
}
void VMWareGraphicsAdapter::primary_screen_flush(Badge<VMWareDisplayConnector>, size_t current_width, size_t current_height)
{
SpinlockLocker locker(m_operation_lock);
m_fifo_registers->start = 16;
m_fifo_registers->size = 16 + (10 * 1024);
m_fifo_registers->next_command = 16 + 4 * 5;
m_fifo_registers->stop = 16;
m_fifo_registers->commands[0] = 1;
m_fifo_registers->commands[1] = 0;
m_fifo_registers->commands[2] = 0;
m_fifo_registers->commands[3] = current_width;
m_fifo_registers->commands[4] = current_height;
write_io_register(VMWareDisplayRegistersOffset::SYNC, 1);
}
void VMWareGraphicsAdapter::modeset_primary_screen_resolution(size_t width, size_t height)
{
SpinlockLocker locker(m_operation_lock);
write_io_register(VMWareDisplayRegistersOffset::ENABLE, 0);
write_io_register(VMWareDisplayRegistersOffset::WIDTH, width);
write_io_register(VMWareDisplayRegistersOffset::HEIGHT, height);
write_io_register(VMWareDisplayRegistersOffset::BITS_PER_PIXEL, 32);
write_io_register(VMWareDisplayRegistersOffset::ENABLE, 1);
write_io_register(VMWareDisplayRegistersOffset::CONFIG_DONE, 1);
}
UNMAP_AFTER_INIT ErrorOr<void> VMWareGraphicsAdapter::initialize_adapter()
{
TRY(negotiate_device_version());
print_svga_capabilities();
TRY(initialize_fifo_registers());
// Note: enable the device by modesetting the primary screen resolution
modeset_primary_screen_resolution(640, 480);
auto bar1_space_size = PCI::get_BAR_space_size(device_identifier(), PCI::HeaderType0BaseRegister::BAR1);
m_display_connector = VMWareDisplayConnector::must_create(*this, PhysicalAddress(PCI::get_BAR1(device_identifier()) & 0xfffffff0), bar1_space_size);
TRY(m_display_connector->set_safe_mode_setting());
return {};
}
}