- Send the SCM reply packet with the final status after completing the requested...
[reactos.git] / reactos / drivers / bus / acpi / utils / cmobject.c
1 /******************************************************************************
2 *
3 * Module Name: cmobject - ACPI object create/delete/size/cache routines
4 * $Revision: 1.1 $
5 *
6 *****************************************************************************/
7
8 /*
9 * Copyright (C) 2000, 2001 R. Byron Moore
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
26
27 #include <acpi.h>
28
29
30 #define _COMPONENT ACPI_UTILITIES
31 MODULE_NAME ("cmobject")
32
33
34 /*******************************************************************************
35 *
36 * FUNCTION: _Cm_create_internal_object
37 *
38 * PARAMETERS: Address - Address of the memory to deallocate
39 * Component - Component type of caller
40 * Module - Source file name of caller
41 * Line - Line number of caller
42 * Type - ACPI Type of the new object
43 *
44 * RETURN: Object - The new object. Null on failure
45 *
46 * DESCRIPTION: Create and initialize a new internal object.
47 *
48 * NOTE: We always allocate the worst-case object descriptor because
49 * these objects are cached, and we want them to be
50 * one-size-satisifies-any-request. This in itself may not be
51 * the most memory efficient, but the efficiency of the object
52 * cache should more than make up for this!
53 *
54 ******************************************************************************/
55
56 ACPI_OPERAND_OBJECT *
57 _cm_create_internal_object (
58 NATIVE_CHAR *module_name,
59 u32 line_number,
60 u32 component_id,
61 OBJECT_TYPE_INTERNAL type)
62 {
63 ACPI_OPERAND_OBJECT *object;
64
65
66 /* Allocate the raw object descriptor */
67
68 object = _cm_allocate_object_desc (module_name, line_number, component_id);
69 if (!object) {
70 /* Allocation failure */
71
72 return (NULL);
73 }
74
75 /* Save the object type in the object descriptor */
76
77 object->common.type = type;
78
79 /* Init the reference count */
80
81 object->common.reference_count = 1;
82
83 /* Any per-type initialization should go here */
84
85
86 return (object);
87 }
88
89
90 /*******************************************************************************
91 *
92 * FUNCTION: Acpi_cm_valid_internal_object
93 *
94 * PARAMETERS: Operand - Object to be validated
95 *
96 * RETURN: Validate a pointer to be an ACPI_OPERAND_OBJECT
97 *
98 ******************************************************************************/
99
100 u8
101 acpi_cm_valid_internal_object (
102 void *object)
103 {
104
105 /* Check for a null pointer */
106
107 if (!object) {
108 return (FALSE);
109 }
110
111 /* Check for a pointer within one of the ACPI tables */
112
113 if (acpi_tb_system_table_pointer (object)) {
114 return (FALSE);
115 }
116
117 /* Check the descriptor type field */
118
119 if (!VALID_DESCRIPTOR_TYPE (object, ACPI_DESC_TYPE_INTERNAL)) {
120 /* Not an ACPI internal object, do some further checking */
121
122
123
124
125 return (FALSE);
126 }
127
128
129 /* The object appears to be a valid ACPI_OPERAND_OBJECT */
130
131 return (TRUE);
132 }
133
134
135 /*******************************************************************************
136 *
137 * FUNCTION: _Cm_allocate_object_desc
138 *
139 * PARAMETERS: Module_name - Caller's module name (for error output)
140 * Line_number - Caller's line number (for error output)
141 * Component_id - Caller's component ID (for error output)
142 * Message - Error message to use on failure
143 *
144 * RETURN: Pointer to newly allocated object descriptor. Null on error
145 *
146 * DESCRIPTION: Allocate a new object descriptor. Gracefully handle
147 * error conditions.
148 *
149 ******************************************************************************/
150
151 void *
152 _cm_allocate_object_desc (
153 NATIVE_CHAR *module_name,
154 u32 line_number,
155 u32 component_id)
156 {
157 ACPI_OPERAND_OBJECT *object;
158
159
160 acpi_cm_acquire_mutex (ACPI_MTX_CACHES);
161
162 acpi_gbl_object_cache_requests++;
163
164 /* Check the cache first */
165
166 if (acpi_gbl_object_cache) {
167 /* There is an object available, use it */
168
169 object = acpi_gbl_object_cache;
170 acpi_gbl_object_cache = object->cache.next;
171 object->cache.next = NULL;
172
173 acpi_gbl_object_cache_hits++;
174 acpi_gbl_object_cache_depth--;
175
176 acpi_cm_release_mutex (ACPI_MTX_CACHES);
177 }
178
179 else {
180 /* The cache is empty, create a new object */
181
182 acpi_cm_release_mutex (ACPI_MTX_CACHES);
183
184 /* Attempt to allocate new descriptor */
185
186 object = _cm_callocate (sizeof (ACPI_OPERAND_OBJECT), component_id,
187 module_name, line_number);
188 if (!object) {
189 /* Allocation failed */
190
191 _REPORT_ERROR (module_name, line_number, component_id,
192 ("Could not allocate an object descriptor\n"));
193
194 return (NULL);
195 }
196
197 /* Memory allocation metrics - compiled out in non debug mode. */
198
199 INCREMENT_OBJECT_METRICS (sizeof (ACPI_OPERAND_OBJECT));
200 }
201
202 /* Mark the descriptor type */
203
204 object->common.data_type = ACPI_DESC_TYPE_INTERNAL;
205
206 return (object);
207 }
208
209
210 /*******************************************************************************
211 *
212 * FUNCTION: Acpi_cm_delete_object_desc
213 *
214 * PARAMETERS: Object - Acpi internal object to be deleted
215 *
216 * RETURN: None.
217 *
218 * DESCRIPTION: Free an ACPI object descriptor or add it to the object cache
219 *
220 ******************************************************************************/
221
222 void
223 acpi_cm_delete_object_desc (
224 ACPI_OPERAND_OBJECT *object)
225 {
226
227
228 /* Make sure that the object isn't already in the cache */
229
230 if (object->common.data_type == (ACPI_DESC_TYPE_INTERNAL | ACPI_CACHED_OBJECT)) {
231 return;
232 }
233
234 /* Object must be an ACPI_OPERAND_OBJECT */
235
236 if (object->common.data_type != ACPI_DESC_TYPE_INTERNAL) {
237 return;
238 }
239
240
241 /* If cache is full, just free this object */
242
243 if (acpi_gbl_object_cache_depth >= MAX_OBJECT_CACHE_DEPTH) {
244 /*
245 * Memory allocation metrics. Call the macro here since we only
246 * care about dynamically allocated objects.
247 */
248 DECREMENT_OBJECT_METRICS (sizeof (ACPI_OPERAND_OBJECT));
249
250 acpi_cm_free (object);
251 return;
252 }
253
254 acpi_cm_acquire_mutex (ACPI_MTX_CACHES);
255
256 /* Clear the entire object. This is important! */
257
258 MEMSET (object, 0, sizeof (ACPI_OPERAND_OBJECT));
259 object->common.data_type = ACPI_DESC_TYPE_INTERNAL | ACPI_CACHED_OBJECT;
260
261 /* Put the object at the head of the global cache list */
262
263 object->cache.next = acpi_gbl_object_cache;
264 acpi_gbl_object_cache = object;
265 acpi_gbl_object_cache_depth++;
266
267
268 acpi_cm_release_mutex (ACPI_MTX_CACHES);
269 return;
270 }
271
272
273 /*******************************************************************************
274 *
275 * FUNCTION: Acpi_cm_delete_object_cache
276 *
277 * PARAMETERS: None
278 *
279 * RETURN: Status
280 *
281 * DESCRIPTION: Purge the global state object cache. Used during subsystem
282 * termination.
283 *
284 ******************************************************************************/
285
286 void
287 acpi_cm_delete_object_cache (
288 void)
289 {
290 ACPI_OPERAND_OBJECT *next;
291
292
293 /* Traverse the global cache list */
294
295 while (acpi_gbl_object_cache) {
296 /* Delete one cached state object */
297
298 next = acpi_gbl_object_cache->cache.next;
299 acpi_gbl_object_cache->cache.next = NULL;
300
301 /*
302 * Memory allocation metrics. Call the macro here since we only
303 * care about dynamically allocated objects.
304 */
305 DECREMENT_OBJECT_METRICS (sizeof (ACPI_OPERAND_OBJECT));
306
307 acpi_cm_free (acpi_gbl_object_cache);
308 acpi_gbl_object_cache = next;
309 acpi_gbl_object_cache_depth--;
310 }
311
312 return;
313 }
314
315
316 /*******************************************************************************
317 *
318 * FUNCTION: Acpi_cm_init_static_object
319 *
320 * PARAMETERS: Obj_desc - Pointer to a "static" object - on stack
321 * or in the data segment.
322 *
323 * RETURN: None.
324 *
325 * DESCRIPTION: Initialize a static object. Sets flags to disallow dynamic
326 * deletion of the object.
327 *
328 ******************************************************************************/
329
330 void
331 acpi_cm_init_static_object (
332 ACPI_OPERAND_OBJECT *obj_desc)
333 {
334
335
336 if (!obj_desc) {
337 return;
338 }
339
340
341 /*
342 * Clear the entire descriptor
343 */
344 MEMSET ((void *) obj_desc, 0, sizeof (ACPI_OPERAND_OBJECT));
345
346
347 /*
348 * Initialize the header fields
349 * 1) This is an ACPI_OPERAND_OBJECT descriptor
350 * 2) The size is the full object (worst case)
351 * 3) The flags field indicates static allocation
352 * 4) Reference count starts at one (not really necessary since the
353 * object can't be deleted, but keeps everything sane)
354 */
355
356 obj_desc->common.data_type = ACPI_DESC_TYPE_INTERNAL;
357 obj_desc->common.flags = AOPOBJ_STATIC_ALLOCATION;
358 obj_desc->common.reference_count = 1;
359
360 return;
361 }
362
363
364 /*******************************************************************************
365 *
366 * FUNCTION: Acpi_cm_get_simple_object_size
367 *
368 * PARAMETERS: *Internal_object - Pointer to the object we are examining
369 * *Ret_length - Where the length is returned
370 *
371 * RETURN: Status
372 *
373 * DESCRIPTION: This function is called to determine the space required to
374 * contain a simple object for return to an API user.
375 *
376 * The length includes the object structure plus any additional
377 * needed space.
378 *
379 ******************************************************************************/
380
381 ACPI_STATUS
382 acpi_cm_get_simple_object_size (
383 ACPI_OPERAND_OBJECT *internal_object,
384 u32 *obj_length)
385 {
386 u32 length;
387 ACPI_STATUS status = AE_OK;
388
389
390 /* Handle a null object (Could be a uninitialized package element -- which is legal) */
391
392 if (!internal_object) {
393 *obj_length = 0;
394 return (AE_OK);
395 }
396
397
398 /* Start with the length of the Acpi object */
399
400 length = sizeof (ACPI_OBJECT);
401
402 if (VALID_DESCRIPTOR_TYPE (internal_object, ACPI_DESC_TYPE_NAMED)) {
403 /* Object is a named object (reference), just return the length */
404
405 *obj_length = (u32) ROUND_UP_TO_NATIVE_WORD (length);
406 return (status);
407 }
408
409
410 /*
411 * The final length depends on the object type
412 * Strings and Buffers are packed right up against the parent object and
413 * must be accessed bytewise or there may be alignment problems on
414 * certain processors
415 */
416
417 switch (internal_object->common.type) {
418
419 case ACPI_TYPE_STRING:
420
421 length += internal_object->string.length + 1;
422 break;
423
424
425 case ACPI_TYPE_BUFFER:
426
427 length += internal_object->buffer.length;
428 break;
429
430
431 case ACPI_TYPE_INTEGER:
432 case ACPI_TYPE_PROCESSOR:
433 case ACPI_TYPE_POWER:
434
435 /*
436 * No extra data for these types
437 */
438 break;
439
440
441 case INTERNAL_TYPE_REFERENCE:
442
443 /*
444 * The only type that should be here is opcode AML_NAMEPATH_OP -- since
445 * this means an object reference
446 */
447 if (internal_object->reference.opcode != AML_NAMEPATH_OP) {
448 status = AE_TYPE;
449 }
450
451 else {
452 /*
453 * Get the actual length of the full pathname to this object.
454 * The reference will be converted to the pathname to the object
455 */
456 length += ROUND_UP_TO_NATIVE_WORD (acpi_ns_get_pathname_length (internal_object->reference.node));
457 }
458 break;
459
460
461 default:
462
463 status = AE_TYPE;
464 break;
465 }
466
467
468 /*
469 * Account for the space required by the object rounded up to the next
470 * multiple of the machine word size. This keeps each object aligned
471 * on a machine word boundary. (preventing alignment faults on some
472 * machines.)
473 */
474 *obj_length = (u32) ROUND_UP_TO_NATIVE_WORD (length);
475
476 return (status);
477 }
478
479
480 /*******************************************************************************
481 *
482 * FUNCTION: Acpi_cm_get_element_length
483 *
484 * PARAMETERS: ACPI_PKG_CALLBACK
485 *
486 * RETURN: Status - the status of the call
487 *
488 * DESCRIPTION: Get the length of one package element.
489 *
490 ******************************************************************************/
491
492 ACPI_STATUS
493 acpi_cm_get_element_length (
494 u8 object_type,
495 ACPI_OPERAND_OBJECT *source_object,
496 ACPI_GENERIC_STATE *state,
497 void *context)
498 {
499 ACPI_STATUS status = AE_OK;
500 ACPI_PKG_INFO *info = (ACPI_PKG_INFO *) context;
501 u32 object_space;
502
503
504 switch (object_type) {
505 case 0:
506
507 /*
508 * Simple object - just get the size (Null object/entry is handled
509 * here also) and sum it into the running package length
510 */
511 status = acpi_cm_get_simple_object_size (source_object, &object_space);
512 if (ACPI_FAILURE (status)) {
513 return (status);
514 }
515
516 info->length += object_space;
517 break;
518
519
520 case 1:
521 /* Package - nothing much to do here, let the walk handle it */
522
523 info->num_packages++;
524 state->pkg.this_target_obj = NULL;
525 break;
526
527 default:
528 return (AE_BAD_PARAMETER);
529 }
530
531
532 return (status);
533 }
534
535
536 /*******************************************************************************
537 *
538 * FUNCTION: Acpi_cm_get_package_object_size
539 *
540 * PARAMETERS: *Internal_object - Pointer to the object we are examining
541 * *Ret_length - Where the length is returned
542 *
543 * RETURN: Status
544 *
545 * DESCRIPTION: This function is called to determine the space required to
546 * contain a package object for return to an API user.
547 *
548 * This is moderately complex since a package contains other
549 * objects including packages.
550 *
551 ******************************************************************************/
552
553 ACPI_STATUS
554 acpi_cm_get_package_object_size (
555 ACPI_OPERAND_OBJECT *internal_object,
556 u32 *obj_length)
557 {
558 ACPI_STATUS status;
559 ACPI_PKG_INFO info;
560
561
562 info.length = 0;
563 info.object_space = 0;
564 info.num_packages = 1;
565
566 status = acpi_cm_walk_package_tree (internal_object, NULL,
567 acpi_cm_get_element_length, &info);
568
569 /*
570 * We have handled all of the objects in all levels of the package.
571 * just add the length of the package objects themselves.
572 * Round up to the next machine word.
573 */
574 info.length += ROUND_UP_TO_NATIVE_WORD (sizeof (ACPI_OBJECT)) *
575 info.num_packages;
576
577 /* Return the total package length */
578
579 *obj_length = info.length;
580 return (status);
581 }
582
583
584 /*******************************************************************************
585 *
586 * FUNCTION: Acpi_cm_get_object_size
587 *
588 * PARAMETERS: *Internal_object - Pointer to the object we are examining
589 * *Ret_length - Where the length will be returned
590 *
591 * RETURN: Status
592 *
593 * DESCRIPTION: This function is called to determine the space required to
594 * contain an object for return to an API user.
595 *
596 ******************************************************************************/
597
598 ACPI_STATUS
599 acpi_cm_get_object_size(
600 ACPI_OPERAND_OBJECT *internal_object,
601 u32 *obj_length)
602 {
603 ACPI_STATUS status;
604
605
606 if ((VALID_DESCRIPTOR_TYPE (internal_object, ACPI_DESC_TYPE_INTERNAL)) &&
607 (IS_THIS_OBJECT_TYPE (internal_object, ACPI_TYPE_PACKAGE))) {
608 status = acpi_cm_get_package_object_size (internal_object, obj_length);
609 }
610
611 else {
612 status = acpi_cm_get_simple_object_size (internal_object, obj_length);
613 }
614
615 return (status);
616 }
617
618