2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2020-03-06 16:59:29 +02:00
|
|
|
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
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 <AK/ByteReader.h>
|
2021-11-08 00:51:39 +01:00
|
|
|
#include <AK/Error.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 <AK/HashTable.h>
|
2022-10-04 03:05:54 +03:00
|
|
|
#if ARCH(X86_64)
|
2022-10-04 13:46:11 +03:00
|
|
|
# include <Kernel/Arch/x86_64/PCI/Controller/HostBridge.h>
|
2022-09-02 13:41:48 +03:00
|
|
|
#endif
|
2021-06-25 09:46:17 +03:00
|
|
|
#include <Kernel/Bus/PCI/Access.h>
|
2022-01-07 14:10:44 +02:00
|
|
|
#include <Kernel/Bus/PCI/Controller/MemoryBackedHostBridge.h>
|
2022-01-21 17:06:39 +02:00
|
|
|
#include <Kernel/Bus/PCI/Initializer.h>
|
2021-01-25 16:07:10 +01:00
|
|
|
#include <Kernel/Debug.h>
|
2021-09-11 10:39:47 +03:00
|
|
|
#include <Kernel/Firmware/ACPI/Definitions.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/Memory/MemoryManager.h>
|
|
|
|
#include <Kernel/Memory/Region.h>
|
2022-01-07 14:10:44 +02:00
|
|
|
#include <Kernel/Memory/TypedMapping.h>
|
2022-01-21 18:09:21 +02:00
|
|
|
#include <Kernel/ProcessExposed.h>
|
2021-06-22 17:40:16 +02:00
|
|
|
#include <Kernel/Sections.h>
|
2019-12-31 13:04:30 +02:00
|
|
|
|
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
|
|
|
namespace Kernel::PCI {
|
2020-02-16 01:27:42 +01:00
|
|
|
|
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
|
|
|
#define PCI_MMIO_CONFIG_SPACE_SIZE 4096
|
2019-12-31 13:04:30 +02:00
|
|
|
|
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
|
|
|
static Access* s_access;
|
2020-04-08 17:08:00 +02:00
|
|
|
|
2020-04-08 17:29:37 +02:00
|
|
|
Access& Access::the()
|
2019-12-31 13:04:30 +02:00
|
|
|
{
|
|
|
|
if (s_access == nullptr) {
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED(); // We failed to initialize the PCI subsystem, so stop here!
|
2019-12-31 13:04:30 +02:00
|
|
|
}
|
|
|
|
return *s_access;
|
|
|
|
}
|
|
|
|
|
2020-04-08 17:29:37 +02:00
|
|
|
bool Access::is_initialized()
|
2019-12-31 13:04:30 +02:00
|
|
|
{
|
|
|
|
return (s_access != nullptr);
|
|
|
|
}
|
|
|
|
|
2022-09-16 13:17:02 +03:00
|
|
|
bool Access::is_hardware_disabled()
|
|
|
|
{
|
|
|
|
return g_pci_access_io_probe_failed;
|
|
|
|
}
|
|
|
|
|
2022-01-21 17:06:39 +02:00
|
|
|
bool Access::is_disabled()
|
|
|
|
{
|
|
|
|
return g_pci_access_is_disabled_from_commandline || g_pci_access_io_probe_failed;
|
|
|
|
}
|
|
|
|
|
2022-01-07 14:10:44 +02:00
|
|
|
UNMAP_AFTER_INIT bool Access::find_and_register_pci_host_bridges_from_acpi_mcfg_table(PhysicalAddress mcfg_table)
|
2019-12-31 13:04:30 +02:00
|
|
|
{
|
2022-01-07 14:10:44 +02:00
|
|
|
u32 length = 0;
|
|
|
|
u8 revision = 0;
|
|
|
|
{
|
2022-01-13 18:20:22 +02:00
|
|
|
auto mapped_mcfg_table_or_error = Memory::map_typed<ACPI::Structures::SDTHeader>(mcfg_table);
|
|
|
|
if (mapped_mcfg_table_or_error.is_error()) {
|
|
|
|
dbgln("Failed to map MCFG table");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
auto mapped_mcfg_table = mapped_mcfg_table_or_error.release_value();
|
2022-01-07 14:10:44 +02:00
|
|
|
length = mapped_mcfg_table->length;
|
|
|
|
revision = mapped_mcfg_table->revision;
|
|
|
|
}
|
2020-12-18 20:24:32 +02:00
|
|
|
|
2022-01-07 14:10:44 +02:00
|
|
|
if (length == sizeof(ACPI::Structures::SDTHeader))
|
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
|
|
|
return false;
|
2020-10-31 22:24:01 +02:00
|
|
|
|
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
|
|
|
dbgln("PCI: MCFG, length: {}, revision: {}", length, revision);
|
2020-10-31 22:24:01 +02:00
|
|
|
|
2021-12-24 11:22:11 -03:00
|
|
|
if (Checked<size_t>::addition_would_overflow(length, PAGE_SIZE)) {
|
|
|
|
dbgln("Overflow when adding extra page to allocation of length {}", length);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
length += PAGE_SIZE;
|
|
|
|
auto region_size_or_error = Memory::page_round_up(length);
|
|
|
|
if (region_size_or_error.is_error()) {
|
|
|
|
dbgln("Failed to round up length of {} to pages", length);
|
|
|
|
return false;
|
|
|
|
}
|
2022-07-11 17:32:29 +00:00
|
|
|
auto mcfg_region_or_error = MM.allocate_kernel_region(mcfg_table.page_base(), region_size_or_error.value(), "PCI Parsing MCFG"sv, Memory::Region::Access::ReadWrite);
|
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
|
|
|
if (mcfg_region_or_error.is_error())
|
|
|
|
return false;
|
|
|
|
auto& mcfg = *(ACPI::Structures::MCFG*)mcfg_region_or_error.value()->vaddr().offset(mcfg_table.offset_in_page()).as_ptr();
|
|
|
|
dbgln_if(PCI_DEBUG, "PCI: Checking MCFG @ {}, {}", VirtualAddress(&mcfg), mcfg_table);
|
|
|
|
for (u32 index = 0; index < ((mcfg.header.length - sizeof(ACPI::Structures::MCFG)) / sizeof(ACPI::Structures::PCI_MMIO_Descriptor)); index++) {
|
|
|
|
u8 start_bus = mcfg.descriptors[index].start_pci_bus;
|
|
|
|
u8 end_bus = mcfg.descriptors[index].end_pci_bus;
|
2022-01-07 14:10:44 +02:00
|
|
|
u64 start_addr = mcfg.descriptors[index].base_addr;
|
2020-10-31 22:24:01 +02:00
|
|
|
|
2022-01-07 14:10:44 +02:00
|
|
|
Domain pci_domain { index, start_bus, end_bus };
|
|
|
|
dmesgln("PCI: New PCI domain @ {}, PCI buses ({}-{})", PhysicalAddress { start_addr }, start_bus, end_bus);
|
|
|
|
auto host_bridge = MemoryBackedHostBridge::must_create(pci_domain, PhysicalAddress { start_addr });
|
|
|
|
add_host_controller(move(host_bridge));
|
2019-12-31 13:04:30 +02:00
|
|
|
}
|
2022-01-07 14:10:44 +02:00
|
|
|
|
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
|
|
|
return true;
|
2019-12-31 13:04:30 +02:00
|
|
|
}
|
|
|
|
|
2022-01-07 14:10:44 +02:00
|
|
|
UNMAP_AFTER_INIT bool Access::initialize_for_multiple_pci_domains(PhysicalAddress mcfg_table)
|
2019-12-31 13:04:30 +02:00
|
|
|
{
|
2022-01-21 18:13:34 +02:00
|
|
|
VERIFY(!Access::is_initialized());
|
2022-01-07 14:10:44 +02:00
|
|
|
auto* access = new Access();
|
|
|
|
if (!access->find_and_register_pci_host_bridges_from_acpi_mcfg_table(mcfg_table))
|
|
|
|
return false;
|
|
|
|
access->rescan_hardware();
|
|
|
|
dbgln_if(PCI_DEBUG, "PCI: access for multiple PCI domain initialised.");
|
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
|
|
|
return true;
|
2019-12-31 13:04:30 +02:00
|
|
|
}
|
|
|
|
|
2022-10-04 03:05:54 +03:00
|
|
|
#if ARCH(X86_64)
|
2022-01-07 14:10:44 +02:00
|
|
|
UNMAP_AFTER_INIT bool Access::initialize_for_one_pci_domain()
|
2020-12-18 20:24:32 +02:00
|
|
|
{
|
2022-01-21 18:13:34 +02:00
|
|
|
VERIFY(!Access::is_initialized());
|
2022-01-07 14:10:44 +02:00
|
|
|
auto* access = new Access();
|
|
|
|
auto host_bridge = HostBridge::must_create_with_io_access();
|
|
|
|
access->add_host_controller(move(host_bridge));
|
|
|
|
access->rescan_hardware();
|
|
|
|
dbgln_if(PCI_DEBUG, "PCI: access for one PCI domain initialised.");
|
|
|
|
return true;
|
2020-12-18 20:24:32 +02:00
|
|
|
}
|
2022-09-02 13:41:48 +03:00
|
|
|
#endif
|
2020-12-18 20:24:32 +02:00
|
|
|
|
2022-02-04 19:48:13 +02:00
|
|
|
ErrorOr<void> Access::add_host_controller_and_enumerate_attached_devices(NonnullOwnPtr<HostController> controller, Function<void(DeviceIdentifier const&)> callback)
|
2022-01-15 09:17:07 +02:00
|
|
|
{
|
2022-02-04 19:48:13 +02:00
|
|
|
// Note: We hold the spinlocks for a moment just to ensure we append the
|
|
|
|
// device identifiers safely. Afterwards, enumeration goes lockless to allow
|
|
|
|
// IRQs to be fired if necessary.
|
|
|
|
Vector<DeviceIdentifier> device_identifiers_behind_host_controller;
|
|
|
|
{
|
|
|
|
SpinlockLocker locker(m_access_lock);
|
|
|
|
SpinlockLocker scan_locker(m_scan_lock);
|
|
|
|
auto domain_number = controller->domain_number();
|
|
|
|
|
|
|
|
VERIFY(!m_host_controllers.contains(domain_number));
|
|
|
|
// Note: We need to register the new controller as soon as possible, and
|
2022-09-30 23:00:30 -04:00
|
|
|
// definitely before enumerating devices behind that.
|
2022-02-04 19:48:13 +02:00
|
|
|
m_host_controllers.set(domain_number, move(controller));
|
|
|
|
ErrorOr<void> expansion_result;
|
2022-02-04 22:11:50 +02:00
|
|
|
m_host_controllers.get(domain_number).value()->enumerate_attached_devices([&](DeviceIdentifier const& device_identifier) -> IterationDecision {
|
2022-02-04 19:48:13 +02:00
|
|
|
m_device_identifiers.append(device_identifier);
|
|
|
|
auto result = device_identifiers_behind_host_controller.try_append(device_identifier);
|
2022-02-04 22:11:50 +02:00
|
|
|
if (result.is_error()) {
|
2022-02-04 19:48:13 +02:00
|
|
|
expansion_result = result;
|
2022-02-04 22:11:50 +02:00
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
return IterationDecision::Continue;
|
2022-02-04 19:48:13 +02:00
|
|
|
});
|
|
|
|
if (expansion_result.is_error())
|
|
|
|
return expansion_result;
|
|
|
|
}
|
2022-01-15 09:17:07 +02:00
|
|
|
|
2022-02-04 19:48:13 +02:00
|
|
|
for (auto const& device_identifier : device_identifiers_behind_host_controller) {
|
2022-01-15 09:17:07 +02:00
|
|
|
callback(device_identifier);
|
2022-02-04 19:48:13 +02:00
|
|
|
}
|
|
|
|
return {};
|
2022-01-15 09:17:07 +02:00
|
|
|
}
|
|
|
|
|
2022-01-07 14:10:44 +02:00
|
|
|
UNMAP_AFTER_INIT void Access::add_host_controller(NonnullOwnPtr<HostController> controller)
|
2020-12-18 17:37:51 +02:00
|
|
|
{
|
2022-01-07 14:10:44 +02:00
|
|
|
auto domain_number = controller->domain_number();
|
|
|
|
m_host_controllers.set(domain_number, move(controller));
|
2020-12-18 17:37:51 +02:00
|
|
|
}
|
|
|
|
|
2022-01-07 14:10:44 +02:00
|
|
|
UNMAP_AFTER_INIT Access::Access()
|
2020-03-22 13:12:45 +13:00
|
|
|
{
|
2022-01-07 14:10:44 +02:00
|
|
|
s_access = this;
|
2020-03-22 13:12:45 +13:00
|
|
|
}
|
|
|
|
|
2022-01-07 14:10:44 +02:00
|
|
|
UNMAP_AFTER_INIT void Access::rescan_hardware()
|
2021-04-16 17:19:29 +03:00
|
|
|
{
|
2022-02-03 15:46:59 +01:00
|
|
|
SpinlockLocker locker(m_access_lock);
|
2022-01-07 14:10:44 +02:00
|
|
|
SpinlockLocker scan_locker(m_scan_lock);
|
|
|
|
VERIFY(m_device_identifiers.is_empty());
|
|
|
|
for (auto it = m_host_controllers.begin(); it != m_host_controllers.end(); ++it) {
|
2022-02-04 22:11:50 +02:00
|
|
|
(*it).value->enumerate_attached_devices([this](DeviceIdentifier device_identifier) -> IterationDecision {
|
2022-01-07 14:10:44 +02:00
|
|
|
m_device_identifiers.append(device_identifier);
|
2022-02-04 22:11:50 +02:00
|
|
|
return IterationDecision::Continue;
|
2022-01-07 14:10:44 +02:00
|
|
|
});
|
|
|
|
}
|
2021-04-16 17:19:29 +03:00
|
|
|
}
|
|
|
|
|
2022-02-04 19:48:13 +02:00
|
|
|
ErrorOr<void> Access::fast_enumerate(Function<void(DeviceIdentifier const&)>& callback) const
|
2020-03-22 13:12:45 +13:00
|
|
|
{
|
2022-02-04 19:48:13 +02:00
|
|
|
// Note: We hold the m_access_lock for a brief moment just to ensure we get
|
|
|
|
// a complete Vector in case someone wants to mutate it.
|
|
|
|
Vector<DeviceIdentifier> device_identifiers;
|
|
|
|
{
|
|
|
|
SpinlockLocker locker(m_access_lock);
|
|
|
|
VERIFY(!m_device_identifiers.is_empty());
|
|
|
|
TRY(device_identifiers.try_extend(m_device_identifiers));
|
|
|
|
}
|
|
|
|
for (auto const& device_identifier : device_identifiers) {
|
2022-01-07 14:10:44 +02:00
|
|
|
callback(device_identifier);
|
|
|
|
}
|
2022-02-04 19:48:13 +02:00
|
|
|
return {};
|
2020-03-22 13:12:45 +13:00
|
|
|
}
|
2020-04-08 16:56:36 +02:00
|
|
|
|
2022-01-07 14:10:44 +02:00
|
|
|
DeviceIdentifier Access::get_device_identifier(Address address) const
|
2020-03-22 13:12:45 +13:00
|
|
|
{
|
2022-01-07 14:10:44 +02:00
|
|
|
for (auto device_identifier : m_device_identifiers) {
|
|
|
|
if (device_identifier.address().domain() == address.domain()
|
|
|
|
&& device_identifier.address().bus() == address.bus()
|
|
|
|
&& device_identifier.address().device() == address.device()
|
|
|
|
&& device_identifier.address().function() == address.function()) {
|
|
|
|
return device_identifier;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VERIFY_NOT_REACHED();
|
2020-03-22 13:12:45 +13:00
|
|
|
}
|
2020-04-08 16:56:36 +02:00
|
|
|
|
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
|
|
|
void Access::write8_field(Address address, u32 field, u8 value)
|
2021-01-02 19:43:45 +02:00
|
|
|
{
|
2022-02-03 15:46:59 +01:00
|
|
|
SpinlockLocker locker(m_access_lock);
|
2022-01-07 14:10:44 +02:00
|
|
|
VERIFY(m_host_controllers.contains(address.domain()));
|
|
|
|
auto& controller = *m_host_controllers.get(address.domain()).value();
|
|
|
|
controller.write8_field(address.bus(), address.device(), address.function(), field, value);
|
2021-01-02 19:43:45 +02:00
|
|
|
}
|
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
|
|
|
void Access::write16_field(Address address, u32 field, u16 value)
|
2020-03-22 13:12:45 +13:00
|
|
|
{
|
2022-02-03 15:46:59 +01:00
|
|
|
SpinlockLocker locker(m_access_lock);
|
2022-01-07 14:10:44 +02:00
|
|
|
VERIFY(m_host_controllers.contains(address.domain()));
|
|
|
|
auto& controller = *m_host_controllers.get(address.domain()).value();
|
|
|
|
controller.write16_field(address.bus(), address.device(), address.function(), field, value);
|
2020-03-22 13:12:45 +13:00
|
|
|
}
|
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
|
|
|
void Access::write32_field(Address address, u32 field, u32 value)
|
2021-01-02 19:33:30 +02:00
|
|
|
{
|
2022-02-03 15:46:59 +01:00
|
|
|
SpinlockLocker locker(m_access_lock);
|
2022-01-07 14:10:44 +02:00
|
|
|
VERIFY(m_host_controllers.contains(address.domain()));
|
|
|
|
auto& controller = *m_host_controllers.get(address.domain()).value();
|
|
|
|
controller.write32_field(address.bus(), address.device(), address.function(), field, value);
|
2021-01-02 19:33:30 +02:00
|
|
|
}
|
|
|
|
|
2021-09-28 20:18:51 +03:00
|
|
|
u8 Access::read8_field(Address address, RegisterOffset field)
|
|
|
|
{
|
|
|
|
return read8_field(address, to_underlying(field));
|
|
|
|
}
|
|
|
|
u16 Access::read16_field(Address address, RegisterOffset field)
|
|
|
|
{
|
|
|
|
return read16_field(address, to_underlying(field));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
u8 Access::read8_field(Address address, u32 field)
|
2021-01-02 19:33:30 +02:00
|
|
|
{
|
2022-02-03 15:46:59 +01:00
|
|
|
SpinlockLocker locker(m_access_lock);
|
2022-01-07 14:10:44 +02:00
|
|
|
VERIFY(m_host_controllers.contains(address.domain()));
|
|
|
|
auto& controller = *m_host_controllers.get(address.domain()).value();
|
|
|
|
return controller.read8_field(address.bus(), address.device(), address.function(), field);
|
2021-01-02 19:33:30 +02:00
|
|
|
}
|
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
|
|
|
u16 Access::read16_field(Address address, u32 field)
|
2021-01-02 19:33:30 +02:00
|
|
|
{
|
2022-02-03 15:46:59 +01:00
|
|
|
SpinlockLocker locker(m_access_lock);
|
2022-01-07 14:10:44 +02:00
|
|
|
VERIFY(m_host_controllers.contains(address.domain()));
|
|
|
|
auto& controller = *m_host_controllers.get(address.domain()).value();
|
|
|
|
return controller.read16_field(address.bus(), address.device(), address.function(), field);
|
2021-01-02 19:33:30 +02:00
|
|
|
}
|
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
|
|
|
u32 Access::read32_field(Address address, u32 field)
|
2021-01-02 19:33:30 +02:00
|
|
|
{
|
2022-02-03 15:46:59 +01:00
|
|
|
SpinlockLocker locker(m_access_lock);
|
2022-01-07 14:10:44 +02:00
|
|
|
VERIFY(m_host_controllers.contains(address.domain()));
|
|
|
|
auto& controller = *m_host_controllers.get(address.domain()).value();
|
|
|
|
return controller.read32_field(address.bus(), address.device(), address.function(), field);
|
2019-12-31 13:04:30 +02:00
|
|
|
}
|
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
|
|
|
|
2020-04-08 17:29:37 +02:00
|
|
|
}
|