aboutsummaryrefslogtreecommitdiffstats
path: root/com32/hdt/hdt-cli-acpi.c
blob: 7f4a380cb569a71871e151ae417605865fe1295d (plain)
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
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2009 Erwan Velu - All Rights Reserved
 *
 *   Permission is hereby granted, free of charge, to any person
 *   obtaining a copy of this software and associated documentation
 *   files (the "Software"), to deal in the Software without
 *   restriction, including without limitation the rights to use,
 *   copy, modify, merge, publish, distribute, sublicense, and/or
 *   sell copies of the Software, and to permit persons to whom
 *   the Software is furnished to do so, subject to the following
 *   conditions:
 *
 *   The above copyright notice and this permission notice shall
 *   be included in all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *   OTHER DEALINGS IN THE SOFTWARE.
 *
 * -----------------------------------------------------------------------
*/

#include "hdt-cli.h"
#include "hdt-common.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <acpi/acpi.h>

/* Print ACPI's table header in a defined formating
 * this particular version is made for displaying 32bit addresses*/
static void show_header_32(uint32_t address, s_acpi_description_header * h)
{
    more_printf("%-4s v%03x %-6s %-7s 0x%08x %-4s    0x%08x @ 0x%016x\n",
		h->signature, h->revision, h->oem_id, h->oem_table_id,
		h->oem_revision, h->creator_id, h->creator_revision, address)
}

/* Print ACPI's table header in a defined formating */
static void show_header(uint64_t address, s_acpi_description_header * h)
{
    more_printf("%-4s v%03x %-6s %-7s 0x%08x %-4s    0x%08x @ 0x%016llx\n",
		h->signature, h->revision, h->oem_id, h->oem_table_id,
		h->oem_revision, h->creator_id, h->creator_revision, address)
}

/* That's an helper to visualize columns*/
void show_table_separator()
{
    more_printf
	("----|----|------|--------|----------|-------|-----------|--------------------\n");
}

/* Display the main header before displaying the ACPI tables */
void show_table_name()
{
    more_printf
	("ACPI rev  oem    table_id oem_rev    creator creat_rev  @ address \n");
    show_table_separator();
}

/* called by "show acpi" */
void main_show_acpi(int argc __unused, char **argv __unused,
		    struct s_hardware *hardware)
{
    reset_more_printf();

    if (hardware->is_acpi_valid == false) {
	more_printf("No ACPI Tables detected\n");
	return;
    }

    show_table_name();

    /* RSDP tables aren't using the same headers as the other
     * So let's use a dedicated rendering */
    if (hardware->acpi.rsdp.valid) {
	s_rsdp *r = &hardware->acpi.rsdp;
	more_printf
	    ("RSDP v%03x %-6s                                        @ 0x%016llx\n",
	     r->revision, r->oem_id, r->address);
    }

    if (hardware->acpi.rsdt.valid)
	show_header_32(hardware->acpi.rsdt.address,
		       &hardware->acpi.rsdt.header);

    if (hardware->acpi.xsdt.valid)
	show_header_32(hardware->acpi.xsdt.address,
		       &hardware->acpi.xsdt.header);

    if (hardware->acpi.fadt.valid)
	show_header(hardware->acpi.fadt.address, &hardware->acpi.fadt.header);

    if (hardware->acpi.madt.valid)
	show_header(hardware->acpi.madt.address, &hardware->acpi.madt.header);

    if (hardware->acpi.dsdt.valid)
	show_header(hardware->acpi.dsdt.address, &hardware->acpi.dsdt.header);

    /* SSDT includes many optional tables, let's display them */
    for (int i = 0; i < hardware->acpi.ssdt_count; i++) {
	if ((hardware->acpi.ssdt[i] != NULL) && (hardware->acpi.ssdt[i]->valid))
	    show_header(hardware->acpi.ssdt[i]->address,
			&hardware->acpi.ssdt[i]->header);
    }

    if (hardware->acpi.sbst.valid)
	show_header(hardware->acpi.sbst.address, &hardware->acpi.sbst.header);

    if (hardware->acpi.ecdt.valid)
	show_header(hardware->acpi.ecdt.address, &hardware->acpi.ecdt.header);

    /* FACS isn't having the same headers, let's use a dedicated rendering */
    if (hardware->acpi.facs.valid) {
	s_facs *fa = &hardware->acpi.facs;
	more_printf
	    ("FACS                                                     @ 0x%016llx\n",
	     fa->address);
    }
}

/* Let's display the Processor Local APIC configuration */
static void show_local_apic(s_madt * madt)
{
    if (madt->processor_local_apic_count == 0) {
	more_printf("No Processor Local APIC found\n");
	return;
    }

    /* For all detected logical CPU */
    for (int i = 0; i < madt->processor_local_apic_count; i++) {
	s_processor_local_apic *sla = &madt->processor_local_apic[i];
	char buffer[8];
	memset(buffer, 0, sizeof(buffer));
	strcpy(buffer, "disable");
	/* Let's check if the flags reports the cpu as enabled */
	if ((sla->flags & PROCESSOR_LOCAL_APIC_ENABLE) ==
	    PROCESSOR_LOCAL_APIC_ENABLE)
	    strcpy(buffer, "enable");
	more_printf("CPU #%u, LAPIC (acpi_id[0x%02x] apic_id[0x%02x]) %s\n",
		    sla->apic_id, sla->acpi_id, sla->apic_id, buffer);
    }
}

