summaryrefslogtreecommitdiff
path: root/sys/dev/acpi/acpi_quirks.c
blob: 7f676c7c1413602ec46f07f9a2bee29d0fb4d5a8 (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
/* $NetBSD: acpi_quirks.c,v 1.22 2020/12/06 11:38:28 jmcneill Exp $ */

/*-
 * Copyright (c) 2011 The NetBSD Foundation, Inc.
 * 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
 */

/*
 * Copyright 2002 Wasabi Systems, Inc.
 * All rights reserved.
 *
 * Written by Frank van der Linden for Wasabi Systems, Inc.
 *
 * 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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *      This product includes software developed for the NetBSD Project by
 *      Wasabi Systems, Inc.
 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
 *    or promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC
 * 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_quirks.c,v 1.22 2020/12/06 11:38:28 jmcneill Exp $");

#include "opt_acpi.h"

#include <sys/param.h>

#include <dev/acpi/acpireg.h>
#include <dev/acpi/acpivar.h>

#define _COMPONENT	ACPI_UTILITIES
ACPI_MODULE_NAME	("acpi_quirks")

#define AQ_GT		0	/* > */
#define AQ_LT		1	/* < */
#define AQ_GTE		2	/* >= */
#define AQ_LTE		3	/* <= */
#define AQ_EQ		4	/* == */

static int acpi_quirks_revcmp(uint32_t, uint32_t, int);

static const struct acpi_quirk acpi_quirks[] = {

	{ ACPI_SIG_FADT, "ASUS  ", 0x30303031, AQ_LTE, "CUV4X-D ",
	  ACPI_QUIRK_BROKEN },

	{ ACPI_SIG_FADT, "PTLTD ", 0x06040000, AQ_LTE, "  FACP  ",
	  ACPI_QUIRK_BROKEN },

	{ ACPI_SIG_FADT, "NVIDIA", 0x06040000, AQ_EQ, "CK8     ",
	  ACPI_QUIRK_IRQ0 },

	{ ACPI_SIG_FADT, "HP    ", 0x06040012, AQ_LTE, "HWPC20F ",
	  ACPI_QUIRK_BROKEN },
};

static int
acpi_quirks_revcmp(uint32_t tabval, uint32_t wanted, int op)
{

	switch (op) {

	case AQ_GT:
		return (tabval > wanted) ? 0 : 1;

	case AQ_LT:
		return (tabval < wanted) ? 0 : 1;

	case AQ_LTE:
		return (tabval <= wanted) ? 0 : 1;

	case AQ_GTE:
		return (tabval >= wanted) ? 0 : 1;

	case AQ_EQ:
		return (tabval == wanted) ? 0 : 1;

	default:
		return 1;
	}
}

#ifdef ACPI_BLACKLIST_YEAR
static int
acpi_quirks_bios_year(void)
{
	const char *datestr = pmf_get_platform("bios-date");
	unsigned long date;

	if (datestr == NULL)
		return -1;

	date = strtoul(datestr, NULL, 10);
	if (date == 0 || date == ULONG_MAX)
		return -1;
	if (date < 19000000 || date > 99999999)
		return -1;
	return date / 10000;
}
#endif

/*
 * Simple function to search the quirk table. Only to be
 * used after AcpiLoadTables() has been successfully called.
 */
int
acpi_find_quirks(void)
{
	ACPI_TABLE_HEADER fadt, dsdt, xsdt, *hdr;
	const struct acpi_quirk *aq;
	ACPI_STATUS rv;
	size_t i, len;

#ifdef ACPI_BLACKLIST_YEAR
	int year = acpi_quirks_bios_year();

	if (year != -1 && year <= ACPI_BLACKLIST_YEAR)
		return ACPI_QUIRK_OLDBIOS;
#endif

	rv = AcpiGetTableHeader(ACPI_SIG_FADT, 0, &fadt);

	if (ACPI_FAILURE(rv))
		(void)memset(&fadt, 0, sizeof(fadt));

	rv = AcpiGetTableHeader(ACPI_SIG_DSDT, 0, &dsdt);

	if (ACPI_FAILURE(rv))
		(void)memset(&dsdt, 0, sizeof(dsdt));

	rv = AcpiGetTableHeader(ACPI_SIG_XSDT, 0, &xsdt);

	if (ACPI_FAILURE(rv))
		(void)memset(&xsdt, 0, sizeof(xsdt));

	for (i = 0; i < __arraycount(acpi_quirks); i++) {

		aq = &acpi_quirks[i];

		if (strncmp(aq->aq_tabletype, ACPI_SIG_DSDT, 4) == 0)
			hdr = &dsdt;
		else if (strncmp(aq->aq_tabletype, ACPI_SIG_XSDT, 4) == 0)
			hdr = &xsdt;
		else if (strncmp(aq->aq_tabletype, ACPI_SIG_FADT, 4) == 0)
			hdr = &fadt;
		else {
			continue;
		}

		len = strlen(aq->aq_oemid);

		if (strncmp(aq->aq_oemid, hdr->OemId, len) != 0)
			continue;

		if (acpi_quirks_revcmp(aq->aq_oemrev,
			hdr->OemRevision, aq->aq_cmpop) != 0)
			continue;

		len = strlen(aq->aq_tabid);

		if (strncmp(aq->aq_tabid, hdr->OemTableId, len) != 0)
			continue;

		return aq->aq_quirks;
	}

	return 0;
}