2021-08-08 21:50:20 +03:00
/*
* Copyright ( c ) 2021 , Luke Wilde < lukew @ serenityos . org >
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
# include <AK/Singleton.h>
Kernel/PCI: Simplify the entire subsystem
A couple of things were changed:
1. Semantic changes - PCI segments are now called PCI domains, to better
match what they are really. It's also the name that Linux gave, and it
seems that Wikipedia also uses this name.
We also remove PCI::ChangeableAddress, because it was used in the past
but now it's no longer being used.
2. There are no WindowedMMIOAccess or MMIOAccess classes anymore, as
they made a bunch of unnecessary complexity. Instead, Windowed access is
removed entirely (this was tested, but never was benchmarked), so we are
left with IO access and memory access options. The memory access option
is essentially mapping the PCI bus (from the chosen PCI domain), to
virtual memory as-is. This means that unless needed, at any time, there
is only one PCI bus being mapped, and this is changed if access to
another PCI bus in the same PCI domain is needed. For now, we don't
support mapping of different PCI buses from different PCI domains at the
same time, because basically it's still a non-issue for most machines
out there.
2. OOM-safety is increased, especially when constructing the Access
object. It means that we pre-allocating any needed resources, and we try
to find PCI domains (if requested to initialize memory access) after we
attempt to construct the Access object, so it's possible to fail at this
point "gracefully".
3. All PCI API functions are now separated into a different header file,
which means only "clients" of the PCI subsystem API will need to include
that header file.
4. Functional changes - we only allow now to enumerate the bus after
a hardware scan. This means that the old method "enumerate_hardware"
is removed, so, when initializing an Access object, the initializing
function must call rescan on it to force it to find devices. This makes
it possible to fail rescan, and also to defer it after construction from
both OOM-safety terms and hotplug capabilities.
2021-09-07 12:08:38 +03:00
# include <Kernel/Bus/PCI/API.h>
2021-08-14 06:31:00 +03:00
# include <Kernel/Bus/USB/SysFSUSB.h>
2021-08-13 09:10:43 +03:00
# include <Kernel/Bus/USB/UHCI/UHCIController.h>
2021-08-08 21:50:20 +03:00
# include <Kernel/Bus/USB/USBManagement.h>
# include <Kernel/CommandLine.h>
# include <Kernel/Sections.h>
namespace Kernel : : USB {
static Singleton < USBManagement > s_the ;
2021-08-14 06:31:00 +03:00
READONLY_AFTER_INIT bool s_initialized_sys_fs_directory = false ;
2021-08-08 21:50:20 +03:00
UNMAP_AFTER_INIT USBManagement : : USBManagement ( )
{
enumerate_controllers ( ) ;
}
UNMAP_AFTER_INIT void USBManagement : : enumerate_controllers ( )
{
2021-08-09 23:16:24 +03:00
if ( kernel_command_line ( ) . disable_usb ( ) )
return ;
2021-09-23 11:05:00 +03:00
PCI : : enumerate ( [ this ] ( PCI : : DeviceIdentifier const & device_identifier ) {
2021-09-23 09:14:51 +03:00
if ( ! ( device_identifier . class_code ( ) . value ( ) = = 0xc & & device_identifier . subclass_code ( ) . value ( ) = = 0x3 ) )
2021-08-09 23:16:24 +03:00
return ;
2021-09-23 09:14:51 +03:00
if ( device_identifier . prog_if ( ) . value ( ) = = 0x0 ) {
2021-08-09 23:16:24 +03:00
if ( kernel_command_line ( ) . disable_uhci_controller ( ) )
return ;
2021-09-23 10:20:54 +03:00
if ( auto uhci_controller_or_error = UHCIController : : try_to_initialize ( device_identifier ) ; ! uhci_controller_or_error . is_error ( ) )
2021-08-09 23:16:24 +03:00
m_controllers . append ( uhci_controller_or_error . release_value ( ) ) ;
return ;
}
2021-09-23 09:14:51 +03:00
if ( device_identifier . prog_if ( ) . value ( ) = = 0x10 ) {
2021-09-23 11:05:00 +03:00
dmesgln ( " USBManagement: OHCI controller found at {} is not currently supported. " , device_identifier . address ( ) ) ;
2021-08-09 23:16:24 +03:00
return ;
}
2021-09-23 09:14:51 +03:00
if ( device_identifier . prog_if ( ) . value ( ) = = 0x20 ) {
2021-09-23 11:05:00 +03:00
dmesgln ( " USBManagement: EHCI controller found at {} is not currently supported. " , device_identifier . address ( ) ) ;
2021-08-09 23:16:24 +03:00
return ;
}
2021-09-23 09:14:51 +03:00
if ( device_identifier . prog_if ( ) . value ( ) = = 0x30 ) {
2021-09-23 11:05:00 +03:00
dmesgln ( " USBManagement: xHCI controller found at {} is not currently supported. " , device_identifier . address ( ) ) ;
2021-08-09 23:16:24 +03:00
return ;
}
2021-09-23 11:05:00 +03:00
dmesgln ( " USBManagement: Unknown/unsupported controller at {} with programming interface 0x{:02x} " , device_identifier . address ( ) , device_identifier . prog_if ( ) . value ( ) ) ;
2021-08-09 23:16:24 +03:00
} ) ;
2021-08-08 21:50:20 +03:00
}
bool USBManagement : : initialized ( )
{
return s_the . is_initialized ( ) ;
}
UNMAP_AFTER_INIT void USBManagement : : initialize ( )
{
2021-08-14 06:31:00 +03:00
if ( ! s_initialized_sys_fs_directory ) {
USB : : SysFSUSBBusDirectory : : initialize ( ) ;
s_initialized_sys_fs_directory = true ;
}
2021-08-08 21:50:20 +03:00
s_the . ensure_instance ( ) ;
}
USBManagement & USBManagement : : the ( )
{
return * s_the ;
}
}