bb1454b7bb0491c097ce70d4616f186ee605acbd
[reactos.git] / reactos / drivers / bus / acpi / acpica / utilities / utresrc.c
1 /*******************************************************************************
2 *
3 * Module Name: utresrc - Resource management utilities
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 "acresrc.h"
47
48
49 #define _COMPONENT ACPI_UTILITIES
50 ACPI_MODULE_NAME ("utresrc")
51
52
53 /*
54 * Base sizes of the raw AML resource descriptors, indexed by resource type.
55 * Zero indicates a reserved (and therefore invalid) resource type.
56 */
57 const UINT8 AcpiGbl_ResourceAmlSizes[] =
58 {
59 /* Small descriptors */
60
61 0,
62 0,
63 0,
64 0,
65 ACPI_AML_SIZE_SMALL (AML_RESOURCE_IRQ),
66 ACPI_AML_SIZE_SMALL (AML_RESOURCE_DMA),
67 ACPI_AML_SIZE_SMALL (AML_RESOURCE_START_DEPENDENT),
68 ACPI_AML_SIZE_SMALL (AML_RESOURCE_END_DEPENDENT),
69 ACPI_AML_SIZE_SMALL (AML_RESOURCE_IO),
70 ACPI_AML_SIZE_SMALL (AML_RESOURCE_FIXED_IO),
71 ACPI_AML_SIZE_SMALL (AML_RESOURCE_FIXED_DMA),
72 0,
73 0,
74 0,
75 ACPI_AML_SIZE_SMALL (AML_RESOURCE_VENDOR_SMALL),
76 ACPI_AML_SIZE_SMALL (AML_RESOURCE_END_TAG),
77
78 /* Large descriptors */
79
80 0,
81 ACPI_AML_SIZE_LARGE (AML_RESOURCE_MEMORY24),
82 ACPI_AML_SIZE_LARGE (AML_RESOURCE_GENERIC_REGISTER),
83 0,
84 ACPI_AML_SIZE_LARGE (AML_RESOURCE_VENDOR_LARGE),
85 ACPI_AML_SIZE_LARGE (AML_RESOURCE_MEMORY32),
86 ACPI_AML_SIZE_LARGE (AML_RESOURCE_FIXED_MEMORY32),
87 ACPI_AML_SIZE_LARGE (AML_RESOURCE_ADDRESS32),
88 ACPI_AML_SIZE_LARGE (AML_RESOURCE_ADDRESS16),
89 ACPI_AML_SIZE_LARGE (AML_RESOURCE_EXTENDED_IRQ),
90 ACPI_AML_SIZE_LARGE (AML_RESOURCE_ADDRESS64),
91 ACPI_AML_SIZE_LARGE (AML_RESOURCE_EXTENDED_ADDRESS64),
92 ACPI_AML_SIZE_LARGE (AML_RESOURCE_GPIO),
93 ACPI_AML_SIZE_LARGE (AML_RESOURCE_PIN_FUNCTION),
94 ACPI_AML_SIZE_LARGE (AML_RESOURCE_COMMON_SERIALBUS),
95 ACPI_AML_SIZE_LARGE (AML_RESOURCE_PIN_CONFIG),
96 ACPI_AML_SIZE_LARGE (AML_RESOURCE_PIN_GROUP),
97 ACPI_AML_SIZE_LARGE (AML_RESOURCE_PIN_GROUP_FUNCTION),
98 ACPI_AML_SIZE_LARGE (AML_RESOURCE_PIN_GROUP_CONFIG),
99 };
100
101 const UINT8 AcpiGbl_ResourceAmlSerialBusSizes[] =
102 {
103 0,
104 ACPI_AML_SIZE_LARGE (AML_RESOURCE_I2C_SERIALBUS),
105 ACPI_AML_SIZE_LARGE (AML_RESOURCE_SPI_SERIALBUS),
106 ACPI_AML_SIZE_LARGE (AML_RESOURCE_UART_SERIALBUS),
107 };
108
109
110 /*
111 * Resource types, used to validate the resource length field.
112 * The length of fixed-length types must match exactly, variable
113 * lengths must meet the minimum required length, etc.
114 * Zero indicates a reserved (and therefore invalid) resource type.
115 */
116 static const UINT8 AcpiGbl_ResourceTypes[] =
117 {
118 /* Small descriptors */
119
120 0,
121 0,
122 0,
123 0,
124 ACPI_SMALL_VARIABLE_LENGTH, /* 04 IRQ */
125 ACPI_FIXED_LENGTH, /* 05 DMA */
126 ACPI_SMALL_VARIABLE_LENGTH, /* 06 StartDependentFunctions */
127 ACPI_FIXED_LENGTH, /* 07 EndDependentFunctions */
128 ACPI_FIXED_LENGTH, /* 08 IO */
129 ACPI_FIXED_LENGTH, /* 09 FixedIO */
130 ACPI_FIXED_LENGTH, /* 0A FixedDMA */
131 0,
132 0,
133 0,
134 ACPI_VARIABLE_LENGTH, /* 0E VendorShort */
135 ACPI_FIXED_LENGTH, /* 0F EndTag */
136
137 /* Large descriptors */
138
139 0,
140 ACPI_FIXED_LENGTH, /* 01 Memory24 */
141 ACPI_FIXED_LENGTH, /* 02 GenericRegister */
142 0,
143 ACPI_VARIABLE_LENGTH, /* 04 VendorLong */
144 ACPI_FIXED_LENGTH, /* 05 Memory32 */
145 ACPI_FIXED_LENGTH, /* 06 Memory32Fixed */
146 ACPI_VARIABLE_LENGTH, /* 07 Dword* address */
147 ACPI_VARIABLE_LENGTH, /* 08 Word* address */
148 ACPI_VARIABLE_LENGTH, /* 09 ExtendedIRQ */
149 ACPI_VARIABLE_LENGTH, /* 0A Qword* address */
150 ACPI_FIXED_LENGTH, /* 0B Extended* address */
151 ACPI_VARIABLE_LENGTH, /* 0C Gpio* */
152 ACPI_VARIABLE_LENGTH, /* 0D PinFunction */
153 ACPI_VARIABLE_LENGTH, /* 0E *SerialBus */
154 ACPI_VARIABLE_LENGTH, /* 0F PinConfig */
155 ACPI_VARIABLE_LENGTH, /* 10 PinGroup */
156 ACPI_VARIABLE_LENGTH, /* 11 PinGroupFunction */
157 ACPI_VARIABLE_LENGTH, /* 12 PinGroupConfig */
158 };
159
160
161 /*******************************************************************************
162 *
163 * FUNCTION: AcpiUtWalkAmlResources
164 *
165 * PARAMETERS: WalkState - Current walk info
166 * PARAMETERS: Aml - Pointer to the raw AML resource template
167 * AmlLength - Length of the entire template
168 * UserFunction - Called once for each descriptor found. If
169 * NULL, a pointer to the EndTag is returned
170 * Context - Passed to UserFunction
171 *
172 * RETURN: Status
173 *
174 * DESCRIPTION: Walk a raw AML resource list(buffer). User function called
175 * once for each resource found.
176 *
177 ******************************************************************************/
178
179 ACPI_STATUS
180 AcpiUtWalkAmlResources (
181 ACPI_WALK_STATE *WalkState,
182 UINT8 *Aml,
183 ACPI_SIZE AmlLength,
184 ACPI_WALK_AML_CALLBACK UserFunction,
185 void **Context)
186 {
187 ACPI_STATUS Status;
188 UINT8 *EndAml;
189 UINT8 ResourceIndex;
190 UINT32 Length;
191 UINT32 Offset = 0;
192 UINT8 EndTag[2] = {0x79, 0x00};
193
194
195 ACPI_FUNCTION_TRACE (UtWalkAmlResources);
196
197
198 /* The absolute minimum resource template is one EndTag descriptor */
199
200 if (AmlLength < sizeof (AML_RESOURCE_END_TAG))
201 {
202 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
203 }
204
205 /* Point to the end of the resource template buffer */
206
207 EndAml = Aml + AmlLength;
208
209 /* Walk the byte list, abort on any invalid descriptor type or length */
210
211 while (Aml < EndAml)
212 {
213 /* Validate the Resource Type and Resource Length */
214
215 Status = AcpiUtValidateResource (WalkState, Aml, &ResourceIndex);
216 if (ACPI_FAILURE (Status))
217 {
218 /*
219 * Exit on failure. Cannot continue because the descriptor
220 * length may be bogus also.
221 */
222 return_ACPI_STATUS (Status);
223 }
224
225 /* Get the length of this descriptor */
226
227 Length = AcpiUtGetDescriptorLength (Aml);
228
229 /* Invoke the user function */
230
231 if (UserFunction)
232 {
233 Status = UserFunction (
234 Aml, Length, Offset, ResourceIndex, Context);
235 if (ACPI_FAILURE (Status))
236 {
237 return_ACPI_STATUS (Status);
238 }
239 }
240
241 /* An EndTag descriptor terminates this resource template */
242
243 if (AcpiUtGetResourceType (Aml) == ACPI_RESOURCE_NAME_END_TAG)
244 {
245 /*
246 * There must be at least one more byte in the buffer for
247 * the 2nd byte of the EndTag
248 */
249 if ((Aml + 1) >= EndAml)
250 {
251 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
252 }
253
254 /*
255 * The EndTag opcode must be followed by a zero byte.
256 * Although this byte is technically defined to be a checksum,
257 * in practice, all ASL compilers set this byte to zero.
258 */
259 if (*(Aml + 1) != 0)
260 {
261 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
262 }
263
264 /* Return the pointer to the EndTag if requested */
265
266 if (!UserFunction)
267 {
268 *Context = Aml;
269 }
270
271 /* Normal exit */
272
273 return_ACPI_STATUS (AE_OK);
274 }
275
276 Aml += Length;
277 Offset += Length;
278 }
279
280 /* Did not find an EndTag descriptor */
281
282 if (UserFunction)
283 {
284 /* Insert an EndTag anyway. AcpiRsGetListLength always leaves room */
285
286 (void) AcpiUtValidateResource (WalkState, EndTag, &ResourceIndex);
287 Status = UserFunction (EndTag, 2, Offset, ResourceIndex, Context);
288 if (ACPI_FAILURE (Status))
289 {
290 return_ACPI_STATUS (Status);
291 }
292 }
293
294 return_ACPI_STATUS (AE_AML_NO_RESOURCE_END_TAG);
295 }
296
297
298 /*******************************************************************************
299 *
300 * FUNCTION: AcpiUtValidateResource
301 *
302 * PARAMETERS: WalkState - Current walk info
303 * Aml - Pointer to the raw AML resource descriptor
304 * ReturnIndex - Where the resource index is returned. NULL
305 * if the index is not required.
306 *
307 * RETURN: Status, and optionally the Index into the global resource tables
308 *
309 * DESCRIPTION: Validate an AML resource descriptor by checking the Resource
310 * Type and Resource Length. Returns an index into the global
311 * resource information/dispatch tables for later use.
312 *
313 ******************************************************************************/
314
315 ACPI_STATUS
316 AcpiUtValidateResource (
317 ACPI_WALK_STATE *WalkState,
318 void *Aml,
319 UINT8 *ReturnIndex)
320 {
321 AML_RESOURCE *AmlResource;
322 UINT8 ResourceType;
323 UINT8 ResourceIndex;
324 ACPI_RS_LENGTH ResourceLength;
325 ACPI_RS_LENGTH MinimumResourceLength;
326
327
328 ACPI_FUNCTION_ENTRY ();
329
330
331 /*
332 * 1) Validate the ResourceType field (Byte 0)
333 */
334 ResourceType = ACPI_GET8 (Aml);
335
336 /*
337 * Byte 0 contains the descriptor name (Resource Type)
338 * Examine the large/small bit in the resource header
339 */
340 if (ResourceType & ACPI_RESOURCE_NAME_LARGE)
341 {
342 /* Verify the large resource type (name) against the max */
343
344 if (ResourceType > ACPI_RESOURCE_NAME_LARGE_MAX)
345 {
346 goto InvalidResource;
347 }
348
349 /*
350 * Large Resource Type -- bits 6:0 contain the name
351 * Translate range 0x80-0x8B to index range 0x10-0x1B
352 */
353 ResourceIndex = (UINT8) (ResourceType - 0x70);
354 }
355 else
356 {
357 /*
358 * Small Resource Type -- bits 6:3 contain the name
359 * Shift range to index range 0x00-0x0F
360 */
361 ResourceIndex = (UINT8)
362 ((ResourceType & ACPI_RESOURCE_NAME_SMALL_MASK) >> 3);
363 }
364
365 /*
366 * Check validity of the resource type, via AcpiGbl_ResourceTypes.
367 * Zero indicates an invalid resource.
368 */
369 if (!AcpiGbl_ResourceTypes[ResourceIndex])
370 {
371 goto InvalidResource;
372 }
373
374 /*
375 * Validate the ResourceLength field. This ensures that the length
376 * is at least reasonable, and guarantees that it is non-zero.
377 */
378 ResourceLength = AcpiUtGetResourceLength (Aml);
379 MinimumResourceLength = AcpiGbl_ResourceAmlSizes[ResourceIndex];
380
381 /* Validate based upon the type of resource - fixed length or variable */
382
383 switch (AcpiGbl_ResourceTypes[ResourceIndex])
384 {
385 case ACPI_FIXED_LENGTH:
386
387 /* Fixed length resource, length must match exactly */
388
389 if (ResourceLength != MinimumResourceLength)
390 {
391 goto BadResourceLength;
392 }
393 break;
394
395 case ACPI_VARIABLE_LENGTH:
396
397 /* Variable length resource, length must be at least the minimum */
398
399 if (ResourceLength < MinimumResourceLength)
400 {
401 goto BadResourceLength;
402 }
403 break;
404
405 case ACPI_SMALL_VARIABLE_LENGTH:
406
407 /* Small variable length resource, length can be (Min) or (Min-1) */
408
409 if ((ResourceLength > MinimumResourceLength) ||
410 (ResourceLength < (MinimumResourceLength - 1)))
411 {
412 goto BadResourceLength;
413 }
414 break;
415
416 default:
417
418 /* Shouldn't happen (because of validation earlier), but be sure */
419
420 goto InvalidResource;
421 }
422
423 AmlResource = ACPI_CAST_PTR (AML_RESOURCE, Aml);
424 if (ResourceType == ACPI_RESOURCE_NAME_SERIAL_BUS)
425 {
426 /* Validate the BusType field */
427
428 if ((AmlResource->CommonSerialBus.Type == 0) ||
429 (AmlResource->CommonSerialBus.Type > AML_RESOURCE_MAX_SERIALBUSTYPE))
430 {
431 if (WalkState)
432 {
433 ACPI_ERROR ((AE_INFO,
434 "Invalid/unsupported SerialBus resource descriptor: BusType 0x%2.2X",
435 AmlResource->CommonSerialBus.Type));
436 }
437 return (AE_AML_INVALID_RESOURCE_TYPE);
438 }
439 }
440
441 /* Optionally return the resource table index */
442
443 if (ReturnIndex)
444 {
445 *ReturnIndex = ResourceIndex;
446 }
447
448 return (AE_OK);
449
450
451 InvalidResource:
452
453 if (WalkState)
454 {
455 ACPI_ERROR ((AE_INFO,
456 "Invalid/unsupported resource descriptor: Type 0x%2.2X",
457 ResourceType));
458 }
459 return (AE_AML_INVALID_RESOURCE_TYPE);
460
461 BadResourceLength:
462
463 if (WalkState)
464 {
465 ACPI_ERROR ((AE_INFO,
466 "Invalid resource descriptor length: Type "
467 "0x%2.2X, Length 0x%4.4X, MinLength 0x%4.4X",
468 ResourceType, ResourceLength, MinimumResourceLength));
469 }
470 return (AE_AML_BAD_RESOURCE_LENGTH);
471 }
472
473
474 /*******************************************************************************
475 *
476 * FUNCTION: AcpiUtGetResourceType
477 *
478 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
479 *
480 * RETURN: The Resource Type with no extraneous bits (except the
481 * Large/Small descriptor bit -- this is left alone)
482 *
483 * DESCRIPTION: Extract the Resource Type/Name from the first byte of
484 * a resource descriptor.
485 *
486 ******************************************************************************/
487
488 UINT8
489 AcpiUtGetResourceType (
490 void *Aml)
491 {
492 ACPI_FUNCTION_ENTRY ();
493
494
495 /*
496 * Byte 0 contains the descriptor name (Resource Type)
497 * Examine the large/small bit in the resource header
498 */
499 if (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_LARGE)
500 {
501 /* Large Resource Type -- bits 6:0 contain the name */
502
503 return (ACPI_GET8 (Aml));
504 }
505 else
506 {
507 /* Small Resource Type -- bits 6:3 contain the name */
508
509 return ((UINT8) (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_SMALL_MASK));
510 }
511 }
512
513
514 /*******************************************************************************
515 *
516 * FUNCTION: AcpiUtGetResourceLength
517 *
518 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
519 *
520 * RETURN: Byte Length
521 *
522 * DESCRIPTION: Get the "Resource Length" of a raw AML descriptor. By
523 * definition, this does not include the size of the descriptor
524 * header or the length field itself.
525 *
526 ******************************************************************************/
527
528 UINT16
529 AcpiUtGetResourceLength (
530 void *Aml)
531 {
532 ACPI_RS_LENGTH ResourceLength;
533
534
535 ACPI_FUNCTION_ENTRY ();
536
537
538 /*
539 * Byte 0 contains the descriptor name (Resource Type)
540 * Examine the large/small bit in the resource header
541 */
542 if (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_LARGE)
543 {
544 /* Large Resource type -- bytes 1-2 contain the 16-bit length */
545
546 ACPI_MOVE_16_TO_16 (&ResourceLength, ACPI_ADD_PTR (UINT8, Aml, 1));
547
548 }
549 else
550 {
551 /* Small Resource type -- bits 2:0 of byte 0 contain the length */
552
553 ResourceLength = (UINT16) (ACPI_GET8 (Aml) &
554 ACPI_RESOURCE_NAME_SMALL_LENGTH_MASK);
555 }
556
557 return (ResourceLength);
558 }
559
560
561 /*******************************************************************************
562 *
563 * FUNCTION: AcpiUtGetResourceHeaderLength
564 *
565 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
566 *
567 * RETURN: Length of the AML header (depends on large/small descriptor)
568 *
569 * DESCRIPTION: Get the length of the header for this resource.
570 *
571 ******************************************************************************/
572
573 UINT8
574 AcpiUtGetResourceHeaderLength (
575 void *Aml)
576 {
577 ACPI_FUNCTION_ENTRY ();
578
579
580 /* Examine the large/small bit in the resource header */
581
582 if (ACPI_GET8 (Aml) & ACPI_RESOURCE_NAME_LARGE)
583 {
584 return (sizeof (AML_RESOURCE_LARGE_HEADER));
585 }
586 else
587 {
588 return (sizeof (AML_RESOURCE_SMALL_HEADER));
589 }
590 }
591
592
593 /*******************************************************************************
594 *
595 * FUNCTION: AcpiUtGetDescriptorLength
596 *
597 * PARAMETERS: Aml - Pointer to the raw AML resource descriptor
598 *
599 * RETURN: Byte length
600 *
601 * DESCRIPTION: Get the total byte length of a raw AML descriptor, including the
602 * length of the descriptor header and the length field itself.
603 * Used to walk descriptor lists.
604 *
605 ******************************************************************************/
606
607 UINT32
608 AcpiUtGetDescriptorLength (
609 void *Aml)
610 {
611 ACPI_FUNCTION_ENTRY ();
612
613
614 /*
615 * Get the Resource Length (does not include header length) and add
616 * the header length (depends on if this is a small or large resource)
617 */
618 return (AcpiUtGetResourceLength (Aml) +
619 AcpiUtGetResourceHeaderLength (Aml));
620 }
621
622
623 /*******************************************************************************
624 *
625 * FUNCTION: AcpiUtGetResourceEndTag
626 *
627 * PARAMETERS: ObjDesc - The resource template buffer object
628 * EndTag - Where the pointer to the EndTag is returned
629 *
630 * RETURN: Status, pointer to the end tag
631 *
632 * DESCRIPTION: Find the EndTag resource descriptor in an AML resource template
633 * Note: allows a buffer length of zero.
634 *
635 ******************************************************************************/
636
637 ACPI_STATUS
638 AcpiUtGetResourceEndTag (
639 ACPI_OPERAND_OBJECT *ObjDesc,
640 UINT8 **EndTag)
641 {
642 ACPI_STATUS Status;
643
644
645 ACPI_FUNCTION_TRACE (UtGetResourceEndTag);
646
647
648 /* Allow a buffer length of zero */
649
650 if (!ObjDesc->Buffer.Length)
651 {
652 *EndTag = ObjDesc->Buffer.Pointer;
653 return_ACPI_STATUS (AE_OK);
654 }
655
656 /* Validate the template and get a pointer to the EndTag */
657
658 Status = AcpiUtWalkAmlResources (NULL, ObjDesc->Buffer.Pointer,
659 ObjDesc->Buffer.Length, NULL, (void **) EndTag);
660
661 return_ACPI_STATUS (Status);
662 }