xref: /freebsd/sys/dev/acpica/acpi_quirk.c (revision 2b743a9e9ddc6736208dc8ca1ce06ce64ad20a19)
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 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 
33 #include <contrib/dev/acpica/acpi.h>
34 #include <dev/acpica/acpivar.h>
35 
36 enum ops_t {
37 	OP_NONE,
38 	OP_LEQ,
39 	OP_GEQ,
40 	OP_EQL,
41 };
42 
43 enum val_t {
44 	OEM,
45 	OEM_REV,
46 	CREATOR,
47 	CREATOR_REV,
48 };
49 
50 #define ACPI_TABLE_END		(ACPI_TABLE_MAX + 1)
51 
52 struct acpi_q_rule {
53     int 	sig;		/* Table signature to match */
54     enum val_t	val;
55     union {
56 	char	*id;
57 	enum ops_t op;
58     } x;
59     union {
60 	char	*tid;
61 	int	rev;
62     } y;
63 };
64 
65 struct acpi_q_entry {
66     const struct acpi_q_rule *match;
67     int		quirks;
68 };
69 
70 #include "acpi_quirks.h"
71 
72 static int	aq_revcmp(int revision, enum ops_t op, int value);
73 static int	aq_strcmp(char *actual, char *possible);
74 static int	aq_match_header(ACPI_TABLE_HEADER *hdr,
75 		    const struct acpi_q_rule *match);
76 
77 static int
78 aq_revcmp(int revision, enum ops_t op, int value)
79 {
80     switch (op) {
81     case OP_LEQ:
82 	if (revision <= value)
83 	    return (TRUE);
84 	break;
85     case OP_GEQ:
86 	if (revision >= value)
87 	    return (TRUE);
88 	break;
89     case OP_EQL:
90 	if (revision == value)
91 	    return (TRUE);
92 	break;
93     case OP_NONE:
94 	return (TRUE);
95     default:
96 	panic("aq_revcmp: invalid op %d", op);
97     }
98 
99     return (FALSE);
100 }
101 
102 static int
103 aq_strcmp(char *actual, char *possible)
104 {
105     if (actual == NULL || possible == NULL)
106 	return (TRUE);
107     return (strncmp(actual, possible, strlen(possible)) == 0);
108 }
109 
110 static int
111 aq_match_header(ACPI_TABLE_HEADER *hdr, const struct acpi_q_rule *match)
112 {
113     int result;
114 
115     result = FALSE;
116     switch (match->val) {
117     case OEM:
118 	if (aq_strcmp(hdr->OemId, match->x.id) &&
119 	    aq_strcmp(hdr->OemTableId, match->y.tid))
120 	    result = TRUE;
121 	break;
122     case CREATOR:
123 	if (aq_strcmp(hdr->AslCompilerId, match->x.id))
124 	    result = TRUE;
125 	break;
126     case OEM_REV:
127 	if (aq_revcmp(hdr->OemRevision, match->x.op, match->y.rev))
128 	    result = TRUE;
129 	break;
130     case CREATOR_REV:
131 	if (aq_revcmp(hdr->AslCompilerRevision, match->x.op, match->y.rev))
132 	    result = TRUE;
133 	break;
134     }
135 
136     return (result);
137 }
138 
139 int
140 acpi_table_quirks(int *quirks)
141 {
142     const struct acpi_q_entry *entry;
143     const struct acpi_q_rule *match;
144     ACPI_TABLE_HEADER *hdr;
145     int done;
146 
147     /* First, allow the machdep system to set its idea of quirks. */
148     KASSERT(quirks != NULL, ("acpi quirks ptr is NULL"));
149     acpi_machdep_quirks(quirks);
150 
151     /* Then, override the quirks with any matched from table signatures. */
152     for (entry = acpi_quirks_table; entry->match; entry++) {
153 	done = TRUE;
154 	for (match = entry->match; match->sig != ACPI_TABLE_END; match++) {
155 	    switch (match->sig) {
156 	    case ACPI_TABLE_FADT:
157 		hdr = (ACPI_TABLE_HEADER *)AcpiGbl_FADT;
158 		break;
159 	    case ACPI_TABLE_DSDT:
160 		hdr = (ACPI_TABLE_HEADER *)AcpiGbl_DSDT;
161 		break;
162 	    case ACPI_TABLE_XSDT:
163 		hdr = (ACPI_TABLE_HEADER *)AcpiGbl_XSDT;
164 		break;
165 	    default:
166 		panic("invalid quirk header\n");
167 	    }
168 
169 	    /* If we don't match any, skip to the next entry. */
170 	    if (!aq_match_header(hdr, match)) {
171 		done = FALSE;
172 		break;
173 	    }
174 	}
175 
176 	/* If all entries matched, update the quirks and return. */
177 	if (done) {
178 	    *quirks = entry->quirks;
179 	    break;
180 	}
181     }
182 
183     return (0);
184 }
185