2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2021-11-29 21:15:01 +00:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2022-03-13 22:09:41 +00:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
#include <AK/ByteString.h>
|
2022-02-08 17:28:08 +00:00
|
|
|
#include <AK/Hex.h>
|
2019-08-13 15:32:22 +00:00
|
|
|
#include <AK/JsonArray.h>
|
|
|
|
#include <AK/JsonObject.h>
|
2022-02-08 17:28:08 +00:00
|
|
|
#include <AK/StringUtils.h>
|
2022-03-13 22:09:41 +00:00
|
|
|
#include <AK/StringView.h>
|
2021-01-26 17:40:44 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2022-02-08 17:28:08 +00:00
|
|
|
#include <LibCore/DirIterator.h>
|
2023-02-09 02:02:46 +00:00
|
|
|
#include <LibCore/File.h>
|
2021-11-29 21:15:01 +00:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2019-08-14 08:10:54 +00:00
|
|
|
#include <LibPCIDB/Database.h>
|
2019-08-13 15:32:22 +00:00
|
|
|
|
2021-01-26 17:40:44 +00:00
|
|
|
static bool flag_show_numerical = false;
|
2022-02-10 13:15:56 +00:00
|
|
|
static bool flag_verbose = false;
|
2021-01-26 17:40:44 +00:00
|
|
|
|
2022-07-11 17:32:29 +00:00
|
|
|
static constexpr StringView format_numerical = "{:04x}:{:02x}:{:02x}.{} {}: {}:{} (rev {:02x})"sv;
|
|
|
|
static constexpr StringView format_textual = "{:04x}:{:02x}:{:02x}.{} {}: {} {} (rev {:02x})"sv;
|
|
|
|
static constexpr StringView format_region = "\tBAR {}: {} region @ {:#x}"sv;
|
2021-01-26 17:40:44 +00:00
|
|
|
|
2022-02-08 17:28:08 +00:00
|
|
|
static u32 read_hex_string_from_bytebuffer(ByteBuffer const& buf)
|
|
|
|
{
|
2022-06-13 12:20:42 +00:00
|
|
|
// FIXME: Propagate errors.
|
|
|
|
return AK::StringUtils::convert_to_uint_from_hex(
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString(MUST(buf.slice(2, buf.size() - 2)).bytes()))
|
2022-06-13 12:20:42 +00:00
|
|
|
.release_value();
|
2022-02-08 17:28:08 +00:00
|
|
|
}
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
static u32 convert_sysfs_value_to_uint(ByteString const& value)
|
2022-02-08 17:28:08 +00:00
|
|
|
{
|
|
|
|
if (auto result = AK::StringUtils::convert_to_uint_from_hex(value); result.has_value())
|
|
|
|
return result.release_value();
|
|
|
|
if (auto result = AK::StringUtils::convert_to_uint(value); result.has_value())
|
|
|
|
return result.release_value();
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2021-11-29 21:15:01 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-08-13 15:32:22 +00:00
|
|
|
{
|
2021-11-29 21:15:01 +00:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
2020-01-23 08:42:50 +00:00
|
|
|
|
2021-01-26 17:40:44 +00:00
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.set_general_help("List PCI devices.");
|
|
|
|
args_parser.add_option(flag_show_numerical, "Show numerical IDs", "numerical", 'n');
|
2022-02-10 13:15:56 +00:00
|
|
|
args_parser.add_option(flag_verbose, "Show verbose info on devices", "verbose", 'v');
|
2021-11-29 21:15:01 +00:00
|
|
|
args_parser.parse(arguments);
|
2021-01-26 17:40:44 +00:00
|
|
|
|
2022-07-15 11:30:39 +00:00
|
|
|
if (!flag_show_numerical)
|
|
|
|
TRY(Core::System::unveil("/res/pci.ids", "r"));
|
|
|
|
TRY(Core::System::unveil("/sys/bus/pci", "r"));
|
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
|
|
|
|
2022-03-13 22:09:41 +00:00
|
|
|
auto const format = flag_show_numerical ? format_numerical : format_textual;
|
2021-01-26 17:40:44 +00:00
|
|
|
|
2021-02-25 20:10:47 +00:00
|
|
|
RefPtr<PCIDB::Database> db;
|
2021-01-26 17:40:44 +00:00
|
|
|
if (!flag_show_numerical) {
|
|
|
|
db = PCIDB::Database::open();
|
|
|
|
if (!db) {
|
|
|
|
warnln("Couldn't open PCI ID database");
|
|
|
|
flag_show_numerical = true;
|
|
|
|
}
|
|
|
|
}
|
2019-08-14 08:10:54 +00:00
|
|
|
|
2022-02-08 17:28:08 +00:00
|
|
|
Core::DirIterator di("/sys/bus/pci/", Core::DirIterator::SkipParentAndBaseDir);
|
|
|
|
if (di.has_error()) {
|
2023-03-01 15:55:15 +00:00
|
|
|
auto error = di.error();
|
|
|
|
warnln("Failed to open /sys/bus/pci - {}", error);
|
|
|
|
return error;
|
2022-02-08 17:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
2019-08-13 15:32:22 +00:00
|
|
|
|
2022-02-08 17:28:08 +00:00
|
|
|
while (di.has_next()) {
|
|
|
|
auto dir = di.next_path();
|
|
|
|
auto domain_bus_device_parts = dir.split(':');
|
|
|
|
VERIFY(domain_bus_device_parts.size() == 3);
|
|
|
|
auto domain = convert_sysfs_value_to_uint(domain_bus_device_parts[0]);
|
|
|
|
auto bus = convert_sysfs_value_to_uint(domain_bus_device_parts[1]);
|
|
|
|
auto device = convert_sysfs_value_to_uint(domain_bus_device_parts[2].split('.')[0]);
|
2020-01-23 08:42:50 +00:00
|
|
|
|
2022-02-08 17:28:08 +00:00
|
|
|
auto function_parts = dir.split('.');
|
|
|
|
VERIFY(function_parts.size() == 2);
|
|
|
|
auto function = convert_sysfs_value_to_uint(function_parts[1]);
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
auto vendor_id_filename = ByteString::formatted("/sys/bus/pci/{}/vendor", dir);
|
2023-02-09 02:02:46 +00:00
|
|
|
auto vendor_id_file = Core::File::open(vendor_id_filename, Core::File::OpenMode::Read);
|
2022-09-14 14:50:59 +00:00
|
|
|
if (vendor_id_file.is_error()) {
|
|
|
|
dbgln("Error: Could not open {}: {}", vendor_id_filename, vendor_id_file.error());
|
2022-02-08 17:28:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-12-16 14:19:34 +00:00
|
|
|
auto device_id_filename = ByteString::formatted("/sys/bus/pci/{}/device_id", dir);
|
2023-02-09 02:02:46 +00:00
|
|
|
auto device_id_file = Core::File::open(device_id_filename, Core::File::OpenMode::Read);
|
2022-09-14 14:50:59 +00:00
|
|
|
if (device_id_file.is_error()) {
|
|
|
|
dbgln("Error: Could not open {}: {}", device_id_filename, device_id_file.error());
|
2022-02-08 17:28:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-12-16 14:19:34 +00:00
|
|
|
auto class_id_filename = ByteString::formatted("/sys/bus/pci/{}/class", dir);
|
2023-02-09 02:02:46 +00:00
|
|
|
auto class_id_file = Core::File::open(class_id_filename, Core::File::OpenMode::Read);
|
2022-09-14 14:50:59 +00:00
|
|
|
if (class_id_file.is_error()) {
|
|
|
|
dbgln("Error: Could not open {}: {}", class_id_filename, class_id_file.error());
|
2022-02-08 17:28:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-12-16 14:19:34 +00:00
|
|
|
auto subclass_id_filename = ByteString::formatted("/sys/bus/pci/{}/subclass", dir);
|
2023-02-09 02:02:46 +00:00
|
|
|
auto subclass_id_file = Core::File::open(subclass_id_filename, Core::File::OpenMode::Read);
|
2022-09-14 14:50:59 +00:00
|
|
|
if (subclass_id_file.is_error()) {
|
|
|
|
dbgln("Error: Could not open {}: {}", subclass_id_filename, subclass_id_file.error());
|
2022-02-08 17:28:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-12-16 14:19:34 +00:00
|
|
|
auto revision_id_filename = ByteString::formatted("/sys/bus/pci/{}/revision", dir);
|
2023-02-09 02:02:46 +00:00
|
|
|
auto revision_id_file = Core::File::open(revision_id_filename, Core::File::OpenMode::Read);
|
2022-09-14 14:50:59 +00:00
|
|
|
if (revision_id_file.is_error()) {
|
|
|
|
dbgln("Error: Could not open {}: {}", revision_id_filename, revision_id_file.error());
|
2022-02-08 17:28:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-12-11 16:49:00 +00:00
|
|
|
auto vendor_id_contents = vendor_id_file.value()->read_until_eof();
|
2022-09-14 14:50:59 +00:00
|
|
|
if (vendor_id_contents.is_error()) {
|
|
|
|
dbgln("Error: Could not read {}: {}", vendor_id_filename, vendor_id_contents.error());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
u32 vendor_id = read_hex_string_from_bytebuffer(vendor_id_contents.value());
|
|
|
|
|
2022-12-11 16:49:00 +00:00
|
|
|
auto device_id_contents = device_id_file.value()->read_until_eof();
|
2022-09-14 14:50:59 +00:00
|
|
|
if (device_id_contents.is_error()) {
|
|
|
|
dbgln("Error: Could not read {}: {}", device_id_filename, device_id_contents.error());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
u32 device_id = read_hex_string_from_bytebuffer(device_id_contents.value());
|
|
|
|
|
2022-12-11 16:49:00 +00:00
|
|
|
auto revision_id_contents = revision_id_file.value()->read_until_eof();
|
2022-09-14 14:50:59 +00:00
|
|
|
if (revision_id_contents.is_error()) {
|
|
|
|
dbgln("Error: Could not read {}: {}", revision_id_filename, revision_id_contents.error());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
u32 revision_id = read_hex_string_from_bytebuffer(revision_id_contents.value());
|
|
|
|
|
2022-12-11 16:49:00 +00:00
|
|
|
auto class_id_contents = class_id_file.value()->read_until_eof();
|
2022-09-14 14:50:59 +00:00
|
|
|
if (class_id_contents.is_error()) {
|
|
|
|
dbgln("Error: Could not read {}: {}", class_id_filename, class_id_contents.error());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
u32 class_id = read_hex_string_from_bytebuffer(class_id_contents.value());
|
|
|
|
|
2022-12-11 16:49:00 +00:00
|
|
|
auto subclass_id_contents = subclass_id_file.value()->read_until_eof();
|
2022-09-14 14:50:59 +00:00
|
|
|
if (subclass_id_contents.is_error()) {
|
|
|
|
dbgln("Error: Could not read {}: {}", subclass_id_filename, subclass_id_contents.error());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
u32 subclass_id = read_hex_string_from_bytebuffer(subclass_id_contents.value());
|
2019-08-13 15:32:22 +00:00
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
ByteString vendor_name;
|
|
|
|
ByteString device_name;
|
|
|
|
ByteString class_name;
|
2019-08-14 08:10:54 +00:00
|
|
|
|
2020-02-20 03:09:20 +00:00
|
|
|
if (db) {
|
|
|
|
vendor_name = db->get_vendor(vendor_id);
|
|
|
|
device_name = db->get_device(vendor_id, device_id);
|
|
|
|
class_name = db->get_class(class_id);
|
|
|
|
}
|
2019-08-14 08:10:54 +00:00
|
|
|
|
2020-02-20 03:09:20 +00:00
|
|
|
if (vendor_name.is_empty())
|
2023-12-16 14:19:34 +00:00
|
|
|
vendor_name = ByteString::formatted("{:04x}", vendor_id);
|
2020-02-20 03:09:20 +00:00
|
|
|
if (device_name.is_empty())
|
2023-12-16 14:19:34 +00:00
|
|
|
device_name = ByteString::formatted("{:04x}", device_id);
|
2020-02-20 03:09:20 +00:00
|
|
|
if (class_name.is_empty())
|
2023-12-16 14:19:34 +00:00
|
|
|
class_name = ByteString::formatted("{:02x}{:02x}", class_id, subclass_id);
|
2019-08-14 08:10:54 +00: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 09:08:38 +00:00
|
|
|
outln(format, domain, bus, device, function, class_name, vendor_name, device_name, revision_id);
|
2022-02-10 13:15:56 +00:00
|
|
|
|
|
|
|
if (!flag_verbose)
|
|
|
|
continue;
|
|
|
|
for (size_t bar_index = 0; bar_index <= 5; bar_index++) {
|
2023-12-16 14:19:34 +00:00
|
|
|
auto bar_value_filename = ByteString::formatted("/sys/bus/pci/{}/bar{}", dir, bar_index);
|
2023-02-09 02:02:46 +00:00
|
|
|
auto bar_value_file = Core::File::open(bar_value_filename, Core::File::OpenMode::Read);
|
2022-09-14 14:50:59 +00:00
|
|
|
if (bar_value_file.is_error()) {
|
|
|
|
dbgln("Error: Could not open {}: {}", bar_value_filename, bar_value_file.error());
|
2022-02-10 13:15:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-09-14 14:50:59 +00:00
|
|
|
|
2022-12-11 16:49:00 +00:00
|
|
|
auto bar_value_contents = bar_value_file.value()->read_until_eof();
|
2022-09-14 14:50:59 +00:00
|
|
|
if (bar_value_contents.is_error()) {
|
|
|
|
dbgln("Error: Could not read {}: {}", bar_value_filename, bar_value_contents.error());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 bar_value = read_hex_string_from_bytebuffer(bar_value_contents.value());
|
2022-02-10 13:15:56 +00:00
|
|
|
if (bar_value == 0)
|
|
|
|
continue;
|
|
|
|
bool memory_region = ((bar_value & 1) == 0);
|
|
|
|
outln(format_region, bar_index, memory_region ? "Memory" : "IO", bar_value);
|
|
|
|
}
|
2022-02-08 17:28:08 +00:00
|
|
|
}
|
2019-08-13 15:32:22 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|