xref: /freebsd/sys/dev/acpica/acpi_quirk.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
2  * Copyright (c) 2004 Nate Lawson (SDG)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 #include <sys/param.h>
29 #include <sys/bus.h>
30 
31 #include <contrib/dev/acpica/include/acpi.h>
32 
33 #include <dev/acpica/acpivar.h>
34 
35 enum ops_t {
36 	OP_NONE,
37 	OP_LEQ,
38 	OP_GEQ,
39 	OP_EQL,
40 };
41 
42 enum val_t {
43 	OEM,
44 	OEM_REV,
45 	CREATOR,
46 	CREATOR_REV,
47 };
48 
49 struct acpi_q_rule {
50     char	sig[ACPI_NAMESEG_SIZE];	/* Table signature to match */
51     enum val_t	val;
52     union {
53 	char	*id;
54 	enum ops_t op;
55     } x;
56     union {
57 	char	*tid;
58 	int	rev;
59     } y;
60 };
61 
62 struct acpi_q_entry {
63     const struct acpi_q_rule *match;
64     int		quirks;
65 };
66 
67 #include "acpi_quirks.h"
68 
69 static int	aq_revcmp(int revision, enum ops_t op, int value);
70 static int	aq_strcmp(char *actual, char *possible);
71 static int	aq_match_header(ACPI_TABLE_HEADER *hdr,
72 		    const struct acpi_q_rule *match);
73 
74 static int
75 aq_revcmp(int revision, enum ops_t op, int value)
76 {
77     switch (op) {
78     case OP_LEQ:
79 	if (revision <= value)
80 	    return (TRUE);
81 	break;
82     case OP_GEQ:
83 	if (revision >= value)
84 	    return (TRUE);
85 	break;
86     case OP_EQL:
87 	if (revision == value)
88 	    return (TRUE);
89 	break;
90     case OP_NONE:
91 	return (TRUE);
92     default:
93 	panic("aq_revcmp: invalid op %d", op);
94     }
95 
96     return (FALSE);
97 }
98 
99 static int
100 aq_strcmp(char *actual, char *possible)
101 {
102     if (actual == NULL || possible == NULL)
103 	return (TRUE);
104     return (strncmp(actual, possible, strlen(possible)) == 0);
105 }
106 
107 static int
108 aq_match_header(ACPI_TABLE_HEADER *hdr, const struct acpi_q_rule *match)
109 {
110     int result;
111 
112     result = FALSE;
113     switch (match->val) {
114     case OEM:
115 	if (aq_strcmp(hdr->OemId, match->x.id) &&
116 	    aq_strcmp(hdr->OemTableId, match->y.tid))
117 	    result = TRUE;
118 	break;
119     case CREATOR:
120 	if (aq_strcmp(hdr->AslCompilerId, match->x.id))
121 	    result = TRUE;
122 	break;
123     case OEM_REV:
124 	if (aq_revcmp(hdr->OemRevision, match->x.op, match->y.rev))
125 	    result = TRUE;
126 	break;
127     case CREATOR_REV:
128 	if (aq_revcmp(hdr->AslCompilerRevision, match->x.op, match->y.rev))
129 	    result = TRUE;
130 	break;
131     }
132 
133     return (result);
134 }
135 
136 int
137 acpi_table_quirks(int *quirks)
138 {
139     const struct acpi_q_entry *entry;
140     const struct acpi_q_rule *match;
141     ACPI_TABLE_HEADER fadt, dsdt, xsdt, *hdr;
142     int done;
143 
144     /* First, allow the machdep system to set its idea of quirks. */
145     KASSERT(quirks != NULL, ("acpi quirks ptr is NULL"));
146     acpi_machdep_quirks(quirks);
147 
148     if (ACPI_FAILURE(AcpiGetTableHeader(ACPI_SIG_FADT, 0, &fadt)))
149 	bzero(&fadt, sizeof(fadt));
150     if (ACPI_FAILURE(AcpiGetTableHeader(ACPI_SIG_DSDT, 0, &dsdt)))
151 	bzero(&dsdt, sizeof(dsdt));
152     if (ACPI_FAILURE(AcpiGetTableHeader(ACPI_SIG_XSDT, 0, &xsdt)))
153 	bzero(&xsdt, sizeof(xsdt));
154 
155     /* Then, override the quirks with any matched from table signatures. */
156     for (entry = acpi_quirks_table; entry->match; entry++) {
157 	done = TRUE;
158 	for (match = entry->match; match->sig[0] != '\0'; match++) {
159 	    if (!strncmp(match->sig, "FADT", ACPI_NAMESEG_SIZE))
160 		hdr = &fadt;
161 	    else if (!strncmp(match->sig, ACPI_SIG_DSDT, ACPI_NAMESEG_SIZE))
162 		hdr = &dsdt;
163 	    else if (!strncmp(match->sig, ACPI_SIG_XSDT, ACPI_NAMESEG_SIZE))
164 		hdr = &xsdt;
165 	    else
166 		panic("invalid quirk header\n");
167 
168 	    /* If we don't match any, skip to the next entry. */
169 	    if (aq_match_header(hdr, match) == FALSE) {
170 		done = FALSE;
171 		break;
172 	    }
173 	}
174 
175 	/* If all entries matched, update the quirks and return. */
176 	if (done) {
177 	    *quirks = entry->quirks;
178 	    break;
179 	}
180     }
181 
182     return (0);
183 }
184