- Update to new ACPI driver by Samuel Serapion (and fixes by me)
[reactos.git] / reactos / drivers / bus / acpi / acpica / resources / rscreate.c
1 /*******************************************************************************
2 *
3 * Module Name: rscreate - Create resource lists/tables
4 *
5 ******************************************************************************/
6
7 /******************************************************************************
8 *
9 * 1. Copyright Notice
10 *
11 * Some or all of this work - Copyright (c) 1999 - 2009, 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 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88 * PARTICULAR PURPOSE.
89 *
90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97 * LIMITED REMEDY.
98 *
99 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 * software or system incorporating such software without first obtaining any
101 * required license or other approval from the U. S. Department of Commerce or
102 * any other agency or department of the United States Government. In the
103 * event Licensee exports any such software from the United States or
104 * re-exports any such software from a foreign destination, Licensee shall
105 * ensure that the distribution and export/re-export of the software is in
106 * compliance with all laws, regulations, orders, or other restrictions of the
107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 * any of its subsidiaries will export/re-export any technical data, process,
109 * software, or service, directly or indirectly, to any country for which the
110 * United States government or any agency thereof requires an export license,
111 * other governmental approval, or letter of assurance, without first obtaining
112 * such license, approval or letter.
113 *
114 *****************************************************************************/
115
116 #define __RSCREATE_C__
117
118 #include "acpi.h"
119 #include "accommon.h"
120 #include "acresrc.h"
121 #include "acnamesp.h"
122
123 #define _COMPONENT ACPI_RESOURCES
124 ACPI_MODULE_NAME ("rscreate")
125
126
127 /*******************************************************************************
128 *
129 * FUNCTION: AcpiRsCreateResourceList
130 *
131 * PARAMETERS: AmlBuffer - Pointer to the resource byte stream
132 * OutputBuffer - Pointer to the user's buffer
133 *
134 * RETURN: Status: AE_OK if okay, else a valid ACPI_STATUS code
135 * If OutputBuffer is not large enough, OutputBufferLength
136 * indicates how large OutputBuffer should be, else it
137 * indicates how may UINT8 elements of OutputBuffer are valid.
138 *
139 * DESCRIPTION: Takes the byte stream returned from a _CRS, _PRS control method
140 * execution and parses the stream to create a linked list
141 * of device resources.
142 *
143 ******************************************************************************/
144
145 ACPI_STATUS
146 AcpiRsCreateResourceList (
147 ACPI_OPERAND_OBJECT *AmlBuffer,
148 ACPI_BUFFER *OutputBuffer)
149 {
150
151 ACPI_STATUS Status;
152 UINT8 *AmlStart;
153 ACPI_SIZE ListSizeNeeded = 0;
154 UINT32 AmlBufferLength;
155 void *Resource;
156
157
158 ACPI_FUNCTION_TRACE (RsCreateResourceList);
159
160
161 ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "AmlBuffer = %p\n",
162 AmlBuffer));
163
164 /* Params already validated, so we don't re-validate here */
165
166 AmlBufferLength = AmlBuffer->Buffer.Length;
167 AmlStart = AmlBuffer->Buffer.Pointer;
168
169 /*
170 * Pass the AmlBuffer into a module that can calculate
171 * the buffer size needed for the linked list
172 */
173 Status = AcpiRsGetListLength (AmlStart, AmlBufferLength,
174 &ListSizeNeeded);
175
176 ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Status=%X ListSizeNeeded=%X\n",
177 Status, (UINT32) ListSizeNeeded));
178 if (ACPI_FAILURE (Status))
179 {
180 return_ACPI_STATUS (Status);
181 }
182
183 /* Validate/Allocate/Clear caller buffer */
184
185 Status = AcpiUtInitializeBuffer (OutputBuffer, ListSizeNeeded);
186 if (ACPI_FAILURE (Status))
187 {
188 return_ACPI_STATUS (Status);
189 }
190
191 /* Do the conversion */
192
193 Resource = OutputBuffer->Pointer;
194 Status = AcpiUtWalkAmlResources (AmlStart, AmlBufferLength,
195 AcpiRsConvertAmlToResources, &Resource);
196 if (ACPI_FAILURE (Status))
197 {
198 return_ACPI_STATUS (Status);
199 }
200
201 ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
202 OutputBuffer->Pointer, (UINT32) OutputBuffer->Length));
203 return_ACPI_STATUS (AE_OK);
204 }
205
206
207 /*******************************************************************************
208 *
209 * FUNCTION: AcpiRsCreatePciRoutingTable
210 *
211 * PARAMETERS: PackageObject - Pointer to an ACPI_OPERAND_OBJECT
212 * package
213 * OutputBuffer - Pointer to the user's buffer
214 *
215 * RETURN: Status AE_OK if okay, else a valid ACPI_STATUS code.
216 * If the OutputBuffer is too small, the error will be
217 * AE_BUFFER_OVERFLOW and OutputBuffer->Length will point
218 * to the size buffer needed.
219 *
220 * DESCRIPTION: Takes the ACPI_OPERAND_OBJECT package and creates a
221 * linked list of PCI interrupt descriptions
222 *
223 * NOTE: It is the caller's responsibility to ensure that the start of the
224 * output buffer is aligned properly (if necessary).
225 *
226 ******************************************************************************/
227
228 ACPI_STATUS
229 AcpiRsCreatePciRoutingTable (
230 ACPI_OPERAND_OBJECT *PackageObject,
231 ACPI_BUFFER *OutputBuffer)
232 {
233 UINT8 *Buffer;
234 ACPI_OPERAND_OBJECT **TopObjectList;
235 ACPI_OPERAND_OBJECT **SubObjectList;
236 ACPI_OPERAND_OBJECT *ObjDesc;
237 ACPI_SIZE BufferSizeNeeded = 0;
238 UINT32 NumberOfElements;
239 UINT32 Index;
240 ACPI_PCI_ROUTING_TABLE *UserPrt;
241 ACPI_NAMESPACE_NODE *Node;
242 ACPI_STATUS Status;
243 ACPI_BUFFER PathBuffer;
244
245
246 ACPI_FUNCTION_TRACE (RsCreatePciRoutingTable);
247
248
249 /* Params already validated, so we don't re-validate here */
250
251 /* Get the required buffer length */
252
253 Status = AcpiRsGetPciRoutingTableLength (PackageObject,
254 &BufferSizeNeeded);
255 if (ACPI_FAILURE (Status))
256 {
257 return_ACPI_STATUS (Status);
258 }
259
260 ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "BufferSizeNeeded = %X\n",
261 (UINT32) BufferSizeNeeded));
262
263 /* Validate/Allocate/Clear caller buffer */
264
265 Status = AcpiUtInitializeBuffer (OutputBuffer, BufferSizeNeeded);
266 if (ACPI_FAILURE (Status))
267 {
268 return_ACPI_STATUS (Status);
269 }
270
271 /*
272 * Loop through the ACPI_INTERNAL_OBJECTS - Each object should be a
273 * package that in turn contains an ACPI_INTEGER Address, a UINT8 Pin,
274 * a Name, and a UINT8 SourceIndex.
275 */
276 TopObjectList = PackageObject->Package.Elements;
277 NumberOfElements = PackageObject->Package.Count;
278 Buffer = OutputBuffer->Pointer;
279 UserPrt = ACPI_CAST_PTR (ACPI_PCI_ROUTING_TABLE, Buffer);
280
281 for (Index = 0; Index < NumberOfElements; Index++)
282 {
283 /*
284 * Point UserPrt past this current structure
285 *
286 * NOTE: On the first iteration, UserPrt->Length will
287 * be zero because we cleared the return buffer earlier
288 */
289 Buffer += UserPrt->Length;
290 UserPrt = ACPI_CAST_PTR (ACPI_PCI_ROUTING_TABLE, Buffer);
291
292 /*
293 * Fill in the Length field with the information we have at this point.
294 * The minus four is to subtract the size of the UINT8 Source[4] member
295 * because it is added below.
296 */
297 UserPrt->Length = (sizeof (ACPI_PCI_ROUTING_TABLE) - 4);
298
299 /* Each element of the top-level package must also be a package */
300
301 if ((*TopObjectList)->Common.Type != ACPI_TYPE_PACKAGE)
302 {
303 ACPI_ERROR ((AE_INFO,
304 "(PRT[%X]) Need sub-package, found %s",
305 Index, AcpiUtGetObjectTypeName (*TopObjectList)));
306 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
307 }
308
309 /* Each sub-package must be of length 4 */
310
311 if ((*TopObjectList)->Package.Count != 4)
312 {
313 ACPI_ERROR ((AE_INFO,
314 "(PRT[%X]) Need package of length 4, found length %d",
315 Index, (*TopObjectList)->Package.Count));
316 return_ACPI_STATUS (AE_AML_PACKAGE_LIMIT);
317 }
318
319 /*
320 * Dereference the sub-package.
321 * The SubObjectList will now point to an array of the four IRQ
322 * elements: [Address, Pin, Source, SourceIndex]
323 */
324 SubObjectList = (*TopObjectList)->Package.Elements;
325
326 /* 1) First subobject: Dereference the PRT.Address */
327
328 ObjDesc = SubObjectList[0];
329 if (ObjDesc->Common.Type != ACPI_TYPE_INTEGER)
330 {
331 ACPI_ERROR ((AE_INFO, "(PRT[%X].Address) Need Integer, found %s",
332 Index, AcpiUtGetObjectTypeName (ObjDesc)));
333 return_ACPI_STATUS (AE_BAD_DATA);
334 }
335
336 UserPrt->Address = ObjDesc->Integer.Value;
337
338 /* 2) Second subobject: Dereference the PRT.Pin */
339
340 ObjDesc = SubObjectList[1];
341 if (ObjDesc->Common.Type != ACPI_TYPE_INTEGER)
342 {
343 ACPI_ERROR ((AE_INFO, "(PRT[%X].Pin) Need Integer, found %s",
344 Index, AcpiUtGetObjectTypeName (ObjDesc)));
345 return_ACPI_STATUS (AE_BAD_DATA);
346 }
347
348 UserPrt->Pin = (UINT32) ObjDesc->Integer.Value;
349
350 /*
351 * If the BIOS has erroneously reversed the _PRT SourceName (index 2)
352 * and the SourceIndex (index 3), fix it. _PRT is important enough to
353 * workaround this BIOS error. This also provides compatibility with
354 * other ACPI implementations.
355 */
356 ObjDesc = SubObjectList[3];
357 if (!ObjDesc || (ObjDesc->Common.Type != ACPI_TYPE_INTEGER))
358 {
359 SubObjectList[3] = SubObjectList[2];
360 SubObjectList[2] = ObjDesc;
361
362 ACPI_WARNING ((AE_INFO,
363 "(PRT[%X].Source) SourceName and SourceIndex are reversed, fixed",
364 Index));
365 }
366
367 /*
368 * 3) Third subobject: Dereference the PRT.SourceName
369 * The name may be unresolved (slack mode), so allow a null object
370 */
371 ObjDesc = SubObjectList[2];
372 if (ObjDesc)
373 {
374 switch (ObjDesc->Common.Type)
375 {
376 case ACPI_TYPE_LOCAL_REFERENCE:
377
378 if (ObjDesc->Reference.Class != ACPI_REFCLASS_NAME)
379 {
380 ACPI_ERROR ((AE_INFO,
381 "(PRT[%X].Source) Need name, found Reference Class %X",
382 Index, ObjDesc->Reference.Class));
383 return_ACPI_STATUS (AE_BAD_DATA);
384 }
385
386 Node = ObjDesc->Reference.Node;
387
388 /* Use *remaining* length of the buffer as max for pathname */
389
390 PathBuffer.Length = OutputBuffer->Length -
391 (UINT32) ((UINT8 *) UserPrt->Source -
392 (UINT8 *) OutputBuffer->Pointer);
393 PathBuffer.Pointer = UserPrt->Source;
394
395 Status = AcpiNsHandleToPathname ((ACPI_HANDLE) Node, &PathBuffer);
396
397 /* +1 to include null terminator */
398
399 UserPrt->Length += (UINT32) ACPI_STRLEN (UserPrt->Source) + 1;
400 break;
401
402
403 case ACPI_TYPE_STRING:
404
405 ACPI_STRCPY (UserPrt->Source, ObjDesc->String.Pointer);
406
407 /*
408 * Add to the Length field the length of the string
409 * (add 1 for terminator)
410 */
411 UserPrt->Length += ObjDesc->String.Length + 1;
412 break;
413
414
415 case ACPI_TYPE_INTEGER:
416 /*
417 * If this is a number, then the Source Name is NULL, since the
418 * entire buffer was zeroed out, we can leave this alone.
419 *
420 * Add to the Length field the length of the UINT32 NULL
421 */
422 UserPrt->Length += sizeof (UINT32);
423 break;
424
425
426 default:
427
428 ACPI_ERROR ((AE_INFO,
429 "(PRT[%X].Source) Need Ref/String/Integer, found %s",
430 Index, AcpiUtGetObjectTypeName (ObjDesc)));
431 return_ACPI_STATUS (AE_BAD_DATA);
432 }
433 }
434
435 /* Now align the current length */
436
437 UserPrt->Length = (UINT32) ACPI_ROUND_UP_TO_64BIT (UserPrt->Length);
438
439 /* 4) Fourth subobject: Dereference the PRT.SourceIndex */
440
441 ObjDesc = SubObjectList[3];
442 if (ObjDesc->Common.Type != ACPI_TYPE_INTEGER)
443 {
444 ACPI_ERROR ((AE_INFO,
445 "(PRT[%X].SourceIndex) Need Integer, found %s",
446 Index, AcpiUtGetObjectTypeName (ObjDesc)));
447 return_ACPI_STATUS (AE_BAD_DATA);
448 }
449
450 UserPrt->SourceIndex = (UINT32) ObjDesc->Integer.Value;
451
452 /* Point to the next ACPI_OPERAND_OBJECT in the top level package */
453
454 TopObjectList++;
455 }
456
457 ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
458 OutputBuffer->Pointer, (UINT32) OutputBuffer->Length));
459 return_ACPI_STATUS (AE_OK);
460 }
461
462
463 /*******************************************************************************
464 *
465 * FUNCTION: AcpiRsCreateAmlResources
466 *
467 * PARAMETERS: LinkedListBuffer - Pointer to the resource linked list
468 * OutputBuffer - Pointer to the user's buffer
469 *
470 * RETURN: Status AE_OK if okay, else a valid ACPI_STATUS code.
471 * If the OutputBuffer is too small, the error will be
472 * AE_BUFFER_OVERFLOW and OutputBuffer->Length will point
473 * to the size buffer needed.
474 *
475 * DESCRIPTION: Takes the linked list of device resources and
476 * creates a bytestream to be used as input for the
477 * _SRS control method.
478 *
479 ******************************************************************************/
480
481 ACPI_STATUS
482 AcpiRsCreateAmlResources (
483 ACPI_RESOURCE *LinkedListBuffer,
484 ACPI_BUFFER *OutputBuffer)
485 {
486 ACPI_STATUS Status;
487 ACPI_SIZE AmlSizeNeeded = 0;
488
489
490 ACPI_FUNCTION_TRACE (RsCreateAmlResources);
491
492
493 ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "LinkedListBuffer = %p\n",
494 LinkedListBuffer));
495
496 /*
497 * Params already validated, so we don't re-validate here
498 *
499 * Pass the LinkedListBuffer into a module that calculates
500 * the buffer size needed for the byte stream.
501 */
502 Status = AcpiRsGetAmlLength (LinkedListBuffer,
503 &AmlSizeNeeded);
504
505 ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "AmlSizeNeeded=%X, %s\n",
506 (UINT32) AmlSizeNeeded, AcpiFormatException (Status)));
507 if (ACPI_FAILURE (Status))
508 {
509 return_ACPI_STATUS (Status);
510 }
511
512 /* Validate/Allocate/Clear caller buffer */
513
514 Status = AcpiUtInitializeBuffer (OutputBuffer, AmlSizeNeeded);
515 if (ACPI_FAILURE (Status))
516 {
517 return_ACPI_STATUS (Status);
518 }
519
520 /* Do the conversion */
521
522 Status = AcpiRsConvertResourcesToAml (LinkedListBuffer, AmlSizeNeeded,
523 OutputBuffer->Pointer);
524 if (ACPI_FAILURE (Status))
525 {
526 return_ACPI_STATUS (Status);
527 }
528
529 ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "OutputBuffer %p Length %X\n",
530 OutputBuffer->Pointer, (UINT32) OutputBuffer->Length));
531 return_ACPI_STATUS (AE_OK);
532 }
533