forked from xinyu391/zircon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.c
More file actions
354 lines (325 loc) · 11.5 KB
/
debug.c
File metadata and controls
354 lines (325 loc) · 11.5 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// Copyright 2016 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <assert.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <acpica/acpi.h>
static inline void do_indent(unsigned int level) {
while (level) {
printf(" ");
level--;
}
}
#define INDENT_PRINTF(...) \
do { \
do_indent(level); \
printf(__VA_ARGS__); \
} while (0)
enum print_resource_request {
CURRENT_RESOURCES,
POSSIBLE_RESOURCES,
};
static ACPI_STATUS acpi_print_resources(
ACPI_HANDLE object,
unsigned int level,
enum print_resource_request type) {
ACPI_BUFFER buffer = {
.Length = ACPI_ALLOCATE_BUFFER,
.Pointer = NULL,
};
ACPI_STATUS status = AE_BAD_PARAMETER;
if (type == POSSIBLE_RESOURCES) {
status = AcpiGetPossibleResources(object, &buffer);
} else if (type == CURRENT_RESOURCES) {
status = AcpiGetCurrentResources(object, &buffer);
} else {
printf("Invalid resource type to print\n");
return AE_BAD_PARAMETER;
;
}
if (status != AE_OK) {
if (buffer.Pointer) {
AcpiOsFree(buffer.Pointer);
}
return status;
}
if (type == POSSIBLE_RESOURCES) {
INDENT_PRINTF("PRS:\n");
} else if (type == CURRENT_RESOURCES) {
INDENT_PRINTF("CRS:\n");
}
uintptr_t entry_addr = (uintptr_t)buffer.Pointer;
ACPI_RESOURCE* res = (ACPI_RESOURCE*)entry_addr;
level += 1;
while (res->Type != ACPI_RESOURCE_TYPE_END_TAG) {
INDENT_PRINTF("Entry: ");
level += 1;
switch (res->Type) {
case ACPI_RESOURCE_TYPE_IO: {
printf("IO\n");
ACPI_RESOURCE_IO* io = &res->Data.Io;
INDENT_PRINTF("io_decode: %d\n", io->IoDecode);
INDENT_PRINTF("alignment: %d\n", io->Alignment);
INDENT_PRINTF("addrlen: %d\n", io->AddressLength);
INDENT_PRINTF("address min: %#04x\n", io->Minimum);
INDENT_PRINTF("address max: %#04x\n", io->Maximum);
break;
}
case ACPI_RESOURCE_TYPE_ADDRESS16: {
printf("Address16\n");
ACPI_RESOURCE_ADDRESS16* a16 = &res->Data.Address16;
INDENT_PRINTF("res_type: %d\n", a16->ResourceType);
INDENT_PRINTF("produce_consume: %d\n", a16->ProducerConsumer);
INDENT_PRINTF("decode: %d\n", a16->Decode);
INDENT_PRINTF("min_addr_fixed: %d\n", a16->MinAddressFixed);
INDENT_PRINTF("max_addr_fixed: %d\n", a16->MaxAddressFixed);
INDENT_PRINTF("address granularity: %#04x\n", a16->Address.Granularity);
INDENT_PRINTF("address min: %#04x\n", a16->Address.Minimum);
INDENT_PRINTF("address max: %#04x\n", a16->Address.Maximum);
INDENT_PRINTF("address xlat offset: %#04x\n", a16->Address.TranslationOffset);
INDENT_PRINTF("address len: %#04x\n", a16->Address.AddressLength);
// TODO: extract MTRR info from a16->Info
break;
}
case ACPI_RESOURCE_TYPE_ADDRESS32: {
printf("Address32\n");
ACPI_RESOURCE_ADDRESS32* a32 = &res->Data.Address32;
INDENT_PRINTF("res_type: %d\n", a32->ResourceType);
INDENT_PRINTF("produce_consume: %d\n", a32->ProducerConsumer);
INDENT_PRINTF("decode: %d\n", a32->Decode);
INDENT_PRINTF("min_addr_fixed: %d\n", a32->MinAddressFixed);
INDENT_PRINTF("max_addr_fixed: %d\n", a32->MaxAddressFixed);
INDENT_PRINTF("address granularity: %#08x\n", a32->Address.Granularity);
INDENT_PRINTF("address min: %#08x\n", a32->Address.Minimum);
INDENT_PRINTF("address max: %#08x\n", a32->Address.Maximum);
INDENT_PRINTF("address xlat offset: %#08x\n", a32->Address.TranslationOffset);
INDENT_PRINTF("address len: %#08x\n", a32->Address.AddressLength);
// TODO: extract MTRR info from a32->Info
break;
}
case ACPI_RESOURCE_TYPE_IRQ: {
printf("IRQ\n");
ACPI_RESOURCE_IRQ* irq = &res->Data.Irq;
INDENT_PRINTF("trigger: %s\n", irq->Triggering == ACPI_EDGE_SENSITIVE ? "edge" : "level");
const char* pol = "invalid";
switch (irq->Polarity) {
case ACPI_ACTIVE_BOTH:
pol = "both";
break;
case ACPI_ACTIVE_LOW:
pol = "low";
break;
case ACPI_ACTIVE_HIGH:
pol = "high";
break;
}
INDENT_PRINTF("polarity: %s\n", pol);
INDENT_PRINTF("sharable: %d\n", irq->Sharable);
INDENT_PRINTF("wake_cap: %d\n", irq->WakeCapable);
for (unsigned int i = 0; i < irq->InterruptCount; ++i) {
INDENT_PRINTF("irq #%d: %d\n", i, irq->Interrupts[i]);
}
break;
}
case ACPI_RESOURCE_TYPE_EXTENDED_IRQ: {
printf("Extended IRQ\n");
ACPI_RESOURCE_EXTENDED_IRQ* irq = &res->Data.ExtendedIrq;
INDENT_PRINTF("produce_consume: %d\n", irq->ProducerConsumer);
INDENT_PRINTF("trigger: %s\n", irq->Triggering == ACPI_EDGE_SENSITIVE ? "edge" : "level");
const char* pol = "invalid";
switch (irq->Polarity) {
case ACPI_ACTIVE_BOTH:
pol = "both";
break;
case ACPI_ACTIVE_LOW:
pol = "low";
break;
case ACPI_ACTIVE_HIGH:
pol = "high";
break;
}
INDENT_PRINTF("polarity: %s\n", pol);
INDENT_PRINTF("sharable: %d\n", irq->Sharable);
INDENT_PRINTF("wake_cap: %d\n", irq->WakeCapable);
for (unsigned int i = 0; i < irq->InterruptCount; ++i) {
INDENT_PRINTF("irq #%d: %d\n", i, irq->Interrupts[i]);
}
break;
}
default:
printf("Unknown (type %u)\n", res->Type);
}
level -= 1;
entry_addr += res->Length;
res = (ACPI_RESOURCE*)entry_addr;
}
level -= 1;
AcpiOsFree(buffer.Pointer);
return AE_OK;
}
static ACPI_STATUS acpi_get_pcie_devices_crs(
ACPI_HANDLE object,
UINT32 nesting_level,
void* context,
void** ret) {
printf("Found object %p\n", object);
return acpi_print_resources(object, 1, CURRENT_RESOURCES);
}
static void acpi_debug_pcie_crs(void) {
ACPI_STATUS status = AcpiGetDevices(
(char*)"PNP0A08",
acpi_get_pcie_devices_crs,
NULL,
NULL);
if (status != AE_OK) {
printf("Could not find PCIe root complex\n");
}
}
static ACPI_STATUS acpi_print_prt(unsigned int level, ACPI_HANDLE object) {
ACPI_STATUS status = AE_OK;
ACPI_BUFFER buffer = {
// Request that the ACPI subsystem allocate the buffer
.Length = ACPI_ALLOCATE_BUFFER,
.Pointer = NULL,
};
status = AcpiGetIrqRoutingTable(object, &buffer);
if (status != AE_OK) {
if (buffer.Pointer) {
AcpiOsFree(buffer.Pointer);
}
return status;
}
assert(buffer.Pointer);
uintptr_t entry_addr = (uintptr_t)buffer.Pointer;
ACPI_PCI_ROUTING_TABLE* entry;
for (entry = (ACPI_PCI_ROUTING_TABLE*)entry_addr;
entry->Length != 0;
entry_addr += entry->Length, entry = (ACPI_PCI_ROUTING_TABLE*)entry_addr) {
assert(entry_addr <= (uintptr_t)buffer.Pointer + buffer.Length);
INDENT_PRINTF("Entry:\n");
level += 1;
if (entry->Pin > 3) {
INDENT_PRINTF("Pin: Invalid (%08x)\n", entry->Pin);
} else {
INDENT_PRINTF("Pin: INT%c\n", 'A' + entry->Pin);
}
INDENT_PRINTF("Address: %#016llx\n", entry->Address);
level += 1;
INDENT_PRINTF("Dev ID: %#04x\n", (uint16_t)(entry->Address >> 16));
level -= 1;
if (entry->Source[0]) {
// If the Source is not just a NULL byte, then it refers to a
// PCI Interrupt Link Device
INDENT_PRINTF("Source: %s\n", entry->Source);
INDENT_PRINTF("Source Index: %u\n", entry->SourceIndex);
ACPI_HANDLE ild;
status = AcpiGetHandle(object, entry->Source, &ild);
if (status != AE_OK) {
INDENT_PRINTF("Could not lookup Interrupt Link Device\n");
continue;
}
status = acpi_print_resources(ild, 2, CURRENT_RESOURCES);
if (status != AE_OK) {
INDENT_PRINTF("Could not lookup ILD CRS\n");
}
status = acpi_print_resources(ild, 2, POSSIBLE_RESOURCES);
if (status != AE_OK) {
INDENT_PRINTF("Could not lookup ILD PRS\n");
}
} else {
// Otherwise, it just refers to a global IRQ number that the pin
// is connected to
INDENT_PRINTF("GlobalIRQ: %u\n", entry->SourceIndex);
}
level -= 1;
}
AcpiOsFree(buffer.Pointer);
return AE_OK;
}
static ACPI_STATUS acpi_get_pcie_devices_irq(
ACPI_HANDLE object,
UINT32 nesting_level,
void* context,
void** ret) {
ACPI_STATUS status = acpi_print_prt(nesting_level, object);
if (status != AE_OK) {
printf("Failed to print PRT for root complex\n");
return status;
}
// Enumerate root ports
ACPI_HANDLE child = NULL;
while (1) {
status = AcpiGetNextObject(ACPI_TYPE_DEVICE, object, child, &child);
if (status == AE_NOT_FOUND) {
break;
} else if (status != AE_OK) {
printf("Failed to get next child object of root complex\n");
return status;
}
ACPI_OBJECT object = {0};
ACPI_BUFFER buffer = {
.Length = sizeof(object),
.Pointer = &object,
};
status = AcpiEvaluateObject(child, (char*)"_ADR", NULL, &buffer);
if (status != AE_OK ||
buffer.Length < sizeof(object) ||
object.Type != ACPI_TYPE_INTEGER) {
continue;
}
UINT64 data = object.Integer.Value;
unsigned int level = nesting_level;
INDENT_PRINTF(
"Device %#02x Function %#01x:\n",
(uint8_t)(data >> 16),
(uint8_t)(data & 0x7));
status = acpi_print_prt(nesting_level + 1, child);
if (status != AE_OK) {
continue;
}
}
return AE_OK;
}
static void acpi_debug_pcie_irq_routing(void) {
ACPI_STATUS status = AcpiGetDevices(
(char*)"PNP0A08",
acpi_get_pcie_devices_irq,
NULL,
NULL);
if (status != AE_OK) {
printf("Could not enumerate PRTs\n");
}
}
static ACPI_STATUS acpi_debug_print_device_name(
ACPI_HANDLE object,
UINT32 nesting_level,
void* context,
void** ret) {
ACPI_DEVICE_INFO* info = NULL;
ACPI_STATUS status = AcpiGetObjectInfo(object, &info);
if (status != AE_OK) {
if (info) {
ACPI_FREE(info);
}
return status;
}
unsigned int level = nesting_level;
INDENT_PRINTF("%4s\n", (char*)&info->Name);
ACPI_FREE(info);
return AE_OK;
}
static void acpi_debug_walk_ns(void) {
ACPI_STATUS status = AcpiWalkNamespace(
ACPI_TYPE_DEVICE,
ACPI_ROOT_OBJECT,
INT_MAX,
acpi_debug_print_device_name,
NULL,
NULL,
NULL);
if (status != AE_OK) {
printf("Failed to walk namespace\n");
}
}