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
|
/* $NetBSD: acpi_dev.c,v 1.2 2021/07/24 11:36:41 jmcneill Exp $ */
/*-
* Copyright (c) 2020 Jared McNeill <jmcneill@invisible.ca>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: acpi_dev.c,v 1.2 2021/07/24 11:36:41 jmcneill Exp $");
#include <sys/param.h>
#include <sys/conf.h>
#include <sys/mman.h>
#include <dev/acpi/acpireg.h>
#include <dev/acpi/acpivar.h>
#define _COMPONENT ACPI_BUS_COMPONENT
ACPI_MODULE_NAME ("acpi_dev")
static dev_type_read(acpi_read);
const struct cdevsw acpi_cdevsw = {
.d_open = nullopen,
.d_close = nullclose,
.d_read = acpi_read,
.d_write = nowrite,
.d_ioctl = noioctl,
.d_stop = nostop,
.d_tty = notty,
.d_poll = nopoll,
.d_mmap = nommap,
.d_kqfilter = nokqfilter,
.d_discard = nodiscard,
.d_flag = D_OTHER | D_MPSAFE,
};
/*
* acpi_find_table_rsdp --
*
* Returns true if the RSDP table is found and overlaps the specified
* physical address. The table's physical start address and length
* are placed in 'paddr' and 'plen' when found.
*
*/
static bool
acpi_find_table_rsdp(ACPI_PHYSICAL_ADDRESS pa,
ACPI_PHYSICAL_ADDRESS *paddr, uint32_t *plen)
{
ACPI_PHYSICAL_ADDRESS table_pa;
table_pa = AcpiOsGetRootPointer();
if (table_pa == 0) {
return false;
}
if (pa >= table_pa && pa < table_pa + sizeof(ACPI_TABLE_RSDP)) {
*paddr = table_pa;
*plen = sizeof(ACPI_TABLE_RSDP);
return true;
}
return false;
}
/*
* acpi_find_table_sdt --
*
* Returns true if the XSDT/RSDT table is found and overlaps the
* specified physical address. The table's physical start address
* and length are placed in 'paddr' and 'plen' when found.
*
*/
static bool
acpi_find_table_sdt(ACPI_PHYSICAL_ADDRESS pa,
ACPI_PHYSICAL_ADDRESS *paddr, uint32_t *plen)
{
ACPI_PHYSICAL_ADDRESS table_pa;
ACPI_TABLE_RSDP *rsdp;
ACPI_TABLE_HEADER *thdr;
uint32_t table_len;
table_pa = AcpiOsGetRootPointer();
KASSERT(table_pa != 0);
/*
* Find the XSDT/RSDT using the RSDP.
*/
rsdp = AcpiOsMapMemory(table_pa, sizeof(ACPI_TABLE_RSDP));
if (rsdp == NULL) {
return false;
}
if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress) {
table_pa = rsdp->XsdtPhysicalAddress;
} else {
table_pa = rsdp->RsdtPhysicalAddress;
}
AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
if (table_pa == 0) {
return false;
}
/*
* Map the XSDT/RSDT and check access.
*/
thdr = AcpiOsMapMemory(table_pa, sizeof(ACPI_TABLE_HEADER));
if (thdr == NULL) {
return false;
}
table_len = thdr->Length;
AcpiOsUnmapMemory(thdr, sizeof(ACPI_TABLE_HEADER));
if (pa >= table_pa && pa < table_pa + table_len) {
*paddr = table_pa;
*plen = table_len;
return true;
}
return false;
}
/*
* acpi_find_table --
*
* Find an ACPI table that overlaps the specified physical address.
* Returns true if the table is found and places the table start
* address into 'paddr' and the length into 'plen'.
*
*/
static bool
acpi_find_table(ACPI_PHYSICAL_ADDRESS pa,
ACPI_PHYSICAL_ADDRESS *paddr, uint32_t *plen)
{
ACPI_TABLE_DESC *tdesc;
bool found_table;
uint32_t i;
/* Check for RSDP access. */
if (acpi_find_table_rsdp(pa, paddr, plen)) {
return true;
}
/* Check for XSDT/RSDT access. */
if (acpi_find_table_sdt(pa, paddr, plen)) {
return true;
}
/* Check for root table access. */
found_table = false;
AcpiUtAcquireMutex(ACPI_MTX_TABLES);
for (i = 0; i < AcpiGbl_RootTableList.CurrentTableCount; i++) {
tdesc = &AcpiGbl_RootTableList.Tables[i];
if (pa >= tdesc->Address &&
pa < tdesc->Address + tdesc->Length) {
*paddr = tdesc->Address;
*plen = tdesc->Length;
found_table = true;
break;
}
}
AcpiUtReleaseMutex(ACPI_MTX_TABLES);
return found_table;
}
/*
* acpi_read --
*
* Read data from an ACPI configuration table that resides in
* physical memory. Only supports reading one table at a time.
*
*/
static int
acpi_read(dev_t dev, struct uio *uio, int flag)
{
ACPI_PHYSICAL_ADDRESS pa, table_pa;
uint32_t table_len;
uint8_t *data;
int error;
size_t len;
if (uio->uio_rw != UIO_READ) {
return EPERM;
}
/* Make sure this is a read of a known table */
if (!acpi_find_table(uio->uio_offset, &table_pa, &table_len)) {
return EIO;
}
/* Copy the contents of the table to user-space */
pa = uio->uio_offset;
len = uimin(table_len - (pa - table_pa), uio->uio_resid);
data = AcpiOsMapMemory(pa, len);
if (data == NULL) {
return ENOMEM;
}
error = uiomove(data, len, uio);
AcpiOsUnmapMemory(data, len);
return error;
}
|