xref: /netbsd-src/sys/external/bsd/acpica/dist/common/dmrestag.c (revision 046a29855e04359424fd074e8313af6b6be8cfb6)
1 /******************************************************************************
2  *
3  * Module Name: dmrestag - Add tags to resource descriptors (Application-level)
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2023, 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 MERCHANTABILITY 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 "acparser.h"
47 #include "acdisasm.h"
48 #include "acnamesp.h"
49 #include "amlcode.h"
50 
51 /* This module used for application-level code only */
52 
53 #define _COMPONENT          ACPI_CA_DISASSEMBLER
54         ACPI_MODULE_NAME    ("dmrestag")
55 
56 /* Local prototypes */
57 
58 static void
59 AcpiDmUpdateResourceName (
60     ACPI_NAMESPACE_NODE     *ResourceNode);
61 
62 static char *
63 AcpiDmSearchTagList (
64     UINT32                  BitIndex,
65     const ACPI_RESOURCE_TAG *TagList);
66 
67 static char *
68 AcpiDmGetResourceTag (
69     UINT32                  BitIndex,
70     AML_RESOURCE            *Resource,
71     UINT8                   ResourceIndex);
72 
73 static char *
74 AcpiGetTagPathname (
75     ACPI_PARSE_OBJECT       *Op,
76     ACPI_NAMESPACE_NODE     *BufferNode,
77     ACPI_NAMESPACE_NODE     *ResourceNode,
78     UINT32                  BitIndex);
79 
80 static ACPI_NAMESPACE_NODE *
81 AcpiDmGetResourceNode (
82     ACPI_NAMESPACE_NODE     *BufferNode,
83     UINT32                  BitIndex);
84 
85 static ACPI_STATUS
86 AcpiDmAddResourceToNamespace (
87     UINT8                   *Aml,
88     UINT32                  Length,
89     UINT32                  Offset,
90     UINT8                   ResourceIndex,
91     void                    **Context);
92 
93 static void
94 AcpiDmAddResourcesToNamespace (
95     ACPI_NAMESPACE_NODE     *BufferNode,
96     ACPI_PARSE_OBJECT       *Op);
97 
98 
99 /******************************************************************************
100  *
101  * Resource Tag tables
102  *
103  * These are the predefined tags that refer to elements of a resource
104  * descriptor. Each name and offset is defined in the ACPI specification.
105  *
106  * Each table entry contains the bit offset of the field and the associated
107  * name.
108  *
109  ******************************************************************************/
110 
111 static const ACPI_RESOURCE_TAG      AcpiDmIrqTags[] =
112 {
113     {( 1 * 8),      ACPI_RESTAG_INTERRUPT},
114     {( 3 * 8) + 0,  ACPI_RESTAG_INTERRUPTTYPE},
115     {( 3 * 8) + 3,  ACPI_RESTAG_INTERRUPTLEVEL},
116     {( 3 * 8) + 4,  ACPI_RESTAG_INTERRUPTSHARE},
117     {0,             NULL}
118 };
119 
120 static const ACPI_RESOURCE_TAG      AcpiDmDmaTags[] =
121 {
122     {( 1 * 8),      ACPI_RESTAG_DMA},
123     {( 2 * 8) + 0,  ACPI_RESTAG_XFERTYPE},
124     {( 2 * 8) + 2,  ACPI_RESTAG_BUSMASTER},
125     {( 2 * 8) + 5,  ACPI_RESTAG_DMATYPE},
126     {0,             NULL}
127 };
128 
129 static const ACPI_RESOURCE_TAG      AcpiDmIoTags[] =
130 {
131     {( 1 * 8) + 0,  ACPI_RESTAG_DECODE},
132     {( 2 * 8),      ACPI_RESTAG_MINADDR},
133     {( 4 * 8),      ACPI_RESTAG_MAXADDR},
134     {( 6 * 8),      ACPI_RESTAG_ALIGNMENT},
135     {( 7 * 8),      ACPI_RESTAG_LENGTH},
136     {0,             NULL}
137 };
138 
139 static const ACPI_RESOURCE_TAG      AcpiDmFixedIoTags[] =
140 {
141     {( 1 * 8),      ACPI_RESTAG_BASEADDRESS},
142     {( 3 * 8),      ACPI_RESTAG_LENGTH},
143     {0,             NULL}
144 };
145 
146 static const ACPI_RESOURCE_TAG      AcpiDmFixedDmaTags[] =
147 {
148     {( 1 * 8),      ACPI_RESTAG_DMA},
149     {( 3 * 8),      ACPI_RESTAG_DMATYPE},
150     {( 5 * 8),      ACPI_RESTAG_XFERTYPE},
151     {0,             NULL}
152 };
153 
154 static const ACPI_RESOURCE_TAG      AcpiDmMemory24Tags[] =
155 {
156     {( 3 * 8) + 0,  ACPI_RESTAG_READWRITETYPE},
157     {( 4 * 8),      ACPI_RESTAG_MINADDR},
158     {( 6 * 8),      ACPI_RESTAG_MAXADDR},
159     {( 8 * 8),      ACPI_RESTAG_ALIGNMENT},
160     {(10 * 8),      ACPI_RESTAG_LENGTH},
161     {0,             NULL}
162 };
163 
164 static const ACPI_RESOURCE_TAG      AcpiDmRegisterTags[] =
165 {
166     {( 3 * 8),      ACPI_RESTAG_ADDRESSSPACE},
167     {( 4 * 8),      ACPI_RESTAG_REGISTERBITWIDTH},
168     {( 5 * 8),      ACPI_RESTAG_REGISTERBITOFFSET},
169     {( 6 * 8),      ACPI_RESTAG_ACCESSSIZE},
170     {( 7 * 8),      ACPI_RESTAG_ADDRESS},
171     {0,             NULL}
172 };
173 
174 static const ACPI_RESOURCE_TAG      AcpiDmMemory32Tags[] =
175 {
176     {( 3 * 8) + 0,  ACPI_RESTAG_READWRITETYPE},
177     {( 4 * 8),      ACPI_RESTAG_MINADDR},
178     {( 8 * 8),      ACPI_RESTAG_MAXADDR},
179     {(12 * 8),      ACPI_RESTAG_ALIGNMENT},
180     {(16 * 8),      ACPI_RESTAG_LENGTH},
181     {0,             NULL}
182 };
183 
184 static const ACPI_RESOURCE_TAG      AcpiDmFixedMemory32Tags[] =
185 {
186     {( 3 * 8) + 0,  ACPI_RESTAG_READWRITETYPE},
187     {( 4 * 8),      ACPI_RESTAG_BASEADDRESS},
188     {( 8 * 8),      ACPI_RESTAG_LENGTH},
189     {0,             NULL}
190 };
191 
192 static const ACPI_RESOURCE_TAG      AcpiDmInterruptTags[] =
193 {
194     {( 3 * 8) + 1,  ACPI_RESTAG_INTERRUPTTYPE},
195     {( 3 * 8) + 2,  ACPI_RESTAG_INTERRUPTLEVEL},
196     {( 3 * 8) + 3,  ACPI_RESTAG_INTERRUPTSHARE},
197     {( 5 * 8),      ACPI_RESTAG_INTERRUPT},
198     {0,             NULL}
199 };
200 
201 static const ACPI_RESOURCE_TAG      AcpiDmAddress16Tags[] =
202 {
203     {( 4 * 8) + 1,  ACPI_RESTAG_DECODE},
204     {( 4 * 8) + 2,  ACPI_RESTAG_MINTYPE},
205     {( 4 * 8) + 3,  ACPI_RESTAG_MAXTYPE},
206     {( 6 * 8),      ACPI_RESTAG_GRANULARITY},
207     {( 8 * 8),      ACPI_RESTAG_MINADDR},
208     {(10 * 8),      ACPI_RESTAG_MAXADDR},
209     {(12 * 8),      ACPI_RESTAG_TRANSLATION},
210     {(14 * 8),      ACPI_RESTAG_LENGTH},
211     {0,             NULL}
212 };
213 
214 static const ACPI_RESOURCE_TAG      AcpiDmAddress32Tags[] =
215 {
216     {( 4 * 8) + 1,  ACPI_RESTAG_DECODE},
217     {( 4 * 8) + 2,  ACPI_RESTAG_MINTYPE},
218     {( 4 * 8) + 3,  ACPI_RESTAG_MAXTYPE},
219     {( 6 * 8),      ACPI_RESTAG_GRANULARITY},
220     {(10 * 8),      ACPI_RESTAG_MINADDR},
221     {(14 * 8),      ACPI_RESTAG_MAXADDR},
222     {(18 * 8),      ACPI_RESTAG_TRANSLATION},
223     {(22 * 8),      ACPI_RESTAG_LENGTH},
224     {0,             NULL}
225 };
226 
227 static const ACPI_RESOURCE_TAG      AcpiDmAddress64Tags[] =
228 {
229     {( 4 * 8) + 1,  ACPI_RESTAG_DECODE},
230     {( 4 * 8) + 2,  ACPI_RESTAG_MINTYPE},
231     {( 4 * 8) + 3,  ACPI_RESTAG_MAXTYPE},
232     {( 6 * 8),      ACPI_RESTAG_GRANULARITY},
233     {(14 * 8),      ACPI_RESTAG_MINADDR},
234     {(22 * 8),      ACPI_RESTAG_MAXADDR},
235     {(30 * 8),      ACPI_RESTAG_TRANSLATION},
236     {(38 * 8),      ACPI_RESTAG_LENGTH},
237     {0,             NULL}
238 };
239 
240 static const ACPI_RESOURCE_TAG      AcpiDmExtendedAddressTags[] =
241 {
242     {( 4 * 8) + 1,  ACPI_RESTAG_DECODE},
243     {( 4 * 8) + 2,  ACPI_RESTAG_MINTYPE},
244     {( 4 * 8) + 3,  ACPI_RESTAG_MAXTYPE},
245     {( 8 * 8),      ACPI_RESTAG_GRANULARITY},
246     {(16 * 8),      ACPI_RESTAG_MINADDR},
247     {(24 * 8),      ACPI_RESTAG_MAXADDR},
248     {(32 * 8),      ACPI_RESTAG_TRANSLATION},
249     {(40 * 8),      ACPI_RESTAG_LENGTH},
250     {(48 * 8),      ACPI_RESTAG_TYPESPECIFICATTRIBUTES},
251     {0,             NULL}
252 };
253 
254 /* Subtype tables for GPIO descriptors */
255 
256 static const ACPI_RESOURCE_TAG      AcpiDmGpioIntTags[] =
257 {
258     {( 7 * 8) + 0,  ACPI_RESTAG_MODE},
259     {( 7 * 8) + 1,  ACPI_RESTAG_POLARITY},
260     {( 7 * 8) + 3,  ACPI_RESTAG_INTERRUPTSHARE},
261     {( 9 * 8),      ACPI_RESTAG_PINCONFIG},
262     {(10 * 8),      ACPI_RESTAG_DRIVESTRENGTH},
263     {(12 * 8),      ACPI_RESTAG_DEBOUNCETIME},
264     {0,             NULL}
265 };
266 
267 static const ACPI_RESOURCE_TAG      AcpiDmGpioIoTags[] =
268 {
269     {( 7 * 8) + 0,  ACPI_RESTAG_IORESTRICTION},
270     {( 7 * 8) + 3,  ACPI_RESTAG_INTERRUPTSHARE},
271     {( 9 * 8),      ACPI_RESTAG_PINCONFIG},
272     {(10 * 8),      ACPI_RESTAG_DRIVESTRENGTH},
273     {(12 * 8),      ACPI_RESTAG_DEBOUNCETIME},
274     {0,             NULL}
275 };
276 
277 /* Subtype tables for SerialBus descriptors */
278 
279 static const ACPI_RESOURCE_TAG      AcpiDmCsi2SerialBusTags[] =    /* ACPI 6.4 */
280 {
281     {( 6 * 8) + 0,  ACPI_RESTAG_SLAVEMODE},
282     {( 7 * 8) + 0,  ACPI_RESTAG_PHYTYPE},
283     {( 7 * 8) + 2,  ACPI_RESTAG_LOCALPORT},
284     {0,             NULL}
285 };
286 
287 static const ACPI_RESOURCE_TAG      AcpiDmI2cSerialBusTags[] =
288 {
289     {( 6 * 8) + 0,  ACPI_RESTAG_SLAVEMODE},
290     {( 6 * 8) + 2,  ACPI_RESTAG_INTERRUPTSHARE},    /* V2 - ACPI 6.0 */
291     {( 7 * 8) + 0,  ACPI_RESTAG_MODE},
292     {(12 * 8),      ACPI_RESTAG_SPEED},
293     {(16 * 8),      ACPI_RESTAG_ADDRESS},
294     {0,             NULL}
295 };
296 
297 static const ACPI_RESOURCE_TAG      AcpiDmSpiSerialBusTags[] =
298 {
299     {( 6 * 8) + 0,  ACPI_RESTAG_SLAVEMODE},
300     {( 6 * 8) + 2,  ACPI_RESTAG_INTERRUPTSHARE},    /* V2 - ACPI 6.0 */
301     {( 7 * 8) + 0,  ACPI_RESTAG_MODE},
302     {( 7 * 8) + 1,  ACPI_RESTAG_DEVICEPOLARITY},
303     {(12 * 8),      ACPI_RESTAG_SPEED},
304     {(16 * 8),      ACPI_RESTAG_LENGTH},
305     {(17 * 8),      ACPI_RESTAG_PHASE},
306     {(18 * 8),      ACPI_RESTAG_POLARITY},
307     {(19 * 8),      ACPI_RESTAG_ADDRESS},
308     {0,             NULL}
309 };
310 
311 static const ACPI_RESOURCE_TAG      AcpiDmUartSerialBusTags[] =
312 {
313     {( 6 * 8) + 0,  ACPI_RESTAG_SLAVEMODE},         /* Note: not part of original macro */
314     {( 6 * 8) + 2,  ACPI_RESTAG_INTERRUPTSHARE},    /* V2 - ACPI 6.0 */
315     {( 7 * 8) + 0,  ACPI_RESTAG_FLOWCONTROL},
316     {( 7 * 8) + 2,  ACPI_RESTAG_STOPBITS},
317     {( 7 * 8) + 4,  ACPI_RESTAG_LENGTH},
318     {( 7 * 8) + 7,  ACPI_RESTAG_ENDIANNESS},
319     {(12 * 8),      ACPI_RESTAG_SPEED},
320     {(16 * 8),      ACPI_RESTAG_LENGTH_RX},
321     {(18 * 8),      ACPI_RESTAG_LENGTH_TX},
322     {(20 * 8),      ACPI_RESTAG_PARITY},
323     {(21 * 8),      ACPI_RESTAG_LINE},
324     {0,             NULL}
325 };
326 
327 
328 /* Subtype tables for PinFunction descriptor */
329 
330 static const ACPI_RESOURCE_TAG      AcpiDmPinFunctionTags[] =
331 {
332     {( 4 * 8),      ACPI_RESTAG_INTERRUPTSHARE},
333     {( 6 * 8),      ACPI_RESTAG_PINCONFIG},
334     {( 7 * 8),      ACPI_RESTAG_FUNCTION},
335     {0,             NULL}
336 };
337 
338 /* Subtype tables for PinConfig descriptor */
339 
340 static const ACPI_RESOURCE_TAG      AcpiDmPinConfigTags[] =
341 {
342     {( 4 * 8),      ACPI_RESTAG_INTERRUPTSHARE},
343     {( 6 * 8),      ACPI_RESTAG_PINCONFIG_TYPE},
344     {( 7 * 8),      ACPI_RESTAG_PINCONFIG_VALUE},
345     {0,             NULL}
346 };
347 
348 /* Subtype tables for PinGroupFunction descriptor */
349 
350 static const ACPI_RESOURCE_TAG      AcpiDmPinGroupFunctionTags[] =
351 {
352     {( 6 * 8),      ACPI_RESTAG_FUNCTION},
353     {0,             NULL}
354 };
355 
356 /* Subtype tables for Address descriptor type-specific flags */
357 
358 static const ACPI_RESOURCE_TAG      AcpiDmMemoryFlagTags[] =
359 {
360     {( 5 * 8) + 0,  ACPI_RESTAG_READWRITETYPE},
361     {( 5 * 8) + 1,  ACPI_RESTAG_MEMTYPE},
362     {( 5 * 8) + 3,  ACPI_RESTAG_MEMATTRIBUTES},
363     {( 5 * 8) + 5,  ACPI_RESTAG_TYPE},
364     {0,             NULL}
365 };
366 
367 static const ACPI_RESOURCE_TAG      AcpiDmIoFlagTags[] =
368 {
369     {( 5 * 8) + 0,  ACPI_RESTAG_RANGETYPE},
370     {( 5 * 8) + 4,  ACPI_RESTAG_TYPE},
371     {( 5 * 8) + 5,  ACPI_RESTAG_TRANSTYPE},
372     {0,             NULL}
373 };
374 
375 /* Subtype tables for ClockInput descriptor */
376 
377 static const ACPI_RESOURCE_TAG      AcpiDmClockInputTags[] =
378 {
379     {( 6 * 8),      ACPI_RESTAG_FQD},
380     {( 8 * 8),      ACPI_RESTAG_FQN},
381     {0,             NULL}
382 };
383 
384 /*
385  * Dispatch table used to obtain the correct tag table for a descriptor.
386  *
387  * A NULL in this table means one of three things:
388  * 1) The descriptor ID is reserved and invalid
389  * 2) The descriptor has no tags associated with it
390  * 3) The descriptor has subtypes and a separate table will be used.
391  */
392 static const ACPI_RESOURCE_TAG      *AcpiGbl_ResourceTags[] =
393 {
394     /* Small descriptors */
395 
396     NULL,                           /* 0x00, Reserved */
397     NULL,                           /* 0x01, Reserved */
398     NULL,                           /* 0x02, Reserved */
399     NULL,                           /* 0x03, Reserved */
400     AcpiDmIrqTags,                  /* 0x04, ACPI_RESOURCE_NAME_IRQ_FORMAT */
401     AcpiDmDmaTags,                  /* 0x05, ACPI_RESOURCE_NAME_DMA_FORMAT */
402     NULL,                           /* 0x06, ACPI_RESOURCE_NAME_START_DEPENDENT */
403     NULL,                           /* 0x07, ACPI_RESOURCE_NAME_END_DEPENDENT */
404     AcpiDmIoTags,                   /* 0x08, ACPI_RESOURCE_NAME_IO_PORT */
405     AcpiDmFixedIoTags,              /* 0x09, ACPI_RESOURCE_NAME_FIXED_IO_PORT */
406     AcpiDmFixedDmaTags,             /* 0x0A, ACPI_RESOURCE_NAME_FIXED_DMA */
407     NULL,                           /* 0x0B, Reserved */
408     NULL,                           /* 0x0C, Reserved */
409     NULL,                           /* 0x0D, Reserved */
410     NULL,                           /* 0x0E, ACPI_RESOURCE_NAME_SMALL_VENDOR */
411     NULL,                           /* 0x0F, ACPI_RESOURCE_NAME_END_TAG (not used) */
412 
413     /* Large descriptors */
414 
415     NULL,                           /* 0x00, Reserved */
416     AcpiDmMemory24Tags,             /* 0x01, ACPI_RESOURCE_NAME_MEMORY_24 */
417     AcpiDmRegisterTags,             /* 0x02, ACPI_RESOURCE_NAME_GENERIC_REGISTER */
418     NULL,                           /* 0x03, Reserved */
419     NULL,                           /* 0x04, ACPI_RESOURCE_NAME_LARGE_VENDOR */
420     AcpiDmMemory32Tags,             /* 0x05, ACPI_RESOURCE_NAME_MEMORY_32 */
421     AcpiDmFixedMemory32Tags,        /* 0x06, ACPI_RESOURCE_NAME_FIXED_MEMORY_32 */
422     AcpiDmAddress32Tags,            /* 0x07, ACPI_RESOURCE_NAME_DWORD_ADDRESS_SPACE */
423     AcpiDmAddress16Tags,            /* 0x08, ACPI_RESOURCE_NAME_WORD_ADDRESS_SPACE */
424     AcpiDmInterruptTags,            /* 0x09, ACPI_RESOURCE_NAME_EXTENDED_XRUPT */
425     AcpiDmAddress64Tags,            /* 0x0A, ACPI_RESOURCE_NAME_QWORD_ADDRESS_SPACE */
426     AcpiDmExtendedAddressTags,      /* 0x0B, ACPI_RESOURCE_NAME_EXTENDED_ADDRESS_SPACE */
427     NULL,                           /* 0x0C, ACPI_RESOURCE_NAME_GPIO - Use Subtype table below */
428     AcpiDmPinFunctionTags,          /* 0x0D, ACPI_RESOURCE_NAME_PIN_FUNCTION */
429     NULL,                           /* 0x0E, ACPI_RESOURCE_NAME_SERIAL_BUS - Use Subtype table below */
430     AcpiDmPinConfigTags,            /* 0x0F, ACPI_RESOURCE_NAME_PIN_CONFIG */
431     NULL,                           /* 0x10, ACPI_RESOURCE_NAME_PIN_GROUP */
432     AcpiDmPinGroupFunctionTags,     /* 0x11, ACPI_RESOURCE_NAME_PIN_GROUP_FUNCTION */
433     AcpiDmPinConfigTags,            /* 0x12, ACPI_RESOURCE_NAME_PIN_GROUP_CONFIG - Same as PinConfig */
434     AcpiDmClockInputTags,           /* 0x13, ACPI_RESOURCE_NAME_CLOCK_INPUT */
435 };
436 
437 /* GPIO Subtypes */
438 
439 static const ACPI_RESOURCE_TAG      *AcpiGbl_GpioResourceTags[] =
440 {
441     AcpiDmGpioIntTags,              /* 0x00 Interrupt Connection */
442     AcpiDmGpioIoTags                /* 0x01 I/O Connection */
443 };
444 
445 /* Serial Bus Subtypes */
446 
447 static const ACPI_RESOURCE_TAG      *AcpiGbl_SerialResourceTags[] =
448 {
449     NULL,                           /* 0x00 Reserved */
450     AcpiDmI2cSerialBusTags,         /* 0x01 I2C SerialBus */
451     AcpiDmSpiSerialBusTags,         /* 0x02 SPI SerialBus */
452     AcpiDmUartSerialBusTags,        /* 0x03 UART SerialBus */
453     AcpiDmCsi2SerialBusTags         /* 0x04 CSI2 SerialBus */
454 };
455 
456 /*
457  * Globals used to generate unique resource descriptor names. We use names that
458  * start with underscore and a prefix letter that is not used by other ACPI
459  * reserved names. To this, we append hex 0x00 through 0xFF. These 5 prefixes
460  * allow for 5*256 = 1280 unique names, probably sufficient for any single ASL
461  * file. If this becomes too small, we can use alpha+numerals for a total
462  * of 5*36*36 = 6480.
463  */
464 #define ACPI_NUM_RES_PREFIX     5
465 
466 static UINT32                   AcpiGbl_NextResourceId = 0;
467 static UINT8                    AcpiGbl_NextPrefix = 0;
468 static char                     AcpiGbl_Prefix[ACPI_NUM_RES_PREFIX] =
469                                     {'Y','Z','J','K','X'};
470 
471 
472 /*******************************************************************************
473  *
474  * FUNCTION:    AcpiDmCheckResourceReference
475  *
476  * PARAMETERS:  Op                  - Parse Op for the AML opcode
477  *              WalkState           - Current walk state (with valid scope)
478  *
479  * RETURN:      None
480  *
481  * DESCRIPTION: Convert a reference to a resource descriptor to a symbolic
482  *              reference if possible
483  *
484  * NOTE:        Bit index is used to transparently handle both resource bit
485  *              fields and byte fields.
486  *
487  ******************************************************************************/
488 
489 void
AcpiDmCheckResourceReference(ACPI_PARSE_OBJECT * Op,ACPI_WALK_STATE * WalkState)490 AcpiDmCheckResourceReference (
491     ACPI_PARSE_OBJECT       *Op,
492     ACPI_WALK_STATE         *WalkState)
493 {
494     ACPI_STATUS             Status;
495     ACPI_PARSE_OBJECT       *BufferNameOp;
496     ACPI_PARSE_OBJECT       *IndexOp;
497     ACPI_NAMESPACE_NODE     *BufferNode;
498     ACPI_NAMESPACE_NODE     *ResourceNode;
499     const ACPI_OPCODE_INFO  *OpInfo;
500     UINT32                  BitIndex;
501 
502 
503     /* We are only interested in the CreateXxxxField opcodes */
504 
505     OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
506     if (OpInfo->Type != AML_TYPE_CREATE_FIELD)
507     {
508         return;
509     }
510 
511     /* Get the buffer term operand */
512 
513     BufferNameOp = AcpiPsGetDepthNext (NULL, Op);
514 
515     /* Must be a named buffer, not an arg or local or method call */
516 
517     if (BufferNameOp->Common.AmlOpcode != AML_INT_NAMEPATH_OP)
518     {
519         return;
520     }
521 
522     /* Get the Index term, must be an integer constant to convert */
523 
524     IndexOp = BufferNameOp->Common.Next;
525 
526     /* Major cheat: The Node field is also used for the Tag ptr. Clear it now */
527 
528     IndexOp->Common.Node = NULL;
529 
530     OpInfo = AcpiPsGetOpcodeInfo (IndexOp->Common.AmlOpcode);
531     if (OpInfo->ObjectType != ACPI_TYPE_INTEGER)
532     {
533         return;
534     }
535 
536     /* Get the bit offset of the descriptor within the buffer */
537 
538     if ((Op->Common.AmlOpcode == AML_CREATE_BIT_FIELD_OP) ||
539         (Op->Common.AmlOpcode == AML_CREATE_FIELD_OP))
540     {
541         /* Index operand is a bit offset */
542 
543         BitIndex = (UINT32) IndexOp->Common.Value.Integer;
544     }
545     else
546     {
547         /* Index operand is a byte offset, convert to bits */
548 
549         BitIndex = (UINT32) ACPI_MUL_8 (IndexOp->Common.Value.Integer);
550     }
551 
552     /* Lookup the buffer in the namespace */
553 
554     Status = AcpiNsLookup (WalkState->ScopeInfo,
555         BufferNameOp->Common.Value.String, ACPI_TYPE_BUFFER,
556         ACPI_IMODE_EXECUTE, ACPI_NS_SEARCH_PARENT, WalkState,
557         &BufferNode);
558     if (ACPI_FAILURE (Status))
559     {
560         return;
561     }
562 
563     /* Validate object type, we must have a buffer */
564 
565     if (BufferNode->Type != ACPI_TYPE_BUFFER)
566     {
567         return;
568     }
569 
570     /* Find the resource descriptor node corresponding to the index */
571 
572     ResourceNode = AcpiDmGetResourceNode (BufferNode, BitIndex);
573     if (!ResourceNode)
574     {
575         return;
576     }
577 
578     /* Translate the Index to a resource tag pathname */
579 
580     AcpiGetTagPathname (IndexOp, BufferNode, ResourceNode, BitIndex);
581 }
582 
583 
584 /*******************************************************************************
585  *
586  * FUNCTION:    AcpiDmGetResourceNode
587  *
588  * PARAMETERS:  BufferNode          - Node for the parent buffer
589  *              BitIndex            - Index into the resource descriptor
590  *
591  * RETURN:      Namespace node for the resource descriptor. NULL if not found
592  *
593  * DESCRIPTION: Find a resource descriptor that corresponds to the bit index
594  *
595  ******************************************************************************/
596 
597 static ACPI_NAMESPACE_NODE *
AcpiDmGetResourceNode(ACPI_NAMESPACE_NODE * BufferNode,UINT32 BitIndex)598 AcpiDmGetResourceNode (
599     ACPI_NAMESPACE_NODE     *BufferNode,
600     UINT32                  BitIndex)
601 {
602     ACPI_NAMESPACE_NODE     *Node;
603     UINT32                  ByteIndex = ACPI_DIV_8 (BitIndex);
604 
605 
606     /*
607      * Child list contains an entry for each resource descriptor. Find
608      * the descriptor that corresponds to the Index.
609      *
610      * If there are no children, this is not a resource template
611      */
612     Node = BufferNode->Child;
613     while (Node)
614     {
615         /*
616          * Check if the Index falls within this resource.
617          *
618          * Value contains the resource offset, Object contains the resource
619          * length (both in bytes)
620          */
621         if ((ByteIndex >= Node->Value) &&
622             (ByteIndex < (Node->Value + Node->Length)))
623         {
624             return (Node);
625         }
626 
627         Node = Node->Peer;
628     }
629 
630     return (NULL);
631 }
632 
633 
634 /*******************************************************************************
635  *
636  * FUNCTION:    AcpiGetTagPathname
637  *
638  * PARAMETERS:  BufferNode          - Node for the parent buffer
639  *              ResourceNode        - Node for a resource descriptor
640  *              BitIndex            - Index into the resource descriptor
641  *
642  * RETURN:      Full pathname for a resource tag. NULL if no match.
643  *              Path is returned in AML (packed) format.
644  *
645  * DESCRIPTION: Convert a BitIndex into a symbolic resource tag (full pathname)
646  *
647  ******************************************************************************/
648 
649 static char *
AcpiGetTagPathname(ACPI_PARSE_OBJECT * IndexOp,ACPI_NAMESPACE_NODE * BufferNode,ACPI_NAMESPACE_NODE * ResourceNode,UINT32 BitIndex)650 AcpiGetTagPathname (
651     ACPI_PARSE_OBJECT       *IndexOp,
652     ACPI_NAMESPACE_NODE     *BufferNode,
653     ACPI_NAMESPACE_NODE     *ResourceNode,
654     UINT32                  BitIndex)
655 {
656     ACPI_STATUS             Status;
657     UINT32                  ResourceBitIndex;
658     UINT8                   ResourceTableIndex;
659     ACPI_SIZE               RequiredSize;
660     char                    *Pathname;
661     char                    *PathnameEnd;
662     AML_RESOURCE            *Aml;
663     ACPI_PARSE_OBJECT       *Op;
664     char                    *InternalPath;
665     char                    *Tag;
666 
667 
668     /* Get the Op that contains the actual buffer data */
669 
670     Op = BufferNode->Op->Common.Value.Arg;
671     Op = Op->Common.Next;
672     if (!Op)
673     {
674         return (NULL);
675     }
676 
677     /* Get the individual resource descriptor and validate it */
678 
679     Aml = ACPI_CAST_PTR (
680         AML_RESOURCE, &Op->Named.Data[ResourceNode->Value]);
681 
682     Status = AcpiUtValidateResource (NULL, Aml, &ResourceTableIndex);
683     if (ACPI_FAILURE (Status))
684     {
685         return (NULL);
686     }
687 
688     /* Get offset into this descriptor (from offset into entire buffer) */
689 
690     ResourceBitIndex = BitIndex - ACPI_MUL_8 (ResourceNode->Value);
691 
692     /* Get the tag associated with this resource descriptor and offset */
693 
694     Tag = AcpiDmGetResourceTag (ResourceBitIndex, Aml, ResourceTableIndex);
695     if (!Tag)
696     {
697         return (NULL);
698     }
699 
700     /*
701      * Now that we know that we have a reference that can be converted to a
702      * symbol, change the name of the resource to a unique name.
703      */
704     AcpiDmUpdateResourceName (ResourceNode);
705 
706     /* Get the full pathname to the parent buffer */
707 
708     RequiredSize = AcpiNsBuildNormalizedPath (BufferNode, NULL, 0, FALSE);
709     if (!RequiredSize)
710     {
711         return (NULL);
712     }
713 
714     Pathname = ACPI_ALLOCATE_ZEROED (RequiredSize + ACPI_PATH_SEGMENT_LENGTH);
715     if (!Pathname)
716     {
717         return (NULL);
718     }
719 
720     (void) AcpiNsBuildNormalizedPath (BufferNode, Pathname,
721         RequiredSize, FALSE);
722 
723     /*
724      * Create the full path to the resource and tag by:
725      *  1) Remove the buffer nameseg from the end of the pathname
726      *  2) Append the resource descriptor nameseg
727      *  3) Append a dot
728      *  4) Append the field tag nameseg
729      *
730      * Always using the full path is a bit brute force, the path can be
731      * often be optimized with carats (if the original buffer namepath is a
732      * single nameseg). This doesn't really matter, because these paths do not
733      * end up in the final compiled AML, it's just an appearance issue for the
734      * disassembled code.
735      */
736     PathnameEnd = Pathname + (RequiredSize - ACPI_NAMESEG_SIZE - 1);
737     ACPI_COPY_NAMESEG (PathnameEnd, ResourceNode->Name.Ascii);
738 
739     PathnameEnd += ACPI_NAMESEG_SIZE;
740     *PathnameEnd = '.';
741 
742     PathnameEnd++;
743     ACPI_COPY_NAMESEG (PathnameEnd, Tag);
744 
745     /* Internalize the namepath to AML format */
746 
747     Status = AcpiNsInternalizeName (Pathname, &InternalPath);
748     ACPI_FREE (Pathname);
749     if (ACPI_FAILURE (Status))
750     {
751         return (NULL);
752     }
753 
754     /* Update the Op with the symbol */
755 
756     AcpiPsInitOp (IndexOp, AML_INT_NAMEPATH_OP);
757     IndexOp->Common.Value.String = InternalPath;
758 
759     /*
760      * We will need the tag later. Cheat by putting it in the Node field.
761      * Note, Tag is a const that is part of a lookup table.
762      */
763     IndexOp->Common.Node = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, Tag);
764     return (InternalPath);
765 }
766 
767 
768 /*******************************************************************************
769  *
770  * FUNCTION:    AcpiDmUpdateResourceName
771  *
772  * PARAMETERS:  ResourceNode        - Node for a resource descriptor
773  *
774  * RETURN:      Stores new name in the ResourceNode
775  *
776  * DESCRIPTION: Create a new, unique name for a resource descriptor. Used by
777  *              both the disassembly of the descriptor itself and any symbolic
778  *              references to the descriptor. Ignored if a unique name has
779  *              already been assigned to the resource.
780  *
781  * NOTE: Single threaded, suitable for applications only!
782  *
783  ******************************************************************************/
784 
785 static void
AcpiDmUpdateResourceName(ACPI_NAMESPACE_NODE * ResourceNode)786 AcpiDmUpdateResourceName (
787     ACPI_NAMESPACE_NODE     *ResourceNode)
788 {
789     char                    Name[ACPI_NAMESEG_SIZE];
790 
791 
792     /* Ignore if a unique name has already been assigned */
793 
794     if (ResourceNode->Name.Integer != ACPI_DEFAULT_RESNAME)
795     {
796         return;
797     }
798 
799     /* Generate a new ACPI name for the descriptor */
800 
801     Name[0] = '_';
802     Name[1] = AcpiGbl_Prefix[AcpiGbl_NextPrefix];
803     Name[2] = AcpiUtHexToAsciiChar ((UINT64) AcpiGbl_NextResourceId, 4);
804     Name[3] = AcpiUtHexToAsciiChar ((UINT64) AcpiGbl_NextResourceId, 0);
805 
806     /* Update globals for next name */
807 
808     AcpiGbl_NextResourceId++;
809     if (AcpiGbl_NextResourceId >= 256)
810     {
811         AcpiGbl_NextResourceId = 0;
812         AcpiGbl_NextPrefix++;
813 
814         if (AcpiGbl_NextPrefix > ACPI_NUM_RES_PREFIX)
815         {
816             AcpiGbl_NextPrefix = 0;
817         }
818     }
819 
820     /* Change the resource descriptor name */
821 
822     ResourceNode->Name.Integer = *ACPI_CAST_PTR (UINT32, &Name[0]);
823 }
824 
825 
826 /*******************************************************************************
827  *
828  * FUNCTION:    AcpiDmGetResourceTag
829  *
830  * PARAMETERS:  BitIndex            - Index into the resource descriptor
831  *              Resource            - Pointer to the raw resource data
832  *              ResourceIndex       - Index corresponding to the resource type
833  *
834  * RETURN:      Pointer to the resource tag (ACPI_NAME). NULL if no match.
835  *
836  * DESCRIPTION: Convert a BitIndex into a symbolic resource tag.
837  *
838  * Note: ResourceIndex should be previously validated and guaranteed to ve
839  *       valid.
840  *
841  ******************************************************************************/
842 
843 static char *
AcpiDmGetResourceTag(UINT32 BitIndex,AML_RESOURCE * Resource,UINT8 ResourceIndex)844 AcpiDmGetResourceTag (
845     UINT32                  BitIndex,
846     AML_RESOURCE            *Resource,
847     UINT8                   ResourceIndex)
848 {
849     const ACPI_RESOURCE_TAG *TagList;
850     char                    *Tag = NULL;
851 
852 
853     /* Get the tag list for this resource descriptor type */
854 
855     TagList = AcpiGbl_ResourceTags[ResourceIndex];
856 
857     /*
858      * Handle descriptors that have multiple subtypes
859      */
860     switch (Resource->DescriptorType)
861     {
862     case ACPI_RESOURCE_NAME_ADDRESS16:
863     case ACPI_RESOURCE_NAME_ADDRESS32:
864     case ACPI_RESOURCE_NAME_ADDRESS64:
865     case ACPI_RESOURCE_NAME_EXTENDED_ADDRESS64:
866         /*
867          * Subtype differentiation is the flags.
868          * Kindof brute force, but just blindly search for an index match
869          */
870         if (Resource->Address.ResourceType == ACPI_ADDRESS_TYPE_MEMORY_RANGE)
871         {
872             Tag = AcpiDmSearchTagList (BitIndex, AcpiDmMemoryFlagTags);
873         }
874         else if (Resource->Address.ResourceType == ACPI_ADDRESS_TYPE_IO_RANGE)
875         {
876             Tag = AcpiDmSearchTagList (BitIndex, AcpiDmIoFlagTags);
877         }
878 
879         /* If we found a match, all done. Else, drop to normal search below */
880 
881         if (Tag)
882         {
883             return (Tag);
884         }
885         break;
886 
887     case ACPI_RESOURCE_NAME_GPIO:
888 
889         /* GPIO connection has 2 subtypes: Interrupt and I/O */
890 
891         if (Resource->Gpio.ConnectionType > AML_RESOURCE_MAX_GPIOTYPE)
892         {
893             return (NULL);
894         }
895 
896         TagList = AcpiGbl_GpioResourceTags[Resource->Gpio.ConnectionType];
897         break;
898 
899     case ACPI_RESOURCE_NAME_SERIAL_BUS:
900 
901         /* SerialBus has 4 subtypes: I2C, SPI, UART, and CSI2 */
902 
903         if ((Resource->CommonSerialBus.Type == 0) ||
904             (Resource->CommonSerialBus.Type > AML_RESOURCE_MAX_SERIALBUSTYPE))
905         {
906             return (NULL);
907         }
908 
909         TagList = AcpiGbl_SerialResourceTags[Resource->CommonSerialBus.Type];
910         break;
911 
912     default:
913 
914         break;
915     }
916 
917     /* Search for a match against the BitIndex */
918 
919     if (TagList)
920     {
921         Tag = AcpiDmSearchTagList (BitIndex, TagList);
922     }
923 
924     return (Tag);
925 }
926 
927 
928 /*******************************************************************************
929  *
930  * FUNCTION:    AcpiDmSearchTagList
931  *
932  * PARAMETERS:  BitIndex            - Index into the resource descriptor
933  *              TagList             - List to search
934  *
935  * RETURN:      Pointer to a tag (ACPI_NAME). NULL if no match found.
936  *
937  * DESCRIPTION: Search a tag list for a match to the input BitIndex. Matches
938  *              a fixed offset to a symbolic resource tag name.
939  *
940  ******************************************************************************/
941 
942 static char *
AcpiDmSearchTagList(UINT32 BitIndex,const ACPI_RESOURCE_TAG * TagList)943 AcpiDmSearchTagList (
944     UINT32                  BitIndex,
945     const ACPI_RESOURCE_TAG *TagList)
946 {
947 
948     /*
949      * Walk the null-terminated tag list to find a matching bit offset.
950      * We are looking for an exact match.
951      */
952     for ( ; TagList->Tag; TagList++)
953     {
954         if (BitIndex == TagList->BitIndex)
955         {
956             return (TagList->Tag);
957         }
958     }
959 
960     /* A matching offset was not found */
961 
962     return (NULL);
963 }
964 
965 
966 /*******************************************************************************
967  *
968  * FUNCTION:    AcpiDmFindResources
969  *
970  * PARAMETERS:  Root                - Root of the parse tree
971  *
972  * RETURN:      None
973  *
974  * DESCRIPTION: Add all ResourceTemplate declarations to the namespace. Each
975  *              resource descriptor in each template is given a node -- used
976  *              for later conversion of resource references to symbolic refs.
977  *
978  ******************************************************************************/
979 
980 void
AcpiDmFindResources(ACPI_PARSE_OBJECT * Root)981 AcpiDmFindResources (
982     ACPI_PARSE_OBJECT       *Root)
983 {
984     ACPI_PARSE_OBJECT       *Op = Root;
985     ACPI_PARSE_OBJECT       *Parent;
986 
987 
988     /* Walk the entire parse tree */
989 
990     while (Op)
991     {
992         /* We are interested in Buffer() declarations */
993 
994         if (Op->Common.AmlOpcode == AML_BUFFER_OP)
995         {
996             /* And only declarations of the form Name (XXXX, Buffer()... ) */
997 
998             Parent = Op->Common.Parent;
999             if (Parent->Common.AmlOpcode == AML_NAME_OP)
1000             {
1001                 /*
1002                  * If the buffer is a resource template, add the individual
1003                  * resource descriptors to the namespace, as children of the
1004                  * buffer node.
1005                  */
1006                 if (ACPI_SUCCESS (AcpiDmIsResourceTemplate (NULL, Op)))
1007                 {
1008                     Op->Common.DisasmOpcode = ACPI_DASM_RESOURCE;
1009                     AcpiDmAddResourcesToNamespace (Parent->Common.Node, Op);
1010                 }
1011             }
1012         }
1013 
1014         Op = AcpiPsGetDepthNext (Root, Op);
1015     }
1016 }
1017 
1018 
1019 /*******************************************************************************
1020  *
1021  * FUNCTION:    AcpiDmAddResourcesToNamespace
1022  *
1023  * PARAMETERS:  BufferNode          - Node for the parent buffer
1024  *              Op                  - Parse op for the buffer
1025  *
1026  * RETURN:      None
1027  *
1028  * DESCRIPTION: Add an entire resource template to the namespace. Each
1029  *              resource descriptor is added as a namespace node.
1030  *
1031  ******************************************************************************/
1032 
1033 static void
AcpiDmAddResourcesToNamespace(ACPI_NAMESPACE_NODE * BufferNode,ACPI_PARSE_OBJECT * Op)1034 AcpiDmAddResourcesToNamespace (
1035     ACPI_NAMESPACE_NODE     *BufferNode,
1036     ACPI_PARSE_OBJECT       *Op)
1037 {
1038     ACPI_PARSE_OBJECT       *NextOp;
1039 
1040 
1041     /* Get to the ByteData list */
1042 
1043     NextOp = Op->Common.Value.Arg;
1044     NextOp = NextOp->Common.Next;
1045     if (!NextOp)
1046     {
1047         return;
1048     }
1049 
1050     /* Set Node and Op to point to each other */
1051 
1052     BufferNode->Op = Op;
1053     Op->Common.Node = BufferNode;
1054 
1055     /*
1056      * Insert each resource into the namespace
1057      * NextOp contains the Aml pointer and the Aml length
1058      */
1059     AcpiUtWalkAmlResources (NULL, (UINT8 *) NextOp->Named.Data,
1060         (ACPI_SIZE) NextOp->Common.Value.Integer,
1061         AcpiDmAddResourceToNamespace, (void **) BufferNode);
1062 }
1063 
1064 
1065 /*******************************************************************************
1066  *
1067  * FUNCTION:    AcpiDmAddResourceToNamespace
1068  *
1069  * PARAMETERS:  ACPI_WALK_AML_CALLBACK
1070  *              BufferNode              - Node for the parent buffer
1071  *
1072  * RETURN:      Status
1073  *
1074  * DESCRIPTION: Add one resource descriptor to the namespace as a child of the
1075  *              parent buffer. The same name is used for each descriptor. This
1076  *              is changed later to a unique name if the resource is actually
1077  *              referenced by an AML operator.
1078  *
1079  ******************************************************************************/
1080 
1081 static ACPI_STATUS
AcpiDmAddResourceToNamespace(UINT8 * Aml,UINT32 Length,UINT32 Offset,UINT8 ResourceIndex,void ** Context)1082 AcpiDmAddResourceToNamespace (
1083     UINT8                   *Aml,
1084     UINT32                  Length,
1085     UINT32                  Offset,
1086     UINT8                   ResourceIndex,
1087     void                    **Context)
1088 {
1089     ACPI_STATUS             Status;
1090     ACPI_GENERIC_STATE      ScopeInfo;
1091     ACPI_NAMESPACE_NODE     *Node;
1092 
1093 
1094     /* TBD: Don't need to add descriptors that have no tags defined? */
1095 
1096     /* Add the resource to the namespace, as child of the buffer */
1097 
1098     ScopeInfo.Scope.Node = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, Context);
1099     Status = AcpiNsLookup (&ScopeInfo, "_TMP", ACPI_TYPE_LOCAL_RESOURCE,
1100         ACPI_IMODE_LOAD_PASS2,
1101         ACPI_NS_NO_UPSEARCH | ACPI_NS_DONT_OPEN_SCOPE | ACPI_NS_PREFIX_IS_SCOPE,
1102         NULL, &Node);
1103     if (ACPI_FAILURE (Status))
1104     {
1105         return (AE_OK);
1106     }
1107 
1108     /* Set the name to the default, changed later if resource is referenced */
1109 
1110     Node->Name.Integer = ACPI_DEFAULT_RESNAME;
1111 
1112     /* Save the offset of the descriptor (within the original buffer) */
1113 
1114     Node->Value = Offset;
1115     Node->Length = Length;
1116     return (AE_OK);
1117 }
1118