Merge trunk HEAD (46152)
[reactos.git] / drivers / bus / acpi / tables / tbxface.c
1 /******************************************************************************
2 *
3 * Module Name: tbxface - Public interfaces to the ACPI subsystem
4 * ACPI table oriented interfaces
5 * $Revision: 1.1 $
6 *
7 *****************************************************************************/
8
9 /*
10 * Copyright (C) 2000, 2001 R. Byron Moore
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27
28 #include <acpi.h>
29
30 #define _COMPONENT ACPI_TABLES
31 MODULE_NAME ("tbxface")
32
33
34 /*******************************************************************************
35 *
36 * FUNCTION: Acpi_load_tables
37 *
38 * PARAMETERS: None
39 *
40 * RETURN: Status
41 *
42 * DESCRIPTION: This function is called to load the ACPI tables from the
43 * provided RSDT
44 *
45 ******************************************************************************/
46
47 ACPI_STATUS
48 acpi_load_tables (
49 ACPI_PHYSICAL_ADDRESS rsdp_physical_address)
50 {
51 ACPI_STATUS status = AE_OK;
52 u32 number_of_tables = 0;
53
54
55 /* Map and validate the RSDP */
56
57 status = acpi_tb_verify_rsdp (rsdp_physical_address);
58 if (ACPI_FAILURE (status)) {
59 REPORT_ERROR (("Acpi_load_tables: RSDP Failed validation: %s\n",
60 acpi_cm_format_exception (status)));
61 goto error_exit;
62 }
63
64 /* Get the RSDT via the RSDP */
65
66 status = acpi_tb_get_table_rsdt (&number_of_tables);
67 if (ACPI_FAILURE (status)) {
68 REPORT_ERROR (("Acpi_load_tables: Could not load RSDT: %s\n",
69 acpi_cm_format_exception (status)));
70 goto error_exit;
71 }
72
73 /* Now get the rest of the tables */
74
75 status = acpi_tb_get_all_tables (number_of_tables, NULL);
76 if (ACPI_FAILURE (status)) {
77 REPORT_ERROR (("Acpi_load_tables: Error getting required tables (DSDT/FADT/FACS): %s\n",
78 acpi_cm_format_exception (status)));
79 goto error_exit;
80 }
81
82
83 /* Load the namespace from the tables */
84
85 status = acpi_ns_load_namespace ();
86 if (ACPI_FAILURE (status)) {
87 REPORT_ERROR (("Acpi_load_tables: Could not load namespace: %s\n",
88 acpi_cm_format_exception (status)));
89 goto error_exit;
90 }
91
92 return (AE_OK);
93
94
95 error_exit:
96 REPORT_ERROR (("Acpi_load_tables: Could not load tables: %s\n",
97 acpi_cm_format_exception (status)));
98
99 return (status);
100 }
101
102
103 /*******************************************************************************
104 *
105 * FUNCTION: Acpi_load_table
106 *
107 * PARAMETERS: Table_ptr - pointer to a buffer containing the entire
108 * table to be loaded
109 *
110 * RETURN: Status
111 *
112 * DESCRIPTION: This function is called to load a table from the caller's
113 * buffer. The buffer must contain an entire ACPI Table including
114 * a valid header. The header fields will be verified, and if it
115 * is determined that the table is invalid, the call will fail.
116 *
117 * If the call fails an appropriate status will be returned.
118 *
119 ******************************************************************************/
120
121 ACPI_STATUS
122 acpi_load_table (
123 ACPI_TABLE_HEADER *table_ptr)
124 {
125 ACPI_STATUS status;
126 ACPI_TABLE_DESC table_info;
127
128
129 if (!table_ptr) {
130 return (AE_BAD_PARAMETER);
131 }
132
133 /* Copy the table to a local buffer */
134
135 status = acpi_tb_get_table (0, table_ptr, &table_info);
136 if (ACPI_FAILURE (status)) {
137 return (status);
138 }
139
140 /* Install the new table into the local data structures */
141
142 status = acpi_tb_install_table (NULL, &table_info);
143 if (ACPI_FAILURE (status)) {
144 /* Free table allocated by Acpi_tb_get_table */
145
146 acpi_tb_delete_single_table (&table_info);
147 return (status);
148 }
149
150
151 status = acpi_ns_load_table (table_info.installed_desc, acpi_gbl_root_node);
152 if (ACPI_FAILURE (status)) {
153 /* Uninstall table and free the buffer */
154
155 acpi_tb_uninstall_table (table_info.installed_desc);
156 return (status);
157 }
158
159
160 return (status);
161 }
162
163
164 /*******************************************************************************
165 *
166 * FUNCTION: Acpi_unload_table
167 *
168 * PARAMETERS: Table_type - Type of table to be unloaded
169 *
170 * RETURN: Status
171 *
172 * DESCRIPTION: This routine is used to force the unload of a table
173 *
174 ******************************************************************************/
175
176 ACPI_STATUS
177 acpi_unload_table (
178 ACPI_TABLE_TYPE table_type)
179 {
180 ACPI_TABLE_DESC *list_head;
181
182
183 /* Parameter validation */
184
185 if (table_type > ACPI_TABLE_MAX) {
186 return (AE_BAD_PARAMETER);
187 }
188
189
190 /* Find all tables of the requested type */
191
192 list_head = &acpi_gbl_acpi_tables[table_type];
193 do {
194 /*
195 * Delete all namespace entries owned by this table. Note that these
196 * entries can appear anywhere in the namespace by virtue of the AML
197 * "Scope" operator. Thus, we need to track ownership by an ID, not
198 * simply a position within the hierarchy
199 */
200
201 acpi_ns_delete_namespace_by_owner (list_head->table_id);
202
203 /* Delete (or unmap) the actual table */
204
205 acpi_tb_delete_acpi_table (table_type);
206
207 } while (list_head != &acpi_gbl_acpi_tables[table_type]);
208
209 return (AE_OK);
210 }
211
212
213 /*******************************************************************************
214 *
215 * FUNCTION: Acpi_get_table_header
216 *
217 * PARAMETERS: Table_type - one of the defined table types
218 * Instance - the non zero instance of the table, allows
219 * support for multiple tables of the same type
220 * see Acpi_gbl_Acpi_table_flag
221 * Out_table_header - pointer to the ACPI_TABLE_HEADER if successful
222 *
223 * DESCRIPTION: This function is called to get an ACPI table header. The caller
224 * supplies an pointer to a data area sufficient to contain an ACPI
225 * ACPI_TABLE_HEADER structure.
226 *
227 * The header contains a length field that can be used to determine
228 * the size of the buffer needed to contain the entire table. This
229 * function is not valid for the RSD PTR table since it does not
230 * have a standard header and is fixed length.
231 *
232 * If the operation fails for any reason an appropriate status will
233 * be returned and the contents of Out_table_header are undefined.
234 *
235 ******************************************************************************/
236
237 ACPI_STATUS
238 acpi_get_table_header (
239 ACPI_TABLE_TYPE table_type,
240 u32 instance,
241 ACPI_TABLE_HEADER *out_table_header)
242 {
243 ACPI_TABLE_HEADER *tbl_ptr;
244 ACPI_STATUS status;
245
246
247 if ((instance == 0) ||
248 (table_type == ACPI_TABLE_RSDP) ||
249 (!out_table_header)) {
250 return (AE_BAD_PARAMETER);
251 }
252
253 /* Check the table type and instance */
254
255 if ((table_type > ACPI_TABLE_MAX) ||
256 (IS_SINGLE_TABLE (acpi_gbl_acpi_table_data[table_type].flags) &&
257 instance > 1)) {
258 return (AE_BAD_PARAMETER);
259 }
260
261
262 /* Get a pointer to the entire table */
263
264 status = acpi_tb_get_table_ptr (table_type, instance, &tbl_ptr);
265 if (ACPI_FAILURE (status)) {
266 return (status);
267 }
268
269 /*
270 * The function will return a NULL pointer if the table is not loaded
271 */
272 if (tbl_ptr == NULL) {
273 return (AE_NOT_EXIST);
274 }
275
276 /*
277 * Copy the header to the caller's buffer
278 */
279 MEMCPY ((void *) out_table_header, (void *) tbl_ptr,
280 sizeof (ACPI_TABLE_HEADER));
281
282 return (status);
283 }
284
285
286 /*******************************************************************************
287 *
288 * FUNCTION: Acpi_get_table
289 *
290 * PARAMETERS: Table_type - one of the defined table types
291 * Instance - the non zero instance of the table, allows
292 * support for multiple tables of the same type
293 * see Acpi_gbl_Acpi_table_flag
294 * Ret_buffer - pointer to a structure containing a buffer to
295 * receive the table
296 *
297 * RETURN: Status
298 *
299 * DESCRIPTION: This function is called to get an ACPI table. The caller
300 * supplies an Out_buffer large enough to contain the entire ACPI
301 * table. The caller should call the Acpi_get_table_header function
302 * first to determine the buffer size needed. Upon completion
303 * the Out_buffer->Length field will indicate the number of bytes
304 * copied into the Out_buffer->Buf_ptr buffer. This table will be
305 * a complete table including the header.
306 *
307 * If the operation fails an appropriate status will be returned
308 * and the contents of Out_buffer are undefined.
309 *
310 ******************************************************************************/
311
312 ACPI_STATUS
313 acpi_get_table (
314 ACPI_TABLE_TYPE table_type,
315 u32 instance,
316 ACPI_BUFFER *ret_buffer)
317 {
318 ACPI_TABLE_HEADER *tbl_ptr;
319 ACPI_STATUS status;
320 u32 ret_buf_len;
321
322
323 /*
324 * If we have a buffer, we must have a length too
325 */
326 if ((instance == 0) ||
327 (!ret_buffer) ||
328 ((!ret_buffer->pointer) && (ret_buffer->length))) {
329 return (AE_BAD_PARAMETER);
330 }
331
332 /* Check the table type and instance */
333
334 if ((table_type > ACPI_TABLE_MAX) ||
335 (IS_SINGLE_TABLE (acpi_gbl_acpi_table_data[table_type].flags) &&
336 instance > 1)) {
337 return (AE_BAD_PARAMETER);
338 }
339
340
341 /* Get a pointer to the entire table */
342
343 status = acpi_tb_get_table_ptr (table_type, instance, &tbl_ptr);
344 if (ACPI_FAILURE (status)) {
345 return (status);
346 }
347
348 /*
349 * Acpi_tb_get_table_ptr will return a NULL pointer if the
350 * table is not loaded.
351 */
352 if (tbl_ptr == NULL) {
353 return (AE_NOT_EXIST);
354 }
355
356 /*
357 * Got a table ptr, assume it's ok and copy it to the user's buffer
358 */
359 if (table_type == ACPI_TABLE_RSDP) {
360 /*
361 * RSD PTR is the only "table" without a header
362 */
363 ret_buf_len = sizeof (RSDP_DESCRIPTOR);
364 }
365 else {
366 ret_buf_len = tbl_ptr->length;
367 }
368
369 /*
370 * Verify we have space in the caller's buffer for the table
371 */
372 if (ret_buffer->length < ret_buf_len) {
373 ret_buffer->length = ret_buf_len;
374 return (AE_BUFFER_OVERFLOW);
375 }
376
377 ret_buffer->length = ret_buf_len;
378
379 MEMCPY ((void *) ret_buffer->pointer, (void *) tbl_ptr, ret_buf_len);
380
381 return (AE_OK);
382 }
383