forked from PFLab-OS/Raph_Kernel
/
raph_acpi.cc
269 lines (230 loc) · 7.87 KB
/
raph_acpi.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
*
* Copyright (c) 2015 Raphine Project
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Author: Liva
*
*/
extern "C" {
#include <acpi.h>
#include <accommon.h>
#include <acparser.h>
#include <amlcode.h>
#include <acnamesp.h>
#include <acdebug.h>
#include <actables.h>
#include <acinterp.h>
#include <amlresrc.h>
}
#include <limits.h>
#include <apic.h>
#include <raph_acpi.h>
#include <dev/pci.h>
#include <tty.h>
#include <task.h>
#include <global.h>
#define ACPI_MAX_INIT_TABLES 128
static ACPI_TABLE_DESC TableArray[ACPI_MAX_INIT_TABLES];
void AcpiCtrl::Setup() {
bzero(TableArray, sizeof(ACPI_TABLE_DESC) * ACPI_MAX_INIT_TABLES);
AcpiInitializeTables (TableArray, ACPI_MAX_INIT_TABLES, TRUE);
ACPI_TABLE_HEADER *table;
kassert(!ACPI_FAILURE(AcpiGetTable("APIC", 1, &table)));
apic_ctrl->SetMADT(reinterpret_cast<MADT *>(table));
}
MCFG *AcpiCtrl::GetMCFG() {
ACPI_TABLE_HEADER *table;
kassert(!ACPI_FAILURE(AcpiGetTable("MCFG", 1, &table)));
return reinterpret_cast<MCFG *>(table);
}
HPETDT *AcpiCtrl::GetHPETDT() {
ACPI_TABLE_HEADER *table;
kassert(!ACPI_FAILURE(AcpiGetTable("HPET", 1, &table)));
return reinterpret_cast<HPETDT *>(table);
}
FADT *AcpiCtrl::GetFADT() {
ACPI_TABLE_HEADER *table;
kassert(!ACPI_FAILURE(AcpiGetTable("FACP", 1, &table)));
return reinterpret_cast<FADT *>(table);
}
void AcpiGlobalEventHandler(UINT32 type, ACPI_HANDLE device, UINT32 num, void *context) {
//TODO cpuid
if (num == ACPI_EVENT_POWER_BUTTON) {
task_ctrl->Register(1, reinterpret_cast<Task *>(context));
}
}
void AcpiCtrl::SetupAcpica() {
kassert(!ACPI_FAILURE(AcpiInitializeSubsystem()));
kassert(!ACPI_FAILURE(AcpiReallocateRootTable()));
kassert(!ACPI_FAILURE(AcpiEnableSubsystem(ACPI_FULL_INITIALIZATION)));
kassert(!ACPI_FAILURE(AcpiLoadTables()));
kassert(!ACPI_FAILURE(AcpiInitializeObjects(ACPI_FULL_INITIALIZATION)));
ClassFunction<AcpiCtrl> func;
func.Init(this, &AcpiCtrl::GlobalEventHandler, nullptr);
_global_event_task.SetFunc(func);
AcpiInstallGlobalEventHandler(AcpiGlobalEventHandler, reinterpret_cast<void *>(&_global_event_task));
}
void AcpiCtrl::Shutdown() {
AcpiEnterSleepStatePrep(5);
asm volatile("cli;");
AcpiEnterSleepState(5);
kernel_panic("acpi", "could not halt system");
}
void AcpiCtrl::GlobalEventHandler(void *) {
gtty->Cprintf("shutting down the system...\n");
Shutdown();
}
void AcpiCtrl::Reset() {
AcpiReset();
}
static ACPI_STATUS GetIntNum(ACPI_RESOURCE *resource, void *context) {
int *irq = reinterpret_cast<int *>(context);
switch(resource->Type) {
case ACPI_RESOURCE_TYPE_START_DEPENDENT:
case ACPI_RESOURCE_TYPE_END_TAG: {
return_ACPI_STATUS (AE_OK);
}
case ACPI_RESOURCE_TYPE_IRQ: {
ACPI_RESOURCE_IRQ *p = &resource->Data.Irq;
if (p == nullptr || p->InterruptCount == 0) {
return_ACPI_STATUS (AE_OK);
}
*irq = p->Interrupts[0];
break;
}
case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: {
ACPI_RESOURCE_EXTENDED_IRQ *p = &resource->Data.ExtendedIrq;
if (p == nullptr || p->InterruptCount == 0) {
return_ACPI_STATUS (AE_OK);
}
*irq = p->Interrupts[0];
break;
}
default: {
kassert(false);
}
}
return_ACPI_STATUS (AE_CTRL_TERMINATE);
}
struct ArgContainer1 {
DevPci *device;
PciCtrl::IntPin intpin;
bool source_not_found;
bool source_available_flag;
int source_index;
char *linkname;
};
static ACPI_STATUS GetRouteTable(ACPI_HANDLE obj_handle, UINT32 level, void *context, void **ReturnValue) {
ArgContainer1 *container = reinterpret_cast<ArgContainer1 *>(context);
DevPci *device = container->device;
ACPI_STATUS status;
ACPI_DEVICE_INFO *info;
ACPI_BUFFER buf;
ACPI_OBJECT param;
buf.Pointer = ¶m;
buf.Length = sizeof(param);
status = AcpiGetObjectInfo(obj_handle, &info);
if (ACPI_FAILURE(status)) {
return_ACPI_STATUS (AE_OK);
}
kassert(info->Type == ACPI_TYPE_DEVICE);
if (info->Flags & ACPI_PCI_ROOT_BRIDGE) {
status = AcpiEvaluateObjectTyped(obj_handle, METHOD_NAME__BBN, nullptr, &buf, ACPI_TYPE_INTEGER);
uint8_t bus = 0;
if (ACPI_SUCCESS(status)) {
kassert(param.Type == ACPI_TYPE_INTEGER);
bus = param.Integer.Value;
}
if (bus == device->GetBus()) {
buf.Pointer = nullptr;
buf.Length = ACPI_ALLOCATE_BUFFER;
status = AcpiGetIrqRoutingTable(obj_handle, &buf);
if (ACPI_SUCCESS(status)) {
ACPI_PCI_ROUTING_TABLE *entry = reinterpret_cast<ACPI_PCI_ROUTING_TABLE *>(buf.Pointer);
while((entry != nullptr) && (entry->Length > 0)) {
if ((((entry->Address >> 16) & 0xFFFF) == device->GetDevice()) &&
(entry->Pin + 1 == static_cast<unsigned int>(container->intpin))) {
if (entry->Source[0] == '\0') {
container->source_not_found = false;
container->source_available_flag = false;
container->source_index = entry->SourceIndex;
return_ACPI_STATUS (AE_CTRL_TERMINATE);
}
status = AcpiGetHandle(ACPI_ROOT_OBJECT, entry->Source, &obj_handle);
if (ACPI_SUCCESS(status)) {
status = AcpiGetObjectInfo(obj_handle, &info);
if (ACPI_SUCCESS(status)) {
container->source_not_found = false;
container->source_available_flag = true;
container->linkname = reinterpret_cast<char *>(&info->Name);
return_ACPI_STATUS (AE_CTRL_TERMINATE);
}
}
}
entry = reinterpret_cast<ACPI_PCI_ROUTING_TABLE *>(reinterpret_cast<virt_addr>(entry) + entry->Length);
}
}
}
}
return_ACPI_STATUS (AE_OK);
}
struct ArgContainer2 {
char *linkname;
int irq;
};
static ACPI_STATUS GetIntLink(ACPI_HANDLE obj_handle, UINT32 level, void *context, void **ReturnValue) {
ArgContainer2 *container = reinterpret_cast<ArgContainer2 *>(context);
ACPI_STATUS status;
ACPI_DEVICE_INFO *info;
status = AcpiGetObjectInfo(obj_handle, &info);
if (ACPI_FAILURE(status)) {
return_ACPI_STATUS (AE_OK);
}
kassert(info->Type == ACPI_TYPE_DEVICE);
if (info->Valid & ACPI_VALID_HID &&
!strncmp(info->HardwareId.String, "PNP0C0F", info->HardwareId.Length)) {
if (!strncmp(reinterpret_cast<char *>(&info->Name), container->linkname, 4)) {
int irq = -1;
AcpiWalkResources(obj_handle, METHOD_NAME__CRS, GetIntNum, &irq);
container->irq = irq;
}
}
return_ACPI_STATUS (AE_OK);
}
int AcpiCtrl::GetPciIntNum(DevPci *device) {
// TODO : is MaxDepth valid?
PciCtrl::IntPin intpin = device->GetLegacyIntPin();
if (intpin == PciCtrl::IntPin::kInvalid) {
return -1;
}
ArgContainer1 container1;
container1.device = device;
container1.intpin = intpin;
container1.source_not_found = true;
AcpiWalkNamespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 100, GetRouteTable, nullptr, reinterpret_cast<void *>(&container1), nullptr);
if (container1.source_not_found) {
return -1;
}
if (!container1.source_available_flag) {
return container1.source_index;
}
ArgContainer2 container2;
container2.irq = -1;
container2.linkname = container1.linkname;
AcpiWalkNamespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 100, GetIntLink, nullptr, reinterpret_cast<void *>(&container2), nullptr);
return container2.irq;
}