/* M1PS flags have to be interpreted as strings */
static char *flags_to_string(char *buffer, uint16_t flags)
{
    memset(buffer, 0, sizeof(buffer));
    strcpy(buffer, "default");
    if ((flags & POLARITY_ACTIVE_HIGH) == POLARITY_ACTIVE_HIGH)
	strcpy(buffer, "high");
    else if ((flags & POLARITY_ACTIVE_LOW) == POLARITY_ACTIVE_LOW)
	strcpy(buffer, "low");
    if ((flags & TRIGGER_EDGE) == TRIGGER_EDGE)
	strncat(buffer, " edge", 5);
    else if ((flags & TRIGGER_LEVEL) == TRIGGER_LEVEL)
	strncat(buffer, " level", 6);
    else
	strncat(buffer, " default", 8);

    return buffer;
}

/* Display the local apic NMI configuration */
static void show_local_apic_nmi(s_madt * madt)
{
    if (madt->local_apic_nmi_count == 0) {
	more_printf("No Local APIC NMI found\n");
	return;
    }

    for (int i = 0; i < madt->local_apic_nmi_count; i++) {
	s_local_apic_nmi *slan = &madt->local_apic_nmi[i];
	char buffer[20];
	more_printf("LAPIC_NMI (acpi_id[0x%02x] %s lint(0x%02x))\n",
		    slan->acpi_processor_id, flags_to_string(buffer,
							     slan->flags),
		    slan->local_apic_lint);
    }
}

/* Display the IO APIC configuration */
static void show_io_apic(s_madt * madt)
{
    if (madt->io_apic_count == 0) {
	more_printf("No IO APIC found\n");
	return;
    }

    /* For all IO APICS */
    for (int i = 0; i < madt->io_apic_count; i++) {
	s_io_apic *sio = &madt->io_apic[i];
	char buffer[15];
	memset(buffer, 0, sizeof(buffer));
	/* GSI base reports the GSI configuration
	 * Let's interpret it as string */
	switch (sio->global_system_interrupt_base) {
	case 0:
	    strcpy(buffer, "GSI 0-23");
	    break;
	case 24:
	    strcpy(buffer, "GSI 24-39");
	    break;
	case 40:
	    strcpy(buffer, "GSI 40-55");
	    break;
	default:
	    strcpy(buffer, "GSI Unknown");
	    break;
	}

	more_printf("IO_APIC[%d] : apic_id[0x%02x] adress[0x%08x] %s\n",
		    i, sio->io_apic_id, sio->io_apic_address, buffer);
    }
}

/* Display the interrupt source override configuration */
static void show_interrupt_source_override(s_madt * madt)
{
    if (madt->interrupt_source_override_count == 0) {
	more_printf("No interrupt source override found\n");
	return;
    }

    /* Let's process each interrupt source override */
    for (int i = 0; i < madt->interrupt_source_override_count; i++) {
	s_interrupt_source_override *siso = &madt->interrupt_source_override[i];
	char buffer[20];
	char bus_type[10];
	memset(bus_type, 0, sizeof(bus_type));
	/* Spec report bus type 0 as ISA */
	if (siso->bus == 0)
	    strcpy(bus_type, "ISA");
	else
	    strcpy(bus_type, "unknown");

	more_printf("INT_SRC_OVR (bus %s (%d) bus_irq %d global_irq %d %s)\n",
		    bus_type, siso->bus, siso->source,
		    siso->global_system_interrupt, flags_to_string(buffer,
								   siso->
								   flags));
    }
}

/* Display the madt configuration
 * This is called by acpi> show madt */
static void show_acpi_madt(int argc __unused, char **argv __unused,
			   struct s_hardware *hardware)
{
    if (hardware->is_acpi_valid == false) {
	more_printf("No ACPI Tables detected\n");
	return;
    }

    s_madt *madt = &hardware->acpi.madt;

    if (madt->valid == false) {
	more_printf("No MADT table found\n");
	return;
    }

    show_table_name();
    show_header(madt->address, &madt->header);
    more_printf("Local APIC at 0x%08x\n", madt->local_apic_address);
    show_table_separator();
    show_local_apic(madt);
    show_local_apic_nmi(madt);
    show_io_apic(madt);
    show_interrupt_source_override(madt);
}

struct cli_callback_descr list_acpi_show_modules[] = {
    {
     .name = "madt",
     .exec = show_acpi_madt,
     },
    {
     .name = NULL,
     .exec = NULL,
     },
};

struct cli_module_descr acpi_show_modules = {
    .modules = list_acpi_show_modules,
    .default_callback = main_show_acpi,
};

struct cli_mode_descr acpi_mode = {
    .mode = ACPI_MODE,
    .name = CLI_ACPI,
    .default_modules = NULL,
    .show_modules = &acpi_show_modules,
    .set_modules = NULL,
};