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
|
|
|
*/
|
|
|
|
|
2019-12-31 13:04:30 +02:00
|
|
|
#pragma once
|
|
|
|
|
2021-05-21 12:06:20 +03:00
|
|
|
#include <AK/Bitmap.h>
|
2022-10-23 21:51:56 +03:00
|
|
|
#include <AK/HashMap.h>
|
2022-11-18 23:17:06 +01:00
|
|
|
#include <AK/NonnullOwnPtr.h>
|
2022-02-04 19:48:13 +02:00
|
|
|
#include <AK/Try.h>
|
2020-04-10 20:25:03 +03:00
|
|
|
#include <AK/Vector.h>
|
2022-01-07 14:10:44 +02:00
|
|
|
#include <Kernel/Bus/PCI/Controller/HostController.h>
|
2021-06-25 09:46:17 +03:00
|
|
|
#include <Kernel/Bus/PCI/Definitions.h>
|
2021-10-22 20:43:33 +03:00
|
|
|
#include <Kernel/Locking/Spinlock.h>
|
2019-12-31 13:04:30 +02:00
|
|
|
|
2021-05-27 22:03:26 +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
|
|
|
class Access {
|
2021-05-27 22:03:26 +03:00
|
|
|
public:
|
2022-01-07 14:10:44 +02:00
|
|
|
static bool initialize_for_multiple_pci_domains(PhysicalAddress mcfg_table);
|
2022-09-02 13:41:48 +03:00
|
|
|
|
2022-10-04 03:05:54 +03:00
|
|
|
#if ARCH(X86_64)
|
2022-01-07 14:10:44 +02:00
|
|
|
static bool initialize_for_one_pci_domain();
|
2022-09-02 13:41:48 +03:00
|
|
|
#endif
|
2021-05-27 22:03:26 +03:00
|
|
|
|
2022-02-04 19:48:13 +02:00
|
|
|
ErrorOr<void> fast_enumerate(Function<void(DeviceIdentifier const&)>&) const;
|
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 rescan_hardware();
|
2019-12-31 13:04:30 +02:00
|
|
|
|
|
|
|
static Access& the();
|
|
|
|
static bool is_initialized();
|
2022-01-21 17:06:39 +02:00
|
|
|
static bool is_disabled();
|
2022-09-16 13:17:02 +03:00
|
|
|
static bool is_hardware_disabled();
|
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 write8_field(Address address, u32 field, u8 value);
|
|
|
|
void write16_field(Address address, u32 field, u16 value);
|
|
|
|
void write32_field(Address address, u32 field, u32 value);
|
|
|
|
u8 read8_field(Address address, u32 field);
|
|
|
|
u16 read16_field(Address address, u32 field);
|
|
|
|
u32 read32_field(Address address, u32 field);
|
2021-09-23 09:14:51 +03:00
|
|
|
DeviceIdentifier get_device_identifier(Address address) const;
|
2020-12-18 20:24:32 +02:00
|
|
|
|
2022-11-09 11:39:58 +01:00
|
|
|
Spinlock<LockRank::None> const& scan_lock() const { return m_scan_lock; }
|
|
|
|
RecursiveSpinlock<LockRank::None> const& access_lock() const { return m_access_lock; }
|
2022-01-07 14:10:44 +02:00
|
|
|
|
2022-02-04 19:48:13 +02:00
|
|
|
ErrorOr<void> add_host_controller_and_enumerate_attached_devices(NonnullOwnPtr<HostController>, Function<void(DeviceIdentifier const&)> callback);
|
2022-01-15 09:17:07 +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
|
|
|
private:
|
2021-09-28 20:18:51 +03:00
|
|
|
u8 read8_field(Address address, RegisterOffset field);
|
|
|
|
u16 read16_field(Address address, RegisterOffset field);
|
|
|
|
|
2022-01-07 14:10:44 +02:00
|
|
|
void add_host_controller(NonnullOwnPtr<HostController>);
|
|
|
|
bool find_and_register_pci_host_bridges_from_acpi_mcfg_table(PhysicalAddress mcfg);
|
|
|
|
Access();
|
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
|
|
|
|
|
|
|
Vector<Capability> get_capabilities(Address);
|
|
|
|
Optional<u8> get_capabilities_pointer(Address address);
|
|
|
|
|
2022-11-09 11:39:58 +01:00
|
|
|
mutable RecursiveSpinlock<LockRank::None> m_access_lock {};
|
|
|
|
mutable Spinlock<LockRank::None> m_scan_lock {};
|
2022-01-07 14:10:44 +02:00
|
|
|
|
2022-10-23 21:51:56 +03:00
|
|
|
HashMap<u32, NonnullOwnPtr<PCI::HostController>> m_host_controllers;
|
2021-09-23 09:14:51 +03:00
|
|
|
Vector<DeviceIdentifier> m_device_identifiers;
|
2019-12-31 13:04:30 +02:00
|
|
|
};
|
2020-02-16 01:27:42 +01:00
|
|
|
}
|