Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <Kernel/ACPI/ACPIParser.h>
28#include <Kernel/KParams.h>
29#include <Kernel/Net/E1000NetworkAdapter.h>
30#include <Kernel/Net/RTL8139NetworkAdapter.h>
31#include <Kernel/PCI/IOAccess.h>
32#include <Kernel/PCI/Initializer.h>
33#include <Kernel/PCI/MMIOAccess.h>
34#include <LibBareMetal/IO.h>
35
36namespace Kernel {
37
38static PCI::Initializer* s_pci_initializer;
39
40PCI::Initializer& PCI::Initializer::the()
41{
42 if (s_pci_initializer == nullptr) {
43 s_pci_initializer = new PCI::Initializer();
44 }
45 return *s_pci_initializer;
46}
47void PCI::Initializer::initialize_pci_mmio_access(PhysicalAddress mcfg)
48{
49 PCI::MMIOAccess::initialize(mcfg);
50 detect_devices();
51}
52void PCI::Initializer::initialize_pci_io_access()
53{
54 PCI::IOAccess::initialize();
55 detect_devices();
56}
57
58void PCI::Initializer::detect_devices()
59{
60 PCI::enumerate_all([&](const PCI::Address& address, PCI::ID id) {
61 kprintf("PCI: device @ %w:%b:%b.%d [%w:%w]\n",
62 address.seg(),
63 address.bus(),
64 address.slot(),
65 address.function(),
66 id.vendor_id,
67 id.device_id);
68 E1000NetworkAdapter::detect(address);
69 RTL8139NetworkAdapter::detect(address);
70 });
71}
72
73void PCI::Initializer::test_and_initialize(bool disable_pci_mmio)
74{
75 if (disable_pci_mmio) {
76 if (test_pci_io()) {
77 initialize_pci_io_access();
78 } else {
79 kprintf("No PCI Bus Access Method Detected, Halt!\n");
80 ASSERT_NOT_REACHED(); // NO PCI Access ?!
81 }
82 return;
83 }
84 if (test_acpi()) {
85 if (test_pci_mmio()) {
86 initialize_pci_mmio_access_after_test();
87 } else {
88 if (test_pci_io()) {
89 initialize_pci_io_access();
90 } else {
91 kprintf("No PCI Bus Access Method Detected, Halt!\n");
92 ASSERT_NOT_REACHED(); // NO PCI Access ?!
93 }
94 }
95 } else {
96 if (test_pci_io()) {
97 initialize_pci_io_access();
98 } else {
99 kprintf("No PCI Bus Access Method Detected, Halt!\n");
100 ASSERT_NOT_REACHED(); // NO PCI Access ?!
101 }
102 }
103}
104PCI::Initializer::Initializer()
105{
106}
107bool PCI::Initializer::test_acpi()
108{
109 if ((KParams::the().has("noacpi")) || !ACPIParser::the().is_operable())
110 return false;
111 else
112 return true;
113}
114
115bool PCI::Initializer::test_pci_io()
116{
117 kprintf("Testing PCI via manual probing... ");
118 u32 tmp = 0x80000000;
119 IO::out32(PCI_ADDRESS_PORT, tmp);
120 tmp = IO::in32(PCI_ADDRESS_PORT);
121 if (tmp == 0x80000000) {
122 kprintf("PCI IO Supported!\n");
123 return true;
124 }
125
126 kprintf("PCI IO Not Supported!\n");
127 return false;
128}
129
130bool PCI::Initializer::test_pci_mmio()
131{
132 return !ACPIParser::the().find_table("MCFG").is_null();
133}
134
135void PCI::Initializer::initialize_pci_mmio_access_after_test()
136{
137 initialize_pci_mmio_access(ACPIParser::the().find_table("MCFG"));
138}
139
140void PCI::Initializer::dismiss()
141{
142 if (s_pci_initializer == nullptr)
143 return;
144 kprintf("PCI Subsystem Initializer dismissed.\n");
145 delete s_pci_initializer;
146 s_pci_initializer = nullptr;
147}
148
149PCI::Initializer::~Initializer()
150{
151}
152
153}