2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2020-03-06 11:59:29 -03:00
|
|
|
* Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03: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 06:08:38 -03:00
|
|
|
#include <AK/ByteReader.h>
|
2021-11-07 20:51:39 -03: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 06:08:38 -03:00
|
|
|
#include <AK/HashTable.h>
|
2021-10-01 03:58:50 -03:00
|
|
|
#include <Kernel/Arch/x86/IO.h>
|
2021-06-25 03:46:17 -03:00
|
|
|
#include <Kernel/Bus/PCI/Access.h>
|
2022-01-07 09:10:44 -03:00
|
|
|
#include <Kernel/Bus/PCI/Controller/HostBridge.h>
|
|
|
|
|
#include <Kernel/Bus/PCI/Controller/MemoryBackedHostBridge.h>
|
2021-01-25 12:07:10 -03:00
|
|
|
#include <Kernel/Debug.h>
|
2021-09-11 04: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 06:08:38 -03:00
|
|
|
#include <Kernel/Memory/MemoryManager.h>
|
|
|
|
|
#include <Kernel/Memory/Region.h>
|
2022-01-07 09:10:44 -03:00
|
|
|
#include <Kernel/Memory/TypedMapping.h>
|
2021-06-22 12:40:16 -03:00
|
|
|
#include <Kernel/Sections.h>
|
2019-12-31 08:04:30 -03: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 06:08:38 -03:00
|
|
|
namespace Kernel::PCI {
|
2020-02-15 21:27:42 -03: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 06:08:38 -03:00
|
|
|
#define PCI_MMIO_CONFIG_SPACE_SIZE 4096
|
2019-12-31 08:04:30 -03: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 06:08:38 -03:00
|
|
|
static Access* s_access;
|
2020-04-08 12:08:00 -03:00
|
|
|
|
2020-04-08 12:29:37 -03:00
|
|
|
Access& Access::the()
|
2019-12-31 08:04:30 -03:00
|
|
|
{
|
|
|
|
|
if (s_access == nullptr) {
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY_NOT_REACHED(); // We failed to initialize the PCI subsystem, so stop here!
|
2019-12-31 08:04:30 -03:00
|
|
|
}
|
|
|
|
|
return *s_access;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-08 12:29:37 -03:00
|
|
|
bool Access::is_initialized()
|
2019-12-31 08:04:30 -03:00
|
|
|
{
|
|
|
|
|
return (s_access != nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-07 09:10:44 -03:00
|
|
|
UNMAP_AFTER_INIT bool Access::find_and_register_pci_host_bridges_from_acpi_mcfg_table(PhysicalAddress mcfg_table)
|
2019-12-31 08:04:30 -03:00
|
|
|
{
|
2022-01-07 09:10:44 -03:00
|
|
|
u32 length = 0;
|
|
|
|
|
u8 revision = 0;
|
|
|
|
|
{
|
2022-01-13 13:20:22 -03: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 09:10:44 -03:00
|
|
|
length = mapped_mcfg_table->length;
|
|
|
|
|
revision = mapped_mcfg_table->revision;
|
|
|
|
|
}
|
2020-12-18 15:24:32 -03:00
|
|
|
|
2022-01-07 09:10:44 -03: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 06:08:38 -03:00
|
|
|
return false;
|
2020-10-31 17:24:01 -03: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 06:08:38 -03:00
|
|
|
dbgln("PCI: MCFG, length: {}, revision: {}", length, revision);
|
2020-10-31 17:24:01 -03: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;
|
|
|
|
|
}
|
|
|
|
|
auto mcfg_region_or_error = MM.allocate_kernel_region(mcfg_table.page_base(), region_size_or_error.value(), "PCI Parsing MCFG", 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 06: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 09:10:44 -03:00
|
|
|
u64 start_addr = mcfg.descriptors[index].base_addr;
|
2020-10-31 17:24:01 -03:00
|
|
|
|
2022-01-07 09:10:44 -03: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 08:04:30 -03:00
|
|
|
}
|
2022-01-07 09:10:44 -03: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 06:08:38 -03:00
|
|
|
return true;
|
2019-12-31 08:04:30 -03:00
|
|
|
}
|
|
|
|
|
|
2022-01-07 09:10:44 -03:00
|
|
|
UNMAP_AFTER_INIT bool Access::initialize_for_multiple_pci_domains(PhysicalAddress mcfg_table)
|
2019-12-31 08:04:30 -03:00
|
|
|
{
|
2022-01-21 13:13:34 -03:00
|
|
|
VERIFY(!Access::is_initialized());
|
2022-01-07 09:10:44 -03: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 06:08:38 -03:00
|
|
|
return true;
|
2019-12-31 08:04:30 -03:00
|
|
|
}
|
|
|
|
|
|
2022-01-07 09:10:44 -03:00
|
|
|
UNMAP_AFTER_INIT bool Access::initialize_for_one_pci_domain()
|
2020-12-18 15:24:32 -03:00
|
|
|
{
|
2022-01-21 13:13:34 -03:00
|
|
|
VERIFY(!Access::is_initialized());
|
2022-01-07 09:10:44 -03: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 15:24:32 -03:00
|
|
|
}
|
|
|
|
|
|
2022-01-15 04:17:07 -03:00
|
|
|
void Access::add_host_controller_and_enumerate_attached_devices(NonnullOwnPtr<HostController> controller, Function<void(DeviceIdentifier const&)> callback)
|
|
|
|
|
{
|
|
|
|
|
MutexLocker 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
|
|
|
|
|
// definitely before enumerating devices behing that.
|
|
|
|
|
m_host_controllers.set(domain_number, move(controller));
|
|
|
|
|
m_host_controllers.get(domain_number).value()->enumerate_attached_devices([&](DeviceIdentifier const& device_identifier) -> void {
|
|
|
|
|
m_device_identifiers.append(device_identifier);
|
|
|
|
|
callback(device_identifier);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-07 09:10:44 -03:00
|
|
|
UNMAP_AFTER_INIT void Access::add_host_controller(NonnullOwnPtr<HostController> controller)
|
2020-12-18 12:37:51 -03:00
|
|
|
{
|
2022-01-07 09:10:44 -03:00
|
|
|
auto domain_number = controller->domain_number();
|
|
|
|
|
m_host_controllers.set(domain_number, move(controller));
|
2020-12-18 12:37:51 -03:00
|
|
|
}
|
|
|
|
|
|
2022-01-07 09:10:44 -03:00
|
|
|
UNMAP_AFTER_INIT Access::Access()
|
2020-03-21 21:12:45 -03:00
|
|
|
{
|
2022-01-07 09:10:44 -03:00
|
|
|
s_access = this;
|
2020-03-21 21:12:45 -03:00
|
|
|
}
|
|
|
|
|
|
2022-01-07 09:10:44 -03:00
|
|
|
UNMAP_AFTER_INIT void Access::rescan_hardware()
|
2021-04-16 11:19:29 -03:00
|
|
|
{
|
2022-01-07 09:10:44 -03:00
|
|
|
MutexLocker locker(m_access_lock);
|
|
|
|
|
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) {
|
|
|
|
|
(*it).value->enumerate_attached_devices([this](DeviceIdentifier device_identifier) -> void {
|
|
|
|
|
m_device_identifiers.append(device_identifier);
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-04-16 11:19:29 -03:00
|
|
|
}
|
|
|
|
|
|
2022-01-07 09:10:44 -03:00
|
|
|
void Access::fast_enumerate(Function<void(DeviceIdentifier const&)>& callback) const
|
2020-03-21 21:12:45 -03:00
|
|
|
{
|
2022-01-07 09:10:44 -03:00
|
|
|
MutexLocker locker(m_access_lock);
|
|
|
|
|
VERIFY(!m_device_identifiers.is_empty());
|
|
|
|
|
for (auto const& device_identifier : m_device_identifiers) {
|
|
|
|
|
callback(device_identifier);
|
|
|
|
|
}
|
2020-03-21 21:12:45 -03:00
|
|
|
}
|
2020-04-08 11:56:36 -03:00
|
|
|
|
2022-01-07 09:10:44 -03:00
|
|
|
DeviceIdentifier Access::get_device_identifier(Address address) const
|
2020-03-21 21:12:45 -03:00
|
|
|
{
|
2022-01-07 09:10:44 -03: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-21 21:12:45 -03:00
|
|
|
}
|
2020-04-08 11:56:36 -03: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 06:08:38 -03:00
|
|
|
void Access::write8_field(Address address, u32 field, u8 value)
|
2021-01-02 14:43:45 -03:00
|
|
|
{
|
2022-01-07 09:10:44 -03:00
|
|
|
MutexLocker lock(m_access_lock);
|
|
|
|
|
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 14:43:45 -03: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 06:08:38 -03:00
|
|
|
void Access::write16_field(Address address, u32 field, u16 value)
|
2020-03-21 21:12:45 -03:00
|
|
|
{
|
2022-01-07 09:10:44 -03:00
|
|
|
MutexLocker lock(m_access_lock);
|
|
|
|
|
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-21 21:12:45 -03: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 06:08:38 -03:00
|
|
|
void Access::write32_field(Address address, u32 field, u32 value)
|
2021-01-02 14:33:30 -03:00
|
|
|
{
|
2022-01-07 09:10:44 -03:00
|
|
|
MutexLocker lock(m_access_lock);
|
|
|
|
|
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 14:33:30 -03:00
|
|
|
}
|
|
|
|
|
|
2021-09-28 14: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 06:08:38 -03:00
|
|
|
u8 Access::read8_field(Address address, u32 field)
|
2021-01-02 14:33:30 -03:00
|
|
|
{
|
2022-01-07 09:10:44 -03:00
|
|
|
MutexLocker lock(m_access_lock);
|
|
|
|
|
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 14:33:30 -03: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 06:08:38 -03:00
|
|
|
u16 Access::read16_field(Address address, u32 field)
|
2021-01-02 14:33:30 -03:00
|
|
|
{
|
2022-01-07 09:10:44 -03:00
|
|
|
MutexLocker lock(m_access_lock);
|
|
|
|
|
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 14:33:30 -03: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 06:08:38 -03:00
|
|
|
u32 Access::read32_field(Address address, u32 field)
|
2021-01-02 14:33:30 -03:00
|
|
|
{
|
2022-01-07 09:10:44 -03:00
|
|
|
MutexLocker lock(m_access_lock);
|
|
|
|
|
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 08:04:30 -03: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 06:08:38 -03:00
|
|
|
|
2020-04-08 12:29:37 -03:00
|
|
|
}
|