[ACPICA]
[reactos.git] / reactos / drivers / bus / acpi / acpica / executer / excreate.c
1 /******************************************************************************
2 *
3 * Module Name: excreate - Named object creation
4 *
5 *****************************************************************************/
6
7 /******************************************************************************
8 *
9 * 1. Copyright Notice
10 *
11 * Some or all of this work - Copyright (c) 1999 - 2015, Intel Corp.
12 * All rights reserved.
13 *
14 * 2. License
15 *
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
19 * property rights.
20 *
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
27 *
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
36 *
37 * The above copyright and patent license is granted only if the following
38 * conditions are met:
39 *
40 * 3. Conditions
41 *
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
53 *
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
64 * make.
65 *
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3.4. Intel retains all right, title, and interest in and to the Original
73 * Intel Code.
74 *
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
79 *
80 * 4. Disclaimer and Export Compliance
81 *
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89 * PARTICULAR PURPOSE.
90 *
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98 * LIMITED REMEDY.
99 *
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government. In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
114 *
115 *****************************************************************************/
116
117 #include "acpi.h"
118 #include "accommon.h"
119 #include "acinterp.h"
120 #include "amlcode.h"
121 #include "acnamesp.h"
122
123
124 #define _COMPONENT ACPI_EXECUTER
125 ACPI_MODULE_NAME ("excreate")
126
127
128 #ifndef ACPI_NO_METHOD_EXECUTION
129 /*******************************************************************************
130 *
131 * FUNCTION: AcpiExCreateAlias
132 *
133 * PARAMETERS: WalkState - Current state, contains operands
134 *
135 * RETURN: Status
136 *
137 * DESCRIPTION: Create a new named alias
138 *
139 ******************************************************************************/
140
141 ACPI_STATUS
142 AcpiExCreateAlias (
143 ACPI_WALK_STATE *WalkState)
144 {
145 ACPI_NAMESPACE_NODE *TargetNode;
146 ACPI_NAMESPACE_NODE *AliasNode;
147 ACPI_STATUS Status = AE_OK;
148
149
150 ACPI_FUNCTION_TRACE (ExCreateAlias);
151
152
153 /* Get the source/alias operands (both namespace nodes) */
154
155 AliasNode = (ACPI_NAMESPACE_NODE *) WalkState->Operands[0];
156 TargetNode = (ACPI_NAMESPACE_NODE *) WalkState->Operands[1];
157
158 if ((TargetNode->Type == ACPI_TYPE_LOCAL_ALIAS) ||
159 (TargetNode->Type == ACPI_TYPE_LOCAL_METHOD_ALIAS))
160 {
161 /*
162 * Dereference an existing alias so that we don't create a chain
163 * of aliases. With this code, we guarantee that an alias is
164 * always exactly one level of indirection away from the
165 * actual aliased name.
166 */
167 TargetNode = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, TargetNode->Object);
168 }
169
170 /*
171 * For objects that can never change (i.e., the NS node will
172 * permanently point to the same object), we can simply attach
173 * the object to the new NS node. For other objects (such as
174 * Integers, buffers, etc.), we have to point the Alias node
175 * to the original Node.
176 */
177 switch (TargetNode->Type)
178 {
179
180 /* For these types, the sub-object can change dynamically via a Store */
181
182 case ACPI_TYPE_INTEGER:
183 case ACPI_TYPE_STRING:
184 case ACPI_TYPE_BUFFER:
185 case ACPI_TYPE_PACKAGE:
186 case ACPI_TYPE_BUFFER_FIELD:
187 /*
188 * These types open a new scope, so we need the NS node in order to access
189 * any children.
190 */
191 case ACPI_TYPE_DEVICE:
192 case ACPI_TYPE_POWER:
193 case ACPI_TYPE_PROCESSOR:
194 case ACPI_TYPE_THERMAL:
195 case ACPI_TYPE_LOCAL_SCOPE:
196 /*
197 * The new alias has the type ALIAS and points to the original
198 * NS node, not the object itself.
199 */
200 AliasNode->Type = ACPI_TYPE_LOCAL_ALIAS;
201 AliasNode->Object = ACPI_CAST_PTR (ACPI_OPERAND_OBJECT, TargetNode);
202 break;
203
204 case ACPI_TYPE_METHOD:
205 /*
206 * Control method aliases need to be differentiated
207 */
208 AliasNode->Type = ACPI_TYPE_LOCAL_METHOD_ALIAS;
209 AliasNode->Object = ACPI_CAST_PTR (ACPI_OPERAND_OBJECT, TargetNode);
210 break;
211
212 default:
213
214 /* Attach the original source object to the new Alias Node */
215
216 /*
217 * The new alias assumes the type of the target, and it points
218 * to the same object. The reference count of the object has an
219 * additional reference to prevent deletion out from under either the
220 * target node or the alias Node
221 */
222 Status = AcpiNsAttachObject (AliasNode,
223 AcpiNsGetAttachedObject (TargetNode), TargetNode->Type);
224 break;
225 }
226
227 /* Since both operands are Nodes, we don't need to delete them */
228
229 return_ACPI_STATUS (Status);
230 }
231
232
233 /*******************************************************************************
234 *
235 * FUNCTION: AcpiExCreateEvent
236 *
237 * PARAMETERS: WalkState - Current state
238 *
239 * RETURN: Status
240 *
241 * DESCRIPTION: Create a new event object
242 *
243 ******************************************************************************/
244
245 ACPI_STATUS
246 AcpiExCreateEvent (
247 ACPI_WALK_STATE *WalkState)
248 {
249 ACPI_STATUS Status;
250 ACPI_OPERAND_OBJECT *ObjDesc;
251
252
253 ACPI_FUNCTION_TRACE (ExCreateEvent);
254
255
256 ObjDesc = AcpiUtCreateInternalObject (ACPI_TYPE_EVENT);
257 if (!ObjDesc)
258 {
259 Status = AE_NO_MEMORY;
260 goto Cleanup;
261 }
262
263 /*
264 * Create the actual OS semaphore, with zero initial units -- meaning
265 * that the event is created in an unsignalled state
266 */
267 Status = AcpiOsCreateSemaphore (ACPI_NO_UNIT_LIMIT, 0,
268 &ObjDesc->Event.OsSemaphore);
269 if (ACPI_FAILURE (Status))
270 {
271 goto Cleanup;
272 }
273
274 /* Attach object to the Node */
275
276 Status = AcpiNsAttachObject ((ACPI_NAMESPACE_NODE *) WalkState->Operands[0],
277 ObjDesc, ACPI_TYPE_EVENT);
278
279 Cleanup:
280 /*
281 * Remove local reference to the object (on error, will cause deletion
282 * of both object and semaphore if present.)
283 */
284 AcpiUtRemoveReference (ObjDesc);
285 return_ACPI_STATUS (Status);
286 }
287
288
289 /*******************************************************************************
290 *
291 * FUNCTION: AcpiExCreateMutex
292 *
293 * PARAMETERS: WalkState - Current state
294 *
295 * RETURN: Status
296 *
297 * DESCRIPTION: Create a new mutex object
298 *
299 * Mutex (Name[0], SyncLevel[1])
300 *
301 ******************************************************************************/
302
303 ACPI_STATUS
304 AcpiExCreateMutex (
305 ACPI_WALK_STATE *WalkState)
306 {
307 ACPI_STATUS Status = AE_OK;
308 ACPI_OPERAND_OBJECT *ObjDesc;
309
310
311 ACPI_FUNCTION_TRACE_PTR (ExCreateMutex, ACPI_WALK_OPERANDS);
312
313
314 /* Create the new mutex object */
315
316 ObjDesc = AcpiUtCreateInternalObject (ACPI_TYPE_MUTEX);
317 if (!ObjDesc)
318 {
319 Status = AE_NO_MEMORY;
320 goto Cleanup;
321 }
322
323 /* Create the actual OS Mutex */
324
325 Status = AcpiOsCreateMutex (&ObjDesc->Mutex.OsMutex);
326 if (ACPI_FAILURE (Status))
327 {
328 goto Cleanup;
329 }
330
331 /* Init object and attach to NS node */
332
333 ObjDesc->Mutex.SyncLevel = (UINT8) WalkState->Operands[1]->Integer.Value;
334 ObjDesc->Mutex.Node = (ACPI_NAMESPACE_NODE *) WalkState->Operands[0];
335
336 Status = AcpiNsAttachObject (ObjDesc->Mutex.Node, ObjDesc, ACPI_TYPE_MUTEX);
337
338
339 Cleanup:
340 /*
341 * Remove local reference to the object (on error, will cause deletion
342 * of both object and semaphore if present.)
343 */
344 AcpiUtRemoveReference (ObjDesc);
345 return_ACPI_STATUS (Status);
346 }
347
348
349 /*******************************************************************************
350 *
351 * FUNCTION: AcpiExCreateRegion
352 *
353 * PARAMETERS: AmlStart - Pointer to the region declaration AML
354 * AmlLength - Max length of the declaration AML
355 * SpaceId - Address space ID for the region
356 * WalkState - Current state
357 *
358 * RETURN: Status
359 *
360 * DESCRIPTION: Create a new operation region object
361 *
362 ******************************************************************************/
363
364 ACPI_STATUS
365 AcpiExCreateRegion (
366 UINT8 *AmlStart,
367 UINT32 AmlLength,
368 UINT8 SpaceId,
369 ACPI_WALK_STATE *WalkState)
370 {
371 ACPI_STATUS Status;
372 ACPI_OPERAND_OBJECT *ObjDesc;
373 ACPI_NAMESPACE_NODE *Node;
374 ACPI_OPERAND_OBJECT *RegionObj2;
375
376
377 ACPI_FUNCTION_TRACE (ExCreateRegion);
378
379
380 /* Get the Namespace Node */
381
382 Node = WalkState->Op->Common.Node;
383
384 /*
385 * If the region object is already attached to this node,
386 * just return
387 */
388 if (AcpiNsGetAttachedObject (Node))
389 {
390 return_ACPI_STATUS (AE_OK);
391 }
392
393 /*
394 * Space ID must be one of the predefined IDs, or in the user-defined
395 * range
396 */
397 if (!AcpiIsValidSpaceId (SpaceId))
398 {
399 /*
400 * Print an error message, but continue. We don't want to abort
401 * a table load for this exception. Instead, if the region is
402 * actually used at runtime, abort the executing method.
403 */
404 ACPI_ERROR ((AE_INFO, "Invalid/unknown Address Space ID: 0x%2.2X", SpaceId));
405 }
406
407 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "Region Type - %s (0x%X)\n",
408 AcpiUtGetRegionName (SpaceId), SpaceId));
409
410 /* Create the region descriptor */
411
412 ObjDesc = AcpiUtCreateInternalObject (ACPI_TYPE_REGION);
413 if (!ObjDesc)
414 {
415 Status = AE_NO_MEMORY;
416 goto Cleanup;
417 }
418
419 /*
420 * Remember location in AML stream of address & length
421 * operands since they need to be evaluated at run time.
422 */
423 RegionObj2 = ObjDesc->Common.NextObject;
424 RegionObj2->Extra.AmlStart = AmlStart;
425 RegionObj2->Extra.AmlLength = AmlLength;
426 if (WalkState->ScopeInfo)
427 {
428 RegionObj2->Extra.ScopeNode = WalkState->ScopeInfo->Scope.Node;
429 }
430 else
431 {
432 RegionObj2->Extra.ScopeNode = Node;
433 }
434
435 /* Init the region from the operands */
436
437 ObjDesc->Region.SpaceId = SpaceId;
438 ObjDesc->Region.Address = 0;
439 ObjDesc->Region.Length = 0;
440 ObjDesc->Region.Node = Node;
441
442 /* Install the new region object in the parent Node */
443
444 Status = AcpiNsAttachObject (Node, ObjDesc, ACPI_TYPE_REGION);
445
446
447 Cleanup:
448
449 /* Remove local reference to the object */
450
451 AcpiUtRemoveReference (ObjDesc);
452 return_ACPI_STATUS (Status);
453 }
454
455
456 /*******************************************************************************
457 *
458 * FUNCTION: AcpiExCreateProcessor
459 *
460 * PARAMETERS: WalkState - Current state
461 *
462 * RETURN: Status
463 *
464 * DESCRIPTION: Create a new processor object and populate the fields
465 *
466 * Processor (Name[0], CpuID[1], PblockAddr[2], PblockLength[3])
467 *
468 ******************************************************************************/
469
470 ACPI_STATUS
471 AcpiExCreateProcessor (
472 ACPI_WALK_STATE *WalkState)
473 {
474 ACPI_OPERAND_OBJECT **Operand = &WalkState->Operands[0];
475 ACPI_OPERAND_OBJECT *ObjDesc;
476 ACPI_STATUS Status;
477
478
479 ACPI_FUNCTION_TRACE_PTR (ExCreateProcessor, WalkState);
480
481
482 /* Create the processor object */
483
484 ObjDesc = AcpiUtCreateInternalObject (ACPI_TYPE_PROCESSOR);
485 if (!ObjDesc)
486 {
487 return_ACPI_STATUS (AE_NO_MEMORY);
488 }
489
490 /* Initialize the processor object from the operands */
491
492 ObjDesc->Processor.ProcId = (UINT8) Operand[1]->Integer.Value;
493 ObjDesc->Processor.Length = (UINT8) Operand[3]->Integer.Value;
494 ObjDesc->Processor.Address = (ACPI_IO_ADDRESS) Operand[2]->Integer.Value;
495
496 /* Install the processor object in the parent Node */
497
498 Status = AcpiNsAttachObject ((ACPI_NAMESPACE_NODE *) Operand[0],
499 ObjDesc, ACPI_TYPE_PROCESSOR);
500
501 /* Remove local reference to the object */
502
503 AcpiUtRemoveReference (ObjDesc);
504 return_ACPI_STATUS (Status);
505 }
506
507
508 /*******************************************************************************
509 *
510 * FUNCTION: AcpiExCreatePowerResource
511 *
512 * PARAMETERS: WalkState - Current state
513 *
514 * RETURN: Status
515 *
516 * DESCRIPTION: Create a new PowerResource object and populate the fields
517 *
518 * PowerResource (Name[0], SystemLevel[1], ResourceOrder[2])
519 *
520 ******************************************************************************/
521
522 ACPI_STATUS
523 AcpiExCreatePowerResource (
524 ACPI_WALK_STATE *WalkState)
525 {
526 ACPI_OPERAND_OBJECT **Operand = &WalkState->Operands[0];
527 ACPI_STATUS Status;
528 ACPI_OPERAND_OBJECT *ObjDesc;
529
530
531 ACPI_FUNCTION_TRACE_PTR (ExCreatePowerResource, WalkState);
532
533
534 /* Create the power resource object */
535
536 ObjDesc = AcpiUtCreateInternalObject (ACPI_TYPE_POWER);
537 if (!ObjDesc)
538 {
539 return_ACPI_STATUS (AE_NO_MEMORY);
540 }
541
542 /* Initialize the power object from the operands */
543
544 ObjDesc->PowerResource.SystemLevel = (UINT8) Operand[1]->Integer.Value;
545 ObjDesc->PowerResource.ResourceOrder = (UINT16) Operand[2]->Integer.Value;
546
547 /* Install the power resource object in the parent Node */
548
549 Status = AcpiNsAttachObject ((ACPI_NAMESPACE_NODE *) Operand[0],
550 ObjDesc, ACPI_TYPE_POWER);
551
552 /* Remove local reference to the object */
553
554 AcpiUtRemoveReference (ObjDesc);
555 return_ACPI_STATUS (Status);
556 }
557 #endif
558
559
560 /*******************************************************************************
561 *
562 * FUNCTION: AcpiExCreateMethod
563 *
564 * PARAMETERS: AmlStart - First byte of the method's AML
565 * AmlLength - AML byte count for this method
566 * WalkState - Current state
567 *
568 * RETURN: Status
569 *
570 * DESCRIPTION: Create a new method object
571 *
572 ******************************************************************************/
573
574 ACPI_STATUS
575 AcpiExCreateMethod (
576 UINT8 *AmlStart,
577 UINT32 AmlLength,
578 ACPI_WALK_STATE *WalkState)
579 {
580 ACPI_OPERAND_OBJECT **Operand = &WalkState->Operands[0];
581 ACPI_OPERAND_OBJECT *ObjDesc;
582 ACPI_STATUS Status;
583 UINT8 MethodFlags;
584
585
586 ACPI_FUNCTION_TRACE_PTR (ExCreateMethod, WalkState);
587
588
589 /* Create a new method object */
590
591 ObjDesc = AcpiUtCreateInternalObject (ACPI_TYPE_METHOD);
592 if (!ObjDesc)
593 {
594 Status = AE_NO_MEMORY;
595 goto Exit;
596 }
597
598 /* Save the method's AML pointer and length */
599
600 ObjDesc->Method.AmlStart = AmlStart;
601 ObjDesc->Method.AmlLength = AmlLength;
602 ObjDesc->Method.Node = Operand[0];
603
604 /*
605 * Disassemble the method flags. Split off the ArgCount, Serialized
606 * flag, and SyncLevel for efficiency.
607 */
608 MethodFlags = (UINT8) Operand[1]->Integer.Value;
609 ObjDesc->Method.ParamCount = (UINT8) (MethodFlags & AML_METHOD_ARG_COUNT);
610
611 /*
612 * Get the SyncLevel. If method is serialized, a mutex will be
613 * created for this method when it is parsed.
614 */
615 if (MethodFlags & AML_METHOD_SERIALIZED)
616 {
617 ObjDesc->Method.InfoFlags = ACPI_METHOD_SERIALIZED;
618
619 /*
620 * ACPI 1.0: SyncLevel = 0
621 * ACPI 2.0: SyncLevel = SyncLevel in method declaration
622 */
623 ObjDesc->Method.SyncLevel = (UINT8)
624 ((MethodFlags & AML_METHOD_SYNC_LEVEL) >> 4);
625 }
626
627 /* Attach the new object to the method Node */
628
629 Status = AcpiNsAttachObject ((ACPI_NAMESPACE_NODE *) Operand[0],
630 ObjDesc, ACPI_TYPE_METHOD);
631
632 /* Remove local reference to the object */
633
634 AcpiUtRemoveReference (ObjDesc);
635
636 Exit:
637 /* Remove a reference to the operand */
638
639 AcpiUtRemoveReference (Operand[1]);
640 return_ACPI_STATUS (Status);
641 }