sync with trunk r46493
[reactos.git] / drivers / bus / acpi / executer / amcreate.c
1 /******************************************************************************
2 *
3 * Module Name: amcreate - Named object creation
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_EXECUTER
31 MODULE_NAME ("amcreate")
32
33
34 /*******************************************************************************
35 *
36 * FUNCTION: Acpi_aml_exec_create_field
37 *
38 * PARAMETERS: Opcode - The opcode to be executed
39 * Operands - List of operands for the opcode
40 *
41 * RETURN: Status
42 *
43 * DESCRIPTION: Execute Create_field operators: Create_bit_field_op,
44 * Create_byte_field_op, Create_word_field_op, Create_dWord_field_op,
45 * Create_field_op (which define fields in buffers)
46 *
47 * ALLOCATION: Deletes Create_field_op's count operand descriptor
48 *
49 *
50 * ACPI SPECIFICATION REFERENCES:
51 * Def_create_bit_field := Create_bit_field_op Src_buf Bit_idx Name_string
52 * Def_create_byte_field := Create_byte_field_op Src_buf Byte_idx Name_string
53 * Def_create_dWord_field := Create_dWord_field_op Src_buf Byte_idx Name_string
54 * Def_create_field := Create_field_op Src_buf Bit_idx Num_bits Name_string
55 * Def_create_word_field := Create_word_field_op Src_buf Byte_idx Name_string
56 * Bit_index := Term_arg=>Integer
57 * Byte_index := Term_arg=>Integer
58 * Num_bits := Term_arg=>Integer
59 * Source_buff := Term_arg=>Buffer
60 *
61 ******************************************************************************/
62
63
64 ACPI_STATUS
65 acpi_aml_exec_create_field (
66 u8 *aml_ptr,
67 u32 aml_length,
68 ACPI_NAMESPACE_NODE *node,
69 ACPI_WALK_STATE *walk_state)
70 {
71 ACPI_STATUS status;
72 ACPI_OPERAND_OBJECT *obj_desc;
73 ACPI_OPERAND_OBJECT *tmp_desc;
74
75
76 /* Create the region descriptor */
77
78 obj_desc = acpi_cm_create_internal_object (ACPI_TYPE_FIELD_UNIT);
79 if (!obj_desc) {
80 status = AE_NO_MEMORY;
81 goto cleanup;
82 }
83
84 /* Construct the field object */
85
86 obj_desc->field_unit.access = (u8) ACCESS_ANY_ACC;
87 obj_desc->field_unit.lock_rule = (u8) GLOCK_NEVER_LOCK;
88 obj_desc->field_unit.update_rule = (u8) UPDATE_PRESERVE;
89
90 /*
91 * Allocate a method object for this field unit
92 */
93
94 obj_desc->field_unit.extra = acpi_cm_create_internal_object (
95 INTERNAL_TYPE_EXTRA);
96 if (!obj_desc->field_unit.extra) {
97 status = AE_NO_MEMORY;
98 goto cleanup;
99 }
100
101 /*
102 * Remember location in AML stream of the field unit
103 * opcode and operands -- since the buffer and index
104 * operands must be evaluated.
105 */
106
107 obj_desc->field_unit.extra->extra.pcode = aml_ptr;
108 obj_desc->field_unit.extra->extra.pcode_length = aml_length;
109 obj_desc->field_unit.node = node;
110
111
112 /*
113 * This operation is supposed to cause the destination Name to refer
114 * to the defined Field_unit -- it must not store the constructed
115 * Field_unit object (or its current value) in some location that the
116 * Name may already be pointing to. So, if the Name currently contains
117 * a reference which would cause Acpi_aml_exec_store() to perform an indirect
118 * store rather than setting the value of the Name itself, clobber that
119 * reference before calling Acpi_aml_exec_store().
120 */
121
122 /* Type of Name's existing value */
123
124 switch (acpi_ns_get_type (node)) {
125
126 case ACPI_TYPE_FIELD_UNIT:
127
128 case INTERNAL_TYPE_ALIAS:
129 case INTERNAL_TYPE_BANK_FIELD:
130 case INTERNAL_TYPE_DEF_FIELD:
131 case INTERNAL_TYPE_INDEX_FIELD:
132
133 tmp_desc = acpi_ns_get_attached_object (node);
134 if (tmp_desc) {
135 /*
136 * There is an existing object here; delete it and zero out the
137 * object field within the Node
138 */
139
140 acpi_cm_remove_reference (tmp_desc);
141 acpi_ns_attach_object ((ACPI_NAMESPACE_NODE *) node, NULL,
142 ACPI_TYPE_ANY);
143 }
144
145 /* Set the type to ANY (or the store below will fail) */
146
147 ((ACPI_NAMESPACE_NODE *) node)->type = ACPI_TYPE_ANY;
148
149 break;
150
151
152 default:
153
154 break;
155 }
156
157
158 /* Store constructed field descriptor in result location */
159
160 status = acpi_aml_exec_store (obj_desc, (ACPI_OPERAND_OBJECT *) node, walk_state);
161
162 /*
163 * If the field descriptor was not physically stored (or if a failure
164 * above), we must delete it
165 */
166 if (obj_desc->common.reference_count <= 1) {
167 acpi_cm_remove_reference (obj_desc);
168 }
169
170
171 return (AE_OK);
172
173
174 cleanup:
175
176 /* Delete region object and method subobject */
177
178 if (obj_desc) {
179 /* Remove deletes both objects! */
180
181 acpi_cm_remove_reference (obj_desc);
182 obj_desc = NULL;
183 }
184
185 return (status);
186 }
187
188
189 /*****************************************************************************
190 *
191 * FUNCTION: Acpi_aml_exec_create_alias
192 *
193 * PARAMETERS: Operands - List of operands for the opcode
194 *
195 * RETURN: Status
196 *
197 * DESCRIPTION: Create a new named alias
198 *
199 ****************************************************************************/
200
201 ACPI_STATUS
202 acpi_aml_exec_create_alias (
203 ACPI_WALK_STATE *walk_state)
204 {
205 ACPI_NAMESPACE_NODE *source_node;
206 ACPI_NAMESPACE_NODE *alias_node;
207 ACPI_STATUS status;
208
209
210 /* Get the source/alias operands (both NTEs) */
211
212 status = acpi_ds_obj_stack_pop_object ((ACPI_OPERAND_OBJECT **) &source_node,
213 walk_state);
214 if (ACPI_FAILURE (status)) {
215 return (status);
216 }
217
218 /*
219 * Don't pop it, it gets removed in the calling routine
220 */
221
222 alias_node = acpi_ds_obj_stack_get_value (0, walk_state);
223
224 /* Add an additional reference to the object */
225
226 acpi_cm_add_reference (source_node->object);
227
228 /*
229 * Attach the original source Node to the new Alias Node.
230 */
231 status = acpi_ns_attach_object (alias_node, source_node->object,
232 source_node->type);
233
234
235 /*
236 * The new alias assumes the type of the source, but it points
237 * to the same object. The reference count of the object has two
238 * additional references to prevent deletion out from under either the
239 * source or the alias Node
240 */
241
242 /* Since both operands are NTEs, we don't need to delete them */
243
244 return (status);
245 }
246
247
248 /*****************************************************************************
249 *
250 * FUNCTION: Acpi_aml_exec_create_event
251 *
252 * PARAMETERS: None
253 *
254 * RETURN: Status
255 *
256 * DESCRIPTION: Create a new event object
257 *
258 ****************************************************************************/
259
260 ACPI_STATUS
261 acpi_aml_exec_create_event (
262 ACPI_WALK_STATE *walk_state)
263 {
264 ACPI_STATUS status;
265 ACPI_OPERAND_OBJECT *obj_desc;
266
267
268 BREAKPOINT3;
269
270 obj_desc = acpi_cm_create_internal_object (ACPI_TYPE_EVENT);
271 if (!obj_desc) {
272 status = AE_NO_MEMORY;
273 goto cleanup;
274 }
275
276 /* Create the actual OS semaphore */
277
278 /* TBD: [Investigate] should be created with 0 or 1 units? */
279
280 status = acpi_os_create_semaphore (ACPI_NO_UNIT_LIMIT, 1,
281 &obj_desc->event.semaphore);
282 if (ACPI_FAILURE (status)) {
283 acpi_cm_remove_reference (obj_desc);
284 goto cleanup;
285 }
286
287 /* Attach object to the Node */
288
289 status = acpi_ns_attach_object (acpi_ds_obj_stack_get_value (0, walk_state),
290 obj_desc, (u8) ACPI_TYPE_EVENT);
291 if (ACPI_FAILURE (status)) {
292 acpi_os_delete_semaphore (obj_desc->event.semaphore);
293 acpi_cm_remove_reference (obj_desc);
294 goto cleanup;
295 }
296
297
298 cleanup:
299
300 return (status);
301 }
302
303
304 /*****************************************************************************
305 *
306 * FUNCTION: Acpi_aml_exec_create_mutex
307 *
308 * PARAMETERS: Interpreter_mode - Current running mode (load1/Load2/Exec)
309 * Operands - List of operands for the opcode
310 *
311 * RETURN: Status
312 *
313 * DESCRIPTION: Create a new mutex object
314 *
315 ****************************************************************************/
316
317 ACPI_STATUS
318 acpi_aml_exec_create_mutex (
319 ACPI_WALK_STATE *walk_state)
320 {
321 ACPI_STATUS status = AE_OK;
322 ACPI_OPERAND_OBJECT *sync_desc;
323 ACPI_OPERAND_OBJECT *obj_desc;
324
325
326 /* Get the operand */
327
328 status = acpi_ds_obj_stack_pop_object (&sync_desc, walk_state);
329 if (ACPI_FAILURE (status)) {
330 return (status);
331 }
332
333 /* Attempt to allocate a new object */
334
335 obj_desc = acpi_cm_create_internal_object (ACPI_TYPE_MUTEX);
336 if (!obj_desc) {
337 status = AE_NO_MEMORY;
338 goto cleanup;
339 }
340
341 /* Create the actual OS semaphore */
342
343 status = acpi_os_create_semaphore (1, 1, &obj_desc->mutex.semaphore);
344 if (ACPI_FAILURE (status)) {
345 acpi_cm_remove_reference (obj_desc);
346 goto cleanup;
347 }
348
349 obj_desc->mutex.sync_level = (u8) sync_desc->integer.value;
350
351 /* Obj_desc was on the stack top, and the name is below it */
352
353 status = acpi_ns_attach_object (acpi_ds_obj_stack_get_value (0, walk_state),
354 obj_desc, (u8) ACPI_TYPE_MUTEX);
355 if (ACPI_FAILURE (status)) {
356 acpi_os_delete_semaphore (obj_desc->mutex.semaphore);
357 acpi_cm_remove_reference (obj_desc);
358 goto cleanup;
359 }
360
361
362 cleanup:
363
364 /* Always delete the operand */
365
366 acpi_cm_remove_reference (sync_desc);
367
368 return (status);
369 }
370
371
372 /*****************************************************************************
373 *
374 * FUNCTION: Acpi_aml_exec_create_region
375 *
376 * PARAMETERS: Aml_ptr - Pointer to the region declaration AML
377 * Aml_length - Max length of the declaration AML
378 * Operands - List of operands for the opcode
379 * Interpreter_mode - Load1/Load2/Execute
380 *
381 * RETURN: Status
382 *
383 * DESCRIPTION: Create a new operation region object
384 *
385 ****************************************************************************/
386
387 ACPI_STATUS
388 acpi_aml_exec_create_region (
389 u8 *aml_ptr,
390 u32 aml_length,
391 u8 region_space,
392 ACPI_WALK_STATE *walk_state)
393 {
394 ACPI_STATUS status;
395 ACPI_OPERAND_OBJECT *obj_desc;
396 ACPI_NAMESPACE_NODE *node;
397
398
399 /*
400 * Space ID must be one of the predefined IDs, or in the user-defined
401 * range
402 */
403 if ((region_space >= NUM_REGION_TYPES) &&
404 (region_space < USER_REGION_BEGIN)) {
405 REPORT_ERROR (("Invalid Address_space type %X\n", region_space));
406 return (AE_AML_INVALID_SPACE_ID);
407 }
408
409
410 /* Get the Node from the object stack */
411
412 node = (ACPI_NAMESPACE_NODE *) acpi_ds_obj_stack_get_value (0, walk_state);
413
414 /* Create the region descriptor */
415
416 obj_desc = acpi_cm_create_internal_object (ACPI_TYPE_REGION);
417 if (!obj_desc) {
418 status = AE_NO_MEMORY;
419 goto cleanup;
420 }
421
422 /*
423 * Allocate a method object for this region.
424 */
425
426 obj_desc->region.extra = acpi_cm_create_internal_object (
427 INTERNAL_TYPE_EXTRA);
428 if (!obj_desc->region.extra) {
429 status = AE_NO_MEMORY;
430 goto cleanup;
431 }
432
433 /*
434 * Remember location in AML stream of address & length
435 * operands since they need to be evaluated at run time.
436 */
437
438 obj_desc->region.extra->extra.pcode = aml_ptr;
439 obj_desc->region.extra->extra.pcode_length = aml_length;
440
441 /* Init the region from the operands */
442
443 obj_desc->region.space_id = region_space;
444 obj_desc->region.address = 0;
445 obj_desc->region.length = 0;
446
447
448 /* Install the new region object in the parent Node */
449
450 obj_desc->region.node = node;
451
452 status = acpi_ns_attach_object (node, obj_desc,
453 (u8) ACPI_TYPE_REGION);
454
455 if (ACPI_FAILURE (status)) {
456 goto cleanup;
457 }
458
459 /*
460 * If we have a valid region, initialize it
461 * Namespace is NOT locked at this point.
462 */
463
464 status = acpi_ev_initialize_region (obj_desc, FALSE);
465
466 if (ACPI_FAILURE (status)) {
467 /*
468 * If AE_NOT_EXIST is returned, it is not fatal
469 * because many regions get created before a handler
470 * is installed for said region.
471 */
472 if (AE_NOT_EXIST == status) {
473 status = AE_OK;
474 }
475 }
476
477 cleanup:
478
479 if (ACPI_FAILURE (status)) {
480 /* Delete region object and method subobject */
481
482 if (obj_desc) {
483 /* Remove deletes both objects! */
484
485 acpi_cm_remove_reference (obj_desc);
486 obj_desc = NULL;
487 }
488 }
489
490 return (status);
491 }
492
493
494 /*****************************************************************************
495 *
496 * FUNCTION: Acpi_aml_exec_create_processor
497 *
498 * PARAMETERS: Op - Op containing the Processor definition and
499 * args
500 * Processor_nTE - Node for the containing Node
501 *
502 * RETURN: Status
503 *
504 * DESCRIPTION: Create a new processor object and populate the fields
505 *
506 ****************************************************************************/
507
508 ACPI_STATUS
509 acpi_aml_exec_create_processor (
510 ACPI_PARSE_OBJECT *op,
511 ACPI_HANDLE processor_nTE)
512 {
513 ACPI_STATUS status;
514 ACPI_PARSE_OBJECT *arg;
515 ACPI_OPERAND_OBJECT *obj_desc;
516
517
518 obj_desc = acpi_cm_create_internal_object (ACPI_TYPE_PROCESSOR);
519 if (!obj_desc) {
520 status = AE_NO_MEMORY;
521 return (status);
522 }
523
524 /* Install the new processor object in the parent Node */
525
526 status = acpi_ns_attach_object (processor_nTE, obj_desc,
527 (u8) ACPI_TYPE_PROCESSOR);
528 if (ACPI_FAILURE (status)) {
529 return(status);
530 }
531
532 arg = op->value.arg;
533
534 /* check existence */
535
536 if (!arg) {
537 status = AE_AML_NO_OPERAND;
538 return (status);
539 }
540
541 /* First arg is the Processor ID */
542
543 obj_desc->processor.proc_id = (u8) arg->value.integer;
544
545 /* Move to next arg and check existence */
546
547 arg = arg->next;
548 if (!arg) {
549 status = AE_AML_NO_OPERAND;
550 return (status);
551 }
552
553 /* Second arg is the PBlock Address */
554
555 obj_desc->processor.address = (ACPI_IO_ADDRESS) arg->value.integer;
556
557 /* Move to next arg and check existence */
558
559 arg = arg->next;
560 if (!arg) {
561 status = AE_AML_NO_OPERAND;
562 return (status);
563 }
564
565 /* Third arg is the PBlock Length */
566
567 obj_desc->processor.length = (u8) arg->value.integer;
568
569 return (AE_OK);
570 }
571
572
573 /*****************************************************************************
574 *
575 * FUNCTION: Acpi_aml_exec_create_power_resource
576 *
577 * PARAMETERS: Op - Op containing the Power_resource definition
578 * and args
579 * Power_res_nTE - Node for the containing Node
580 *
581 * RETURN: Status
582 *
583 * DESCRIPTION: Create a new Power_resource object and populate the fields
584 *
585 ****************************************************************************/
586
587 ACPI_STATUS
588 acpi_aml_exec_create_power_resource (
589 ACPI_PARSE_OBJECT *op,
590 ACPI_HANDLE power_res_nTE)
591 {
592 ACPI_STATUS status;
593 ACPI_PARSE_OBJECT *arg;
594 ACPI_OPERAND_OBJECT *obj_desc;
595
596
597 obj_desc = acpi_cm_create_internal_object (ACPI_TYPE_POWER);
598 if (!obj_desc) {
599 status = AE_NO_MEMORY;
600 return (status);
601 }
602
603 /* Install the new power resource object in the parent Node */
604
605 status = acpi_ns_attach_object (power_res_nTE, obj_desc,
606 (u8) ACPI_TYPE_POWER);
607 if (ACPI_FAILURE (status)) {
608 return(status);
609 }
610
611 arg = op->value.arg;
612
613 /* check existence */
614
615 if (!arg) {
616 status = AE_AML_NO_OPERAND;
617 return (status);
618 }
619
620 /* First arg is the System_level */
621
622 obj_desc->power_resource.system_level = (u8) arg->value.integer;
623
624 /* Move to next arg and check existence */
625
626 arg = arg->next;
627 if (!arg) {
628 status = AE_AML_NO_OPERAND;
629 return (status);
630 }
631
632 /* Second arg is the PBlock Address */
633
634 obj_desc->power_resource.resource_order = (u16) arg->value.integer;
635
636 return (AE_OK);
637 }
638
639
640 /*****************************************************************************
641 *
642 * FUNCTION: Acpi_aml_exec_create_method
643 *
644 * PARAMETERS: Aml_ptr - First byte of the method's AML
645 * Aml_length - AML byte count for this method
646 * Method_flags - AML method flag byte
647 * Method - Method Node
648 *
649 * RETURN: Status
650 *
651 * DESCRIPTION: Create a new method object
652 *
653 ****************************************************************************/
654
655 ACPI_STATUS
656 acpi_aml_exec_create_method (
657 u8 *aml_ptr,
658 u32 aml_length,
659 u32 method_flags,
660 ACPI_HANDLE method)
661 {
662 ACPI_OPERAND_OBJECT *obj_desc;
663 ACPI_STATUS status;
664
665
666 /* Create a new method object */
667
668 obj_desc = acpi_cm_create_internal_object (ACPI_TYPE_METHOD);
669 if (!obj_desc) {
670 return (AE_NO_MEMORY);
671 }
672
673 /* Get the method's AML pointer/length from the Op */
674
675 obj_desc->method.pcode = aml_ptr;
676 obj_desc->method.pcode_length = aml_length;
677
678 /*
679 * First argument is the Method Flags (contains parameter count for the
680 * method)
681 */
682
683 obj_desc->method.method_flags = (u8) method_flags;
684 obj_desc->method.param_count = (u8) (method_flags &
685 METHOD_FLAGS_ARG_COUNT);
686
687 /*
688 * Get the concurrency count. If required, a semaphore will be
689 * created for this method when it is parsed.
690 */
691 if (method_flags & METHOD_FLAGS_SERIALIZED) {
692 /*
693 * ACPI 1.0: Concurrency = 1
694 * ACPI 2.0: Concurrency = (Sync_level (in method declaration) + 1)
695 */
696 obj_desc->method.concurrency = (u8)
697 (((method_flags & METHOD_FLAGS_SYNCH_LEVEL) >> 4) + 1);
698 }
699
700 else {
701 obj_desc->method.concurrency = INFINITE_CONCURRENCY;
702 }
703
704 /* Attach the new object to the method Node */
705
706 status = acpi_ns_attach_object (method, obj_desc, (u8) ACPI_TYPE_METHOD);
707 if (ACPI_FAILURE (status)) {
708 acpi_cm_delete_object_desc (obj_desc);
709 }
710
711 return (status);
712 }
713
714