Serenity Operating System
1/*
2 * Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <Kernel/Bus/PCI/API.h>
8#include <Kernel/Graphics/Console/ContiguousFramebufferConsole.h>
9#include <Kernel/Graphics/Definitions.h>
10#include <Kernel/Graphics/GraphicsManagement.h>
11#include <Kernel/Graphics/Intel/NativeGraphicsAdapter.h>
12#include <Kernel/PhysicalAddress.h>
13
14namespace Kernel {
15
16static constexpr u16 supported_models[] {
17 0x29c2, // Intel G35 Adapter
18};
19
20static bool is_supported_model(u16 device_id)
21{
22 for (auto& id : supported_models) {
23 if (id == device_id)
24 return true;
25 }
26 return false;
27}
28
29ErrorOr<bool> IntelNativeGraphicsAdapter::probe(PCI::DeviceIdentifier const& pci_device_identifier)
30{
31 return is_supported_model(pci_device_identifier.hardware_id().device_id);
32}
33
34ErrorOr<NonnullLockRefPtr<GenericGraphicsAdapter>> IntelNativeGraphicsAdapter::create(PCI::DeviceIdentifier const& pci_device_identifier)
35{
36 auto adapter = TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) IntelNativeGraphicsAdapter(pci_device_identifier)));
37 TRY(adapter->initialize_adapter());
38 return adapter;
39}
40
41ErrorOr<void> IntelNativeGraphicsAdapter::initialize_adapter()
42{
43 dbgln_if(INTEL_GRAPHICS_DEBUG, "Intel Native Graphics Adapter @ {}", device_identifier().address());
44 auto bar0_space_size = PCI::get_BAR_space_size(device_identifier(), PCI::HeaderType0BaseRegister::BAR0);
45 auto bar2_space_size = PCI::get_BAR_space_size(device_identifier(), PCI::HeaderType0BaseRegister::BAR2);
46 dmesgln_pci(*this, "MMIO @ {}, space size is {:x} bytes", PhysicalAddress(PCI::get_BAR0(device_identifier())), bar0_space_size);
47 dmesgln_pci(*this, "framebuffer @ {}", PhysicalAddress(PCI::get_BAR2(device_identifier())));
48
49 using MMIORegion = IntelDisplayConnectorGroup::MMIORegion;
50 MMIORegion first_region { MMIORegion::BARAssigned::BAR0, PhysicalAddress(PCI::get_BAR0(device_identifier()) & 0xfffffff0), bar0_space_size };
51 MMIORegion second_region { MMIORegion::BARAssigned::BAR2, PhysicalAddress(PCI::get_BAR2(device_identifier()) & 0xfffffff0), bar2_space_size };
52
53 PCI::enable_bus_mastering(device_identifier());
54 PCI::enable_io_space(device_identifier());
55 PCI::enable_memory_space(device_identifier());
56
57 switch (device_identifier().hardware_id().device_id) {
58 case 0x29c2:
59 m_connector_group = TRY(IntelDisplayConnectorGroup::try_create({}, IntelGraphics::Generation::Gen4, first_region, second_region));
60 return {};
61 default:
62 return Error::from_errno(ENODEV);
63 }
64}
65
66IntelNativeGraphicsAdapter::IntelNativeGraphicsAdapter(PCI::DeviceIdentifier const& pci_device_identifier)
67 : GenericGraphicsAdapter()
68 , PCI::Device(const_cast<PCI::DeviceIdentifier&>(pci_device_identifier))
69{
70}
71
72}