8fcf7bf1ece04ff4931ac11763d624f0e8083a7b
[reactos.git] / reactos / drivers / bus / acpi / acpica / executer / excreate.c
1 /******************************************************************************
2 *
3 * Module Name: excreate - Named object creation
4 *
5 *****************************************************************************/
6
7 /*
8 * Copyright (C) 2000 - 2017, Intel Corp.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
29 *
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
42 */
43
44 #include "acpi.h"
45 #include "accommon.h"
46 #include "acinterp.h"
47 #include "amlcode.h"
48 #include "acnamesp.h"
49
50
51 #define _COMPONENT ACPI_EXECUTER
52 ACPI_MODULE_NAME ("excreate")
53
54
55 #ifndef ACPI_NO_METHOD_EXECUTION
56 /*******************************************************************************
57 *
58 * FUNCTION: AcpiExCreateAlias
59 *
60 * PARAMETERS: WalkState - Current state, contains operands
61 *
62 * RETURN: Status
63 *
64 * DESCRIPTION: Create a new named alias
65 *
66 ******************************************************************************/
67
68 ACPI_STATUS
69 AcpiExCreateAlias (
70 ACPI_WALK_STATE *WalkState)
71 {
72 ACPI_NAMESPACE_NODE *TargetNode;
73 ACPI_NAMESPACE_NODE *AliasNode;
74 ACPI_STATUS Status = AE_OK;
75
76
77 ACPI_FUNCTION_TRACE (ExCreateAlias);
78
79
80 /* Get the source/alias operands (both namespace nodes) */
81
82 AliasNode = (ACPI_NAMESPACE_NODE *) WalkState->Operands[0];
83 TargetNode = (ACPI_NAMESPACE_NODE *) WalkState->Operands[1];
84
85 if ((TargetNode->Type == ACPI_TYPE_LOCAL_ALIAS) ||
86 (TargetNode->Type == ACPI_TYPE_LOCAL_METHOD_ALIAS))
87 {
88 /*
89 * Dereference an existing alias so that we don't create a chain
90 * of aliases. With this code, we guarantee that an alias is
91 * always exactly one level of indirection away from the
92 * actual aliased name.
93 */
94 TargetNode = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, TargetNode->Object);
95 }
96
97 /* Ensure that the target node is valid */
98 if (!TargetNode)
99 {
100 return_ACPI_STATUS (AE_NULL_OBJECT);
101 }
102
103 /*
104 * For objects that can never change (i.e., the NS node will
105 * permanently point to the same object), we can simply attach
106 * the object to the new NS node. For other objects (such as
107 * Integers, buffers, etc.), we have to point the Alias node
108 * to the original Node.
109 */
110 switch (TargetNode->Type)
111 {
112
113 /* For these types, the sub-object can change dynamically via a Store */
114
115 case ACPI_TYPE_INTEGER:
116 case ACPI_TYPE_STRING:
117 case ACPI_TYPE_BUFFER:
118 case ACPI_TYPE_PACKAGE:
119 case ACPI_TYPE_BUFFER_FIELD:
120 /*
121 * These types open a new scope, so we need the NS node in order to access
122 * any children.
123 */
124 case ACPI_TYPE_DEVICE:
125 case ACPI_TYPE_POWER:
126 case ACPI_TYPE_PROCESSOR:
127 case ACPI_TYPE_THERMAL:
128 case ACPI_TYPE_LOCAL_SCOPE:
129 /*
130 * The new alias has the type ALIAS and points to the original
131 * NS node, not the object itself.
132 */
133 AliasNode->Type = ACPI_TYPE_LOCAL_ALIAS;
134 AliasNode->Object = ACPI_CAST_PTR (ACPI_OPERAND_OBJECT, TargetNode);
135 break;
136
137 case ACPI_TYPE_METHOD:
138 /*
139 * Control method aliases need to be differentiated
140 */
141 AliasNode->Type = ACPI_TYPE_LOCAL_METHOD_ALIAS;
142 AliasNode->Object = ACPI_CAST_PTR (ACPI_OPERAND_OBJECT, TargetNode);
143 break;
144
145 default:
146
147 /* Attach the original source object to the new Alias Node */
148
149 /*
150 * The new alias assumes the type of the target, and it points
151 * to the same object. The reference count of the object has an
152 * additional reference to prevent deletion out from under either the
153 * target node or the alias Node
154 */
155 Status = AcpiNsAttachObject (AliasNode,
156 AcpiNsGetAttachedObject (TargetNode), TargetNode->Type);
157 break;
158 }
159
160 /* Since both operands are Nodes, we don't need to delete them */
161
162 return_ACPI_STATUS (Status);
163 }
164
165
166 /*******************************************************************************
167 *
168 * FUNCTION: AcpiExCreateEvent
169 *
170 * PARAMETERS: WalkState - Current state
171 *
172 * RETURN: Status
173 *
174 * DESCRIPTION: Create a new event object
175 *
176 ******************************************************************************/
177
178 ACPI_STATUS
179 AcpiExCreateEvent (
180 ACPI_WALK_STATE *WalkState)
181 {
182 ACPI_STATUS Status;
183 ACPI_OPERAND_OBJECT *ObjDesc;
184
185
186 ACPI_FUNCTION_TRACE (ExCreateEvent);
187
188
189 ObjDesc = AcpiUtCreateInternalObject (ACPI_TYPE_EVENT);
190 if (!ObjDesc)
191 {
192 Status = AE_NO_MEMORY;
193 goto Cleanup;
194 }
195
196 /*
197 * Create the actual OS semaphore, with zero initial units -- meaning
198 * that the event is created in an unsignalled state
199 */
200 Status = AcpiOsCreateSemaphore (ACPI_NO_UNIT_LIMIT, 0,
201 &ObjDesc->Event.OsSemaphore);
202 if (ACPI_FAILURE (Status))
203 {
204 goto Cleanup;
205 }
206
207 /* Attach object to the Node */
208
209 Status = AcpiNsAttachObject (
210 (ACPI_NAMESPACE_NODE *) WalkState->Operands[0],
211 ObjDesc, ACPI_TYPE_EVENT);
212
213 Cleanup:
214 /*
215 * Remove local reference to the object (on error, will cause deletion
216 * of both object and semaphore if present.)
217 */
218 AcpiUtRemoveReference (ObjDesc);
219 return_ACPI_STATUS (Status);
220 }
221
222
223 /*******************************************************************************
224 *
225 * FUNCTION: AcpiExCreateMutex
226 *
227 * PARAMETERS: WalkState - Current state
228 *
229 * RETURN: Status
230 *
231 * DESCRIPTION: Create a new mutex object
232 *
233 * Mutex (Name[0], SyncLevel[1])
234 *
235 ******************************************************************************/
236
237 ACPI_STATUS
238 AcpiExCreateMutex (
239 ACPI_WALK_STATE *WalkState)
240 {
241 ACPI_STATUS Status = AE_OK;
242 ACPI_OPERAND_OBJECT *ObjDesc;
243
244
245 ACPI_FUNCTION_TRACE_PTR (ExCreateMutex, ACPI_WALK_OPERANDS);
246
247
248 /* Create the new mutex object */
249
250 ObjDesc = AcpiUtCreateInternalObject (ACPI_TYPE_MUTEX);
251 if (!ObjDesc)
252 {
253 Status = AE_NO_MEMORY;
254 goto Cleanup;
255 }
256
257 /* Create the actual OS Mutex */
258
259 Status = AcpiOsCreateMutex (&ObjDesc->Mutex.OsMutex);
260 if (ACPI_FAILURE (Status))
261 {
262 goto Cleanup;
263 }
264
265 /* Init object and attach to NS node */
266
267 ObjDesc->Mutex.SyncLevel = (UINT8) WalkState->Operands[1]->Integer.Value;
268 ObjDesc->Mutex.Node = (ACPI_NAMESPACE_NODE *) WalkState->Operands[0];
269
270 Status = AcpiNsAttachObject (
271 ObjDesc->Mutex.Node, ObjDesc, ACPI_TYPE_MUTEX);
272
273
274 Cleanup:
275 /*
276 * Remove local reference to the object (on error, will cause deletion
277 * of both object and semaphore if present.)
278 */
279 AcpiUtRemoveReference (ObjDesc);
280 return_ACPI_STATUS (Status);
281 }
282
283
284 /*******************************************************************************
285 *
286 * FUNCTION: AcpiExCreateRegion
287 *
288 * PARAMETERS: AmlStart - Pointer to the region declaration AML
289 * AmlLength - Max length of the declaration AML
290 * SpaceId - Address space ID for the region
291 * WalkState - Current state
292 *
293 * RETURN: Status
294 *
295 * DESCRIPTION: Create a new operation region object
296 *
297 ******************************************************************************/
298
299 ACPI_STATUS
300 AcpiExCreateRegion (
301 UINT8 *AmlStart,
302 UINT32 AmlLength,
303 UINT8 SpaceId,
304 ACPI_WALK_STATE *WalkState)
305 {
306 ACPI_STATUS Status;
307 ACPI_OPERAND_OBJECT *ObjDesc;
308 ACPI_NAMESPACE_NODE *Node;
309 ACPI_OPERAND_OBJECT *RegionObj2;
310
311
312 ACPI_FUNCTION_TRACE (ExCreateRegion);
313
314
315 /* Get the Namespace Node */
316
317 Node = WalkState->Op->Common.Node;
318
319 /*
320 * If the region object is already attached to this node,
321 * just return
322 */
323 if (AcpiNsGetAttachedObject (Node))
324 {
325 return_ACPI_STATUS (AE_OK);
326 }
327
328 /*
329 * Space ID must be one of the predefined IDs, or in the user-defined
330 * range
331 */
332 if (!AcpiIsValidSpaceId (SpaceId))
333 {
334 /*
335 * Print an error message, but continue. We don't want to abort
336 * a table load for this exception. Instead, if the region is
337 * actually used at runtime, abort the executing method.
338 */
339 ACPI_ERROR ((AE_INFO,
340 "Invalid/unknown Address Space ID: 0x%2.2X", SpaceId));
341 }
342
343 ACPI_DEBUG_PRINT ((ACPI_DB_LOAD, "Region Type - %s (0x%X)\n",
344 AcpiUtGetRegionName (SpaceId), SpaceId));
345
346 /* Create the region descriptor */
347
348 ObjDesc = AcpiUtCreateInternalObject (ACPI_TYPE_REGION);
349 if (!ObjDesc)
350 {
351 Status = AE_NO_MEMORY;
352 goto Cleanup;
353 }
354
355 /*
356 * Remember location in AML stream of address & length
357 * operands since they need to be evaluated at run time.
358 */
359 RegionObj2 = AcpiNsGetSecondaryObject (ObjDesc);
360 RegionObj2->Extra.AmlStart = AmlStart;
361 RegionObj2->Extra.AmlLength = AmlLength;
362 RegionObj2->Extra.Method_REG = NULL;
363 if (WalkState->ScopeInfo)
364 {
365 RegionObj2->Extra.ScopeNode = WalkState->ScopeInfo->Scope.Node;
366 }
367 else
368 {
369 RegionObj2->Extra.ScopeNode = Node;
370 }
371
372 /* Init the region from the operands */
373
374 ObjDesc->Region.SpaceId = SpaceId;
375 ObjDesc->Region.Address = 0;
376 ObjDesc->Region.Length = 0;
377 ObjDesc->Region.Node = Node;
378 ObjDesc->Region.Handler = NULL;
379 ObjDesc->Common.Flags &=
380 ~(AOPOBJ_SETUP_COMPLETE | AOPOBJ_REG_CONNECTED |
381 AOPOBJ_OBJECT_INITIALIZED);
382
383 /* Install the new region object in the parent Node */
384
385 Status = AcpiNsAttachObject (Node, ObjDesc, ACPI_TYPE_REGION);
386
387
388 Cleanup:
389
390 /* Remove local reference to the object */
391
392 AcpiUtRemoveReference (ObjDesc);
393 return_ACPI_STATUS (Status);
394 }
395
396
397 /*******************************************************************************
398 *
399 * FUNCTION: AcpiExCreateProcessor
400 *
401 * PARAMETERS: WalkState - Current state
402 *
403 * RETURN: Status
404 *
405 * DESCRIPTION: Create a new processor object and populate the fields
406 *
407 * Processor (Name[0], CpuID[1], PblockAddr[2], PblockLength[3])
408 *
409 ******************************************************************************/
410
411 ACPI_STATUS
412 AcpiExCreateProcessor (
413 ACPI_WALK_STATE *WalkState)
414 {
415 ACPI_OPERAND_OBJECT **Operand = &WalkState->Operands[0];
416 ACPI_OPERAND_OBJECT *ObjDesc;
417 ACPI_STATUS Status;
418
419
420 ACPI_FUNCTION_TRACE_PTR (ExCreateProcessor, WalkState);
421
422
423 /* Create the processor object */
424
425 ObjDesc = AcpiUtCreateInternalObject (ACPI_TYPE_PROCESSOR);
426 if (!ObjDesc)
427 {
428 return_ACPI_STATUS (AE_NO_MEMORY);
429 }
430
431 /* Initialize the processor object from the operands */
432
433 ObjDesc->Processor.ProcId = (UINT8) Operand[1]->Integer.Value;
434 ObjDesc->Processor.Length = (UINT8) Operand[3]->Integer.Value;
435 ObjDesc->Processor.Address = (ACPI_IO_ADDRESS) Operand[2]->Integer.Value;
436
437 /* Install the processor object in the parent Node */
438
439 Status = AcpiNsAttachObject ((ACPI_NAMESPACE_NODE *) Operand[0],
440 ObjDesc, ACPI_TYPE_PROCESSOR);
441
442 /* Remove local reference to the object */
443
444 AcpiUtRemoveReference (ObjDesc);
445 return_ACPI_STATUS (Status);
446 }
447
448
449 /*******************************************************************************
450 *
451 * FUNCTION: AcpiExCreatePowerResource
452 *
453 * PARAMETERS: WalkState - Current state
454 *
455 * RETURN: Status
456 *
457 * DESCRIPTION: Create a new PowerResource object and populate the fields
458 *
459 * PowerResource (Name[0], SystemLevel[1], ResourceOrder[2])
460 *
461 ******************************************************************************/
462
463 ACPI_STATUS
464 AcpiExCreatePowerResource (
465 ACPI_WALK_STATE *WalkState)
466 {
467 ACPI_OPERAND_OBJECT **Operand = &WalkState->Operands[0];
468 ACPI_STATUS Status;
469 ACPI_OPERAND_OBJECT *ObjDesc;
470
471
472 ACPI_FUNCTION_TRACE_PTR (ExCreatePowerResource, WalkState);
473
474
475 /* Create the power resource object */
476
477 ObjDesc = AcpiUtCreateInternalObject (ACPI_TYPE_POWER);
478 if (!ObjDesc)
479 {
480 return_ACPI_STATUS (AE_NO_MEMORY);
481 }
482
483 /* Initialize the power object from the operands */
484
485 ObjDesc->PowerResource.SystemLevel = (UINT8) Operand[1]->Integer.Value;
486 ObjDesc->PowerResource.ResourceOrder = (UINT16) Operand[2]->Integer.Value;
487
488 /* Install the power resource object in the parent Node */
489
490 Status = AcpiNsAttachObject ((ACPI_NAMESPACE_NODE *) Operand[0],
491 ObjDesc, ACPI_TYPE_POWER);
492
493 /* Remove local reference to the object */
494
495 AcpiUtRemoveReference (ObjDesc);
496 return_ACPI_STATUS (Status);
497 }
498 #endif
499
500
501 /*******************************************************************************
502 *
503 * FUNCTION: AcpiExCreateMethod
504 *
505 * PARAMETERS: AmlStart - First byte of the method's AML
506 * AmlLength - AML byte count for this method
507 * WalkState - Current state
508 *
509 * RETURN: Status
510 *
511 * DESCRIPTION: Create a new method object
512 *
513 ******************************************************************************/
514
515 ACPI_STATUS
516 AcpiExCreateMethod (
517 UINT8 *AmlStart,
518 UINT32 AmlLength,
519 ACPI_WALK_STATE *WalkState)
520 {
521 ACPI_OPERAND_OBJECT **Operand = &WalkState->Operands[0];
522 ACPI_OPERAND_OBJECT *ObjDesc;
523 ACPI_STATUS Status;
524 UINT8 MethodFlags;
525
526
527 ACPI_FUNCTION_TRACE_PTR (ExCreateMethod, WalkState);
528
529
530 /* Create a new method object */
531
532 ObjDesc = AcpiUtCreateInternalObject (ACPI_TYPE_METHOD);
533 if (!ObjDesc)
534 {
535 Status = AE_NO_MEMORY;
536 goto Exit;
537 }
538
539 /* Save the method's AML pointer and length */
540
541 ObjDesc->Method.AmlStart = AmlStart;
542 ObjDesc->Method.AmlLength = AmlLength;
543 ObjDesc->Method.Node = Operand[0];
544
545 /*
546 * Disassemble the method flags. Split off the ArgCount, Serialized
547 * flag, and SyncLevel for efficiency.
548 */
549 MethodFlags = (UINT8) Operand[1]->Integer.Value;
550 ObjDesc->Method.ParamCount = (UINT8)
551 (MethodFlags & AML_METHOD_ARG_COUNT);
552
553 /*
554 * Get the SyncLevel. If method is serialized, a mutex will be
555 * created for this method when it is parsed.
556 */
557 if (MethodFlags & AML_METHOD_SERIALIZED)
558 {
559 ObjDesc->Method.InfoFlags = ACPI_METHOD_SERIALIZED;
560
561 /*
562 * ACPI 1.0: SyncLevel = 0
563 * ACPI 2.0: SyncLevel = SyncLevel in method declaration
564 */
565 ObjDesc->Method.SyncLevel = (UINT8)
566 ((MethodFlags & AML_METHOD_SYNC_LEVEL) >> 4);
567 }
568
569 /* Attach the new object to the method Node */
570
571 Status = AcpiNsAttachObject ((ACPI_NAMESPACE_NODE *) Operand[0],
572 ObjDesc, ACPI_TYPE_METHOD);
573
574 /* Remove local reference to the object */
575
576 AcpiUtRemoveReference (ObjDesc);
577
578 Exit:
579 /* Remove a reference to the operand */
580
581 AcpiUtRemoveReference (Operand[1]);
582 return_ACPI_STATUS (Status);
583 }