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