summaryrefslogtreecommitdiffstats
path: root/drivers/bus/acpi.c
blob: 2515b6633b3301f3ed55a4aa2e44d1e656fac936 (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
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (c) 2019 Ahmad Fatoum
 */

#include <common.h>
#include <driver.h>
#include <init.h>
#include <efi/efi.h>
#include <efi/efi-device.h>
#include <acpi.h>

static struct sig_desc {
	const char sig[4];
	const char *desc;
} signatures[] = {
	/* ACPI 6.3 Table 5-29, Defined DESCRIPTION_HEADER Signatures */
	{ "APIC", "Multiple APIC Description" },
	{ "BERT", "Boot Error Record" },
	{ "BGRT", "Boot Graphics Resource" },
	{ "CPEP", "Corrected Platform Error Polling" },
	{ "DSDT", "Differentiated System Description" },
	{ "ECDT", "Embedded Controller Boot Resource" },
	{ "EINJ", "Error Injection" },
	{ "ERST", "Error Record Serialization" },
	{ "FACP", "Fixed ACPI Description" },
	{ "FACS", "Firmware ACPI Control Structure" },
	{ "FPDT", "Firmware Performance Data" },
	{ "GTDT", "Generic Timer Description" },
	{ "HEST", "Hardware Error Source" },
	{ "MSCT", "Maximum System Characteristics" },
	{ "MPST", "Memory Power State" },
	{ "NFIT", "NVDIMM Firmware Interface" },
	{ "OEM\0", "OEM Specific Information" },
	{ "PCCT", "Platform Communications Channel" },
	{ "PMTT", "Platform Memory Topology" },
	{ "PSDT", "Persistent System Description" },
	{ "RASF", "ACPI RAS Feature" },
	{ "RSDT", "Root System Description" },
	{ "SBST", "Smart Battery Specification" },
	{ "SDEV", "Secure Devices" },
	{ "SLIT", "System Locality Distance Information" },
	{ "SRAT", "System Resource Affinity" },
	{ "SSDT", "Secondary System Description" },
	{ "XSDT", "Extended System Description" },

	/* ACPI 6.3 Table 5-30, Reserved DESCRIPTION_HEADER Signatures */
	{ "BOOT", "Simple BOOT Flag" },
	{ "CSRT", "Core System Resource" },
	{ "DBG2", "Microsoft Debug Port 2" },
	{ "DBGP", "Debug Port" },
	{ "DMAR", "DMA Remapping" },
	{ "DPPT", "DMA Protection Policy" },
	{ "DRTM", "Dynamic Root of Trust for Measurement" },
	{ "ETDT", "(Obsolete) Event Timer Description" },
	{ "HPET", "IA-PC High Precision Event Timer" },
	{ "IBFT", "iSCSI Boot Firmware" },
	{ "IORT", "I/O Remapping" },
	{ "IVRS", "I/O Virtualization Reporting Structure" },
	{ "LPIT", "Low Power Idle" },
	{ "MCFG", "PCI Express memory mapped configuration" },
	{ "MCHI", "Management Controller Host Interface" },
	{ "MSDM", "Microsoft Data Management" },
	{ "SDEI", "Software Delegated Exceptions Interface" },
	{ "SLIC", "Microsoft Software Licensing Specification" },
	{ "SPCR", "Serial Port Console Redirection" },
	{ "SPMI", "Server Platform Management Interface" },
	{ "STAO", "_STA Override" },
	{ "TCPA", "Trusted Computing Platform Alliance Capabilities" },
	{ "TPM2", "Trusted Platform Module 2" },
	{ "UEFI", "UEFI ACPI Data" },
	{ "WAET", "Windows ACPI Emulated Devices" },
	{ "WDAT", "Watch Dog Action" },
	{ "WDRT", "Watchdog Resource" },
	{ "WPBT", "Platform Binary" },
	{ "WSMT", "Windows SMM Security Mitigation" },
	{ "XENV", "Xen Project" },

	/* Others */
	{ "NHLT", "Non-HD Audio" },
	{ "ASF!", "Alert Standard Format" },

	{ /* sentinel */ }
};

static struct acpi_sdt *acpi_get_dev_sdt(struct device_d *dev)
{
	int i;

	for (i = 0; i < dev->num_resources; i++) {
		if (!strcmp(dev->resource[i].name, "SDT"))
			return (struct acpi_sdt *)dev->resource[i].start;
	}

	return NULL;
}

static void acpi_devinfo(struct device_d *dev)
{
	struct acpi_sdt *sdt = acpi_get_dev_sdt(dev);
	struct sig_desc *sig_desc;

	printf("Signature: %.4s", sdt->signature);

	for (sig_desc = signatures; sig_desc->desc; sig_desc++) {
		size_t len = strnlen(sig_desc->sig, 4);

		if (!memcmp(sdt->signature, sig_desc->sig, len)) {
			printf(" (%s Table)", sig_desc->desc);
			break;
		}
	}

	printf("\nRevision: %u\n", sdt->revision);
	printf("OemId: %.6s\n", sdt->oem_id);
	printf("OemTableId: %.8s\n", sdt->oem_table_id);
	printf("OemRevision: %u\n", sdt->oem_revision);
	printf("CreatorId: 0x%08x\n", sdt->creator_id);
	printf("CreatorRevision: %u\n", sdt->creator_revision);
}

static int acpi_register_device(struct device_d *dev, struct acpi_sdt *sdt)
{
	int ret;

	ret = register_device(dev);
	if (ret)
		return ret;

	device_add_resource(dev, "SDT", (resource_size_t)sdt, sdt->len,
		IORESOURCE_MEM | IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY);

	dev_dbg(dev, "registered as ACPI device\n");

	return 0;
}

static struct device_d *acpi_add_device(struct bus_type *bus,
					acpi_sig_t signature)
{
	struct device_d *dev;

	dev = xzalloc(sizeof(*dev));

	dev->bus = bus;
	dev->parent = bus->dev;
	dev->id = DEVICE_ID_DYNAMIC;
	dev->info = acpi_devinfo;

	dev_set_name(dev, "acpi-%.4s", signature);

	return dev;
}

static int acpi_register_devices(struct bus_type *bus)
{
	efi_config_table_t *table = bus->dev->priv;
	struct acpi_rsdp *rsdp;
	struct acpi_rsdt *root;
	size_t entry_count;
	const char *sig;
	int i;

	rsdp = (struct acpi_rsdp *)table->table;

	if (!rsdp)
		return -EFAULT;

	/* ACPI specification v6.3
	 * 5.2.5.2 Finding the RSDP on UEFI Enabled Systems
	 */
	if (memcmp("RSD PTR ", rsdp->signature, sizeof(rsdp->signature))) {
		dev_dbg(bus->dev, "unexpected signature at start of config table: '%.8s'\n",
			rsdp->signature);
		return -ENODEV;
	}

	if (rsdp->revision < 0x02) {
		sig = "RSDT";
		root = (struct acpi_rsdt *)(unsigned long)rsdp->rsdt_addr;
		entry_count = (root->sdt.len - sizeof(struct acpi_rsdt)) / sizeof(u32);
	} else {
		sig = "XSDT";
		root = (struct acpi_rsdt *)((struct acpi2_rsdp *)rsdp)->xsdt_addr;
		entry_count = (root->sdt.len - sizeof(struct acpi_rsdt)) / sizeof(u64);
	}

	if (acpi_sigcmp(sig, root->sdt.signature)) {
		dev_err(bus->dev, "Expected %s, but found '%.4s'.\n",
			sig, root->sdt.signature);
		return -EIO;
	}

	dev_info(bus->dev, "Found %s (OEM: %.8s) with %lu entries\n",
		sig, root->sdt.oem_id, entry_count);

	for (i = 0; i < entry_count; i++) {
		struct acpi_sdt *sdt = root->entries[i];
		acpi_register_device(acpi_add_device(bus, sdt->signature), sdt);
	}

	return 0;
}

static int acpi_bus_match(struct device_d *dev, struct driver_d *drv)
{
	struct acpi_driver *acpidrv = to_acpi_driver(drv);
	struct acpi_sdt *sdt = acpi_get_dev_sdt(dev);

	return acpi_sigcmp(acpidrv->signature, sdt->signature);
}

static int acpi_bus_probe(struct device_d *dev)
{
	return dev->driver->probe(dev);
}

static void acpi_bus_remove(struct device_d *dev)
{
	if (dev->driver->remove)
		dev->driver->remove(dev);
}

struct bus_type acpi_bus = {
	.match = acpi_bus_match,
	.probe = acpi_bus_probe,
	.remove = acpi_bus_remove,
};

static int efi_acpi_probe(void)
{
	efi_config_table_t *table = NULL;
	int i;

	for (i = 0; i < efi_sys_table->nr_tables; i++) {
		efi_config_table_t *ect = &efi_sys_table->tables[i];
		/* take ACPI < 2 table only if no ACPI 2.0 is available */
		if (!efi_guidcmp(ect->guid, EFI_ACPI_20_TABLE_GUID)) {
			acpi_bus.name = "acpi2";
			table = ect;
		} else if (!table && !efi_guidcmp(ect->guid, EFI_ACPI_TABLE_GUID)) {
			acpi_bus.name = "acpi";
			table = ect;
		}
	}

	if (!table)
		return 0;

	bus_register(&acpi_bus);
	acpi_bus.dev->priv = table;

	return acpi_register_devices(&acpi_bus);
}
postcore_initcall(efi_acpi_probe);