forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kernel: Much improved BochsVGA (BXVGA) support.
Instead of cowboy-calling the VESA BIOS in the bootloader, find the emulator VGA adapter by scanning the PCI bus. Then set up the desired video mode by sending device commands.
- Loading branch information
1 parent
e9f6508
commit 731fc5a
Showing
16 changed files
with
298 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,6 @@ | |
*.creator.user.* | ||
*.files | ||
*.includes | ||
*.cflags | ||
*.cxxflags | ||
Patches |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include <Kernel/BochsVGADevice.h> | ||
#include <Kernel/IO.h> | ||
#include <Kernel/PCI.h> | ||
|
||
#define VBE_DISPI_IOPORT_INDEX 0x01CE | ||
#define VBE_DISPI_IOPORT_DATA 0x01CF | ||
|
||
#define VBE_DISPI_INDEX_ID 0x0 | ||
#define VBE_DISPI_INDEX_XRES 0x1 | ||
#define VBE_DISPI_INDEX_YRES 0x2 | ||
#define VBE_DISPI_INDEX_BPP 0x3 | ||
#define VBE_DISPI_INDEX_ENABLE 0x4 | ||
#define VBE_DISPI_INDEX_BANK 0x5 | ||
#define VBE_DISPI_INDEX_VIRT_WIDTH 0x6 | ||
#define VBE_DISPI_INDEX_VIRT_HEIGHT 0x7 | ||
#define VBE_DISPI_DISABLED 0x00 | ||
#define VBE_DISPI_ENABLED 0x01 | ||
#define VBE_DISPI_LFB_ENABLED 0x40 | ||
|
||
static BochsVGADevice* s_the; | ||
|
||
BochsVGADevice& BochsVGADevice::the() | ||
{ | ||
return *s_the; | ||
} | ||
|
||
void BochsVGADevice::initialize_statics() | ||
{ | ||
s_the = nullptr; | ||
} | ||
|
||
BochsVGADevice::BochsVGADevice() | ||
{ | ||
s_the = this; | ||
m_framebuffer_address = PhysicalAddress(find_framebuffer_address()); | ||
} | ||
|
||
void BochsVGADevice::set_register(word index, word data) | ||
{ | ||
IO::out16(VBE_DISPI_IOPORT_INDEX, index); | ||
IO::out16(VBE_DISPI_IOPORT_DATA, data); | ||
} | ||
|
||
void BochsVGADevice::set_resolution(int width, int height) | ||
{ | ||
set_register(VBE_DISPI_INDEX_ENABLE, VBE_DISPI_DISABLED); | ||
set_register(VBE_DISPI_INDEX_XRES, width); | ||
set_register(VBE_DISPI_INDEX_YRES, height); | ||
set_register(VBE_DISPI_INDEX_VIRT_WIDTH, width); | ||
set_register(VBE_DISPI_INDEX_VIRT_HEIGHT, height); | ||
set_register(VBE_DISPI_INDEX_BPP, 32); | ||
set_register(VBE_DISPI_INDEX_ENABLE, VBE_DISPI_ENABLED | VBE_DISPI_LFB_ENABLED); | ||
set_register(VBE_DISPI_INDEX_BANK, 0); | ||
} | ||
|
||
dword BochsVGADevice::find_framebuffer_address() | ||
{ | ||
static const PCI::ID bochs_vga_id = { 0x1234, 0x1111 }; | ||
static const PCI::ID virtualbox_vga_id = { 0x80ee, 0xbeef }; | ||
dword framebuffer_address = 0; | ||
PCI::enumerate_all([&framebuffer_address] (const PCI::Address& address, PCI::ID id) { | ||
if (id == bochs_vga_id || id == virtualbox_vga_id) { | ||
framebuffer_address = PCI::get_BAR0(address) & 0xfffffff0; | ||
kprintf("BochsVGA framebuffer @ PCI %w:%w BAR0=%x\n", id.vendor_id, id.device_id, framebuffer_address); | ||
} | ||
}); | ||
return framebuffer_address; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#pragma once | ||
|
||
#include <AK/Types.h> | ||
#include <AK/AKString.h> | ||
#include <Kernel/types.h> | ||
|
||
// FIXME: This should be a BlockDevice once we have BlockDevice. | ||
|
||
class BochsVGADevice { | ||
AK_MAKE_ETERNAL | ||
public: | ||
static BochsVGADevice& the(); | ||
static void initialize_statics(); | ||
|
||
BochsVGADevice(); | ||
|
||
PhysicalAddress framebuffer_address() const { return m_framebuffer_address; } | ||
void set_resolution(int width, int height); | ||
|
||
private: | ||
void set_register(word index, word value); | ||
dword find_framebuffer_address(); | ||
|
||
PhysicalAddress m_framebuffer_address; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#include <Kernel/PCI.h> | ||
#include <Kernel/IO.h> | ||
|
||
#define PCI_VENDOR_ID 0x00 // word | ||
#define PCI_DEVICE_ID 0x02 // word | ||
#define PCI_COMMAND 0x04 // word | ||
#define PCI_STATUS 0x06 // word | ||
#define PCI_REVISION_ID 0x08 // byte | ||
#define PCI_PROG_IF 0x09 // byte | ||
#define PCI_SUBCLASS 0x0a // byte | ||
#define PCI_CLASS 0x0b // byte | ||
#define PCI_CACHE_LINE_SIZE 0x0c // byte | ||
#define PCI_LATENCY_TIMER 0x0d // byte | ||
#define PCI_HEADER_TYPE 0x0e // byte | ||
#define PCI_BIST 0x0f // byte | ||
#define PCI_BAR0 0x10 // dword | ||
#define PCI_BAR1 0x14 // dword | ||
#define PCI_BAR2 0x18 // dword | ||
#define PCI_BAR3 0x1C // dword | ||
#define PCI_BAR4 0x20 // dword | ||
#define PCI_BAR5 0x24 // dword | ||
#define PCI_INTERRUPT_LINE 0x3C // byte | ||
#define PCI_SECONDARY_BUS 0x19 // byte | ||
#define PCI_HEADER_TYPE_DEVICE 0 | ||
#define PCI_HEADER_TYPE_BRIDGE 1 | ||
#define PCI_TYPE_BRIDGE 0x0604 | ||
#define PCI_ADDRESS_PORT 0xCF8 | ||
#define PCI_VALUE_PORT 0xCFC | ||
#define PCI_NONE 0xFFFF | ||
|
||
namespace PCI { | ||
|
||
template<typename T> | ||
T read_field(Address address, dword field) | ||
{ | ||
IO::out32(PCI_ADDRESS_PORT, address.io_address_for_field(field)); | ||
if constexpr (sizeof(T) == 4) | ||
return IO::in32(PCI_VALUE_PORT); | ||
if constexpr (sizeof(T) == 2) | ||
return IO::in16(PCI_VALUE_PORT + (field & 2)); | ||
if constexpr (sizeof(T) == 1) | ||
return IO::in8(PCI_VALUE_PORT + (field & 3)); | ||
} | ||
|
||
template<typename T> | ||
void write_field(Address address, dword field, T value) | ||
{ | ||
IO::out32(PCI_ADDRESS_PORT, address.io_address_for_field(field)); | ||
if constexpr (sizeof(T) == 4) | ||
IO::out32(PCI_VALUE_PORT, value); | ||
if constexpr (sizeof(T) == 2) | ||
IO::out16(PCI_VALUE_PORT + (field & 2), value); | ||
if constexpr (sizeof(T) == 1) | ||
IO::out8(PCI_VALUE_PORT + (field & 3), value); | ||
} | ||
|
||
word read_type(Address address) | ||
{ | ||
return (read_field<byte>(address, PCI_CLASS) << 8u) | read_field<byte>(address, PCI_SUBCLASS); | ||
} | ||
|
||
void enumerate_bus(int type, byte bus, Function<void(Address, ID)>&); | ||
|
||
void enumerate_functions(int type, byte bus, byte slot, byte function, Function<void(Address, ID)>& callback) | ||
{ | ||
Address address(bus, slot, function); | ||
if (type == -1 || type == read_type(address)) | ||
callback(address, { read_field<word>(address, PCI_VENDOR_ID), read_field<word>(address, PCI_DEVICE_ID) }); | ||
if (read_type(address) == PCI_TYPE_BRIDGE) { | ||
byte secondary_bus = read_field<byte>(address, PCI_SECONDARY_BUS); | ||
kprintf("PCI: Found secondary bus: %u\n", secondary_bus); | ||
ASSERT(secondary_bus != bus); | ||
enumerate_bus(type, secondary_bus, callback); | ||
} | ||
} | ||
|
||
void enumerate_slot(int type, byte bus, byte slot, Function<void(Address, ID)>& callback) | ||
{ | ||
Address address(bus, slot, 0); | ||
if (read_field<word>(address, PCI_VENDOR_ID) == PCI_NONE) | ||
return; | ||
enumerate_functions(type, bus, slot, 0, callback); | ||
if (!(read_field<byte>(address, PCI_HEADER_TYPE) & 0x80)) | ||
return; | ||
for (byte function = 1; function < 8; ++function) { | ||
Address address(bus, slot, function); | ||
if (read_field<word>(address, PCI_VENDOR_ID) != PCI_NONE) | ||
enumerate_functions(type, bus, slot, function, callback); | ||
} | ||
} | ||
|
||
void enumerate_bus(int type, byte bus, Function<void(Address, ID)>& callback) | ||
{ | ||
for (byte slot = 0; slot < 32; ++slot) | ||
enumerate_slot(type, bus, slot, callback); | ||
} | ||
|
||
dword get_BAR0(Address address) { return read_field<dword>(address, PCI_BAR0); } | ||
dword get_BAR1(Address address) { return read_field<dword>(address, PCI_BAR1); } | ||
dword get_BAR2(Address address) { return read_field<dword>(address, PCI_BAR2); } | ||
dword get_BAR3(Address address) { return read_field<dword>(address, PCI_BAR3); } | ||
dword get_BAR4(Address address) { return read_field<dword>(address, PCI_BAR4); } | ||
dword get_BAR5(Address address) { return read_field<dword>(address, PCI_BAR5); } | ||
|
||
void enumerate_all(Function<void(Address, ID)> callback) | ||
{ | ||
// Single PCI host controller. | ||
if ((read_field<byte>(Address(), PCI_HEADER_TYPE) & 0x80) == 0) { | ||
enumerate_bus(-1, 0, callback); | ||
return; | ||
} | ||
|
||
// Multiple PCI host controllers. | ||
for (byte function = 0; function < 8; ++function) { | ||
if (read_field<word>(Address(0, 0, function), PCI_VENDOR_ID) == PCI_NONE) | ||
break; | ||
enumerate_bus(-1, function, callback); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#pragma once | ||
|
||
#include <AK/Function.h> | ||
#include <AK/Types.h> | ||
|
||
namespace PCI { | ||
|
||
struct ID { | ||
word vendor_id { 0 }; | ||
word device_id { 0 }; | ||
|
||
bool operator==(const ID& other) const | ||
{ | ||
return vendor_id == other.vendor_id && device_id == other.device_id; | ||
} | ||
}; | ||
|
||
struct Address { | ||
Address() { } | ||
Address(byte bus, byte slot, byte function) : m_bus(bus), m_slot(slot), m_function(function) { } | ||
|
||
bool is_null() const { return !m_bus && !m_slot && !m_function; } | ||
operator bool() const { return !is_null(); } | ||
|
||
byte bus() const { return m_bus; } | ||
byte slot() const { return m_slot; } | ||
byte function() const { return m_function; } | ||
|
||
dword io_address_for_field(byte field) const | ||
{ | ||
return 0x80000000u | (m_bus << 16u) | (m_slot << 11u) | (m_function << 8u) | (field & 0xfc); | ||
} | ||
|
||
private: | ||
byte m_bus { 0 }; | ||
byte m_slot { 0 }; | ||
byte m_function { 0 }; | ||
}; | ||
|
||
void enumerate_all(Function<void(Address, ID)>); | ||
dword get_BAR0(Address); | ||
dword get_BAR1(Address); | ||
dword get_BAR2(Address); | ||
dword get_BAR3(Address); | ||
dword get_BAR4(Address); | ||
dword get_BAR5(Address); | ||
|
||
} |
Oops, something went wrong.